From 378371289b90fbe3cbfabca03895e74030b069e7 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Thu, 13 Jun 2019 19:20:12 +0200 Subject: [PATCH] Finishing BauServer-System --- pom.xml | 5 + src/de/steamwar/bungeecore/BungeeCore.java | 17 +- .../steamwar/bungeecore/PlayerListener.java | 48 ++-- .../bungeecore/arenasystem/Arena.java | 26 +- .../bungeecore/arenasystem/ArenaSystem.java | 8 +- .../bungeecore/bausystem/BauServer.java | 174 ++++++++++++ .../bungeecore/bausystem/BauSystem.java | 72 +++++ .../bungeecore/commands/BanCommand.java | 6 +- .../bungeecore/commands/BauCommand.java | 259 ++++++++++++++++++ .../bungeecore/commands/HelpCommand.java | 81 ++++-- .../bungeecore/commands/JoinmeCommand.java | 22 +- src/de/steamwar/bungeecore/sql/Bauwelt.java | 77 ++++++ .../bungeecore/sql/BauweltMember.java | 134 +++++++++ .../steamwar/bungeecore/sql/Permission.java | 9 + .../steamwar/bungeecore/sql/WarkingUser.java | 86 ++++-- src/de/steamwar/bungeecore/sql/sql.java | 16 +- 16 files changed, 957 insertions(+), 83 deletions(-) create mode 100644 src/de/steamwar/bungeecore/bausystem/BauServer.java create mode 100644 src/de/steamwar/bungeecore/bausystem/BauSystem.java create mode 100644 src/de/steamwar/bungeecore/commands/BauCommand.java create mode 100644 src/de/steamwar/bungeecore/sql/Bauwelt.java create mode 100644 src/de/steamwar/bungeecore/sql/BauweltMember.java create mode 100644 src/de/steamwar/bungeecore/sql/Permission.java diff --git a/pom.xml b/pom.xml index ce9d7cb..5925e60 100644 --- a/pom.xml +++ b/pom.xml @@ -61,5 +61,10 @@ javadoc provided + + commons-io + commons-io + 2.6 + \ No newline at end of file diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index cbbd0b5..0ddcc56 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -2,7 +2,11 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.arenasystem.ArenaMode; import de.steamwar.bungeecore.arenasystem.ArenaSystem; +import de.steamwar.bungeecore.bausystem.BauSystem; import de.steamwar.bungeecore.commands.*; +import de.steamwar.bungeecore.sql.Bauwelt; +import de.steamwar.bungeecore.sql.BauweltMember; +import de.steamwar.bungeecore.sql.WarkingUser; import de.steamwar.bungeecore.sql.sql; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.plugin.Command; @@ -24,6 +28,8 @@ public class BungeeCore extends Plugin { public static BungeeCore instance; public static String ChatPrefix; + public static String WorldFolder; + public static String BauweltPrototyp; public static final String TeamchatPrefix = "§4T§7c§r §e"; public static final DateTimeFormatter DateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy"); public static String LobbyServer; @@ -65,6 +71,8 @@ public class BungeeCore extends Plugin { } ChatPrefix = config.getString("prefix"); + WorldFolder = config.getString("worldfolder"); + BauweltPrototyp = config.getString("bauweltprototyp"); LobbyServer = config.getString("lobbyserver"); sql.connect( config.getString("db.url"), @@ -116,17 +124,24 @@ public class BungeeCore extends Plugin { LastBroadCast = 0; } }, 10, 10, TimeUnit.MINUTES); + + getProxy().getScheduler().schedule(this, () -> { + WarkingUser.clearCache(); + Bauwelt.clearCache(); + BauweltMember.clearCache(); + }, 1, 1, TimeUnit.HOURS); } @Override public void onDisable(){ ArenaSystem.shutdown(); + BauSystem.shutdown(); sql.close(); getProxy().getPluginManager().unregisterCommands(this); getProxy().getPluginManager().unregisterListeners(this); } - public static void log(final String message, final ServerInfo server){ + static void log(final String message, final ServerInfo server){ log("[" + server.getName() + "] " + message); } diff --git a/src/de/steamwar/bungeecore/PlayerListener.java b/src/de/steamwar/bungeecore/PlayerListener.java index 61a29fe..0a54786 100644 --- a/src/de/steamwar/bungeecore/PlayerListener.java +++ b/src/de/steamwar/bungeecore/PlayerListener.java @@ -2,6 +2,9 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.arenasystem.Arena; import de.steamwar.bungeecore.arenasystem.ArenaSystem; +import de.steamwar.bungeecore.bausystem.BauServer; +import de.steamwar.bungeecore.bausystem.BauSystem; +import de.steamwar.bungeecore.commands.BauCommand; import de.steamwar.bungeecore.sql.BannedUserIPs; import de.steamwar.bungeecore.sql.WarkingUser; import net.md_5.bungee.api.AbstractReconnectHandler; @@ -28,11 +31,11 @@ public class PlayerListener implements Listener { if(user.isBanned()) { user.updateBanIP(event.getConnection().getAddress().getAddress().getHostAddress()); event.setCancelled(true); - if (user.BanTime.before(WarkingUser.PermaBan)) { - event.setCancelReason(new TextComponent(BungeeCore.ChatPrefix + "§cDu bist permanent gebannt. §r§lGrund§r: §c" + user.BanReason)); + if (user.getBanTime().before(WarkingUser.PermaBan)) { + event.setCancelReason(new TextComponent(BungeeCore.ChatPrefix + "§cDu bist permanent gebannt. §r§lGrund§r: §c" + user.getBanReason())); } else { event.setCancelReason(new TextComponent(BungeeCore.ChatPrefix + " Du bist bis zum " + - user.BanTime.toLocalDateTime().format(BungeeCore.DateFormat) + " gebannt. §r§lGrund§r: §c" + user.BanReason)); + user.getBanTime().toLocalDateTime().format(BungeeCore.DateFormat) + " gebannt. §r§lGrund§r: §c" + user.getBanReason())); } return; } @@ -42,7 +45,7 @@ public class PlayerListener implements Listener { StringBuilder potentialBan = new StringBuilder(); potentialBan.append(BungeeCore.ChatPrefix); potentialBan.append("§cMögliche Bannumgehung durch §r"); - potentialBan.append(user.UserName); + potentialBan.append(user.getUserName()); potentialBan.append("§c:"); for(BannedUserIPs banned : ips){ @@ -50,12 +53,12 @@ public class PlayerListener implements Listener { potentialBan.append(" §e"); potentialBan.append(banned.getTimestamp().toLocalDateTime().format(BungeeCore.DateFormat)); potentialBan.append(" §c"); - potentialBan.append(bannedUser.UserName); + potentialBan.append(bannedUser.getUserName()); } TextComponent msg = new TextComponent(potentialBan.toString()); msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cBanne Spieler wegen Bannumgehung").create())); - msg.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ban " + user.UserName + " perma Bannumgehung")); + msg.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/ban " + user.getUserName() + " perma Bannumgehung")); for (ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ if ((target.hasPermission("bungeecore.teamchat")) && (target.getChatMode() == ProxiedPlayer.ChatMode.COMMANDS_ONLY @@ -71,7 +74,7 @@ public class PlayerListener implements Listener { ProxiedPlayer player = event.getPlayer(); WarkingUser user = new WarkingUser(player); player.removeGroups("Admin", "team"); - switch(user.UserGroup){ + switch(user.getUserGroup()){ case "Admin": player.addGroups("admin", "team"); player.setPermission("bungeecore.group.admin", true); @@ -152,6 +155,12 @@ public class PlayerListener implements Listener { Arena a = ArenaSystem.getArena(p); if(a != null && !p.getServer().getInfo().equals(a.server)){ a.removePlayer(p); + return; + } + + BauServer bau = BauSystem.getBau(p); + if(bau != null && !p.getServer().getInfo().equals(bau.getServer())){ + bau.removePlayer(p); } } @@ -166,11 +175,7 @@ public class PlayerListener implements Listener { sender.sendMessage(BungeeCore.ChatPrefix + "§cUnbekannter Befehl."); } }else if(command[0].equalsIgnoreCase("/bau") && e.getSender() instanceof ProxiedPlayer){ - ProxiedPlayer sender = (ProxiedPlayer) e.getSender(); - if(!sender.getServer().getInfo().getName().equals("Bau")){ - e.setCancelled(true); - sender.connect(ProxyServer.getInstance().getServerInfo("Bau")); - } + BauCommand.onBau(e, command); } }else{ if(e.getSender() instanceof ProxiedPlayer){ @@ -191,16 +196,11 @@ public class PlayerListener implements Listener { if(sender.hasPermission("bungeecore.group.admin")){ chatsuffix = "§7» §6"; message = ChatColor.translateAlternateColorCodes('&', message); - }else if(sender.hasPermission("bungeecore.group.developer")){ - chatsuffix = "§7» §r"; - message = ChatColor.translateAlternateColorCodes('&', message); - }else if(sender.hasPermission("bungeecore.group.moderator")){ - chatsuffix = "§7» §r"; - message = ChatColor.translateAlternateColorCodes('&', message); - }else if(sender.hasPermission("bungeecore.group.supporter")){ - chatsuffix = "§7» §r"; - message = ChatColor.translateAlternateColorCodes('&', message); - }else if(sender.hasPermission("bungeecore.group.builder")){ + }else if( + sender.hasPermission("bungeecore.group.developer") || + sender.hasPermission("bungeecore.group.moderator") || + sender.hasPermission("bungeecore.group.supporter") || + sender.hasPermission("bungeecore.group.builder")){ chatsuffix = "§7» §r"; message = ChatColor.translateAlternateColorCodes('&', message); }else if(sender.hasPermission("bungeecore.group.youtuber") || @@ -209,8 +209,8 @@ public class PlayerListener implements Listener { } String msg = name + chatsuffix + message; - for(ProxiedPlayer target : sender.getServer().getInfo().getPlayers()){ - if(target.getChatMode() == ProxiedPlayer.ChatMode.SHOWN){ + for(ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ + if(target.getChatMode() == ProxiedPlayer.ChatMode.SHOWN && !ArenaSystem.inArena(target)){ target.sendMessage(msg); } } diff --git a/src/de/steamwar/bungeecore/arenasystem/Arena.java b/src/de/steamwar/bungeecore/arenasystem/Arena.java index 57e6234..eddf6ef 100644 --- a/src/de/steamwar/bungeecore/arenasystem/Arena.java +++ b/src/de/steamwar/bungeecore/arenasystem/Arena.java @@ -42,7 +42,7 @@ public class Arena implements Runnable{ ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.instance, this); InetSocketAddress address = new InetSocketAddress("127.0.0.1", port); server = ProxyServer.getInstance().constructServerInfo( - serverName, address, "WarKing.de - Arena", false); + serverName, address, "SteamWar.de - Arena", false); ProxyServer.getInstance().getServers().put(serverName, server); writer = new PrintWriter( p.getOutputStream(), true); }catch(IOException e) { @@ -84,24 +84,26 @@ public class Arena implements Runnable{ player.connect(ProxyServer.getInstance().getServerInfo(BungeeCore.LobbyServer)); } writer.println("stop"); - try{ - p.waitFor(); - }catch(InterruptedException e){ - e.printStackTrace(); - } - } - - ProxyServer.getInstance().getServers().remove(serverName); - ArenaSystem.arenen.remove(this); + ProxyServer.getInstance().getServers().remove(serverName); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.instance, ()->{ + try { + p.waitFor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + ArenaSystem.arenen.remove(this); + }); + }else + ArenaSystem.arenen.remove(this); } private void fatalError(){ for(ProxiedPlayer cached : cachedPlayers){ - cached.sendMessage(BungeeCore.ChatPrefix + "§7Unerwarteter Fehler beim Arenenstart"); + cached.sendMessage(BungeeCore.ChatPrefix + "§cUnerwarteter Fehler beim Arenenstart."); } for(ProxiedPlayer player : server.getPlayers()){ player.connect(ProxyServer.getInstance().getServerInfo(BungeeCore.LobbyServer)); - player.sendMessage(BungeeCore.ChatPrefix + "§7Unerwarteter Fehler in der Arena"); + player.sendMessage(BungeeCore.ChatPrefix + "§cUnerwarteter Fehler in der Arena."); } stop(); } diff --git a/src/de/steamwar/bungeecore/arenasystem/ArenaSystem.java b/src/de/steamwar/bungeecore/arenasystem/ArenaSystem.java index e8fad38..cd83c9d 100644 --- a/src/de/steamwar/bungeecore/arenasystem/ArenaSystem.java +++ b/src/de/steamwar/bungeecore/arenasystem/ArenaSystem.java @@ -3,11 +3,13 @@ package de.steamwar.bungeecore.arenasystem; import net.md_5.bungee.api.connection.ProxiedPlayer; -import java.util.Vector; +import java.util.ArrayList; public class ArenaSystem { - protected static Vector arenen = new Vector<>(); + private ArenaSystem(){} + + static ArrayList arenen = new ArrayList<>(); private static int freeId(){ int id = 0; @@ -33,7 +35,7 @@ public class ArenaSystem { public static void shutdown(){ while(!arenen.isEmpty()) - arenen.firstElement().stop(); + arenen.get(0).stop(); } public static boolean inArena(ProxiedPlayer p){ diff --git a/src/de/steamwar/bungeecore/bausystem/BauServer.java b/src/de/steamwar/bungeecore/bausystem/BauServer.java new file mode 100644 index 0000000..bac05fe --- /dev/null +++ b/src/de/steamwar/bungeecore/bausystem/BauServer.java @@ -0,0 +1,174 @@ +package de.steamwar.bungeecore.bausystem; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.WarkingUser; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class BauServer implements Runnable{ + + private final UUID owner; + private final int id; + private final String name; + + private Process p; + private PrintWriter writer; + private ServerInfo server; + private boolean started = false; + private boolean stopped = false; + + private final List cachedPlayers = new ArrayList<>(); + private final List players = new ArrayList<>(); + + BauServer(int id, UUID owner){ + this.id = id; + this.owner = owner; + this.name = new WarkingUser(owner).getUserName() + "s Bau"; + int port = 4000 + id; + ProcessBuilder pb = new ProcessBuilder( + "/home/minecraft/mc", + "-b", owner.toString(), Integer.toString(port)); + try{ + p = pb.start(); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.instance, this); + InetSocketAddress address = new InetSocketAddress("127.0.0.1", port); + server = ProxyServer.getInstance().constructServerInfo( + name, address, "SteamWar.de - BauServer", false); + ProxyServer.getInstance().getServers().put(name, server); + writer = new PrintWriter( p.getOutputStream(), true); + }catch(IOException e) { + e.printStackTrace(); + stop(); + } + } + + public void stop(){ + if(p != null && p.isAlive()) { + for (ProxiedPlayer player : server.getPlayers()) { + player.connect(ProxyServer.getInstance().getServerInfo(BungeeCore.LobbyServer)); + } + stopped = true; + writer.println("stop"); + ProxyServer.getInstance().getServers().remove(name); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.instance, ()->{ + try { + p.waitFor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + BauSystem.server.remove(owner); + }); + }else + BauSystem.server.remove(owner); + } + + void sendPlayer(ProxiedPlayer p){ + if(stopped) { + p.sendMessage(BungeeCore.ChatPrefix + "§7Die Bauwelt derzeit entladen, bitte versuche es später erneut."); + }else if(!started){ + p.sendMessage(BungeeCore.ChatPrefix + "§7Die Bauwelt wird geladen, einen Moment bitte..."); + cachedPlayers.add(p); + }else{ + p.connect(server); + players.add(p); + } + } + + boolean onBau(ProxiedPlayer p){ + for(ProxiedPlayer ap : cachedPlayers) + if(ap.equals(p)) + return true; + for(ProxiedPlayer ap : players) + if(ap.equals(p)) + return true; + return false; + } + + public void removePlayer(ProxiedPlayer p){ + cachedPlayers.remove(p); + players.remove(p); + } + + private void fatalError(){ + for(ProxiedPlayer cached : cachedPlayers){ + cached.sendMessage(BungeeCore.ChatPrefix + "§cUnerwarteter Fehler beim Laden der Bauwelt."); + } + for(ProxiedPlayer player : server.getPlayers()){ + player.connect(ProxyServer.getInstance().getServerInfo(BungeeCore.LobbyServer)); + player.sendMessage(BungeeCore.ChatPrefix + "§cUnerwarteter Fehler in der Bauwelt."); + } + stop(); + } + + @Override + public void run() { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + String line = ""; + + while (!started && (line = reader.readLine()) != null) { + started = line.contains(" INFO]: Done ("); + } + + if(line == null){ + BungeeCore.log("Bau "+name+" stopped to early!"); + fatalError(); + return; + } + + Thread.sleep(1000); + + for(ProxiedPlayer player : cachedPlayers){ + sendPlayer(player); + } + cachedPlayers.clear(); + + p.waitFor(); + }catch(IOException e){ + e.printStackTrace(); + fatalError(); + }catch(InterruptedException e){ + BungeeCore.log("Bau "+name+" was interrupted!"); + fatalError(); + } + stop(); + } + + public UUID getOwner() { + return owner; + } + + public int getId() { + return id; + } + + public ServerInfo getServer() { + return server; + } + + public static void sendDeniedMessage(ProxiedPlayer p, UUID owner){ + ProxiedPlayer o = ProxyServer.getInstance().getPlayer(owner); + if(o == null) + return; + + o.sendMessage(BungeeCore.ChatPrefix + "§e" + p.getName() + " §7möchte auf deine Bauwelt."); + TextComponent tc = new TextComponent("§7Klicke §ehier§7, wenn du das erlauben möchtest."); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§e/bau addmember " + p.getName()).create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bau addmember " + p.getName())); + o.sendMessage(tc); + } +} diff --git a/src/de/steamwar/bungeecore/bausystem/BauSystem.java b/src/de/steamwar/bungeecore/bausystem/BauSystem.java new file mode 100644 index 0000000..f3454db --- /dev/null +++ b/src/de/steamwar/bungeecore/bausystem/BauSystem.java @@ -0,0 +1,72 @@ +package de.steamwar.bungeecore.bausystem; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.Bauwelt; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.UUID; + +public class BauSystem { + + private BauSystem(){} + + static HashMap server = new HashMap<>(); + + private static int freeId(){ + int id = 0; + boolean isFree = false; + while(!isFree){ + id++; + isFree = true; + for(BauServer bau : server.values()){ + if(bau.getId() == id){ + isFree = false; + break; + } + } + } + return id; + } + + public static void sendToServer(ProxiedPlayer p, UUID owner){ + if(server.containsKey(owner)){ + server.get(owner).sendPlayer(p); + }else{ + File w = new File(BungeeCore.WorldFolder + owner); + if (!w.exists() || !w.isDirectory()){ + try { + FileUtils.copyDirectory(new File(BungeeCore.BauweltPrototyp), w); + } catch (IOException e) { + e.printStackTrace(); + p.sendMessage(BungeeCore.ChatPrefix + "§cDas Erstellen der Welt ist fehlgeschlagen."); + return; + } + if(Bauwelt.getBauwelt(owner) == null) + new Bauwelt(owner, false, false); + } + BauServer bau = new BauServer(freeId(), owner); + server.put(owner, bau); + bau.sendPlayer(p); + } + } + + public static BauServer getBau(ProxiedPlayer p){ + for(BauServer bau : server.values()){ + if(bau.onBau(p)) + return bau; + } + return null; + } + + public static void shutdown(){ + Iterator it = server.values().iterator(); + while(it.hasNext()){ + it.next().stop(); + } + } +} diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index 4108906..949ab05 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -24,7 +24,7 @@ public class BanCommand extends Command { } WarkingUser target = new WarkingUser(args[0]); - if(target.UserName == null){ + if(target.getUserName() == null){ sender.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler existiert nicht."); return; } @@ -55,8 +55,8 @@ public class BanCommand extends Command { } String msg = banReason.toString(); msgBuilder.append(msg); - sender.sendMessage(BungeeCore.ChatPrefix + "Du hast " + target.UserName + " gebannt. Grund: §c" + msg); - ProxiedPlayer targetPlayer = ProxyServer.getInstance().getPlayer(target.UUID); + sender.sendMessage(BungeeCore.ChatPrefix + "Du hast " + target.getUserName() + " gebannt. Grund: §c" + msg); + ProxiedPlayer targetPlayer = ProxyServer.getInstance().getPlayer(target.getUUID()); if(targetPlayer != null){ target.banPlayer(targetPlayer.getAddress().getAddress().getHostAddress(), banTime, msg); targetPlayer.disconnect(msgBuilder.toString()); diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java new file mode 100644 index 0000000..0a2d819 --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -0,0 +1,259 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.bausystem.BauServer; +import de.steamwar.bungeecore.bausystem.BauSystem; +import de.steamwar.bungeecore.sql.Bauwelt; +import de.steamwar.bungeecore.sql.BauweltMember; +import de.steamwar.bungeecore.sql.Permission; +import de.steamwar.bungeecore.sql.WarkingUser; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.ChatEvent; + +import java.io.File; +import java.util.UUID; + +public class BauCommand { + + private BauCommand(){} + + public static void onBau(ChatEvent e, String[] command){ + ProxiedPlayer p = (ProxiedPlayer) e.getSender(); + BauServer bau = BauSystem.getBau(p); + boolean ownBau = bau != null && bau.getOwner().equals(p.getUniqueId()); + e.setCancelled(true); + + if(command.length == 1){ + BauSystem.sendToServer(p, p.getUniqueId()); + }else if(ownBau && command.length != 2 && ( + command[1].equalsIgnoreCase("togglebuild") || + command[1].equalsIgnoreCase("togglewe") || + command[1].equalsIgnoreCase("toggleworld"))) { + e.setCancelled(false); + }else if(command[1].equalsIgnoreCase("addmember")){ + if (!hasWorld(p)){ + return; + }else if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau addmember "); + return; + } + + UUID id = WarkingUser.get(command[2]).getUUID(); + if (id == null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cUnbekannter Spieler"); + return; + }else if (BauweltMember.getBauMember(p.getUniqueId(), id) != null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cDieser Spieler ist bereits Mitglied auf deiner Welt"); + return; + } + + new BauweltMember(p.getUniqueId(), id, true, false, false); + p.sendMessage(BungeeCore.ChatPrefix + "§aDer Spieler wurde zu deiner Welt hinzugefügt"); + + ProxiedPlayer z = ProxyServer.getInstance().getPlayer(id); + if(z != null) + z.sendMessage(BungeeCore.ChatPrefix + "§aDu wurdest zu der Welt von §6" + p.getName() + " §ahinzugefügt"); + }else if(command[1].equalsIgnoreCase("tp")){ + if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau tp "); + return; + } + + UUID worldOwner = WarkingUser.get(command[2]).getUUID(); + if (worldOwner == null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cUnbekannter Spieler"); + return; + }else if (!hasPermission(worldOwner, p, Permission.member)){ + BauServer.sendDeniedMessage(p, worldOwner); + p.sendMessage(BungeeCore.ChatPrefix + "§cDu darfst dich nicht auf diese Welt teleportieren"); + return; + } + BauSystem.sendToServer(p, worldOwner); + }else if(command[1].equalsIgnoreCase("togglebuild")){ + if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau togglebuild "); + return; + } + + UUID id = WarkingUser.get(command[2]).getUUID(); + if(!toggleCheck(p, id)){ + return; + } + + BauweltMember target = BauweltMember.getBauMember(p.getUniqueId(), id); + toggleBuild(p, target); + }else if(command[1].equalsIgnoreCase("togglewe")){ + if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau togglewe "); + return; + } + + UUID id = WarkingUser.get(command[2]).getUUID(); + if(!toggleCheck(p, id)){ + return; + } + + BauweltMember target = BauweltMember.getBauMember(p.getUniqueId(), id); + toggleWE(p, target); + }else if(command[1].equalsIgnoreCase("toggleworld")) { + if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau toggleworld "); + return; + } + + UUID id = WarkingUser.get(command[2]).getUUID(); + if (!toggleCheck(p, id)) { + return; + } + + BauweltMember target = BauweltMember.getBauMember(p.getUniqueId(), id); + toggleTestblock(p, target); + }else if(command[1].equalsIgnoreCase("delmember")){ + if (command.length == 2) { + p.sendMessage(BungeeCore.ChatPrefix + "/bau delmember "); + return; + } + + UUID id = WarkingUser.get(command[2]).getUUID(); + if(!toggleCheck(p, id)){ + return; + } + + BauweltMember target = BauweltMember.getBauMember(p.getUniqueId(), id); + removePlayer(p, target); + }else if(command[1].equalsIgnoreCase("resetall") || + command[1].equalsIgnoreCase("delete")){ + p.sendMessage(BungeeCore.ChatPrefix + "§aDeine Welt wird zurückgesetzt. Einen Moment bitte..."); + BauServer bauserver = BauSystem.getBau(p); + if(bauserver != null){ + bauserver.stop(); + } + File directory = new File(BungeeCore.WorldFolder + p.getUniqueId().toString()); + del(directory); + }else{ + HelpCommand.sendBauHelp(p); + } + } + + public static void toggleBuild(ProxiedPlayer p, BauweltMember target){ + target.setBuild(!target.isBuild()); + ProxiedPlayer z = ProxyServer.getInstance().getPlayer(WarkingUser.get(target.getMemberID()).getUUID()); + if (z != null) { + if (target.isBuild()) { + z.sendMessage(BungeeCore.ChatPrefix + "§aDu kannst nun auf der Welt von §6" + p.getName() + "§a bauen"); + }else { + z.sendMessage(BungeeCore.ChatPrefix + "§cDu kannst nun nicht mehr auf der Welt von §6" + p.getName() + "§c bauen"); + } + } + if (target.isBuild()) { + p.sendMessage(BungeeCore.ChatPrefix + "§aDer Spieler darf nun bauen"); + } else { + p.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler darf nun nicht mehr bauen"); + } + } + + public static void toggleWE(ProxiedPlayer p, BauweltMember target){ + target.setWorldEdit(!target.isWorldEdit()); + ProxiedPlayer z = ProxyServer.getInstance().getPlayer(WarkingUser.get(target.getMemberID()).getUUID()); + if (z != null) { + if (target.isWorldEdit()) { + z.sendMessage(BungeeCore.ChatPrefix + "§aDu kannst nun auf der Welt von §6" + p.getName() + "§a WorldEdit verwenden"); + }else { + z.sendMessage(BungeeCore.ChatPrefix + "§cDu kannst nun nicht mehr auf der Welt von §6" + p.getName() + "§c WorldEdit verwenden"); + } + } + if (target.isWorldEdit()) { + p.sendMessage(BungeeCore.ChatPrefix + "§aDer Spieler darf nun WorldEdit verwenden"); + } else { + p.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler darf nun kein WorldEdit mehr verwenden"); + } + } + + public static void toggleTestblock(ProxiedPlayer p, BauweltMember target){ + target.setWorld(!target.isWorld()); + ProxiedPlayer z = ProxyServer.getInstance().getPlayer(WarkingUser.get(target.getMemberID()).getUUID()); + if (z != null) { + if (target.isWorld()) { + z.sendMessage(BungeeCore.ChatPrefix + "§aDu kannst nun auf der Welt von §6" + p.getName() + "§a Einstellungen vornehmen"); + }else { + z.sendMessage(BungeeCore.ChatPrefix + "§cDu kannst nun nicht mehr auf der Welt von §6" + p.getName() + "§c Einstellungen vornehmen"); + } + } + if (target.isWorld()) { + p.sendMessage(BungeeCore.ChatPrefix + "§aDer Spieler darf nun Einstellungen vornehmen"); + } else { + p.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler darf nun nicht mehr Einstellungen vornehmen"); + } + } + + public static void removePlayer(ProxiedPlayer p, BauweltMember target){ + target.remove(); + ProxiedPlayer z = ProxyServer.getInstance().getPlayer(WarkingUser.get(target.getMemberID()).getUUID()); + if(z != null){ + z.sendMessage(BungeeCore.ChatPrefix + "§cDu wurdest von der Welt von §6" + p.getName() + " §centfernt."); + BauServer server = BauSystem.getBau(z); + if(server != null && server.getOwner().equals(p.getUniqueId())) + z.connect(ProxyServer.getInstance().getServerInfo(BungeeCore.LobbyServer)); + } + p.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler wurde entfernt."); + } + + private static boolean toggleCheck(ProxiedPlayer p, UUID id){ + if (id == null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cUnbekannter Spieler"); + return false; + } + + BauweltMember target = BauweltMember.getBauMember(p.getUniqueId(), id); + if (target == null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler ist kein Mitglied deiner Welt!"); + return false; + } + return true; + } + + public static boolean hasPermission(UUID welt, ProxiedPlayer member, Permission perm){ + if(member.getUniqueId().equals(welt)) + return true; + + BauweltMember member1 = BauweltMember.getBauMember(welt, member.getUniqueId()); + if(member1 == null) + return false; + + switch(perm){ + case build: + return member1.isBuild(); + case worldedit: + return member1.isWorldEdit(); + case world: + return member1.isWorld(); + case member: + return true; + default: + return false; + } + } + + private static void del(File dir){ + if (dir.isDirectory()){ + String[] entries = dir.list(); + for (String entry : entries) { + File aktFile = new File(dir.getPath(), entry); + del(aktFile); + } + dir.delete(); + } + else{ + dir.delete(); + } + } + + private static boolean hasWorld(ProxiedPlayer p){ + if (Bauwelt.getBauwelt(p.getUniqueId()).getUUID() == null) { + p.sendMessage(BungeeCore.ChatPrefix + "§cDu hast keine Bauwelt"); + return false; + } + return true; + } +} diff --git a/src/de/steamwar/bungeecore/commands/HelpCommand.java b/src/de/steamwar/bungeecore/commands/HelpCommand.java index 11930b8..999202d 100644 --- a/src/de/steamwar/bungeecore/commands/HelpCommand.java +++ b/src/de/steamwar/bungeecore/commands/HelpCommand.java @@ -6,6 +6,7 @@ import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.connection.ProxiedPlayer; public class HelpCommand extends Command { public HelpCommand() { @@ -14,36 +15,84 @@ public class HelpCommand extends Command { @Override public void execute(CommandSender sender, String[] args) { - if(args.length != 1){ - TextComponent tc = new TextComponent(BungeeCore.ChatPrefix + "§7Kehre von überall mit §e/l §7zur Lobby zurück!"); - tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cZurück zur Lobby").create())); + TextComponent tc = new TextComponent(BungeeCore.ChatPrefix + "§7Kehre von überall mit §8/§el §7zur Lobby zurück!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eZurück zur Lobby").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/l")); sender.sendMessage(tc); - tc = new TextComponent(BungeeCore.ChatPrefix + "§7Komme mit §e/bau §7auf den Bauserver!"); - tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cZum Bauserver").create())); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Komme mit §8/§ebau §7auf den Bauserver!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eZum Bauserver").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bauserver")); sender.sendMessage(tc); - tc = new TextComponent(BungeeCore.ChatPrefix + "§7Erhalte mit §e/bau §7Hilfe auf dem Bauserver!"); - tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cHilfe zum Bausystem").create())); - tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bau")); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Erhalte mit §8/§ehelp bauserver §7Hilfe zum Bauserver!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eHilfe zum Bauserver").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help bauserver")); sender.sendMessage(tc); - tc = new TextComponent(BungeeCore.ChatPrefix + "§7Starte mit §e/fight §7einen neuen Kampf!"); - tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cZum Kampfsystem").create())); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Starte mit §8/§efight §7einen neuen Kampf!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eZum Kampfsystem").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/fight")); sender.sendMessage(tc); - tc = new TextComponent(BungeeCore.ChatPrefix + "§7Trete mit §e/join [Spieler] §7einem Kampf bei!"); - tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§cSpieler nachjoinen").create())); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Trete mit §8/§ejoin §8[§eSpieler§8] §7einem Kampf bei!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSpieler beitreten").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/join")); sender.sendMessage(tc); - }else{ - switch(args[0]){ - - } + }else if(args[0].equalsIgnoreCase("bauserver")){ + sendBauHelp((ProxiedPlayer) sender); + }else if(args[0].equalsIgnoreCase("bau")){ + sender.sendMessage("§8/§etnt §8- §7(de)aktiviert Explosionsschaden"); + sender.sendMessage("§8/§efire §8- §7(de)aktiviert Feuerschaden"); + sender.sendMessage("§8/§etestblock §8- §7Erneuert den nächsten Testblock"); + sender.sendMessage("§8/§ereset §8- §7Setzt die derzeitige Region zurück"); + sender.sendMessage("§8/§espeed §8- §7Ändert deine Fluggeschwindigkeit"); + sender.sendMessage("§8/§env §8- §7(de)aktiviert Nachtsicht"); + sender.sendMessage("§8/§etrace §8- §7Gibt einen Überblick über den TNT-Tracer"); + sender.sendMessage("§8/§ehelp bauserver §8- §7Hilfe zum Bauserver"); } } + + static void sendBauHelp(ProxiedPlayer p){ + TextComponent tc = new TextComponent("§8/§ebau tp §8- §7Gehe auf Bauserver von Freunden!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eZu einem anderen Bauserver").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau tp ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau addmember §8- §7Fügt einen Freund hinzu"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eFüge einen Freund hinzu").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau addmember ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau delmember §8- §7Entfernt einen Spieler"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eEntfernt einen Spieler").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau delmember ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau togglebuild §8- §7Berechtigung zum Bauen, /tp, /gm"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eStellt Spielerberechtigungen ein").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau togglebuild ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau togglewe §8- §7Berechtigung für WorldEdit, /testblock"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eStellt Spielerberechtigungen ein").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau togglewe ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau toggleworld §8- §7/reset, /trace, /fire, /tnt"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eStellt Spielerberechtigungen ein").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau toggleworld ")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ebau delete §8- §7Setzt deine Bauwelt zurück"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eBauwelt zurücksetzen").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/bau delete")); + p.sendMessage(tc); + + tc = new TextComponent("§8/§ehelp bau §8- §7Hilfe zu nützlichen Werkzeugen"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eNützliche Zusatzfunktionen").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help bau")); + p.sendMessage(tc); + } } diff --git a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java index e699d85..fb0e9d4 100644 --- a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java +++ b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java @@ -3,6 +3,9 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.arenasystem.Arena; import de.steamwar.bungeecore.arenasystem.ArenaSystem; +import de.steamwar.bungeecore.bausystem.BauServer; +import de.steamwar.bungeecore.bausystem.BauSystem; +import de.steamwar.bungeecore.sql.BauweltMember; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.ClickEvent; @@ -16,6 +19,8 @@ import java.util.ArrayList; public class JoinmeCommand extends Command { + private static final String DENY_MESSAGE = "§cDu kannst diesem Spieler derzeit nicht folgen."; + public JoinmeCommand() { super("join", "", "joinme"); } @@ -42,18 +47,27 @@ public class JoinmeCommand extends Command { ServerInfo server = target.getServer().getInfo(); String ServerPerm = BungeeCore.serverPermissions.get(server.getName()); Arena arena = ArenaSystem.getArena(target); + BauServer bauserver = BauSystem.getBau(target); - if(arena != null){ + if(arena != null) { arena.sendPlayer(player); + }else if(bauserver != null){ + if(bauserver.getOwner().equals(player.getUniqueId()) || + BauweltMember.getBauMember(bauserver.getOwner(), player.getUniqueId()) != null){ + BauSystem.sendToServer(player, bauserver.getOwner()); + }else{ + BauServer.sendDeniedMessage(player, bauserver.getOwner()); + player.sendMessage(BungeeCore.ChatPrefix + DENY_MESSAGE); + } }else if(ServerPerm != null && !player.hasPermission(ServerPerm)){ - player.sendMessage(BungeeCore.ChatPrefix + "§cDu kannst diesem Spieler derzeit nicht folgen."); + player.sendMessage(BungeeCore.ChatPrefix + DENY_MESSAGE); }else if(ServerPerm == null && !player.getGroups().contains("team")) { - player.sendMessage(BungeeCore.ChatPrefix + "§cDu kannst diesem Spieler derzeit nicht folgen."); + player.sendMessage(BungeeCore.ChatPrefix + DENY_MESSAGE); }else{ player.connect(server); } } else { - player.sendMessage(BungeeCore.ChatPrefix + "§7Mit §r/join §8[§rSpieler§8] §7kannst du einem Spieler folgen §8(z.B. in eine Arena)§7."); + player.sendMessage(BungeeCore.ChatPrefix + "§7Mit §8/§ejoin §8[§eSpieler§8] §7kannst du einem Spieler folgen §8(z.B. in eine Arena)§7."); } } } diff --git a/src/de/steamwar/bungeecore/sql/Bauwelt.java b/src/de/steamwar/bungeecore/sql/Bauwelt.java new file mode 100644 index 0000000..49b15d8 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Bauwelt.java @@ -0,0 +1,77 @@ +package de.steamwar.bungeecore.sql; + + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Bauwelt { + + private final int UserID; + private boolean TNTDmg; + private boolean FireDmg; + + private static List welten = new ArrayList<>(); + + private Bauwelt(int userID, boolean tntDmg, boolean fireDmg, boolean updateDB){ + UserID = userID; + TNTDmg = tntDmg; + FireDmg = fireDmg; + if(updateDB) + updateDB(); + welten.add(this); + } + + public Bauwelt(int userID, boolean tntDmg, boolean fireDmg){ + this(userID, tntDmg, fireDmg, true); + } + + public Bauwelt(UUID userID, boolean tntDmg, boolean fireDmg){ + this(WarkingUser.get(userID).getId(), tntDmg, fireDmg, true); + } + + private void updateDB(){ + sql.update("INSERT INTO Bauwelt" + + " (UserID, TNTDmg, FireDmg)" + + " VALUES" + + " ('" + UserID + "', '" + sql.booleanToInt(TNTDmg) + "', '" + sql.booleanToInt(FireDmg) + "')" + + " ON DUPLICATE KEY UPDATE" + + " TNTDmg = VALUES(TNTDmg), FireDmg = VALUES(FireDmg)"); + } + + public static Bauwelt getBauwelt(UUID user){ + return getBauwelt(WarkingUser.get(user).getId()); + } + + public static Bauwelt getBauwelt(int userId){ + for(Bauwelt welt : welten) + if(welt.UserID == userId) + return welt; + ResultSet bauwelt = sql.select("SELECT * FROM Bauwelt WHERE UserID = '" + userId + "'"); + try { + if(bauwelt == null || !bauwelt.next()){ + return null; + } + boolean tntDmg = bauwelt.getBoolean("TNTDmg"); + boolean fireDmg = bauwelt.getBoolean("FireDmg"); + return new Bauwelt(userId, tntDmg, fireDmg, false); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public static void clearCache(){ + welten.clear(); + } + + public int getUserID() { + return UserID; + } + + public UUID getUUID(){ + return WarkingUser.get(UserID).getUUID(); + } +} \ No newline at end of file diff --git a/src/de/steamwar/bungeecore/sql/BauweltMember.java b/src/de/steamwar/bungeecore/sql/BauweltMember.java new file mode 100644 index 0000000..1cc384b --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/BauweltMember.java @@ -0,0 +1,134 @@ +package de.steamwar.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +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 ownerID, int memberID, boolean build, boolean worldEdit, boolean world, boolean updateDB){ + BauweltID = ownerID; + MemberID = memberID; + Build = build; + WorldEdit = worldEdit; + World = world; + if(updateDB) + updateDB(); + members.add(this); + } + + public BauweltMember(int ownerID, int memberID, boolean build, boolean worldEdit, boolean world){ + this(ownerID, memberID, build, worldEdit, world, true); + } + + public BauweltMember(UUID ownerID, UUID memberID, boolean build, boolean worldEdit, boolean world){ + this(WarkingUser.get(ownerID).getId(), 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, ChangeGM, Teleport, WorldEdit, Testblock)" + + " VALUES" + + " ('" + BauweltID + "', '" + MemberID + "', '" + sql.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 ownerID, UUID memberID){ + return getBauMember(WarkingUser.get(ownerID).getId(), WarkingUser.get(memberID).getId()); + } + + public static BauweltMember getBauMember(int ownerID, int memberID){ + for(BauweltMember member : members) + if(member.BauweltID == ownerID && member.MemberID == memberID) + return member; + ResultSet member = sql.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + ownerID + "' AND MemberID = '" + memberID + "'"); + try { + if(member == null || !member.next()){ + return null; + } + boolean build = member.getBoolean("Build"); + boolean worldEdit = member.getBoolean("WorldEdit"); + boolean world = member.getBoolean("World"); + return new BauweltMember(ownerID, memberID, build, worldEdit, world, false); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public static void clearCache(){ + members.clear(); + } + + 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 world = memberlist.getBoolean("World"); + members.add(new BauweltMember(bauweltID, memberID, build, worldEdit, world, 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/bungeecore/sql/Permission.java b/src/de/steamwar/bungeecore/sql/Permission.java new file mode 100644 index 0000000..0064e6c --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Permission.java @@ -0,0 +1,9 @@ +package de.steamwar.bungeecore.sql; + +public enum Permission { + world, + worldedit, + build, + member, + owner +} diff --git a/src/de/steamwar/bungeecore/sql/WarkingUser.java b/src/de/steamwar/bungeecore/sql/WarkingUser.java index c0ca4cc..81c6486 100644 --- a/src/de/steamwar/bungeecore/sql/WarkingUser.java +++ b/src/de/steamwar/bungeecore/sql/WarkingUser.java @@ -7,17 +7,20 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.UUID; public class WarkingUser { - public int id; - public UUID UUID; - public String UserName; - public String UserGroup; - public Timestamp BanTime; - public String BanReason; + private int id; + private UUID UUID; + private String UserName; + private String UserGroup; + private Timestamp BanTime; + private String BanReason; + private static final List allUsers = new ArrayList<>(); public static Timestamp PermaBan = Timestamp.from(Instant.ofEpochSecond(946684800)); public WarkingUser(int id){ @@ -42,9 +45,9 @@ public class WarkingUser { UUID = connection.getUniqueId(); UserName = connection.getName(); UserGroup = "Member"; - sql.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES ('" + UUID.toString() + "', '" + UserName + "', '" + UserGroup + "')"); + sql.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES ('" + getUUID().toString() + "', '" + getUserName() + "', '" + getUserGroup() + "')"); - dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + UUID.toString() + "'"); + dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + getUUID().toString() + "'"); init(dbplayer); } } catch (SQLException e) { @@ -62,9 +65,9 @@ public class WarkingUser { UUID = player.getUniqueId(); UserName = player.getName(); UserGroup = "Member"; - sql.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES ('" + UUID.toString() + "', '" + UserName + "', '" + UserGroup + "')"); + sql.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES ('" + getUUID().toString() + "', '" + getUserName() + "', '" + getUserGroup() + "')"); - dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + UUID.toString() + "'"); + dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + getUUID().toString() + "'"); init(dbplayer); } } catch (SQLException e) { @@ -72,6 +75,32 @@ public class WarkingUser { } } + public static WarkingUser get(String userName){ + userName = sql.disarmString(userName); + for(WarkingUser user : allUsers) + if(user.getUserName().equalsIgnoreCase(userName)) + return user; + return new WarkingUser(userName); + } + + public static WarkingUser get(UUID uuid){ + for(WarkingUser user : allUsers) + if(user.getUUID().equals(uuid)) + return user; + return new WarkingUser(uuid); + } + + public static WarkingUser get(int id){ + for(WarkingUser user : allUsers) + if(user.getId() == id) + return user; + return new WarkingUser(id); + } + + public static void clearCache(){ + allUsers.clear(); + } + private void init(ResultSet dbplayer){ try { if(!dbplayer.next()){ @@ -83,19 +112,20 @@ public class WarkingUser { UserGroup = dbplayer.getString("UserGroup"); BanTime = dbplayer.getTimestamp("BanTime"); BanReason = dbplayer.getString("BanReason"); + allUsers.add(this); } catch (SQLException e) { e.printStackTrace(); } } public boolean isBanned(){ - if(BanTime == null){ + if(getBanTime() == null){ return false; - }else if(BanTime.after(new Date()) || BanTime.before(PermaBan)){ + }else if(getBanTime().after(new Date()) || getBanTime().before(PermaBan)){ return true; }else{ - sql.update("UPDATE UserData SET BanTime = NULL, BanReason = '' WHERE UUID = '" + UUID.toString() + "'"); - sql.update("DELETE FROM BannedUserIPs WHERE UserID = '" + id + "'"); + sql.update("UPDATE UserData SET BanTime = NULL, BanReason = '' WHERE UUID = '" + getUUID().toString() + "'"); + sql.update("DELETE FROM BannedUserIPs WHERE UserID = '" + getId() + "'"); BanTime = null; BanReason = ""; return false; @@ -106,14 +136,38 @@ public class WarkingUser { sql.update("INSERT INTO BannedUserIPs\n" + " (UserID, Timestamp, IP)\n" + "VALUES\n" + - " (" + id + ", NOW(), '" + ip + "')\n" + + " (" + getId() + ", NOW(), '" + ip + "')\n" + "ON DUPLICATE KEY UPDATE\n" + " Timestamp=NOW()"); } public void banPlayer(String ip, Timestamp time, String banReason){ - sql.update("UPDATE UserData SET BanTime = '" + time.toString() + "', BanReason = '" + banReason + "' WHERE UUID = '" + UUID.toString() + "'"); + sql.update("UPDATE UserData SET BanTime = '" + time.toString() + "', BanReason = '" + banReason + "' WHERE UUID = '" + getUUID().toString() + "'"); if(ip != null) updateBanIP(ip); } + + public int getId() { + return id; + } + + public UUID getUUID() { + return UUID; + } + + public String getUserName() { + return UserName; + } + + public String getUserGroup() { + return UserGroup; + } + + public Timestamp getBanTime() { + return BanTime; + } + + public String getBanReason() { + return BanReason; + } } diff --git a/src/de/steamwar/bungeecore/sql/sql.java b/src/de/steamwar/bungeecore/sql/sql.java index 2ca98d9..294c7df 100644 --- a/src/de/steamwar/bungeecore/sql/sql.java +++ b/src/de/steamwar/bungeecore/sql/sql.java @@ -73,8 +73,16 @@ public class sql { return null; } - public static Connection getCon(){ - return con; + + public static String disarmString(String s){ + return s.replace("'", ""); + } + + public static Integer booleanToInt(boolean b){ + if(b) + return 1; + else + return 0; } public static void setWebpw(WarkingUser user, String password){ @@ -85,7 +93,7 @@ public class sql { " (?, password(?))\n" + "ON DUPLICATE KEY UPDATE\n" + " WebPassword = VALUES(WebPassword)"); - st.setInt(1, user.id); + st.setInt(1, user.getId()); st.setString(2, password); st.executeUpdate(); } catch (SQLException e) { @@ -98,7 +106,7 @@ public class sql { " (?, password(?))\n" + "ON DUPLICATE KEY UPDATE\n" + " WebPassword = VALUES(WebPassword)"); - st.setInt(1, user.id); + st.setInt(1, user.getId()); st.setString(2, password); st.executeUpdate(); } catch (SQLException ex) {