目标是在玩家输入/fly
时启用飞行,并在玩家输入/dfly
时禁用它,但当我/dfly
时没有任何反应。
package ts.fl.me;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public void onEnable() {
getLogger().info("plugin started yaya");
}
public void onDisable() {
getLogger().info("plugin stoped:(");
}
public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("fly")) {
String fly = "fly " + player.getName();
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), fly);
player.setAllowFlight(true);
player.setFlying(true);
player.sendMessage(Color.RED + "you are now flying mate");
}
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("dfly")) {
if(player.isFlying()){
String dfly = "dfly " + player.getName();
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), dfly);
player.setAllowFlight(false);
player.setFlying(false);
player.sendMessage("you are no longer flying!");
}
}
}
return false;
}
}
答案 0 :(得分:0)
对于dfly
命令(根据您的代码),只有当他正在积极飞行时,才会撤销玩家的飞行许可。因此,如果玩家没有飞行(例如在地面上),但是能力飞行,dfly
命令将不会做任何事情,这就是我假设你的意思是使用命令时没有任何事情发生。
如果您打算让dfly
命令同时取消玩家的飞行许可,即使他们当前没有飞行(但有能力),您也可以删除“if (player.isFlying())
“条件。这将反映fly
命令的相同行为,该命令在给予他们许可之前不会检查玩家是否不当前正在飞行。
答案 1 :(得分:0)
只是举一个我所做的例子:D
@SuppressWarnings("rawtypes")
public boolean onCommandFly(CommandSender sender, Command cmd, String label, String[] args) {
if (label.equalsIgnoreCase("fly")) {
Player p = (Player) sender;
if(sender instanceof Player) {
if(p.hasPermission("smpenhance.fly")) {
if(args.length == 1) {
if (args.length > 1) {
p.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', prefix + " &fToo many arguments!"));
} else {
if(p.isFlying() == true) {
Player po = Bukkit.getPlayer(args[0]);
po.setAllowFlight(false);
po.setFlying(false);
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &fFlight disabled for &b" + po + "&r"));
} else {
Player po = Bukkit.getPlayer(args[0]);
po.setAllowFlight(true);
po.setFlying(true);
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &fFlight enabled for &b" + po + "&r"));
}
}
} else {
if(p.isFlying() == true) {
p.setAllowFlight(false);
p.setFlying(false);
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &fFlight disabled"));
} else {
p.setAllowFlight(true);
p.setFlying(true);
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &fFlight enabled"));
}
p.setAllowFlight(true);
p.setFlying(true);
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &fFlight enabled"));
}
} else {
p.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + " &4&lSORRY BUT NO PERMS DUDE!"));
}
} else {
System.out.println(prefix + ChatColor.translateAlternateColorCodes('&', "&4Player only command!"));
}
}
return false;
}