3
0
Mirror von https://github.com/TheSilentPro/HeadDB.git synchronisiert 2024-12-27 11:20:05 +01:00

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

Datei anzeigen

@ -11,7 +11,9 @@ import tsp.headdb.util.Log;
import tsp.headdb.util.Utils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class Head {
@ -21,6 +23,7 @@ public class Head {
private String value;
private Category category;
private int id;
private List<String> tags;
public Head() {}
@ -50,7 +53,8 @@ public class Head {
}
meta.setLore(Arrays.asList(
Utils.colorize("&cID: " + id),
" ",
Utils.colorize("&e" + buildTagLore((String[]) tags.toArray())),
"",
Utils.colorize("&8Right-Click to add/remove from favorites.")
));
item.setItemMeta(meta);
@ -78,6 +82,10 @@ public class Head {
return id;
}
public List<String> getTags() {
return tags;
}
public Head withName(String name) {
this.name = name;
return this;
@ -102,5 +110,22 @@ public class Head {
this.id = id;
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);
}
public static List<Head> getHeadsByTag(String tag) {
return database.getHeadsByTag(tag);
}
/**
* 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
*/
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)) {
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
*/
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++) {
if (favs.get(i) == id) {
favs.remove(i);
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) {
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) {
Head head = getHeadByID(id);
heads.add(head);
@ -192,7 +196,7 @@ public final class HeadAPI {
*/
public static List<LocalHead> getLocalHeads() {
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));
heads.add(new LocalHead(player.getUniqueId())
.withName(player.getName()));

Datei anzeigen

@ -68,6 +68,27 @@ public class Command_headdb implements CommandExecutor {
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 (!sender.hasPermission("headdb.give")) {
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 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 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, " ");
return true;

Datei anzeigen

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

Datei anzeigen

@ -96,6 +96,31 @@ public class InventoryUtils {
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) {
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
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) {
if (level == LogLevel.DEBUG && !HeadDB.getCfg().getBoolean("debug")) {
if (level == LogLevel.DEBUG && !HeadDB.getInstance().getCfg().getBoolean("debug")) {
return;
}
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.favorites: true
headdb.local: true
headdb.tagsearch: true
headdb.open:
default: op
headdb.search:
@ -30,4 +31,6 @@ permissions:
headdb.favorites:
default: op
headdb.local:
default: op
headdb.tagsearch:
default: op