adjust origins to listen to config

This commit is contained in:
hyperdefined
2025-01-28 22:51:49 -05:00
parent 8a1759b6b1
commit 3e1c2dcbc3
9 changed files with 107 additions and 88 deletions

View File

@@ -85,7 +85,7 @@ public class CraftItem implements Listener {
// if the slot was empty before we crafted, this means we just made it // if the slot was empty before we crafted, this means we just made it
if (oldSlotItem == null) { if (oldSlotItem == null) {
// add the lore // add the lore
ItemStack newItem = addLore(newSlotItem, player); ItemStack newItem = addCraftOrigin(newSlotItem, player);
if (newItem != null) { if (newItem != null) {
player.getInventory().setItem(i, newItem); player.getInventory().setItem(i, newItem);
} }
@@ -97,7 +97,7 @@ public class CraftItem implements Listener {
} }
// the player did not shift click // the player did not shift click
ItemStack newItem = addLore(craftedItem, player); ItemStack newItem = addCraftOrigin(craftedItem, player);
if (newItem != null) { if (newItem != null) {
// set the result // set the result
event.setCurrentItem(newItem); event.setCurrentItem(newItem);
@@ -111,7 +111,7 @@ public class CraftItem implements Listener {
* @param owner The player crafting. * @param owner The player crafting.
* @return A copy of the item with the tags + lore. * @return A copy of the item with the tags + lore.
*/ */
private ItemStack addLore(ItemStack itemStack, Player owner) { private ItemStack addCraftOrigin(ItemStack itemStack, Player owner) {
// clone the item // clone the item
ItemStack newItem = itemStack.clone(); ItemStack newItem = itemStack.clone();
ItemMeta meta = newItem.getItemMeta(); ItemMeta meta = newItem.getItemMeta();
@@ -130,25 +130,19 @@ public class CraftItem implements Listener {
return null; return null;
} }
// only make the hash if it's enabled
if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
List<Component> lore;
// get the current lore the item // get the current lore the item
List<Component> lore;
if (meta.hasLore()) { if (meta.hasLore()) {
lore = meta.lore(); lore = meta.lore();
} else { } else {
lore = new ArrayList<>(); lore = new ArrayList<>();
} }
// do we add the lore based on the config?
// if creation date is enabled, add it
if (toolStats.configTools.checkConfig(itemStack.getType(), "created-date")) { if (toolStats.configTools.checkConfig(itemStack.getType(), "created-date")) {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
String date = toolStats.numberFormat.formatDate(finalDate); String date = toolStats.numberFormat.formatDate(finalDate);
Component newLine = toolStats.configTools.formatLore("created.created-on", "{date}", date); Component newLine = toolStats.configTools.formatLore("created.created-on", "{date}", date);
if (newLine == null) { if (newLine == null) {
@@ -157,7 +151,12 @@ public class CraftItem implements Listener {
lore.add(newLine); lore.add(newLine);
meta.lore(lore); meta.lore(lore);
} }
// if creation owner is enabled, add it
if (toolStats.configTools.checkConfig(itemStack.getType(), "created-by")) { if (toolStats.configTools.checkConfig(itemStack.getType(), "created-by")) {
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
Component newLine = toolStats.configTools.formatLore("created.created-by", "{player}", owner.getName()); Component newLine = toolStats.configTools.formatLore("created.created-by", "{player}", owner.getName());
if (newLine == null) { if (newLine == null) {
return null; return null;
@@ -165,6 +164,12 @@ public class CraftItem implements Listener {
lore.add(newLine); lore.add(newLine);
meta.lore(lore); meta.lore(lore);
} }
// if hash is enabled, add it
if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }

View File

@@ -65,50 +65,53 @@ public class CreativeEvent implements Listener {
} }
// add the tags to the item // add the tags to the item
ItemStack newItem = addLore(spawnedItem, player); ItemStack newItem = addCreativeOrigin(spawnedItem, player);
if (newItem != null) { if (newItem != null) {
event.setCursor(newItem); event.setCursor(newItem);
} }
} }
/** /**
* Adds tags to newly spawned items in creative. * Adds spawned in tags to item.
* *
* @param spawnedItem The item. * @param itemStack The item add item to.
* @param owner The player spawning in.
* @return A copy of the item with the tags + lore.
*/ */
private ItemStack addLore(ItemStack spawnedItem, Player owner) { private ItemStack addCreativeOrigin(ItemStack itemStack, Player owner) {
ItemStack newSpawnedItem = spawnedItem.clone(); ItemStack newSpawnedItem = itemStack.clone();
ItemMeta meta = newSpawnedItem.getItemMeta(); ItemMeta meta = newSpawnedItem.getItemMeta();
if (meta == null) { if (meta == null) {
toolStats.logger.warning(newSpawnedItem + " does NOT have any meta! Unable to update stats."); toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
return null; return null;
} }
// get the current time
long timeCreated = System.currentTimeMillis(); long timeCreated = System.currentTimeMillis();
Date finalDate = new Date(timeCreated); Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
// if the item already has an origin set, don't add it again // if the item already has the tag
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) { // this is to prevent duplicate tags
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) {
return null; return null;
} }
// only make the hash if it's enabled // if hash is enabled, add it
if (toolStats.config.getBoolean("generate-hash-for-items")) { if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(spawnedItem.getType(), owner.getUniqueId(), timeCreated); String hash = toolStats.hashMaker.makeHash(newSpawnedItem.getType(), owner.getUniqueId(), timeCreated);
container.set(toolStats.hash, PersistentDataType.STRING, hash); container.set(toolStats.hash, PersistentDataType.STRING, hash);
} }
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated); // if spawned in is enabled, add it
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
if (toolStats.configTools.checkConfig(newSpawnedItem.getType(), "spawned-in")) { if (toolStats.configTools.checkConfig(newSpawnedItem.getType(), "spawned-in")) {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
String formattedDate = toolStats.numberFormat.formatDate(finalDate); String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate); List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
meta.lore(newLore); meta.lore(newLore);
} }
newSpawnedItem.setItemMeta(meta); newSpawnedItem.setItemMeta(meta);
return newSpawnedItem; return newSpawnedItem;
} }

View File

@@ -84,18 +84,19 @@ public class EntityDeath implements Listener {
return null; return null;
} }
if (!toolStats.config.getBoolean("enabled.dropped-by")) {
return null;
}
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(toolStats.originType, PersistentDataType.INTEGER, 1); container.set(toolStats.originType, PersistentDataType.INTEGER, 1);
String mobName = toolStats.config.getString("messages.mob." + entity.getType());
if (toolStats.config.getBoolean("enabled.dropped-by")) { if (mobName == null) {
String mobName = toolStats.config.getString("messages.mob." + entity.getType()); mobName = entity.getName();
if (mobName == null) {
mobName = entity.getName();
}
Component newLine = toolStats.configTools.formatLore("dropped-by", "{name}", mobName);
List<Component> newLore = toolStats.itemLore.addItemLore(meta, newLine);
meta.lore(newLore);
} }
Component newLine = toolStats.configTools.formatLore("dropped-by", "{name}", mobName);
List<Component> newLore = toolStats.itemLore.addItemLore(meta, newLine);
meta.lore(newLore);
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }

View File

@@ -92,7 +92,7 @@ public class GenerateLoot implements Listener {
* @param owner The player that found the item. * @param owner The player that found the item.
* @return The item with the lore. * @return The item with the lore.
*/ */
private ItemStack addLore(ItemStack itemStack, Player owner) { private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
ItemStack newItem = itemStack.clone(); ItemStack newItem = itemStack.clone();
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
if (meta == null) { if (meta == null) {
@@ -102,6 +102,10 @@ public class GenerateLoot implements Listener {
Date finalDate = new Date(timeCreated); Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
if (!toolStats.configTools.checkConfig(newItem.getType(), "looted-tag")) {
return null;
}
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) {
return null; return null;
} }
@@ -115,12 +119,9 @@ public class GenerateLoot implements Listener {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated); container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId()); container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 2); container.set(toolStats.originType, PersistentDataType.INTEGER, 2);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
if (toolStats.configTools.checkConfig(newItem.getType(), "looted-tag")) { List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
String formattedDate = toolStats.numberFormat.formatDate(finalDate); meta.lore(newLore);
List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
meta.lore(newLore);
}
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }
@@ -139,7 +140,7 @@ public class GenerateLoot implements Listener {
continue; continue;
} }
if (toolStats.itemChecker.isValidItem(itemStack.getType())) { if (toolStats.itemChecker.isValidItem(itemStack.getType())) {
ItemStack newItem = addLore(itemStack, player); ItemStack newItem = addLootedOrigin(itemStack, player);
if (newItem != null) { if (newItem != null) {
loot.set(i, newItem); loot.set(i, newItem);
} }

View File

@@ -22,7 +22,10 @@ import lol.hyper.toolstats.tools.UUIDDataType;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.*; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@@ -64,7 +67,7 @@ public class PickupItem implements Listener {
if (itemStack.getType() == Material.ELYTRA) { if (itemStack.getType() == Material.ELYTRA) {
// the elytra has the new key, set the lore to it // the elytra has the new key, set the lore to it
if (container.has(toolStats.newElytra, PersistentDataType.INTEGER)) { if (container.has(toolStats.newElytra, PersistentDataType.INTEGER)) {
ItemStack newElytra = addLore(itemStack, (Player) event.getEntity()); ItemStack newElytra = addElytraOrigin(itemStack, (Player) event.getEntity());
if (newElytra != null) { if (newElytra != null) {
item.setItemStack(newElytra); item.setItemStack(newElytra);
} }
@@ -80,7 +83,7 @@ public class PickupItem implements Listener {
* @param itemStack The elytra to add lore to. * @param itemStack The elytra to add lore to.
* @param owner The player who found it. * @param owner The player who found it.
*/ */
private ItemStack addLore(ItemStack itemStack, Player owner) { private ItemStack addElytraOrigin(ItemStack itemStack, Player owner) {
ItemStack finalItem = itemStack.clone(); ItemStack finalItem = itemStack.clone();
ItemMeta meta = finalItem.getItemMeta(); ItemMeta meta = finalItem.getItemMeta();
if (meta == null) { if (meta == null) {
@@ -90,6 +93,10 @@ public class PickupItem implements Listener {
Date finalDate = new Date(timeCreated); Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
if (!toolStats.config.getBoolean("enabled.elytra-tag")) {
return null;
}
// only make the hash if it's enabled // only make the hash if it's enabled
if (toolStats.config.getBoolean("generate-hash-for-items")) { if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(finalItem.getType(), owner.getUniqueId(), timeCreated); String hash = toolStats.hashMaker.makeHash(finalItem.getType(), owner.getUniqueId(), timeCreated);
@@ -100,12 +107,9 @@ public class PickupItem implements Listener {
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId()); container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 4); container.set(toolStats.originType, PersistentDataType.INTEGER, 4);
container.remove(toolStats.newElytra); container.remove(toolStats.newElytra);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
if (toolStats.config.getBoolean("enabled.elytra-tag")) { List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
String formattedDate = toolStats.numberFormat.formatDate(finalDate); meta.lore(newLore);
List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
meta.lore(newLore);
}
finalItem.setItemMeta(meta); finalItem.setItemMeta(meta);
return finalItem; return finalItem;
} }

View File

@@ -111,22 +111,26 @@ public class PlayerFish implements Listener {
Date finalDate = new Date(timeCreated); Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer(); PersistentDataContainer container = meta.getPersistentDataContainer();
if (!toolStats.configTools.checkConfig(newItem.getType(), "fished-tag")) {
return null;
}
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) {
return null; return null;
} }
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated); // only make the hash if it's enabled
if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
container.set(toolStats.hash, PersistentDataType.STRING, hash);
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated); container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId()); container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 5); container.set(toolStats.originType, PersistentDataType.INTEGER, 5);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
if (toolStats.configTools.checkConfig(newItem.getType(), "fished-tag")) { List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
String formattedDate = toolStats.numberFormat.formatDate(finalDate); meta.lore(newLore);
List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
meta.lore(newLore);
}
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }

View File

@@ -94,7 +94,7 @@ public class VillagerTrade implements Listener {
// if the slot was empty before we traded, this means we just traded it // if the slot was empty before we traded, this means we just traded it
if (oldSlotItem == null) { if (oldSlotItem == null) {
// add the lore // add the lore
ItemStack newItem = addLore(newSlotItem, player); ItemStack newItem = addTradeOrigin(newSlotItem, player);
if (newItem != null) { if (newItem != null) {
player.getInventory().setItem(i, newItem); player.getInventory().setItem(i, newItem);
} }
@@ -104,7 +104,7 @@ public class VillagerTrade implements Listener {
}, null, 1); }, null, 1);
return; return;
} }
ItemStack newItem = addLore(tradedItem, player); ItemStack newItem = addTradeOrigin(tradedItem, player);
if (newItem != null) { if (newItem != null) {
// set the new item // set the new item
inventory.setItem(event.getSlot(), newItem); inventory.setItem(event.getSlot(), newItem);
@@ -118,7 +118,7 @@ public class VillagerTrade implements Listener {
* @param owner The player who traded. * @param owner The player who traded.
* @return The item with lore. * @return The item with lore.
*/ */
private ItemStack addLore(ItemStack oldItem, Player owner) { private ItemStack addTradeOrigin(ItemStack oldItem, Player owner) {
ItemStack newItem = oldItem.clone(); ItemStack newItem = oldItem.clone();
ItemMeta meta = newItem.getItemMeta(); ItemMeta meta = newItem.getItemMeta();
if (meta == null) { if (meta == null) {
@@ -133,6 +133,10 @@ public class VillagerTrade implements Listener {
return null; return null;
} }
if (!toolStats.configTools.checkConfig(newItem.getType(), "traded-tag")) {
return null;
}
// only make the hash if it's enabled // only make the hash if it's enabled
if (toolStats.config.getBoolean("generate-hash-for-items")) { if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated); String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated);
@@ -142,12 +146,9 @@ public class VillagerTrade implements Listener {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated); container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId()); container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 3); container.set(toolStats.originType, PersistentDataType.INTEGER, 3);
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
if (toolStats.configTools.checkConfig(newItem.getType(), "traded-tag")) { List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
String formattedDate = toolStats.numberFormat.formatDate(finalDate); meta.lore(newLore);
List<Component> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
meta.lore(newLore);
}
newItem.setItemMeta(meta); newItem.setItemMeta(meta);
return newItem; return newItem;
} }

View File

@@ -88,6 +88,19 @@ public class ItemLore {
return itemLore; return itemLore;
} }
/**
* Remove a given lore from an item.
*
* @param inputLore The item's lore.
* @param toRemove The line to remove.
* @return The lore with the line removed.
*/
public List<Component> removeLore(List<Component> inputLore, Component toRemove) {
List<Component> newLore = new ArrayList<>(inputLore);
newLore.removeIf(line -> PlainTextComponentSerializer.plainText().serialize(line).equals(PlainTextComponentSerializer.plainText().serialize(toRemove)));
return newLore;
}
/** /**
* Adds new ownership to an item. * Adds new ownership to an item.
* *
@@ -977,17 +990,4 @@ public class ItemLore {
meta.lore(newLore); meta.lore(newLore);
return meta; return meta;
} }
/**
* Remove a given lore from an item.
*
* @param inputLore The item's lore.
* @param toRemove The line to remove.
* @return The lore with the line removed.
*/
public List<Component> removeLore(List<Component> inputLore, Component toRemove) {
List<Component> newLore = new ArrayList<>(inputLore);
newLore.removeIf(line -> PlainTextComponentSerializer.plainText().serialize(line).equals(PlainTextComponentSerializer.plainText().serialize(toRemove)));
return newLore;
}
} }

View File

@@ -45,7 +45,7 @@ public class ConfigTools {
* *
* @param material The item type to check. * @param material The item type to check.
* @param configName The config we are checking under. * @param configName The config we are checking under.
* @return If we want to allow lore or not. * @return If we want to add data or not.
*/ */
public boolean checkConfig(Material material, String configName) { public boolean checkConfig(Material material, String configName) {
String itemName = material.toString().toLowerCase(); String itemName = material.toString().toLowerCase();