Right Click Listener

Discussion in 'Plugin Development' started by TheDiamondGuy, Dec 31, 2014.

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

    TheDiamondGuy

    Hey,

    I'm trying to make it so that when a user right clicks, they get effects, and their is a set cool down.

    Here is my code:
    Code:
    public class PickaxeClickListener implements Listener {
    
        private static OPPrison plugin = OPPrison.getInstance();
        private ArrayList<UUID> cooldown = new ArrayList<UUID>();
        private ArrayList<ItemStack> picks = new ArrayList<ItemStack>();
    
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
            final UUID uuid = p.getUniqueId();
    
            picks.add(new ItemStack(Material.WOOD_PICKAXE));
            picks.add(new ItemStack(Material.STONE_PICKAXE));
            picks.add(new ItemStack(Material.IRON_PICKAXE));
            picks.add(new ItemStack(Material.GOLD_PICKAXE));
            picks.add(new ItemStack(Material.DIAMOND_PICKAXE));
    
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if (p.getItemInHand().equals(picks)) {
                if (cooldown.contains(uuid));
                p.sendMessage(Color.translate("&c&lForsaken&8&lAnarchy &7>> This ability is currently on cooldown!"));
                return;
                }
            }
            p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 200, 500));
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 3));
            p.sendMessage(Color.translate("&c&lForsaken&8&lAnarchy &7>> &6Activated Pickaxe ability!"));
            cooldown.add(uuid);
    
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    cooldown.remove(uuid);
                    p.sendMessage(Color.translate("&c&lForsaken&8&lAnarchy &7>> &cYour Pickaxe ability has been refreshed!"));
                }
            }, 6000);
            return;
        }
    }
    It seems to let players use the effect, even if they don't have a pick in their hand, even if they didn't right click and block and, even if they are on cooldown.

    Any ideas?

    Thanks!
     
  2. Offline

    mine-care

    On interact event check just if the action is what u want and if the cooldown contains player, if yes cancel the event else allow player to do the operations and put them in the map. As value in the map have a Long with system mills and check if the value from the map plus any amount of time u want the cooldown to be in milliseconds is less than system mills.
     
  3. Offline

    drpk

    @TheDiamondGuy
    Code:
    if (p.getItemInHand().equals(picks)) {
    
    you're comparing an ItemStack to an ArrayList. Make a for loop and iterate through the ArrayList. Then check the itemInHand.
     
  4. @drpk Beat me to it but, yeah. You have to test if the item in their hand is the pick.

    Here is some code that I put together real quick, and I haven't tested it, so it may not work. #SpoonFeeding a little. :)
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Action a = event.getAction();
        ItemStack is = event.getPlayer().getItemInHand();
     
        if (is.equals(picks) && a == Action.RIGHT_CLICK_BLOCK || a == Action.RIGHT_CLICK_AIR && is.getItemMeta().equals(picksMeta) {
        // Whatever happens when they right click
    } else {
        return;
    }
    Hope this helped a little.
     
    Last edited: Dec 31, 2014
  5. Offline

    Shortninja66

    Hey I was wondering about your "p.sendMessage(Color.translate("")" section, where do you get that? I normally use "player.sendMessage(ChatColor.[COLOR] + "[Message]");" and it gets real annoying when you are sending messages with a lot of different colors.

    Never mind, figured it out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  6. Offline

    TheDiamondGuy

    How exactly would this be done?
    Code:
    for (ItemStack i : picks.iterate()) {
    ?

    If anybody else is wandering, its a util class:
    Code:
        public static String translate(String s) {
        return ChatColor.translateAlternateColorCodes('&', s);
    Then by using
    Code:
    CLASSNAME.translate(string);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  7. Offline

    drpk

  8. Offline

    Shortninja66

    I figured it out, I did:

    Code:
        public String colorize(String msg)
        {
            String coloredMsg = "";
            for(int i = 0; i < msg.length(); i++)
            {
                if(msg.charAt(i) == '&')
                    coloredMsg += '§';
                else
                    coloredMsg += msg.charAt(i);
            }
            return coloredMsg;
        }
    Then to add colored messages I did:

    Code:
    player.sendMessage(colorize("&6Orange message."));
     
  9. Offline

    es359

    Or you could just use the ChatColor.translateAlternateColorCodes('&',""); method...
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page