Finish the god forsaken punishments gui

This commit is contained in:
spacerocket62 2020-11-09 20:08:37 -08:00
parent b072027458
commit 8b94ef3789
9 changed files with 443 additions and 17 deletions

View File

@ -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)
{

View File

@ -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 = "/<command> [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<String> onTabComplete(CommandSource sender, String[] args) {
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -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()));
}
}

View File

@ -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<Inventory> 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<Inventory> 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 <player> 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);
}
}

View File

@ -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<Inventory> 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<Inventory> 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 <player> 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);
}
}

View File

@ -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<Punishment> getPunishments()
{
List<Punishment> 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;
}
}

View File

@ -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))

View File

@ -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;
}
}

View File

@ -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);
}