SteamWar/FightSystem
Archiviert
13
1

Add GamemodeCommand #162

Manuell gemergt
Lixfel hat 1 Commits von Gamemode nach master 2020-06-03 08:45:37 +02:00 zusammengeführt
3 geänderte Dateien mit 123 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,6 @@
package de.steamwar.fightsystem; package de.steamwar.fightsystem;
import de.steamwar.core.CommandRemover;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import de.steamwar.fightsystem.commands.*; import de.steamwar.fightsystem.commands.*;
import de.steamwar.fightsystem.countdown.*; import de.steamwar.fightsystem.countdown.*;
@ -23,6 +24,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
public class FightSystem extends JavaPlugin { public class FightSystem extends JavaPlugin {
@ -47,6 +49,15 @@ public class FightSystem extends JavaPlugin {
TechHider.init(); TechHider.init();
FightScoreboard.init(); FightScoreboard.init();
try {
CommandRemover.removeAll("gamemode");
CommandInjector.injectCommand(new GamemodeCommand());
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Failed to replace commands", e);
Bukkit.shutdown();
return;
}
new EntityDamageListener(); new EntityDamageListener();
new EntityExplodeListener(); new EntityExplodeListener();
new FoodLevelChangeListener(); new FoodLevelChangeListener();

Datei anzeigen

@ -0,0 +1,24 @@
package de.steamwar.fightsystem.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import java.lang.reflect.Field;
public class CommandInjector {
private CommandInjector(){}
private static final String PACKAGE_NAME = Bukkit.getServer().getClass().getPackage().getName();
private static final String VERSION = PACKAGE_NAME.substring(PACKAGE_NAME.lastIndexOf('.') + 1);
public static void injectCommand(Command cmd) throws Exception {
Class serverClass = Class.forName("org.bukkit.craftbukkit." + VERSION + ".CraftServer");
Field f1 = serverClass.getDeclaredField("commandMap");
f1.setAccessible(true);
SimpleCommandMap commandMap = (SimpleCommandMap) f1.get(Bukkit.getServer());
commandMap.register("BauSystem", cmd);
}
}

Datei anzeigen

@ -0,0 +1,88 @@
package de.steamwar.fightsystem.commands;
import com.google.common.collect.ImmutableList;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import org.apache.commons.lang.Validate;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
public class GamemodeCommand extends BukkitCommand {
private static final List<String> GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival",
"spectator");
public GamemodeCommand() {
super("gamemode");
List<String> aliases = new ArrayList<>();
aliases.add("gm");
this.setAliases(aliases);
this.description = "Ändert den Spielmodus eines Spielers";
this.usageMessage = "/gm [Spielmodus]";
}
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!(sender instanceof Player)) {
return false;
}else if (args.length == 0) {
sender.sendMessage(FightSystem.PREFIX + this.usageMessage);
return false;
}
Player p = (Player) sender;
if (!(Config.test() || p == FightSystem.getEventLeiter())) {
p.sendMessage(FightSystem.PREFIX + "§cDu darfst hier deinen Spielmodus nicht ändern");
return false;
}
GameMode mode = createMode(args[0]);
if(mode == null){
p.sendMessage(FightSystem.PREFIX + "§cUnbekannter Spielmodus");
return false;
}
p.setGameMode(mode);
return true;
}
@SuppressWarnings("deprecation")
private GameMode createMode(String modeArg){
try {
return GameMode.getByValue(Integer.parseInt(modeArg));
} catch (NumberFormatException ignored) {
if ((modeArg.equalsIgnoreCase("creative")) || (modeArg.equalsIgnoreCase("c")))
return GameMode.CREATIVE;
else if ((modeArg.equalsIgnoreCase("adventure")) || (modeArg.equalsIgnoreCase("a")))
return GameMode.ADVENTURE;
else if ((modeArg.equalsIgnoreCase("spectator")) || (modeArg.equalsIgnoreCase("sp")))
return GameMode.SPECTATOR;
else if ((modeArg.equalsIgnoreCase("survival")) || (modeArg.equalsIgnoreCase("s")))
return GameMode.SURVIVAL;
}
return null;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(args, "Arguments cannot be null");
Validate.notNull(alias, "Alias cannot be null");
if (args.length == 1)
return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES,
new ArrayList<>(GAMEMODE_NAMES.size()));
if (args.length == 2) {
return super.tabComplete(sender, alias, args);
}
return ImmutableList.of();
}
}