Jump to content

bongotezz

Members
  • Posts

    29
  • Joined

  • Last visited

Posts posted by bongotezz

  1. Hello

     

    I recently made some code to lock a chest by locking the tile entity. Does this work the same for doors? Do they have a tile entity that i can lock? If they do have tile entities is there 2 of them, one for the top block and one for the bottom?

     

    Thanks in advance.

  2. Well, i switched it to NBT and actually made it worse. Now when i press ESC to exit the game it crashes with an error in the AnvilChunckLoader class and highlights this line of code that's not mine.

     

      this.pendingAnvilChunksCoordinates.remove(chunkpos);
    

     

    Maybe i'm not understanding NBT. Here's how i set it up.

     

    public static void fillInventoryChest(int x, int y, int z, World world, int itemID, int amount, int meta, int slot)
    {
    	BlockPos blockPos = new BlockPos(x,y,z);
    
    	Item item = new Item();
    	item = Item.getItemById(itemID);
    
    	ItemStack i = new ItemStack(item, amount, meta); 
    
    	i.setStackDisplayName("displayName");
    
    	TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);
    	te.setInventorySlotContents(slot, i);
    	te.setCustomName("TestChest");
    
    	NBTTagCompound underworld = te.getTileData();
    	underworld.setBoolean("underworld", true);
    	te.writeToNBT(underworld);
    
    	LockCode code = new LockCode("displayName");
    	te.setLockCode(code);
    	te.markDirty();
    
    } //end fill chest
    

     

    and here's how i'm testing for it

     

    public class ModEvents 
    {
    
    @SubscribeEvent (priority = EventPriority.HIGHEST)
    public void NoBlockBreaking(BlockEvent.BreakEvent event)
    {
    	System.out.println("test");
    	if(event.getState().getBlock()==Blocks.CHEST)
    	{
    		System.out.println("Inside IF");
    		World world = event.getWorld();
    		BlockPos blockPos = event.getPos();
    		TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);
    
    		boolean x = false;
    
    		NBTTagCompound underworld;
    		underworld = te.getTileData();
    
    		x = underworld.getBoolean("underworld");
    
    		if(x)
    		{
    
    			System.out.println("about to cancel");
    			event.setCanceled(true);
    		}
    
    	}
    
    }
    
    }
    

  3. What you need to do is call markDirty after setting the bame, as names are saved to the disk, but they need to know they need to be saved.

     

    I tried markDirty while testing. It came up in my google search. It didn't make any difference because it looks like the name saved. If i read the name even after logging out and in the name is correct. The problem seems to be that after the log out and log in the if statement is not returning true even though it should.

  4. I don't know much in the way of what's wrong with your current issue, but as an alternative you could try saving and loading the String you want in the tile entity's NBT, instead of using TileEntity#setCustomName, which seems to be more future proof, as I went to investigate that method, and couldn't find it in the TileEntity class for 1.11 (which is what I am currently working with).

     

    Thanks for your reply. I will now have to learn about NBT. I thought making the chests my mod spawns in unbreakable would be easy but i've had to learn a ton to get this close. Figuring out NBT should be fun. Any good guides you're familiar with?

  5. So i did some further testing and i'm now thinking this is either a Minecraft bug, a Forge bug, or some kind of lack of understanding something on my part.

     

    here's my debugging code

     

    @SubscribeEvent (priority = EventPriority.HIGHEST)
    public void NoBlockBreaking(BlockEvent.BreakEvent event)
    {
    	System.out.println("test");
    	if(event.getState().getBlock()==Blocks.CHEST)
    	{
    		System.out.println("Inside IF");
    		World world = event.getWorld();
    		BlockPos blockPos = event.getPos();
    		TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);
    
    		String boxName = te.getName();
    		String name = "Test Chest";
    
    		System.out.println("te.getName" + boxName + "is there a space?");
    		System.out.println("String" + name + "is there a space?");
    
    		if(boxName == name)
    		{
    
    			System.out.println("about to cancel");
    			event.setCanceled(true);
    		}
    
    	}
    
    }
    

     

    The text output from the first test - spawn the chest into the world and try to break it. The first time it's spawned it works properly. i cannot break the chest.

    [01:54:52] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:19]: test
    [01:54:52] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:22]: Inside IF
    [01:54:52] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:30]: te.getNameTest Chestis there a space?
    [01:54:52] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:31]: StringTest Chestis there a space?
    [01:54:52] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:36]: about to cancel
    

     

    Now i log out and back in and here's the output. I try to break the chest and it breaks just fine. It's not supposed to.

     

    [01:56:23] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:19]: test
    [01:56:23] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:22]: Inside IF
    [01:56:23] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:30]: te.getNameTest Chestis there a space?
    [01:56:23] [server thread/INFO] [sTDOUT]: [com.bongotezz.tutorialmod.events.ModEvents:NoBlockBreaking:31]: StringTest Chestis there a space?
    

     

    It's exactly the same except the chest now breaks and the last println is not called. For some reason after i log out and back in the IF statement fails even though the 2 strings are the same.

     

  6. I created an event to stop people from breaking specifically named chests. The even works find until i log out and back in again. It then no longer works.

     

    Code for filling and naming the tile entity.

     

    public static void fillInventoryChest(int x, int y, int z, World world, int itemID, int amount, int meta, int slot)
    {
    	BlockPos blockPos = new BlockPos(x,y,z);
    
    	Item item = new Item();
    	item = Item.getItemById(itemID);
    
    	ItemStack i = new ItemStack(item, amount, meta); 
    
    	i.setStackDisplayName("displayName");
    
    	TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);
    	te.setInventorySlotContents(slot, i);
    	te.setCustomName("Test Chest");
    
    	LockCode code = new LockCode("displayName");
    	te.setLockCode(code);
    
    }
    

     

    Code for the event

     

    @SubscribeEvent
    public void NoBlockBreaking(BlockEvent.BreakEvent event)
    {
    	if(event.getState().getBlock()==Blocks.CHEST)
    	{
    		World world = event.getWorld();
    		BlockPos blockPos = event.getPos();
    		TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);
    
    		if(te.getName()=="Test Chest")
    		{
    			event.setCanceled(true);
    		}
    
    	}
    
    }
    

     

    registering the even

     

        @EventHandler
        public void postInit(FMLPostInitializationEvent event)
        {
        	MinecraftForge.EVENT_BUS.register(new ModEvents());
        	proxy.postInit(event);
        }
    

     

    Anyone know what i'm missing? Thanks in advance.

  7. If you want to prevent a specific chest from being broken (whose position you know), you could just subscribe to BlockEvent.BreakEvent and cancel it as necessary.

     

    Thanks for this tip. I was able to learn how to use events to stop chest that my mod places from being broken. Other chests remain normal. I did it by naming the chest's tileEntityChest while filling it and then checking for it in the event. If it has the same name the block break gets cancelled. If anyone wants more details just let me know.

  8. I don't know anything about events yet. There's so much to learn. I've been at this for 12 whole days. I should know everything by now.  ;)  Honestly i'm surprised by how much progress i've made so far. Thanks for your help and suggestions. Back to the tutorials.

     

    Hey, if you'd like you can reference my OpenSource test-things mod for making your chest. https://github.com/EscapeMC/Things-Mod-1.10.2/tree/master/src/main/java/com/github/escapemc/thingsmod Good luck!

     

    Thanks man. I'll check it out.

  9. Thanks for replying. I came back to let people know that i figured it out. Here's the code in case anyone is curious how i did it. It appears to work. It's locked and can be opened with a specially named item and a diamond axe cannot break it in survival.

     

    public static void lockChest(int x, int y, int z, World world, String unlockCode)

    {

    BlockPos blockPos = new BlockPos(x,y,z);

     

    world.getBlockState(blockPos).getBlock().setBlockUnbreakable();

     

    TileEntityChest te = (TileEntityChest)world.getTileEntity(blockPos);

     

    LockCode code = new LockCode(unlockCode);

    te.setLockCode(code);

     

    }// end lock chest

  10. I have a chest that my mod spawns in. It's empty and i want to put items in it. I have the exact coordinates, the world, and i know the exact vanilla item that i want to put into it and what slot i want it in. How do i add the item to the chest?

     

    Thanks.

    TileEntityChest te = (TileEntityChest)worldObj.getTileEntity(BlockPos);
    te.setInventorySlotContents(index, stack);
    

     

    Thanks. One last question. I have the x y and z coordinates of the chest. How do i convert that into a BlockPos?

     

    I think i found it in net.minecraft.util.math.BlockPos;  :)

  11. I have a chest that my mod spawns in. It's empty and i want to put items in it. I have the exact coordinates, the world, and i know the exact vanilla item that i want to put into it and what slot i want it in. How do i add the item to the chest?

     

    Thanks.

    TileEntityChest te = (TileEntityChest)worldObj.getTileEntity(BlockPos);
    te.setInventorySlotContents(index, stack);
    

     

    Thanks. One last question. I have the x y and z coordinates of the chest. How do i convert that into a BlockPos?

  12. I found the following in some code on github.

     

        short width = tagCompound.func_74765_d("Width");

        short length = tagCompound.func_74765_d("Length");

        short height = tagCompound.func_74765_d("Height");

     

    tagCompound is an NBTTagCompound as shown in the method below.

     

    public ISchematic readFromNBT(NBTTagCompound tagCompound)

     

    Is anyone familiar with this func_74765_d?  I can't seem to find this anywhere in the NBTTagCompound class.

×
×
  • Create New...

Important Information

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