我正在尝试编写一个命令,如果(a)玩家通过单击标志和(b)满足其他条件来运行它,则该命令应该传送玩家。这就是我迄今为止的
CommandExecutor
onCommand
功能:
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("ERROR: You must be a player to use this command.");
return true;
}
if (sender.hasPermission(permission) && /* Other conditions */) { // permission is given as an argument to the command
((Player) sender).teleport(new Location(world, destX, destY, destZ, destYw, destPt)); // These variables are all determined based on the arguments to the command.
return true;
}
return true;
}
这允许任何玩家通过输入
/<name of my custom command>
并使用他们拥有的权限(例如 group.default
)(用于 LuckPerms)来传送到任何地方。我不希望玩家能够使用我的命令来执行此操作。
相比之下,在未修改的 Minecraft 服务器上,如果您使用
给自己一个标志/give @s minecraft:oak_sign{BlockEntityTag:{front_text:{messages:['{"text":"","clickEvent":{"action":"run_command","value":"tp @s 0 100 0"}}','{"text":"MySign"}','{"text":"Select to teleport.","italic":true,"color":"white"}','{"text":""}']},is_waxed:1b}}
然后任何人(即没有op的人)都可以点击标志传送到(0, 100, 0),但他们不能在聊天中运行
/tp 0 100 0
或/tp <anything>
。
如何使用自定义 Bukkit 命令完成此行为? 有没有办法通过右键单击标志来检测玩家是否正在运行命令(如果没有,则返回而不传送),或者是否有更简单的方法来做到这一点?
有这个标志
/give @s minecraft:oak_sign{BlockEntityTag:{front_text:{messages:['{"text":"","clickEvent":{"action":"run_command","value":"tp @s 0 100 0"}}','{"text":"MySign"}','{"text":"Select to teleport.","italic":true,"color":"white"}','{"text":""}']},is_waxed:1b}}
服务器执行命令/tp @s 0 100 0,这就是为什么玩家可以单击该标志,即使他们没有运行/tp命令的权限。
我认为最好的方法是在 Bukkit Listener 中监听 PlayerInteractEvent 而不是命令。
这是当玩家与标志互动时如何传送玩家:
@EventHandler
public void OnInteract(PlayerInteractEvent event)
{
if(event.getClickedBlock().getState() instanceof Sign)
{
//When the player clicked on a Sign
event.getPlayer().teleport(........);
}
}
希望这对您有帮助!