From 8b94ef3789522a9885aa3210500d62e117a54ade Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Mon, 9 Nov 2020 20:08:37 -0800 Subject: [PATCH] Finish the god forsaken punishments gui --- .../totalfreedom/plex/banning/BanManager.java | 2 +- .../plex/command/impl/PunishmentsCMD.java | 36 ++++ .../plex/handlers/CommandHandler.java | 1 + .../plex/menu/PunishedPlayerMenu.java | 154 ++++++++++++++++++ .../plex/menu/PunishmentMenu.java | 142 ++++++++++++++++ .../plex/player/PunishedPlayer.java | 68 ++++++++ .../plex/punishment/PunishmentManager.java | 17 +- .../plex/util/menu/AbstractMenu.java | 20 +++ .../me/totalfreedom/plex/util/menu/IMenu.java | 20 +++ 9 files changed, 443 insertions(+), 17 deletions(-) create mode 100644 src/main/java/me/totalfreedom/plex/command/impl/PunishmentsCMD.java create mode 100644 src/main/java/me/totalfreedom/plex/menu/PunishedPlayerMenu.java create mode 100644 src/main/java/me/totalfreedom/plex/menu/PunishmentMenu.java create mode 100644 src/main/java/me/totalfreedom/plex/util/menu/AbstractMenu.java create mode 100644 src/main/java/me/totalfreedom/plex/util/menu/IMenu.java diff --git a/src/main/java/me/totalfreedom/plex/banning/BanManager.java b/src/main/java/me/totalfreedom/plex/banning/BanManager.java index b381ee1..ce96c33 100644 --- a/src/main/java/me/totalfreedom/plex/banning/BanManager.java +++ b/src/main/java/me/totalfreedom/plex/banning/BanManager.java @@ -20,7 +20,7 @@ import java.util.UUID; public class BanManager { private final String SELECT = "SELECT * FROM `bans` WHERE uuid=?"; - private final String INSERT = "INSERT INTO `bans` (`banID`, `uuid`, `banner`, `ip`, ``reason`, `enddate`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?);"; + private final String INSERT = "INSERT INTO `bans` (`banID`, `uuid`, `banner`, `ip`, `reason`, `enddate`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?);"; public void executeBan(Ban ban) { diff --git a/src/main/java/me/totalfreedom/plex/command/impl/PunishmentsCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/PunishmentsCMD.java new file mode 100644 index 0000000..d5368c8 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/command/impl/PunishmentsCMD.java @@ -0,0 +1,36 @@ +package me.totalfreedom.plex.command.impl; + +import com.google.common.collect.ImmutableList; +import me.totalfreedom.plex.command.PlexCommand; +import me.totalfreedom.plex.command.annotation.CommandParameters; +import me.totalfreedom.plex.command.annotation.CommandPermissions; +import me.totalfreedom.plex.command.source.CommandSource; +import me.totalfreedom.plex.command.source.RequiredCommandSource; +import me.totalfreedom.plex.menu.PunishmentMenu; +import me.totalfreedom.plex.rank.enums.Rank; +import me.totalfreedom.plex.util.PlexUtils; +import org.bukkit.entity.Player; + +import java.util.List; + +@CommandParameters(usage = "/ [player]", description = "Opens the Punishments GUI", aliases = "punishlist,punishes") +@CommandPermissions(level = Rank.ADMIN, source = RequiredCommandSource.IN_GAME) +public class PunishmentsCMD extends PlexCommand +{ + + public PunishmentsCMD() { + super("punishments"); + } + + @Override + public void execute(CommandSource sender, String[] args) + { + Player player = sender.getPlayer(); + new PunishmentMenu().openInv(player, 0); + } + + @Override + public List onTabComplete(CommandSource sender, String[] args) { + return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of(); + } +} diff --git a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java index 8c2ac96..663cba2 100644 --- a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java +++ b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java @@ -26,6 +26,7 @@ public class CommandHandler commands.add(new SurvivalCMD()); commands.add(new SpectatorCMD()); commands.add(new BanCMD()); + commands.add(new PunishmentsCMD()); PlexLog.log(String.format("Registered %s commands!", commands.size())); } } diff --git a/src/main/java/me/totalfreedom/plex/menu/PunishedPlayerMenu.java b/src/main/java/me/totalfreedom/plex/menu/PunishedPlayerMenu.java new file mode 100644 index 0000000..8263053 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/menu/PunishedPlayerMenu.java @@ -0,0 +1,154 @@ +package me.totalfreedom.plex.menu; + +import com.google.common.collect.Lists; +import me.totalfreedom.plex.cache.PlayerCache; +import me.totalfreedom.plex.player.PunishedPlayer; +import me.totalfreedom.plex.punishment.Punishment; +import me.totalfreedom.plex.util.menu.AbstractMenu; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; + +import java.util.Arrays; +import java.util.List; + +public class PunishedPlayerMenu extends AbstractMenu +{ + + private PunishedPlayer punishedPlayer; + + private List inventories = Lists.newArrayList(); + + public PunishedPlayerMenu(PunishedPlayer player) { + super("§c§lPunishments"); + this.punishedPlayer = player; + for (int i = 0; i <= punishedPlayer.getPunishments().size() / 53; i++) + { + Inventory inventory = Bukkit.createInventory(null, 54, "Punishments Page " + (i + 1)); + ItemStack nextPage = new ItemStack(Material.FEATHER); + ItemMeta meta = nextPage.getItemMeta(); + meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Next Page"); + nextPage.setItemMeta(meta); + + ItemStack previousPage = new ItemStack(Material.FEATHER); + ItemMeta meta2 = previousPage.getItemMeta(); + meta2.setDisplayName(ChatColor.LIGHT_PURPLE + "Previous Page"); + previousPage.setItemMeta(meta2); + + ItemStack back = new ItemStack(Material.BARRIER); + ItemMeta meta3 = back.getItemMeta(); + meta3.setDisplayName(ChatColor.RED + "Return"); + back.setItemMeta(meta3); + + inventory.setItem(50, nextPage); + inventory.setItem(49, back); + inventory.setItem(48, previousPage); + inventories.add(inventory); + } + } + + public List getInventory() { + return inventories; + } + + public void openInv(Player player, int index) { + int currentItemIndex = 0; + int currentInvIndex = 0; + for (Punishment punishment : punishedPlayer.getPunishments()) + { + Inventory inv = inventories.get(currentInvIndex); + if (currentInvIndex > inventories.size() - 1) + { + break; + } + + if (currentItemIndex == inv.getSize() - 1) { + currentInvIndex++; + currentItemIndex = 0; + inv = inventories.get(currentInvIndex); + } + + + ItemStack item = new ItemStack(Material.NETHER_STAR); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(ChatColor.RED + punishment.getType().name().toUpperCase()); + meta.setLore(Arrays.asList(ChatColor.YELLOW + "Reason: \n" + ChatColor.GRAY + punishment.getReason())); + item.setItemMeta(meta); + + inv.setItem(currentItemIndex, item); + + currentItemIndex++; + } + player.openInventory(inventories.get(index)); + } + + @EventHandler + public void onClick(InventoryClickEvent event) + { + if (event.getClickedInventory() == null) return; + Inventory inv = event.getClickedInventory(); + if (!isValidInventory(inv)) return; + if (event.getCurrentItem() == null) return; + ItemStack item = event.getCurrentItem(); + event.setCancelled(true); + if (item.getType() == Material.FEATHER) + { + if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Next Page")) + { + if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1) return; + if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0) return; + openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1); + } else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page")) + { + if (getCurrentInventoryIndex(inv) - 1 < 0) return; + if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1) return; + if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0) return; + openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) - 1); + } + + + } else if (item.getType() == Material.BARRIER) + { + new PunishmentMenu().openInv((Player) event.getWhoClicked(), 0); + } else if (item.getType() == Material.PLAYER_HEAD) + { + SkullMeta meta = (SkullMeta) item.getItemMeta(); + OfflinePlayer player = meta.getOwningPlayer(); + assert player != null; + PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId()) == null ? null : PlayerCache.getPunishedPlayer(player.getUniqueId()); + if (punishedPlayer == null) + { + event.getWhoClicked().sendMessage(ChatColor.RED + "This player does not exist. Try doing /punishments instead."); + event.getWhoClicked().closeInventory(); + return; + } + + } + + } + + public int getCurrentInventoryIndex(Inventory inventory) + { + for (int i = 0; i <= inventories.size() - 1; i++) + { + if (inventories.get(i).hashCode() == inventory.hashCode()) + { + return i; + } + } + return 0; + } + + private boolean isValidInventory(Inventory inventory) + { + return inventories.contains(inventory); + } +} diff --git a/src/main/java/me/totalfreedom/plex/menu/PunishmentMenu.java b/src/main/java/me/totalfreedom/plex/menu/PunishmentMenu.java new file mode 100644 index 0000000..4bda9b0 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/menu/PunishmentMenu.java @@ -0,0 +1,142 @@ +package me.totalfreedom.plex.menu; + +import com.google.common.collect.Lists; +import me.totalfreedom.plex.cache.PlayerCache; +import me.totalfreedom.plex.player.PunishedPlayer; +import me.totalfreedom.plex.util.menu.AbstractMenu; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; + +import java.util.List; + +public class PunishmentMenu extends AbstractMenu +{ + + private List inventories = Lists.newArrayList(); + + public PunishmentMenu() { + super("§c§lPunishments"); + for (int i = 0; i <= Bukkit.getOnlinePlayers().size() / 53; i++) + { + Inventory inventory = Bukkit.createInventory(null, 54, "Punishments Page " + (i + 1)); + ItemStack nextPage = new ItemStack(Material.FEATHER); + ItemMeta meta = nextPage.getItemMeta(); + meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Next Page"); + nextPage.setItemMeta(meta); + + ItemStack previousPage = new ItemStack(Material.FEATHER); + ItemMeta meta2 = previousPage.getItemMeta(); + meta2.setDisplayName(ChatColor.LIGHT_PURPLE + "Previous Page"); + previousPage.setItemMeta(meta2); + + inventory.setItem(50, nextPage); + inventory.setItem(48, previousPage); + inventories.add(inventory); + } + } + + public List getInventory() { + return inventories; + } + + public void openInv(Player player, int index) { + int currentItemIndex = 0; + int currentInvIndex = 0; + for (Player players : Bukkit.getOnlinePlayers()) + { + Inventory inv = inventories.get(currentInvIndex); + if (currentInvIndex > inventories.size() - 1) + { + break; + } + + if (currentItemIndex == inv.getSize() - 1) { + currentInvIndex++; + currentItemIndex = 0; + inv = inventories.get(currentInvIndex); + } + + + ItemStack item = new ItemStack(Material.PLAYER_HEAD); + SkullMeta meta = (SkullMeta) item.getItemMeta(); + meta.setOwningPlayer(players); + meta.setDisplayName(ChatColor.YELLOW + players.getName()); + item.setItemMeta(meta); + + inv.setItem(currentItemIndex, item); + + currentItemIndex++; + } + player.openInventory(inventories.get(index)); + } + + @EventHandler + public void onClick(InventoryClickEvent event) + { + if (event.getClickedInventory() == null) return; + Inventory inv = event.getClickedInventory(); + if (!isValidInventory(inv)) return; + if (event.getCurrentItem() == null) return; + ItemStack item = event.getCurrentItem(); + event.setCancelled(true); + if (item.getType() == Material.FEATHER) + { + if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Next Page")) + { + if (getCurrentInventoryIndex(inv) + 1 > inventories.size() - 1) return; + if (inventories.get(getCurrentInventoryIndex(inv) + 1).getContents().length == 0) return; + openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) + 1); + } else if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Previous Page")) + { + if (getCurrentInventoryIndex(inv) - 1 < 0) return; + if (getCurrentInventoryIndex(inv) - 1 > inventories.size() - 1) return; + if (inventories.get(getCurrentInventoryIndex(inv) - 1).getContents().length == 0) return; + openInv((Player) event.getWhoClicked(), getCurrentInventoryIndex(inv) - 1); + } + + + } else if (item.getType() == Material.PLAYER_HEAD) + { + SkullMeta meta = (SkullMeta) item.getItemMeta(); + OfflinePlayer player = meta.getOwningPlayer(); + assert player != null; + PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId()) == null ? null : PlayerCache.getPunishedPlayer(player.getUniqueId()); + if (punishedPlayer == null) + { + event.getWhoClicked().sendMessage(ChatColor.RED + "This player does not exist. Try doing /punishments instead."); + event.getWhoClicked().closeInventory(); + return; + } + new PunishedPlayerMenu(punishedPlayer).openInv((Player) event.getWhoClicked(), 0); + } + + + + } + + public int getCurrentInventoryIndex(Inventory inventory) + { + for (int i = 0; i <= inventories.size() - 1; i++) + { + if (inventories.get(i).hashCode() == inventory.hashCode()) + { + return i; + } + } + return 0; + } + + private boolean isValidInventory(Inventory inventory) + { + return inventories.contains(inventory); + } +} diff --git a/src/main/java/me/totalfreedom/plex/player/PunishedPlayer.java b/src/main/java/me/totalfreedom/plex/player/PunishedPlayer.java index 9daec72..ec5a866 100644 --- a/src/main/java/me/totalfreedom/plex/player/PunishedPlayer.java +++ b/src/main/java/me/totalfreedom/plex/player/PunishedPlayer.java @@ -1,12 +1,27 @@ package me.totalfreedom.plex.player; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.UUID; + +import com.google.common.collect.Lists; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.cache.DataUtils; import me.totalfreedom.plex.event.PunishedPlayerFreezeEvent; import me.totalfreedom.plex.event.PunishedPlayerMuteEvent; +import me.totalfreedom.plex.punishment.Punishment; +import me.totalfreedom.plex.util.PlexLog; +import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; +import org.json.JSONObject; +import org.json.JSONTokener; @Getter public class PunishedPlayer @@ -44,4 +59,57 @@ public class PunishedPlayer this.muted = muted; } } + + public File getPunishmentsFile() + { + File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments"); + if (!folder.exists()) + { + folder.mkdir(); + } + + File file = new File(folder, getUuid() + ".json"); + if (!file.exists()) + { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + PlexLog.log("Created new punishment file for " + getUuid() + " (" + DataUtils.getPlayer(getUuid()).getName() + ")"); + } + return file; + + } + + public List getPunishments() + { + List punishments = Lists.newArrayList(); + + File file = getPunishmentsFile(); + + if (isNotEmpty(file)) + { + try { + JSONTokener tokener = new JSONTokener(new FileInputStream(file)); + JSONObject object = new JSONObject(tokener); + object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj -> { + Punishment punishment = Punishment.fromJson(obj.toString()); + punishments.add(punishment); + }); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + return punishments; + } + + private boolean isNotEmpty(File file) { + try { + return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty(); + } catch (IOException e) { + e.printStackTrace(); + } + return false; + } } diff --git a/src/main/java/me/totalfreedom/plex/punishment/PunishmentManager.java b/src/main/java/me/totalfreedom/plex/punishment/PunishmentManager.java index 69136af..e3568fe 100644 --- a/src/main/java/me/totalfreedom/plex/punishment/PunishmentManager.java +++ b/src/main/java/me/totalfreedom/plex/punishment/PunishmentManager.java @@ -32,22 +32,7 @@ public class PunishmentManager public void insertPunishment(PunishedPlayer player, Punishment punishment) { - File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments"); - if (!folder.exists()) - { - folder.mkdir(); - } - - File file = new File(folder, player.getUuid() + ".json"); - if (!file.exists()) - { - try { - file.createNewFile(); - PlexLog.log("Created new punishment file for " + player.getUuid() + " (" + DataUtils.getPlayer(punishment.getPunished()).getName() + ")"); - } catch (IOException e) { - e.printStackTrace(); - } - } + File file = player.getPunishmentsFile(); try { if (isNotEmpty(file)) diff --git a/src/main/java/me/totalfreedom/plex/util/menu/AbstractMenu.java b/src/main/java/me/totalfreedom/plex/util/menu/AbstractMenu.java new file mode 100644 index 0000000..789cafe --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/util/menu/AbstractMenu.java @@ -0,0 +1,20 @@ +package me.totalfreedom.plex.util.menu; + +import me.totalfreedom.plex.Plex; +import org.bukkit.event.Listener; + +public abstract class AbstractMenu implements Listener +{ + private String name; + + public AbstractMenu(String name) + { + this.name = name; + + Plex.get().getServer().getPluginManager().registerEvents(this, Plex.get()); + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/me/totalfreedom/plex/util/menu/IMenu.java b/src/main/java/me/totalfreedom/plex/util/menu/IMenu.java new file mode 100644 index 0000000..098f531 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/util/menu/IMenu.java @@ -0,0 +1,20 @@ +package me.totalfreedom.plex.util.menu; + + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; + +public interface IMenu +{ + + Inventory getInventory(); + + + void openInv(Player player); + + @EventHandler + void onClick(InventoryClickEvent event); + +}