From 093b8f250d5609dd3bcf94b1bb9fe720b3977126 Mon Sep 17 00:00:00 2001 From: TheTrouper <93684527+thetrouper@users.noreply.github.com> Date: Thu, 29 Feb 2024 15:52:41 -0600 Subject: [PATCH] Tried to fix the GUI --- .../ultradupe/cmds/UltraDupeCommand.java | 22 ++ .../ultradupe/data/GUIs/DupeBanGUI.java | 120 ++++++-- .../trouper/ultradupe/data/GUIs/GuiItems.java | 15 +- .../ultradupe/server/sound/SoundPlayer.java | 263 ------------------ .../ultradupe/server/util/CipherUtils.java | 2 +- .../ultradupe/server/util/Randomizer.java | 78 ------ 6 files changed, 125 insertions(+), 375 deletions(-) delete mode 100644 src/main/java/me/trouper/ultradupe/server/sound/SoundPlayer.java delete mode 100644 src/main/java/me/trouper/ultradupe/server/util/Randomizer.java diff --git a/src/main/java/me/trouper/ultradupe/cmds/UltraDupeCommand.java b/src/main/java/me/trouper/ultradupe/cmds/UltraDupeCommand.java index 1694c32..701fc91 100644 --- a/src/main/java/me/trouper/ultradupe/cmds/UltraDupeCommand.java +++ b/src/main/java/me/trouper/ultradupe/cmds/UltraDupeCommand.java @@ -39,6 +39,13 @@ public class UltraDupeCommand implements CustomCommand { default -> DupeBansCommand.handleListBans(p); } } + case "debug" -> { + switch (args.get(1).toString()) { + case "bans" -> { + populateBannedMaterials(); + } + } + } case "toggle" -> { switch (args.get(1).toString()) { case "debug" -> { @@ -143,4 +150,19 @@ private void handleItemEdit(Player p, Args args) { } } } + + private static void populateBannedMaterials() { + int numberOfMaterials = 100; + + for (int i = 0; i < numberOfMaterials; i++) { + Material randomMaterial = getRandomMaterial(); + UltraDupe.dupeBanStorage.bannedMaterials.add(randomMaterial); + } + } + + private static Material getRandomMaterial() { + Material[] materials = Material.values(); + int randomIndex = (int) (Math.random() * materials.length); + return materials[randomIndex]; + } } diff --git a/src/main/java/me/trouper/ultradupe/data/GUIs/DupeBanGUI.java b/src/main/java/me/trouper/ultradupe/data/GUIs/DupeBanGUI.java index 23acac4..cd03d38 100644 --- a/src/main/java/me/trouper/ultradupe/data/GUIs/DupeBanGUI.java +++ b/src/main/java/me/trouper/ultradupe/data/GUIs/DupeBanGUI.java @@ -5,9 +5,12 @@ import io.github.itzispyder.pdk.plugin.builders.ItemBuilder; import io.github.itzispyder.pdk.plugin.gui.CustomGui; import io.github.itzispyder.pdk.utils.misc.SoundPlayer; import me.trouper.ultradupe.UltraDupe; +import me.trouper.ultradupe.server.util.ServerUtils; import me.trouper.ultradupe.server.util.Text; +import net.kyori.adventure.text.Component; import org.bukkit.Material; import org.bukkit.Sound; +import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; @@ -36,45 +39,53 @@ public class DupeBanGUI extends CustomGui implements Global { .size(54) .defineMain(DupeBanGUI::handleMainClick) .onDefine(DupeBanGUI::handleDefine) - .define(45, GuiItems.BACK_ARROW, event -> event.getWhoClicked().sendMessage(Text.prefix("You clicked back"))) - .define(53, GuiItems.NEXT_ARROW, event -> event.getWhoClicked().sendMessage(Text.prefix("You clicked next"))) + .define(45, GuiItems.BACK_ARROW, event -> { + event.getWhoClicked().sendMessage(Text.prefix("You clicked back on %s".formatted(event.getClickedInventory()))); + decrementPage(event.getClickedInventory()); + }) + .define(53, GuiItems.NEXT_ARROW, event -> { + event.getWhoClicked().sendMessage(Text.prefix("You clicked next on %s".formatted(event.getClickedInventory()))); + incrementPage(event.getClickedInventory()); + }) .define(49, GuiItems.INFO_ICON, event -> {}) .build(); private static void handleMainClick(InventoryClickEvent e) { + Player who = (Player) e.getWhoClicked(); + SoundPlayer deny = new SoundPlayer(e.getWhoClicked().getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1); SoundPlayer allow = new SoundPlayer(e.getWhoClicked().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2); e.setCancelled(true); - Player who = (Player) e.getWhoClicked(); + if (!isInGUI.contains(e.getWhoClicked().getUniqueId())) { + deny.play(who); + e.getWhoClicked().setHealth(0); + return; + } + int where = e.getSlot(); + ItemStack what = e.getClickedInventory().getItem(where); + ItemStack with = e.getCursor(); + ItemStack current = e.getCurrentItem(); + + if (!with.isEmpty()) { + //who.sendMessage(Text.prefix("You clicked %s with %s at %s").formatted(current,with,where)); + allow.play(who); + UltraDupe.dupeBanStorage.bannedMaterials.add(with.getType()); + who.sendMessage(Text.prefix("You have &cadded&7 the material &e%s&7 from the dupe bans.".formatted(Text.cleanName(with.getType().toString())))); + who.closeInventory(); + refreshGUI(who); + isInGUI.add(who.getUniqueId()); + UltraDupe.dupeBanStorage.save(); + return; + } if (e.getClickedInventory().getItem(where) == null || e.getClickedInventory().getItem(where).isEmpty()) { deny.play(who); return; } - ItemStack what = e.getClickedInventory().getItem(where); - - if (!isInGUI.contains(e.getWhoClicked().getUniqueId())) { - e.setCancelled(true); - deny.play(who); - e.getWhoClicked().setHealth(0); - return; - } - - if (!e.getCursor().isEmpty()) { - allow.play(who); - UltraDupe.dupeBanStorage.bannedMaterials.add(e.getCursor().getType()); - who.sendMessage(Text.prefix("You have &cadded&7 the material &e%s&7 from the dupe bans.".formatted(Text.cleanName(what.getType().toString())))); - who.closeInventory(); - refreshGUI(who); - isInGUI.add(who.getUniqueId()); - UltraDupe.dupeBanStorage.save(); - } - if ((e.getSlot() > 44 && e.getSlot() < 54) && what.getType().equals(Material.LIGHT_GRAY_STAINED_GLASS_PANE)) { - e.setCancelled(true); deny.play(who); return; } @@ -90,10 +101,52 @@ public class DupeBanGUI extends CustomGui implements Global { } } + public static void incrementPage(Inventory inv) { + ItemStack info = inv.getItem(49); + int pageNumber = info.getItemMeta().getCustomModelData() + 1; + ServerUtils.verbose("Incrementing to Page: %s".formatted(pageNumber)); + + inv.getItem(49).getItemMeta().setCustomModelData(pageNumber); + info.getItemMeta().lore().set(3, Component.text(g.color("&7Page &9%s&7/&b%s".formatted(pageNumber,getMaxPages())))); + + updatePage(inv); + } + + public static void decrementPage(Inventory inv) { + ItemStack info = inv.getItem(49); + int pageNumber = info.getItemMeta().getCustomModelData() - 1; + ServerUtils.verbose("Decrementing to Page: %s".formatted(pageNumber)); + + inv.getItem(49).getItemMeta().setCustomModelData(pageNumber); + info.getItemMeta().lore().set(3, Component.text(g.color("&7Page &9%s&7/&b%s".formatted(pageNumber,getMaxPages())))); + + updatePage(inv); + } + + public static void updatePage(Inventory inv) { + ItemStack info = inv.getItem(49); + int pageNumber = info.getItemMeta().getCustomModelData(); + ServerUtils.verbose("Updating Page: %s".formatted(pageNumber)); + + inv.getItem(49).getItemMeta().setCustomModelData(pageNumber); + info.getItemMeta().lore().set(3, Component.text(g.color("&7Page &9%s&7/&b%s".formatted(pageNumber,getMaxPages())))); + + setupPage(inv,pageNumber); + } + private static void handleDefine(Inventory inv) { + setupPage(inv,1); + } + + private static void setupPage(Inventory inv, int pageNumber) { + int pageSize = 46; + int startIndex = (pageNumber - 1) * pageSize; + int endIndex = Math.min(startIndex + pageSize, UltraDupe.dupeBanStorage.bannedMaterials.size()); + int pointer = 0; - for (Material bannedMaterial : UltraDupe.dupeBanStorage.bannedMaterials) { - if (pointer > 44) return; + + for (int i = startIndex; i < endIndex; i++) { + Material bannedMaterial = UltraDupe.dupeBanStorage.bannedMaterials.get(i); inv.setItem(pointer, ItemBuilder.create() .material(bannedMaterial) .lore("") @@ -101,12 +154,25 @@ public class DupeBanGUI extends CustomGui implements Global { .build()); pointer++; } + for (int i = 45; i < 53; i++) { inv.setItem(i,GuiItems.BLANK); } + + ItemStack info = GuiItems.INFO_ICON; + info.getItemMeta().lore().set(3, Component.text(g.color("&7Page &9%s&7/&b%s".formatted(pageNumber,getMaxPages())))); + info.getItemMeta().setCustomModelData(pageNumber); + + inv.setItem(45,GuiItems.BACK_ARROW); + inv.setItem(49,info); + inv.setItem(53,GuiItems.NEXT_ARROW); + } + + private static int getMaxPages() { + int pageSize = 46; + int totalMaterials = UltraDupe.dupeBanStorage.bannedMaterials.size(); + return (int) Math.ceil((double) totalMaterials / pageSize); } - - } diff --git a/src/main/java/me/trouper/ultradupe/data/GUIs/GuiItems.java b/src/main/java/me/trouper/ultradupe/data/GUIs/GuiItems.java index d45af8a..8346aff 100644 --- a/src/main/java/me/trouper/ultradupe/data/GUIs/GuiItems.java +++ b/src/main/java/me/trouper/ultradupe/data/GUIs/GuiItems.java @@ -10,22 +10,25 @@ public class GuiItems implements Global { public static final ItemStack NEXT_ARROW = ItemBuilder.create() .material(Material.ARROW) - .name(g.color("Next Page")) + .name(g.color("&bNext Page &7➔")) .build(); public static final ItemStack BACK_ARROW = ItemBuilder.create() .material(Material.ARROW) - .name(g.color("Previous Page")) + .name(g.color("&7\uD83E\uDC78 &bPrevious Page")) .build(); public static final ItemStack INFO_ICON = ItemBuilder.create() .material(Material.NAME_TAG) - .name(g.color("Info")) - .lore(g.color("Right click an item in your inventory to add it")) - .lore(g.color("Left click any item to remove it")) + .name(g.color("&b&lInfo")) + .lore(g.color("&3➥ &7(Drag-n-drop) &fAdds the item")) + .lore(g.color("&3➥ &7(Left-Click) &fRemoves the item")) + .lore(g.color("")) + .lore(g.color("Page 1/1")) + .customModelData(1) .build(); public static final ItemStack BLANK = ItemBuilder.create() .material(Material.LIGHT_GRAY_STAINED_GLASS_PANE) - .name("") + .name(g.color("&7")) .build(); } diff --git a/src/main/java/me/trouper/ultradupe/server/sound/SoundPlayer.java b/src/main/java/me/trouper/ultradupe/server/sound/SoundPlayer.java deleted file mode 100644 index c30b7df..0000000 --- a/src/main/java/me/trouper/ultradupe/server/sound/SoundPlayer.java +++ /dev/null @@ -1,263 +0,0 @@ -/** - * This file is for tutorial purposes made by ImproperIssues. Distribute if you want :) - * - * I made this cuz Bukkit API sounds management is trash. - * by ImproperIssues - */ - - -package me.trouper.ultradupe.server.sound; - -import me.trouper.ultradupe.UltraDupe; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; - -public class SoundPlayer { - - private Location location; - private Sound sound; - private float volume; - private float pitch; - - /** - * Constructs a new sound, this aims to add more methods to - * the Bukkit APIs Sound class, as they don't have many - * methods to use. - * - * @param location Location - * @param sound Sound - * @param volume float - * @param pitch float - */ - public SoundPlayer(Location location, Sound sound, float volume, float pitch) { - this.location = location; - this.sound = sound; - this.pitch = pitch; - this.volume = volume; - } - - - /** - * Plays a sound to a player but at the store location - * - * @param player Player - */ - public void play(Player player) { - player.playSound(this.location,this.sound,this.volume,this.pitch); - } - - /** - * Plays a sound to a player but at the player's location - * - * @param player Player - */ - public void playAt(Player player) { - player.playSound(player.getLocation(),this.sound,this.volume,this.pitch); - } - - /** - * Plays the sound to all players within a distance, but at the stored location. - * - * @param distance double - */ - public void playWithin(double distance) { - for (Player p : Bukkit.getOnlinePlayers()) { - if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) { - p.playSound(this.location,this.sound,this.volume,this.pitch); - } - } - } - - /** - * Plays the sound to all players within a distance, but at the players' location. - * - * @param distance double - */ - public void playWithinAt(double distance) { - for (Player p : Bukkit.getOnlinePlayers()) { - if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) { - p.playSound(p.getLocation(),this.sound,this.volume,this.pitch); - } - } - } - - - /** - * Plays the sound to all players on the server, but at the stored location. - */ - public void playAll() { - for (Player p : Bukkit.getOnlinePlayers()) p.playSound(this.location,this.sound,this.volume,this.pitch); - } - - /** - * Plays the sound to all players on the server, but at the players' location. - */ - public void playAllAt() { - for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(),this.sound,this.volume,this.pitch); - } - - /** - * Repeats a sound to a player, but at the stored location. - * - * @param player Player - * @param times int - * @param tickDelay int - */ - public void repeat(Player player, int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - play(player); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - /** - * Repeats a sound to a player, but at the player's location. - * - * @param player Player - * @param times int - * @param tickDelay int - */ - public void repeatAt(Player player, int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - playAt(player); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - /** - * Repeats a sound to all players on the server, but at the stored location. - * - * @param times int - * @param tickDelay int - */ - public void repeatAll(int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - playAll(); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - /** - * Repeats a sound to all players on the server, but at the players' location. - * - * @param times int - * @param tickDelay int - */ - public void repeatAllAt(int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - playAllAt(); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - /** - * Repeats a sound to all players within a radius, but at the stored location. - * - * @param radius double - * @param times int - * @param tickDelay int - */ - public void repeatAll(double radius,int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - playWithin(radius); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - /** - * Repeats a sound to all players within a radius, but at the players' location. - * - * @param distance double - * @param times int - * @param tickDelay int - */ - public void repeatAllAt(double distance, int times, int tickDelay) { - new BukkitRunnable() { - int i = 0; - @Override - public void run() { - if (i < times) { - playWithinAt(distance); - i ++; - } else { - this.cancel(); - } - } - }.runTaskTimer(UltraDupe.getInstance(),0,tickDelay); - } - - public Sound getSound() { - return sound; - } - - public float getPitch() { - return pitch; - } - - public float getVolume() { - return volume; - } - - public Location getLocation() { - return location; - } - - public void setPitch(float pitch) { - this.pitch = pitch; - } - - public void setVolume(float volume) { - this.volume = volume; - } - - public void setSound(Sound sound) { - this.sound = sound; - } - - public void setLocation(Location location) { - this.location = location; - } -} diff --git a/src/main/java/me/trouper/ultradupe/server/util/CipherUtils.java b/src/main/java/me/trouper/ultradupe/server/util/CipherUtils.java index 7d238d2..6e737e3 100644 --- a/src/main/java/me/trouper/ultradupe/server/util/CipherUtils.java +++ b/src/main/java/me/trouper/ultradupe/server/util/CipherUtils.java @@ -5,7 +5,7 @@ import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class CipherUtils { - private static final String secretKey = "GG8T885O4Yd/86OMVFdL0w=="; // 16, 24, or 32 bytes + private static final String secretKey = "aYN8iuz1kMHU2af8iMv7UY0HTmiqr2yqserThqQQufNO8E9jBMFdgAbo5deLVM7B"; // 16, 24, or 32 bytes private static final String algorithm = "AES"; public static String encrypt(String strToEncrypt) { try { diff --git a/src/main/java/me/trouper/ultradupe/server/util/Randomizer.java b/src/main/java/me/trouper/ultradupe/server/util/Randomizer.java deleted file mode 100644 index b9b6c90..0000000 --- a/src/main/java/me/trouper/ultradupe/server/util/Randomizer.java +++ /dev/null @@ -1,78 +0,0 @@ -package me.trouper.ultradupe.server.util; - -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Set; - -/** - * Randomize items from a list - * @param list of? - */ -public class Randomizer { - public static long generateID() { - Date now = new Date(); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS"); - String formattedDate = dateFormat.format(now); - long id = Long.parseLong(formattedDate); - - return id; - } - - private final List array; - - /** - * From array list - * @param array list - */ - public Randomizer(List array) { - this.array = array; - } - - /** - * From set - * @param array set - */ - public Randomizer(Set array) { - this.array = new ArrayList<>(array); - } - - /** - * From array - * @param array array - */ - public Randomizer(T[] array) { - this.array = List.of(array); - } - - /** - * Pick random from the array - * @return random of list of? - */ - public T pickRand() { - return array.get(rand(array.size() - 1)); - } - - /** - * Generates a random integer from 1 to (max) - * @param max max value - * @return random - */ - public static int rand(int max) { - if (max <= 0) throw new IllegalArgumentException("max cannot be less than 1!"); - return (int) Math.ceil(Math.random() * max); - } - - /** - * Generates a random integer from (min) to (max) - * @param min min value - * @param max max value - * @return random - */ - public static int rand(int min, int max) { - if (max <= 0 || min <= 0) throw new IllegalArgumentException("max or min cannot be less than 1!"); - if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!"); - return min + (int) Math.floor(Math.random() * (max - min + 1)); - } -}