From b82c72b4cdc552e4eb1c4ddfe1e1aff83a8fc3f8 Mon Sep 17 00:00:00 2001 From: Silent Date: Tue, 17 Nov 2020 22:39:22 +0100 Subject: [PATCH] Local Heads and minor fixes/changes --- pom.xml | 5 +- src/main/java/tsp/headdb/HeadDB.java | 3 +- src/main/java/tsp/headdb/api/Head.java | 3 +- src/main/java/tsp/headdb/api/HeadAPI.java | 15 ++++ src/main/java/tsp/headdb/api/LocalHead.java | 77 +++++++++++++++++++ .../tsp/headdb/inventory/InventoryUtils.java | 68 +++++++++++++--- .../tsp/headdb/listener/JoinListener.java | 19 +++++ .../tsp/headdb/listener/MenuListener.java | 10 +++ src/main/resources/plugin.yml | 5 +- 9 files changed, 191 insertions(+), 14 deletions(-) create mode 100644 src/main/java/tsp/headdb/api/LocalHead.java create mode 100644 src/main/java/tsp/headdb/listener/JoinListener.java diff --git a/pom.xml b/pom.xml index fcdb4aa..fa0998e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,17 +6,19 @@ tsp.headdb HeadDB - 1.2 + 1.3 jar HeadDB Head Database + papermc https://papermc.io/repo/repository/maven-public/ + mojang-repo https://libraries.minecraft.net/ @@ -36,6 +38,7 @@ json-simple 1.1.1 + com.mojang authlib diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 06a8566..3e65a91 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -1,9 +1,9 @@ package tsp.headdb; import org.bukkit.plugin.java.JavaPlugin; -import tsp.headdb.api.HeadAPI; import tsp.headdb.command.Command_headdb; import tsp.headdb.database.HeadDatabase; +import tsp.headdb.listener.JoinListener; import tsp.headdb.listener.PagedPaneListener; import tsp.headdb.listener.MenuListener; import tsp.headdb.util.Config; @@ -32,6 +32,7 @@ public class HeadDB extends JavaPlugin { Log.debug("Registering listeners..."); new PagedPaneListener(this); new MenuListener(this); + new JoinListener(this); Log.debug("Registering commands..."); getCommand("headdb").setExecutor(new Command_headdb()); diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java index f78b15f..6acc698 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -26,11 +26,10 @@ public class Head { Validate.notNull(name, "name must not be null!"); Validate.notNull(uuid, "uuid must not be null!"); Validate.notNull(value, "value must not be null!"); - Validate.notNull(category, "category must not be null!"); ItemStack item = new ItemStack(Material.PLAYER_HEAD); SkullMeta meta = (SkullMeta) item.getItemMeta(); - meta.setDisplayName(Utils.colorize(category.getColor() + name)); + meta.setDisplayName(Utils.colorize(category != null ? category.getColor() + name : "&8" + name)); // set skull owner GameProfile profile = new GameProfile(uuid, name); profile.getProperties().put("textures", new Property("textures", value)); diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index 20d963e..2f9ce39 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -1,5 +1,7 @@ package tsp.headdb.api; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import tsp.headdb.HeadDB; import tsp.headdb.database.Category; @@ -84,6 +86,19 @@ public class HeadAPI { return heads; } + public static List getLocalHeads() { + List heads = new ArrayList<>(); + for (String key : HeadDB.getPlayerdata().getKeys(false)) { + OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key)); + heads.add(new LocalHead.Builder() + .withUUID(player.getUniqueId()) + .withName(player.getName()) + .build()); + } + + return heads; + } + public static void updateDatabase() { HeadDatabase.update(); } diff --git a/src/main/java/tsp/headdb/api/LocalHead.java b/src/main/java/tsp/headdb/api/LocalHead.java new file mode 100644 index 0000000..020a896 --- /dev/null +++ b/src/main/java/tsp/headdb/api/LocalHead.java @@ -0,0 +1,77 @@ +package tsp.headdb.api; + +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; +import tsp.headdb.util.Utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class LocalHead { + + private UUID uuid; + private String name; + + public ItemStack getItemStack() { + Validate.notNull(uuid, "uuid must not be null!"); + + ItemStack item = new ItemStack(Material.PLAYER_HEAD); + SkullMeta meta = (SkullMeta) item.getItemMeta(); + meta.setOwningPlayer(Bukkit.getOfflinePlayer(uuid)); + meta.setDisplayName(Utils.colorize("&8" + name)); + List lore = new ArrayList<>(); + lore.add(Utils.colorize("&aUUID: " + uuid.toString())); + meta.setLore(lore); + item.setItemMeta(meta); + + return item; + } + + public LocalHead(UUID uuid) { + this.uuid = uuid; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public static class Builder { + + private UUID uuid; + private String name; + + public Builder withUUID(UUID uuid) { + this.uuid = uuid; + return this; + } + + public Builder withName(String name) { + this.name = name; + return this; + } + + public LocalHead build() { + LocalHead head = new LocalHead(uuid); + head.setName(name); + return head; + } + + } + +} diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 3277044..3a8d0bb 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -1,7 +1,6 @@ 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; @@ -9,6 +8,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; +import tsp.headdb.api.LocalHead; import tsp.headdb.database.Category; import tsp.headdb.util.Utils; import tsp.headdb.util.XMaterial; @@ -18,8 +18,34 @@ import java.util.List; public class InventoryUtils { + public static void openLocalMenu(Player player) { + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &aLocal Heads")); + + List heads = HeadAPI.getLocalHeads(); + for (LocalHead localHead : heads) { + pane.addButton(new Button(localHead.getItemStack(), e -> { + if (e.getClick() == ClickType.SHIFT_LEFT) { + ItemStack item = localHead.getItemStack(); + item.setAmount(64); + player.getInventory().addItem(item); + return; + } + if (e.getClick() == ClickType.LEFT) { + player.getInventory().addItem(localHead.getItemStack()); + return; + } + if (e.getClick() == ClickType.RIGHT) { + player.closeInventory(); + Utils.sendMessage(player, "&cLocal heads can not be added to favorites!"); + } + })); + } + + pane.open(player); + } + public static void openFavoritesMenu(Player player) { - PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eFavorites: " + player.getName())); + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &eFavorites: " + player.getName())); List heads = HeadAPI.getFavoriteHeads(player.getUniqueId()); for (Head head : heads) { @@ -45,7 +71,7 @@ public class InventoryUtils { } public static void openSearchDatabase(Player player, String search) { - PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &eSearch: " + search)); + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &eSearch: " + search)); List heads = HeadAPI.getHeadsByName(search); for (Head head : heads) { @@ -70,7 +96,7 @@ public class InventoryUtils { } public static void openCategoryDatabase(Player player, Category category) { - PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB - &e" + category.getName())); + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &e" + category.getName())); List heads = HeadAPI.getHeads(category); for (Head head : heads) { @@ -109,11 +135,19 @@ 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); + inventory.setItem(37, buildButton( + XMaterial.BOOK.parseItem(), + "&eFavorites", + "", + "&8Click to view your favorites") + ); + + inventory.setItem(43, buildButton( + XMaterial.COMPASS.parseItem(), + "&aLocal", + "", + "&8Heads from any players that have logged on the server" + )); player.openInventory(inventory); } @@ -140,4 +174,20 @@ public class InventoryUtils { } } + private static ItemStack buildButton(ItemStack item, String name, String... lore) { + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(Utils.colorize(name)); + List list = meta.getLore(); + if (list == null) { + list = new ArrayList<>(); + } + + for (String line : lore) { + list.add(Utils.colorize(line)); + } + + item.setItemMeta(meta); + return item; + } + } diff --git a/src/main/java/tsp/headdb/listener/JoinListener.java b/src/main/java/tsp/headdb/listener/JoinListener.java new file mode 100644 index 0000000..4b2f007 --- /dev/null +++ b/src/main/java/tsp/headdb/listener/JoinListener.java @@ -0,0 +1,19 @@ +package tsp.headdb.listener; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import tsp.headdb.HeadDB; + +public class JoinListener implements Listener { + + public JoinListener(HeadDB plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + HeadDB.getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName()); + } + +} diff --git a/src/main/java/tsp/headdb/listener/MenuListener.java b/src/main/java/tsp/headdb/listener/MenuListener.java index 57e8e28..2dcd2b5 100644 --- a/src/main/java/tsp/headdb/listener/MenuListener.java +++ b/src/main/java/tsp/headdb/listener/MenuListener.java @@ -44,6 +44,16 @@ public class MenuListener implements Listener { InventoryUtils.openFavoritesMenu(player); return; } + if (name.equalsIgnoreCase("local")) { + if (!player.hasPermission("headdb.local")) { + Utils.sendMessage(player, "&cYou do not have permission to view local heads!"); + player.closeInventory(); + return; + } + InventoryUtils.openLocalMenu(player); + return; + } + Category category = Category.getByName(name); if (category != null) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 041f6c1..bae6c85 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.2 +version: 1.3 api-version: 1.16 author: Silent @@ -20,6 +20,7 @@ permissions: headdb.search: true headdb.give: true headdb.favorites: true + headdb.local: true headdb.open: default: op headdb.search: @@ -27,4 +28,6 @@ permissions: headdb.give: default: op headdb.favorites: + default: op + headdb.local: default: op \ No newline at end of file