player.sendMessage发送两次

时间:2019-01-27 16:15:05

标签: java minecraft bukkit

右键单击带有修剪剂的食物后,它会说“您已经消耗了修剪剂”,然后再次尝试后显示“您不要过量服用,孩子们”,但是如果食用糖,它会告诉您“您吸食了可卡因”“不要过量服用,孩子们”。似乎是什么问题?

我尝试了单独的类,也在右键单击sendMessage时,如果食用糖则双击鼠标右键。

事件类

package com.maverick.drugPlugin.Events;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;


public class EventsClass implements Listener {
    @EventHandler
    @SuppressWarnings({ "deprecation" })
    public void consumeDrugs(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        Action action = event.getAction();

        if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
            if (player.getItemInHand().getType() == Material.BROWN_MUSHROOM) {
                if(!player.hasPotionEffect(PotionEffectType.CONFUSION)) {
                player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount() - 1);
                player.sendMessage(ChatColor.RED + "You consumed " + ChatColor.BLUE + "shrooms");
                player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 120*20, 1, true, false));
                } else {
                player.sendMessage(ChatColor.DARK_RED + "Don't overdose, kids");
            }
    }
}
        if(action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
            if (player.getItemInHand().getType() == Material.SUGAR) {
                if(!player.hasPotionEffect(PotionEffectType.CONFUSION)) {
                player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount() - 1);
                player.sendMessage(ChatColor.RED + "You snorted " + ChatColor.BLUE + "cocaine");
                player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 120*20, 1, true, false));
                } else {
                player.sendMessage(ChatColor.DARK_RED + "Don't overdose, kids");
                }

            }
        }
    }
}
package com.maverick.drugPlugin;

import org.bukkit.plugin.java.JavaPlugin;

import com.maverick.drugPlugin.Events.EventsClass;

import org.bukkit.ChatColor;

public class Drugs extends JavaPlugin {

    public void onEnable() {
        getServer().getPluginManager().registerEvents(new EventsClass(), this);
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nDrugs are enabled\n\n");
    }

    public void onDisable() {
        getServer().getConsoleSender().sendMessage(ChatColor.RED + "\n\nDrugs are Disabled\n\n");
    }
}

我想要在食用糖(可卡因)时先说“你吸了可卡因”,然后再尝试食用时,它会告诉你“不要过量,孩子们”,就像在蘑菇上一样。

1 个答案:

答案 0 :(得分:0)

PlayerInteract事件可能有点气质,有时会触发一次,两次甚至多次。在这种情况下,我过去使用的解决方案是使用Map<UUID, Long>存储玩家触发互动事件的当前时间戳记System.currentTimeMillis()

然后进行一些测试,并且如果您在短时间内(例如100-200毫秒)内发生了后续的交互事件,则取决于您要采取的行为,那么您可以丢弃它们,这将解决您的双击问题。 / p>