From d047fa1dd1fb6fac06352cf161169dff4f58a8ba Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 12 Mar 2021 09:18:48 +0100 Subject: [PATCH] Add SWUtils --- .../commands/CommandDebugStick_15.java | 4 +-- .../{PlayerUtils.java => SWUtils.java} | 19 +++++++++- .../bausystem/commands/CommandDetonator.java | 4 +-- .../CommandDetonatorTabCompleter.java | 27 ++++---------- .../bausystem/commands/CommandGUI.java | 4 +-- .../bausystem/commands/CommandScript.java | 4 +-- .../bausystem/commands/CommandSimulator.java | 4 +-- .../CommandSimulatorTabCompleter.java | 26 ++++---------- .../bausystem/commands/CommandSkull.java | 4 +-- .../commands/CommandTNTTabComplete.java | 35 ++++++------------- .../CommandTPSLimiterTabComplete.java | 11 +++--- .../commands/CommandTraceTabCompleter.java | 14 +++----- 12 files changed, 60 insertions(+), 96 deletions(-) rename BauSystem_API/src/de/steamwar/bausystem/{PlayerUtils.java => SWUtils.java} (68%) diff --git a/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java b/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java index 8bbf9dd..104da37 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java @@ -19,7 +19,7 @@ package de.steamwar.bausystem.commands; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -28,6 +28,6 @@ class CommandDebugStick_15 { private CommandDebugStick_15(){} static void giveStick(Player player){ - PlayerUtils.giveItemToPlayer(player, new ItemStack(Material.DEBUG_STICK)); + SWUtils.giveItemToPlayer(player, new ItemStack(Material.DEBUG_STICK)); } } diff --git a/BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java b/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java similarity index 68% rename from BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java rename to BauSystem_API/src/de/steamwar/bausystem/SWUtils.java index 8dd1181..e5b2468 100644 --- a/BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java +++ b/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java @@ -23,7 +23,10 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -public class PlayerUtils { +import java.util.ArrayList; +import java.util.List; + +public class SWUtils { public static void giveItemToPlayer(Player player, ItemStack itemStack) { ItemStack current = player.getInventory().getItemInMainHand(); @@ -33,4 +36,18 @@ public class PlayerUtils { } } + public static List manageList(List strings, String[] args, int index) { + strings = new ArrayList<>(strings); + for (int i = strings.size() - 1; i >= 0; i--) { + if (!strings.get(i).startsWith(args[index])) { + strings.remove(i); + } + } + return strings; + } + + public static List manageList(List strings, String[] args) { + return manageList(strings, args, args.length - 1); + } + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java index e45e53b..866bc47 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java @@ -21,7 +21,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.world.Detonator; import de.steamwar.bausystem.world.Welt; import org.bukkit.command.Command; @@ -63,7 +63,7 @@ public class CommandDetonator implements CommandExecutor { case "wand": case "detonator": case "item": - PlayerUtils.giveItemToPlayer(player, Detonator.WAND); + SWUtils.giveItemToPlayer(player, Detonator.WAND); player.updateInventory(); break; case "remove": diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java index 4a20f89..f4a166a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java @@ -19,41 +19,26 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.SWUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class CommandDetonatorTabCompleter implements TabCompleter { + private List arguments = new ArrayList<>(Arrays.asList("wand", "reset", "detonate", "remove")); + @Override public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { if(!(sender instanceof Player)) return new ArrayList<>(); - return detonaterTabComplete((Player) sender, args); - } - - private List detonaterTabComplete(Player player, String[] args) { - List tabComplete = new ArrayList<>(); - tabComplete.add("wand"); - tabComplete.add("reset"); - tabComplete.add("detonate"); - tabComplete.add("remove"); - - if (args.length >= 2) { + if (args.length != 1) { return new ArrayList<>(); } - return manageList(tabComplete, args, 0); - } - - private List manageList(List strings, String[] args, int index) { - for (int i = strings.size() - 1; i >= 0; i--) { - if (!strings.get(i).startsWith(args[index])) { - strings.remove(i); - } - } - return strings; + return SWUtils.manageList(arguments, args); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java index 5576d67..48abaca 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java @@ -2,7 +2,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.tracer.record.RecordStateMachine; import de.steamwar.bausystem.tracer.show.TraceShowManager; import de.steamwar.bausystem.world.*; @@ -587,7 +587,7 @@ public class CommandGUI implements CommandExecutor, Listener { openBauGui(player); OPEN_INVS.add(player); } else if ("item".equalsIgnoreCase(strings[0])) { - PlayerUtils.giveItemToPlayer(player, new ItemStack(Material.NETHER_STAR)); + SWUtils.giveItemToPlayer(player, new ItemStack(Material.NETHER_STAR)); } return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java index 6f03dbe..e8d0811 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java @@ -21,7 +21,7 @@ package de.steamwar.bausystem.commands; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -68,7 +68,7 @@ public class CommandScript implements CommandExecutor { if (!(sender instanceof Player)) { return false; } - PlayerUtils.giveItemToPlayer((Player) sender, BOOK); + SWUtils.giveItemToPlayer((Player) sender, BOOK); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java index 325f1a3..ad876df 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java @@ -22,7 +22,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.world.TNTSimulator; import de.steamwar.bausystem.world.Welt; import org.bukkit.command.Command; @@ -59,7 +59,7 @@ public class CommandSimulator implements CommandExecutor { if (args.length == 1) { switch (args[0].toLowerCase()) { case "wand": - PlayerUtils.giveItemToPlayer(player, TNTSimulator.WAND); + SWUtils.giveItemToPlayer(player, TNTSimulator.WAND); break; case "start": TNTSimulator.get(player).start(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java index 0103d30..9ece2fd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java @@ -20,41 +20,27 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.SWUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class CommandSimulatorTabCompleter implements TabCompleter { + private List arguments = new ArrayList<>(Arrays.asList("wand", "start", "delete")); + @Override public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { if(!(sender instanceof Player)) return new ArrayList<>(); - return simulatorTabComplete((Player) sender, args); - } - - private List simulatorTabComplete(Player player, String[] args) { - List tabComplete = new ArrayList<>(); - tabComplete.add("wand"); - tabComplete.add("start"); - tabComplete.add("delete"); - - if (args.length >= 2) { + if (args.length != 1) { return new ArrayList<>(); } - return manageList(tabComplete, args, 0); - } - - private List manageList(List strings, String[] args, int index) { - for (int i = strings.size() - 1; i >= 0; i--) { - if (!strings.get(i).startsWith(args[index])) { - strings.remove(i); - } - } - return strings; + return SWUtils.manageList(arguments, args); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java index bd8d121..da00ae6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java @@ -20,7 +20,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.PlayerUtils; +import de.steamwar.bausystem.SWUtils; import de.steamwar.inventory.SWItem; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -47,7 +47,7 @@ public class CommandSkull implements CommandExecutor { assert sm != null; sm.setDisplayName("§e" + args[0] + "§8s Kopf"); is.setItemMeta(sm); - PlayerUtils.giveItemToPlayer(p, is); + SWUtils.giveItemToPlayer(p, is); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNTTabComplete.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNTTabComplete.java index dbc18b1..26dea52 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNTTabComplete.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNTTabComplete.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.world.Region; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -28,40 +29,24 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class CommandTNTTabComplete implements TabCompleter { + private List arguments = new ArrayList<>(Arrays.asList("an", "on", "aus", "off")); + + public CommandTNTTabComplete() { + if (Region.buildAreaEnabled()) arguments.addAll(Arrays.asList("testblock", "tb")); + } + @Override public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { if(!(sender instanceof Player)) return new ArrayList<>(); - return tntTabComplete(args); - } - - private List tntTabComplete(String[] args) { - List tabComplete = new ArrayList<>(); - tabComplete.add("an"); - tabComplete.add("on"); - tabComplete.add("aus"); - tabComplete.add("off"); - if (Region.buildAreaEnabled()) { - tabComplete.add("testblock"); - tabComplete.add("tb"); - } - - if (args.length >= 2) { + if (args.length != 1) { return new ArrayList<>(); } - return manageList(tabComplete, args, 0); - } - - private List manageList(List strings, String[] args, int index) { - for (int i = strings.size() - 1; i >= 0; i--) { - if (!strings.get(i).startsWith(args[index])) { - strings.remove(i); - } - } - return strings; + return SWUtils.manageList(arguments, args); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java index 00dc116..d16158f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiterTabComplete.java @@ -19,10 +19,12 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.world.TPSUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.Arrays; @@ -38,16 +40,11 @@ public class CommandTPSLimiterTabComplete implements TabCompleter { @Override public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if(!(sender instanceof Player)) return new ArrayList<>(); if (args.length != 1) { return new ArrayList<>(); } - List validArguments = new ArrayList<>(arguments.size()); - for (String s : arguments) { - if (s.startsWith(args[0])) { - validArguments.add(s); - } - } - return validArguments; + return SWUtils.manageList(arguments, args); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java index 47e9912..0d54a66 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.tracer.record.RecordStateMachine; import de.steamwar.bausystem.tracer.record.RecordStatus; import org.bukkit.command.Command; @@ -49,19 +50,12 @@ public class CommandTraceTabCompleter implements TabCompleter { return tracerTabComplete((Player) sender, args); } - private List tracerTabComplete(Player player, String[] args) { - List tabComplete = new ArrayList<>(); + private List tracerTabComplete(Player player, java.lang.String[] args) { + List tabComplete = new ArrayList<>(); for (TabComplete tab : tabCompletes) { if (tab.test(player, args)) tabComplete.addAll(Arrays.asList(tab.getTabCompletes())); } - return manageList(tabComplete, args); - } - - private List manageList(List strings, String[] args) { - for (int i = strings.size() - 1; i >= 0; i--) { - if (!strings.get(i).startsWith(args[args.length - 1])) strings.remove(i); - } - return strings; + return SWUtils.manageList(tabComplete, args); } private static class TabComplete {