added damage done system

This commit is contained in:
hyperdefined
2025-03-30 20:33:30 -04:00
parent e37048ff01
commit d0199a85e0
10 changed files with 460 additions and 128 deletions

View File

@@ -333,6 +333,9 @@ public class ItemChecker {
if (container.has(toolStats.flightTime)) {
tokens.add("flight-time");
}
if (container.has(toolStats.damageDone)) {
tokens.add("damage-done");
}
if (tokens.isEmpty()) {
return null;
}

View File

@@ -567,7 +567,7 @@ public class ItemLore {
* @param damage The amount of damage to apply.
* @param bypass Bypass the negative damage check.
*/
public ItemMeta updateDamage(ItemStack armorPiece, double damage, boolean bypass) {
public ItemMeta updateArmorDamage(ItemStack armorPiece, double damage, boolean bypass) {
// ignore if the damage is zero or negative
if (damage < 0) {
if (!bypass) {
@@ -651,6 +651,97 @@ public class ItemLore {
return meta;
}
/**
* Add damage to a weapon.
*
* @param weapon The weapon to update.
* @param damage The amount of damage to apply.
* @param bypass Bypass the negative damage check.
*/
public ItemMeta updateWeaponDamage(ItemStack weapon, double damage, boolean bypass) {
// ignore if the damage is zero or negative
if (damage < 0) {
if (!bypass) {
return null;
}
}
ItemStack clone = weapon.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();
// if it's disabled, don't update the stats
// check to see if the item has the stats, remove them if it does
if (!toolStats.configTools.checkConfig(clone.getType(), "damage-done")) {
if (container.has(toolStats.damageDone)) {
Double damageDone = container.get(toolStats.damageDone, PersistentDataType.DOUBLE);
if (damageDone == null) {
return null;
}
container.remove(toolStats.damageDone);
if (meta.hasLore()) {
String oldDamageDoneFormatted = toolStats.numberFormat.formatDouble(damageDone);
Component lineToRemove = toolStats.configTools.formatLore("damage-done", "{damage}", oldDamageDoneFormatted);
List<Component> newLore = removeLore(meta.lore(), lineToRemove);
meta.lore(newLore);
}
return meta;
}
return null;
}
// check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "damage-done");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has stats but no token, add the token
if (container.has(toolStats.damageDone) && !validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
// the item does not have a valid token
if (!validToken) {
return null;
}
} else {
if (!validToken) {
String newTokens = toolStats.itemChecker.addTokensToExisting(clone);
if (newTokens != null) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, newTokens);
}
}
}
Double damageDone = 0.0;
if (container.has(toolStats.damageDone, PersistentDataType.DOUBLE)) {
damageDone = container.get(toolStats.damageDone, PersistentDataType.DOUBLE);
}
if (damageDone == null) {
damageDone = 0.0;
toolStats.logger.warning(clone + " does not have valid damage-done set! Resting to zero. This should NEVER happen.");
}
container.set(toolStats.damageDone, PersistentDataType.DOUBLE, damageDone + damage);
String oldDamageFormatted = toolStats.numberFormat.formatDouble(damageDone);
String newDamageFormatted = toolStats.numberFormat.formatDouble(damageDone + damage);
Component oldLine = toolStats.configTools.formatLore("damage-done", "{damage}", oldDamageFormatted);
Component newLine = toolStats.configTools.formatLore("damage-done", "{damage}", newDamageFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = toolStats.itemLore.updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
return meta;
}
/**
* Add flight time to an elytra.
*

View File

@@ -86,6 +86,13 @@ public class TokenCrafting {
armorDamageRecipe.setIngredient('C', Material.LEATHER_CHESTPLATE);
recipes.add(armorDamageRecipe);
NamespacedKey damageDoneKey = new NamespacedKey(toolStats, "damage-done-token");
ShapedRecipe damageDoneRecipe = new ShapedRecipe(damageDoneKey, toolStats.tokenItems.damageDone());
damageDoneRecipe.shape(" P ", "PSP", " P ");
damageDoneRecipe.setIngredient('P', Material.PAPER);
damageDoneRecipe.setIngredient('S', Material.SHIELD);
recipes.add(damageDoneRecipe);
NamespacedKey arrowsShotKey = new NamespacedKey(toolStats, "arrows-shot-token");
ShapedRecipe arrowsShotRecipe = new ShapedRecipe(arrowsShotKey, toolStats.tokenItems.arrowsShot());
arrowsShotRecipe.shape(" P ", "PAP", " P ");

View File

@@ -56,5 +56,10 @@ public class ConfigUpdater {
Version10 version10 = new Version10(toolStats);
version10.update();
}
// Version 10 to 11
if (version == 10) {
Version11 version11 = new Version11(toolStats);
version11.update();
}
}
}

View File

@@ -162,6 +162,24 @@ public class TokenItems {
return token;
}
public ItemStack damageDone() {
// 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-done.title");
List<Component> lore = toolStats.configTools.getTokenLore("damage-done");
tokenMeta.displayName(title);
tokenMeta.lore(lore);
// set the PDC
tokenData.set(toolStats.tokenType, PersistentDataType.STRING, "damage-done");
token.setItemMeta(tokenMeta);
return token;
}
public ItemStack arrowsShot() {
// set up the item
ItemStack token = new ItemStack(Material.PAPER);

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 Version11 {
private final ToolStats toolStats;
/**
* Used for updating from version 10 to 11.
*
* @param toolStats ToolStats instance.
*/
public Version11(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-10.yml");
} catch (IOException exception) {
toolStats.logger.severe("Unable to save config-10.yml!");
throw new RuntimeException(exception);
}
// we make this super verbose so that admins can see what's being added
toolStats.logger.info("Updating config.yml to version 11.");
toolStats.config.set("config-version", 11);
// add new tokens
toolStats.logger.info("Adding tokens.data.damage-done.title to config.yml.");
toolStats.config.set("tokens.data.damage-done.title", "&7ToolStats: &8Damage Done Token");
List<String> damageDoneLore = new ArrayList<>();
damageDoneLore.add("&8Combine with a melee or ranged weapon in an anvil to track damage done.");
toolStats.config.set("tokens.data.damage-done.lore", damageDoneLore);
toolStats.logger.info("Adding tokens.data.damage-done.lore to config.yml.");
toolStats.config.set("tokens.data.damage-done.levels", 1);
toolStats.logger.info("Adding tokens.data.damage-done.levels to config.yml.");
toolStats.logger.info("Adding tokens.data.remove.title to config.yml.");
toolStats.config.set("tokens.data.remove.title", "&7ToolStats: &8Remove Token");
List<String> removeLore = new ArrayList<>();
removeLore.add("&8Combine in an anvil with to REMOVE ALL stats and tokens for this item.");
toolStats.config.set("tokens.data.remove.lore", removeLore);
toolStats.logger.info("Adding tokens.data.remove.lore to config.yml.");
toolStats.config.set("tokens.data.remove.levels", 1);
toolStats.logger.info("Adding tokens.data.remove.levels to config.yml.");
toolStats.logger.info("Adding messages.damage-done to config.yml.");
toolStats.config.set("messages.damage-done", "&7Damage done: &8{damage}");
toolStats.config.set("enabled.damage-done.sword", true);
toolStats.config.set("enabled.damage-done.axe", true);
toolStats.config.set("enabled.damage-done.trident", true);
toolStats.config.set("enabled.damage-done.bow", true);
toolStats.config.set("enabled.damage-done.mace", true);
toolStats.logger.info("Adding enabled.damage-done.sword to config.yml");
toolStats.logger.info("Adding enabled.damage-done.axe to config.yml");
toolStats.logger.info("Adding enabled.damage-done.trident to config.yml");
toolStats.logger.info("Adding enabled.damage-done.bow to config.yml");
toolStats.logger.info("Adding enabled.damage-done.mace to config.yml");
// 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 11. A copy of version 10 has been saved as config-10.yml");
}
}