Merge pull request #7 from TheSilentPro/dev

Dev
Dieser Commit ist enthalten in:
Silent 2021-04-21 19:04:48 +02:00 committet von GitHub
Commit cfd2ed9368
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
14 geänderte Dateien mit 462 neuen und 340 gelöschten Zeilen

20
pom.xml
Datei anzeigen

@ -6,7 +6,7 @@
<groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId>
<version>2.2</version>
<version>2.3</version>
<packaging>jar</packaging>
<name>HeadDB</name>
@ -28,6 +28,11 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- CodeMC -->
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories>
<dependencies>
@ -57,9 +62,22 @@
<artifactId>simplixstorage</artifactId>
<version>3.2.0</version>
</dependency>
<!-- AnvilGUI -->
<dependency>
<groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- Compiler -->
<plugin>

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,21 +42,21 @@ 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)");
Bukkit.getScheduler().runTask(this, task -> HeadAPI.getDatabase().update());
HeadAPI.getDatabase().update();
}
}
Log.info("Done!");
}
public static Config getCfg() {
public Config getCfg() {
return config;
}
public static Json getPlayerdata() {
public Json getPlayerdata() {
return playerdata;
}

Datei anzeigen

@ -3,15 +3,16 @@ package tsp.headdb.api;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import tsp.headdb.database.Category;
import tsp.headdb.util.Log;
import tsp.headdb.util.Utils;
import tsp.headdb.util.XMaterial;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class Head {
@ -21,6 +22,7 @@ public class Head {
private String value;
private Category category;
private int id;
private List<String> tags;
public Head() {}
@ -33,11 +35,11 @@ public class Head {
Validate.notNull(uuid, "uuid must not be null!");
Validate.notNull(value, "value must not be null!");
ItemStack item = new ItemStack(Material.PLAYER_HEAD);
ItemStack item = new ItemStack(XMaterial.PLAYER_HEAD.parseItem());
SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setDisplayName(Utils.colorize(category != null ? category.getColor() + name : "&8" + name));
// set skull owner
GameProfile profile = new GameProfile(uuid, name);
GameProfile profile = new GameProfile(uuid, null);
profile.getProperties().put("textures", new Property("textures", value));
Field profileField;
try {
@ -50,7 +52,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 +81,10 @@ public class Head {
return id;
}
public List<String> getTags() {
return tags;
}
public Head withName(String name) {
this.name = name;
return this;
@ -102,5 +109,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

@ -19,7 +19,9 @@ import java.util.UUID;
*
* @author TheSilentPro
*/
public class HeadAPI {
public final class HeadAPI {
private HeadAPI() {}
private static final HeadDatabase database = new HeadDatabase();
@ -83,6 +85,10 @@ public 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
*
@ -141,11 +147,11 @@ public 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);
}
/**
@ -155,14 +161,14 @@ public 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);
}
/**
@ -173,7 +179,7 @@ public 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);
@ -190,7 +196,7 @@ public 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

@ -2,10 +2,10 @@ package tsp.headdb.api;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import tsp.headdb.util.Utils;
import tsp.headdb.util.XMaterial;
import java.util.ArrayList;
import java.util.List;
@ -19,7 +19,7 @@ public class LocalHead {
public ItemStack getItemStack() {
Validate.notNull(uuid, "uuid must not be null!");
ItemStack item = new ItemStack(Material.PLAYER_HEAD);
ItemStack item = new ItemStack(XMaterial.PLAYER_HEAD.parseItem());
SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setOwningPlayer(Bukkit.getOfflinePlayer(uuid));
meta.setDisplayName(Utils.colorize("&e" + name));

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()))) {
@ -132,6 +148,7 @@ public class HeadDatabase {
.withName(obj.get("name").toString())
.withUUID(uuid.isEmpty() ? UUID.randomUUID() : UUID.fromString(uuid))
.withValue(obj.get("value").toString())
.withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None")
.withCategory(category);
id++;
@ -164,8 +181,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

@ -71,7 +71,7 @@ public class InventoryUtils {
pane.open(player);
}
public static void openSearchDatabase(Player player, String search) {
public static PagedPane openSearchDatabase(Player player, String search) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &eSearch: " + search));
List<Head> heads = HeadAPI.getHeadsByName(search);
@ -93,6 +93,32 @@ public class InventoryUtils {
}));
}
pane.open(player);
return pane;
}
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);
}
@ -143,6 +169,13 @@ public class InventoryUtils {
"&8Click to view your favorites")
);
inventory.setItem(40, buildButton(
XMaterial.DARK_OAK_SIGN.parseItem(),
"&9Search",
"",
"&8Click to open search menu"
));
inventory.setItem(41, buildButton(
XMaterial.COMPASS.parseItem(),
"&aLocal",
@ -155,7 +188,7 @@ public class InventoryUtils {
public static void fill(Inventory inv, ItemStack item) {
int size = inv.getSize();
int[] ignored = new int[]{20, 21, 22, 23, 24, 29, 30, 31, 32, 33, 39, 41};
int[] ignored = new int[]{20, 21, 22, 23, 24, 29, 30, 31, 32, 33, 39, 40, 41};
// Fill
for (int i = 0; i < size; i++) {

Datei anzeigen

@ -1,15 +1,18 @@
package tsp.headdb.inventory;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import tsp.headdb.HeadDB;
import tsp.headdb.api.HeadAPI;
import tsp.headdb.util.Utils;
import tsp.headdb.util.XMaterial;
@ -245,9 +248,38 @@ public class PagedPane implements InventoryHolder {
"&3&lPage &a&l%d &7/ &c&l%d",
getCurrentPage(), getPageAmount()
);
String lore = "&7Click to go to the &cMain Menu";
ItemStack itemStack = setMeta(HeadAPI.getHeadByValue("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2Q5MWY1MTI2NmVkZGM2MjA3ZjEyYWU4ZDdhNDljNWRiMDQxNWFkYTA0ZGFiOTJiYjc2ODZhZmRiMTdmNGQ0ZSJ9fX0=").getItemStack(), name, lore);
controlMain = new Button(itemStack, event -> InventoryUtils.openDatabase((Player) event.getWhoClicked()));
ItemStack itemStack = setMeta(HeadAPI.getHeadByValue("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2Q5MWY1MTI2NmVkZGM2MjA3ZjEyYWU4ZDdhNDljNWRiMDQxNWFkYTA0ZGFiOTJiYjc2ODZhZmRiMTdmNGQ0ZSJ9fX0=").getItemStack(),
name,
"&7Left-Click to go to the &cMain Menu",
"&7Right-Click to go to a &6Specific Page");
controlMain = new Button(itemStack, event -> {
if (event.getClick() == ClickType.RIGHT) {
new AnvilGUI.Builder()
.onComplete((player, text) -> {
try {
int i = Integer.parseInt(text);
if (i > getPageAmount()) {
Utils.sendMessage(player, "&cPage number is out of bounds! Max: &e" + getPageAmount());
return AnvilGUI.Response.text("&cOut of bounds!");
}
Bukkit.getScheduler().runTaskLater(HeadDB.getInstance(), () -> {
open(player);
selectPage(i - 1);
}, 40L);
return AnvilGUI.Response.close();
} catch (NumberFormatException nfe) {
Utils.sendMessage(player, "&cValue must be a number!");
return AnvilGUI.Response.text(Utils.colorize("&cValue must be a number!"));
}
})
.title("Select Page")
.text("Page number...")
.plugin(HeadDB.getInstance())
.open((Player) event.getWhoClicked());
} else {
InventoryUtils.openDatabase((Player) event.getWhoClicked());
}
});
inventory.setItem(inventory.getSize() - 5, itemStack);
}
}

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

@ -1,5 +1,6 @@
package tsp.headdb.listener;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -8,6 +9,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.HeadDB;
import tsp.headdb.database.Category;
import tsp.headdb.inventory.InventoryUtils;
import tsp.headdb.util.Utils;
@ -53,6 +55,18 @@ public class MenuListener implements Listener {
InventoryUtils.openLocalMenu(player);
return;
}
if (name.equalsIgnoreCase("search")) {
new AnvilGUI.Builder()
.onComplete((p, text) -> {
InventoryUtils.openSearchDatabase(p, text);
return AnvilGUI.Response.openInventory(InventoryUtils.openSearchDatabase(p, text).getInventory());
})
.title("Search Heads")
.text("Name...")
.plugin(HeadDB.getInstance())
.open(player);
return;
}
Category category = Category.getByName(name);

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-Diff unterdrückt, da er zu groß ist Diff laden

Datei anzeigen

@ -1,9 +1,8 @@
name: HeadDB
description: Database with thousands of heads
name: ${project.name}
description: ${project.description}
main: tsp.headdb.HeadDB
version: 2.2
api-version: 1.16
version: ${project.version}
author: Silent
commands:
@ -21,6 +20,7 @@ permissions:
headdb.give: true
headdb.favorites: true
headdb.local: true
headdb.tagsearch: true
headdb.open:
default: op
headdb.search:
@ -30,4 +30,6 @@ permissions:
headdb.favorites:
default: op
headdb.local:
default: op
headdb.tagsearch:
default: op