How to acces the NBT structure of an placed block

Discussion in 'Plugin Development' started by ferrybig, Nov 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    ferrybig

    I have an monster spawner placed on the ground, how can I acces its nbt to add things like "RequiredPlayerRange" if you have an monster spawner, or "Command"if you have an command block, since theres no sutch api for inside bukkit (I know that you need to import craftbukkit to make it work, but I am unable to find whits var I need to modify from the blocks used by normal mc
     
  2. Offline

    fireblast709

    ferrybig
    Dug deep, dug far, ended up in void. NPE when I wanted to do anything with its data. Nonetheless my code:
    Code:text
    1. Block b = p.getTargetBlock(null, 10);
    2. CraftCreatureSpawner csp = ((CraftCreatureSpawner)b.getState());
    3. try {
    4. Field f = CraftCreatureSpawner.class.getDeclaredField("spawner");
    5. f.setAccessible(true);
    6. TileEntityMobSpawner tems = (TileEntityMobSpawner)f.get(csp);
    7. f = TileEntityMobSpawner.class.getDeclaredField("spawnData");
    8. f.setAccessible(true);
    9. NBTTagCompound tag = (NBTTagCompound)f.get(tems);
    10. for(Object o : tag.c())
    11. {
    12. if(o instanceof NBTBase && !(o instanceof NBTTagCompound))
    13. {
    14. NBTBase base = (NBTBase)o;
    15. System.out.println("name found: "+base.getName());
    16. }
    17. }
    18. } catch (NoSuchFieldException ex) {
    19. log.info("NoSuchField");
    20. } catch (SecurityException ex) {
    21. log.info("SecurityException");
    22. }
    23. catch (IllegalArgumentException ex) {
    24. log.info("IllegalArgument");
    25. } catch (IllegalAccessException ex) {
    26. log.info("IllegalAccess");
    27. }

    I used the block I was looking at. the NBTTagCompound was null
    Ps. For all the people say "you do not check for null". True, I was not. I was only trying to get actual data with this DEBUG code, which means null values were useless anyway
     
  3. Offline

    one4me

    Setting the distance on spawners was easy enough, but command blocks are set up a bit differently. They don't store a variable for the required range separate from the command string itself. So you'd have to parse the command and add the argument in the correct place.

    This method does work for spawners, and I started on command blocks, but don't have enough time right now to finish it.
    Code:
    public void setTileRange(Block block, int range) {
      TileEntity te = ((CraftWorld)block.getWorld()).getHandle().getTileEntity(block.getX(), block.getY(), block.getZ());
      if(te instanceof TileEntityMobSpawner) {
        TileEntityMobSpawner tems = (TileEntityMobSpawner)te;
        NBTTagCompound c = new NBTTagCompound();
        tems.b(c);
        c.setShort("RequiredPlayerRange", (short)range);
        tems.a(c);
      }
      if(te instanceof TileEntityCommand) {
        TileEntityCommand tec = (TileEntityCommand)te;
        NBTTagCompound c = new NBTTagCompound();
        tec.b(c);
        System.out.println("Command: " + c.getString("Command"));
        //tec.b(new StringBuilder(c.getString("Command")).append("[r=" + range + "]").toString());
      }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page