From 1e38e9a0e246e717ab0119c26a99ec390095130c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 1 May 2020 11:33:32 +0200 Subject: [PATCH] New Bungee-Based check system Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/ArenaMode.java | 8 + src/de/steamwar/bungeecore/BungeeCore.java | 3 + .../steamwar/bungeecore/SubserverSystem.java | 11 +- .../bungeecore/commands/BauCommand.java | 2 +- .../bungeecore/commands/ChallengeCommand.java | 2 +- .../bungeecore/commands/CheckCommand.java | 253 ++++++++++++++++++ .../bungeecore/commands/FightCommand.java | 4 +- .../bungeecore/commands/JoinmeCommand.java | 3 + .../bungeecore/listeners/CheckListener.java | 24 ++ .../listeners/ConnectionListener.java | 5 + .../bungeecore/sql/CheckedSchematic.java | 12 + src/de/steamwar/bungeecore/sql/Schematic.java | 82 ++++++ .../bungeecore/sql/SchematicType.java | 115 ++++++++ 13 files changed, 516 insertions(+), 8 deletions(-) create mode 100644 src/de/steamwar/bungeecore/commands/CheckCommand.java create mode 100644 src/de/steamwar/bungeecore/listeners/CheckListener.java create mode 100644 src/de/steamwar/bungeecore/sql/CheckedSchematic.java create mode 100644 src/de/steamwar/bungeecore/sql/Schematic.java create mode 100644 src/de/steamwar/bungeecore/sql/SchematicType.java diff --git a/src/de/steamwar/bungeecore/ArenaMode.java b/src/de/steamwar/bungeecore/ArenaMode.java index a661712e..a0313d77 100644 --- a/src/de/steamwar/bungeecore/ArenaMode.java +++ b/src/de/steamwar/bungeecore/ArenaMode.java @@ -8,6 +8,7 @@ public class ArenaMode { private static Map byChat = new HashMap<>(); private static Map byInternal = new HashMap<>(); + private static Map byCheckSchemType = new HashMap<>(); private static List allModes = new LinkedList<>(); private static Random random = new Random(); @@ -33,6 +34,10 @@ public class ArenaMode { return chatNames; } + public static ArenaMode getByCheckSchemType(String checkSchemType){ + return byCheckSchemType.get(checkSchemType); + } + public static List getAllModes(){ return allModes; } @@ -55,6 +60,9 @@ public class ArenaMode { for(String name : chatNames){ byChat.put(name.toLowerCase(), this); } + + if(config.contains("checkSchemType")) + byCheckSchemType.put(config.getString("checkSchemType").toLowerCase(), this); } public String getInternalName() { diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 50283bb1..b06c2b37 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -58,6 +58,7 @@ public class BungeeCore extends Plugin { new LabyMod(); new ChatListener(); new BanListener(); + new CheckListener(); commands.put("/b", null); commands.put("/gs", null); @@ -92,6 +93,7 @@ public class BungeeCore extends Plugin { new BugCommand(); new WhoisCommand(); new RegelnCommand(); + new CheckCommand(); new EventStarter(); new Broadcaster(); @@ -195,6 +197,7 @@ public class BungeeCore extends Plugin { BAUWELT15 = config.getString("bauwelt15"); Broadcaster.setBroadCastMsgs(config.getStringList("broadcasts").toArray(new String[1])); PollSystem.init(config.getString("poll.question"), config.getStringList("poll.answers")); + CheckCommand.loadCheckQuestions(config.getSection("checkquestions")); Persistent.setChatPrefix(CHAT_PREFIX); Persistent.setLobbyServer(LOBBY_SERVER); diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 0386c3b8..6787ae2c 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -65,7 +65,7 @@ public class SubserverSystem { * @return * The new started subserver. */ - public static Subserver startArena(ArenaMode modus, String map, int eventFightID, String serverName, String mapName, UUID player1, UUID player2){ + public static Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, String serverName, String mapName, UUID player1, UUID player2){ //Generate missing parameters int port = freePort(2500); @@ -96,6 +96,8 @@ public class SubserverSystem { cmd.add("-DblueLeader=" + player1.toString()); if(player2 != null) cmd.add("-DredLeader=" + player2.toString()); + if(checkSchemID != 0) + cmd.add("-DcheckSchemID=" + checkSchemID); cmd.add("-Xmx2G"); cmd.addAll(JVM_ARGS); cmd.add("-jar"); @@ -135,7 +137,7 @@ public class SubserverSystem { } public static Subserver startArena(ArenaMode modus, String map){ - return startArena(modus, map, 0, null, null, null, null); + return startArena(modus, map, 0, 0, null, null, null, null); } public static Subserver startEventArena(EventFight eventFight, String serverName){ @@ -143,14 +145,15 @@ public class SubserverSystem { eventFight.getSpielmodus(), eventFight.getMap(), eventFight.getFightID(), + 0, serverName, serverName.replace(' ', '_') + eventFight.getStartTime().toLocalDateTime().format(EventCommand.TIME_FORMAT), null, null); } - public static void sendToTestServer(ProxiedPlayer p, ArenaMode m, String map){ - startArena(m, map, -1, p.getName() + "s Bau", p.getName(), p.getUniqueId(), null).sendPlayer(p); + public static void sendToTestServer(ProxiedPlayer p, ArenaMode m, String map, int checkSchemId){ + startArena(m, map, -1, checkSchemId, p.getName() + "s Bau", p.getName(), p.getUniqueId(), null).sendPlayer(p); } public static void sendToBauServer(ProxiedPlayer p, UUID owner){ diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index 3dd26286..360bb210 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -213,7 +213,7 @@ public class BauCommand { break; } } - SubserverSystem.sendToTestServer(p, mode, map); + SubserverSystem.sendToTestServer(p, mode, map, 0); })); } diff --git a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java index 3b3636bd..690cfee6 100644 --- a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java +++ b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java @@ -40,7 +40,7 @@ public class ChallengeCommand extends BasicCommand { challenges.remove(target); challenges.remove(player); - Subserver arena = SubserverSystem.startArena(mode, map, 0, null, null, player.getUniqueId(), target.getUniqueId()); + Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, null, null, player.getUniqueId(), target.getUniqueId()); arena.sendPlayer(player); arena.sendPlayer(target); diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java new file mode 100644 index 00000000..eb0eea73 --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -0,0 +1,253 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.*; +import de.steamwar.bungeecore.listeners.ConnectionListener; +import de.steamwar.bungeecore.sql.CheckedSchematic; +import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicType; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.config.Configuration; + +import java.sql.Timestamp; +import java.time.Instant; +import java.util.*; +import java.util.logging.Level; + +public class CheckCommand extends BasicCommand { + private static Map> checkQuestions = new HashMap<>(); + + private static Map currentCheckers = new HashMap<>(); + private static Map currentSchems = new HashMap<>(); + + public static void loadCheckQuestions(Configuration config){ + for(String schemType : config.getKeys()){ + checkQuestions.put(SchematicType.fromDB(schemType), config.getStringList(schemType)); + } + } + + public static boolean isChecking(ProxiedPlayer player){ + return currentCheckers.containsKey(player); + } + + public CheckCommand() { + super("check", ConnectionListener.CHECK_PERMISSION); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if(!(sender instanceof ProxiedPlayer)) + return; + ProxiedPlayer player = (ProxiedPlayer) sender; + + if(args.length == 0){ + help(sender); + return; + } + + switch(args[0].toLowerCase()){ + case "list": + list(player); + break; + case "schematic": + schematic(player, args[1]); + break; + case "next": + case "allow": + next(player); + break; + case "cancel": + abort(player); + break; + case "decline": + decline(player, args); + break; + default: + help(player); + } + } + + private void list(ProxiedPlayer player) { + List schematicList = new LinkedList<>(); + + for (SchematicType type : SchematicType.values()) { + if (type.check()) + schematicList.addAll(Schematic.getAllSchemsOfType(type)); + } + + BungeeCore.send(player, "§e§lZu prüfen sind " + schematicList.size() + " Schematics"); + + for (Schematic schematic : schematicList) { + CheckSession current = currentSchems.get(schematic.getSchemID()); + if (current == null) { + BungeeCore.send(player, + "§8" + schematic.getSchemType().getKuerzel() + " §7" + SteamwarUser.get(schematic.getSchemOwner()).getUserName() + " §e" + schematic.getSchemName(), + "§eSchematic prüfen", + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check schematic " + schematic.getSchemID())); + } else { + BungeeCore.send(player, + "§8" + schematic.getSchemType().getKuerzel() + " §7" + SteamwarUser.get(schematic.getSchemOwner()).getUserName() + " §e" + schematic.getSchemName() + " §7wird geprüft von §e" + current.checker.getName(), + "§eZum Prüfer", + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + current.checker.getName())); + } + } + } + + private void schematic(ProxiedPlayer player, String schemID){ + if(currentCheckers.containsKey(player)){ + BungeeCore.send(player, "§cDu prüfst schon eine Schematic!"); + return; + } + + Schematic schem = Schematic.getSchemFromDB(Integer.parseInt(schemID)); + if(!schem.getSchemType().check()){ + ProxyServer.getInstance().getLogger().log(Level.SEVERE, player.getName() + " tried to check an uncheckable schematic!"); + return; + }else if(schem.getSchemOwner() == SteamwarUser.get(player.getUniqueId()).getId()) { + BungeeCore.send(player, "§cDu kannst nicht deine eigenen Schematics prüfen"); + return; + } + + new CheckSession(player, schem); + } + + private static boolean notChecking(ProxiedPlayer player){ + if(!currentCheckers.containsKey(player)){ + BungeeCore.send(player, "§cDu prüfst derzeit nicht."); + return true; + } + return false; + } + + private void next(ProxiedPlayer player){ + if(notChecking(player)) + return; + + currentCheckers.get(player).next(); + } + + public static void abort(ProxiedPlayer player){ + if(notChecking(player)) + return; + + BungeeCore.send(player, "§aDer Prüfvorgang wurde abgebrochen!"); + currentCheckers.get(player).abort(); + } + + private void decline(ProxiedPlayer player, String[] args){ + if(notChecking(player)) + return; + + if(args.length < 2) { + help(player); + return; + } + + StringBuilder message = new StringBuilder(); + for (int i = 1; i < args.length; i++) + message.append(args[i]).append(" "); + + currentCheckers.get(player).decline(message.toString()); + } + + private void help(CommandSender sender){ + BungeeCore.send(sender, "§8/§echeck list §8- §7Zeigt die Liste der ungeprüften Schematics"); + BungeeCore.send(sender, "§8/§echeck next §8- §7Nächste Prüffrage"); + BungeeCore.send(sender, "§8/§echeck allow §8- §7Schematic freigeben"); + BungeeCore.send(sender, "§8/§echeck decline [Grund] §8- §7Schematic nicht freigeben"); + BungeeCore.send(sender, "§8/§echeck cancel §8- §7Bricht das Prüfen ab"); + } + + private static class CheckSession{ + private final ProxiedPlayer checker; + private final Schematic schematic; + private final Timestamp startTime; + private final ListIterator checkList; + + private CheckSession(ProxiedPlayer checker, Schematic schematic){ + this.checker = checker; + this.schematic = schematic; + this.startTime = Timestamp.from(Instant.now()); + this.checkList = checkQuestions.get(schematic.getSchemType()).listIterator(); + + currentCheckers.put(checker, this); + currentSchems.put(schematic.getSchemID(), this); + + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(checker.getUniqueId())) { + subserver.stop(); + break; + } + } + + ArenaMode mode = ArenaMode.getByCheckSchemType(schematic.getSchemType().toDB()); + SubserverSystem.sendToTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getSchemID()); + }); + } + + private void next() { + if(!checkList.hasNext()){ + accept(); + return; + } + + BungeeCore.send(checker, checkList.next()); + + TextComponent next; + if(checkList.hasNext()){ + next = new TextComponent("next "); + next.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check next")); + }else{ + next = new TextComponent("accept "); + next.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check allow")); + } + next.setColor(ChatColor.GREEN); + + TextComponent decline = new TextComponent("decline"); + decline.setColor(ChatColor.RED); + decline.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/check decline ")); + + next.addExtra(decline); + checker.sendMessage(next); + } + + private void accept(){ + schematic.setSchemType(schematic.getSchemType().fightType()); + CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); + ProxiedPlayer player = ProxyServer.getInstance().getPlayer(SteamwarUser.get(schematic.getSchemOwner()).getUuid()); + if(player != null) + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§aDein §e" + schematic.getSchemType().name() + " " + schematic.getSchemName() + " §ewurde freigegeben§8!"); + BungeeCore.send(checker, "§aDie Schematic §e" + schematic.getSchemName() + " §avon §e" + SteamwarUser.get(schematic.getSchemOwner()).getUserName() + " §aist nun freigegeben!"); + abort(); + } + + private void decline(String reason){ + schematic.setSchemType(SchematicType.Normal); + CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); + ProxiedPlayer player = ProxyServer.getInstance().getPlayer(SteamwarUser.get(schematic.getSchemOwner()).getUuid()); + if(player != null) + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDein §e" + schematic.getSchemType().name() + " " + schematic.getSchemName() + " §cwurde abgelehnt§8: §c" + reason); + BungeeCore.send(checker, "§aDie Schematic §e" + schematic.getSchemName() + " §avon §e" + SteamwarUser.get(schematic.getSchemOwner()).getUserName() + " §awurde aufgrund von §e" + reason + " §aabgelehnt!"); + abort(); + } + + private void abort(){ + currentCheckers.remove(checker); + currentSchems.remove(schematic.getSchemID()); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(checker.getUniqueId())) { + subserver.stop(); + break; + } + } + }); + } + } +} diff --git a/src/de/steamwar/bungeecore/commands/FightCommand.java b/src/de/steamwar/bungeecore/commands/FightCommand.java index d744552d..5a53a483 100644 --- a/src/de/steamwar/bungeecore/commands/FightCommand.java +++ b/src/de/steamwar/bungeecore/commands/FightCommand.java @@ -35,7 +35,7 @@ public class FightCommand extends BasicCommand { return null; } - private static String getMap(CommandSender sender, ArenaMode mode, String arg){ + static String getMap(CommandSender sender, ArenaMode mode, String arg){ String realMap = mode.hasMap(arg.toLowerCase()); if(realMap != null) return realMap; @@ -140,7 +140,7 @@ public class FightCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { createArena(sender, "/fight ", args, 0, (player, mode, map) -> { - Subserver arena = SubserverSystem.startArena(mode, map, 0, null, null, player.getUniqueId(), null); + Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, null, null, player.getUniqueId(), null); arena.sendPlayer(player); TextComponent tc = new TextComponent(BungeeCore.CHAT_PREFIX + "§7Klicke §ehier§7, um §e" + mode.getDisplayName() + " §7gegen §e" + player.getName() + " §7zu §7kämpfen!"); tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§aGegen §7" + player.getName() + " §ekämpfen").create())); diff --git a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java index 23a13dd3..bd7fdb1b 100644 --- a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java +++ b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java @@ -39,6 +39,9 @@ public class JoinmeCommand extends BasicCommand { }else if(target.equals(player)){ BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cSei eins mit dir selbst!"); return; + }else if(CheckCommand.isChecking(player)){ + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§cDu prüfst gerade eine Schematic!"); + return; } ServerInfo server = target.getServer().getInfo(); String serverPerm = BungeeCore.serverPermissions.get(server.getName()); diff --git a/src/de/steamwar/bungeecore/listeners/CheckListener.java b/src/de/steamwar/bungeecore/listeners/CheckListener.java new file mode 100644 index 00000000..1c10b4b6 --- /dev/null +++ b/src/de/steamwar/bungeecore/listeners/CheckListener.java @@ -0,0 +1,24 @@ +package de.steamwar.bungeecore.listeners; + +import de.steamwar.bungeecore.commands.CheckCommand; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.PlayerDisconnectEvent; +import net.md_5.bungee.api.event.ServerSwitchEvent; +import net.md_5.bungee.event.EventHandler; + +public class CheckListener extends BasicListener { + + @EventHandler + public void onServerSwitch(ServerSwitchEvent e){ + ProxiedPlayer player = e.getPlayer(); + if(CheckCommand.isChecking(player)) + CheckCommand.abort(player); + } + + @EventHandler + public void onPlayerDisconnect(PlayerDisconnectEvent e){ + ProxiedPlayer player = e.getPlayer(); + if(CheckCommand.isChecking(player)) + CheckCommand.abort(player); + } +} diff --git a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java index ddba2827..3d6e1df1 100644 --- a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java +++ b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java @@ -26,6 +26,7 @@ public class ConnectionListener extends BasicListener { private static final String ADMIN_GROUP = "admin"; public static final String TEAM_GROUP = "team"; + public static final String CHECK_PERMISSION = "bungeecore.check"; private static final String YOUTUBER_MODS = "bungeecore.youtubermods"; @EventHandler @@ -37,24 +38,28 @@ public class ConnectionListener extends BasicListener { case "Admin": player.addGroups(ADMIN_GROUP, TEAM_GROUP); player.setPermission("bungeecore.group.admin", true); + player.setPermission("bungeecore.check", true); player.setPermission(YOUTUBER_MODS, true); player.setDisplayName("§4Admin " + player.getName() + "§r"); break; case "Developer": player.addGroups(ADMIN_GROUP, TEAM_GROUP); player.setPermission("bungeecore.group.developer", true); + player.setPermission("bungeecore.check", true); player.setPermission(YOUTUBER_MODS, true); player.setDisplayName("§3Developer " + player.getName() + "§r"); break; case "Moderator": player.addGroups(ADMIN_GROUP, TEAM_GROUP); player.setPermission("bungeecore.group.moderator", true); + player.setPermission("bungeecore.check", true); player.setPermission(YOUTUBER_MODS, true); player.setDisplayName("§bModerator " + player.getName() + "§r"); break; case "Supporter": player.addGroups(TEAM_GROUP); player.setPermission("bungeecore.group.supporter", true); + player.setPermission("bungeecore.check", true); player.setPermission(YOUTUBER_MODS, true); player.setDisplayName("§1Supporter " + player.getName() + "§r"); break; diff --git a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java new file mode 100644 index 00000000..f089cef8 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java @@ -0,0 +1,12 @@ +package de.steamwar.bungeecore.sql; + +import java.sql.Timestamp; + +public class CheckedSchematic { + private CheckedSchematic(){} + + public static void create(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String reason){ + SQL.update("INSERT INTO CheckedSchematic (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", + schemName, schemOwner, validator, startTime, endTime, reason); + } +} diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java new file mode 100644 index 00000000..914f1ff6 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -0,0 +1,82 @@ +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 Schematic { + private final int schemID; + private final String schemName; + private final int schemOwner; + private SchematicType schemType; + + private Schematic(ResultSet rs) throws SQLException { + this.schemID = rs.getInt("SchemID"); + this.schemName = rs.getString("SchemName"); + this.schemOwner = rs.getInt("SchemOwner"); + this.schemType = SchematicType.fromDB(rs.getString("SchemType")); + } + + public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ + return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static Schematic getSchemFromDB(String schemName, int schemOwner){ + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); + try { + if(schematic == null || !schematic.next()){ + return null; + } + return new Schematic(schematic); + } catch (SQLException e) { + throw new SecurityException("Failed loading schematic", e); + } + } + + public static Schematic getSchemFromDB(int schemID){ + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemID = ?", schemID); + try { + if(!schematic.next()) + throw new SecurityException("Failed loading schematic " + schemID); + return new Schematic(schematic); + } catch (SQLException e) { + throw new SecurityException("Failed loading schematic", e); + } + } + + public static List getAllSchemsOfType(SchematicType schemType){ + try{ + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemType = ?", schemType.toDB()); + List schematics = new ArrayList<>(); + while(schematic.next()){ + schematics.add(new Schematic(schematic)); + } + return schematics; + }catch(SQLException e){ + throw new SecurityException("Failed loading all schems of type", e); + } + } + + public int getSchemID() { + return schemID; + } + + public String getSchemName() { + return schemName; + } + + public int getSchemOwner() { + return schemOwner; + } + + public SchematicType getSchemType() { + return schemType; + } + + public void setSchemType(SchematicType schemType) { + this.schemType = schemType; + SQL.update("UPDATE Schematic SET SchemType = ? WHERE SchemID = ?", schemType, schemID); + } +} diff --git a/src/de/steamwar/bungeecore/sql/SchematicType.java b/src/de/steamwar/bungeecore/sql/SchematicType.java new file mode 100644 index 00000000..2b9d4632 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchematicType.java @@ -0,0 +1,115 @@ +package de.steamwar.bungeecore.sql; + +import de.steamwar.bungeecore.BungeeCore; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.*; + +public class SchematicType { + public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible + + private static final Map fromDB; + private static final Map fightType; + private static final List types; + + static{ + File file = new File(BungeeCore.get().getDataFolder(), "SchematicTypes.yml"); + + if(!file.exists()) + throw new SecurityException("SchematicType-ConfigFile not found!"); + + Configuration config; + try { + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); + } catch (IOException e) { + throw new SecurityException("Could not load SchematicTypes", e); + } + + List tmpTypes = new LinkedList<>(); + Map tmpFromDB = new HashMap<>(); + Map tmpFightType = new HashMap<>(); + + tmpTypes.add(Normal); + tmpFromDB.put(Normal.name().toLowerCase(), Normal); + + for(String type : config.getKeys()){ + Configuration section = config.getSection(type); + assert section != null; + String checktype = section.getString("checktype"); + SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? tmpFromDB.get(checktype.toLowerCase()) : null); + tmpTypes.add(current); + tmpFightType.put(current, current.checkType); + tmpFromDB.put(type.toLowerCase(), current); + } + + fromDB = Collections.unmodifiableMap(tmpFromDB); + fightType = Collections.unmodifiableMap(tmpFightType); + types = Collections.unmodifiableList(tmpTypes); + } + + private final String name; + private final String kuerzel; + private final Type type; + private SchematicType checkType; + + private SchematicType(String name, String kuerzel, Type type, SchematicType checkType){ + this.name = name; + this.kuerzel = kuerzel; + this.type = type; + this.checkType = checkType; + } + + public boolean isAssignable(){ + return type == Type.NORMAL || (type == Type.FIGHT_TYPE && checkType != null); + } + + public SchematicType checkType(){ + return checkType; + } + + public SchematicType fightType(){ + return fightType.get(this); + } + + public boolean check(){ + return type == Type.CHECK_TYPE; + } + + public boolean fight(){ + return type == Type.FIGHT_TYPE; + } + + public boolean writeable(){ + return type == Type.NORMAL; + } + + public String name(){ + return name; + } + + public String getKuerzel() { + return kuerzel; + } + + public String toDB(){ + return name.toLowerCase(); + } + + public static SchematicType fromDB(String input){ + return fromDB.getOrDefault(input.toLowerCase(), null); + } + + public static List values(){ + return types; + } + + enum Type{ + NORMAL, + CHECK_TYPE, + FIGHT_TYPE + } +}