From 41b4fc0eb2161975dd8c636fe3b6a064067a634d Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 8 May 2019 14:06:36 -0400 Subject: [PATCH] Added latest pack option and changed from tags to category. --- .../java/com/songoda/epicheads/EpicHeads.java | 27 +++++++++------- .../com/songoda/epicheads/gui/GUIHeads.java | 6 ++-- .../songoda/epicheads/gui/GUIOverview.java | 22 +++++++------ .../com/songoda/epicheads/head/Category.java | 31 ++++++++++++++++++ .../java/com/songoda/epicheads/head/Head.java | 18 +++++++---- .../songoda/epicheads/head/HeadManager.java | 32 +++++++++++++------ .../java/com/songoda/epicheads/head/Tag.java | 21 ------------ .../epicheads/listeners/LoginListeners.java | 10 +++--- .../epicheads/utils/storage/Storage.java | 2 +- src/main/resources/en_US.lang | 1 + 10 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/songoda/epicheads/head/Category.java delete mode 100644 src/main/java/com/songoda/epicheads/head/Tag.java diff --git a/src/main/java/com/songoda/epicheads/EpicHeads.java b/src/main/java/com/songoda/epicheads/EpicHeads.java index d1365a2..b58c40e 100644 --- a/src/main/java/com/songoda/epicheads/EpicHeads.java +++ b/src/main/java/com/songoda/epicheads/EpicHeads.java @@ -1,9 +1,9 @@ package com.songoda.epicheads; import com.songoda.epicheads.command.CommandManager; +import com.songoda.epicheads.head.Category; import com.songoda.epicheads.head.Head; import com.songoda.epicheads.head.HeadManager; -import com.songoda.epicheads.head.Tag; import com.songoda.epicheads.listeners.ItemListeners; import com.songoda.epicheads.listeners.LoginListeners; import com.songoda.epicheads.players.EPlayer; @@ -159,6 +159,7 @@ public class EpicHeads extends JavaPlugin { private boolean loadHeads() { try { this.headManager.clear(); + this.headManager.addCategory(new Category("Latest Pack", true)); JSONParser parser = new JSONParser(); JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(getDataFolder() + "/heads.json")); @@ -166,22 +167,23 @@ public class EpicHeads extends JavaPlugin { for (Object o : jsonArray) { JSONObject jsonObject = (JSONObject) o; - String tags = (String) jsonObject.get("tags"); - Optional tagOptional = headManager.getTags().stream().filter(t -> t.getName().equalsIgnoreCase(tags)).findFirst(); + String categoryStr = (String) jsonObject.get("tags"); + Optional tagOptional = headManager.getTags().stream().filter(t -> t.getName().equalsIgnoreCase(categoryStr)).findFirst(); - Tag tag = tagOptional.orElseGet(() -> new Tag(tags)); + Category category = tagOptional.orElseGet(() -> new Category(categoryStr)); Head head = new Head(Integer.parseInt((String) jsonObject.get("id")), (String) jsonObject.get("name"), (String) jsonObject.get("url"), - tag, + category, + (String) jsonObject.get("pack"), Byte.parseByte((String) jsonObject.get("staff_picked"))); - if (head.getName() == null || - head.getName().equals("null")) continue; + if (head.getName() == null || head.getName().equals("null") + || head.getPack() != null && head.getPack().equals("null")) continue; if (!tagOptional.isPresent()) - headManager.addTag(tag); + headManager.addCategory(category); headManager.addHead(head); } @@ -189,19 +191,20 @@ public class EpicHeads extends JavaPlugin { for (StorageRow row : storage.getRowsByGroup("local")) { String tagStr = row.get("category").asString(); - Optional tagOptional = headManager.getTags().stream() + Optional tagOptional = headManager.getTags().stream() .filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); - Tag tag = tagOptional.orElseGet(() -> new Tag(tagStr)); + Category category = tagOptional.orElseGet(() -> new Category(tagStr)); Head head = new Head(row.get("id").asInt(), row.get("name").asString(), row.get("url").asString(), - tag, + category, + null, (byte) 0); if (!tagOptional.isPresent()) - headManager.addTag(tag); + headManager.addCategory(category); headManager.addLocalHead(head); } } diff --git a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java index 996e4ae..8a1a48f 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java @@ -2,7 +2,7 @@ package com.songoda.epicheads.gui; import com.songoda.epicheads.EpicHeads; import com.songoda.epicheads.head.Head; -import com.songoda.epicheads.head.Tag; +import com.songoda.epicheads.head.Category; import com.songoda.epicheads.players.EPlayer; import com.songoda.epicheads.utils.AbstractChatConfirm; import com.songoda.epicheads.utils.SettingsManager; @@ -52,10 +52,10 @@ public class GUIHeads extends AbstractGUI { player.sendMessage(plugin.getReferences().getPrefix() + plugin.getLocale().getMessage("general.search.nonefound")); return; } - Tag tag = heads.get(0).getTag(); + Category category = heads.get(0).getCategory(); this.maxPage = (int) Math.floor(numHeads / 45.0); - init((query != null ? plugin.getLocale().getMessage("general.word.query") + ": " + query : tag.getName()) + init((query != null ? plugin.getLocale().getMessage("general.word.query") + ": " + query : category.getName()) + " (" + numHeads + ") " + plugin.getLocale().getMessage("general.word.page") + " " + (page + 1) + "/" + (maxPage + 1), 54); constructGUI(); } diff --git a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java index 470bc42..a46da22 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java @@ -1,7 +1,8 @@ package com.songoda.epicheads.gui; import com.songoda.epicheads.EpicHeads; -import com.songoda.epicheads.head.Tag; +import com.songoda.epicheads.head.Category; +import com.songoda.epicheads.head.Head; import com.songoda.epicheads.utils.Methods; import com.songoda.epicheads.utils.ServerVersion; import com.songoda.epicheads.utils.SettingsManager; @@ -57,23 +58,27 @@ public class GUIOverview extends AbstractGUI { inventory.setItem(38, Methods.getBackgroundGlass(false)); inventory.setItem(42, Methods.getBackgroundGlass(false)); - List tags = plugin.getHeadManager().getTags(); + List tags = plugin.getHeadManager().getTags(); int add = 0; for (int i = 0; i < tags.size(); i++) { if (i + add == 7 || i + add == 16) add = add + 2; - Tag tag = plugin.getHeadManager().getTags().get(i); + Category category = plugin.getHeadManager().getTags().get(i); - if (!player.hasPermission("epicheads.category." + tag.getName().replace(" ", "_"))) continue; + List heads = category.isLatestPack() ? plugin.getHeadManager().getLatestPack() : plugin.getHeadManager().getHeadsByTag(category); + + Head firstHead = heads.get(0); + + if (!player.hasPermission("epicheads.category." + category.getName().replace(" ", "_"))) continue; createButton(i + 10 + add, Methods.addTexture(new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM"), 1, (byte) 3), - plugin.getHeadManager().getHeadsByTag(tag).get(0).getURL()), - plugin.getLocale().getMessage("gui.overview.headname", Color.getRandomColor() + tag.getName()), - plugin.getLocale().getMessage("gui.overview.headlore", String.format("%,d", tag.getCount()))); + firstHead.getURL()), + plugin.getLocale().getMessage("gui.overview.headname", Color.getRandomColor() + category.getName()), + category.isLatestPack() ? plugin.getLocale().getMessage("gui.overview.packlore", firstHead.getPack()) : plugin.getLocale().getMessage("gui.overview.headlore", String.format("%,d", category.getCount()))); registerClickable(i + 10 + add, ((player1, inventory1, cursor, slot, type) -> - new GUIHeads(plugin, player, null, plugin.getHeadManager().getHeadsByTag(tag)))); + new GUIHeads(plugin, player, category.isLatestPack() ? category.getName() : null, heads))); } createButton(SettingsManager.Setting.DISCORD.getBoolean() ? 39 : 40, Material.COMPASS, plugin.getLocale().getMessage("gui.overview.search")); @@ -92,7 +97,6 @@ public class GUIOverview extends AbstractGUI { plugin.getLocale().getMessage("gui.overview.discord"), lore2); } - } @Override diff --git a/src/main/java/com/songoda/epicheads/head/Category.java b/src/main/java/com/songoda/epicheads/head/Category.java new file mode 100644 index 0000000..9bb8b44 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/head/Category.java @@ -0,0 +1,31 @@ +package com.songoda.epicheads.head; + +import com.songoda.epicheads.EpicHeads; + +public class Category { + + private final String name; + private boolean latestPack = false; + + public Category(String name) { + this.name = name; + } + + public Category(String name, boolean latestPack) { + this.name = name; + this.latestPack = latestPack; + } + + public String getName() { + return name; + } + + public boolean isLatestPack() { + return latestPack; + } + + public int getCount() { + return Math.toIntExact(EpicHeads.getInstance().getHeadManager() + .getHeads().stream().filter(head -> head.getCategory() == this).count()); + } +} diff --git a/src/main/java/com/songoda/epicheads/head/Head.java b/src/main/java/com/songoda/epicheads/head/Head.java index 6749e64..b9672d4 100644 --- a/src/main/java/com/songoda/epicheads/head/Head.java +++ b/src/main/java/com/songoda/epicheads/head/Head.java @@ -11,21 +11,24 @@ import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class Head { private final int id; private final String name; private final String URL; + private final String pack; private final byte staffPicked; - private final Tag tag; + private final Category category; - public Head(int id, String name, String URL, Tag tag, byte staffPicked) { + public Head(int id, String name, String URL, Category category, String pack, byte staffPicked) { this.id = id; this.name = name; this.URL = URL; - this.tag = tag; + this.category = category; + this.pack = pack; this.staffPicked = staffPicked; } @@ -37,12 +40,14 @@ public class Head { return name; } + public String getPack() { return pack; } + public String getURL() { return URL; } - public Tag getTag() { - return tag; + public Category getCategory() { + return category; } public byte getStaffPicked() { @@ -81,7 +86,8 @@ public class Head { + "Id:\"" + id + "\"," + "Name:\"" + name + "\"," + "URL:\"" + URL + "\"," - + "Tags:\"" + tag.getName() + "\"," + + "Category:\"" + category.getName() + "\"," + + "Pack:\"" + pack + "\"," + "StaffPicked:\"" + staffPicked + "\"" + "}"; } diff --git a/src/main/java/com/songoda/epicheads/head/HeadManager.java b/src/main/java/com/songoda/epicheads/head/HeadManager.java index bbcc363..21ef106 100644 --- a/src/main/java/com/songoda/epicheads/head/HeadManager.java +++ b/src/main/java/com/songoda/epicheads/head/HeadManager.java @@ -8,7 +8,7 @@ public class HeadManager { private static final Set registeredHeads = new HashSet<>(); private static final List localRegisteredHeads = new ArrayList<>(); - private static final List registeredTags = new ArrayList<>(); + private static final List registeredTags = new ArrayList<>(); public Head addHead(Head head) { registeredHeads.add(head); @@ -43,17 +43,17 @@ public class HeadManager { List result = getHeads().stream().filter(head -> head.getName().contains(query)).collect(Collectors.toList()); if (result.isEmpty()) { - for (Tag tag : registeredTags) { + for (Category tag : registeredTags) { if (!tag.getName().equalsIgnoreCase(query)) continue; - return getHeads().stream().filter(head -> head.getTag() == tag).collect(Collectors.toList()); + return getHeads().stream().filter(head -> head.getCategory() == tag).collect(Collectors.toList()); } } return result; } - public List getHeadsByTag(Tag tag) { - return getHeads().stream().filter(head -> head.getTag() == tag).collect(Collectors.toList()); + public List getHeadsByTag(Category tag) { + return getHeads().stream().filter(head -> head.getCategory() == tag).collect(Collectors.toList()); } public List getHeads() { @@ -76,16 +76,30 @@ public class HeadManager { return new ArrayList<>(registeredHeads); } + public List getLatestPack() { + List heads = getHeads().stream().sorted(Comparator.comparingInt(Head::getId)).filter(head -> head.getPack() != null).collect(Collectors.toList()); + + Collections.reverse(heads); + + if (heads.isEmpty()) return new ArrayList<>(); + + String latestPack = heads.get(0).getPack(); + + heads = heads.stream().filter(head -> head.getPack().equals(latestPack)).collect(Collectors.toList()); + + return heads; + } + public void removeLocalHead(Head head) { localRegisteredHeads.remove(head); } - public Tag addTag(Tag tag) { - registeredTags.add(tag); - return tag; + public Category addCategory(Category category) { + registeredTags.add(category); + return category; } - public List getTags() { + public List getTags() { return Collections.unmodifiableList(registeredTags); } diff --git a/src/main/java/com/songoda/epicheads/head/Tag.java b/src/main/java/com/songoda/epicheads/head/Tag.java deleted file mode 100644 index 648fffc..0000000 --- a/src/main/java/com/songoda/epicheads/head/Tag.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.songoda.epicheads.head; - -import com.songoda.epicheads.EpicHeads; - -public class Tag { - - private final String name; - - public Tag(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public int getCount() { - return Math.toIntExact(EpicHeads.getInstance().getHeadManager() - .getHeads().stream().filter(head -> head.getTag() == this).count()); - } -} diff --git a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java index cb86150..d71e064 100644 --- a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java @@ -3,7 +3,7 @@ package com.songoda.epicheads.listeners; import com.songoda.epicheads.EpicHeads; import com.songoda.epicheads.head.Head; import com.songoda.epicheads.head.HeadManager; -import com.songoda.epicheads.head.Tag; +import com.songoda.epicheads.head.Category; import com.songoda.epicheads.utils.Methods; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -32,13 +32,13 @@ public class LoginListeners implements Listener { String tagStr = "Player Heads"; - Optional tagOptional = headManager.getTags() + Optional tagOptional = headManager.getTags() .stream().filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); - Tag tag = tagOptional.orElseGet(() -> new Tag(tagStr)); + Category tag = tagOptional.orElseGet(() -> new Category(tagStr)); if (!tagOptional.isPresent()) - headManager.addTag(tag); + headManager.addCategory(tag); Optional optional = headManager.getLocalHeads().stream() .filter(h -> h.getName().equalsIgnoreCase(event.getPlayer().getName())).findFirst(); @@ -51,7 +51,7 @@ public class LoginListeners implements Listener { headManager.removeLocalHead(head); } - headManager.addLocalHeads(new Head(id, player.getName(), url, tag, (byte) 0)); + headManager.addLocalHeads(new Head(id, player.getName(), url, tag, null, (byte) 0)); } diff --git a/src/main/java/com/songoda/epicheads/utils/storage/Storage.java b/src/main/java/com/songoda/epicheads/utils/storage/Storage.java index 9e30975..9d520a2 100644 --- a/src/main/java/com/songoda/epicheads/utils/storage/Storage.java +++ b/src/main/java/com/songoda/epicheads/utils/storage/Storage.java @@ -37,7 +37,7 @@ public abstract class Storage { prepareSaveItem("local", new StorageItem("url", head.getURL()), new StorageItem("name", head.getName()), new StorageItem("id", head.getId()), - new StorageItem("category", head.getTag().getName())); + new StorageItem("category", head.getCategory().getName())); } } diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index 9a85011..f80c430 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -21,6 +21,7 @@ gui.overview.title = "EpicHeads (%count% heads)" gui.overview.search = "&a&lSearch" gui.overview.headname = "&c&l%name%" gui.overview.headlore = "&e%count% heads" +gui.overview.packlore = "&e%pack%" gui.overview.viewfavorites = "&6&lView Favorites" gui.overview.favoriteslore = "&8Shift click any head|&8to save as a favorite." gui.overview.discord = "&9&lDiscord"