From be7e5acec5c96dbcf45c126afa695f65e107651d Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 10 Nov 2019 17:29:01 +0100 Subject: [PATCH] Refactoring for new Core --- pom.xml | 4 +- src/de/steamwar/bausystem/BauSystem.java | 11 +- .../bausystem/commands/CommandBau.java | 22 ++- .../bausystem/commands/CommandGamemode.java | 9 +- .../bausystem/commands/CommandInfo.java | 9 +- .../bausystem/commands/CommandInjector.java | 24 ++++ .../bausystem/commands/CommandRemover.java | 43 ------ .../bausystem/commands/CommandSkull.java | 1 + .../bausystem/commands/CommandTeleport.java | 4 +- .../steamwar/bausystem/sql/BauweltMember.java | 136 ------------------ .../steamwar/bausystem/world/AutoLoader.java | 1 + .../steamwar/bausystem/world/TNTTracer.java | 1 + src/de/steamwar/bausystem/world/Welt.java | 8 +- src/plugin.yml | 2 +- 14 files changed, 62 insertions(+), 213 deletions(-) create mode 100644 src/de/steamwar/bausystem/commands/CommandInjector.java delete mode 100644 src/de/steamwar/bausystem/commands/CommandRemover.java delete mode 100644 src/de/steamwar/bausystem/sql/BauweltMember.java diff --git a/pom.xml b/pom.xml index 5f43787..8aa7bdb 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ maven - https://steamwar.de:81/maven/ + https://steamwar.de/maven/ @@ -52,7 +52,7 @@ steamwar SpigotCore - 1.0 + 2.0 provided diff --git a/src/de/steamwar/bausystem/BauSystem.java b/src/de/steamwar/bausystem/BauSystem.java index ba26878..c28fbf1 100644 --- a/src/de/steamwar/bausystem/BauSystem.java +++ b/src/de/steamwar/bausystem/BauSystem.java @@ -4,7 +4,8 @@ import de.steamwar.bausystem.commands.*; import de.steamwar.bausystem.world.ArenaSection; import de.steamwar.bausystem.world.RegionListener; import de.steamwar.bausystem.world.TNTListener; -import de.warking.hunjy.MySQL.WarkingUser; +import de.steamwar.core.CommandRemover; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.entity.Player; @@ -43,9 +44,9 @@ public class BauSystem extends JavaPlugin implements Listener { try { CommandRemover.removeAll("tp", "gamemode", "time"); - CommandRemover.injectCommand(new CommandTeleport()); - CommandRemover.injectCommand(new CommandGamemode()); - CommandRemover.injectCommand(new CommandTime()); + CommandInjector.injectCommand(new CommandTeleport()); + CommandInjector.injectCommand(new CommandGamemode()); + CommandInjector.injectCommand(new CommandTime()); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to replace commands", e); Bukkit.shutdown(); @@ -89,7 +90,7 @@ public class BauSystem extends JavaPlugin implements Listener { return sections; } public static int getOwnerID(){ - return WarkingUser.get(owner).getId(); + return SteamwarUser.get(owner).getId(); } @EventHandler diff --git a/src/de/steamwar/bausystem/commands/CommandBau.java b/src/de/steamwar/bausystem/commands/CommandBau.java index a57abec..77167e7 100644 --- a/src/de/steamwar/bausystem/commands/CommandBau.java +++ b/src/de/steamwar/bausystem/commands/CommandBau.java @@ -1,55 +1,53 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.sql.BauweltMember; import de.steamwar.bausystem.world.Welt; -import de.warking.hunjy.MySQL.WarkingUser; +import de.steamwar.sql.BauweltMember; +import de.steamwar.sql.SteamwarUser; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.UUID; - public class CommandBau implements CommandExecutor { private void onToggleBD(Player p, String arg) { - UUID id = WarkingUser.get(arg).getUUID(); + SteamwarUser id = SteamwarUser.get(arg); if(negativeToggleCheck(p, id)){ return; } - BauweltMember target = BauweltMember.getBauMember(id); + BauweltMember target = BauweltMember.getBauMember(BauSystem.getOwnerID(), id.getId()); Welt.toggleBuild(p, target); } private void onToggleWE(Player p, String arg) { - UUID id = WarkingUser.get(arg).getUUID(); + SteamwarUser id = SteamwarUser.get(arg); if(negativeToggleCheck(p, id)){ return; } - BauweltMember target = BauweltMember.getBauMember(id); + BauweltMember target = BauweltMember.getBauMember(BauSystem.getOwnerID(), id.getId()); Welt.toggleWE(p, target); } private void onToggleWorld(Player p, String arg) { - UUID id = WarkingUser.get(arg).getUUID(); + SteamwarUser id = SteamwarUser.get(arg); if(negativeToggleCheck(p, id)){ return; } - BauweltMember target = BauweltMember.getBauMember(id); + BauweltMember target = BauweltMember.getBauMember(BauSystem.getOwnerID(), id.getId()); Welt.toggleWorld(p, target); } - private boolean negativeToggleCheck(Player p, UUID id){ + private boolean negativeToggleCheck(Player p, SteamwarUser id){ if (id == null) { p.sendMessage(BauSystem.PREFIX + "§cUnbekannter Spieler"); return true; } - BauweltMember target = BauweltMember.getBauMember(id); + BauweltMember target = BauweltMember.getBauMember(BauSystem.getOwnerID(), id.getId()); if (target == null) { p.sendMessage(BauSystem.PREFIX + "§cDer Spieler ist kein Mitglied deiner Welt!"); return true; diff --git a/src/de/steamwar/bausystem/commands/CommandGamemode.java b/src/de/steamwar/bausystem/commands/CommandGamemode.java index 4d38bd0..9d4ac90 100644 --- a/src/de/steamwar/bausystem/commands/CommandGamemode.java +++ b/src/de/steamwar/bausystem/commands/CommandGamemode.java @@ -2,7 +2,7 @@ package de.steamwar.bausystem.commands; import com.google.common.collect.ImmutableList; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.sql.BauweltMember; +import de.steamwar.sql.BauweltMember; import org.apache.commons.lang.Validate; import org.bukkit.GameMode; import org.bukkit.command.CommandSender; @@ -37,7 +37,7 @@ public class CommandGamemode extends BukkitCommand { Player p = (Player) sender; if(!p.getUniqueId().equals(BauSystem.getOwner())){ - BauweltMember member = BauweltMember.getBauMember(p.getUniqueId()); + BauweltMember member = BauweltMember.getBauMember(BauSystem.getOwner(), p.getUniqueId()); if(member == null || !member.isBuild()){ p.sendMessage(BauSystem.PREFIX + "§cDu darfst hier deinen Spielmodus nicht ändern"); return false; @@ -55,6 +55,7 @@ public class CommandGamemode extends BukkitCommand { return true; } + @SuppressWarnings("deprecation") private GameMode createMode(String modeArg){ try { return GameMode.getByValue(Integer.parseInt(modeArg)); @@ -76,8 +77,8 @@ public class CommandGamemode extends BukkitCommand { Validate.notNull(alias, "Alias cannot be null"); if (args.length == 1) - return ((List) StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, - new ArrayList(GAMEMODE_NAMES.size()))); + return StringUtil.copyPartialMatches(args[0], GAMEMODE_NAMES, + new ArrayList<>(GAMEMODE_NAMES.size())); if (args.length == 2) { return super.tabComplete(sender, alias, args); } diff --git a/src/de/steamwar/bausystem/commands/CommandInfo.java b/src/de/steamwar/bausystem/commands/CommandInfo.java index edd6e77..b6bfa75 100644 --- a/src/de/steamwar/bausystem/commands/CommandInfo.java +++ b/src/de/steamwar/bausystem/commands/CommandInfo.java @@ -1,8 +1,8 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.sql.BauweltMember; -import de.warking.hunjy.MySQL.WarkingUser; +import de.steamwar.sql.BauweltMember; +import de.steamwar.sql.SteamwarUser; import net.minecraft.server.v1_12_R1.MinecraftServer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -13,8 +13,9 @@ import java.util.List; public class CommandInfo implements CommandExecutor { @Override + @SuppressWarnings("deprecation") public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - sender.sendMessage(BauSystem.PREFIX + "Besitzer: §e" + WarkingUser.get(BauSystem.getOwnerID()).getUserName()); + sender.sendMessage(BauSystem.PREFIX + "Besitzer: §e" + SteamwarUser.get(BauSystem.getOwnerID()).getUserName()); sender.sendMessage(BauSystem.PREFIX + "TNT-Schaden: " + (CommandTNT.getInstance().isOn() ? "§aAUS" : "§cAN")); sender.sendMessage(BauSystem.PREFIX + "Feuerschaden: " + (CommandFire.getInstance().isOn() ? "§aAUS" : "§cAN")); sender.sendMessage(BauSystem.PREFIX + "Eingefroren: " + (CommandFreeze.getInstance().isOn() ? "§aJA" : "§cNEIN")); @@ -23,7 +24,7 @@ public class CommandInfo implements CommandExecutor { StringBuilder membermessage = new StringBuilder().append(BauSystem.PREFIX).append("Mitglieder: "); for (BauweltMember member : members) { - membermessage.append("§e").append(WarkingUser.get(member.getMemberID()).getUserName()).append("§8["); + membermessage.append("§e").append(SteamwarUser.get(member.getMemberID()).getUserName()).append("§8["); membermessage.append(member.isBuild() ? "§a" : "§c").append("B").append("§8,"); membermessage.append(member.isWorldEdit() ? "§a" : "§c").append("WE").append("§8,"); membermessage.append(member.isWorld() ? "§a" : "§c").append("W").append("§8]").append(" "); diff --git a/src/de/steamwar/bausystem/commands/CommandInjector.java b/src/de/steamwar/bausystem/commands/CommandInjector.java new file mode 100644 index 0000000..b09b720 --- /dev/null +++ b/src/de/steamwar/bausystem/commands/CommandInjector.java @@ -0,0 +1,24 @@ +package de.steamwar.bausystem.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/src/de/steamwar/bausystem/commands/CommandRemover.java b/src/de/steamwar/bausystem/commands/CommandRemover.java deleted file mode 100644 index 472a8ce..0000000 --- a/src/de/steamwar/bausystem/commands/CommandRemover.java +++ /dev/null @@ -1,43 +0,0 @@ -package de.steamwar.bausystem.commands; - -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.SimpleCommandMap; - -import java.lang.reflect.Field; -import java.util.Map; - -public class CommandRemover { - - private CommandRemover(){} - - 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 removeAll(String... cmds) throws Exception { - for (String cmd : cmds) removeCommand(cmd); - } - - private static void removeCommand(String command) 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()); - - Field f2 = SimpleCommandMap.class.getDeclaredField("knownCommands"); - f2.setAccessible(true); - Map knownCommands = (Map) f2.get(commandMap); - - knownCommands.remove(command.toLowerCase()); - } - - 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/src/de/steamwar/bausystem/commands/CommandSkull.java b/src/de/steamwar/bausystem/commands/CommandSkull.java index 8540d2a..e581e85 100644 --- a/src/de/steamwar/bausystem/commands/CommandSkull.java +++ b/src/de/steamwar/bausystem/commands/CommandSkull.java @@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.SkullMeta; public class CommandSkull implements CommandExecutor { @Override + @SuppressWarnings("deprecation") public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { if(!(commandSender instanceof Player)) return false; diff --git a/src/de/steamwar/bausystem/commands/CommandTeleport.java b/src/de/steamwar/bausystem/commands/CommandTeleport.java index 38ad52e..e2b9539 100644 --- a/src/de/steamwar/bausystem/commands/CommandTeleport.java +++ b/src/de/steamwar/bausystem/commands/CommandTeleport.java @@ -1,7 +1,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.sql.BauweltMember; +import de.steamwar.sql.BauweltMember; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -41,7 +41,7 @@ public class CommandTeleport extends BukkitCommand { } if(!BauSystem.getOwner().equals(p.getUniqueId())){ - BauweltMember member = BauweltMember.getBauMember(p.getUniqueId()); + BauweltMember member = BauweltMember.getBauMember(BauSystem.getOwner(), p.getUniqueId()); if(member == null || !member.isBuild()){ p.sendMessage(BauSystem.PREFIX + "§cDu darfst dich auf dieser Welt nicht teleportieren!"); return false; diff --git a/src/de/steamwar/bausystem/sql/BauweltMember.java b/src/de/steamwar/bausystem/sql/BauweltMember.java deleted file mode 100644 index 68b2f43..0000000 --- a/src/de/steamwar/bausystem/sql/BauweltMember.java +++ /dev/null @@ -1,136 +0,0 @@ -package de.steamwar.bausystem.sql; - -import de.steamwar.bausystem.BauSystem; -import de.warking.hunjy.MySQL.MySQL; -import de.warking.hunjy.MySQL.WarkingUser; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import static de.warking.hunjy.Core.sql; - -public class BauweltMember{ - private final int BauweltID; - private final int MemberID; - private boolean Build; - private boolean WorldEdit; - private boolean World; - - private static final List members = new ArrayList<>(); - - private BauweltMember(int memberID, boolean build, boolean worldEdit, boolean world, boolean updateDB){ - BauweltID = BauSystem.getOwnerID(); - MemberID = memberID; - Build = build; - WorldEdit = worldEdit; - World = world; - if(updateDB) - updateDB(); - members.add(this); - } - - public BauweltMember(int memberID, boolean build, boolean worldEdit, boolean world){ - this(memberID, build, worldEdit, world, true); - } - - public BauweltMember(UUID memberID, boolean build, boolean worldEdit, boolean world){ - this(WarkingUser.get(memberID).getId(), build, worldEdit, world, true); - } - - public void remove(){ - sql.update("DELETE FROM BauweltMember WHERE BauweltID = " + BauweltID + " AND MemberID = " + MemberID); - members.remove(this); - } - - private void updateDB(){ - sql.update("INSERT INTO BauweltMember" + - " (BauweltID, MemberID, Build, WorldEdit, World)" + - " VALUES" + - " ('" + BauweltID + "', '" + MemberID + "', '" + MySQL.booleanToInt(Build) + "', '" + sql.booleanToInt(WorldEdit) + "', '" + sql.booleanToInt(World) + "')" + - " ON DUPLICATE KEY UPDATE" + - " Build = VALUES(Build), WorldEdit = VALUES(WorldEdit), World = VALUES(World)"); - } - - public static BauweltMember getBauMember(UUID memberID){ - return getBauMember(WarkingUser.get(memberID).getId()); - } - - public static BauweltMember getBauMember(int memberID){ - for(BauweltMember member : members) - if(member.MemberID == memberID) - return member; - ResultSet member = sql.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + BauSystem.getOwnerID() + "' AND MemberID = '" + memberID + "'"); - try { - if(member == null || !member.next()){ - return null; - } - boolean build = member.getBoolean("Build"); - boolean worldEdit = member.getBoolean("WorldEdit"); - boolean testblock = member.getBoolean("World"); - return new BauweltMember(memberID, build, worldEdit, testblock, false); - } catch (SQLException e) { - e.printStackTrace(); - } - return null; - } - - public static List getMembers(UUID bauweltID){ - return getMembers(WarkingUser.get(bauweltID).getId()); - } - - public static List getMembers(int bauweltID){ - try{ - ResultSet memberlist = sql.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + bauweltID + "'"); - List members = new ArrayList<>(); - while(memberlist.next()){ - int memberID = memberlist.getInt("MemberID"); - boolean build = memberlist.getBoolean("Build"); - boolean worldEdit = memberlist.getBoolean("WorldEdit"); - boolean testblock = memberlist.getBoolean("World"); - members.add(new BauweltMember(memberID, build, worldEdit, testblock, false)); - } - return members; - }catch(SQLException e){ - e.printStackTrace(); - } - return new ArrayList<>(); - } - - public int getBauweltID() { - return BauweltID; - } - - public int getMemberID() { - return MemberID; - } - - public boolean isBuild() { - return Build; - } - - public void setBuild(boolean build) { - Build = build; - updateDB(); - } - - public boolean isWorldEdit() { - return WorldEdit; - } - - public void setWorldEdit(boolean worldEdit) { - WorldEdit = worldEdit; - updateDB(); - } - - public boolean isWorld() { - return World; - } - - public void setWorld(boolean world) { - World = world; - updateDB(); - } -} \ No newline at end of file diff --git a/src/de/steamwar/bausystem/world/AutoLoader.java b/src/de/steamwar/bausystem/world/AutoLoader.java index b454b6f..af1418a 100644 --- a/src/de/steamwar/bausystem/world/AutoLoader.java +++ b/src/de/steamwar/bausystem/world/AutoLoader.java @@ -138,6 +138,7 @@ public class AutoLoader implements Listener { //BlockRedstoneEvent? @EventHandler + @SuppressWarnings("deprecation") public void onPlayerInteract(PlayerInteractEvent event){ if(!setup || !event.getPlayer().equals(player)) return; diff --git a/src/de/steamwar/bausystem/world/TNTTracer.java b/src/de/steamwar/bausystem/world/TNTTracer.java index 73b0ee6..54f0b0b 100644 --- a/src/de/steamwar/bausystem/world/TNTTracer.java +++ b/src/de/steamwar/bausystem/world/TNTTracer.java @@ -13,6 +13,7 @@ import org.bukkit.scheduler.BukkitTask; import java.util.*; +@SuppressWarnings("deprecation") public class TNTTracer { private static final byte BRICK_STEP = new Step(Material.BRICK).getData(); diff --git a/src/de/steamwar/bausystem/world/Welt.java b/src/de/steamwar/bausystem/world/Welt.java index 4b7839c..c443bec 100644 --- a/src/de/steamwar/bausystem/world/Welt.java +++ b/src/de/steamwar/bausystem/world/Welt.java @@ -2,8 +2,8 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.sql.BauweltMember; -import de.warking.hunjy.MySQL.WarkingUser; +import de.steamwar.sql.BauweltMember; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -15,7 +15,7 @@ public class Welt { if(member.getUniqueId().equals(BauSystem.getOwner())) return false; - BauweltMember member1 = BauweltMember.getBauMember(member.getUniqueId()); + BauweltMember member1 = BauweltMember.getBauMember(BauSystem.getOwner(), member.getUniqueId()); if(member1 == null) return true; @@ -34,7 +34,7 @@ public class Welt { } private static void sendMessages(Player p, boolean ableTo, BauweltMember target, String what){ - Player z = Bukkit.getPlayer(WarkingUser.get(target.getMemberID()).getUUID()); + Player z = Bukkit.getPlayer(SteamwarUser.get(target.getMemberID()).getUUID()); if (z != null) { if (ableTo) { z.sendMessage(BauSystem.PREFIX + "§aDu kannst nun auf der Welt von §6" + p.getName() + "§a " + what); diff --git a/src/plugin.yml b/src/plugin.yml index be74606..06b680b 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,7 +1,7 @@ name: BauSystem author: Lixfel version: 1.0 -depend: [WorldEdit, CoreSystem] +depend: [WorldEdit, SpigotCore] load: POSTWORLD main: de.steamwar.bausystem.BauSystem commands: