Jump to content

[1.12.2] TileEntity Container crashing when placed in world.


rafacost

Recommended Posts

Hey guys!

Trying to create a small container that keeps inventory when broke.

When I place the container block in the world it crashes the game.

When I restart the game the block is placed and working as planned.

If I break this block and place it again it keeps the inventory.

The problem seems to be when the block doesn't have an NBT or TE associated with it.

But I can't pinpoint where is the problem because the crashlog doesn't shows nothing about my code.

Could you please take a look?

Crash Log

Spoiler

[22:31:08] [Server thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_73]
    at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_73]
    at net.minecraft.util.Util.runTask(Util.java:54) [Util.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:796) [MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741) [MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590) [MinecraftServer.class:?]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_73]
Caused by: java.lang.NullPointerException
    at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:558) ~[NBTTagCompound.class:?]
    at net.minecraftforge.common.ForgeHooks.onPlaceItemIntoWorld(ForgeHooks.java:871) ~[ForgeHooks.class:?]
    at net.minecraft.item.ItemStack.onItemUse(ItemStack.java:200) ~[ItemStack.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.processRightClickBlock(PlayerInteractionManager.java:515) ~[PlayerInteractionManager.class:?]
    at net.minecraft.network.NetHandlerPlayServer.processTryUseItemOnBlock(NetHandlerPlayServer.java:767) ~[NetHandlerPlayServer.class:?]
    at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:68) ~[CPacketPlayerTryUseItemOnBlock.class:?]
    at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:13) ~[CPacketPlayerTryUseItemOnBlock.class:?]
    at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) ~[PacketThreadUtil$1.class:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_73]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_73]
    at net.minecraft.util.Util.runTask(Util.java:53) ~[Util.class:?]
    ... 5 more
[22:31:08] [Server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking player
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:212) ~[NetworkSystem.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:863) ~[MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741) ~[MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) ~[IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590) [MinecraftServer.class:?]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_73]
Caused by: java.lang.NullPointerException
    at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:558) ~[NBTTagCompound.class:?]
    at net.minecraft.item.ItemStack.copy(ItemStack.java:461) ~[ItemStack.class:?]
    at net.minecraft.network.play.server.SPacketEntityEquipment.<init>(SPacketEntityEquipment.java:26) ~[SPacketEntityEquipment.class:?]
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2356) ~[EntityLivingBase.class:?]
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:272) ~[EntityPlayer.class:?]
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:423) ~[EntityPlayerMP.class:?]
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:185) ~[NetHandlerPlayServer.class:?]
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:212) ~[NetworkDispatcher$1.class:?]
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:307) ~[NetworkManager.class:?]
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197) ~[NetworkSystem.class:?]
    ... 5 more
[22:31:08] [Server thread/ERROR]: This crash report has been saved to: C:\MinecraftMods\bbs_mod\run\.\crash-reports\crash-2018-03-16_22.31.08-server.txt
[22:31:08] [Server thread/INFO]: Stopping server
[22:31:08] [Server thread/INFO]: Saving players
[22:31:08] [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
// Don't do that.

Time: 3/16/18 10:31 PM
Description: Ticking player

java.lang.NullPointerException: Ticking player
    at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:558)
    at net.minecraft.item.ItemStack.copy(ItemStack.java:461)
    at net.minecraft.network.play.server.SPacketEntityEquipment.<init>(SPacketEntityEquipment.java:26)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2356)
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:272)
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:423)
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:185)
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:212)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:307)
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197)
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:863)
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741)
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590)
    at java.lang.Thread.run(Thread.java:745)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
    at net.minecraft.nbt.NBTTagCompound.copy(NBTTagCompound.java:558)
    at net.minecraft.item.ItemStack.copy(ItemStack.java:461)
    at net.minecraft.network.play.server.SPacketEntityEquipment.<init>(SPacketEntityEquipment.java:26)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2356)
    at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:272)

-- Player being ticked --
Details:
    Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
    Entity ID: 64
    Entity Name: Player932
    Entity's Exact location: -136.22, 4.00, -161.36
    Entity's Block location: World: (-137,4,-162), Chunk: (at 7,0,14 in -9,-11; contains blocks -144,0,-176 to -129,255,-161), Region: (-1,-1; contains chunks -32,-32 to -1,-1, blocks -512,0,-512 to -1,255,-1)
    Entity's Momentum: 0.00, -0.08, 0.00
    Entity's Passengers: []
    Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
    at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:423)
    at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:185)
    at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:212)
    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:307)

-- Ticking connection --
Details:
    Connection: net.minecraft.network.NetworkManager@2a5fa956
Stacktrace:
    at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197)
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:863)
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741)
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590)
    at java.lang.Thread.run(Thread.java:745)

-- System Details --
Details:
    Minecraft Version: 1.12.2
    Operating System: Windows 10 (amd64) version 10.0
    Java Version: 1.8.0_73, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 511295384 bytes (487 MB) / 1064304640 bytes (1015 MB) up to 1778384896 bytes (1696 MB)
    JVM Flags: 0 total; 
    IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
    FML: MCP 9.42 Powered by Forge 14.23.2.2611 6 mods loaded, 6 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

    | State     | ID        | Version      | Source                           | Signature |
    |:--------- |:--------- |:------------ |:-------------------------------- |:--------- |
    | UCHIJAAAA | minecraft | 1.12.2       | minecraft.jar                    | None      |
    | UCHIJAAAA | mcp       | 9.42         | minecraft.jar                    | None      |
    | UCHIJAAAA | FML       | 8.0.99.99    | forgeSrc-1.12.2-14.23.2.2611.jar | None      |
    | UCHIJAAAA | forge     | 14.23.2.2611 | forgeSrc-1.12.2-14.23.2.2611.jar | None      |
    | UCHIJAAAA | bbs_mod   | ${version}   | bbs_mod_main                     | None      |
    | UCHIJAAAA | jei       | 4.8.5.147    | jei_1.12.2-4.8.5.147.jar         | None      |

    Loaded coremods (and transformers): 
    GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread.
    Profiler Position: N/A (disabled)
    Player Count: 1 / 8; [EntityPlayerMP['Player932'/64, l='New World', x=-136.22, y=4.00, z=-161.36]]
    Type: Integrated Server (map_client.txt)
    Is Modded: Definitely; Client brand changed to 'fml,forge'
[22:31:08] [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: #@!@# Game crashed! Crash report saved to: #@!@# .\crash-reports\crash-2018-03-16_22.31.08-server.txt
[22:31:08] [main/INFO] [FML]: Waiting for the server to terminate/save.
[22:31:08] [Server thread/INFO]: Saving worlds
[22:31:08] [Server thread/INFO]: Saving chunks for level 'New World'/overworld
[22:31:09] [Server thread/INFO]: Saving chunks for level 'New World'/the_nether
[22:31:09] [Server thread/INFO]: Saving chunks for level 'New World'/the_end
[22:31:09] [Server thread/INFO] [FML]: Unloading dimension 0
[22:31:09] [Server thread/INFO] [FML]: Unloading dimension -1
[22:31:09] [Server thread/INFO] [FML]: Unloading dimension 1
[22:31:09] [Server thread/INFO] [FML]: Applying holder lookups
[22:31:09] [Server thread/INFO] [FML]: Holder lookups applied
[22:31:09] [Server thread/INFO] [FML]: The state engine was in incorrect state SERVER_STOPPING and forced into state SERVER_STOPPED. Errors may have been discarded.
[22:31:09] [main/INFO] [FML]: Server terminated.
[22:31:09] [Client Shutdown Thread/INFO]: Stopping server
AL lib: (EE) alc_cleanup: 1 device not closed

Process finished with exit code -1

 

Block Code:

Spoiler

public class MicroPackBlock extends Block {
    public ItemStackHandler stackPlacer = null;
    public MicroPackBlock(){
        super(Material.GLASS);
        setUnlocalizedName(Reference.MODID + ".micropack");
        setSoundType(SoundType.GLASS);
        setRegistryName("micropack");
    }

    @Override
    public int quantityDropped(Random random) {
        return 0;
    }

    @SideOnly(Side.CLIENT)
    public void initModel(){
        ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
    }

    @Override
    @Nullable
    public TileEntity createTileEntity(World world, IBlockState state) {
        return new MicroPackTileEntity();
    }

    @Override
    public boolean hasTileEntity(IBlockState state) {
        return true;
    }

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        if(worldIn.isRemote){
            return true;
        }
        TileEntity te = worldIn.getTileEntity(pos);
        if (!(te instanceof MicroPackTileEntity)) {
            return false;
        }
        playerIn.openGui(BBSMod.instance, Reference.GUI_MICROPACK, worldIn,pos.getX(), pos.getY(), pos.getZ());

        return true;
    }

    @Override
    public EnumBlockRenderType getRenderType(IBlockState state) {
        return EnumBlockRenderType.MODEL;
    }

    @SideOnly(Side.CLIENT)
    public BlockRenderLayer getBlockLayer() {
        return BlockRenderLayer.CUTOUT;
    }

    @Override
    public boolean isFullCube(IBlockState state) {
        return false;
    }

    @Override
    public boolean isOpaqueCube(IBlockState state) {
        return false;
    }

    @SideOnly(Side.CLIENT)
    @Override
    public boolean shouldSideBeRendered(final IBlockState bs, final IBlockAccess world, final BlockPos coord, final EnumFacing face) {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
        tooltip.add("Keeps inventory when broken");
    }


    @Override
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
        TileEntity tee = worldIn.getTileEntity(pos);
        MicroPackTileEntity tileEntity = (MicroPackTileEntity)tee;
        ItemStack itemStack = new ItemStack(Item.getItemFromBlock(this));
        NBTTagCompound nbtTagCompound = new NBTTagCompound();
        nbtTagCompound.setTag("items", tileEntity.itemStackHandler.serializeNBT());
        itemStack.setTagCompound(nbtTagCompound);
        spawnAsEntity(worldIn,pos,itemStack);
        //BBSMod.logger.info(itemStack.getTagCompound().getTag("items"));
        super.breakBlock(worldIn,pos,state);
    }

    @Override
    public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
        MicroPackTileEntity te = (MicroPackTileEntity) world.getTileEntity(pos);
        ItemStack itemStack = new ItemStack(Item.getItemFromBlock(this));
        NBTTagCompound tag = new NBTTagCompound();
        tag.setTag("items", te.itemStackHandler.serializeNBT());
        itemStack.setTagCompound(tag);
        return itemStack;
    }


    @Override
    public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player,
                                   boolean willHarvest) {
        if (willHarvest)
            return true;
        return super.removedByPlayer(state, world, pos, player, willHarvest);
    }

    @Override
    public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te,
                             ItemStack tool) {
        super.harvestBlock(world, player, pos, state, te, tool);
        world.setBlockToAir(pos);
    }

    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        MicroPackTileEntity te = (MicroPackTileEntity)worldIn.getTileEntity(pos);
        stackPlacer = te.itemStackHandler;
        if(!stack.hasTagCompound()){
            NBTTagCompound tag = new NBTTagCompound();
            tag.setTag("items", null);
            stack.setTagCompound(tag);
        } else {
            NBTTagCompound data = (NBTTagCompound) stack.getTagCompound().getTag("items");
            ItemStackHandler handler = new ItemStackHandler(9);
            handler.deserializeNBT(data);
            for (int i = 0; i < 9; i++) {
                te.itemStackHandler.setStackInSlot(i, handler.getStackInSlot(i));
            }
        }
        super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
    }
}
 

Tile Entity Code:

Spoiler

public class MicroPackTileEntity extends TileEntity {
    public static final int SIZE = 9;

    public ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE){
        @Override
        protected void onContentsChanged(int slot) {
            MicroPackTileEntity.this.markDirty();
        }
    };

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        if(compound.hasKey("items")) {
            itemStackHandler.deserializeNBT((NBTTagCompound)compound.getTag("items"));
        }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setTag("items", itemStackHandler.serializeNBT());
        return compound;
    }

    public boolean canInteractWith(EntityPlayer playerIn){
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <=64D;
    }

    @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Nullable
    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
            return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemStackHandler);
        }
        return super.getCapability(capability, facing);
    }

}
 

Container Code:

Spoiler

public class MicroPackContainer extends Container {
    private MicroPackTileEntity te;
    public MicroPackContainer(IInventory playerInventory, MicroPackTileEntity te){
        this.te = te;
        addOwnSlots();
        addPlayerSlots(playerInventory);
    }

    private void addPlayerSlots(IInventory playerInventory) {
        // Slots for the main inventory
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 9; ++col) {
                int x = 9 + col * 18;
                int y = row * 18 + 70;
                this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 10, x, y));
            }
        }
        // Slots for the hotbar
        for (int row = 0; row < 9; ++row) {
            int x = 9 + row * 18;
            int y = 58 + 70;
            this.addSlotToContainer(new Slot(playerInventory, row, x, y));
        }
    }

    private void addOwnSlots(){
        IItemHandler itemHandler = this.te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        int x = 9;
        int y = 6;
        // Add our own slots
        int slotIndex = 0;
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            addSlotToContainer(new SlotItemHandler(itemHandler, slotIndex, x, y));
            slotIndex++;
            x += 18;
        }
    }

    @Nullable
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
        ItemStack itemstack = null;
        Slot slot = this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index < MicroPackTileEntity.SIZE) {
                if (!this.mergeItemStack(itemstack1, MicroPackTileEntity.SIZE, this.inventorySlots.size(), true)) {
                    return null;
                }
            } else if (!this.mergeItemStack(itemstack1, 0, MicroPackTileEntity.SIZE, false)) {
                return null;
            }

            if (itemstack1.isEmpty()) {
                slot.putStack(ItemStack.EMPTY);
            } else {
                slot.onSlotChanged();
            }
        }

        return itemstack;
    }

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
        return te.canInteractWith(playerIn);
    }
}
 

 

Developer of Beer Brewing Simulator

Quote

"There is a difference between knowing the path and walking the path." - Morpheus

 

 

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.

Announcements



×
×
  • Create New...

Important Information

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