lots of updates

- add hash to items when created
- added "crops harvested" for hoes
This commit is contained in:
hyperdefined
2023-09-30 20:05:31 -04:00
parent 0a3f46fc6e
commit 5551c24202
14 changed files with 238 additions and 57 deletions

View File

@@ -19,7 +19,10 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.UUIDDataType;
import org.bukkit.GameMode;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -32,6 +35,8 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
public class BlocksMined implements Listener {
@@ -50,15 +55,23 @@ public class BlocksMined implements Listener {
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
return;
}
// if the player mines something with their fist
PlayerInventory inventory = player.getInventory();
ItemStack heldItem = inventory.getItemInMainHand();
Block block = event.getBlock();
// only check certain items
if (!ItemChecker.isMineTool(heldItem.getType())) {
return;
}
// update the blocks mined
updateBlocksMined(heldItem);
if (heldItem.getType().toString().toLowerCase(Locale.ROOT).contains("hoe")) {
// player is breaking crops with a hoe
if (block.getBlockData() instanceof Ageable) {
updateCropsMined(heldItem, (Ageable) block.getBlockData());
}
} else {
// update the blocks mined
updateBlocksMined(heldItem);
}
}
private void updateBlocksMined(ItemStack playerTool) {
@@ -87,7 +100,44 @@ public class BlocksMined implements Listener {
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{blocks}", blocksMinedFormatted, "blocks-mined");
// do we add the lore based on the config?
if (toolStats.checkConfig(playerTool, "blocks-mined")) {
if (toolStats.checkConfig(playerTool.getType(), "blocks-mined")) {
meta.setLore(newLore);
}
playerTool.setItemMeta(meta);
}
private void updateCropsMined(ItemStack playerTool, Ageable block) {
// ignore crops that are not fully grown
if (block.getAge() != block.getMaximumAge()) {
return;
}
ItemMeta meta = playerTool.getItemMeta();
if (meta == null) {
toolStats.logger.warning(playerTool + " does NOT have any meta! Unable to update stats.");
return;
}
// read the current stats from the item
// if they don't exist, then start from 0
Integer cropsMined = 0;
PersistentDataContainer container = meta.getPersistentDataContainer();
if (container.has(toolStats.cropsHarvested, PersistentDataType.INTEGER)) {
cropsMined = container.get(toolStats.cropsHarvested, PersistentDataType.INTEGER);
}
if (cropsMined == null) {
cropsMined = 0;
toolStats.logger.warning(playerTool + " does not have valid crops-mined set! Resting to zero. This should NEVER happen.");
}
cropsMined++;
container.set(toolStats.cropsHarvested, PersistentDataType.INTEGER, cropsMined);
String cropsMinedFormatted = toolStats.numberFormat.formatInt(cropsMined);
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{crops}", cropsMinedFormatted, "crops-harvested");
// do we add the lore based on the config?
if (toolStats.checkConfig(playerTool.getType(), "blocks-mined")) {
meta.setLore(newLore);
}
playerTool.setItemMeta(meta);

View File

@@ -104,6 +104,9 @@ public class CraftItem implements Listener {
return null;
}
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.genericOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 0);
@@ -128,10 +131,10 @@ public class CraftItem implements Listener {
lore = new ArrayList<>();
}
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "created-date")) {
if (toolStats.checkConfig(itemStack.getType(), "created-date")) {
lore.add(createdOnRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
}
if (toolStats.checkConfig(itemStack, "created-by")) {
if (toolStats.checkConfig(itemStack.getType(), "created-by")) {
lore.add(createdByRaw.replace("{player}", owner.getName()));
}
meta.setLore(lore);

View File

@@ -249,7 +249,7 @@ public class EntityDamage implements Listener {
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", playerKillsFormatted, "kills.player");
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "player-kills")) {
if (toolStats.checkConfig(itemStack.getType(), "player-kills")) {
meta.setLore(newLore);
}
itemStack.setItemMeta(meta);
@@ -284,7 +284,7 @@ public class EntityDamage implements Listener {
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", mobKillsFormatted, "kills.mob");
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "mob-kills")) {
if (toolStats.checkConfig(itemStack.getType(), "mob-kills")) {
meta.setLore(newLore);
}
itemStack.setItemMeta(meta);
@@ -356,7 +356,7 @@ public class EntityDamage implements Listener {
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", mobKillsFormatted, "kills.mob");
// do we add the lore based on the config?
if (toolStats.checkConfig(newTrident, "mob-kills")) {
if (toolStats.checkConfig(newTrident.getType(), "mob-kills")) {
meta.setLore(newLore);
}
newTrident.setItemMeta(meta);
@@ -393,7 +393,7 @@ public class EntityDamage implements Listener {
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{kills}", playerKillsFormatted, "kills.player");
// do we add the lore based on the config?
if (toolStats.checkConfig(newTrident, "player-kills")) {
if (toolStats.checkConfig(newTrident.getType(), "player-kills")) {
meta.setLore(newLore);
}
newTrident.setItemMeta(meta);

View File

@@ -144,6 +144,9 @@ public class GenerateLoot implements Listener {
return null;
}
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.genericOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 2);
@@ -151,7 +154,7 @@ public class GenerateLoot implements Listener {
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
if (toolStats.checkConfig(newItem, "looted-tag")) {
if (toolStats.checkConfig(newItem.getType(), "looted-tag")) {
meta.setLore(newLore);
}
newItem.setItemMeta(meta);

View File

@@ -19,6 +19,7 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.UUIDDataType;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -32,7 +33,7 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
import java.util.UUID;
public class InventoryOpen implements Listener {
@@ -49,36 +50,54 @@ public class InventoryOpen implements Listener {
}
Inventory inventory = event.getInventory();
Location location = event.getInventory().getLocation();
for (ItemStack itemStack : inventory) {
if (itemStack == null) {
continue;
}
// ignore items that are not the right type
if (!ItemChecker.isValidItem(itemStack.getType())) {
continue;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
continue;
}
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
// ignore any items that already have the origin tag
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
continue;
}
// ignore items that are not the right type
if (!ItemChecker.isValidItem(itemStack.getType())) {
continue;
// generate a hash if the item doesn't have one
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
// make sure the item has an owner
if (!container.has(toolStats.genericOwner, new UUIDDataType())) {
continue;
}
UUID owner = container.get(toolStats.genericOwner, new UUIDDataType());
if (owner == null) {
continue;
}
Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG);
if (timestamp == null) {
continue;
}
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
toolStats.logger.info(hash);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
ItemMeta newMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
if (newMeta == null) {
continue;
// add origin tag
if (!container.has(toolStats.originType, PersistentDataType.INTEGER)) {
itemMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
if (itemMeta == null) {
continue;
}
}
ItemMeta clone = itemMeta.clone();
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
itemStack.setItemMeta(newMeta);
itemStack.setItemMeta(clone);
}
};
Location location = inventory.getLocation();
// only run for actual inventories
if (location != null) {
toolStats.scheduleRegion(runnable, location.getWorld(), location.getChunk(), 1);
}

View File

@@ -155,6 +155,9 @@ public class PlayerFish implements Listener {
return null;
}
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.genericOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 5);
@@ -162,7 +165,7 @@ public class PlayerFish implements Listener {
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
if (toolStats.checkConfig(newItem, "fished-tag")) {
if (toolStats.checkConfig(newItem.getType(), "fished-tag")) {
meta.setLore(newLore);
}
newItem.setItemMeta(meta);

View File

@@ -19,7 +19,7 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import org.bukkit.Location;
import lol.hyper.toolstats.tools.UUIDDataType;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -32,6 +32,8 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.UUID;
public class PlayerJoin implements Listener {
private final ToolStats toolStats;
@@ -49,35 +51,50 @@ public class PlayerJoin implements Listener {
if (itemStack == null) {
continue;
}
// ignore items that are not the right type
if (!ItemChecker.isValidItem(itemStack.getType())) {
continue;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
continue;
}
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
// ignore any items that already have the origin tag
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
continue;
}
// ignore items that are not the right type
if (!ItemChecker.isValidItem(itemStack.getType())) {
continue;
// generate a hash if the item doesn't have one
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
// make sure the item has an owner
if (!container.has(toolStats.genericOwner, new UUIDDataType())) {
continue;
}
UUID owner = container.get(toolStats.genericOwner, new UUIDDataType());
if (owner == null) {
continue;
}
Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG);
if (timestamp == null) {
continue;
}
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
toolStats.logger.info(hash);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
ItemMeta newMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
if (newMeta == null) {
continue;
// add origin tag
if (!container.has(toolStats.originType, PersistentDataType.INTEGER)) {
itemMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
if (itemMeta == null) {
continue;
}
}
ItemMeta clone = itemMeta.clone();
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
itemStack.setItemMeta(newMeta);
itemStack.setItemMeta(clone);
}
};
Location location = inventory.getLocation();
// only run for actual inventories
if (location != null) {
toolStats.scheduleRegion(runnable, location.getWorld(), location.getChunk(), 1);
}
toolStats.scheduleEntity(runnable, player, 1);
}
}
}

View File

@@ -106,6 +106,9 @@ public class VillagerTrade implements Listener {
return null;
}
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.genericOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 3);
@@ -113,7 +116,7 @@ public class VillagerTrade implements Listener {
String formattedDate = toolStats.numberFormat.formatDate(finalDate);
List<String> newLore = toolStats.itemLore.addNewOwner(meta, owner.getName(), formattedDate);
if (toolStats.checkConfig(newItem, "traded-tag")) {
if (toolStats.checkConfig(newItem.getType(), "traded-tag")) {
meta.setLore(newLore);
}
newItem.setItemMeta(meta);