Internal (& API) changes/updates

Dieser Commit ist enthalten in:
Silent 2021-07-13 20:18:16 +02:00
Ursprung bc8fcbd33e
Commit 7fceb6ca59
9 geänderte Dateien mit 214 neuen und 42 gelöschten Zeilen

Datei anzeigen

@ -38,6 +38,16 @@ public class HeadDB extends JavaPlugin {
} }
} }
if (storage.getConfig().getBoolean("fetchStartup")) {
if (storage.getConfig().getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update());
} else {
Log.debug("Initializing Database... (SYNC)");
HeadAPI.getDatabase().update();
}
}
Log.debug("Starting metrics..."); Log.debug("Starting metrics...");
new Metrics(this, Utils.METRICS_ID); new Metrics(this, Utils.METRICS_ID);
@ -49,16 +59,6 @@ public class HeadDB extends JavaPlugin {
Log.debug("Registering commands..."); Log.debug("Registering commands...");
getCommand("headdb").setExecutor(new Command_headdb()); getCommand("headdb").setExecutor(new Command_headdb());
if (storage.getConfig().getBoolean("fetchStartup")) {
if (storage.getConfig().getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update());
} else {
Log.debug("Initializing Database... (SYNC)");
HeadAPI.getDatabase().update();
}
}
Log.info("Done!"); Log.info("Done!");
} }

Datei anzeigen

@ -23,7 +23,7 @@ public final class HeadAPI {
private HeadAPI() {} private HeadAPI() {}
private static final HeadDatabase database = new HeadDatabase(); private static final HeadDatabase database = new HeadDatabase(HeadDB.getInstance());
/** /**
* Retrieves the main {@link HeadDatabase} * Retrieves the main {@link HeadDatabase}
@ -49,7 +49,7 @@ public final class HeadAPI {
* @param player Target player * @param player Target player
* @param category Category to open * @param category Category to open
*/ */
public static void openDatabase(Player player, Category category) { public static void openCategoryDatabase(Player player, Category category) {
InventoryUtils.openCategoryDatabase(player, category); InventoryUtils.openCategoryDatabase(player, category);
} }
@ -59,10 +59,14 @@ public final class HeadAPI {
* @param player Target player * @param player Target player
* @param search Search term * @param search Search term
*/ */
public static void openDatabase(Player player, String search) { public static void openSearchDatabase(Player player, String search) {
InventoryUtils.openSearchDatabase(player, search); InventoryUtils.openSearchDatabase(player, search);
} }
public static void openTagSearchDatabase(Player player, String tag) {
InventoryUtils.openTagSearchDatabase(player, tag);
}
/** /**
* Retrieve a {@link Head} by it's ID * Retrieve a {@link Head} by it's ID
* *

Datei anzeigen

@ -5,17 +5,22 @@ import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import tsp.headdb.database.Category;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class LocalHead { public class LocalHead extends Head {
private UUID uuid; private UUID uuid;
private String name; private String name;
public LocalHead(UUID uuid) {
this.uuid = uuid;
}
public ItemStack getItemStack() { public ItemStack getItemStack() {
Validate.notNull(uuid, "uuid must not be null!"); Validate.notNull(uuid, "uuid must not be null!");
@ -31,10 +36,6 @@ public class LocalHead {
return item; return item;
} }
public LocalHead(UUID uuid) {
this.uuid = uuid;
}
public UUID getUniqueId() { public UUID getUniqueId() {
return uuid; return uuid;
} }
@ -43,6 +44,26 @@ public class LocalHead {
return name; return name;
} }
@Override
public String getValue() {
return null;
}
@Override
public Category getCategory() {
return null;
}
@Override
public int getId() {
return -1;
}
@Override
public List<String> getTags() {
return null;
}
public LocalHead withUniqueId(UUID uuid) { public LocalHead withUniqueId(UUID uuid) {
this.uuid = uuid; this.uuid = uuid;
return this; return this;

Datei anzeigen

@ -28,7 +28,7 @@ public class Command_headdb implements CommandExecutor {
Player player = (Player) sender; Player player = (Player) sender;
Utils.sendMessage(player, "Opening &cHead Database"); Utils.sendMessage(player, "Opening &cHead Database");
InventoryUtils.openDatabase(player); HeadAPI.openDatabase(player);
return true; return true;
} }
String sub = args[0]; String sub = args[0];
@ -64,7 +64,7 @@ public class Command_headdb implements CommandExecutor {
} }
String name = builder.toString(); String name = builder.toString();
Utils.sendMessage(sender, "Searching for &e" + name); Utils.sendMessage(sender, "Searching for &e" + name);
InventoryUtils.openSearchDatabase(player, name); HeadAPI.openSearchDatabase(player, name);
return true; return true;
} }
@ -85,7 +85,7 @@ public class Command_headdb implements CommandExecutor {
String tag = args[1]; String tag = args[1];
Utils.sendMessage(sender, "Searching for heads with tag &e" + tag); Utils.sendMessage(sender, "Searching for heads with tag &e" + tag);
InventoryUtils.openTagSearchDatabase(player, tag); HeadAPI.openTagSearchDatabase(player, tag);
return true; return true;
} }

Datei anzeigen

@ -1,12 +1,14 @@
package tsp.headdb.database; package tsp.headdb.database;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import tsp.headdb.HeadDB;
import tsp.headdb.api.Head; import tsp.headdb.api.Head;
import tsp.headdb.event.HeadDatabaseUpdateEvent;
import tsp.headdb.util.Log; import tsp.headdb.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -25,12 +27,23 @@ import javax.annotation.Nullable;
*/ */
public class HeadDatabase { public class HeadDatabase {
private final JavaPlugin plugin;
private final long refresh;
private int timeout;
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 TAGS = "&tags=true";
private long updated; private long updated;
public HeadDatabase() {} public HeadDatabase(JavaPlugin plugin) {
this.plugin = plugin;
this.refresh = 3600;
this.timeout = 5000;
}
public HeadDatabase(JavaPlugin plugin, long refresh) {
this.plugin = plugin;
this.refresh = refresh;
this.timeout = 5000;
}
public Head getHeadByValue(String value) { public Head getHeadByValue(String value) {
List<Head> heads = getHeads(); List<Head> heads = getHeads();
@ -138,9 +151,9 @@ public class HeadDatabase {
String line; String line;
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
URLConnection connection = new URL(URL + category.getName() + TAGS).openConnection(); URLConnection connection = new URL("https://minecraft-heads.com/scripts/api.php?cat=" + category.getName() + "&tags=true").openConnection();
connection.setConnectTimeout(5000); connection.setConnectTimeout(timeout);
connection.setRequestProperty("User-Agent", "HeadDB"); connection.setRequestProperty("User-Agent", plugin.getName() + "-DatabaseUpdater");
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
response.append(line); response.append(line);
@ -194,6 +207,7 @@ public class HeadDatabase {
for (Map.Entry<Category, List<Head>> entry : heads.entrySet()) { for (Map.Entry<Category, List<Head>> entry : heads.entrySet()) {
HEADS.put(entry.getKey(), entry.getValue()); HEADS.put(entry.getKey(), entry.getValue());
} }
Bukkit.getPluginManager().callEvent(new HeadDatabaseUpdateEvent(this));
return true; return true;
} }
@ -204,8 +218,23 @@ public class HeadDatabase {
} }
public boolean isLastUpdateOld() { public boolean isLastUpdateOld() {
if (HeadDB.getInstance().getConfiguration() == null && getLastUpdate() >= 3600) return true; return getLastUpdate() >= refresh;
return getLastUpdate() >= HeadDB.getInstance().getConfiguration().getLong("refresh"); }
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getTimeout() {
return timeout;
}
public long getRefresh() {
return refresh;
}
public JavaPlugin getPlugin() {
return plugin;
} }
} }

Datei anzeigen

@ -0,0 +1,38 @@
package tsp.headdb.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import tsp.headdb.database.HeadDatabase;
public class HeadDatabaseUpdateEvent extends Event implements Cancellable {
private final HandlerList handlerList = new HandlerList();
private boolean cancelled;
private final HeadDatabase database;
public HeadDatabaseUpdateEvent(HeadDatabase database) {
this.database = database;
}
public HeadDatabase getDatabase() {
return database;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
@Override
public @NotNull HandlerList getHandlers() {
return handlerList;
}
}

Datei anzeigen

@ -0,0 +1,64 @@
package tsp.headdb.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import tsp.headdb.api.Head;
public class PlayerHeadPurchaseEvent extends Event implements Cancellable {
private final HandlerList handlerList = new HandlerList();
private boolean cancelled;
private Player player;
private Head head;
private double cost;
public PlayerHeadPurchaseEvent(Player player, Head head, double cost) {
this.player = player;
this.head = head;
this.cost = cost;
}
public Player getPlayer() {
return player;
}
public Head getHead() {
return head;
}
public double getCost() {
return cost;
}
public void setPlayer(Player player) {
this.player = player;
}
public void setHead(Head head) {
this.head = head;
}
public void setCost(double cost) {
this.cost = cost;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
@Override
public @NotNull HandlerList getHandlers() {
return handlerList;
}
}

Datei anzeigen

@ -13,6 +13,7 @@ import tsp.headdb.api.Head;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
import tsp.headdb.api.LocalHead; import tsp.headdb.api.LocalHead;
import tsp.headdb.database.Category; import tsp.headdb.database.Category;
import tsp.headdb.event.PlayerHeadPurchaseEvent;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@ -80,11 +81,11 @@ public class InventoryUtils {
for (LocalHead localHead : heads) { for (LocalHead localHead : heads) {
pane.addButton(new Button(localHead.getItemStack(), e -> { pane.addButton(new Button(localHead.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) { if (e.getClick() == ClickType.SHIFT_LEFT) {
purchaseItem(player, localHead.getItemStack(), 64, "local", localHead.getName()); purchaseHead(player, localHead, 64, "local", localHead.getName());
return; return;
} }
if (e.getClick() == ClickType.LEFT) { if (e.getClick() == ClickType.LEFT) {
purchaseItem(player, localHead.getItemStack(), 1, "local", localHead.getName()); purchaseHead(player, localHead, 1, "local", localHead.getName());
return; return;
} }
if (e.getClick() == ClickType.RIGHT) { if (e.getClick() == ClickType.RIGHT) {
@ -104,11 +105,11 @@ public class InventoryUtils {
for (Head head : heads) { for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> { pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) { if (e.getClick() == ClickType.SHIFT_LEFT) {
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
return; return;
} }
if (e.getClick() == ClickType.LEFT) { if (e.getClick() == ClickType.LEFT) {
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
} }
if (e.getClick() == ClickType.RIGHT) { if (e.getClick() == ClickType.RIGHT) {
HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId()); HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId());
@ -128,11 +129,11 @@ public class InventoryUtils {
for (Head head : heads) { for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> { pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) { if (e.getClick() == ClickType.SHIFT_LEFT) {
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
return; return;
} }
if (e.getClick() == ClickType.LEFT) { if (e.getClick() == ClickType.LEFT) {
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
} }
if (e.getClick() == ClickType.RIGHT) { if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -152,11 +153,11 @@ public class InventoryUtils {
for (Head head : heads) { for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> { pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) { if (e.getClick() == ClickType.SHIFT_LEFT) {
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
return; return;
} }
if (e.getClick() == ClickType.LEFT) { if (e.getClick() == ClickType.LEFT) {
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
} }
if (e.getClick() == ClickType.RIGHT) { if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -175,11 +176,11 @@ public class InventoryUtils {
for (Head head : heads) { for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> { pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) { if (e.getClick() == ClickType.SHIFT_LEFT) {
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
return; return;
} }
if (e.getClick() == ClickType.LEFT) { if (e.getClick() == ClickType.LEFT) {
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
} }
if (e.getClick() == ClickType.RIGHT) { if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -303,10 +304,23 @@ public class InventoryUtils {
return true; return true;
} }
@Deprecated
public static void purchaseItem(Player player, ItemStack item, int amount, String category, String description) { public static void purchaseItem(Player player, ItemStack item, int amount, String category, String description) {
if (!processPayment(player, amount, category, description)) return; if (!processPayment(player, amount, category, description)) return;
item.setAmount(amount); item.setAmount(amount);
player.getInventory().addItem(item); player.getInventory().addItem(item);
} }
public static void purchaseHead(Player player, Head head, int amount, String category, String description) {
if (!processPayment(player, amount, category, description)) return;
PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category));
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
ItemStack item = head.getItemStack();
item.setAmount(amount);
player.getInventory().addItem(item);
}
}
} }

Datei anzeigen

@ -11,8 +11,10 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.HeadDB; import tsp.headdb.HeadDB;
import tsp.headdb.api.HeadAPI;
import tsp.headdb.database.Category; import tsp.headdb.database.Category;
import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.inventory.InventoryUtils;
import tsp.headdb.util.Log;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
public class MenuListener implements Listener { public class MenuListener implements Listener {
@ -71,7 +73,7 @@ public class MenuListener implements Listener {
Category category = Category.getByName(name); Category category = Category.getByName(name);
if (category != null) { if (category != null) {
InventoryUtils.openCategoryDatabase(player, category); HeadAPI.openCategoryDatabase(player, category);
} }
} }
} }