Convert item to color.

Discussion in 'Spigot Plugin Development' started by Marido, May 14, 2017.

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

    I have a small question.

    How would I convert the wool I click in an inventory to color to set the armor

    for example:

    I click green wool in the inventory, then I get leather armor with the color green (because I clicked that).
     
  2. By the way I dont use 'if statements', I'm just creating an EventHandler that detects which DyeColor is clicked and applies it to the item.
     
  3. You would need to check the block data for the wool item, then somehow convert that short value to a hex value to use on the leather armour set.
     
    • Agree Agree x 1
  4. Do you have the convert method for it? like BlockData blabla = etc
     
  5. something like this:
    Code (Text):
    ItemStack wool = new ItemStack(Material.WOOL, 1, (short)14); //red wool
    short colour = wool.getDurability();
    ItemStack helmet = new ItemStack(Material.LEATHER_HELMET, 1);
    LeatherArmorMeta lam = (LeatherArmorMeta) helmet.getItemMeta();
    lam.setColor(Color.fromRGB(???, ???, ???)); //set the armour colour...
    helmet.setItemMeta(lam); //remember to do this for each leather armour peice...
     
  6. You would need to use something like this to get the color of the wool and cast it to the meta:
    Code (Text):
    switch(colour) {
        case 14:
        lam.setColor(Color.RED); //find out the short values and convert them to colours, e.g. 14 = red
    }
     
  7. The method fromRGB(int) in the type Color is not applicable for the arguments ()
     
  8. I get an error and it only gives a black helmet help
     
  9. Post your final code so far please?
     
  10. Code (Text):
    @EventHandler
    public void setManualColor(InventoryClickEvent event) {
    if (event.getCurrentItem().getType() == Material.WOOL) {
    if (event.getWhoClicked().hasPermission("colorpicker.use")) {
    short colour = event.getCurrentItem().getDurability();
    ItemStack helmet = new ItemStack(Material.LEATHER_HELMET, 1);
    LeatherArmorMeta lam = (LeatherArmorMeta) helmet.getItemMeta();
    lam.setColor(Color.fromRGB(colour)); //set the armour colour...
    helmet.setItemMeta(lam); //remember to do this for each leather armour peice...
    event.getWhoClicked().getInventory().addItem(helmet);
    }
    }
    }
     
  11. Alright and what variable has color to be in switch(color)? Durability of the item?
     
  12. durability of the wool in the gui :)
     
  13. Thanks, I fixed it with the durability and switch mthod
     
  14. Thank you very much!
     
  15. Isn't this one a bit better?

    Code (Text):

    ItemStack is = new ItemStack(Material.WOOL, 1,  3);
    ItemStack chestPlate = new ItemStack(Material.LEATHER_CHESTPLATE);

    Wool wData = (Wool) is.getData();
    LeatherArmorMeta lma = (LeatherArmorMeta) chestPlate.getItemMeta();

    lma.setColor(wData.getColor().getColor());
    chestPlate.setItemMeta(lma);
     
     
Thread Status:
Not open for further replies.