From abc90629ea1b6a2a639a222f31ad322a4b8befb1 Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 7 Nov 2020 00:45:44 +0100 Subject: [PATCH] Added favorites menu --- pom.xml | 2 +- src/main/java/tsp/headdb/HeadDB.java | 7 +++ src/main/java/tsp/headdb/api/Head.java | 37 +++++++------- src/main/java/tsp/headdb/api/HeadAPI.java | 34 +++++++++++++ .../tsp/headdb/inventory/InventoryUtils.java | 51 +++++++++++++++++-- .../tsp/headdb/listener/MenuListener.java | 13 ++++- src/main/java/tsp/headdb/util/Config.java | 4 ++ src/main/java/tsp/headdb/util/Log.java | 4 ++ src/main/resources/config.yml | 2 +- src/main/resources/plugin.yml | 5 +- 10 files changed, 133 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 00cfa83..fcdb4aa 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 1.1 + 1.2 jar HeadDB diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 5648473..06a8566 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -15,6 +15,7 @@ public class HeadDB extends JavaPlugin { private static HeadDB instance; private static Config config; + private static Config playerdata; @Override public void onEnable() { @@ -22,6 +23,8 @@ public class HeadDB extends JavaPlugin { Log.info("Loading HeadDB - " + getDescription().getVersion()); saveDefaultConfig(); config = new Config("plugins/HeadDB/config.yml"); + playerdata = new Config("plugins/HeadDB/playerdata.yml"); + playerdata.create(); Log.debug("Starting metrics..."); new Metrics(this, Utils.METRICS_ID); @@ -43,6 +46,10 @@ public class HeadDB extends JavaPlugin { return config; } + public static Config getPlayerdata() { + return playerdata; + } + public static HeadDB getInstance() { return instance; } diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java index 1a1a589..f78b15f 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -11,7 +11,7 @@ import tsp.headdb.util.Log; import tsp.headdb.util.Utils; import java.lang.reflect.Field; -import java.util.Collections; +import java.util.Arrays; import java.util.UUID; public class Head { @@ -29,24 +29,25 @@ public class Head { Validate.notNull(category, "category must not be null!"); ItemStack item = new ItemStack(Material.PLAYER_HEAD); - if (item != null) { - SkullMeta meta = (SkullMeta) item.getItemMeta(); - meta.setDisplayName(Utils.colorize(category.getColor() + name)); - // set skull owner - GameProfile profile = new GameProfile(uuid, name); - profile.getProperties().put("textures", new Property("textures", value)); - Field profileField; - try { - profileField = meta.getClass().getDeclaredField("profile"); - profileField.setAccessible(true); - profileField.set(meta, profile); - } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) { - Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:"); - e1.printStackTrace(); - } - meta.setLore(Collections.singletonList(Utils.colorize("&cID: " + id))); - item.setItemMeta(meta); + SkullMeta meta = (SkullMeta) item.getItemMeta(); + meta.setDisplayName(Utils.colorize(category.getColor() + name)); + // set skull owner + GameProfile profile = new GameProfile(uuid, name); + profile.getProperties().put("textures", new Property("textures", value)); + Field profileField; + try { + profileField = meta.getClass().getDeclaredField("profile"); + profileField.setAccessible(true); + profileField.set(meta, profile); + } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) { + Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:"); + e1.printStackTrace(); } + meta.setLore(Arrays.asList( + Utils.colorize("&cID: " + id), + Utils.colorize("&8Right-Click to add/remove from favorites.") + )); + item.setItemMeta(meta); return item; } diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index c0f1860..20d963e 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -1,10 +1,12 @@ package tsp.headdb.api; import org.bukkit.entity.Player; +import tsp.headdb.HeadDB; import tsp.headdb.database.Category; import tsp.headdb.database.HeadDatabase; import tsp.headdb.inventory.InventoryUtils; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -50,6 +52,38 @@ public class HeadAPI { return HeadDatabase.getHeads(); } + public static void addFavoriteHead(UUID uuid, int id) { + List favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + if (!favs.contains(id)) { + favs.add(id); + } + HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); + HeadDB.getPlayerdata().save(); + } + + public static void removeFavoriteHead(UUID uuid, int id) { + List favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + for (int i = 0; i < favs.size(); i++) { + if (favs.get(i) == id) { + favs.remove(i); + break; + } + } + HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); + HeadDB.getPlayerdata().save(); + } + + public static List getFavoriteHeads(UUID uuid) { + List heads = new ArrayList<>(); + List ids = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + for (int id : ids) { + Head head = getHeadByID(id); + heads.add(head); + } + + return heads; + } + public static void updateDatabase() { HeadDatabase.update(); } diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index aaa0225..3277044 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -1,6 +1,7 @@ package tsp.headdb.inventory; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; @@ -17,6 +18,32 @@ import java.util.List; public class InventoryUtils { + public static void openFavoritesMenu(Player player) { + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eFavorites: " + player.getName())); + + List heads = HeadAPI.getFavoriteHeads(player.getUniqueId()); + for (Head head : heads) { + pane.addButton(new Button(head.getItemStack(), e -> { + if (e.getClick() == ClickType.SHIFT_LEFT) { + ItemStack item = head.getItemStack(); + item.setAmount(64); + player.getInventory().addItem(item); + return; + } + if (e.getClick() == ClickType.LEFT) { + player.getInventory().addItem(head.getItemStack()); + } + if (e.getClick() == ClickType.RIGHT) { + HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId()); + openFavoritesMenu(player); + Utils.sendMessage(player, "Removed &e" + head.getName() + " &8from favorites."); + } + })); + } + + pane.open(player); + } + public static void openSearchDatabase(Player player, String search) { PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eSearch: " + search)); @@ -29,7 +56,13 @@ public class InventoryUtils { player.getInventory().addItem(item); return; } - player.getInventory().addItem(head.getItemStack()); + if (e.getClick() == ClickType.LEFT) { + player.getInventory().addItem(head.getItemStack()); + } + if (e.getClick() == ClickType.RIGHT) { + HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); + Utils.sendMessage(player, "Added &e" + head.getName() + " &8to favorites."); + } })); } @@ -48,7 +81,13 @@ public class InventoryUtils { player.getInventory().addItem(item); return; } - player.getInventory().addItem(head.getItemStack()); + if (e.getClick() == ClickType.LEFT) { + player.getInventory().addItem(head.getItemStack()); + } + if (e.getClick() == ClickType.RIGHT) { + HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); + Utils.sendMessage(player, "Added &e" + head.getName() + " &8to favorites."); + } })); } @@ -56,7 +95,7 @@ public class InventoryUtils { } public static void openDatabase(Player player) { - Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB")); + Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB &8(" + HeadAPI.getHeads().size() + ")")); fillBorder(inventory, XMaterial.BLACK_STAINED_GLASS_PANE.parseItem()); for (Category category : Category.getCategories()) { @@ -70,6 +109,12 @@ public class InventoryUtils { inventory.addItem(item); } + ItemStack fav = XMaterial.BOOK.parseItem(); + ItemMeta meta = fav.getItemMeta(); + meta.setDisplayName(Utils.colorize("&eFavorites")); + fav.setItemMeta(meta); + inventory.setItem(37, fav); + player.openInventory(inventory); } diff --git a/src/main/java/tsp/headdb/listener/MenuListener.java b/src/main/java/tsp/headdb/listener/MenuListener.java index 4de72aa..57e8e28 100644 --- a/src/main/java/tsp/headdb/listener/MenuListener.java +++ b/src/main/java/tsp/headdb/listener/MenuListener.java @@ -24,6 +24,7 @@ public class MenuListener implements Listener { if (!(e.getWhoClicked() instanceof Player)) { return; } + Player player = (Player) e.getWhoClicked(); if (e.getView().getTitle().startsWith(Utils.colorize("&c&lHeadDB"))) { e.setCancelled(true); Inventory inventory = e.getClickedInventory(); @@ -34,11 +35,19 @@ public class MenuListener implements Listener { if (item != null && item.getType() != XMaterial.AIR.parseMaterial()) { String name = ChatColor.stripColor(item.getItemMeta().getDisplayName().toLowerCase()); + if (name.equalsIgnoreCase("favorites")) { + if (!player.hasPermission("headdb.favorites")) { + Utils.sendMessage(player, "&cYou do not have permission for favorites!"); + player.closeInventory(); + return; + } + InventoryUtils.openFavoritesMenu(player); + return; + } Category category = Category.getByName(name); if (category != null) { - InventoryUtils.openCategoryDatabase((Player) e.getWhoClicked(), category); - + InventoryUtils.openCategoryDatabase(player, category); } } } diff --git a/src/main/java/tsp/headdb/util/Config.java b/src/main/java/tsp/headdb/util/Config.java index bf6f5cf..625dc4e 100644 --- a/src/main/java/tsp/headdb/util/Config.java +++ b/src/main/java/tsp/headdb/util/Config.java @@ -48,6 +48,10 @@ public class Config { return config.getInt(path); } + public List getIntList(String path) { + return config.getIntegerList(path); + } + public long getLong(String path) { return config.getLong(path); } diff --git a/src/main/java/tsp/headdb/util/Log.java b/src/main/java/tsp/headdb/util/Log.java index 4ecafe3..023736e 100644 --- a/src/main/java/tsp/headdb/util/Log.java +++ b/src/main/java/tsp/headdb/util/Log.java @@ -2,6 +2,7 @@ package tsp.headdb.util; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import tsp.headdb.HeadDB; import java.io.PrintWriter; import java.io.StringWriter; @@ -34,6 +35,9 @@ public class Log { } public static void log(LogLevel level, String message) { + if (level == LogLevel.DEBUG && !HeadDB.getCfg().getBoolean("debug")) { + return; + } Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message)); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d815b93..81f0e9b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,4 +2,4 @@ refresh: 3600 # Debug Mode -debug: true \ No newline at end of file +debug: false \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 65d9a9b..041f6c1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ name: HeadDB description: Head Database main: tsp.headdb.HeadDB -version: 1.1 +version: 1.2 api-version: 1.16 author: Silent @@ -19,9 +19,12 @@ permissions: headdb.open: true headdb.search: true headdb.give: true + headdb.favorites: true headdb.open: default: op headdb.search: default: op headdb.give: + default: op + headdb.favorites: default: op \ No newline at end of file