3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-18 14:00:05 +01:00
Paper/src/main/java/net/minecraft/server/ConsoleCommandHandler.java

370 Zeilen
20 KiB
Java

2011-02-23 03:37:56 +01:00
package net.minecraft.server;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;
2011-05-26 14:48:22 +02:00
2011-06-12 00:02:58 +02:00
// CraftBukkit start
import java.util.List;
import org.bukkit.craftbukkit.command.ServerCommandListener;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.command.CommandSender;
2011-06-12 00:02:58 +02:00
// CraftBukkit end
2011-02-23 03:37:56 +01:00
public class ConsoleCommandHandler {
private static Logger a = Logger.getLogger("Minecraft");
private MinecraftServer server;
private ICommandListener listener; // CraftBukkit
2011-02-23 03:37:56 +01:00
public ConsoleCommandHandler(MinecraftServer minecraftserver) {
this.server = minecraftserver;
2011-02-23 03:37:56 +01:00
}
public boolean handle(ServerCommand servercommand) { // CraftBukkit - returns boolean
String s = servercommand.command;
2011-02-23 03:37:56 +01:00
ICommandListener icommandlistener = servercommand.b;
String s1 = icommandlistener.getName();
listener = icommandlistener; // CraftBukkit
ServerConfigurationManager serverconfigurationmanager = this.server.serverConfigurationManager;
2011-02-23 03:37:56 +01:00
if (!s.toLowerCase().startsWith("help") && !s.toLowerCase().startsWith("?")) {
if (s.toLowerCase().startsWith("list")) {
icommandlistener.sendMessage("Connected players: " + serverconfigurationmanager.c());
2011-02-23 03:37:56 +01:00
} else if (s.toLowerCase().startsWith("stop")) {
this.print(s1, "Stopping the server..");
this.server.a();
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
int i;
WorldServer worldserver;
if (s.toLowerCase().startsWith("save-all")) {
this.print(s1, "Forcing save..");
if (serverconfigurationmanager != null) {
serverconfigurationmanager.savePlayers();
}
2011-06-12 00:02:58 +02:00
// CraftBukkit start
2011-05-26 14:48:22 +02:00
for (i = 0; i < this.server.worlds.size(); ++i) {
worldserver = this.server.worlds.get(i);
boolean save = worldserver.E;
worldserver.E = false;
worldserver.save(true, (IProgressUpdate) null);
worldserver.E = save;
}
// CraftBukkit end
this.print(s1, "Save complete.");
} else if (s.toLowerCase().startsWith("save-off")) {
this.print(s1, "Disabling level saving..");
2011-06-12 00:02:58 +02:00
for (i = 0; i < this.server.worlds.size(); ++i) { // CraftBukkit
worldserver = this.server.worlds.get(i); // CraftBukkit
2011-05-26 14:48:22 +02:00
worldserver.E = true;
}
} else if (s.toLowerCase().startsWith("save-on")) {
this.print(s1, "Enabling level saving..");
2011-06-12 00:02:58 +02:00
for (i = 0; i < this.server.worlds.size(); ++i) { // CraftBukkit
worldserver = this.server.worlds.get(i); // CraftBukkit
2011-05-26 14:48:22 +02:00
worldserver.E = false;
}
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
String s2;
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (s.toLowerCase().startsWith("op ")) {
2011-02-23 03:37:56 +01:00
s2 = s.substring(s.indexOf(" ")).trim();
2011-05-26 14:48:22 +02:00
serverconfigurationmanager.e(s2);
this.print(s1, "Opping " + s2);
serverconfigurationmanager.a(s2, "\u00A7eYou are now op!");
} else if (s.toLowerCase().startsWith("deop ")) {
2011-02-23 03:37:56 +01:00
s2 = s.substring(s.indexOf(" ")).trim();
2011-05-26 14:48:22 +02:00
serverconfigurationmanager.f(s2);
serverconfigurationmanager.a(s2, "\u00A7eYou are no longer op!");
this.print(s1, "De-opping " + s2);
} else if (s.toLowerCase().startsWith("ban-ip ")) {
s2 = s.substring(s.indexOf(" ")).trim();
serverconfigurationmanager.c(s2);
this.print(s1, "Banning ip " + s2);
} else if (s.toLowerCase().startsWith("pardon-ip ")) {
s2 = s.substring(s.indexOf(" ")).trim();
serverconfigurationmanager.d(s2);
this.print(s1, "Pardoning ip " + s2);
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
EntityPlayer entityplayer;
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (s.toLowerCase().startsWith("ban ")) {
s2 = s.substring(s.indexOf(" ")).trim();
serverconfigurationmanager.a(s2);
this.print(s1, "Banning " + s2);
entityplayer = serverconfigurationmanager.i(s2);
2011-02-23 03:37:56 +01:00
if (entityplayer != null) {
2011-05-26 14:48:22 +02:00
entityplayer.netServerHandler.disconnect("Banned by admin");
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} else if (s.toLowerCase().startsWith("pardon ")) {
s2 = s.substring(s.indexOf(" ")).trim();
serverconfigurationmanager.b(s2);
this.print(s1, "Pardoning " + s2);
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
int j;
if (s.toLowerCase().startsWith("kick ")) {
// CraftBukkit start - Add kick message compatibility
String[] parts = s.split(" ");
s2 = parts.length >= 2 ? parts[1] : "";
// CraftBukkit end
entityplayer = null;
for (j = 0; j < serverconfigurationmanager.players.size(); ++j) {
EntityPlayer entityplayer1 = (EntityPlayer) serverconfigurationmanager.players.get(j);
if (entityplayer1.name.equalsIgnoreCase(s2)) {
entityplayer = entityplayer1;
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
}
if (entityplayer != null) {
entityplayer.netServerHandler.disconnect("Kicked by admin");
this.print(s1, "Kicking " + entityplayer.name);
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
icommandlistener.sendMessage("Can\'t find user " + s2 + ". No kick.");
2011-02-23 03:37:56 +01:00
}
} else {
2011-05-26 14:48:22 +02:00
EntityPlayer entityplayer2;
String[] astring;
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (s.toLowerCase().startsWith("tp ")) {
2011-02-23 03:37:56 +01:00
astring = s.split(" ");
2011-05-26 14:48:22 +02:00
if (astring.length == 3) {
entityplayer = serverconfigurationmanager.i(astring[1]);
entityplayer2 = serverconfigurationmanager.i(astring[2]);
if (entityplayer == null) {
icommandlistener.sendMessage("Can\'t find user " + astring[1] + ". No tp.");
} else if (entityplayer2 == null) {
icommandlistener.sendMessage("Can\'t find user " + astring[2] + ". No tp.");
} else if (entityplayer.dimension != entityplayer2.dimension) {
icommandlistener.sendMessage("User " + astring[1] + " and " + astring[2] + " are in different dimensions. No tp.");
} else {
entityplayer.netServerHandler.a(entityplayer2.locX, entityplayer2.locY, entityplayer2.locZ, entityplayer2.yaw, entityplayer2.pitch);
this.print(s1, "Teleporting " + astring[1] + " to " + astring[2] + ".");
}
} else {
icommandlistener.sendMessage("Syntax error, please provice a source and a target.");
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} else {
String s3;
int k;
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (s.toLowerCase().startsWith("give ")) {
astring = s.split(" ");
if (astring.length != 3 && astring.length != 4) {
return true; // CraftBukkit
}
s3 = astring[1];
entityplayer2 = serverconfigurationmanager.i(s3);
if (entityplayer2 != null) {
try {
k = Integer.parseInt(astring[2]);
if (Item.byId[k] != null) {
this.print(s1, "Giving " + entityplayer2.name + " some " + k);
int l = 1;
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (astring.length > 3) {
l = this.a(astring[3], 1);
}
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
if (l < 1) {
l = 1;
}
if (l > 64) {
l = 64;
}
entityplayer2.b(new ItemStack(k, l, 0));
} else {
icommandlistener.sendMessage("There\'s no item with id " + k);
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} catch (NumberFormatException numberformatexception) {
icommandlistener.sendMessage("There\'s no item with id " + astring[2]);
}
} else {
icommandlistener.sendMessage("Can\'t find user " + s3);
}
} else if (s.toLowerCase().startsWith("time ")) {
astring = s.split(" ");
if (astring.length != 3) {
return true; // CraftBukkit
}
s3 = astring[1];
2011-02-23 03:37:56 +01:00
2011-05-26 14:48:22 +02:00
try {
j = Integer.parseInt(astring[2]);
WorldServer worldserver1;
if ("add".equalsIgnoreCase(s3)) {
2011-06-12 00:02:58 +02:00
for (k = 0; k < this.server.worlds.size(); ++k) { // CraftBukkit
worldserver1 = this.server.worlds.get(k); // CraftBukkit
2011-05-26 14:48:22 +02:00
worldserver1.setTime(worldserver1.getTime() + (long) j);
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
this.print(s1, "Added " + j + " to time");
} else if ("set".equalsIgnoreCase(s3)) {
2011-06-12 00:02:58 +02:00
for (k = 0; k < this.server.worlds.size(); ++k) { // CraftBukkit
worldserver1 = this.server.worlds.get(k); // CraftBukkit
2011-05-26 14:48:22 +02:00
worldserver1.setTime((long) j);
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
this.print(s1, "Set time to " + j);
2011-02-23 03:37:56 +01:00
} else {
2011-05-26 14:48:22 +02:00
icommandlistener.sendMessage("Unknown method, use either \"add\" or \"set\"");
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} catch (NumberFormatException numberformatexception1) {
icommandlistener.sendMessage("Unable to convert time value, " + astring[2]);
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} else if (s.toLowerCase().startsWith("say ")) {
2011-02-23 03:37:56 +01:00
s = s.substring(s.indexOf(" ")).trim();
2011-05-26 14:48:22 +02:00
a.info("[" + s1 + "] " + s);
serverconfigurationmanager.sendAll(new Packet3Chat("\u00A7d[Server] " + s));
} else if (s.toLowerCase().startsWith("tell ")) {
astring = s.split(" ");
if (astring.length >= 3) {
s = s.substring(s.indexOf(" ")).trim();
s = s.substring(s.indexOf(" ")).trim();
a.info("[" + s1 + "->" + astring[1] + "] " + s);
s = "\u00A77" + s1 + " whispers " + s;
a.info(s);
if (!serverconfigurationmanager.a(astring[1], (Packet) (new Packet3Chat(s)))) {
icommandlistener.sendMessage("There\'s no player by that name online.");
}
2011-02-23 03:37:56 +01:00
}
2011-05-26 14:48:22 +02:00
} else if (s.toLowerCase().startsWith("whitelist ")) {
this.a(s1, s, icommandlistener);
} else {
icommandlistener.sendMessage("Unknown console command. Type \"help\" for help."); // CraftBukkit
return false; // CraftBukkit
2011-02-23 03:37:56 +01:00
}
}
}
}
}
}
}
} else {
this.a(icommandlistener);
}
return true; // CraftBukkit
2011-02-23 03:37:56 +01:00
}
private void a(String s, String s1, ICommandListener icommandlistener) {
String[] astring = s1.split(" ");
listener = icommandlistener; // CraftBukkit
2011-02-23 03:37:56 +01:00
if (astring.length >= 2) {
String s2 = astring[1].toLowerCase();
if ("on".equals(s2)) {
this.print(s, "Turned on white-listing");
this.server.propertyManager.b("white-list", true);
2011-02-23 03:37:56 +01:00
} else if ("off".equals(s2)) {
this.print(s, "Turned off white-listing");
this.server.propertyManager.b("white-list", false);
2011-02-23 03:37:56 +01:00
} else if ("list".equals(s2)) {
Set set = this.server.serverConfigurationManager.e();
2011-02-23 03:37:56 +01:00
String s3 = "";
String s4;
for (Iterator iterator = set.iterator(); iterator.hasNext(); s3 = s3 + s4 + " ") {
s4 = (String) iterator.next();
}
icommandlistener.sendMessage("White-listed players: " + s3);
2011-02-23 03:37:56 +01:00
} else {
String s5;
if ("add".equals(s2) && astring.length == 3) {
s5 = astring[2].toLowerCase();
this.server.serverConfigurationManager.k(s5);
this.print(s, "Added " + s5 + " to white-list");
2011-02-23 03:37:56 +01:00
} else if ("remove".equals(s2) && astring.length == 3) {
s5 = astring[2].toLowerCase();
this.server.serverConfigurationManager.l(s5);
this.print(s, "Removed " + s5 + " from white-list");
2011-02-23 03:37:56 +01:00
} else if ("reload".equals(s2)) {
this.server.serverConfigurationManager.f();
this.print(s, "Reloaded white-list from file");
2011-02-23 03:37:56 +01:00
}
}
}
}
private void a(ICommandListener icommandlistener) {
icommandlistener.sendMessage("To run the server without a gui, start it like this:");
icommandlistener.sendMessage(" java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui");
icommandlistener.sendMessage("Console commands:");
icommandlistener.sendMessage(" help or ? shows this message");
icommandlistener.sendMessage(" kick <player> removes a player from the server");
icommandlistener.sendMessage(" ban <player> bans a player from the server");
icommandlistener.sendMessage(" pardon <player> pardons a banned player so that they can connect again");
icommandlistener.sendMessage(" ban-ip <ip> bans an IP address from the server");
icommandlistener.sendMessage(" pardon-ip <ip> pardons a banned IP address so that they can connect again");
icommandlistener.sendMessage(" op <player> turns a player into an op");
icommandlistener.sendMessage(" deop <player> removes op status from a player");
icommandlistener.sendMessage(" tp <player1> <player2> moves one player to the same location as another player");
icommandlistener.sendMessage(" give <player> <id> [num] gives a player a resource");
icommandlistener.sendMessage(" tell <player> <message> sends a private message to a player");
icommandlistener.sendMessage(" stop gracefully stops the server");
icommandlistener.sendMessage(" save-all forces a server-wide level save");
icommandlistener.sendMessage(" save-off disables terrain saving (useful for backup scripts)");
icommandlistener.sendMessage(" save-on re-enables terrain saving");
icommandlistener.sendMessage(" list lists all currently connected players");
icommandlistener.sendMessage(" say <message> broadcasts a message to all players");
icommandlistener.sendMessage(" time <add|set> <amount> adds to or sets the world time (0-24000)");
}
private void print(String s, String s1) {
2011-02-23 03:37:56 +01:00
String s2 = s + ": " + s1;
// CraftBukkit start
listener.sendMessage(s1);
informOps("\u00A77(" + s2 + ")");
if (listener instanceof MinecraftServer) {
return; // Already logged so don't call a.info()
}
// CraftBukkit end
2011-02-23 03:37:56 +01:00
a.info(s2);
}
// CraftBukkit start
private void informOps(String msg) {
Packet3Chat packet3chat = new Packet3Chat(msg);
EntityPlayer sender = null;
if (listener instanceof ServerCommandListener) {
CommandSender commandSender = ((ServerCommandListener) listener).getSender();
if (commandSender instanceof CraftPlayer) {
sender = ((CraftPlayer) commandSender).getHandle();
}
}
List<EntityPlayer> players = server.serverConfigurationManager.players;
for (int i = 0; i < players.size(); ++i) {
EntityPlayer entityPlayer = (EntityPlayer) players.get(i);
if (sender != entityPlayer && server.serverConfigurationManager.isOp(entityPlayer.name)) {
entityPlayer.netServerHandler.sendPacket(packet3chat);
}
}
}
// CraftBukkit end
2011-02-23 03:37:56 +01:00
private int a(String s, int i) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException numberformatexception) {
return i;
}
}
}