2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.menu;
|
2020-11-10 04:08:37 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.cache.PlayerCache;
|
|
|
|
import dev.plex.player.PunishedPlayer;
|
|
|
|
import dev.plex.util.menu.AbstractMenu;
|
2022-01-27 21:23:01 +00:00
|
|
|
import java.util.List;
|
2020-11-10 04:08:37 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
public class PunishmentMenu extends AbstractMenu
|
|
|
|
{
|
|
|
|
|
|
|
|
private List<Inventory> inventories = Lists.newArrayList();
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
public PunishmentMenu()
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
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);
|
2022-01-30 21:03:47 +00:00
|
|
|
|
2020-11-10 04:08:37 +00:00
|
|
|
ItemStack previousPage = new ItemStack(Material.FEATHER);
|
|
|
|
ItemMeta meta2 = previousPage.getItemMeta();
|
|
|
|
meta2.setDisplayName(ChatColor.LIGHT_PURPLE + "Previous Page");
|
|
|
|
previousPage.setItemMeta(meta2);
|
2022-01-30 21:03:47 +00:00
|
|
|
|
2020-11-10 04:08:37 +00:00
|
|
|
inventory.setItem(50, nextPage);
|
|
|
|
inventory.setItem(48, previousPage);
|
|
|
|
inventories.add(inventory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
public List<Inventory> getInventory()
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
return inventories;
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
public void openInv(Player player, int index)
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
int currentItemIndex = 0;
|
|
|
|
int currentInvIndex = 0;
|
|
|
|
for (Player players : Bukkit.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
Inventory inv = inventories.get(currentInvIndex);
|
|
|
|
if (currentInvIndex > inventories.size() - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
if (currentItemIndex == inv.getSize() - 1)
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
currentInvIndex++;
|
|
|
|
currentItemIndex = 0;
|
|
|
|
inv = inventories.get(currentInvIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ItemStack item = new ItemStack(Material.PLAYER_HEAD);
|
2022-01-30 21:03:47 +00:00
|
|
|
SkullMeta meta = (SkullMeta)item.getItemMeta();
|
2020-11-10 04:08:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
if (event.getClickedInventory() == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-11-10 04:08:37 +00:00
|
|
|
Inventory inv = event.getClickedInventory();
|
2022-01-30 21:03:47 +00:00
|
|
|
if (!isValidInventory(inv))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.getCurrentItem() == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-11-10 04:08:37 +00:00
|
|
|
ItemStack item = event.getCurrentItem();
|
|
|
|
event.setCancelled(true);
|
|
|
|
if (item.getType() == Material.FEATHER)
|
|
|
|
{
|
|
|
|
if (item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "Next Page"))
|
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
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"))
|
2020-11-10 04:08:37 +00:00
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
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);
|
2020-11-10 04:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
else if (item.getType() == Material.PLAYER_HEAD)
|
2020-11-10 04:08:37 +00:00
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
SkullMeta meta = (SkullMeta)item.getItemMeta();
|
2020-11-10 04:08:37 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-01-30 21:03:47 +00:00
|
|
|
new PunishedPlayerMenu(punishedPlayer).openInv((Player)event.getWhoClicked(), 0);
|
2020-11-10 04:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|