Added tags and tag search

Dieser Commit ist enthalten in:
Silent 2020-12-31 17:49:48 +01:00
Ursprung 24edccf01d
Commit 121267a71a
9 geänderte Dateien mit 114 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -17,8 +17,8 @@ import tsp.headdb.util.Utils;
public class HeadDB extends JavaPlugin { public class HeadDB extends JavaPlugin {
private static HeadDB instance; private static HeadDB instance;
private static Config config; private Config config;
private static Json playerdata; private Json playerdata;
@Override @Override
public void onEnable() { public void onEnable() {
@ -42,8 +42,8 @@ public class HeadDB extends JavaPlugin {
if (config.getBoolean("fetchStartup")) { if (config.getBoolean("fetchStartup")) {
if (config.getBoolean("asyncStartup")) { if (config.getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)"); Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, task -> HeadAPI.getDatabase().update()); Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update());
}else { } else {
Log.debug("Initializing Database... (SYNC)"); Log.debug("Initializing Database... (SYNC)");
HeadAPI.getDatabase().update(); HeadAPI.getDatabase().update();
} }
@ -52,11 +52,11 @@ public class HeadDB extends JavaPlugin {
Log.info("Done!"); Log.info("Done!");
} }
public static Config getCfg() { public Config getCfg() {
return config; return config;
} }
public static Json getPlayerdata() { public Json getPlayerdata() {
return playerdata; return playerdata;
} }

Datei anzeigen

@ -11,7 +11,9 @@ import tsp.headdb.util.Log;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public class Head { public class Head {
@ -21,6 +23,7 @@ public class Head {
private String value; private String value;
private Category category; private Category category;
private int id; private int id;
private List<String> tags;
public Head() {} public Head() {}
@ -50,7 +53,8 @@ public class Head {
} }
meta.setLore(Arrays.asList( meta.setLore(Arrays.asList(
Utils.colorize("&cID: " + id), Utils.colorize("&cID: " + id),
" ", Utils.colorize("&e" + buildTagLore((String[]) tags.toArray())),
"",
Utils.colorize("&8Right-Click to add/remove from favorites.") Utils.colorize("&8Right-Click to add/remove from favorites.")
)); ));
item.setItemMeta(meta); item.setItemMeta(meta);
@ -78,6 +82,10 @@ public class Head {
return id; return id;
} }
public List<String> getTags() {
return tags;
}
public Head withName(String name) { public Head withName(String name) {
this.name = name; this.name = name;
return this; return this;
@ -103,4 +111,21 @@ public class Head {
return this; return this;
} }
public Head withTags(String tags) {
this.tags = Arrays.asList(tags.split(","));
return this;
}
private String buildTagLore(String... tags) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < tags.length; i++) {
builder.append(tags[i]);
if (i != tags.length - 1) {
builder.append(",");
}
}
return builder.toString();
}
} }

Datei anzeigen

@ -85,6 +85,10 @@ public final class HeadAPI {
return database.getHeadByUUID(uuid); return database.getHeadByUUID(uuid);
} }
public static List<Head> getHeadsByTag(String tag) {
return database.getHeadsByTag(tag);
}
/** /**
* Retrieves a {@link List} of {@link Head}'s matching a name * Retrieves a {@link List} of {@link Head}'s matching a name
* *
@ -143,11 +147,11 @@ public final class HeadAPI {
* @param id The ID of the head * @param id The ID of the head
*/ */
public static void addFavoriteHead(UUID uuid, int id) { public static void addFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); List<Integer> favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
if (!favs.contains(id)) { if (!favs.contains(id)) {
favs.add(id); favs.add(id);
} }
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs);
} }
/** /**
@ -157,14 +161,14 @@ public final class HeadAPI {
* @param id The ID of the head * @param id The ID of the head
*/ */
public static void removeFavoriteHead(UUID uuid, int id) { public static void removeFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); List<Integer> favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
for (int i = 0; i < favs.size(); i++) { for (int i = 0; i < favs.size(); i++) {
if (favs.get(i) == id) { if (favs.get(i) == id) {
favs.remove(i); favs.remove(i);
break; break;
} }
} }
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs);
} }
/** /**
@ -175,7 +179,7 @@ public final class HeadAPI {
*/ */
public static List<Head> getFavoriteHeads(UUID uuid) { public static List<Head> getFavoriteHeads(UUID uuid) {
List<Head> heads = new ArrayList<>(); List<Head> heads = new ArrayList<>();
List<Integer> ids = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); List<Integer> ids = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
for (int id : ids) { for (int id : ids) {
Head head = getHeadByID(id); Head head = getHeadByID(id);
heads.add(head); heads.add(head);
@ -192,7 +196,7 @@ public final class HeadAPI {
*/ */
public static List<LocalHead> getLocalHeads() { public static List<LocalHead> getLocalHeads() {
List<LocalHead> heads = new ArrayList<>(); List<LocalHead> heads = new ArrayList<>();
for (String key : HeadDB.getPlayerdata().singleLayerKeySet()) { for (String key : HeadDB.getInstance().getPlayerdata().singleLayerKeySet()) {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key)); OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key));
heads.add(new LocalHead(player.getUniqueId()) heads.add(new LocalHead(player.getUniqueId())
.withName(player.getName())); .withName(player.getName()));

Datei anzeigen

@ -68,6 +68,27 @@ public class Command_headdb implements CommandExecutor {
return true; return true;
} }
if (sub.equalsIgnoreCase("tagsearch") || sub.equalsIgnoreCase("ts")) {
if (!sender.hasPermission("headdb.tagsearch")) {
Utils.sendMessage(sender, "&cNo permission!");
return true;
}
if (args.length < 2) {
Utils.sendMessage(sender, "&c/hdb tagsearch <tag>");
return true;
}
if (!(sender instanceof Player)) {
Utils.sendMessage(sender, "&cOnly players may open the database.");
return true;
}
Player player = (Player) sender;
String tag = args[1];
Utils.sendMessage(sender, "Searching for heads with tag &e" + tag);
InventoryUtils.openTagSearchDatabase(player, tag);
return true;
}
if (sub.equalsIgnoreCase("give") || sub.equalsIgnoreCase("g")) { if (sub.equalsIgnoreCase("give") || sub.equalsIgnoreCase("g")) {
if (!sender.hasPermission("headdb.give")) { if (!sender.hasPermission("headdb.give")) {
Utils.sendMessage(sender, "&cNo permission!"); Utils.sendMessage(sender, "&cNo permission!");
@ -112,6 +133,7 @@ public class Command_headdb implements CommandExecutor {
Utils.sendMessage(sender, " > &c/hdb &7- Opens the database"); Utils.sendMessage(sender, " > &c/hdb &7- Opens the database");
Utils.sendMessage(sender, " > &c/hdb info &9(i) &7- Plugin Information"); Utils.sendMessage(sender, " > &c/hdb info &9(i) &7- Plugin Information");
Utils.sendMessage(sender, " > &c/hdb search &9(s) &c<name> &7- Search for heads matching a name"); Utils.sendMessage(sender, " > &c/hdb search &9(s) &c<name> &7- Search for heads matching a name");
Utils.sendMessage(sender, " > &c/hdb tagsearch &9(ts) &c<tag> &7- Search for heads matching a tag");
Utils.sendMessage(sender, " > &c/hdb give &9(g) &c<id> <player> &6[amount] &7- Give player a head"); Utils.sendMessage(sender, " > &c/hdb give &9(g) &c<id> <player> &6[amount] &7- Give player a head");
Utils.sendMessage(sender, " "); Utils.sendMessage(sender, " ");
return true; return true;

Datei anzeigen

@ -26,6 +26,7 @@ public class HeadDatabase {
private final Map<Category, List<Head>> HEADS = new HashMap<>(); private final Map<Category, List<Head>> HEADS = new HashMap<>();
private final String URL = "https://minecraft-heads.com/scripts/api.php?cat="; private final String URL = "https://minecraft-heads.com/scripts/api.php?cat=";
private final String TAGS = "&tags=true";
private long updated; private long updated;
public HeadDatabase() {} public HeadDatabase() {}
@ -63,6 +64,21 @@ public class HeadDatabase {
return null; return null;
} }
public List<Head> getHeadsByTag(String tag) {
List<Head> result = new ArrayList<>();
List<Head> heads = getHeads();
tag = tag.toLowerCase(Locale.ROOT);
for (Head head : heads) {
for (String t : head.getTags()) {
if (t.toLowerCase(Locale.ROOT).contains(tag)) {
result.add(head);
}
}
}
return result;
}
public List<Head> getHeadsByName(Category category, String name) { public List<Head> getHeadsByName(Category category, String name) {
List<Head> result = new ArrayList<>(); List<Head> result = new ArrayList<>();
List<Head> heads = getHeads(category); List<Head> heads = getHeads(category);
@ -114,7 +130,7 @@ public class HeadDatabase {
String line; String line;
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
URLConnection connection = new URL(URL + category.getName()).openConnection(); URLConnection connection = new URL(URL + category.getName() + TAGS).openConnection();
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setRequestProperty("User-Agent", "HeadDB"); connection.setRequestProperty("User-Agent", "HeadDB");
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
@ -130,6 +146,7 @@ public class HeadDatabase {
.withName(obj.get("name").toString()) .withName(obj.get("name").toString())
.withUUID(UUID.fromString(obj.get("uuid").toString())) .withUUID(UUID.fromString(obj.get("uuid").toString()))
.withValue(obj.get("value").toString()) .withValue(obj.get("value").toString())
.withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None")
.withCategory(category); .withCategory(category);
id++; id++;
@ -162,8 +179,8 @@ public class HeadDatabase {
} }
public boolean isLastUpdateOld() { public boolean isLastUpdateOld() {
if (HeadDB.getCfg() == null && getLastUpdate() >= 3600) return true; if (HeadDB.getInstance().getCfg() == null && getLastUpdate() >= 3600) return true;
return getLastUpdate() >= HeadDB.getCfg().getLong("refresh"); return getLastUpdate() >= HeadDB.getInstance().getCfg().getLong("refresh");
} }
} }

Datei anzeigen

@ -96,6 +96,31 @@ public class InventoryUtils {
pane.open(player); pane.open(player);
} }
public static void openTagSearchDatabase(Player player, String tag) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &eTag Search: " + tag));
List<Head> heads = HeadAPI.getHeadsByTag(tag);
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.addFavoriteHead(player.getUniqueId(), head.getId());
Utils.sendMessage(player, "Added &e" + head.getName() + " &7to favorites.");
}
}));
}
pane.open(player);
}
public static void openCategoryDatabase(Player player, Category category) { public static void openCategoryDatabase(Player player, Category category) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &e" + category.getName())); PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &e" + category.getName()));

Datei anzeigen

@ -13,7 +13,7 @@ public class JoinListener implements Listener {
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent e) { public void onJoin(PlayerJoinEvent e) {
HeadDB.getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName()); HeadDB.getInstance().getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName());
} }
} }

Datei anzeigen

@ -35,7 +35,7 @@ public class Log {
} }
public static void log(LogLevel level, String message) { public static void log(LogLevel level, String message) {
if (level == LogLevel.DEBUG && !HeadDB.getCfg().getBoolean("debug")) { if (level == LogLevel.DEBUG && !HeadDB.getInstance().getCfg().getBoolean("debug")) {
return; return;
} }
Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message)); Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message));

Datei anzeigen

@ -21,6 +21,7 @@ permissions:
headdb.give: true headdb.give: true
headdb.favorites: true headdb.favorites: true
headdb.local: true headdb.local: true
headdb.tagsearch: true
headdb.open: headdb.open:
default: op default: op
headdb.search: headdb.search:
@ -31,3 +32,5 @@ permissions:
default: op default: op
headdb.local: headdb.local:
default: op default: op
headdb.tagsearch:
default: op