Posted November 12, 20177 yr Hey, I want to save my structure on a item so you can put it in a chest or where you want and can rebuild it whenever you want. First i tested out a counter on a item worked fine but when i used the same code in the TileEntity it disappears when i get it out of the container. Loading getting the content out of the item worked well when i already put the number in the item with rightclicking. Screenshots Spoiler Item MemoryCard: Spoiler package com.kyproject.mynewmod.item; import com.kyproject.mynewmod.MyNewMod; import com.kyproject.mynewmod.block.ModBlocks; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.List; public class ItemMemoryCard extends Item { public ItemMemoryCard(String name) { setUnlocalizedName(name); setCreativeTab(MyNewMod.tabMyNewMod); setMaxStackSize(5); } @Override public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) { NBTTagCompound nbt; if(stack.hasTagCompound()) { nbt = stack.getTagCompound(); } else { nbt = new NBTTagCompound(); } if (nbt.hasKey("Uses")) { nbt.setInteger("Uses", nbt.getInteger("Uses") + 1); } else { nbt.setInteger("Uses", 1); } stack.setTagCompound(nbt); return super.onItemRightClick(stack, worldIn, playerIn, hand); } @Override public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) { if (stack.hasTagCompound() && stack.getTagCompound().hasKey("Uses")) { tooltip.add("Structure"); } else { tooltip.add("Empty"); } } @Override public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if(worldIn.getBlockState(pos).getBlock() == ModBlocks.containerBlock) { } return super.onItemUse(stack, playerIn, worldIn, pos, hand, facing, hitX, hitY, hitZ); } } TileEntityBuilder Spoiler package com.kyproject.mynewmod.tileentity; import com.kyproject.mynewmod.item.ModItems; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.nbt.*; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.ITickable; import net.minecraft.util.math.BlockPos; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.Constants; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.ItemStackHandler; import java.util.ArrayList; public class TileEntityBuilder extends TileEntity implements ITickable { public ArrayList<BlockPlace> blockStructure = new ArrayList<>(); public ArrayList<BlockPlace> ORGIN = new ArrayList<>(); public Block savedBlockpos; ItemStackHandler inventory = new ItemStackHandler(55); boolean blockIsBuilding = false; int countBlocks = 0; int counter = 0; public static class BlockPlace { public BlockPos pos; public IBlockState state; public BlockPlace(BlockPos pos, IBlockState state) { this.pos = pos; this.state = state; } } public void createStructure() { ArrayList<BlockPlace> blocks = new ArrayList<>(); EnumFacing forward = EnumFacing.getFront(this.getBlockMetadata()); int fX = forward.getFrontOffsetX(); int fZ = forward.getFrontOffsetZ(); // Reading // if(inventory.getStackInSlot(54) != null) { // if(inventory.getStackInSlot(54).getItem() == ModItems.memory_card) { // if(inventory.getStackInSlot(54).hasTagCompound()) { // if(inventory.getStackInSlot(54).getTagCompound().hasKey("Uses")) { // System.out.println(inventory.getStackInSlot(54).getTagCompound().getInteger("Uses")); // } // } // // // // } // } //Writing if(inventory.getStackInSlot(54) != null) { if(inventory.getStackInSlot(54).getItem() == ModItems.memory_card) { NBTTagCompound nbt; if(inventory.getStackInSlot(54).hasTagCompound()) { nbt = inventory.getStackInSlot(54).getTagCompound(); } else { nbt = new NBTTagCompound(); } if (nbt.hasKey("Uses")) { nbt.setInteger("Uses", nbt.getInteger("Uses") + 1); } else { nbt.setInteger("Uses", 1); } inventory.getStackInSlot(54).setTagCompound(nbt); } } if(forward == EnumFacing.NORTH) { for(int x = 0;x < 15;x++) { for(int z = 0;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add((-x) + fX, y, (fZ * z) + fZ))) { IBlockState state = worldObj.getBlockState(pos.add((-x) + fX, y, (fZ * z) + fZ)).getActualState(worldObj, pos.add((-x) + fX, y, (fZ * z) + fZ)); blocks.add(new BlockPlace(pos.add((-x) + fX, y, (fZ * z) + fZ), state)); } } } } } else if(forward == EnumFacing.SOUTH) { for(int x = 0;x < 15;x++) { for(int z = 0;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add((x) + fX, y, (fZ * z) + fZ))) { IBlockState state = worldObj.getBlockState(pos.add((x) + fX, y, (fZ * z) + fZ)).getActualState(worldObj, pos.add((x) + fX, y, (fZ * z) + fZ)); blocks.add(new BlockPlace(pos.add((x) + fX, y, (fZ * z) + fZ), state)); } } } } } else if(forward == EnumFacing.EAST) { for(int x = 0;x < 15;x++) { for(int z = 0;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add((fX * x) + fX, y, (-z) + fZ))) { IBlockState state = worldObj.getBlockState(pos.add((fX * x) + fX, y, (-z) + fZ)).getActualState(worldObj, pos.add((fX * x) + fX, y, (-z) + fZ)); blocks.add(new BlockPlace(pos.add((fX * x) + fX, y, (-z) + fZ), state)); } } } } } else { for(int x = 0;x < 15;x++) { for(int z = 0;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add((fX * x) + fX, y, (z) + fZ))) { IBlockState state = worldObj.getBlockState(pos.add((fX * x) + fX, y, (z) + fZ)).getActualState(worldObj, pos.add((fX * x) + fX, y, (z) + fZ)); blocks.add(new BlockPlace(pos.add((fX * x) + fX, y, (z) + fZ), state)); worldObj.setBlockState(pos.add((fX * x) + fX, y, (z) + fZ), Blocks.DIAMOND_BLOCK.getDefaultState()); } } } } } ORGIN = blocks; } public void startStructure() { blockStructure.clear(); blockStructure = (ArrayList<BlockPlace>) ORGIN.clone(); blockIsBuilding = true; countBlocks = 0; counter = 0; } @Override public void update() { if(blockIsBuilding) { int tickCounter = 0; if(counter == tickCounter) { if(blockStructure.size() == 0) { System.out.println("Finished"); blockIsBuilding = false; countBlocks = tickCounter; } else { if(worldObj.isAirBlock(blockStructure.get(0).pos)) { for (int slot = 0; slot < 9; slot++) { if (this.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH)) { if (inventory.getStackInSlot(slot) != null) { if (inventory.getStackInSlot(slot).getItem().getRegistryName().equals(blockStructure.get(0).state.getBlock().getRegistryName())) { boolean canBuild = false; if(blockStructure.get(0).state.getBlock().getRegistryName().equals(Blocks.LOG.getRegistryName())) { if(blockStructure.get(0).state.getBlock().getMetaFromState(blockStructure.get(0).state) == inventory.getStackInSlot(slot).getMetadata()) { canBuild = true; } } else if(blockStructure.get(0).state.getBlock().getRegistryName().equals(Blocks.LOG2.getRegistryName())) { if(blockStructure.get(0).state.getBlock().getMetaFromState(blockStructure.get(0).state) == inventory.getStackInSlot(slot).getMetadata()) { canBuild = true; } } else if(blockStructure.get(0).state.getBlock().getRegistryName().equals(Blocks.PLANKS.getRegistryName())) { if(blockStructure.get(0).state.getBlock().getMetaFromState(blockStructure.get(0).state) == inventory.getStackInSlot(slot).getMetadata()) { canBuild = true; } } else if(blockStructure.get(0).state.getBlock().getRegistryName().equals(Blocks.STONE.getRegistryName())) { if(blockStructure.get(0).state.getBlock().getMetaFromState(blockStructure.get(0).state) == inventory.getStackInSlot(slot).getMetadata()) { canBuild = true; } } else { canBuild = true; } if(canBuild) { inventory.extractItem(slot, 1, false); worldObj.setBlockState(blockStructure.get(0).pos, blockStructure.get(0).state); blockStructure.remove(0); break; } } } } } } else { blockStructure.remove(0); } countBlocks++; } counter = 0; } else { counter++; } } } //Some other stuff @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); inventory.deserializeNBT(compound.getCompoundTag("inventory")); NBTTagList tagList = compound.getTagList("MyList", Constants.NBT.TAG_COMPOUND); NBTTagCompound tag1 = tagList.getCompoundTagAt(0); savedBlockpos = Block.getBlockFromName(tag1.getString("nameBlock0")); for(int i=0;i < tagList.tagCount();i++) { NBTTagCompound tag = tagList.getCompoundTagAt(i); BlockPos pos = NBTUtil.getPosFromTag(tag.getCompoundTag("blockPos" + i)); IBlockState state = NBTUtil.readBlockState(tag); ORGIN.add(new BlockPlace(pos,state)); } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setTag("inventory", inventory.serializeNBT()); NBTTagList tagList = new NBTTagList(); for(int i = 0;i < ORGIN.size();i++) { if(ORGIN.get(i) != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setTag("blockPos" + i, NBTUtil.createPosTag(ORGIN.get(i).pos)); NBTUtil.writeBlockState(tag, ORGIN.get(i).state); tagList.appendTag(tag); } } compound.setTag("MyList", tagList); return super.writeToNBT(compound); } @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing); } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inventory : super.getCapability(capability, facing); } } Container (custom inventory): Spoiler package com.kyproject.mynewmod.container.builderContainer; import com.kyproject.mynewmod.tileentity.TileEntityBuilder; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nullable; public class ContainerTutorial extends Container { public ContainerTutorial(InventoryPlayer inventoryPlayer, TileEntityBuilder tileEntityBuilder) { if(tileEntityBuilder.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH)) { IItemHandler inventory = tileEntityBuilder.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH); // Memory slot addSlotToContainer(new SlotMemory(inventory, 54,192,54)); // Container inventory for(int y = 0;y < 6;y++) { for(int x = 0; x < 9; x++) { addSlotToContainer(new SlotBuilderInventory(inventory, x + (y * 9), 8 + x * 18, 18 + y * 18)); } } // Main player inventory for(int y = 0;y < 3;y++) { for(int x = 0; x < 9; x++) { addSlotToContainer(new Slot(inventoryPlayer, x + (y * 9) + 9, 8 + x * 18, 140 + y * 18)); } } // Player hotbar for(int i = 0;i < 9;i++) { addSlotToContainer(new Slot(inventoryPlayer, i, 8 + (i * 18), 198)); } } } @Nullable @Override public ItemStack transferStackInSlot(EntityPlayer player, int index) { ItemStack stack = null; Slot slot = inventorySlots.get(index); if(slot != null && slot.getHasStack()) { ItemStack stackInSlot = slot.getStack(); stack = stackInSlot.copy(); int containerSlots = 55; if(index < containerSlots) { if(!this.mergeItemStack(stackInSlot, containerSlots, inventorySlots.size(), true)) { return null; } } else if (!this.mergeItemStack(stackInSlot, 0, containerSlots, false)) { return null; } if(stackInSlot.stackSize == 0) { slot.putStack(null); } else { slot.onSlotChange(null, null); } slot.onPickupFromSlot(player, stackInSlot); } return stack; } @Override public boolean canInteractWith(EntityPlayer playerIn) { return true; } } Just the writing code in TileEntityBuilder: Spoiler if(inventory.getStackInSlot(54) != null) { if(inventory.getStackInSlot(54).getItem() == ModItems.memory_card) { NBTTagCompound nbt; if(inventory.getStackInSlot(54).hasTagCompound()) { nbt = inventory.getStackInSlot(54).getTagCompound(); } else { nbt = new NBTTagCompound(); } if (nbt.hasKey("Uses")) { nbt.setInteger("Uses", nbt.getInteger("Uses") + 1); } else { nbt.setInteger("Uses", 1); } inventory.getStackInSlot(54).setTagCompound(nbt); } } Edited November 12, 20177 yr by KYPremco
November 12, 20177 yr I don't understand your problem. What disappears? Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
November 12, 20177 yr Author The NBT data, it won't save i assume but don't understand why can't find it.
November 12, 20177 yr Author Maybe a gif is more understandable ? Edited November 12, 20177 yr by KYPremco
November 12, 20177 yr You're clicking a button on the client, writing the NBT to the item on the client, then picking up the item, the server then overwrites the client's data because it is the authority. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
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.