tokens update

This commit is contained in:
hyperdefined
2025-01-25 16:56:03 -05:00
parent ff578e170e
commit 30b26533ef
21 changed files with 1705 additions and 531 deletions

View File

@@ -17,9 +17,15 @@
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@@ -29,11 +35,16 @@ public class ItemChecker {
private final List<Material> armorItems = new ArrayList<>();
private final List<Material> meleeItems = new ArrayList<>();
private final List<Material> mineItems = new ArrayList<>();
private final ToolStats toolStats;
public ItemChecker(ToolStats toolStats) {
this.toolStats = toolStats;
}
/**
* Creates an item checker and saves all valid items we want.
* Set up the item checker.
*/
public ItemChecker() {
public void setup() {
for (Material material : Material.values()) {
String lowerCase = material.toString().toLowerCase(Locale.ROOT);
if (lowerCase.contains("_pickaxe") || lowerCase.contains("_axe") || lowerCase.contains("_hoe") || lowerCase.contains("_shovel")) {
@@ -52,11 +63,12 @@ public class ItemChecker {
// hardcode these
mineItems.add(Material.SHEARS);
meleeItems.add(Material.TRIDENT);
meleeItems.add(Material.MACE);
validItems.add(Material.BOW);
validItems.add(Material.FISHING_ROD);
validItems.add(Material.CROSSBOW);
validItems.add(Material.FISHING_ROD);
validItems.add(Material.ELYTRA);
validItems.add(Material.MACE);
// combine the lists
validItems.addAll(armorItems);
@@ -103,4 +115,89 @@ public class ItemChecker {
public boolean isMineTool(Material itemType) {
return mineItems.contains(itemType);
}
/**
* Check a given item for a target token.
*
* @param container The PDC of the item.
* @param targetToken The target to look for.
* @return True if the item has a given token, false if not.
*/
public boolean checkTokens(PersistentDataContainer container, String targetToken) {
// make sure the item has tokens
if (!container.has(toolStats.tokenApplied, PersistentDataType.STRING)) {
return false;
}
// get the tokens for this item
String tokens = container.get(toolStats.tokenApplied, PersistentDataType.STRING);
if (tokens == null) {
return false;
}
return tokens.contains(targetToken);
}
/**
* Get the tokens for a given item.
*
* @param item The item.
* @return An array of the tokens, empty if there are none.
*/
private String[] getTokens(ItemStack item) {
// make sure the item has tokens
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return new String[0];
}
PersistentDataContainer container = meta.getPersistentDataContainer();
if (!container.has(toolStats.tokenApplied, PersistentDataType.STRING)) {
return new String[0];
}
// get the tokens for this item
String tokensRaw = container.get(toolStats.tokenApplied, PersistentDataType.STRING);
if (tokensRaw == null) {
return new String[0];
}
return tokensRaw.split(",");
}
/**
* Add a token to an item.
*
* @param item The item.
* @param token The token to add.
* @return The new PDC with the new token. Null if something went wrong.
*/
public ItemStack addToken(ItemStack item, String token) {
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
String[] tokens = getTokens(item);
// there are no tokens
if (tokens.length == 0) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, token);
} else {
// other tokens exist, so add
String[] newTokens = Arrays.copyOf(tokens, tokens.length + 1);
newTokens[tokens.length] = token;
container.set(toolStats.tokenApplied, PersistentDataType.STRING, String.join(",", newTokens));
}
item.setItemMeta(meta);
return item;
}
/**
* Get the XP levels required to use token in anvil.
*
* @param tokenType The token type.
* @return The amount of levels to use.
*/
public int getCost(String tokenType) {
return toolStats.config.getInt("tokens.data." + tokenType + ".levels");
}
}

View File

@@ -20,6 +20,7 @@ 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.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
@@ -68,6 +69,13 @@ public class ItemLore {
return itemLore;
}
/**
* Add lore to a given item.
*
* @param itemMeta The item's meta.
* @param newLine The new line to add to the lore.
* @return The new item's lore.
*/
public List<Component> addItemLore(ItemMeta itemMeta, Component newLine) {
List<Component> itemLore;
if (itemMeta.hasLore()) {
@@ -204,4 +212,486 @@ public class ItemLore {
newLore.add(itemOwnerLore);
return newLore;
}
/**
* Add x to the crops mined stat.
*
* @param playerTool The tool to update.
*/
public ItemStack updateCropsMined(ItemStack playerTool, int add) {
ItemStack clone = playerTool.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
// read the current stats from the item
// if they don't exist, then start from 0
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "crops-mined");
if (!validTokens) {
return null;
}
}
Integer cropsMined = 0;
if (container.has(toolStats.cropsHarvested, PersistentDataType.INTEGER)) {
cropsMined = container.get(toolStats.cropsHarvested, PersistentDataType.INTEGER);
}
if (cropsMined == null) {
cropsMined = 0;
toolStats.logger.warning(clone + " does not have valid crops-mined set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.cropsHarvested, PersistentDataType.INTEGER, cropsMined + add);
// do we add the lore based on the config?
if (toolStats.configTools.checkConfig(clone.getType(), "blocks-mined")) {
String oldCropsMinedFormatted = toolStats.numberFormat.formatInt(cropsMined);
String newCropsMinedFormatted = toolStats.numberFormat.formatInt(cropsMined + add);
Component oldLine = toolStats.configTools.formatLore("crops-harvested", "{crops}", oldCropsMinedFormatted);
Component newLine = toolStats.configTools.formatLore("crops-harvested", "{crops}", newCropsMinedFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add x to the blocks mined stat.
*
* @param playerTool The tool to update.
*/
public ItemStack updateBlocksMined(ItemStack playerTool, int add) {
ItemStack clone = playerTool.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
toolStats.logger.info("tokens are enabled!");
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "blocks-mined");
if (!validTokens) {
return null;
}
} else {
toolStats.logger.info("tokens are disabled!");
}
// read the current stats from the item
// if they don't exist, then start from 0
Integer blocksMined = 0;
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
}
if (blocksMined == null) {
blocksMined = 0;
toolStats.logger.warning(clone + " does not have valid generic-mined set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined + add);
// do we add the lore based on the config?
if (toolStats.configTools.checkConfig(clone.getType(), "blocks-mined")) {
String oldBlocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined);
String newBlocksMinedFormatted = toolStats.numberFormat.formatInt(blocksMined + add);
Component oldLine = toolStats.configTools.formatLore("blocks-mined", "{blocks}", oldBlocksMinedFormatted);
Component newLine = toolStats.configTools.formatLore("blocks-mined", "{blocks}", newBlocksMinedFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
toolStats.logger.info("adding lore!");
}
clone.setItemMeta(meta);
toolStats.logger.info("reached end of function!");
return clone;
}
/**
* Add +1 to the player kills stat.
*
* @param playerWeapon The tool to update.
*/
public ItemStack updatePlayerKills(ItemStack playerWeapon, int add) {
ItemStack clone = playerWeapon.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "player-kills");
if (!validTokens) {
return null;
}
}
Integer playerKills = 0;
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
playerKills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
}
if (playerKills == null) {
playerKills = 0;
toolStats.logger.warning(clone + " does not have valid player-kills set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.swordPlayerKills, PersistentDataType.INTEGER, playerKills + add);
// do we add the lore based on the config?
if (toolStats.configTools.checkConfig(clone.getType(), "player-kills")) {
String oldPlayerKillsFormatted = toolStats.numberFormat.formatInt(playerKills);
String newPlayerKillsFormatted = toolStats.numberFormat.formatInt(playerKills + add);
Component oldLine = toolStats.configTools.formatLore("kills.player", "{kills}", oldPlayerKillsFormatted);
Component newLine = toolStats.configTools.formatLore("kills.player", "{kills}", newPlayerKillsFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add x to the mob kills stat.
*
* @param playerWeapon The tool to update.
*/
public ItemStack updateMobKills(ItemStack playerWeapon, int add) {
ItemStack clone = playerWeapon.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "mob-kills");
if (!validTokens) {
return null;
}
}
Integer mobKills = 0;
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
mobKills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
}
if (mobKills == null) {
mobKills = 0;
toolStats.logger.warning(clone + " does not have valid mob-kills set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.swordMobKills, PersistentDataType.INTEGER, mobKills + add);
// do we add the lore based on the config?
if (toolStats.configTools.checkConfig(clone.getType(), "mob-kills")) {
String oldMobKillsFormatted = toolStats.numberFormat.formatInt(mobKills);
String newMobKillsFormatted = toolStats.numberFormat.formatInt(mobKills + add);
Component oldLine = toolStats.configTools.formatLore("kills.mob", "{kills}", oldMobKillsFormatted);
Component newLine = toolStats.configTools.formatLore("kills.mob", "{kills}", newMobKillsFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add damage to an armor piece.
*
* @param armorPiece The armor to update.
*/
public ItemStack updateDamage(ItemStack armorPiece, double damage) {
// ignore if the damage is zero or negative
if (damage < 0) {
return null;
}
ItemStack clone = armorPiece.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "damage-taken");
if (!validTokens) {
return null;
}
}
Double damageTaken = 0.0;
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
damageTaken = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
}
if (damageTaken == null) {
damageTaken = 0.0;
toolStats.logger.warning(clone + " does not have valid damage-taken set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken + damage);
if (toolStats.config.getBoolean("enabled.armor-damage")) {
String oldDamageFormatted = toolStats.numberFormat.formatDouble(damageTaken);
String newDamageFormatted = toolStats.numberFormat.formatDouble(damageTaken + damage);
Component oldLine = toolStats.configTools.formatLore("damage-taken", "{damage}", oldDamageFormatted);
Component newLine = toolStats.configTools.formatLore("damage-taken", "{damage}", newDamageFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add flight time to an elytra.
*
* @param elytra The player's elytra.
*/
public ItemStack updateFlightTime(ItemStack elytra, long duration) {
ItemStack clone = elytra.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "flight-time");
if (!validTokens) {
return null;
}
}
// read the current stats from the item
// if they don't exist, then start from 0
Long flightTime = 0L;
if (container.has(toolStats.flightTime, PersistentDataType.LONG)) {
flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG);
}
if (flightTime == null) {
flightTime = 0L;
toolStats.logger.warning(flightTime + " does not have valid flight-time set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.flightTime, PersistentDataType.LONG, flightTime + duration);
// do we add the lore based on the config?
if (toolStats.config.getBoolean("enabled.flight-time")) {
String oldFlightFormatted = toolStats.numberFormat.formatDouble((double) flightTime / 1000);
String newFlightFormatted = toolStats.numberFormat.formatDouble((double) (flightTime + duration) / 1000);
Component oldLine = toolStats.configTools.formatLore("flight-time", "{time}", oldFlightFormatted);
Component newLine = toolStats.configTools.formatLore("flight-time", "{time}", newFlightFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add x to sheep sheared stat.
*
* @param shears The shears.
*/
public ItemStack updateSheepSheared(ItemStack shears, int add) {
ItemStack clone = shears.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "sheep-sheared");
if (!validTokens) {
return null;
}
}
Integer sheepSheared = 0;
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
sheepSheared = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
}
if (sheepSheared == null) {
sheepSheared = 0;
toolStats.logger.warning(clone + " does not have valid sheared set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.shearsSheared, PersistentDataType.INTEGER, sheepSheared + add);
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
String oldSheepFormatted = toolStats.numberFormat.formatInt(sheepSheared);
String newSheepFormatted = toolStats.numberFormat.formatInt(sheepSheared + add);
Component oldLine = toolStats.configTools.formatLore("sheep-sheared", "{sheep}", oldSheepFormatted);
Component newLine = toolStats.configTools.formatLore("sheep-sheared", "{sheep}", newSheepFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add x to arrows shot stat.
*
* @param bow The bow.
*/
public ItemStack updateArrowsShot(ItemStack bow, int add) {
ItemStack clone = bow.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "arrows-shot");
if (!validTokens) {
return null;
}
}
// read the current stats from the item
// if they don't exist, then start from 0
Integer arrowsShot = 0;
if (container.has(toolStats.arrowsShot, PersistentDataType.INTEGER)) {
arrowsShot = container.get(toolStats.arrowsShot, PersistentDataType.INTEGER);
}
if (arrowsShot == null) {
arrowsShot = 0;
toolStats.logger.warning(arrowsShot + " does not have valid arrows-shot set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.arrowsShot, PersistentDataType.INTEGER, arrowsShot + add);
// do we add the lore based on the config?
if (toolStats.config.getBoolean("enabled.arrows-shot")) {
String oldArrowsFormatted = toolStats.numberFormat.formatInt(arrowsShot);
String newArrowsFormatted = toolStats.numberFormat.formatInt(arrowsShot + add);
Component oldLine = toolStats.configTools.formatLore("arrows-shot", "{arrows}", oldArrowsFormatted);
Component newLine = toolStats.configTools.formatLore("arrows-shot", "{arrows}", newArrowsFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
/**
* Add x to fish caught stat.
*
* @param fishingRod The fishing rod.
*/
public ItemStack updateFishCaught(ItemStack fishingRod, int add) {
ItemStack clone = fishingRod.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warning(clone + " does NOT have any meta! Unable to update stats.");
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has this token, then continue
// if the item does not, ignore
boolean validTokens = toolStats.itemChecker.checkTokens(container, "fish-caught");
if (!validTokens) {
return null;
}
}
Integer fishCaught = 0;
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
fishCaught = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
}
if (fishCaught == null) {
fishCaught = 0;
toolStats.logger.warning(clone + " does not have valid fish-caught set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.fishingRodCaught, PersistentDataType.INTEGER, fishCaught + add);
if (toolStats.config.getBoolean("enabled.fish-caught")) {
String oldFishFormatted = toolStats.numberFormat.formatInt(fishCaught);
String newFishFormatted = toolStats.numberFormat.formatInt(fishCaught + add);
Component oldLine = toolStats.configTools.formatLore("fished.fish-caught", "{fish}", oldFishFormatted);
Component newLine = toolStats.configTools.formatLore("fished.fish-caught", "{fish}", newFishFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
}
clone.setItemMeta(meta);
return clone;
}
}

View File

@@ -65,7 +65,7 @@ public class NumberFormat {
}
if (decimalFormat == null) {
decimalFormat = "#,###.00";
decimalFormat = "#,##0.00";
toolStats.logger.warning("number-formats.comma-separator is missing! Using default #,###.00 instead.");
}

View File

@@ -0,0 +1,121 @@
/*
* This file is part of ToolStats.
*
* ToolStats is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ToolStats is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ShapedRecipe;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class TokenCrafting {
private final ToolStats toolStats;
private final Set<ShapedRecipe> recipes = new HashSet<>();
private final ArrayList<String> tokenTypes = new ArrayList<>();
public TokenCrafting(ToolStats toolStats) {
this.toolStats = toolStats;
}
public void setup() {
NamespacedKey playerKillsKey = new NamespacedKey(toolStats, "player-kills-token");
ShapedRecipe playerKillRecipe = new ShapedRecipe(playerKillsKey, toolStats.tokenItems.playerKills());
playerKillRecipe.shape(" P ", "PSP", " P ");
playerKillRecipe.setIngredient('P', Material.PAPER);
playerKillRecipe.setIngredient('S', Material.WOODEN_SWORD);
recipes.add(playerKillRecipe);
NamespacedKey mobKillsKey = new NamespacedKey(toolStats, "mob-kills-token");
ShapedRecipe mobKillsRecipe = new ShapedRecipe(mobKillsKey, toolStats.tokenItems.mobKills());
mobKillsRecipe.shape(" P ", "PRP", " P ");
mobKillsRecipe.setIngredient('P', Material.PAPER);
mobKillsRecipe.setIngredient('R', Material.ROTTEN_FLESH);
recipes.add(mobKillsRecipe);
NamespacedKey blocksMinedKey = new NamespacedKey(toolStats, "blocks-mined-token");
ShapedRecipe blocksMinedRecipe = new ShapedRecipe(blocksMinedKey, toolStats.tokenItems.blocksMined());
blocksMinedRecipe.shape(" P ", "PSP", " P ");
blocksMinedRecipe.setIngredient('P', Material.PAPER);
blocksMinedRecipe.setIngredient('S', Material.WOODEN_PICKAXE);
recipes.add(blocksMinedRecipe);
NamespacedKey cropsMinedKey = new NamespacedKey(toolStats, "crops-mined-token");
ShapedRecipe cropsMinedRecipe = new ShapedRecipe(cropsMinedKey, toolStats.tokenItems.cropsMined());
cropsMinedRecipe.shape(" P ", "PHP", " P ");
cropsMinedRecipe.setIngredient('P', Material.PAPER);
cropsMinedRecipe.setIngredient('H', Material.WOODEN_HOE);
recipes.add(cropsMinedRecipe);
NamespacedKey fishCaughtKey = new NamespacedKey(toolStats, "fish-caught-token");
ShapedRecipe fishCaughtRecipe = new ShapedRecipe(fishCaughtKey, toolStats.tokenItems.fishCaught());
fishCaughtRecipe.shape(" P ", "PCP", " P ");
fishCaughtRecipe.setIngredient('P', Material.PAPER);
fishCaughtRecipe.setIngredient('C', Material.COD);
recipes.add(fishCaughtRecipe);
NamespacedKey sheepShearedKey = new NamespacedKey(toolStats, "sheep-sheared-token");
ShapedRecipe sheepShearedRecipe = new ShapedRecipe(sheepShearedKey, toolStats.tokenItems.sheepSheared());
sheepShearedRecipe.shape(" P ", "PWP", " P ");
sheepShearedRecipe.setIngredient('P', Material.PAPER);
sheepShearedRecipe.setIngredient('W', Material.WHITE_WOOL);
recipes.add(sheepShearedRecipe);
NamespacedKey armorDamageKey = new NamespacedKey(toolStats, "damage-taken-token");
ShapedRecipe armorDamageRecipe = new ShapedRecipe(armorDamageKey, toolStats.tokenItems.damageTaken());
armorDamageRecipe.shape(" P ", "PCP", " P ");
armorDamageRecipe.setIngredient('P', Material.PAPER);
armorDamageRecipe.setIngredient('C', Material.LEATHER_CHESTPLATE);
recipes.add(armorDamageRecipe);
NamespacedKey arrowsShotKey = new NamespacedKey(toolStats, "arrows-shot-token");
ShapedRecipe arrowsShotRecipe = new ShapedRecipe(arrowsShotKey, toolStats.tokenItems.arrowsShot());
arrowsShotRecipe.shape(" P ", "PAP", " P ");
arrowsShotRecipe.setIngredient('P', Material.PAPER);
arrowsShotRecipe.setIngredient('A', Material.ARROW);
recipes.add(arrowsShotRecipe);
NamespacedKey flightTimeKey = new NamespacedKey(toolStats, "flight-time-token");
ShapedRecipe flightTimeRecipe = new ShapedRecipe(flightTimeKey, toolStats.tokenItems.flightTime());
flightTimeRecipe.shape(" P ", "PFP", " P ");
flightTimeRecipe.setIngredient('P', Material.PAPER);
flightTimeRecipe.setIngredient('F', Material.FEATHER);
recipes.add(flightTimeRecipe);
tokenTypes.add("crops-mined");
tokenTypes.add("blocks-mined");
tokenTypes.add("damage-taken");
tokenTypes.add("mob-kills");
tokenTypes.add("player-kills");
tokenTypes.add("arrows-shot");
tokenTypes.add("sheep-sheared");
tokenTypes.add("flight-time");
tokenTypes.add("fish-caught");
}
public Set<ShapedRecipe> getRecipes() {
return recipes;
}
public ArrayList<String> getTokenTypes() {
return tokenTypes;
}
}

View File

@@ -20,6 +20,7 @@ package lol.hyper.toolstats.tools.config;
import lol.hyper.toolstats.ToolStats;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Material;
@@ -31,7 +32,6 @@ public class ConfigTools {
private final ToolStats toolStats;
public static final Pattern COLOR_CODES = Pattern.compile("[&§]([0-9a-fk-or])");
public static final Pattern CONFIG_HEX_PATTERN = Pattern.compile("[&§]#([A-Fa-f0-9]{6})");
public static final Pattern MINECRAFT_HEX_PATTERN = Pattern.compile("§x(?:§[a-fA-F0-9]){6}|§[a-fA-F0-9]");
public ConfigTools(ToolStats toolStats) {
this.toolStats = toolStats;
@@ -124,22 +124,42 @@ public class ConfigTools {
component = LegacyComponentSerializer.legacyAmpersand().deserialize(lore);
} else {
// otherwise format them normally
component = Component.text(lore);
component = MiniMessage.miniMessage().deserialize(lore);
}
return component.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
}
/**
* Remove all color codes from a message.
* Format a string from the config.
*
* @param message The message.
* @return The message without color codes.
* @param configName The config to format.
* @return Formatted string, null if the configName doesn't exist.
*/
public String removeColor(String message) {
message = MINECRAFT_HEX_PATTERN.matcher(message).replaceAll("");
message = COLOR_CODES.matcher(message).replaceAll("");
message = CONFIG_HEX_PATTERN.matcher(message).replaceAll("");
return message;
public Component format(String configName) {
String message = toolStats.config.getString(configName);
if (message == null) {
toolStats.logger.warning("Unable to find config message for: " + configName);
return null;
}
// if the config message is empty, don't send it
if (message.isEmpty()) {
return null;
}
// the final component for this lore
Component component;
// if we match the old color codes, then format them as so
Matcher hexMatcher = CONFIG_HEX_PATTERN.matcher(message);
Matcher colorMatcher = COLOR_CODES.matcher(message);
if (hexMatcher.find() || colorMatcher.find()) {
component = LegacyComponentSerializer.legacyAmpersand().deserialize(message);
} else {
// otherwise format them normally
component = MiniMessage.miniMessage().deserialize(message);
}
return component;
}
}

View File

@@ -21,6 +21,7 @@ import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.config.versions.Version6;
import lol.hyper.toolstats.tools.config.versions.Version7;
import lol.hyper.toolstats.tools.config.versions.Version8;
import lol.hyper.toolstats.tools.config.versions.Version9;
public class ConfigUpdater {
@@ -52,6 +53,12 @@ public class ConfigUpdater {
version8.update();
break;
}
case 8: {
// Version 8 to 9
Version9 version9 = new Version9(toolStats);
version9.update();
break;
}
}
}
}

View File

@@ -0,0 +1,219 @@
/*
* This file is part of ToolStats.
*
* ToolStats is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ToolStats is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.toolstats.tools.config;
import lol.hyper.toolstats.ToolStats;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.List;
public class TokenItems {
private final ToolStats toolStats;
public TokenItems(ToolStats toolStats) {
this.toolStats = toolStats;
}
public ItemStack playerKills() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.player-kills.title");
Component lore = toolStats.configTools.format("tokens.data.player-kills.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "player-kills");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack mobKills() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.mob-kills.title");
Component lore = toolStats.configTools.format("tokens.data.mob-kills.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "mob-kills");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack blocksMined() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.blocks-mined.title");
Component lore = toolStats.configTools.format("tokens.data.blocks-mined.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "blocks-mined");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack cropsMined() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.crops-mined.title");
Component lore = toolStats.configTools.format("tokens.data.crops-mined.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "crops-mined");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack fishCaught() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.fish-caught.title");
Component lore = toolStats.configTools.format("tokens.data.fish-caught.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "fish-caught");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack sheepSheared() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.sheep-sheared.title");
Component lore = toolStats.configTools.format("tokens.data.sheep-sheared.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "sheep-sheared");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack damageTaken() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.damage-taken.title");
Component lore = toolStats.configTools.format("tokens.data.damage-taken.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "damage-taken");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack arrowsShot() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.arrows-shot.title");
Component lore = toolStats.configTools.format("tokens.data.arrows-shot.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "arrows-shot");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack flightTime() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);
ItemMeta tokenMeta = token.getItemMeta();
PersistentDataContainer tokenData = tokenMeta.getPersistentDataContainer();
// set the title and lore
Component title = toolStats.configTools.format("tokens.data.flight-time.title");
Component lore = toolStats.configTools.format("tokens.data.flight-time.lore");
tokenMeta.displayName(title);
List<Component> newLore = new ArrayList<>();
newLore.add(lore);
tokenMeta.lore(newLore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "flight-time");
token.setItemMeta(tokenMeta);
return token;
}
}

View File

@@ -0,0 +1,99 @@
/*
* This file is part of ToolStats.
*
* ToolStats is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ToolStats is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.toolstats.tools.config.versions;
import lol.hyper.toolstats.ToolStats;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Version9 {
private final ToolStats toolStats;
/**
* Used for updating from version 8 to 9.
*
* @param toolStats ToolStats instance.
*/
public Version9(ToolStats toolStats) {
this.toolStats = toolStats;
}
/**
* Perform the config update.
*/
public void update() {
// save the old config first
try {
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-8.yml");
} catch (IOException exception) {
toolStats.logger.severe("Unable to save config-8.yml!");
throw new RuntimeException(exception);
}
toolStats.logger.info("Updating config.yml to version 9.");
toolStats.config.set("config-version", 9);
toolStats.logger.info("Adding new tokens configuration!");
// false by default so it doesn't break servers on updating
toolStats.config.set("tokens.enabled", false);
toolStats.config.set("tokens.craft-tokens", true);
List<String> tokenComments = new ArrayList<>();
tokenComments.add("Use token system for tracking stats.");
tokenComments.add("See https://github.com/hyperdefined/ToolStats/wiki/Token-System");
toolStats.config.setComments("tokens", tokenComments);
addToken("player-kills", "&7ToolStats: &8Player Kills Token", "&8Combine with a melee or ranged weapon in an anvil to track player kills.");
addToken("mob-kills", "&7ToolStats: &8Mob Kills Token", "&8Combine with a melee or ranged weapon in an anvil to track mob kills.");
addToken("blocks-mined", "&7ToolStats: &8Blocks Mined Token", "&8Combine with a pickaxe, axe, shovel, or shears in an anvil to track blocks mined.");
addToken("crops-mined", "&7ToolStats: &8Crops Mined Token", "&8Combine with a hoe in an anvil to track crops broken.");
addToken("fish-caught", "&7ToolStats: &8Fish Caught Token", "&8Combine with a fishing rod in an anvil to track fish caught.");
addToken("sheep-sheared", "&7ToolStats: &8Sheep Sheared Token", "&8Combine with shears in an anvil to track sheep sheared.");
addToken("damage-taken", "&7ToolStats: &8Damage Taken Token", "&8Combine with an armor piece in an anvil to track damage taken.");
addToken("arrows-shot", "&7ToolStats: &8Arrows Shot Token", "&8Combine with a bow or crossbow in an anvil to track arrows shot.");
addToken("flight-time", "&7ToolStats: &8Flight Time Token", "&8Combine with an elytra in an anvil to track flight time.");
// save the config and reload it
try {
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
} catch (IOException exception) {
toolStats.logger.severe("Unable to save config.yml!");
throw new RuntimeException(exception);
}
toolStats.loadConfig();
toolStats.logger.info("Config has been updated to version 9. A copy of version 8 has been saved as config-8.yml");
}
/**
* Add a given token to the config. Made this since I was lazy.
*
* @param tokenType The token type to add.
* @param title The title for the item.
* @param lore The lore of the item.
*/
private void addToken(String tokenType, String title, String lore) {
toolStats.logger.info("Adding token type configuration for " + tokenType);
toolStats.config.set("tokens.data." + tokenType + ".title", title);
toolStats.config.set("tokens.data." + tokenType + ".lore", lore);
toolStats.config.set("tokens.data." + tokenType + ".levels", 1);
}
}