player.getInventory().getItemInHand().setData()??

Discussion in 'Plugin Development' started by Soxra, Apr 19, 2012.

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

    Soxra

    Hey,
    i want to change the iteminhand to junge wood.
    I have found the code player.getInventory().getItemInHand().setData() but what is what i have to do? the material data for jungle wood is 3 (17:3).
    Regards Soxra
     
  2. Offline

    heisan213

    player.getInventory().getItemInHand().setData( (byte)3) ; ?
     
  3. Offline

    Soxra

    I have tried that already, not working.
     
  4. Offline

    ZNickq

    ItemStack is = getItemInHand();
    is.setData ((byte) 3);
    setItemInHand(is);
     
  5. Offline

    heisan213

    Right. Didn't think at all, did I? *shakes head*
     
  6. Offline

    Soxra

    Thanks
     
  7. Offline

    AmoebaMan

    Yes, I believe getItemInHand() is one of those methods that returns a copy of the object in question, rather than a reference to the actual object itself. As such, you'd need to re-update it when you're done modifying it.
     
  8. Offline

    lixuan0303

    I have tried that already, not working.

    [​IMG]
     
  9. Offline

    Soxra

    I get an error:
    [​IMG]
    error message:
    [​IMG]
     
  10. Offline

    ZNickq

    donno, give it the right argument? :p maybe remove the (byte)
     
  11. Offline

    Soxra

    ZNickq
    i have solved it now:
    Code:
    int anzahl = player.getItemInHand().getAmound();
     
    ItemStack is = new ItemStack(Material.LOG, anzahl, (byte)3);
    player.setItemInHand(is);
     
  12. Offline

    nubebuster

    Soxra setData somehow isnt the right way, the right way would be setDurabillity. This will set the durability for tools and the data for oether items.
    Your way is also fine
     
  13. Offline

    xTigerRebornx

    nubebuster Did you really have to revive a post from over a year ago?
     
  14. Offline

    nubebuster

    xTigerRebornx People will read old posts and I want people to know how to do it the right way
     
  15. Offline

    Badeye

    xTigerRebornx he had to, i am currently trying to set the data of a block in the world and i get the same problem. changing teh type works, changing the adata makes teh object... well simply disappear and it turns into air :/
     
  16. Offline

    AoH_Ruthless

    Badeye
    Instead of re-reviving a post (from 3 months ago), make your own thread.

    Show us your code. I don't know what I'm supposed to be working with.
     
  17. Offline

    Badeye

    AoH_Ruthless i am sorry, just found this thread and it wasn't that old. If it has the same topic, why being angry about it? The code is quite complex, i am just going to post the basic problem:
    Code:java
    1. Block replaceBlock = loc.getBlock();
    2. replaceBlock.setType(Material.STONE);
    3. replaceBlock.setData((byte) 4, true);
    4. replaceBlock.getState().update();

    The location is valid, the block was gotten aswell. Without the data line it works, the requested block at teh location loc gets changed (in this simlyfied example) to stone. If i remove the physics boolean in the setData method it still amkes no difference, the block stays "gone".
     
  18. Offline

    AoH_Ruthless

    Badeye
    I wasn't being angry .. it just makes sense to make your own thread...

    What do you mean by gone? I understand the logic in giving me a simplified rundown of it, but I need to see the whole method (the same method you are using!). You shouldn't be needing to update the state of the block. Remove your update because it doesn't really do anything..
     
  19. Offline

    Badeye

    AoH_Ruthless ah well oaky lets see. createTombstone() gets called when a player dies, remove tombstone gets called every 10 minutes (12000 ticks) via a hasmap or after a restart (values are being written /read out of the config, works very well). That all works smooth when i remove the setData line. I have debuged it, the data value gets saved correctly to the config (if it is a brick halfblock for example it has the value 7) and gets read correctly. But with setting teh data value the block turns into air and not into the "old block"...

    Here are my hasmaps, at teh top of this class:
    Code:java
    1. public static HashMap<Location, Inventory> tombstones = new HashMap();
    2. public static HashMap<Location, Long> tombstoneTimer = new HashMap();
    3. public static HashMap<Location, Material> tombstoneOldBlock = new HashMap();
    4. public static HashMap<Location, Byte> tombstoneOldBlockData = new HashMap();


    I am refering to the "main" class with "plugin" to get teh config etc, this is also on top of the same class:
    Code:java
    1. static main plugin;
    2.  
    3. public PlayerManager(main instance) {
    4. plugin = instance;
    5. }



    Here is my createTombstone method, which gets called when the player dies, please excuse the shitty formating and the blockAtDeath cll in teh beginning... I was just trying to place the tombstone not into lava/water, i wanted it to be placed ontop of the water if a player dies in the water/lava:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public static void createTombstone(Player p, Location loc, List<ItemStack> drops){
    3.  
    4. Material blockAtDeath = loc.getBlock().getType();
    5.  
    6.  
    7. if(blockAtDeath == Material.STATIONARY_WATER || blockAtDeath == Material.WATER || blockAtDeath == Material.STATIONARY_LAVA || blockAtDeath == Material.LAVA){
    8. for(int i = 0; i>0; i++){
    9. loc.setY(loc.getY() + i);
    10. if(loc.getBlock().getType() == Material.AIR){
    11. break;
    12. } else if(i == 30)
    13. break;
    14. }
    15. }
    16.  
    17. Material oldBlock = loc.getBlock().getType();
    18. Byte oldBlockData = loc.getBlock().getData();
    19.  
    20. Block replaceBlock = loc.getBlock();
    21. replaceBlock.setType(Material.SKULL);
    22. replaceBlock.setData((byte) 1);
    23.  
    24. BlockState skullState = ((Skull) replaceBlock.getState());
    25.  
    26. ((Skull) skullState).setSkullType(SkullType.ZOMBIE);
    27. ((Skull) replaceBlock.getState()).update();
    28.  
    29. Inventory inv = Bukkit.getServer().createInventory(p, 27, "Body of " + p.getDisplayName());
    30. for(int i = 0; i<drops.size(); i++){
    31. if(drops.get(i).getType() != Material.GHAST_TEAR && drops.get(i).getType() != Material.NETHER_STAR)
    32. if(inv.firstEmpty() != -1)
    33. inv.addItem(drops.get(i));
    34. }
    35.  
    36. tombstones.put(replaceBlock.getLocation(), inv);
    37. tombstoneTimer.put(replaceBlock.getLocation(), main.tickID);
    38. tombstoneOldBlock.put(replaceBlock.getLocation(), oldBlock);
    39. System.out.println("create data: " + oldBlockData);
    40. tombstoneOldBlockData.put(replaceBlock.getLocation(), oldBlockData);
    41.  
    42. List<String> list = plugin.getConfig().getStringList("Tombstone");
    43. Location reploc = replaceBlock.getLocation();
    44.  
    45. list.add(String.valueOf(reploc.getX() + " " + reploc.getY() + " " + reploc.getZ()) + " " + oldBlock + " " + oldBlockData);
    46. plugin.getConfig().set("Tombstone", list);
    47. plugin.saveConfig();
    48.  
    49. }


    My remove tombstone methd, gets called after 10 mins, here is teh problem somewhere since teh create tombstone saves the values correctly and sets teh block correctly to a skull:
    Code:java
    1. public static void removeTombstone(Location loc){
    2. Material oldBlock = tombstoneOldBlock.get(loc);
    3. Byte oldBlockData = tombstoneOldBlockData.get(loc);
    4.  
    5. tombstones.remove(loc);
    6. tombstoneTimer.remove(loc);
    7.  
    8. Block replaceBlock = loc.getBlock();
    9. replaceBlock.setType(oldBlock);
    10. System.out.println("Data" + oldBlockData);
    11. replaceBlock.setData(oldBlockData, true);
    12. replaceBlock.getState().update();
    13. for (int i = 0; i < 8; i++)
    14. loc.getWorld().playEffect(loc, Effect.SMOKE, i);
    15. loc.getWorld().playEffect(loc, Effect.EXTINGUISH, 500);
    16.  
    17.  
    18. List<String> list = plugin.getConfig().getStringList("Tombstone");
    19. list.remove(String.valueOf(loc.getX() + " " + loc.getY() + " " + loc.getZ()) + " " + oldBlock + " " + oldBlockData);
    20. plugin.getConfig().set("Tombstone", list);
    21. plugin.saveConfig();
    22. }

    I know, the code is not very nice... but still it works without setting the data!
     
  20. Offline

    AoH_Ruthless

    Badeye
    Your createTombstone(<parameters) {} needs some work.

    1. Your for loop when checking if the block at death is water or lava is completely wrong. You could just loop 30 times instead of looping forever and breaking at 30. You also are checking if the location is AIR, when it clearly can't be because your if statement executes only if the block is water or lava.
    2. Also, it's not working because block's can't have byte data ... you are confusing it with an itemstack. You need to be using MetaData of the block when you use the setData method: http://jd.bukkit.org/rb/apidocs/org/bukkit/metadata/Metadatable.html#setMetadata(java.lang.String, org.bukkit.metadata.MetadataValue)
    3. Your code could use some cleaning; also, to format, if you are in eclipse just use CTRL + SHIFT + F
     
    Badeye likes this.
  21. Offline

    Badeye

    AoH_Ruthless I love you :*
    I knew the water loop was wrong, but i wanted to fix the rest before fixing that. The data/metadata thing is interesting, i had no idea about that one, that explains why setting the byte data went wrong. So thanks again for the time you took for checking my code :)
     
    AoH_Ruthless likes this.
Thread Status:
Not open for further replies.

Share This Page