add trident throws

This commit is contained in:
hyperdefined
2026-01-14 17:44:33 -05:00
parent 25e5a13095
commit 8a4da8ad56
7 changed files with 272 additions and 0 deletions

View File

@@ -119,6 +119,10 @@ public final class ToolStats extends JavaPlugin {
* Key for critical strikes.
*/
public final NamespacedKey criticalStrikes = new NamespacedKey(this, "critical-strikes");
/**
* Key for trident throws.
*/
public final NamespacedKey tridentThrows = new NamespacedKey(this, "trident-throws");
/**
* Stores how an item was created.
@@ -167,6 +171,7 @@ public final class ToolStats extends JavaPlugin {
public BlockDispenseEvent blockDispenseEvent;
public HyperLib hyperLib;
public TextUtils textUtils;
public ProjectileShoot projectileShoot;
@Override
public void onEnable() {
@@ -216,6 +221,7 @@ public final class ToolStats extends JavaPlugin {
anvilEvent = new AnvilEvent(this);
prepareCraft = new PrepareCraft(this);
blockDispenseEvent = new BlockDispenseEvent(this);
projectileShoot = new ProjectileShoot(this);
// save which stat can be used by a reset token
tokenKeys.add(blocksMined);
@@ -227,6 +233,10 @@ public final class ToolStats extends JavaPlugin {
tokenKeys.add(flightTime);
tokenKeys.add(arrowsShot);
tokenKeys.add(armorDamage);
tokenKeys.add(witherKills);
tokenKeys.add(enderDragonKills);
tokenKeys.add(criticalStrikes);
tokenKeys.add(tridentThrows);
Bukkit.getServer().getPluginManager().registerEvents(blockBreak, this);
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
@@ -247,6 +257,7 @@ public final class ToolStats extends JavaPlugin {
Bukkit.getServer().getPluginManager().registerEvents(anvilEvent, this);
Bukkit.getServer().getPluginManager().registerEvents(prepareCraft, this);
Bukkit.getServer().getPluginManager().registerEvents(blockDispenseEvent, this);
Bukkit.getServer().getPluginManager().registerEvents(projectileShoot, this);
this.getCommand("toolstats").setExecutor(commandToolStats);

View File

@@ -414,6 +414,14 @@ public class CommandToolStats implements TabExecutor {
}
}
}
if (toolStats.config.getBoolean("enabled.trident-throws")) {
if (container.has(toolStats.tridentThrows, PersistentDataType.INTEGER)) {
Integer tridentThrows = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
if (tridentThrows != null) {
lore.add(toolStats.configTools.formatLore("trident-throws", "{times}", toolStats.numberFormat.formatInt(tridentThrows)));
}
}
}
finalMeta.lore(lore);
finalItem.setItemMeta(finalMeta);
int slot = player.getInventory().getHeldItemSlot();
@@ -825,6 +833,35 @@ public class CommandToolStats implements TabExecutor {
}
break;
}
case "trident-throws": {
if (!toolStats.config.getBoolean("enabled.trident-throws")) {
player.sendMessage(Component.text("This stat is disabled.", NamedTextColor.RED));
return;
}
if (container.has(toolStats.tridentThrows)) {
int value;
try {
value = Integer.parseInt((String) userValue);
} catch (NumberFormatException exception) {
player.sendMessage(Component.text("That is not a valid number.", NamedTextColor.RED));
return;
}
if (value < 0) {
player.sendMessage(Component.text("Number must be positive.", NamedTextColor.RED));
return;
}
Integer statValue = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
if (statValue == null) {
player.sendMessage(Component.text("Unable to get stat from item.", NamedTextColor.RED));
return;
}
int difference = value - statValue;
editedItemMeta = toolStats.itemLore.updateTridentThrows(editedItem, difference);
} else {
player.sendMessage(Component.text("This item does not have that stat.", NamedTextColor.RED));
}
break;
}
default: {
player.sendMessage(Component.text("That is not a valid stat to update.", NamedTextColor.RED));
return;
@@ -1187,6 +1224,34 @@ public class CommandToolStats implements TabExecutor {
}
break;
}
case "trident-throws": {
if (container.has(toolStats.tridentThrows)) {
Integer statValue = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
if (statValue == null) {
player.sendMessage(Component.text("Unable to get stat from item.", NamedTextColor.RED));
return;
}
String tokens = container.get(toolStats.tokenApplied, PersistentDataType.STRING);
if (tokens == null) {
player.sendMessage(Component.text("Unable to get tokens from item.", NamedTextColor.RED));
return;
}
container.remove(toolStats.tridentThrows);
List<String> newTokens = toolStats.itemChecker.removeToken(tokens, "trident-throws");
if (newTokens.isEmpty()) {
container.remove(toolStats.tokenApplied);
} else {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, String.join(",", newTokens));
}
Component oldLine = toolStats.configTools.formatLore("trident-throws", "{times}", toolStats.numberFormat.formatInt(statValue));
List<Component> newLore = toolStats.itemLore.removeLore(editedItemMeta.lore(), oldLine);
editedItemMeta.lore(newLore);
} else {
player.sendMessage(Component.text("This item does not have that stat.", NamedTextColor.RED));
}
break;
}
default: {
player.sendMessage(Component.text("That is not a valid stat to update.", NamedTextColor.RED));
return;

View File

@@ -175,6 +175,10 @@ public class AnvilEvent implements Listener {
addToken(event, tokenType, "critical-strikes", clone);
return;
}
if (tokenType.equalsIgnoreCase("trident-throws")) {
addToken(event, tokenType, "trident-throws", clone);
return;
}
return;
}
if (firstSlotMaterial == Material.BOW || firstSlotMaterial == Material.CROSSBOW) {
@@ -350,6 +354,15 @@ public class AnvilEvent implements Listener {
}
break;
}
case "trident-throws": {
if (toolStats.config.getBoolean("enabled.trident-throws")) {
newItem.setItemMeta(toolStats.itemLore.updateTridentThrows(newItem, 0));
} else {
event.setResult(null);
return;
}
break;
}
}
event.setResult(newItem);
event.getView().setRepairCost(toolStats.itemChecker.getCost(targetToken));
@@ -475,6 +488,14 @@ public class AnvilEvent implements Listener {
meta = toolStats.itemLore.updateCriticalStrikes(finalItem, -criticalStrikes);
finalItem.setItemMeta(meta);
}
if (container.has(toolStats.tridentThrows)) {
Integer tridentThrows = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
if (tridentThrows == null) {
return;
}
meta = toolStats.itemLore.updateTridentThrows(finalItem, -tridentThrows);
finalItem.setItemMeta(meta);
}
event.setResult(finalItem);
event.getView().setRepairCost(toolStats.itemChecker.getCost("reset"));
}

View File

@@ -0,0 +1,55 @@
/*
* 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.events;
import com.destroystokyo.paper.event.player.PlayerLaunchProjectileEvent;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.entity.Trident;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
;
public class ProjectileShoot implements Listener {
private final ToolStats toolStats;
public ProjectileShoot(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onProjectileLaunch(PlayerLaunchProjectileEvent event) {
if (!(event.getProjectile() instanceof Trident tridentEntity)) {
return;
}
tridentEntity.getScheduler().runDelayed(toolStats, scheduledTask -> {
ItemStack tridentStack = tridentEntity.getItemStack();
ItemMeta newTridentMeta = toolStats.itemLore.updateTridentThrows(tridentStack, 1);
if (newTridentMeta == null) {
return;
}
tridentStack.setItemMeta(newTridentMeta);
tridentEntity.setItemStack(tridentStack);
}, null, 1);
}
}

View File

@@ -1315,6 +1315,105 @@ public class ItemLore {
return meta;
}
/**
* Add x to trident throws.
*
* @param trident The trident used.
*/
public ItemMeta updateTridentThrows(ItemStack trident, int add) {
ItemStack clone = trident.clone();
ItemMeta meta = clone.getItemMeta();
if (meta == null) {
toolStats.logger.warn("{} does NOT have any meta! Unable to update stats.", clone);
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.config.getBoolean("enabled.trident-throws")) {
if (container.has(toolStats.tridentThrows)) {
Integer tridentThrows = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
if (tridentThrows == null) {
return null;
}
container.remove(toolStats.tridentThrows);
// remove the applied token if this stat is disabled
if (container.has(toolStats.tokenApplied)) {
String appliedTokens = container.get(toolStats.tokenApplied, PersistentDataType.STRING);
if (appliedTokens != null) {
// remove the token from the list
// if the list is empty, remove the PDC
// otherwise set the PDC back with the new list
List<String> newTokens = toolStats.itemChecker.removeToken(appliedTokens, "trident-throws");
if (!newTokens.isEmpty()) {
container.set(toolStats.tokenApplied, PersistentDataType.STRING, String.join(",", newTokens));
} else {
container.remove(toolStats.tokenApplied);
}
}
}
if (meta.hasLore()) {
String oldTridentThrows = toolStats.numberFormat.formatInt(tridentThrows);
Component lineToRemove = toolStats.configTools.formatLore("trident-throws", "{times}", oldTridentThrows);
List<Component> newLore = removeLore(meta.lore(), lineToRemove);
meta.lore(newLore);
}
return meta;
}
return null;
}
// check for tokens
boolean validToken = toolStats.itemChecker.checkTokens(container, "trident-throws");
// check for tokens
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the item has stats but no token, add the token
if (container.has(toolStats.tridentThrows) && !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);
}
}
}
Integer tridentThrows = 0;
if (container.has(toolStats.tridentThrows, PersistentDataType.INTEGER)) {
tridentThrows = container.get(toolStats.tridentThrows, PersistentDataType.INTEGER);
}
if (tridentThrows == null) {
tridentThrows = 0;
toolStats.logger.warn("{} does not have valid fish-caught set! Resting to zero. This should NEVER happen.", clone);
}
container.set(toolStats.tridentThrows, PersistentDataType.INTEGER, tridentThrows + add);
String oldTridentThrowsFormatted = toolStats.numberFormat.formatInt(tridentThrows);
String newTridentThrowsFormatted = toolStats.numberFormat.formatInt(tridentThrows + add);
Component oldLine = toolStats.configTools.formatLore("trident-throws", "{times}", oldTridentThrowsFormatted);
Component newLine = toolStats.configTools.formatLore("trident-throws", "{times}", newTridentThrowsFormatted);
if (oldLine == null || newLine == null) {
return null;
}
List<Component> newLore = updateItemLore(meta, oldLine, newLine);
meta.lore(newLore);
toolStats.logger.info(meta.toString());
return meta;
}
/**
* Format the item owner lore.
*

View File

@@ -146,6 +146,13 @@ public class TokenData {
criticalStrikesRecipe.setIngredient('S', Material.GOLDEN_SWORD);
recipes.add(criticalStrikesRecipe);
NamespacedKey tridentThrowsKey = new NamespacedKey(toolStats, "trident-throws-token");
ShapedRecipe tridentThrowsRecipe = new ShapedRecipe(tridentThrowsKey, createToken("trident-throws"));
tridentThrowsRecipe.shape(" P ", "PSP", " P ");
tridentThrowsRecipe.setIngredient('P', Material.PAPER);
tridentThrowsRecipe.setIngredient('S', Material.PRISMARINE_SHARD);
recipes.add(tridentThrowsRecipe);
tokenTypes.add("crops-mined");
tokenTypes.add("blocks-mined");
tokenTypes.add("damage-taken");
@@ -161,6 +168,7 @@ public class TokenData {
tokenTypes.add("wither-kills");
tokenTypes.add("enderdragon-kills");
tokenTypes.add("critical-strikes");
tokenTypes.add("trident-throws");
}
public Set<ShapedRecipe> getRecipes() {

View File

@@ -169,6 +169,17 @@ tokens:
enabled: false
type: float
value: 1001
trident-throws:
title: "&7ToolStats: &8Trident Throws Token"
lore:
- "&8Combine with a trident in an anvil to track times thrown."
- "&8Uses &7{levels} &8level."
levels: 1
material: PAPER
custom-model-data:
enabled: false
type: float
value: 1001
enabled:
# Will show "Crafted by <player>"
@@ -331,6 +342,7 @@ enabled:
flight-time: true
crops-harvested: true
critical-strikes: true
trident-throws: true
messages:
crafted:
@@ -367,6 +379,7 @@ messages:
flight-time: "&7Flight time: &8{years}y {months}m {days}d {hours}h {minutes}m {seconds}s"
damage-done: "&7Damage done: &8{damage}"
critical-strikes: "&7Critical strikes: &8{strikes}"
trident-throws: "&7Times thrown: &8{times}"
# Set display name for mobs. See: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html
mobs:
ZOMBIE: "Zombie"