Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Chests and getStackInSlot [Solved]


Varogh
 Share

Recommended Posts

I can't get the getStackInSlot function to work on a chest.

I even tried to copy the code of hoppers but it's still not working.

 

This is the code to get the inventory:

 

      public void updateEntity() {
        	TileEntity inpTile = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord+1);
        	if ((inpTile != null) && (inpTile instanceof IInventory)){
        		IInventory inpInventory = (IInventory) inpTile;
        		
        		
                if (inpInventory instanceof TileEntityChest)
                {
                    int l = this.getWorldObj().getBlockId(xCoord, yCoord, zCoord+1);
                    Block block = Block.blocksList[l];

                    if (block instanceof BlockChest)
                    {
                    	inpInventory = ((BlockChest)block).getInventory(this.getWorldObj(), xCoord, yCoord, zCoord+1);
                    }
                }
        		
        		
        		
        		for (int i=0; i<inpInventory.getSizeInventory();i++){
        			if (inpInventory.getStackInSlot(i)!=null){
        				ItemStack incomingStack = inpInventory.getStackInSlot(i); //always returns null
        				//stub
        				}
        			}
        		}
        	}
        

 

The problem is, from what I can see from the debug, that inpInventory.getStackInSlot(i) always returns null.

inpInventory and inpTile aren't null and are instance of TileEntityChest, and inpInventory.getSizeInventory() correctly returns 27.

Could you tell me what's wrong?

Link to comment
Share on other sites

They would be both xCoord, yCoord, zCoord+1 , I changed that in the code I posted to make it more clear that those were the coordinates I'm getting the chest from, but I forgot to change them in the getInventory method.

Anyway, they're the same in my code, so that's not the problem.

Link to comment
Share on other sites

        public void updateEntity() {
        	TileEntity inpTile = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord+1);
        	if ((inpTile != null) && (inpTile instanceof IInventory)){
        		IInventory inpInventory = (IInventory) inpTile;
        		
        		
                if (inpInventory instanceof TileEntityChest)
                {
                    int l = this.getWorldObj().getBlockId(xCoord, yCoord, zCoord+1);
                    Block block = Block.blocksList[l];

                    if (block instanceof BlockChest)
                    {
                    	inpInventory = ((BlockChest)block).getInventory(this.getWorldObj(), xCoord, yCoord, zCoord+1);
                    }
                }
        		
        		
        		
        		for (int i=0; i<inpInventory.getSizeInventory();i++){
        			if (inpInventory.getStackInSlot(i)!=null){
        				ItemStack incomingStack = inpInventory.getStackInSlot(i);
        				//stub
        				}
        			}
        		}
        	}
        

 

But I can't really see what you would need to know why zCoord+1, since it has absolutely nothing to do with my problem.

 

PS: before you ask, yes, there is a chest at (xCoord, yCoord, zCoord + 1), and the tileentity returned by getBlockTileEntity is not null, it's a TileEntityChest instance.

Link to comment
Share on other sites

I didn't mean to be rude about it, but you've got a lot of wonky stuff going on in your code. For instance, when you get the TileEntityChest, it is already an IInventory, meaning you should have access right away; why are you doing all this other weird stuff getting the block and then getting the block's inventory, when you already have it?

 

If you're absolutely 100% sure that the IInventory you want is at zCoord+1, that's fine, but it seems off to me. This code is in a TileEntity already, right? But you don't want this TileEntity's inventory, you want a different one? Ok. Still:

TileEntity te = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord + 1); // going along with you here
if (te instanceof IInventory) {
// now you've got an inventory
IInventory inv = (IInventory) te;
for (int i = 0; i < inv.getSizeInventory(); ++i) {
// etc.
}
}

Should be as simple as that. If it's not working, try putting println's all over the place, if you haven't already, to make sure it's getting called and that the variables are what you think they are.

Link to comment
Share on other sites

My objective is to make a machine that gets a chest's inventory and puts it into its own inventory, that's why I'm calling to another inventory.

 

The code you wrote is identical to the code I had before looking at the TileEntityHopper class, and it's not working, so I looked how the hopper did that, and I saw it has special code for chests, so I copied the extra bit, but it's still not working.

I followed the code step by step for both my TE and the Hopper, and the only difference seems to be that, when I call getStackInSlot I get null, while the hopper gets the itemStack, as it should

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Got the rotation working. I had to look at LivingEntityRenderer#render and setupRotations a little more closely, and I realized that I needed to change the custom entity's yBodyRot and yBodyRotO.  private static boolean renderingCustomEntity = false; private static CustomEntity customEntity; private static void refreshCustomEntity(LocalPlayer localPlayer, LivingEntity livingEntity) { if (customEntity == null || customEntity.isRemoved()) { customEntity = ModEntities.CUSTOM_ENTITY.get().create(localPlayer.clientLevel); } if (!customEntity.level.dimension().equals(livingEntity.level.dimension())) { customEntity.discard(); } } @SubscribeEvent public static void replaceMobModel(RenderLivingEvent.Pre<LivingEntity, EntityModel<LivingEntity>> event) { LocalPlayer localPlayer = Minecraft.getInstance().player; if (localPlayer != null && event.getEntity() instanceof Mob mob) { if (/* put your own condition here */) { if (!renderingCustomEntity) { try { renderingCustomEntity = true; event.setCanceled(true); refreshCustomEntity(localPlayer, mob); // (truth be told, my custom entity does not have a "head", so these lines aren't needed) // customEntity.setYHeadRot(mob.getYHeadRot()); // customEntity.yHeadRotO = mob.yHeadRotO; customEntity.setYBodyRot(mob.yBodyRot); customEntity.yBodyRotO = mob.yBodyRotO; customEntity.setPose(mob.getPose()); Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(customEntity).render( customEntity, customEntity.yBodyRot, event.getPartialTick(), event.getPoseStack(), event.getMultiBufferSource(), event.getPackedLight() ); } finally { renderingCustomEntity = false; } } } } }   As shown in my code, if the mob whose model is replaced moves to a different dimension, I unload the custom entity with Entity#discard and then reinitialize it on the next tick. I wonder if I also have to unload the custom entity when the mob it is "replacing" dies.
    • Thank you for the video, but I found the issue. I had to update the Gradle from 7.2 to 7.3 in gradle/wrapper/gradle-wrapper.properties.
    • Some zip managers like to take control of the .jar file extension away from Java. Make sure you have Java installed and try running Jarfix once, then try the installer again.
    • Whenever i try and download forge specifically 1.18.2 it will download as a JAR file as a note page and theirs no way to install it i have watched a few videos but none show how to fix this problem
    • Hello, I dont really understand why the nbt tag in the code below is not attached to the item. @Override public InteractionResult interactLivingEntity(ItemStack pStack, Player pPlayer, LivingEntity pInteractionTarget, InteractionHand pUsedHand) { if (pPlayer.getLevel().isClientSide()) { if (pInteractionTarget instanceof Mob mob) { System.out.println("ITEMSTACK " + pStack); CompoundTag nbtTag = null; System.out.println("nbt tag " + nbtTag); if (!(pStack.hasTag())) { nbtTag = new CompoundTag(); CompoundTag nbtMobTag = new CompoundTag(); mob.save(nbtMobTag); nbtTag.put("mob", nbtMobTag); System.out.println("IN NBT NULL " + nbtTag); pStack.setTag(nbtTag); System.out.println("AFTER TAG SET " + pStack.hasTag()); pPlayer.sendMessage(new TextComponent("Shard used on " + mob.getDisplayName().getString()), pPlayer.getUUID()); } else { nbtTag = pStack.getTag(); System.out.println("item has tag"); System.out.println(nbtTag.contains("mob")); } } } return InteractionResult.PASS; } @Override public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) { CompoundTag nbtTag = pStack.getTag(); if (nbtTag != null) { if (nbtTag.contains("mob")) { Mob targetMob = null; targetMob.deserializeNBT(nbtTag.getCompound("mob")); pTooltipComponents.add(new TextComponent("Mob : " + targetMob.getDisplayName().getString())); } } } Everytime I right click with the Item on a LivingEntity, It goes on the if(!pStack.hasTag()), but It shouldn't because we set the tag on the first right click on an Entity   Do you have a solution ?
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.