Solved Same Material With Switches

Discussion in 'Spigot Plugin Development' started by MaxNatural, May 29, 2016.

Thread Status:
Not open for further replies.
  1. I have made a inventory and I want to use the same material but with my switches you can only use the item once, Is there a better way to do this or some type of fix? Thank you :)

    Code (Text):
                case LEATHER_CHESTPLATE:
                    if (player.hasPermission("costume.chest")) {
                        ItemStack mchest = new ItemStack(Material.LEATHER_CHESTPLATE);
                        LeatherArmorMeta mclMeta = (LeatherArmorMeta)mchest.getItemMeta();
                        mclMeta.setColor(Color.WHITE);
                        mchest.setItemMeta(mclMeta);
                        ItemMeta mchestMeta = mchest.getItemMeta();
                        mchestMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Chestplate");
                        List<String> mchestLore = new ArrayList<String>();
                        mchestLore.add(ChatColor.GRAY + "" + ChatColor.ITALIC + ";)");
                        mchestMeta.setLore(mchestLore);
                        mchest.setItemMeta(mchestMeta);

                       
                        player.closeInventory();
                        player.getInventory().setChestplate(mchest);
                        //player.sendMessage(Hub.prefix + "You have equipped the " + ChatColor.RED + "" + ChatColor.ITALIC + "Slime Block" + ChatColor.GRAY + " hat!");

                    } else {
                        player.sendMessage(Core.noperm);
                        player.playSound(player.getLocation(), Sound.BLOCK_NOTE_PLING, 1.0F, 1.0F);
                    }
     
  2. What exactly do you want to do?
     
  3. You can still switch on the material just inside the cases for duplicated materials preform a second check on the item name or data value.
    Code (Text):
    case STONE:
        if (...name().equals("1st item that is stone") {
            ...
        } else if (...name().equals("2nd item that is stone"){
            ...
        }
        break;
     
  4. What would go before the name? "...name" I have tried everything :/
     
  5. I was just writing a bit of pseudocode to explain my point. You would get the item's display name:
    Code (Text):
    ItemMeta meta = item.getItemMeta();
    if (meta.hasDisplayName()) {
        String name = meta.getDisplayName();
    }
    I assumed you knew what I was talking about when I saw
    Code (Text):
     mchestMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Chestplate");
    in your original post but I guess "name()" was a bit misleading, sorry about that :p
     
  6. I came up with this... It's almost getting here. Is there anything I'm doing wrong because it still sends the same message to all of them
    Thank you :)

    Code (Text):

            switch (event.getCurrentItem().getType()) {
            case STAINED_GLASS_PANE:
                ItemStack red = new ItemStack(Material.STAINED_GLASS_PANE, 1);
                red.setDurability((byte) 14);
                ItemMeta redMeta = red.getItemMeta();
                redMeta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "RED REALM");
                List<String> redLore = new ArrayList<String>();
                redLore.add(ChatColor.GRAY + "Click here to to join our " + ChatColor.RED + "custom enchantments" + ChatColor.GRAY + " faction server!");
                redMeta.setLore(redLore);
                red.setItemMeta(redMeta);
                red.getItemMeta().getDisplayName();
               
                final ItemStack green = new ItemStack(Material.STAINED_GLASS_PANE, 1);
                green.setDurability((byte) 3);
                ItemMeta greenMeta = green.getItemMeta();
                greenMeta.setDisplayName(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "GREEN REALM");
                List<String> greenLore = new ArrayList<String>();
                greenLore.add(ChatColor.GRAY + "Click here to to join our " + ChatColor.RED + "custom enchantments" + ChatColor.GRAY + " faction server!");
                greenMeta.setLore(greenLore);
                green.setItemMeta(greenMeta);
                green.getItemMeta().getDisplayName();
               
               
                if (redMeta.hasDisplayName()) {
                    String name = redMeta.getDisplayName();
                    player.sendMessage("Test1");
               
                } else if (greenMeta.hasDisplayName()) {
                    String namegreen = greenMeta.getDisplayName();
                    player.sendMessage("Test2");
                }

               
                break;
     
     
  7. always check null

    Code (Java):
    if ( item has no displayname  ) return; if ( item in hand is null ) return; if ( the inventory is null ) return;
     
  8. Will do thank you :D Do you have any idea why this isn't working?
     
  9. Code (Java):
    e.getCurrentItem.getItemMeta().getDisplayName().equals(redMeta.getDisplayName()) {
      // do something
    }
     
  10. Got it working perfect! Thank you guys! :D
     
  11. what about marking the thread as solved ?
     
  12. Thread close please :)
     
Thread Status:
Not open for further replies.