diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 2571b4d..c1215e2 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(); + } + +} diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java index b9f59d6..5a16f24 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java @@ -13,6 +13,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerKickEvent; import java.util.EnumSet; @@ -47,6 +48,12 @@ public class FreezeWorldStateListener extends BasicListener { event.setCancelled(true); } + @EventHandler + public void handlePlayerKickEvent(PlayerKickEvent e){ + if(e.getReason().contains("Flying is not enabled")) + e.setCancelled(true); + } + @EventHandler public void onInventoryDrag(InventoryDragEvent event) { if(PersonalKitCreator.notInKitCreator(event.getWhoClicked())) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java index 3b673a1..0b81775 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java @@ -7,51 +7,35 @@ import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.utils.WaterRemover; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.World; import org.bukkit.scheduler.BukkitTask; import java.util.EnumSet; -import java.util.HashSet; -import java.util.Set; public class WinconditionWaterTechKO extends Wincondition { - private static final Set teamRedWater = new HashSet<>(); - private static final Set teamBlueWater = new HashSet<>(); + private static int teamRedWater; + private static int teamBlueWater; private static final World WORLD = Bukkit.getWorlds().get(0); private BukkitTask task; public WinconditionWaterTechKO() { - super(Config.WaterTechKO, EnumSet.of(FightState.PRE_RUNNING, FightState.RUNNING, FightState.SPECTATE)); + super(Config.WaterTechKO, EnumSet.of(FightState.PRE_RUNNING, FightState.RUNNING)); } public static int getTeamBlueWater() { - return teamBlueWater.size(); + return teamBlueWater; } public static int getTeamRedWater() { - return teamRedWater.size(); + return teamRedWater; } @Override public void enable() { - checkForWater(teamRedWater, - Config.TeamRedCornerX, - Config.TeamRedCornerY, - Config.TeamRedCornerZ, - Config.TeamRedCornerX + Config.SchemsizeX, - Config.TeamRedCornerY + Config.SchemsizeY, - Config.TeamRedCornerZ + Config.SchemsizeZ); - checkForWater(teamBlueWater, - Config.TeamBlueCornerX, - Config.TeamBlueCornerY, - Config.TeamBlueCornerZ, - Config.TeamBlueCornerX + Config.SchemsizeX, - Config.TeamBlueCornerY + Config.SchemsizeY, - Config.TeamBlueCornerZ + Config.SchemsizeZ); - task = Bukkit.getScheduler().runTaskTimerAsynchronously(FightSystem.getPlugin(), this::addWater, 200, 200); + checkTask(); + task = Bukkit.getScheduler().runTaskTimerAsynchronously(FightSystem.getPlugin(), this::checkTask, 200, 200); } @Override @@ -59,25 +43,22 @@ public class WinconditionWaterTechKO extends Wincondition { task.cancel(); } - private void checkEmpty(FightTeam team, Set teamWater){ - if(teamWater.isEmpty()){ + private void checkEmpty(FightTeam team, int teamWater){ + if(teamWater == 0){ Bukkit.broadcastMessage(FightSystem.PREFIX + "§eDas Team " + team.getColoredName() + " §eist Tech K.O.!"); Bukkit.getScheduler().runTask(FightSystem.getPlugin(), () -> FightSystem.setSpectateState(Fight.getOpposite(team), "WaterTechKO")); } } - private void addWater() { - if(FightSystem.getFightState() != FightState.PRE_RUNNING && FightSystem.getFightState() != FightState.RUNNING) - return; - - checkForWater(teamRedWater, + private void checkTask() { + teamRedWater = calcWater( Config.TeamRedCornerX, Config.TeamRedCornerY, Config.TeamRedCornerZ, Config.TeamRedCornerX + Config.SchemsizeX, Config.TeamRedCornerY + Config.SchemsizeY, Config.TeamRedCornerZ + Config.SchemsizeZ); - checkForWater(teamBlueWater, + teamBlueWater = calcWater( Config.TeamBlueCornerX, Config.TeamBlueCornerY, Config.TeamBlueCornerZ, @@ -89,16 +70,16 @@ public class WinconditionWaterTechKO extends Wincondition { checkEmpty(Fight.getBlueTeam(), teamBlueWater); } - private void checkForWater(Set teamWater, int minX, int minY, int minZ, int maxX, int maxY, int maxZ){ - teamWater.clear(); - + private int calcWater(int minX, int minY, int minZ, int maxX, int maxY, int maxZ){ + int teamWater = 0; for(int x = minX; x <= maxX; x++) { for(int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { if (WaterRemover.isWater(WORLD.getBlockAt(x, y, z))) - teamWater.add(new Location(WORLD, x, y, z)); + teamWater++; } } } + return teamWater; } }