Jump to content

[1.8] How do we use ISmartItemModel to create dynamic models?


TrashCaster

Recommended Posts

What I want to do is render my item as a series of mini-blocks stored in the item's NBT data.

 

I'm used to doing this via IItemRenderer, and seeing as how that's deprecated, I've been guided into using ISmartItemModel. Problem is, I don't know where to start.

 

Has anyone delved into this?

 

How would I go about doing this?

 

I'm trying to use SimpleBakedModel's builder, and don't really know where to go from there.

 

I have no code to show, because I don't know what I'm doing to begin with. Just being honest here.

 

All I've done is create empty fields to see what methods are what, and what parameters they can take, so that I could have a general idea of a starting ground, but would rather not waste my time banging out code that's wrong or won't compile or run.

 

Help me, without spoon-feeding complete code? I'd like to learn the new way of dynamic model generation.

Link to comment
Share on other sites

Hi

 

The key bit to understand is that ISmartItemModel lets you choose which IBakedModel to return, based on the itemstack given to its method

 

    IBakedModel handleItemState(ItemStack stack);

 

So- you can create as many SimpleBakedModels as you like - and pick which one to return.  I'd suggest you create a variant for each one just like you would normally register them and just return the variant by name.

 

This example 12 does something very similar using the Item.getModel() method instead of handleItemState().

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe12_item_nbt_animate

 

Alternatively, if you implement IBakedModel with your own class, you can change which faces are returned by getFaceQuads() and getGeneralQuads() depending on what your handleItemState has told it to.

 

For example (won't compile but you get the idea?)

    IBakedModel handleItemState(ItemStack stack) {

if (stack.stackSize == 1) return new MyBakedModel(SINGLE_FACE);
if (stack.stackSize == 2) return new MyBakedModel(TWO_FACES);
return new MyBakedModel(MANY_FACES);

}
where
class MyBakedModel implements IBakedModel {

  private FaceCount facecount;
  public MyBakedModel(FaceCount i_facecount) {
     facecount = i_facecount;
  }

  public List getFaceQuads(EnumFacing p_177551_1_)
    {
        List allfaces = new List();
        allfaces.add(FIRST_BAKED_QUAD);
        if (facecount == SINGLE_FACE) return allFaces;
        allfaces.add(SECOND_BAKED_QUAD);
        if (facecount == TWO_FACES) return allFaces;
        allfaces.add(THIRD_BAKED_QUAD);
       return allFaces;
    }
}

 

In your specific case, you might implement an IBakedModel that contains a number of mini-blocks and the getFaceQuads method compiles a list from all the miniblocks listed one after the other.

 

-TGG

 

 

Link to comment
Share on other sites

Alright, and is there a function that gets the block models of said blocks?

 

I'm thinking getItemModel() in the ItemMesher, but I don't know if that will work. If it's wrong, where do I start with that?

 

Also, when I return the TextureAtlasSprite, how will I compile all the textures of all the blocks into one? And if I do so, will all the block faces grab the right textures, or do I have to re-uv map them?

Link to comment
Share on other sites

Vanilla uses

BlockRendererDispatcher.getModelFromBlockState()

you can get BlockRendererDispatcher from

Minecraft.getMinecraft.getBlockRendererDispatcher()

 

The TextureAtlasSprite is just used for the block breaking texture I think.

 

I think that if your block models are registered in the normal way, the uv mappings will all be correct because the textures are automatically stitched together into a single sheet and the BlockModel already got its own uv when it was baked.

I'm not sure of that - try it and let us know?

 

-TGG

Link to comment
Share on other sites

What parameters would I be passing in for the IBlockState, IBlockAccess, and BlockPos? Cause it's not physical blocks in the world, it's just ItemStacks stored in NBT. Is it safe to pass null for the last two? This is why I thought getItemModel would work cause I will be using item stacks.

Link to comment
Share on other sites

Hi

 

well, I _think_ nulls for world and blockPos are probably ok, because of this in getModelFromBlockState:

 

            try
            {
                p_175022_1_ = block.getActualState(p_175022_1_, p_175022_2_, p_175022_3_);
            }
            catch (Exception exception)
            {
                ;
            }

but I reckon it would be better to bypass it properly, using just 

IBakedModel ibakedmodel = blockModelShapes.getModelForState(blockState);

 

You can get blockModelShapes from BlockRendererDispatcher using the corresponding get method.

 

-TGG

 

Link to comment
Share on other sites

See, the thing is, is I have no block states since the blocks aren't real. They are only ItemStacks.

 

Should I just fetch the default states from the blocks?

Ah. I think I've misunderstood what you want.  I reckon your thought about getItemModel() in the ItemMesher is right, i.e.:

 

If your ItemStack has an NBT with (say) two ItemStacks "A" and "B" in it, you would get the vanilla IBakedModels for those items using

 

IBakedModel ibakedmodel = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getItemModel(itemStackA);

and same for B.

 

The only trick is - if the Camera Transforms aren't the same for both items, you will have to choose one of the two...

 

-TGG

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I made a block entity in forge 1.20.1, I want to prevent hopper from taking input slot item, i tried to override the extractItem method, it prevented hopper from taking input slot item, but the player also unable to take/change the item in input slot unless the slot is empty. public class FluidSeparatorBlockEntity extends BlockEntity implements MenuProvider { private static final int INPUT_SLOT = 0; private final CustomItemHandler itemHandler = new CustomItemHandler(3){ @Override protected void onContentsChanged(int slot) { setChanged(); } @Override public boolean isItemValid(int slot, @NotNull ItemStack stack) { return slot == INPUT_SLOT; } @Override public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) { if (slot == INPUT_SLOT) { return ItemStack.EMPTY; } return super.extractItem(slot, amount, simulate); } }; private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty(); protected final ContainerData data; private int progress = 0; private int maxProgress = 78; public FluidSeparatorBlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.FLUID_SEPARATOR_BE.get(), pPos, pBlockState); this.data = new ContainerData() { @Override public int get(int pIndex) { return switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress; default -> 0; }; } @Override public void set(int pIndex, int pValue) { switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress = pValue; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress = pValue; } } @Override public int getCount() { return 3; } }; } @Override public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { if(cap == ForgeCapabilities.ITEM_HANDLER) { return lazyItemHandler.cast(); } return super.getCapability(cap, side); } @Override public void onLoad() { super.onLoad(); lazyItemHandler = LazyOptional.of(() -> itemHandler); } @Override public void invalidateCaps() { super.invalidateCaps(); lazyItemHandler.invalidate(); } public void drops() { SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots()); for(int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, itemHandler.getStackInSlot(i)); } Containers.dropContents(this.level, this.worldPosition, inventory); } @Override public Component getDisplayName() { return Component.translatable("block.chemmaster.fluid_separator"); } @Nullable @Override public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { return new FluidSeparatorMenu(pContainerId, pPlayerInventory, this, this.data); } @Override protected void saveAdditional(CompoundTag pTag) { pTag.put("inventory", itemHandler.serializeNBT()); pTag.putInt("fluid_separator.progress", progress); super.saveAdditional(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); itemHandler.deserializeNBT(pTag.getCompound("inventory")); progress = pTag.getInt("fluid_separator.progress"); } public void tick(Level pLevel, BlockPos pPos, BlockState pState) { ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { resetProgress(); return; } if(hasRecipe()) { increaseCraftingProgress(); setChanged(pLevel, pPos, pState); if(hasProgressFinished()) { craftItem(); resetProgress(); } } else { resetProgress(); } } private void resetProgress() { progress = 0; } private void craftItem() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isPresent()) { List<ItemStack> results = recipe.get().getOutputs(); ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { // If there are not enough items, do not proceed with crafting return; } // Extract the input item from the input slot this.itemHandler.internalExtractItem(INPUT_SLOT, 2, false); // Loop through each result item and find suitable output slots for (ItemStack result : results) { int outputSlot = findSuitableOutputSlot(result); if (outputSlot != -1) { this.itemHandler.setStackInSlot(outputSlot, new ItemStack(result.getItem(), this.itemHandler.getStackInSlot(outputSlot).getCount() + result.getCount())); } else { // Handle the case where no suitable output slot is found // This can be logging an error, throwing an exception, or any other handling logic System.err.println("No suitable output slot found for item: " + result); } } } } private int findSuitableOutputSlot(ItemStack result) { // Implement logic to find a suitable output slot for the given result // Return the slot index or -1 if no suitable slot is found for (int i = 0; i < this.itemHandler.getSlots(); i++) { // Ensure we do not place the output item in the input slot if (i == INPUT_SLOT) { continue; } ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return i; } } return -1; } private boolean hasRecipe() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isEmpty()) { return false; } List<ItemStack> results = recipe.get().getOutputs(); for (ItemStack result : results) { if (!canInsertAmountIntoOutputSlot(result) || !canInsertItemIntoOutputSlot(result.getItem())) { return false; } } return true; } private Optional<FluidSeparatingRecipe> getCurrentRecipe(){ SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots()); for (int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, this.itemHandler.getStackInSlot(i)); } return this.level.getRecipeManager().getRecipeFor(FluidSeparatingRecipe.Type.INSTANCE, inventory, level); } private boolean canInsertAmountIntoOutputSlot(ItemStack result) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return true; } } return false; } private boolean canInsertItemIntoOutputSlot(Item item) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || stackInSlot.getItem() == item) { return true; } } return false; } private boolean hasProgressFinished() { return progress >= maxProgress; } private void increaseCraftingProgress() { progress++; } }  
    • No dice. Unfortunately this fix didn't work, thank you though.
    • Maybe you need a rayon mod. https://www.curseforge.com/minecraft/mc-mods/rayon
    • Not sure what's going on the logs are making even less sense than usual to me. Any help would be much appreciated.   https://paste.ee/p/KBHyP#s=0
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.