Solved Replace color of a dye in player's workbench inventory with another

Discussion in 'Plugin Development' started by PDKnight, May 11, 2015.

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

    PDKnight

    So, I have a big problem now. I just replace a dye in player's inventory with a new dye, BUT with different color, for example from cyan to gray. My plugin changes dye to another perfectly. But when I move it to my hotbar, I can't drop it and when I click on it in my inventory, it suddenly disappears. So I suppose it does not exist then. So my question is, how can I change the color of this item? [creeper]

    Screenshots:
    [​IMG]
    Before

    [​IMG]
    After

    [​IMG]
    I moved it to hotbar - a second before it disappeared (because I didn't click on it :D)​

    Code:
    Code:java
    1. Dye dye = new Dye();
    2. dye.setColor(DyeColor.RED);
    3. ItemStack dye4 = dye.toItemStack();
    4. dye4.setAmount(1);
    5. dye.setColor(DyeColor.ORANGE);
    6. ItemStack dye3 = dye.toItemStack();
    7. dye4.setAmount(1);
    8. dye.setColor(DyeColor.PURPLE);
    9. ItemStack dye2 = dye.toItemStack();
    10. dye4.setAmount(1);
    11. dye.setColor(DyeColor.CYAN);
    12. ItemStack dye1 = dye.toItemStack();
    13. dye4.setAmount(1);
    14. dye.setColor(DyeColor.GRAY);
    15. ItemStack dye0 = dye.toItemStack();
    16.  
    17. ArrayList<ItemStack> dyes = new ArrayList<ItemStack>(Arrays.asList(
    18. dye0, dye1, dye2, dye3, dye4
    19. ));
    20. p.getOpenInventory().getTopInventory().setItem(1, dyes.get(0));

    PS: anyway, why does this happens for me? I don't understand :eek:

    This example doesn't work with dyes, but with glass (for example) it does :confused:

    Okay, I made a plugin with this error, so you can see what I'm doing and what it does:
    Main class (not compiled): https://raw.githubusercontent.com/PDKnight/pdknight.github.io/master/bukkit-error-plugin/awd.java
    The plugin: https://www.dropbox.com/s/763mim1jhliypin/AWD.jar?dl=0

    Please, help me with that :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. try p.updateInventory()
     
  3. Offline

    Abs0rbed

    What exactly are you trying to do? Why do you need to change the color of the dye? There might be easier ways to do it instead of how you're trying to
     
  4. Offline

    PDKnight

    @FisheyLP Before setting or after setting the slot?
    @Abs0rbed This is just the part of the code, I've shortened it.
     
  5. Offline

    PDKnight

    @FisheyLP Both doesn't work :( It does the same thing anyway :/
     
  6. try just p.getInventory().setItem
    or p.getInventory().getTopInventory().setItem
     
  7. Offline

    PDKnight

    @FisheyLP I don't know if I understand right, but this is the original code in my question:
     
  8. Offline

    nbrandwine

    I'm pretty sure that when it poofs from your inventory is because it was invalidated by something somewhere. Like an example, if you press 1 or 2 while hovering over a block it will stack a lot, and when you move it out of your inventory it disappears. I think it's something like that.

    Kinda of a longshot, and a guess, but...

    Maybe you can make a method which is something like 'swapMe' which is like:

    This might be the first...? It's either Player or Inventory.
    public void swapMe(Player p, Dye oldDye, Dye newDye, int oldQuant, int newQuant)

    which does something like,
    -> Takes player, makes inventory of that player
    -> Searches for old dye (passed in)
    -> If it's found,
    -> Remove amount of old dye (oldQuant)
    -> Add new amount of dye (newQuant)

    That's just my guess.
     
  9. Offline

    PDKnight

    @nbrandwine
    Oh, you gave an idea and now it works! I was playing around it and I found out that I forgot to set amount of the dye to 1, how stupid bug :D Thanks alot!
    Code:java
    1. Dye dye = new Dye();
    2. dye.setColor(DyeColor.RED);
    3. ItemStack dye4 = dye.toItemStack();
    4. dye4.setAmount(1);
    5. dye.setColor(DyeColor.ORANGE);
    6. ItemStack dye3 = dye.toItemStack();
    7. dye3.setAmount(1);
    8. dye.setColor(DyeColor.PURPLE);
    9. ItemStack dye2 = dye.toItemStack();
    10. dye2.setAmount(1);
    11. dye.setColor(DyeColor.CYAN);
    12. ItemStack dye1 = dye.toItemStack();
    13. dye1.setAmount(1);
    14. dye.setColor(DyeColor.GRAY);
    15. ItemStack dye0 = dye.toItemStack();
    16. dye0.setAmount(1);
    17.  
    18. ArrayList<ItemStack> dyes = new ArrayList<ItemStack>(Arrays.asList(
    19. dye0, dye1, dye2, dye3, dye4
    20. ));
    21.  
    22. p.getOpenInventory().getTopInventory().setItem(1, dyes.get(0));

    - solved -
     
    Last edited: May 11, 2015
  10. Offline

    nbrandwine

    Aeyyyyy! Good job dude ^_^
     
Thread Status:
Not open for further replies.

Share This Page