Solved Change Wool Block Color in 1.13

Discussion in 'Spigot Plugin Development' started by Butter1484, Jul 17, 2018.

  1. Today I started to get my plugin ready for 1.13. One thing I am having trouble with is changing the color of wool blocks. What I had been doing previously was checking if the block material was Material.WOOL, but that does not exist anymore, so I need an alternative to that as well but at the moment I think using Material.LEGACY_WOOL is working for testing. I would then set the data of the block.
    Code (Text):
    if (toChange.getType() == Material.WOOL) {
                                    if (GameManager.green.contains(p.getName())) {
                                        toChange.setData(DyeColor.LIME.getWoolData());
                                    }
                                    if (GameManager.purple.contains(p.getName())) {
                                        toChange.setData(DyeColor.PURPLE.getWoolData());
                                    }
                                }
    Now that Material has a separate material for each wool color, I thought I could just use this code
    Code (Text):
    if (toChange.getType() == Material.LEGACY_WOOL) {
                                    if (GameManager.green.contains(p.getName())) {
                                        toChange.setType(Material.LIME_WOOL);
                                    }
                                    if (GameManager.purple.contains(p.getName())) {
                                        toChange.setType(Material.PURPLE_WOOL);
                                    }
                                }
    This is not working and I have tried searching around and trying some other things, such as getting the data from the block state, casting to wool then setting the color with no success.
     
    • Like Like x 1
    • Informative Informative x 1
  2. I did say I tried that at the bottom of my post, specifically I tried this code that I found in another post
    Code (Text):
    Wool wool = (Wool) e.getClickedBlock().getState().getData();
                    wool.setColor(DyeColor.LIME);
                    e.getClickedBlock().getState().update();
    but that did not work for me either
     
    • Like Like x 1
  3. I am experiencing the same problem.
     
  4. You need to set the data back to the state before updating the state.
     
  5. I deleted it now, but I had a line in there trying to do that but it gave me an error. I believe what I had was e.getClickedBlock().getState().setData(wool)
     
  6. You keep "getting" new objects like that. Get them once, store them in local fields, work with them, then set them back/update them.

    When you constantly use getters you throw away any changes you are making by getting fresh copies.
     
  7. Code (Text):

    if(Tag.WOOL.isValid(toChange.getType()){

       if (GameManager.green.contains(p.getName())) {
            toChange.setType(Material.LIME_WOOL);
      }   if (GameManager.purple.contains(p.getName())) {              
            toChange.setType(Material.PURPLE_WOOL);
      }
    }
     
     
    • Winner Winner x 1
  8. I usually would do so, and this code is not intended to be kept, just to act as a quick way for me to test. It should be fine here though, as it is not making a new object each time but simply accessing the references to the objects. The same state object will always be accessed, given it has not been deleted of course, so it should be safe to do, just may hurt efficiency by making unnecessary repeated calls.
     
  9. No. You get a new state every time you get it. Otherwise, it's not a state at all... It's like a snapshot in time, the moment you got it.
     
  10. Then I misunderstood how the state object worked, my apologies. I will try that tomorrow and see if that does what I need it to do.
     
  11. This is not working:
    Code (Java):
    block.setType(Material.RED_WOOL);

    This IS working:
    Code (Java):
    Block b = loc.getBlock();
    BlockState bState = b.getState();
    bState.setType(Material.RED_WOOL);
    bState.update(true, true);
    Like if helped.
    PS: Don't use deprecated methods working with 1.13 API....
     
  12. itemstack a = new ItemStack(material.wool, 1, (short) 14) // red wool
    ItemMeta s = a.getItemMeta();
    s.setdisplayname("");
    a.setitemMeta(s);
     
    • Funny Funny x 3
  13. Well now I've tried this but this isn't working
    Code (Text):
    BlockState state = e.getClickedBlock().getState();
    Wool wool = (Wool) state.getData();
    wool.setColor(DyeColor.LIME);
    state.update(true);
     
  14. Test this.

    Code (Text):
    if(Tag.WOOL.isTagged(toChange.getType()){

         BlockState bs = toChange.getState();
       if (GameManager.green.contains(p.getName())) {
            bs.setType(Material.LIME_WOOL);
      }   if (GameManager.purple.contains(p.getName())) {            
            bs.setType(Material.PURPLE_WOOL);
      }
    bs.update(true);
    }
    EDIT: Replaced isValid with is Tagged
     
    #15 ysl3000, Jul 17, 2018
    Last edited: Jul 17, 2018
    • Like Like x 2
  15. It is Tag.WOOL.isTagged but yes thank you that is working
     
    • Informative Informative x 1