so Block.setData() is deprecated...whats the new way to change the data of an existing block?

Discussion in 'Plugin Development' started by scarletomato, Oct 30, 2013.

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

    scarletomato

    updated problem further down in the thread here

    So i've been trying to figure out the new way to do this now that setData() has been deprecated. How do you guys change a block now. Like a sapling to a birch sapling, or growing wheat?

    thanks.

    Edit: Setting the material i get.. what i want to do is set the data for that particular material. Like different types of logs, different color wool blocks, or different saplings.
     
  2. Offline

    1Rogue

    Code:java
    1. Block.setType(Material.ENUM_TYPE);
     
  3. Offline

    scarletomato

    Yes i can set the type, but how do i change it to, say, a birch sapling, instead of just a regular one?
     
  4. Offline

    amhokies

    I don't think that's what he needed. He was asking how to set a block's data, like the different wool colours.
     
  5. Offline

    johnnywoof

    1Rogue
    That is setting the type, not material data.
    Anyways I'm not sure why it is deprecated, that really stinks. However I don't know what the new method is :/
     
  6. Offline

    1Rogue

  7. Offline

    scarletomato

    that is apparently for plugin specific data. not the same as the data that minecraft uses. For instance, i can set an invisible "tag" on a block with a lookup key to access later.
     
  8. Offline

    lycano

    It the same with all "marked as deprecated" methods. There will be a replacement (if needed) when its removed. As long as it is marked as deprecated it will only mean "this could be changed/removed in the near future" as a reminder for you to check this.

    Its probably marked as deprecated because of magic value. When its done checkout the new system ... change your code accordingly.
     
  9. Offline

    1Rogue

    Aha, my bad. It looks like for setting wool color you would use the Wool class, which extends MaterialData. Looking for a way to set that to a block now...
     
  10. Offline

    DenialMC

    You can still use Block.setData(), nobody restricts you from doing so.

    I personally still use the deprecated "magic id" methods.

    Daniel
     
  11. Offline

    1Rogue

    Got it:

    Code:java
    1. Block b;
    2. MaterialData state = b.getState().getData();
    3. if (state instanceof Wool) {
    4. Wool w = (Wool) state;
    5. w.setColor(DyeColor.BLACK);
    6. } else {
    7. // handling code if it isn't wool
    8. }
     
    NathanWolf likes this.
  12. Offline

    scarletomato

    oh hmm, that kinda blows.. So , for example a sapling, I have to create a tree object first and set it as the material data of the blockstate and then run the update()? Kinda sucks if i'm using mods and mcpc :( I was hoping i could have a config.yml where i could just define the item IDs and data values.
     
  13. Offline

    The_Doctor_123

    scarletomato
    Just use the deprecated method and suppress warnings. Sometimes you just need to bite the bullet and use those deprecated methods(unless they're not functional).
     
    NathanWolf likes this.
  14. Offline

    1Rogue


    That is, until they're removed and you have to update to the new method anyhow. Why wait?
     
  15. Offline

    The_Doctor_123

    1Rogue
    I honestly believe they're bluffing.
     
    NathanWolf likes this.
  16. Offline

    scarletomato

    yeah that's what i'm afraid of... I may just make my own extends MaterialData class to handle any mods.
     
  17. Offline

    amhokies

    Is there any good reason as to why it was deprecated in the first place?
     
  18. Offline

    jorisk322

    Not supported by Minecraft itself anymore I think.
     
  19. Offline

    lycano

    1Rogue in this case (Magic Numbers) we have to wait when the new system is implemented. We can't switch from magic numbers to string notation if there are no methods to switch to. Why wait? Because there is no alternative yet.
     
  20. Offline

    desht

    It's all part of the slow move towards the modding API. Using explicit item & data ID's is being phased out, the intention being that all materials will be referred to (at least by players and plugins/mods) by their material name only. Then the problem of conflicting item ID's (not a problem for Bukkit but a major plague for e.g. Forge) simply goes away. Mods will never allocate their own item ID; the server will do it for them (and, I would speculate, provide the client with the complete item->ID map dynamically when it connects).

    Some materials have ways of updating blocks without using explicit data bytes, e.g. wool. But for some (trees/saplings) for example, you just need to carry on working with data bytes for now. I would not recommend telling your IDE to ignore the deprecation warnings; all that will do is hide the issue until it becomes a fatal error later on. Leaving those warnings enabled serves as a TODO list for you: code to change when the API is updated.
     
    frymaster likes this.
  21. Offline

    geNAZt

    Well you should never use DataValues (setData and getData) since they only work in Bukkit. If you want to use DataValues in Minecraft you have to set the Durability setting of an item.

    For example if you create a ItemStack of wool and let the Wool be another Color (for example DataValue 2) you would have a code like this:

    Code:java
    1. ItemStack itemStack = new ItemStack(Material.WOOL);
    2. itemStack.getData().setData(2);
    3.  
    4. Bukkit.getWorld("world").dropItem(new Location(Bukkit.getWorld("world"), 1500D, 80D, 1500D), itemStack);


    It will in all cases spawn a WHITE wool. If you use the Durability Minecraft gets the right Datavalue and the correct ItemDrop.

    To lookup here: https://github.com/Bukkit/CraftBukk...tbukkit/inventory/CraftItemStack.java#L25-L38

    The ItemStack from Minecraft: https://github.com/Bukkit/CraftBukk...net/minecraft/server/ItemStack.java#L153-L175

    So if you give a Block/Item into Minecraft the Datavalue (getData() / setData()) is ignored. So i would never use it (i had a bug where the Datavalues arent right in the Minecraft world, in bukkit they were right). It confuses alot.

    And a little addition. The Code from Minecraft and from Bukkit allows the creation of Datavalues out of the Byte range. So you can create a Wool block with an Datavalue of 256 or 16.
     
  22. Offline

    1Rogue


    From what I see, I just barely finished writing an alternative above without using deprecated methods.
     
  23. Offline

    desht

    geNAZt the right way to do the above would be:
    PHP:
    world.dropItem(location, new Wool(DyeColor.RED).toItemStack());
    No need to worry about data or durability at all. Working with durability really only makes sense for items which actually have durability, i.e. tools & weapons. The fact that durability is overloaded to also represent item data like colour or tree species is an implementation detail which plugins really shouldn't be depending on.
     
  24. Offline

    geNAZt

    Yes but i store items in a Database and dont want to have a 250 Lines+ switch Statement :D
     
  25. Offline

    scarletomato

    okay i'm running into another error now. Obviously I san use Block.setType(Material.SAPLING) to change a block to a sapling. But i still can't figure out how to spawn a birch sapling. here's the code currently:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if(commandLabel.equals(MAIN_COMMAND)){
    3. if(sender instanceof Player){
    4. Player p = (Player)sender;
    5. Block b = p.getWorld().getBlockAt(p.getLocation());
    6. BlockState bs = b.getState();
    7. bs.setData((MaterialData)new Tree(TreeSpecies.BIRCH));
    8. bs.update();
    9. }
    10. return true;
    11. }
    12. return false;
    13. }

    this throws an exception "Provided data is not of type MaterialData, found org.bukkit.Material.Tree" even though Tree extends MaterialData

    ALSO: after using the blockstate's setType() and calling update() it doesn't actually change the block in the world. How do i use blockstate to actually change the blocks?

    any ideas?
     
  26. Offline

    sirrus86

    Been a few days, but in case you're still having issues or anyone else has the same question...

    Since Tree extends MaterialData you don't need to cast it, otherwise so long as you set the blockstate's type to sapling before setting its data it should work.

    As for updating the blockstate, instead of blockstate.update() (which won't update if the block type has changed) try using blockstate.update(true) (which forces an update regardless).
     
Thread Status:
Not open for further replies.

Share This Page