KYPremco
-
Posts
9 -
Joined
-
Last visited
Posts posted by KYPremco
-
-
19 minutes ago, diesieben07 said:
Like @quadraxis eluded to, unfortunately Minecraft does not check equals when comparing properties in certain circumstances, it uses ==.
In this case (and many other cases where String is used) this can be circumvented by callingString::intern on the property values before passing them into IBlockState::withProperty. This works, because the IProperty instances are created using string literals, which always resolve to interned strings.
Ah okay didn't understand the first time, well sad but true.
Then i'll just check if they going to cause errors and remove that type of block before passing into the item.
I hope not to much blocks have this problem, but thanks for the help anyway
-
15 minutes ago, quadraxis said:
This is due to the property used. The way Minecraft handles things means that it expects the exact value used to create the property, so unless you're being particularly careful, strings won't work here.
I use NBTUtil#readBlockState & NBTUtil#writeBlockState
After some more testeing: not all blocks will let it crash probably only a small amount.
But what bugs me is why is it working without the command while it's setup exact the same
This is the player data saved, one card from the command left, the other from the block right.
-
Hello,
I want to create a command to give an item with the structure that it get's from a file.
This worked properly with all vanilla items, after some tests i tried it with a mod and it crashed.
Whenever i use the card wich i get from the command it crashes but if i create it without the file in a block it works, i checked the NBT data of the players inventory and everything is exact the same.
What am i doing wrong ?
NBTUtil#readBlockState crashes with modBlocks
Commands.java - private void giveMemorycard gives error
Spoilerpackage com.kyproject.justcopyit.commands; import com.kyproject.justcopyit.JustCopyIt; import com.kyproject.justcopyit.init.ModItems; import com.kyproject.justcopyit.templates.StructureTemplate; import com.kyproject.justcopyit.tileentity.TileEntityBuilder; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.command.NumberInvalidException; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTUtil; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; import net.minecraftforge.common.util.Constants; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; public class JciCommands extends CommandBase { @Nonnull @Override public String getName() { return "jci"; } @Nonnull @Override public String getUsage(ICommandSender sender) { return "/jci <memorycard|reload>"; } @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if(args.length > 0) { switch (args[0]) { case "memorycard": this.giveMemorycard(args, sender); break; case "?": case "help": sender.sendMessage( new TextComponentString("§cUsage: " + this.getUsage(sender))); break; case "reload": this.reloadFitler(sender); break; default: break; } } } @Nonnull @Override public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) { if (args.length == 1) { return getListOfStringsMatchingLastWord(args, "memorycard", "reload"); } return super.getTabCompletions(server, sender, args, targetPos); } private void reloadFitler(ICommandSender sender) { TileEntityBuilder tileEntityBuilder = new TileEntityBuilder(); TileEntityBuilder.filter = tileEntityBuilder.readJsonFilter(); StructureTemplate structureTemplate = new StructureTemplate(); structureTemplate.loadBlockItemFilter(); sender.sendMessage( new TextComponentString("§2[JCI] Filter is updated!")); sender.sendMessage( new TextComponentString("§2[JCI] Layer filter is updated!")); } private void giveMemorycard(String[] args, ICommandSender sender) { if(args.length > 1) { if(!args[1].equals("?") && !args[1].equals("help")) { EntityPlayer player = (EntityPlayer) sender; World world = ((EntityPlayer) sender).world; StructureTemplate structureTemplate = new StructureTemplate(); NBTTagCompound nbt = structureTemplate.getNBT(args[1]); if(args.length > 2) { if(this.tryParseInt(args[2]) != null) { int durability = Integer.parseInt(args[2]); if(durability <= 0) { nbt.setInteger("durability", -1); } else { nbt.setInteger("durability", durability); } } else { sender.sendMessage( new TextComponentString("§cUsage: /jci memorycard <file name> <usages> <creative>")); } } else { nbt.setInteger("durability", -1); } if(nbt != null) { ItemStack item; if(args.length > 3) { if(args[3].equals("true")) { item = new ItemStack(ModItems.MEMORY_CARD_CREATIVE); } else { item = new ItemStack(ModItems.MEMORY_CARD); } } else { item = new ItemStack(ModItems.MEMORY_CARD); } if(nbt.hasKey("name")) { NBTTagList tagList = nbt.getTagList("blocks", Constants.NBT.TAG_COMPOUND); String test = nbt.getString("name"); <-- is working fine from the file System.out.println(NBTUtil.readBlockState(tagList.getCompoundTagAt(0))); <-- line 131 crashes System.out.println(test); } item.setTagCompound(nbt); if (player.inventory.getFirstEmptyStack() != -1) { //player.inventory.addItemStackToInventory(item); } else { world.spawnEntity(new EntityItem(world, player.posX, player.posY, player.posZ, item)); } } else { JustCopyIt.logger.warn("File doesn't exist!!"); sender.sendMessage( new TextComponentString("§cFile doesn't exist!!")); } } else { sender.sendMessage( new TextComponentString("§cUsage: /jci memorycard <file name> <usages> <creative>")); } } else { this.getUsage(sender); sender.sendMessage( new TextComponentString("§cUsage: /jci memorycard <file name> <usages> <creative>")); } } private Integer tryParseInt(String text) { try { return Integer.parseInt(text); } catch (NumberFormatException e) { return null; } } }
Error log:
Spoiler[21:06:42] [Server thread/WARN]: Couldn't process command: jci memorycard test2 -1 java.lang.IllegalArgumentException: Cannot set property PropertyStringTemp{name=type, clazz=class java.lang.String, values=[flux, fluid]} to flux on block draconicevolution:flow_gate, it is not an allowed value at net.minecraft.block.state.BlockStateContainer$StateImplementation.withProperty(BlockStateContainer.java:233) ~[BlockStateContainer$StateImplementation.class:?] at net.minecraft.nbt.NBTUtil.setValueHelper(NBTUtil.java:293) ~[NBTUtil.class:?] at net.minecraft.nbt.NBTUtil.readBlockState(NBTUtil.java:278) ~[NBTUtil.class:?] at com.kyproject.justcopyit.commands.JciCommands.giveMemorycard(JciCommands.java:131) ~[JciCommands.class:?] at com.kyproject.justcopyit.commands.JciCommands.execute(JciCommands.java:53) ~[JciCommands.class:?] at net.minecraft.command.CommandHandler.tryExecute(CommandHandler.java:126) [CommandHandler.class:?] at net.minecraft.command.CommandHandler.executeCommand(CommandHandler.java:98) [CommandHandler.class:?] at net.minecraft.network.NetHandlerPlayServer.handleSlashCommand(NetHandlerPlayServer.java:1001) [NetHandlerPlayServer.class:?] at net.minecraft.network.NetHandlerPlayServer.processChatMessage(NetHandlerPlayServer.java:977) [NetHandlerPlayServer.class:?] at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:47) [CPacketChatMessage.class:?] at net.minecraft.network.play.client.CPacketChatMessage.processPacket(CPacketChatMessage.java:8) [CPacketChatMessage.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_151] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_151] at net.minecraft.util.Util.runTask(Util.java:53) [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:748) [?:1.8.0_151] [21:06:42] [main/INFO]: [CHAT] An unknown error occurred while attempting to perform this command
-
Maybe a gif is more understandable ?
-
The NBT data,
it won't save i assume but don't understand why can't find it.
-
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
SpoilerItem MemoryCard:
Spoilerpackage 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
Spoilerpackage 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):
Spoilerpackage 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:
Spoilerif(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); } }
-
Thank Draco that maded a little bit more clear.
I got it working but ended up doing it totally different.
If you still see something stupid please tell me.
package com.kyproject.mynewmod.tileentity; import net.minecraft.block.BlockDirectional; import net.minecraft.block.BlockHorizontal; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.state.IBlockState; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.ITickable; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraftforge.common.capabilities.Capability; 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<>(); ItemStackHandler inventory = new ItemStackHandler(9); 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<>(); for(int x = 0;x < 15;x++) { for(int z = 1;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add(x,y,z))) { IBlockState state = worldObj.getBlockState(pos.add(x,y,z)).getActualState(worldObj, pos.add(x,y,z)); blocks.add(new BlockPlace(pos.add(x,y,z), state)); } } } } ORGIN = blocks; } public void startStructure() { blockStructure.clear(); blockStructure = (ArrayList<BlockPlace>) ORGIN.clone(); blockIsBuilding = true; countBlocks = 0; counter = 0; } @Override public void update() { if(blockIsBuilding) { if(counter == 0) { if(blockStructure.size() == 0) { blockIsBuilding = false; countBlocks = 0; } else { worldObj.setBlockState(blockStructure.get(0).pos, blockStructure.get(0).state); if(blockStructure.size() - 1 > 1) { worldObj.setBlockState(blockStructure.get(blockStructure.size() - 1).pos, blockStructure.get(blockStructure.size() - 1).state); blockStructure.remove(blockStructure.size() - 1); } blockStructure.remove(0); countBlocks++; } counter = 0; } else { counter++; } System.out.println(countBlocks); } } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); inventory.deserializeNBT(compound.getCompoundTag("inventory")); } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setTag("inventory", inventory.serializeNBT()); 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); } }
-
I have a problem with my mod.
It's my first thing i try out but just can't find anywhere an aswer.
I want to save a structure that a player build in a 15x15x15 area and then rebuild it. this works for how far i am.
But when it's building it changes the direction of the stairs, doors will be bugged and chests didn't test. Also my wood block went from spruce to normal.
I activate CreateStructure when i click on the top and startStructure on north side
When i use this on WithProperty it crashes and saying that the block (dispenser) doesnt have itpublic static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
Code:
package com.kyproject.mynewmod.tileentity; import net.minecraft.block.Block; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.state.IBlockState; import net.minecraft.nbt.NBTTagCompound; 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.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<>(); ItemStackHandler inventory = new ItemStackHandler(9); boolean blockIsBuilding = false; int countBlocks = 0; int counter = 0; public static class BlockPlace { public Block block; public BlockPos pos; public IBlockState state; public BlockPlace(BlockPos pos, Block block, IBlockState state) { this.pos = pos; this.block = block; this.state = state; } } public void createStructure() { ArrayList<BlockPlace> blocks = new ArrayList<>(); for(int x = 0;x < 15;x++) { for(int z = 1;z < 15;z++) { for(int y = 0;y < 15; y++) { if(!worldObj.isAirBlock(pos.add(x,y,z))) { blocks.add(new BlockPlace(pos.add(x,y,z),worldObj.getBlockState(pos.add(x,y,z)).getBlock(), worldObj.getBlockState(pos.add(x,y,z)).getBlock().getBlockState().getBaseState())); } } } } ORGIN = blocks; } // Tried this but error occured public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public void startStructure() { blockStructure.clear(); blockStructure = (ArrayList<BlockPlace>) ORGIN.clone(); blockIsBuilding = true; countBlocks = 0; counter = 0; } @Override public void update() { if(blockIsBuilding) { if(counter == 0) { if(blockStructure.size() == 0) { blockIsBuilding = false; countBlocks = 0; } else { worldObj.setBlockState(blockStructure.get(0).pos, blockStructure.get(0).block.getDefaultState()); if(blockStructure.size()- 1 > 1) { worldObj.setBlockState(blockStructure.get(blockStructure.size() - 1).pos, blockStructure.get(blockStructure.size() - 1).block.getDefaultState()); blockStructure.remove(blockStructure.size() - 1); } blockStructure.remove(0); countBlocks++; } counter = 0; } else { counter++; } System.out.println(countBlocks); } } //Some other stuff @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); inventory.deserializeNBT(compound.getCompoundTag("inventory")); } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setTag("inventory", inventory.serializeNBT()); 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); } }
[1.12.2][SOLVED] Cannot set property
in Modder Support
Posted
Oh sorry i misunderstood it because i tought i could not change the NBTUtils#readBlockState
Now i have copied that code in the command and changed
To:
And it's workign fine now indeed
But could i do this more smooth then copy paste that code ?(sorry i'm still a beginner)
Full code added: