3
0
Mirror von https://github.com/TheSilentPro/HeadDB.git synchronisiert 2024-12-26 19:02:39 +01:00
Dieser Commit ist enthalten in:
Silent 2020-12-08 22:22:41 +01:00
Ursprung a69d3a07de
Commit 6af5c2684b
11 geänderte Dateien mit 152 neuen und 264 gelöschten Zeilen

39
pom.xml
Datei anzeigen

@ -6,11 +6,11 @@
<groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId>
<version>1.3</version>
<version>2.0</version>
<packaging>jar</packaging>
<name>HeadDB</name>
<description>Thousands of heads in a GUI</description>
<description>Database with thousands of heads</description>
<repositories>
<!-- Paper Repo -->
@ -23,9 +23,15 @@
<id>mojang-repo</id>
<url>https://libraries.minecraft.net/</url>
</repository>
<!-- JitPack -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!-- Paper API -->
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
@ -45,10 +51,17 @@
<version>1.5.21</version>
<scope>provided</scope>
</dependency>
<!-- Storage Lib -->
<dependency>
<groupId>com.github.simplix-softworks</groupId>
<artifactId>simplixstorage</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -57,6 +70,28 @@
<target>8</target>
</configuration>
</plugin>
<!-- Shade -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>tsp.headdb.HeadDB</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Datei anzeigen

@ -1,12 +1,15 @@
package tsp.headdb;
import de.leonhard.storage.Config;
import de.leonhard.storage.Json;
import de.leonhard.storage.LightningBuilder;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.api.HeadAPI;
import tsp.headdb.command.Command_headdb;
import tsp.headdb.database.HeadDatabase;
import tsp.headdb.listener.JoinListener;
import tsp.headdb.listener.PagedPaneListener;
import tsp.headdb.listener.MenuListener;
import tsp.headdb.util.Config;
import tsp.headdb.util.Log;
import tsp.headdb.util.Metrics;
import tsp.headdb.util.Utils;
@ -15,16 +18,15 @@ public class HeadDB extends JavaPlugin {
private static HeadDB instance;
private static Config config;
private static Config playerdata;
private static Json playerdata;
@Override
public void onEnable() {
instance = this;
Log.info("Loading HeadDB - " + getDescription().getVersion());
saveDefaultConfig();
config = new Config("plugins/HeadDB/config.yml");
playerdata = new Config("plugins/HeadDB/playerdata.yml");
playerdata.create();
config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream();
playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson();
Log.debug("Starting metrics...");
new Metrics(this, Utils.METRICS_ID);
@ -37,8 +39,15 @@ public class HeadDB extends JavaPlugin {
Log.debug("Registering commands...");
getCommand("headdb").setExecutor(new Command_headdb());
Log.debug("Initializing Database...");
HeadDatabase.update();
if (config.getBoolean("fetchStartup")) {
if (config.getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, task -> HeadAPI.getDatabase().update());
}else {
Log.debug("Initializing Database... (SYNC)");
Bukkit.getScheduler().runTask(this, task -> HeadAPI.getDatabase().update());
}
}
Log.info("Done!");
}
@ -47,7 +56,7 @@ public class HeadDB extends JavaPlugin {
return config;
}
public static Config getPlayerdata() {
public static Json getPlayerdata() {
return playerdata;
}

Datei anzeigen

@ -22,6 +22,12 @@ public class Head {
private Category category;
private int id;
public Head() {}
public Head(int id) {
this.id = id;
}
public ItemStack getItemStack() {
Validate.notNull(name, "name must not be null!");
Validate.notNull(uuid, "uuid must not be null!");
@ -44,6 +50,7 @@ public class Head {
}
meta.setLore(Arrays.asList(
Utils.colorize("&cID: " + id),
" ",
Utils.colorize("&8Right-Click to add/remove from favorites.")
));
item.setItemMeta(meta);
@ -55,85 +62,45 @@ public class Head {
return name;
}
public void setName(String name) {
this.name = name;
}
public UUID getUUID() {
return uuid;
}
public void setUUID(UUID uuid) {
this.uuid = uuid;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static class Builder {
private String name;
private UUID uuid;
private String value;
private Category category;
private int id;
public Builder withName(String name) {
public Head withName(String name) {
this.name = name;
return this;
}
public Builder withUUID(UUID uuid) {
public Head withUUID(UUID uuid) {
this.uuid = uuid;
return this;
}
public Builder withValue(String value) {
public Head withValue(String value) {
this.value = value;
return this;
}
public Builder withCategory(Category category) {
public Head withCategory(Category category) {
this.category = category;
return this;
}
public Builder withId(int id) {
public Head withId(int id) {
this.id = id;
return this;
}
public Head build() {
Head head = new Head();
head.setName(name);
head.setUUID(uuid);
head.setValue(value);
head.setCategory(category);
head.setId(id);
return head;
}
}
}

Datei anzeigen

@ -8,6 +8,7 @@ import tsp.headdb.database.Category;
import tsp.headdb.database.HeadDatabase;
import tsp.headdb.inventory.InventoryUtils;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -20,6 +21,17 @@ import java.util.UUID;
*/
public class HeadAPI {
private static final HeadDatabase database = new HeadDatabase();
/**
* Retrieves the main {@link HeadDatabase}
*
* @return Head Database
*/
public static HeadDatabase getDatabase() {
return database;
}
/**
* Opens the database for a player
*
@ -55,8 +67,9 @@ public class HeadAPI {
* @param id The ID of the head
* @return The head
*/
@Nullable
public static Head getHeadByID(int id) {
return HeadDatabase.getHeadByID(id);
return database.getHeadByID(id);
}
/**
@ -65,8 +78,9 @@ public class HeadAPI {
* @param uuid The UUID of the head
* @return The head
*/
@Nullable
public static Head getHeadByUUID(UUID uuid) {
return HeadDatabase.getHeadByUUID(uuid);
return database.getHeadByUUID(uuid);
}
/**
@ -76,7 +90,7 @@ public class HeadAPI {
* @return List of heads
*/
public static List<Head> getHeadsByName(String name) {
return HeadDatabase.getHeadsByName(name);
return database.getHeadsByName(name);
}
/**
@ -87,7 +101,7 @@ public class HeadAPI {
* @return List of heads
*/
public static List<Head> getHeadsByName(Category category, String name) {
return HeadDatabase.getHeadsByName(category, name);
return database.getHeadsByName(category, name);
}
/**
@ -96,8 +110,9 @@ public class HeadAPI {
* @param value The texture value
* @return The head
*/
@Nullable
public static Head getHeadByValue(String value) {
return HeadDatabase.getHeadByValue(value);
return database.getHeadByValue(value);
}
/**
@ -107,7 +122,7 @@ public class HeadAPI {
* @return List of heads
*/
public static List<Head> getHeads(Category category) {
return HeadDatabase.getHeads(category);
return database.getHeads(category);
}
/**
@ -116,7 +131,7 @@ public class HeadAPI {
* @return List of all heads
*/
public static List<Head> getHeads() {
return HeadDatabase.getHeads();
return database.getHeads();
}
/**
@ -126,12 +141,11 @@ public class HeadAPI {
* @param id The ID of the head
*/
public static void addFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
List<Integer> favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
if (!favs.contains(id)) {
favs.add(id);
}
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getPlayerdata().save();
}
/**
@ -141,7 +155,7 @@ public class HeadAPI {
* @param id The ID of the head
*/
public static void removeFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
List<Integer> favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
for (int i = 0; i < favs.size(); i++) {
if (favs.get(i) == id) {
favs.remove(i);
@ -149,7 +163,6 @@ public class HeadAPI {
}
}
HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getPlayerdata().save();
}
/**
@ -160,7 +173,7 @@ public class HeadAPI {
*/
public static List<Head> getFavoriteHeads(UUID uuid) {
List<Head> heads = new ArrayList<>();
List<Integer> ids = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites");
List<Integer> ids = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
for (int id : ids) {
Head head = getHeadByID(id);
heads.add(head);
@ -177,12 +190,10 @@ public class HeadAPI {
*/
public static List<LocalHead> getLocalHeads() {
List<LocalHead> heads = new ArrayList<>();
for (String key : HeadDB.getPlayerdata().getKeys(false)) {
for (String key : HeadDB.getPlayerdata().keySet()) {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key));
heads.add(new LocalHead.Builder()
.withUUID(player.getUniqueId())
.withName(player.getName())
.build());
heads.add(new LocalHead(player.getUniqueId())
.withName(player.getName()));
}
return heads;
@ -192,7 +203,7 @@ public class HeadAPI {
* Update the Head Database
*/
public static void updateDatabase() {
HeadDatabase.update();
database.update();
}
}

Datei anzeigen

@ -39,39 +39,18 @@ public class LocalHead {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static class Builder {
private UUID uuid;
private String name;
public Builder withUUID(UUID uuid) {
public LocalHead withUUID(UUID uuid) {
this.uuid = uuid;
return this;
}
public Builder withName(String name) {
public LocalHead withName(String name) {
this.name = name;
return this;
}
public LocalHead build() {
LocalHead head = new LocalHead(uuid);
head.setName(name);
return head;
}
}
}

Datei anzeigen

@ -36,6 +36,7 @@ public class Command_headdb implements CommandExecutor {
if (sub.equalsIgnoreCase("info") || sub.equalsIgnoreCase("i")) {
Utils.sendMessage(sender, "Running &cHeadDB v" + HeadDB.getInstance().getDescription().getVersion());
Utils.sendMessage(sender, "Created by &c" + HeadDB.getInstance().getDescription().getAuthors());
Utils.sendMessage(sender, "There are currently &c" + HeadAPI.getHeads().size() + " &7heads in the database.");
return true;
}
@ -56,7 +57,10 @@ public class Command_headdb implements CommandExecutor {
StringBuilder builder = new StringBuilder();
for (int i = 1; i < args.length; i++) {
builder.append(args[i]).append(" ");
builder.append(args[i]);
if (i != args.length - 1) {
builder.append(" ");
}
}
String name = builder.toString();
Utils.sendMessage(sender, "Searching for &e" + name);

Datei anzeigen

@ -1,5 +1,6 @@
package tsp.headdb.database;
import org.bukkit.ChatColor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -23,11 +24,13 @@ import java.util.concurrent.TimeUnit;
*/
public class HeadDatabase {
private static final Map<Category, List<Head>> HEADS = new HashMap<>();
private static final String URL = "https://minecraft-heads.com/scripts/api.php?cat=";
private static long updated;
private final Map<Category, List<Head>> HEADS = new HashMap<>();
private final String URL = "https://minecraft-heads.com/scripts/api.php?cat=";
private long updated;
public static Head getHeadByValue(String value) {
public HeadDatabase() {}
public Head getHeadByValue(String value) {
List<Head> heads = getHeads();
for (Head head : heads) {
if (head.getValue().equals(value)) {
@ -38,7 +41,7 @@ public class HeadDatabase {
return null;
}
public static Head getHeadByID(int id) {
public Head getHeadByID(int id) {
List<Head> heads = getHeads();
for (Head head : heads) {
if (head.getId() == id) {
@ -49,7 +52,7 @@ public class HeadDatabase {
return null;
}
public static Head getHeadByUUID(UUID uuid) {
public Head getHeadByUUID(UUID uuid) {
List<Head> heads = getHeads();
for (Head head : heads) {
if (head.getUUID().equals(uuid)) {
@ -60,11 +63,12 @@ public class HeadDatabase {
return null;
}
public static List<Head> getHeadsByName(Category category, String name) {
public List<Head> getHeadsByName(Category category, String name) {
List<Head> result = new ArrayList<>();
List<Head> heads = getHeads(category);
for (Head head : heads) {
if (head.getName().toLowerCase().contains(name.toLowerCase())) {
String hName = ChatColor.stripColor(head.getName().toLowerCase(Locale.ROOT));
if (hName.contains(ChatColor.stripColor(name.toLowerCase(Locale.ROOT)))) {
result.add(head);
}
}
@ -72,23 +76,20 @@ public class HeadDatabase {
return result;
}
public static List<Head> getHeadsByName(String name) {
public List<Head> getHeadsByName(String name) {
List<Head> result = new ArrayList<>();
List<Head> heads = getHeads();
for (Head head : heads) {
if (head.getName().toLowerCase().contains(name.toLowerCase())) {
result.add(head);
}
for (Category category : Category.values()) {
result.addAll(getHeadsByName(category, name));
}
return result;
}
public static List<Head> getHeads(Category category) {
public List<Head> getHeads(Category category) {
return HEADS.get(category);
}
public static List<Head> getHeads() {
public List<Head> getHeads() {
if (!HEADS.isEmpty() && !isLastUpdateOld()) {
List<Head> heads = new ArrayList<>();
for (Category category : HEADS.keySet()) {
@ -101,7 +102,7 @@ public class HeadDatabase {
return getHeads();
}
public static Map<Category, List<Head>> getHeadsNoCache() {
public Map<Category, List<Head>> getHeadsNoCache() {
Map<Category, List<Head>> result = new HashMap<>();
List<Category> categories = Category.getCategories();
@ -116,9 +117,7 @@ public class HeadDatabase {
URLConnection connection = new URL(URL + category.getName()).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestProperty("User-Agent", "HeadDB");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()))) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
while ((line = in.readLine()) != null) {
response.append(line);
}
@ -127,13 +126,11 @@ public class HeadDatabase {
JSONArray array = (JSONArray) parser.parse(response.toString());
for (Object o : array) {
JSONObject obj = (JSONObject) o;
Head head = new Head.Builder()
Head head = new Head(id)
.withName(obj.get("name").toString())
.withUUID(UUID.fromString(obj.get("uuid").toString()))
.withValue(obj.get("value").toString())
.withCategory(category)
.withId(id)
.build();
.withCategory(category);
id++;
heads.add(head);
@ -150,7 +147,7 @@ public class HeadDatabase {
return result;
}
public static void update() {
public void update() {
Map<Category, List<Head>> heads = getHeadsNoCache();
HEADS.clear();
for (Map.Entry<Category, List<Head>> entry : heads.entrySet()) {
@ -158,13 +155,13 @@ public class HeadDatabase {
}
}
public static long getLastUpdate() {
public long getLastUpdate() {
long now = System.nanoTime();
long elapsed = now - updated;
return TimeUnit.NANOSECONDS.toSeconds(elapsed);
}
public static boolean isLastUpdateOld() {
public boolean isLastUpdateOld() {
if (HeadDB.getCfg() == null && getLastUpdate() >= 3600) return true;
return getLastUpdate() >= HeadDB.getCfg().getLong("refresh");
}

Datei anzeigen

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

Datei anzeigen

@ -1,119 +0,0 @@
package tsp.headdb.util;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
/**
* @author TheSilentPro
*/
public class Config {
private final File file;
private FileConfiguration config;
public Config(File file) {
this.file = file;
this.config = YamlConfiguration.loadConfiguration(file);
}
public Config(String path) {
this.file = new File(path);
this.config = YamlConfiguration.loadConfiguration(file);
}
public Config(Config config) {
this.file = config.getFile();
this.config = config.getConfig();
}
public String getString(String path) {
return Utils.colorize(config.getString(path));
}
public boolean getBoolean(String path) {
return config.getBoolean(path);
}
public List<String> getStringList(String path) {
return config.getStringList(path);
}
public int getInt(String path) {
return config.getInt(path);
}
public List<Integer> getIntList(String path) {
return config.getIntegerList(path);
}
public long getLong(String path) {
return config.getLong(path);
}
public Set<String> getKeys(boolean deep) {
return config.getKeys(deep);
}
public ConfigurationSection getConfigurationSection(String path) {
return config.getConfigurationSection(path);
}
public Set<String> getKeys(ConfigurationSection configurationSection, boolean b) {
return configurationSection.getKeys(b);
}
public void set(String path, Object value) {
config.set(path, value);
}
public Object get(String path) {
return config.get(path);
}
public FileConfiguration getConfig() {
return config;
}
public File getFile() {
return file;
}
public boolean exists() {
return file.exists();
}
public void reload() {
this.config = YamlConfiguration.loadConfiguration(file);
}
public boolean create() {
if (!file.exists()) {
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public boolean save() {
try {
config.save(file);
return true;
} catch (IOException e) {
Log.error("Failed to save " + file.getName() + " | Stack Trace:");
e.printStackTrace();
}
return false;
}
}

Datei anzeigen

@ -1,3 +1,9 @@
# When enabled heads will be fetched from startup, otherwise when the /hdb command is ran
fetchStartup: true
# When enabled, heads will be fetched async (Startup Only)
asyncStartup: false
# If the cached heads are older than these amount of seconds, the plugin will refresh the database
refresh: 3600

Datei anzeigen

@ -1,8 +1,8 @@
name: HeadDB
description: Adds a menu with thousands of heads
description: Database with thousands of heads
main: tsp.headdb.HeadDB
version: 1.3
version: 2.0
api-version: 1.16
author: Silent