Added a command allowing you to add custom heads and categories.

This commit is contained in:
Brianna 2019-05-13 20:49:57 -04:00
parent dbe3030339
commit f2828b97f6
7 changed files with 85 additions and 16 deletions

View File

@ -186,7 +186,7 @@ public class EpicHeads extends JavaPlugin {
JSONObject jsonObject = (JSONObject) o;
String categoryStr = (String) jsonObject.get("tags");
Optional<Category> tagOptional = headManager.getTags().stream().filter(t -> t.getName().equalsIgnoreCase(categoryStr)).findFirst();
Optional<Category> 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<Category> tagOptional = headManager.getTags().stream()
Optional<Category> tagOptional = headManager.getCategories().stream()
.filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
Category category = tagOptional.orElseGet(() -> new Category(tagStr));

View File

@ -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;

View File

@ -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<Category> 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<String> onTab(EpicHeads instance, CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "epicheads.add";
}
@Override
public String getSyntax() {
return "/heads add <url> <name> <category>";
}
@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.";
}
}

View File

@ -58,14 +58,14 @@ public class GUIOverview extends AbstractGUI {
inventory.setItem(38, Methods.getBackgroundGlass(false));
inventory.setItem(42, Methods.getBackgroundGlass(false));
List<Category> tags = plugin.getHeadManager().getTags();
List<Category> 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<Head> heads = category.isLatestPack() ? plugin.getHeadManager().getLatestPack() : plugin.getHeadManager().getHeadsByTag(category);
List<Head> heads = category.isLatestPack() ? plugin.getHeadManager().getLatestPack() : plugin.getHeadManager().getHeadsByCategory(category);
Head firstHead = heads.get(0);

View File

@ -8,7 +8,7 @@ public class HeadManager {
private static final Set<Head> registeredHeads = new HashSet<>();
private static final List<Head> localRegisteredHeads = new ArrayList<>();
private static final List<Category> registeredTags = new ArrayList<>();
private static final List<Category> registeredCategories = new ArrayList<>();
public Head addHead(Head head) {
registeredHeads.add(head);
@ -43,17 +43,17 @@ public class HeadManager {
List<Head> 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<Head> getHeadsByTag(Category tag) {
return getHeads().stream().filter(head -> head.getCategory() == tag).collect(Collectors.toList());
public List<Head> getHeadsByCategory(Category category) {
return getHeads().stream().filter(head -> head.getCategory() == category).collect(Collectors.toList());
}
public List<Head> getHeads() {
@ -95,17 +95,17 @@ public class HeadManager {
}
public Category addCategory(Category category) {
registeredTags.add(category);
registeredCategories.add(category);
return category;
}
public List<Category> getTags() {
return Collections.unmodifiableList(registeredTags);
public List<Category> getCategories() {
return Collections.unmodifiableList(registeredCategories);
}
public void clear() {
registeredHeads.clear();
localRegisteredHeads.clear();
registeredTags.clear();
registeredCategories.clear();
}
}

View File

@ -34,7 +34,7 @@ public class LoginListeners implements Listener {
String tagStr = "Player Heads";
Optional<Category> tagOptional = headManager.getTags()
Optional<Category> tagOptional = headManager.getCategories()
.stream().filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
Category tag = tagOptional.orElseGet(() -> new Category(tagStr));

View File

@ -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."