Gems plugin

Discussion in 'Spigot Plugin Development' started by josh13_, Jul 13, 2018.

  1. okay soo im having little problem making Gems plugin...
    Code (Java):
        @EventHandler
        public void PlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            ItemStack item = event.getItem();
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&a&lGreen Gem"));
            meta.setLore(Arrays.asList(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II"));
            item.setItemMeta(meta);
            if(player.getItemInHand() != null){
                if(event.getAction() == Action.RIGHT_CLICK_AIR) {
                    if(player.getItemInHand().getType() == Material.EMERALD) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 700, 1));
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 1));
                        player.getInventory().remove(item);
                    }
                }
            }
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event){
            Player p = event.getPlayer();

            ItemStack i = new ItemStack(Material.EMERALD);
            ItemMeta m = i.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II");
            m.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&aGreen Gem"));
            m.setLore(lore);
            i.setItemMeta(m);

            p.getInventory().addItem(i);
        }
    }
    that my current Code. i want it to only work on item that has same Meta Data and i want item to be removed after used. i tried everything i knew but it didnt worked as i wanted. please help. im kind of new in coding soo i might have some dub mistake
     
  2. What exactly is your problem?
     
  3. To make sure an item is the same, the ItemStack’s equals method has been overrides, i think, so to make sure two ItemStacks are the same you can do a.equals(b);

    Store your ItemStack as a variable somewhere, and when you need to compare, simply do storedItemStack.equals(comparing); and it will return true if it’s the same
     
  4. it removes all items that is same Material type and it works on all kind of emerald when i want it to only work items with name "Green Gem"
    sorry for bad grammar
     
  5. Checking the name isn't reliable cause of anvils. But checking the lore and the name is a lot better.
    Code (Java):
    @EventHandler
        public void PlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            ItemStack item = event.getItem();

            //Checks if the item is an emerald block and if it has item meta
            if (item.getType() == Material.EMERALD && item.hasItemMeta())
            {
                ItemMeta meta = item.getItemMeta();
                Action action = event.getAction();
                List<String> lore = meta.getLore();
               

                //Added check for right_click_block and checks the lore and if the item's display name contains green gem
                if (meta.getDisplayName().contains("Green Gem") && lore != null && lore.get(0).equals(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II") && (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK))
                {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 700, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 1));
                    player.getInventory().remove(item);
                }
            }
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event)
        {
            Player p = event.getPlayer();

            ItemStack item = new ItemStack(Material.EMERALD);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&aGreen Gem"));
            //It's recommended to use singletonList for a single string
            meta.setLore(Collections.singletonList(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II"));
            item.setItemMeta(meta);

            p.getInventory().addItem(item);

        }
     
  6. Am i missing something? Just compare ItemStacks, declare a constant that’s the green gem and then just compare ItemStacks.
     
  7. declare a constant? you mean a global variable right?
     
  8. I mean, somewhere in his plugin, perhaps a utility class, make a public static final ItemStack of his gem Item, if he has multiple he could even use an Enum
     
  9. when i pasted that code it is getting error on this line

    meta.setLore(Collections.emptyList().singletonList(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II"));
    it says "Cant resolve sybol "singletonList""
     
  10. I try to avoid global variables but it would simplify it.
    Code (Java):
    private static final ItemStack item;
     
        EventListener(Main plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            item = new ItemStack(Material.EMERALD);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&aGreen Gem"));
            //It's recommended to use singletonList for a single string (apart from in this case cause the setLore methods doesnt support it)
            meta.setLore(Arrays.asList(ChatColor.DARK_PURPLE + "This Gem Gives Jump II And Speed II"));
            item.setItemMeta(meta);
        }
     
        @EventHandler
        public void PlayerInteract(PlayerInteractEvent event)
        {
            if (item == event.getItem() && (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK))
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 700, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 1));
                player.getInventory().remove(item);
            }
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event)
        {
            event.getPlayer().getInventory().addItem(item);
        }
     
    #10 AgainstTheNight, Jul 13, 2018
    Last edited: Jul 13, 2018
  11. Make it static & final so that way you only create it once, more efficient ;)
     
  12. Players could rename the item and even change the lores if you decide to add plugin that supports that. What you could do is to make lore invisible by adding the section sign before every letter something like §s§e§c§r§e§t and check for that.
     
  13. Just use the normal list then :) (Arrays.asList())
    Good shout :) I should've done. Fixed it now :)
     
    • Like Like x 1
  14. I would also implement a cooldown for this ^

    Hint: System.currentTimeMillis()
     
  15. The best way (in my opinion) would actually be using an alphanumeric tag. That way its not likely to conflict with other plugins. Though if the player places the emerald block it will loose its NBT data (which is such a pain).
     
  16. ayyy thanks all of you it works now :D
    thanks!!!
     
    • Like Like x 1