Mirror von
https://github.com/TheSilentPro/HeadDB.git
synchronisiert 2024-12-27 11:20:05 +01:00
refactoring
Dieser Commit ist enthalten in:
Ursprung
0e81da9d7c
Commit
b3c06013de
@ -17,8 +17,6 @@ import tsp.headdb.util.Metrics;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class HeadDB extends JavaPlugin {
|
||||
|
||||
@ -95,25 +93,14 @@ public class HeadDB extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void createLocalizationFile() {
|
||||
if (getClass().getResource("messages.yml") == null || new File(getDataFolder() + "/messages.yml").exists()) {
|
||||
// File exists or not default available
|
||||
return;
|
||||
if (getClass().getResource("messages.yml") != null && !new File(getDataFolder() + "/messages.yml").exists()) {
|
||||
saveResource("messages.yml", false);
|
||||
Log.debug("Localization loaded from jar file.");
|
||||
}
|
||||
|
||||
try {
|
||||
saveResource("messages.yml", false);
|
||||
File messagesFile = new File(getDataFolder() + "/messages.yml");
|
||||
if (!messagesFile.exists()) {
|
||||
messagesFile = new File(getClass().getResource("messages.yml").toURI());
|
||||
messagesFile.createNewFile();
|
||||
}
|
||||
this.localization = new Localization(messagesFile);
|
||||
this.localization.load();
|
||||
Log.debug("Localization loaded from jar file.");
|
||||
} catch (URISyntaxException | IOException ex) {
|
||||
Log.error("Failed to load localization!");
|
||||
Log.error(ex);
|
||||
}
|
||||
File messagesFile = new File(getDataFolder() + "/messages.yml");
|
||||
this.localization = new Localization(messagesFile);
|
||||
this.localization.load();
|
||||
}
|
||||
|
||||
|
||||
|
@ -74,7 +74,11 @@ public class Head {
|
||||
if (itemStack == null) {
|
||||
itemStack = menuItem;
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
meta.setDisplayName(HeadDB.getInstance().getLocalization().getMessage("head.name"));
|
||||
meta.setDisplayName(HeadDB.getInstance().getLocalization().getMessage("head.name")
|
||||
.replace("%name%", name)
|
||||
.replace("%id%", String.valueOf(id))
|
||||
.replace("%value%", value)
|
||||
.replace("%tags%", buildTagLore(tags)));
|
||||
meta.setLore(HeadDB.getInstance().getLocalization().getData().getStringList("head.lore"));
|
||||
itemStack.setItemMeta(meta);
|
||||
}
|
||||
|
@ -50,16 +50,17 @@ public class CommandHeadDB implements CommandExecutor {
|
||||
Utils.sendMessage(sender, localization.getMessage("noPermission"));
|
||||
return true;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
Utils.sendMessage(sender, "&c/hdb search <name>");
|
||||
return true;
|
||||
}
|
||||
if (!(sender instanceof Player)) {
|
||||
Utils.sendMessage(sender, localization.getMessage("onlyPlayers"));
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length < 2) {
|
||||
Utils.sendMessage(player, "&c/hdb search <name>");
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
builder.append(args[i]);
|
||||
|
@ -34,52 +34,6 @@ public class InventoryUtils {
|
||||
private static final Map<String, Integer> uiLocation = new HashMap<>();
|
||||
private static final Map<String, ItemStack> uiItem = new HashMap<>();
|
||||
|
||||
public static int getUILocation(String category, int slot) {
|
||||
// Try to use the cached value first.
|
||||
if (uiLocation.containsKey(category)) return uiLocation.get(category);
|
||||
|
||||
// Try to get the value from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".location")) {
|
||||
uiLocation.put(category, HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".location"));
|
||||
return uiLocation.get(category);
|
||||
}
|
||||
|
||||
// No valid value in the config file, return the given default.
|
||||
uiLocation.put(category, slot);
|
||||
return slot;
|
||||
}
|
||||
|
||||
public static ItemStack getUIItem(String category, ItemStack item) {
|
||||
// Try to use the cached item first.
|
||||
if (uiItem.containsKey(category)) return uiItem.get(category);
|
||||
|
||||
// Try to get a head from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".head")) {
|
||||
int id = HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".head");
|
||||
Head head = HeadAPI.getHeadByID(id);
|
||||
if (head != null) {
|
||||
uiItem.put(category, head.getMenuItem());
|
||||
return uiItem.get(category);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get an item from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".item")) {
|
||||
String cfg = HeadDB.getInstance().getConfig().getString("ui.category." + category + ".item");
|
||||
Material mat = Material.matchMaterial(cfg);
|
||||
|
||||
// AIR is allowed as the fill material for the menu, but not as a category item.
|
||||
if (mat != null && (category.equals("fill") || mat != Material.AIR)) {
|
||||
uiItem.put(category, new ItemStack(mat));
|
||||
return uiItem.get(category);
|
||||
}
|
||||
}
|
||||
|
||||
// No valid head or item in the config file, return the given default.
|
||||
uiItem.put(category, item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public static void openLocalMenu(Player player) {
|
||||
List<LocalHead> heads = HeadAPI.getLocalHeads();
|
||||
|
||||
@ -113,7 +67,6 @@ public class InventoryUtils {
|
||||
pane.addButton(new Button(head.getMenuItem(), e -> {
|
||||
if (e.getClick() == ClickType.SHIFT_LEFT) {
|
||||
purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
|
||||
return;
|
||||
}
|
||||
if (e.getClick() == ClickType.LEFT) {
|
||||
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
|
||||
@ -139,7 +92,6 @@ public class InventoryUtils {
|
||||
pane.addButton(new Button(head.getMenuItem(), e -> {
|
||||
if (e.getClick() == ClickType.SHIFT_LEFT) {
|
||||
purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
|
||||
return;
|
||||
}
|
||||
if (e.getClick() == ClickType.LEFT) {
|
||||
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
|
||||
@ -171,7 +123,6 @@ public class InventoryUtils {
|
||||
pane.addButton(new Button(head.getMenuItem(), e -> {
|
||||
if (e.getClick() == ClickType.SHIFT_LEFT) {
|
||||
purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
|
||||
return;
|
||||
}
|
||||
if (e.getClick() == ClickType.LEFT) {
|
||||
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
|
||||
@ -202,7 +153,6 @@ public class InventoryUtils {
|
||||
pane.addButton(new Button(head.getMenuItem(), e -> {
|
||||
if (e.getClick() == ClickType.SHIFT_LEFT) {
|
||||
purchaseHead(player, head, 64, head.getCategory().getName(), head.getName());
|
||||
return;
|
||||
}
|
||||
if (e.getClick() == ClickType.LEFT) {
|
||||
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
|
||||
@ -270,7 +220,7 @@ public class InventoryUtils {
|
||||
player.openInventory(inventory);
|
||||
}
|
||||
|
||||
public static void fill(Inventory inv) {
|
||||
private static void fill(Inventory inv) {
|
||||
ItemStack item = getUIItem("fill", new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
|
||||
// Do not bother filling the inventory if item to fill it with is AIR.
|
||||
if (item == null || item.getType() == Material.AIR) return;
|
||||
@ -285,6 +235,53 @@ public class InventoryUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static int getUILocation(String category, int slot) {
|
||||
// Try to use the cached value first.
|
||||
if (uiLocation.containsKey(category)) return uiLocation.get(category);
|
||||
|
||||
// Try to get the value from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".location")) {
|
||||
uiLocation.put(category, HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".location"));
|
||||
return uiLocation.get(category);
|
||||
}
|
||||
|
||||
// No valid value in the config file, return the given default.
|
||||
uiLocation.put(category, slot);
|
||||
return slot;
|
||||
}
|
||||
|
||||
private static ItemStack getUIItem(String category, ItemStack item) {
|
||||
// Try to use the cached item first.
|
||||
if (uiItem.containsKey(category)) return uiItem.get(category);
|
||||
|
||||
// Try to get a head from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".head")) {
|
||||
int id = HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".head");
|
||||
Head head = HeadAPI.getHeadByID(id);
|
||||
if (head != null) {
|
||||
uiItem.put(category, head.getMenuItem());
|
||||
return uiItem.get(category);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get an item from the config file.
|
||||
if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".item")) {
|
||||
String cfg = HeadDB.getInstance().getConfig().getString("ui.category." + category + ".item");
|
||||
Material mat = Material.matchMaterial(cfg);
|
||||
|
||||
// AIR is allowed as the fill material for the menu, but not as a category item.
|
||||
if (mat != null && (category.equals("fill") || mat != Material.AIR)) {
|
||||
uiItem.put(category, new ItemStack(mat));
|
||||
return uiItem.get(category);
|
||||
}
|
||||
}
|
||||
|
||||
// No valid head or item in the config file, return the given default.
|
||||
uiItem.put(category, item);
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
private static ItemStack buildButton(ItemStack item, String name, String... lore) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.colorize(name));
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren