migration to Paper API

This commit is contained in:
hyperdefined
2024-12-14 17:28:02 -05:00
parent 50b5823daf
commit 585e020baf
22 changed files with 224 additions and 341 deletions

View File

@@ -18,6 +18,8 @@
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
@@ -41,17 +43,16 @@ public class ItemLore {
* @param newLine The new line to replace oldLine.
* @return The item's new lore.
*/
public List<String> updateItemLore(ItemMeta itemMeta, String oldLine, String newLine) {
List<String> itemLore;
oldLine = toolStats.configTools.removeColor(oldLine);
public List<Component> updateItemLore(ItemMeta itemMeta, Component oldLine, Component newLine) {
List<Component> itemLore;
if (itemMeta.hasLore()) {
itemLore = itemMeta.getLore();
itemLore = itemMeta.lore();
// keep track of line index
// this doesn't mess the lore of existing items
for (int x = 0; x < itemLore.size(); x++) {
// check to see if the line matches the config value
Component line = itemLore.get(x);
// find the old line to update, keeping index
// this means we update this line only!
String line = toolStats.configTools.removeColor(itemLore.get(x));
if (line.equals(oldLine)) {
itemLore.set(x, newLine);
return itemLore;
@@ -67,10 +68,10 @@ public class ItemLore {
return itemLore;
}
public List<String> addItemLore(ItemMeta itemMeta, String newLine) {
List<String> itemLore;
public List<Component> addItemLore(ItemMeta itemMeta, Component newLine) {
List<Component> itemLore;
if (itemMeta.hasLore()) {
itemLore = itemMeta.getLore();
itemLore = itemMeta.lore();
itemLore.add(newLine);
} else {
itemLore = new ArrayList<>();
@@ -87,9 +88,9 @@ public class ItemLore {
* @param formattedDate The date of the ownership.
* @return The item's new lore.
*/
public List<String> addNewOwner(ItemMeta itemMeta, String playerName, String formattedDate) {
String dateCreatedLore;
String itemOwnerLore;
public List<Component> addNewOwner(ItemMeta itemMeta, String playerName, String formattedDate) {
Component dateCreatedLore;
Component itemOwnerLore;
Integer origin = null;
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
@@ -100,7 +101,7 @@ public class ItemLore {
if (origin == null) {
toolStats.logger.info("Unable to determine origin for item " + itemMeta.getAsString());
toolStats.logger.info("This IS a bug, please report this to the GitHub.");
return itemMeta.getLore();
return itemMeta.lore();
}
// set the lore based on the origin
@@ -112,12 +113,12 @@ public class ItemLore {
if (dateCreatedLore == null) {
toolStats.logger.warning("messages.looted.looted-on is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
if (itemOwnerLore == null) {
toolStats.logger.warning("messages.looted.looted-by is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
break;
}
@@ -128,12 +129,12 @@ public class ItemLore {
if (dateCreatedLore == null) {
toolStats.logger.warning("messages.traded.traded-on is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
if (itemOwnerLore == null) {
toolStats.logger.warning("messages.traded.traded-by is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
break;
}
@@ -144,12 +145,12 @@ public class ItemLore {
if (dateCreatedLore == null) {
toolStats.logger.warning("messages.looted.found-on is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
if (itemOwnerLore == null) {
toolStats.logger.warning("messages.looted.found-by is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
break;
}
@@ -160,12 +161,12 @@ public class ItemLore {
if (dateCreatedLore == null) {
toolStats.logger.warning("messages.fished.caught-on is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
if (itemOwnerLore == null) {
toolStats.logger.warning("messages.fished.caught-by is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
break;
}
@@ -176,31 +177,31 @@ public class ItemLore {
if (dateCreatedLore == null) {
toolStats.logger.warning("messages.spawned-in.spawned-on is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
if (itemOwnerLore == null) {
toolStats.logger.warning("messages.spawned-in.spawned-by is not set in your config!");
toolStats.logger.warning("Unable to update lore for item.");
return itemMeta.getLore();
return itemMeta.lore();
}
break;
}
default: {
toolStats.logger.warning("Origin " + origin + " was found. Data was modified OR something REALLY broke.");
toolStats.logger.warning(itemMeta.getAsString());
return itemMeta.getLore();
return itemMeta.lore();
}
}
List<String> newLore;
List<Component> newLore;
if (itemMeta.hasLore()) {
newLore = itemMeta.getLore();
newLore = itemMeta.lore();
} else {
newLore = new ArrayList<>();
}
newLore.add(dateCreatedLore.replace("{date}", formattedDate));
newLore.add(itemOwnerLore.replace("{player}", playerName));
newLore.add(dateCreatedLore);
newLore.add(itemOwnerLore);
return newLore;
}
}

View File

@@ -18,7 +18,9 @@
package lol.hyper.toolstats.tools.config;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.ChatColor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Material;
import java.util.regex.Matcher;
@@ -70,46 +72,21 @@ public class ConfigTools {
itemType = itemName.substring(itemName.indexOf('_') + 1);
}
switch (itemType) {
case "pickaxe": {
return toolStats.config.getBoolean("enabled." + configName + ".pickaxe");
}
case "sword": {
return toolStats.config.getBoolean("enabled." + configName + ".sword");
}
case "shovel": {
return toolStats.config.getBoolean("enabled." + configName + ".shovel");
}
case "axe": {
return toolStats.config.getBoolean("enabled." + configName + ".axe");
}
case "hoe": {
return toolStats.config.getBoolean("enabled." + configName + ".hoe");
}
case "shears": {
return toolStats.config.getBoolean("enabled." + configName + ".shears");
}
case "crossbow":
case "bow": {
return toolStats.config.getBoolean("enabled." + configName + ".bow");
}
case "trident": {
return toolStats.config.getBoolean("enabled." + configName + ".trident");
}
case "fishing-rod": {
return toolStats.config.getBoolean("enabled." + configName + ".fishing-rod");
}
case "mace": {
return toolStats.config.getBoolean("enabled." + configName + ".mace");
}
case "helmet":
case "chestplate":
case "leggings":
case "boots": {
return toolStats.config.getBoolean("enabled." + configName + ".armor");
}
}
return false;
return switch (itemType) {
case "pickaxe" -> toolStats.config.getBoolean("enabled." + configName + ".pickaxe");
case "sword" -> toolStats.config.getBoolean("enabled." + configName + ".sword");
case "shovel" -> toolStats.config.getBoolean("enabled." + configName + ".shovel");
case "axe" -> toolStats.config.getBoolean("enabled." + configName + ".axe");
case "hoe" -> toolStats.config.getBoolean("enabled." + configName + ".hoe");
case "shears" -> toolStats.config.getBoolean("enabled." + configName + ".shears");
case "crossbow", "bow" -> toolStats.config.getBoolean("enabled." + configName + ".bow");
case "trident" -> toolStats.config.getBoolean("enabled." + configName + ".trident");
case "fishing-rod" -> toolStats.config.getBoolean("enabled." + configName + ".fishing-rod");
case "mace" -> toolStats.config.getBoolean("enabled." + configName + ".mace");
case "helmet", "chestplate", "leggings", "boots" ->
toolStats.config.getBoolean("enabled." + configName + ".armor");
default -> false;
};
}
/**
@@ -120,28 +97,29 @@ public class ConfigTools {
* @param value The value to set the placeholder.
* @return Formatted string, null if the configName doesn't exist.
*/
public String formatLore(String configName, String placeHolder, Object value) {
public Component formatLore(String configName, String placeHolder, Object value) {
String lore = toolStats.config.getString("messages." + configName);
if (lore == null) {
return null;
}
// the final component for this lore
Component component;
// set the placeholder to the value
lore = lore.replace(placeHolder, String.valueOf(value));
// if we match the old color codes, then format them as so
Matcher hexMatcher = CONFIG_HEX_PATTERN.matcher(lore);
while (hexMatcher.find()) {
String hexCode = hexMatcher.group(1);
lore = lore.replaceAll(hexMatcher.group(), net.md_5.bungee.api.ChatColor.of("#" + hexCode).toString());
}
Matcher colorMatcher = COLOR_CODES.matcher(lore);
while (colorMatcher.find()) {
String colorCode = colorMatcher.group(1);
lore = lore.replaceAll("&" + colorCode, ChatColor.getByChar(colorCode).toString());
if (hexMatcher.find() || colorMatcher.find()) {
component = LegacyComponentSerializer.legacyAmpersand().deserialize(lore);
} else {
// otherwise format them normally
component = Component.text(lore);
}
return ChatColor.translateAlternateColorCodes('§', lore);
return component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
/**

View File

@@ -18,7 +18,6 @@
package lol.hyper.toolstats.tools.config.versions;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.configuration.ConfigurationSection;
import java.io.File;
import java.io.IOException;