diff --git a/src/de/steamwar/bungeecore/commands/ModCommand.java b/src/de/steamwar/bungeecore/commands/ModCommand.java index 21bfa5ca..9825bec4 100644 --- a/src/de/steamwar/bungeecore/commands/ModCommand.java +++ b/src/de/steamwar/bungeecore/commands/ModCommand.java @@ -1,6 +1,24 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.inventory.SWInventory; import de.steamwar.bungeecore.inventory.SWItem; @@ -8,17 +26,11 @@ import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; import de.steamwar.bungeecore.sql.*; import de.steamwar.command.SWCommand; -import lombok.Getter; -import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.connection.ProxiedPlayer; -import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; - -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; +import java.util.HashMap; +import java.util.Map; import java.util.stream.Collectors; public class ModCommand extends SWCommand { @@ -26,38 +38,21 @@ public class ModCommand extends SWCommand { public ModCommand() { super("mod", "bungeecore.softreload", "mods"); } - private static FilterType filtertype = FilterType.ALL; - @Register() + public static final Map playerFilterType = new HashMap<>(); + + @Register public void genericCommand(ProxiedPlayer p) { + playerFilterType.putIfAbsent(p,Mod.ModType.UNKLASSIFIED); openGui(p); } private void openGui(ProxiedPlayer p) { - SWStreamInv swStreamInv = new SWStreamInv<>(p,Message.parse("MOD_COMMAND_GUI_TITLE",p), (click, element) -> { - SWInventory swInventory = new SWInventory(p,9,Message.parse("MOD_COMMAND_GUI",p)); - - String modName = element.modName; - int modPlatform = element.platform.get(); - - swInventory.addItem(2, new SWItem(Message.parse("MOD_UNCLASSIFIED",p),8), (click1 -> Mod.set.update(0, modName, modPlatform))); - swInventory.addItem(3, new SWItem(Message.parse("MOD_ALLOWED",p), 2), (click1 -> Mod.set.update(1, modName, modPlatform))); - swInventory.addItem(4, new SWItem(Message.parse("MOD_PENDING",p), 11), (click1 -> Mod.set.update(2, modName, modPlatform))); - swInventory.addItem(5, new SWItem(Message.parse("MOD_FORBIDDEN",p),1), (click1 -> Mod.set.update(3, modName, modPlatform))); - swInventory.addItem(6, new SWItem(Message.parse("MOD_YT",p), 13), (click1 -> Mod.set.update(4, modName, modPlatform))); - - swInventory.addItem(8,new SWItem("ARROW",Message.parse("MOD_ITEM_BACK",p)), click1 -> { - swInventory.close(); - openGui(p); - }); - - swInventory.open(); + SWStreamInv swStreamInv = new SWStreamInv<>(p,Message.parse("MOD_COMMAND_GUI_TITLE",p), (click, element) -> { + openClassificationGui(p,element); },page -> { - if(filtertype == FilterType.ALL) { - return getAllMods(page,45).stream().map(mod -> new SWListInv.SWListEntry<>(getModItem(mod),mod)).collect(Collectors.toList()); - }else { - return getAllModsFiltered(page,45,filtertype.value).stream().map(mod -> new SWListInv.SWListEntry<>(getModItem(mod),mod)).collect(Collectors.toList()); - } + Mod.ModType filtertype = playerFilterType.get(p); + return Mod.getAllModsFiltered(page,45, filtertype).stream().map(mod -> new SWListInv.SWListEntry<>(getModItem(mod),mod)).collect(Collectors.toList()); }); swStreamInv.addItem(52,new SWItem("NAME_TAG","Filter"), click -> { @@ -68,15 +63,20 @@ public class ModCommand extends SWCommand { swStreamInv.open(); } + public void updateAndCloseGui(Mod.ModType modType,String modName,Mod.Platform modPlatform,SWInventory toClose,ProxiedPlayer p) { + Mod.setMod(modType, modName, modPlatform); + toClose.close(); + openGui(p); + } + private void openFilterGui(ProxiedPlayer p) { SWInventory inv = new SWInventory(p, 9, "Filter"); - inv.addItem(1, new SWItem(Message.parse("MOD_UNCLASSIFIED",p),8), click -> filtertype = FilterType.UNCLASSIFIED); - inv.addItem(2, new SWItem(Message.parse("MOD_ALLOWED",p),2), click -> filtertype = FilterType.ALLOWED); - inv.addItem(3, new SWItem(Message.parse("MOD_PENDING",p), 11),click -> filtertype = FilterType.PENDING); - inv.addItem(4, new SWItem(Message.parse("MOD_FORBIDDEN",p),1), click -> filtertype = FilterType.FORBIDDEN); - inv.addItem(5, new SWItem(Message.parse("MOD_YT",p),13), click -> filtertype = FilterType.YT); - inv.addItem(6, new SWItem(Message.parse("MOD_ALL",p),15), click -> filtertype = FilterType.ALL); + inv.addItem(1, new SWItem(Message.parse("MOD_UNCLASSIFIED",p),8), click -> playerFilterType.replace(p, Mod.ModType.UNKLASSIFIED)); + inv.addItem(2, new SWItem(Message.parse("MOD_ALLOWED",p),2), click -> playerFilterType.replace(p, Mod.ModType.GREEN)); + inv.addItem(3, new SWItem(Message.parse("MOD_PENDING",p), 11),click -> playerFilterType.replace(p, Mod.ModType.YELLOW)); + inv.addItem(4, new SWItem(Message.parse("MOD_FORBIDDEN",p),1), click -> playerFilterType.replace(p, Mod.ModType.RED)); + inv.addItem(5, new SWItem(Message.parse("MOD_YT",p),13), click -> playerFilterType.replace(p, Mod.ModType.YOUTUBER_ONLY)); inv.addItem(8, new SWItem("ARROW", Message.parse("MOD_ITEM_BACK",p)), click -> { inv.close(); @@ -86,106 +86,63 @@ public class ModCommand extends SWCommand { inv.open(); } - private SWItem getModItem(ModEntry modEntry) { - SWItem item = new SWItem("NAME_TAG", modEntry.modName); + private void openClassificationGui(ProxiedPlayer p,Mod element) { + SWInventory swInventory = new SWInventory(p,9,Message.parse("MOD_COMMAND_CLASSICIATION_GUI",p)); - item.addLore(modEntry.platform.name()); + String modName = element.getModName(); + Mod.Platform modPlatform = element.getPlatform(); + + swInventory.addItem(2, new SWItem(Message.parse("MOD_UNCLASSIFIED",p),8), (click1 -> updateAndCloseGui(Mod.ModType.UNKLASSIFIED,modName,modPlatform,swInventory,p))); + swInventory.addItem(3, new SWItem(Message.parse("MOD_ALLOWED",p), 2), (click1 -> updateAndCloseGui(Mod.ModType.GREEN,modName,modPlatform,swInventory,p))); + swInventory.addItem(4, new SWItem(Message.parse("MOD_PENDING",p), 11), (click1 -> updateAndCloseGui(Mod.ModType.YELLOW,modName,modPlatform,swInventory,p))); + swInventory.addItem(5, new SWItem(Message.parse("MOD_FORBIDDEN",p),1), (click1 -> updateAndCloseGui(Mod.ModType.RED,modName,modPlatform,swInventory,p))); + swInventory.addItem(6, new SWItem(Message.parse("MOD_YT",p), 13), (click1 -> updateAndCloseGui(Mod.ModType.YOUTUBER_ONLY,modName,modPlatform,swInventory,p))); + + swInventory.addItem(8,new SWItem("ARROW",Message.parse("MOD_ITEM_BACK",p)), click1 -> { + swInventory.close(); + openGui(p); + }); + + swInventory.open(); + } + + private SWItem getModItem(Mod modEntry) { + SWItem item = new SWItem("NAME_TAG", modEntry.getModName()); + + item.addLore(modEntry.getPlatform().name()); return item; } - public List getAllMods(int page,int elementsPerPage) { - return Mod.getAll.select(rs -> { - List f = new ArrayList<>(); - while(rs.next()){ - ModEntry entry = new ModEntry(rs.getString("ModName"), Mod.Platform.getByValue(rs.getInt("Platform"))); - f.add(entry); - } - return f; - }, page * elementsPerPage, elementsPerPage); - } - - public List getAllModsFiltered(int page,int elementsPerPage, int filter) { - return Mod.getAllFiltered.select(rs -> { - List f = new ArrayList<>(); - while(rs.next()){ - ModEntry entry = new ModEntry(rs.getString("ModName"), Mod.Platform.getByValue(rs.getInt("Platform"))); - f.add(entry); - } - return f; - },filter, page * elementsPerPage, elementsPerPage); - } - @Register(value = {"set"},description = "MOD_COMMAND_SET_USAGE") public void set(ProxiedPlayer p,String modName,Mod.Platform platform,Mod.ModType newModType) { - int modPlatform = platform.get(); - boolean modExists = Mod.get.select(ResultSet::next,modName,modPlatform); + boolean modExists = Mod.doesModExist(modName,platform); if(!modExists) { Message.send("MOD_COMMAND_NOT_FOUND_IN_DATABASE",p,modName); return; } - Mod.set.update(newModType.getValue(),modName,modPlatform); + Mod.setMod(newModType,modName,platform); - Message.send("MOD_CHANGED_TYPE",p,modName,newModType.name(),newModType); + Message.send("MOD_CHANGED_TYPE",p,modName,platform.name(),newModType.name()); } @Register(value = {"get"},description = "MOD_COMMAND_GET_USAGE") public void get(ProxiedPlayer p,String modName,Mod.Platform platform) { - Message.send("MOD_COMMAND_INFO",p,modName,platform.name(),Mod.ModType.valueOf(getModType(modName,platform.get())).name()); - } - - private int getModType(String modName,int modPlatform) { - return Mod.get.select(rs -> { - if(rs.next()) { - return rs.getInt("ModType"); - } - return 0; - },modName,modPlatform); + Message.send("MOD_COMMAND_INFO",p,modName,platform.name(),Mod.getModType(modName,platform).name()); } @Register(value = {"next"}) public void next(ProxiedPlayer p) { - Pair foundMod = Mod.findFirst.select(rs -> { - if(rs.next()) { - String name = rs.getString("ModName"); - int platform = rs.getInt("Platform"); - return new ImmutablePair<>(name,platform); - } - return null; - }); + Pair foundMod = Mod.findFirstMod(); + Mod mod = Mod.get(foundMod.getKey(),foundMod.getValue()); if(foundMod == null) { Message.send("MOD_NO_MORE_UNCLASSIFIED_MODS",p); return; } - Message.send("MOD_FOUND_NEXT_MOD",p,"MOD_OPEN_GUI",new ClickEvent(ClickEvent.Action.RUN_COMMAND,""),foundMod.getKey(),Mod.Platform.getByValue(foundMod.getValue())); - } - - private enum FilterType { - ALL(-1), - UNCLASSIFIED(0), - ALLOWED(1), - PENDING(2), - FORBIDDEN(3), - YT(4); - - final int value; - - FilterType(int value) { - this.value = value; - } - } - - private class ModEntry { - private final String modName; - private final Mod.Platform platform; - - public ModEntry(String modName, Mod.Platform platform) { - this.modName = modName; - this.platform = platform; - } + Message.send("MOD_FOUND_NEXT_MOD",p,"MOD_OPEN_GUI",new ClickEvent(ClickEvent.Action.RUN_COMMAND,""),mod.getModName(),mod.getPlatform().name()); } } diff --git a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java index 44bba9fe..491b0622 100644 --- a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java +++ b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java @@ -27,6 +27,7 @@ import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.util.DiscordRanks; import de.steamwar.bungeecore.commands.ChallengeCommand; import de.steamwar.bungeecore.commands.CheckCommand; +import de.steamwar.bungeecore.commands.ModCommand; import de.steamwar.bungeecore.commands.MsgCommand; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.UserGroup; @@ -135,6 +136,7 @@ public class ConnectionListener extends BasicListener { public void onDisconnect(PlayerDisconnectEvent e){ ChallengeCommand.remove(e.getPlayer()); MsgCommand.remove(e.getPlayer()); + ModCommand.playerFilterType.remove(e.getPlayer()); } @EventHandler diff --git a/src/de/steamwar/bungeecore/sql/Mod.java b/src/de/steamwar/bungeecore/sql/Mod.java index 1695f15f..7f9a1d4b 100644 --- a/src/de/steamwar/bungeecore/sql/Mod.java +++ b/src/de/steamwar/bungeecore/sql/Mod.java @@ -20,18 +20,24 @@ package de.steamwar.bungeecore.sql; import lombok.Getter; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; public class Mod { - public static final Statement get = new Statement("SELECT * FROM Mods WHERE ModName = ? AND Platform = ?"); + private static final Statement get = new Statement("SELECT * FROM Mods WHERE ModName = ? AND Platform = ?"); private static final Statement insert = new Statement("INSERT INTO Mods (ModName, Platform) VALUES (?, ?)"); - public static final Statement set = new Statement("UPDATE Mods Set ModType = ? WHERE ModName = ? AND Platform = ?"); - public static final Statement findFirst = new Statement("SELECT * FROM Mods WHERE ModType = 0 LIMIT 1"); + private static final Statement set = new Statement("UPDATE Mods Set ModType = ? WHERE ModName = ? AND Platform = ?"); + private static final Statement findFirst = new Statement("SELECT * FROM Mods WHERE ModType = 0 LIMIT 1"); - public static final Statement getAll = new Statement("SELECT * FROM Mods ORDER BY ModName DESC LIMIT ?, ?"); + private static final Statement getAll = new Statement("SELECT * FROM Mods ORDER BY ModName DESC LIMIT ?, ?"); - public static final Statement getAllFiltered = new Statement("SELECT * FROM Mods WHERE ModType = ? ORDER BY ModName DESC LIMIT ?, ?"); + private static final Statement getAllFiltered = new Statement("SELECT * FROM Mods WHERE ModType = ? ORDER BY ModName DESC LIMIT ?, ?"); private final String modName; private final Platform platform; @@ -56,6 +62,53 @@ public class Mod { return new Mod(modName, platform, ModType.UNKLASSIFIED); } + public static Mod get(String modName, int platform) { + return get(modName,Mod.Platform.valueOf(platform)); + } + + public static ModType getModType(String modName,Mod.Platform platform) { + return get(modName,platform).modType; + } + + public static void setMod(Mod.ModType newModType,String modName,Mod.Platform platform) { + set.update(newModType.value ,modName,platform.value); + } + + public static boolean doesModExist(String modName,Mod.Platform modPlatform) { + return get.select(ResultSet::next,modName,modPlatform.value); + } + + public static List getAll(int page, int elementsPerPage) { + return Mod.getAll.select(rs -> { + List f = new ArrayList<>(); + while(rs.next()){ + Mod entry = new Mod(rs.getString("ModName"), Mod.Platform.valueOf(rs.getInt("Platform")),Mod.ModType.valueOf(rs.getInt("ModType"))); + f.add(entry); + } + return f; + },page * elementsPerPage, elementsPerPage); + } + + public static List getAllModsFiltered(int page,int elementsPerPage, Mod.ModType filter) { + return Mod.getAllFiltered.select(rs -> { + List f = new ArrayList<>(); + while(rs.next()){ + Mod entry = new Mod(rs.getString("ModName"), Mod.Platform.valueOf(rs.getInt("Platform")),Mod.ModType.valueOf(rs.getInt("ModType"))); + f.add(entry); + } + return f; + },filter.value, page * elementsPerPage, elementsPerPage); + } + + public static Pair findFirstMod() { + findFirst.select(rs -> { + String name = rs.getString("ModName"); + int platform = rs.getInt("Platform"); + return new ImmutablePair<>(name,platform); + }); + return null; + } + public String getModName() { return modName; } @@ -76,19 +129,18 @@ public class Mod { Platform(int value){ this.value = value; } + + static Platform valueOf(int value){ + for(Platform mt : values()){ + if(value == mt.value) + return mt; + } + throw new EnumConstantNotPresentException(Platform.class, Integer.toString(value)); + } int value; public int get() { return value; } - - public static Platform getByValue(int val) { - switch (val) { - case 0 : return FORGE; - case 1: return LABYMOD; - case 2: return FABRIC; - default: return null; - } - } } public enum ModType { @@ -98,7 +150,7 @@ public class Mod { RED(3), YOUTUBER_ONLY(4); - public static ModType valueOf(int value){ + static ModType valueOf(int value){ for(ModType mt : values()){ if(value == mt.value) return mt; @@ -109,7 +161,7 @@ public class Mod { ModType(int value){ this.value = value; } - @Getter + int value; } } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 1dc88dc0..5fa66bd0 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -662,11 +662,11 @@ MOD_COMMAND_SET_USAGE=§7/mod set [mod name] [platform] [ModType 1-4] MOD_COMMAND_GET_USAGE=§7/mod get [mod name] [platform] MOD_CHANGED_TYPE=§7Successfully reclassified mod {0} on platform {1} to type {2}! MOD_NO_MORE_UNCLASSIFIED_MODS=§7No more unclassified mods found in databank! -MOD_FOUND_NEXT_MOD=§7Next unclassified mod is {0}! +MOD_FOUND_NEXT_MOD=§7Next unclassified mod is {0} on platform {1}! MOD_COMMAND_NOT_FOUND_IN_DATABASE=§7The Mod {0} on platform {1} was§c not §7found in the database! MOD_COMMAND_INFO=§7The mod {0} on platform {1} is of the type {2}. -MOD_COMMAND_GUI_TITLE=§7Unclassified Mods -MOD_COMMAND_GUI=§7Mod Type Changer +MOD_COMMAND_GUI_TITLE=Unclassified Mods +MOD_COMMAND_CLASSICIATION_GUI=Mod Type Changer MOD_OPEN_GUI=§7Open Gui MOD_UNCLASSIFIED=Unclassified MOD_ALLOWED=Allowed