Java 在PlayerMoveEvent中仅执行p.sendMessage一次
当playerMoveEvent执行时,它会在他每次走动时发送一条取消消息。能否只发送一次 以下是主要课程:Java 在PlayerMoveEvent中仅执行p.sendMessage一次,java,bukkit,Java,Bukkit,当playerMoveEvent执行时,它会在他每次走动时发送一条取消消息。能否只发送一次 以下是主要课程: package me.mortadelle2.togglePVP; import java.util.ArrayList; import java.util.HashMap; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender;
package me.mortadelle2.togglePVP;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class PVP extends JavaPlugin{
public int turnOn;
public int turnOff;
ArrayList<String> noPVP = new ArrayList<String>();
HashMap<String, Integer> stepCounter = new HashMap<String, Integer>();
public void onEnable(){
new MainEvents(this);
getLogger().info("You may now toggle PVP!");
}
public void onDisable(){
getLogger().info("You may no longer toggle PVP ;(");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
final Player p = (Player) sender;
if (cmd.getName().equalsIgnoreCase("pvp") && p.hasPermission("togglepvp.toggle")){
if (args.length == 0){
p.sendMessage(ChatColor.RED + "Invalid usage! /pvp [on or off]");
return true;
}
if (args.length == 1){
if (args[0].equalsIgnoreCase("on")){
stepCounter.put(p.getName(), 0);
p.sendMessage(ChatColor.RED + "PVP " + ChatColor.YELLOW + "will be turned on in 5 seconds! Don't move!");
turnOn = this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
noPVP.remove(p.getName());
p.sendMessage(ChatColor.RED + "PVP" + ChatColor.YELLOW + " has been turned on!");
}
}, 100L);
return true;
}
if (args[0].equalsIgnoreCase("off") && p.hasPermission("togglepvp.toggle")) {
stepCounter.put(p.getName(), 0);
p.sendMessage(ChatColor.RED + "PVP " + ChatColor.YELLOW
+ "will be turned off in 5 seconds! Don't move!");
turnOff = this.getServer().getScheduler()
.scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
noPVP.add(p.getName());
p.sendMessage(ChatColor.RED + "PVP"
+ ChatColor.YELLOW
+ " has been turned off!");
}
}, 100L);
return true;
}
}
}
return false;
}
}
我试着计算他的步数,在他走路时递增,但这不起作用,所以我无法想象会发生什么。你可以从删除所有全局变量开始,然后创建一个公共静态数组列表,用于保存所有启用pvp保护的玩家,以及一个用于等待更改状态的玩家
public static ArrayList<String> noPVP = new ArrayList<String>();//Holds players that have pvp protection enabled
public static ArrayList<String> playersWaiting = new ArrayList<String>();//Holds players waiting to change states
如果玩家移动事件改变了位置,则应将其从地图中完全移除:
@EventHandler
public void playerMovesSoCancelsTask(PlayerMoveEvent e) {
Player player = e.getPlayer();//Get the player
//Check if the player is moving to a new block
if (e.getTo().getBlockX() != e.getFrom().getBlockX() || e.getTo().getBlockY() != e.getFrom().getBlockY() || e.getTo().getBlockZ() != e.getFrom().getBlockZ()) {
//Check if the player is waiting to turn on/off pvp
if (PVP.playersWaiting.contains(player.getName())) {
player.sendMessage(ChatColor.RED + "You moved so the action was cancelled!");
PVP.playersWaiting.remove(player.getName()); //Remove the player from the waiting list
}
}
}
我注意到的其他几个问题:
1) 你在没有任何检查的情况下向玩家施放CommandSender。尝试从控制台执行命令,看看会发生什么。谷歌搜索应该告诉你如何解决这个问题
2) 在侦听器的playerDamagesWhenToggleOff
方法中,检查播放器本身,而不是其名称。确保使用它们的名称:if(Main.novp.containsKey(被害人.getName()){e.setCancelled()}
3) 从不从变量中删除播放器,请确保创建新的playerquiteevent
,并从所有全局变量中删除播放器
if (args[0].equalsIgnoreCase("on")) {
player.sendMessage(ChatColor.YELLOW + "PVP will be turned on in 5 seconds! Don't move!");
playersWaiting.add(player.getName());
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
if (playersWaiting.contains(player.getName())) {
playersWaiting.remove(player.getName());
noPVP.remove(player.getName());
player.sendMessage(ChatColor.YELLOW + "You have turned PVP on!");
}
}
}, 100L);
return true;
} else if (args[0].equalsIgnoreCase("off")) {
player.sendMessage(ChatColor.YELLOW + "PVP will be turned off in 5 seconds. Don't move!");
playersWaiting.add(player.getName());
this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
if (playersWaiting.contains(player.getName())) {
playersWaiting.remove(player.getName());
noPVP.add(player.getName());
player.sendMessage(ChatColor.YELLOW + "You have turned PVP off!");
}
}
}, 100L);
return true;
}
@EventHandler
public void playerMovesSoCancelsTask(PlayerMoveEvent e) {
Player player = e.getPlayer();//Get the player
//Check if the player is moving to a new block
if (e.getTo().getBlockX() != e.getFrom().getBlockX() || e.getTo().getBlockY() != e.getFrom().getBlockY() || e.getTo().getBlockZ() != e.getFrom().getBlockZ()) {
//Check if the player is waiting to turn on/off pvp
if (PVP.playersWaiting.contains(player.getName())) {
player.sendMessage(ChatColor.RED + "You moved so the action was cancelled!");
PVP.playersWaiting.remove(player.getName()); //Remove the player from the waiting list
}
}
}