From 4e8152d28220c471d464128df9f3c2e89efa6d00 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 31 May 2020 18:56:50 +0200 Subject: [PATCH] Add GamemodeCommand --- .../de/steamwar/fightsystem/FightSystem.java | 11 +++ .../fightsystem/commands/CommandInjector.java | 24 +++++ .../fightsystem/commands/GamemodeCommand.java | 88 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/commands/CommandInjector.java create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/commands/GamemodeCommand.java diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index fa6e642..0e0fe09 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -1,5 +1,6 @@ package de.steamwar.fightsystem; +import de.steamwar.core.CommandRemover; import de.steamwar.core.Core; import de.steamwar.fightsystem.commands.*; import de.steamwar.fightsystem.countdown.*; @@ -23,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.logging.Level; public class FightSystem extends JavaPlugin { @@ -47,6 +49,15 @@ public class FightSystem extends JavaPlugin { TechHider.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 EntityExplodeListener(); new FoodLevelChangeListener(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/commands/CommandInjector.java b/FightSystem_Main/src/de/steamwar/fightsystem/commands/CommandInjector.java new file mode 100644 index 0000000..7864bec --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/commands/CommandInjector.java @@ -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); + } +} + diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/commands/GamemodeCommand.java b/FightSystem_Main/src/de/steamwar/fightsystem/commands/GamemodeCommand.java new file mode 100644 index 0000000..97de445 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/commands/GamemodeCommand.java @@ -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 GAMEMODE_NAMES = ImmutableList.of("adventure", "creative", "survival", + "spectator"); + + public GamemodeCommand() { + super("gamemode"); + List 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 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(); + } + +}