From f2828b97f6b67761cd2dc6173300f7d0f271fa09 Mon Sep 17 00:00:00 2001 From: Brianna Date: Mon, 13 May 2019 20:49:57 -0400 Subject: [PATCH] Added a command allowing you to add custom heads and categories. --- .../java/com/songoda/epicheads/EpicHeads.java | 4 +- .../epicheads/command/CommandManager.java | 1 + .../command/commands/CommandAdd.java | 65 +++++++++++++++++++ .../songoda/epicheads/gui/GUIOverview.java | 6 +- .../songoda/epicheads/head/HeadManager.java | 20 +++--- .../epicheads/listeners/LoginListeners.java | 2 +- src/main/resources/en_US.lang | 3 + 7 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/songoda/epicheads/command/commands/CommandAdd.java diff --git a/src/main/java/com/songoda/epicheads/EpicHeads.java b/src/main/java/com/songoda/epicheads/EpicHeads.java index ddfa973..307d445 100644 --- a/src/main/java/com/songoda/epicheads/EpicHeads.java +++ b/src/main/java/com/songoda/epicheads/EpicHeads.java @@ -186,7 +186,7 @@ public class EpicHeads extends JavaPlugin { JSONObject jsonObject = (JSONObject) o; String categoryStr = (String) jsonObject.get("tags"); - Optional tagOptional = headManager.getTags().stream().filter(t -> t.getName().equalsIgnoreCase(categoryStr)).findFirst(); + Optional tagOptional = headManager.getCategories().stream().filter(t -> t.getName().equalsIgnoreCase(categoryStr)).findFirst(); Category category = tagOptional.orElseGet(() -> new Category(categoryStr)); @@ -209,7 +209,7 @@ 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.getCategories().stream() .filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); Category category = tagOptional.orElseGet(() -> new Category(tagStr)); diff --git a/src/main/java/com/songoda/epicheads/command/CommandManager.java b/src/main/java/com/songoda/epicheads/command/CommandManager.java index c675921..f33f3f7 100644 --- a/src/main/java/com/songoda/epicheads/command/CommandManager.java +++ b/src/main/java/com/songoda/epicheads/command/CommandManager.java @@ -34,6 +34,7 @@ public class CommandManager implements CommandExecutor { addCommand(new CommandBase64(commandEpicHeads)); addCommand(new CommandGive(commandEpicHeads)); addCommand(new CommandGiveToken(commandEpicHeads)); + addCommand(new CommandAdd(commandEpicHeads)); for (AbstractCommand abstractCommand : commands) { if (abstractCommand.getParent() != null) continue; diff --git a/src/main/java/com/songoda/epicheads/command/commands/CommandAdd.java b/src/main/java/com/songoda/epicheads/command/commands/CommandAdd.java new file mode 100644 index 0000000..0ba5ce2 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/command/commands/CommandAdd.java @@ -0,0 +1,65 @@ +package com.songoda.epicheads.command.commands; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.command.AbstractCommand; +import com.songoda.epicheads.head.Category; +import com.songoda.epicheads.head.Head; +import com.songoda.epicheads.head.HeadManager; +import org.bukkit.command.CommandSender; + +import java.util.List; +import java.util.stream.Collectors; + +public class CommandAdd extends AbstractCommand { + + public CommandAdd(AbstractCommand parent) { + super(parent, false, "add"); + } + + @Override + protected ReturnType runCommand(EpicHeads instance, CommandSender sender, String... args) { + if (args.length != 4) return ReturnType.SYNTAX_ERROR; + + String url = args[1]; + String name = args[2].replace("_", " "); + String categoryStr = args[3].replace("_", " "); + + HeadManager headManager = instance.getHeadManager(); + + if (headManager.getLocalHeads().stream().anyMatch(head -> head.getURL().equals(url))) { + sender.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.add.exists")); + return ReturnType.FAILURE; + } + + List categories = headManager.getCategories().stream().filter(category1 -> category1.getName().equals(categoryStr)).collect(Collectors.toList()); + + Category category = categories.isEmpty() ? new Category(categoryStr) : categories.get(0); + + headManager.addLocalHead(new Head(headManager.getNextLocalId(), name, url, category, null, (byte)0)); + sender.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.add.success", name)); + if (categories.isEmpty()) { + instance.reload(); + } + return ReturnType.SUCCESS; + } + + @Override + protected List onTab(EpicHeads instance, CommandSender sender, String... args) { + return null; + } + + @Override + public String getPermissionNode() { + return "epicheads.add"; + } + + @Override + public String getSyntax() { + return "/heads add "; + } + + @Override + public String getDescription() { + return "Adds a head to your local database. Including a category that does not exist will create new category. Make sure to use underscores and now spaces."; + } +} diff --git a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java index 75390c4..346eb5b 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java @@ -58,14 +58,14 @@ 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().getCategories(); int add = 0; for (int i = 0; i < tags.size(); i++) { if (i + add == 7 || i + add == 16) add = add + 2; - Category category = plugin.getHeadManager().getTags().get(i); + Category category = plugin.getHeadManager().getCategories().get(i); - List heads = category.isLatestPack() ? plugin.getHeadManager().getLatestPack() : plugin.getHeadManager().getHeadsByTag(category); + List heads = category.isLatestPack() ? plugin.getHeadManager().getLatestPack() : plugin.getHeadManager().getHeadsByCategory(category); Head firstHead = heads.get(0); diff --git a/src/main/java/com/songoda/epicheads/head/HeadManager.java b/src/main/java/com/songoda/epicheads/head/HeadManager.java index 21ef106..41328f7 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 registeredCategories = 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 (Category tag : registeredTags) { - if (!tag.getName().equalsIgnoreCase(query)) continue; - return getHeads().stream().filter(head -> head.getCategory() == tag).collect(Collectors.toList()); + for (Category category : registeredCategories) { + if (!category.getName().equalsIgnoreCase(query)) continue; + return getHeads().stream().filter(head -> head.getCategory() == category).collect(Collectors.toList()); } } return result; } - public List getHeadsByTag(Category tag) { - return getHeads().stream().filter(head -> head.getCategory() == tag).collect(Collectors.toList()); + public List getHeadsByCategory(Category category) { + return getHeads().stream().filter(head -> head.getCategory() == category).collect(Collectors.toList()); } public List getHeads() { @@ -95,17 +95,17 @@ public class HeadManager { } public Category addCategory(Category category) { - registeredTags.add(category); + registeredCategories.add(category); return category; } - public List getTags() { - return Collections.unmodifiableList(registeredTags); + public List getCategories() { + return Collections.unmodifiableList(registeredCategories); } public void clear() { registeredHeads.clear(); localRegisteredHeads.clear(); - registeredTags.clear(); + registeredCategories.clear(); } } diff --git a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java index 1c80923..dc9809c 100644 --- a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java @@ -34,7 +34,7 @@ public class LoginListeners implements Listener { String tagStr = "Player Heads"; - Optional tagOptional = headManager.getTags() + Optional tagOptional = headManager.getCategories() .stream().filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); Category tag = tagOptional.orElseGet(() -> new Category(tagStr)); diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index d78a439..503f6d1 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -37,5 +37,8 @@ command.give.notfound = "&cThe head &4%name%&c could not be found." command.give.success = "&7You have given &6%player% &7a head named &6%name%&7." command.give.receive = "&7You have been given a head named &6%name%&7." +command.add.success = "&7Added the head &6%name% &7successfully." +command.add.exists = "&cThat head already exists." + command.givetoken.success = "&7You gave &6%player% %amount% &7player head tokens." command.givetoken.receive = "&7You have been given &6%amount% &7player head tokens." \ No newline at end of file