added more trackers

* when opening a loot chest
* when trading items
* when fishing for items
This commit is contained in:
hyperdefined
2022-01-30 15:36:33 -05:00
parent 3cf6554984
commit 8736519d34
6 changed files with 318 additions and 17 deletions

View File

@@ -37,10 +37,10 @@ import java.util.logging.Logger;
public final class ToolStats extends JavaPlugin {
// stores who crafted an item
public final NamespacedKey craftedOwner = new NamespacedKey(this, "owner");
// stores when an item was crafted
public final NamespacedKey craftedTime = new NamespacedKey(this, "time-created");
// stores who created an item
public final NamespacedKey genericOwner = new NamespacedKey(this, "owner");
// stores when an item was created
public final NamespacedKey timeCreated = new NamespacedKey(this, "time-created");
// stores how many player kills by sword
public final NamespacedKey swordPlayerKills = new NamespacedKey(this, "player-kills");
// stores how many mob kills by sword
@@ -59,15 +59,17 @@ public final class ToolStats extends JavaPlugin {
public BlocksMined blocksMined;
public CraftItem craftItem;
public EntityDeath entityDeath;
public GenerateLoot generateLoot;
public EntityDamage mobKill;
public PlayerFish playerFish;
public SheepShear sheepShear;
public VillagerTrade villagerTrade;
public CommandToolStats commandToolStats;
public final Logger logger = this.getLogger();
public final File configFile = new File(this.getDataFolder(), "config.yml");
public FileConfiguration config;
public final int CONFIG_VERSION = 1;
public final int CONFIG_VERSION = 2;
@Override
public void onEnable() {
@@ -79,17 +81,21 @@ public final class ToolStats extends JavaPlugin {
blocksMined = new BlocksMined(this);
craftItem = new CraftItem(this);
entityDeath = new EntityDeath(this);
generateLoot = new GenerateLoot(this);
mobKill = new EntityDamage(this);
playerFish = new PlayerFish(this);
sheepShear = new SheepShear(this);
villagerTrade = new VillagerTrade(this);
commandToolStats = new CommandToolStats(this);
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
Bukkit.getServer().getPluginManager().registerEvents(craftItem, this);
Bukkit.getServer().getPluginManager().registerEvents(entityDeath, this);
Bukkit.getServer().getPluginManager().registerEvents(generateLoot, this);
Bukkit.getServer().getPluginManager().registerEvents(mobKill, this);
Bukkit.getServer().getPluginManager().registerEvents(playerFish, this);
Bukkit.getServer().getPluginManager().registerEvents(sheepShear, this);
Bukkit.getServer().getPluginManager().registerEvents(villagerTrade, this);
this.getCommand("toolstats").setExecutor(commandToolStats);
@@ -97,8 +103,8 @@ public final class ToolStats extends JavaPlugin {
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
keys.add(craftedOwner);
keys.add(craftedTime);
keys.add(genericOwner);
keys.add(timeCreated);
keys.add(swordPlayerKills);
keys.add(swordMobKills);
keys.add(genericMined);
@@ -177,7 +183,10 @@ public final class ToolStats extends JavaPlugin {
case "bow": {
return config.getBoolean("enabled." + configName + ".bow");
}
case "armor": {
case "helmet":
case "chestplate":
case "leggings":
case "boots": {
return config.getBoolean("enabled." + configName + ".armor");
}
}

View File

@@ -71,8 +71,8 @@ public class CraftItem implements Listener {
long timeCreated = System.currentTimeMillis();
Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(toolStats.craftedTime, PersistentDataType.LONG, timeCreated);
container.set(toolStats.craftedOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
@@ -80,10 +80,10 @@ public class CraftItem implements Listener {
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(itemStack, "crafted-date")) {
if (toolStats.checkConfig(itemStack, "created-date")) {
lore.add(timeCreatedLore.replace("X", format.format(finalDate)));
}
if (toolStats.checkConfig(itemStack, "crafted-by")) {
if (toolStats.checkConfig(itemStack, "created-by")) {
lore.add(ownerLore.replace("X", owner.getName()));
}
meta.setLore(lore);

View File

@@ -0,0 +1,114 @@
/*
* 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 lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.UUIDDataType;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.LootGenerateEvent;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class GenerateLoot implements Listener {
private final ToolStats toolStats;
private final String LOOT_OWNER = ChatColor.GRAY + "Looted by: " + ChatColor.DARK_GRAY + "X";
private final String LOOT_TIME = ChatColor.GRAY + "Looted on: " + ChatColor.DARK_GRAY + "X";
public final String[] validItems = {
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
};
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public GenerateLoot(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler
public void onGenerateLoot(LootGenerateEvent event) {
InventoryHolder inventoryHolder = event.getInventoryHolder();
if (inventoryHolder == null) {
return;
}
Inventory chest = inventoryHolder.getInventory();
toolStats.logger.info(String.valueOf(chest.getContents().length));
toolStats.logger.info(String.valueOf(chest.getViewers()));
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
toolStats.logger.info("Later: " + chest.getViewers());
Player player = (Player) chest.getViewers().get(0);
for (int i = 0; i < chest.getContents().length; i++) {
ItemStack itemStack = chest.getItem(i);
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
for (String x : validItems) {
toolStats.logger.info("Checking " + name);
if (name.contains(x)) {
toolStats.logger.info("contains");
chest.setItem(i, addLore(itemStack, player));
}
}
}
},1);
}
private ItemStack addLore(ItemStack itemStack, Player owner) {
ItemStack newItem = itemStack.clone();
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return null;
}
long timeCreated = System.currentTimeMillis();
Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
assert lore != null;
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "looted-tag")) {
toolStats.logger.info("adding lore to new item in chest");
lore.add(LOOT_TIME.replace("X", format.format(finalDate)));
lore.add(LOOT_OWNER.replace("X", owner.getName()));
}
meta.setLore(lore);
newItem.setItemMeta(meta);
return newItem;
}
}

View File

@@ -18,8 +18,10 @@
package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.UUIDDataType;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -29,13 +31,22 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class PlayerFish implements Listener {
private final ToolStats toolStats;
private final String fishCaughtLore = ChatColor.GRAY + "Fish caught: " + ChatColor.DARK_GRAY + "X";
private final String FISH_OWNER = ChatColor.GRAY + "Caught by: " + ChatColor.DARK_GRAY + "X";
private final String FISH_TIME = ChatColor.GRAY + "Caught on: " + ChatColor.DARK_GRAY + "X";
public final String[] validItems = {
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
};
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public PlayerFish(ToolStats toolStats) {
this.toolStats = toolStats;
@@ -53,10 +64,19 @@ public class PlayerFish implements Listener {
if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.FISHING_ROD) {
return;
}
addLore(heldItem);
updateFishCount(heldItem);
if (event.getCaught() == null) {
return;
}
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
for (String x : validItems) {
if (caughtItem.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
addNewLore(caughtItem, player);
}
}
}
private void addLore(ItemStack itemStack) {
private void updateFishCount(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
@@ -100,4 +120,29 @@ public class PlayerFish implements Listener {
}
itemStack.setItemMeta(meta);
}
private void addNewLore(ItemStack itemStack, Player owner) {
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
}
long timeCreated = System.currentTimeMillis();
Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
assert lore != null;
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(itemStack, "fished-tag")) {
lore.add(FISH_TIME.replace("X", format.format(finalDate)));
lore.add(FISH_OWNER.replace("X", owner.getName()));
}
meta.setLore(lore);
itemStack.setItemMeta(meta);
}
}

View File

@@ -0,0 +1,101 @@
/*
* 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 lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.UUIDDataType;
import org.bukkit.ChatColor;
import org.bukkit.entity.Cat;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class VillagerTrade implements Listener {
private final ToolStats toolStats;
private final String TRADED_OWNER = ChatColor.GRAY + "Traded by: " + ChatColor.DARK_GRAY + "X";
private final String TRADED_TIME = ChatColor.GRAY + "Traded on: " + ChatColor.DARK_GRAY + "X";
public final String[] validItems = {
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
};
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public VillagerTrade(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler
public void onTrade(InventoryClickEvent event) {
if (event.isCancelled() || event.getCurrentItem() == null) {
return;
}
Inventory inventory = event.getClickedInventory();
if (inventory instanceof MerchantInventory) {
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
ItemStack item = event.getCurrentItem();
for (String x : validItems) {
if (item.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
ItemStack newItem = addLore(item, (Player) event.getWhoClicked());
event.getView().setCursor(newItem);
}
}
}
}
}
private ItemStack addLore(ItemStack itemStack, Player owner) {
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return null;
}
long timeCreated = System.currentTimeMillis();
Date finalDate = new Date(timeCreated);
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.genericOwner, new UUIDDataType(), owner.getUniqueId());
List<String> lore;
if (meta.hasLore()) {
lore = meta.getLore();
assert lore != null;
} else {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(itemStack, "traded-tag")) {
lore.add(TRADED_TIME.replace("X", format.format(finalDate)));
lore.add(TRADED_OWNER.replace("X", owner.getName()));
}
meta.setLore(lore);
itemStack.setItemMeta(meta);
return itemStack;
}
}

View File

@@ -1,5 +1,6 @@
enabled:
crafted-by:
# Will show ownership of items when they are created/found.
created-by:
pickaxe: true
sword: true
shovel: true
@@ -8,7 +9,38 @@ enabled:
shears: true
bow: true
armor: true
crafted-date:
# Will show time the item is created
created-date:
pickaxe: true
sword: true
shovel: true
axe: true
hoe: true
shears: true
bow: true
armor: true
# Will show "Fished by <player>"
fished-tag:
pickaxe: true
sword: true
shovel: true
axe: true
hoe: true
shears: true
bow: true
armor: true
# Will show "Found by <player>"
looted-tag:
pickaxe: true
sword: true
shovel: true
axe: true
hoe: true
shears: true
bow: true
armor: true
# Will show "Trade by <player>"
traded-tag:
pickaxe: true
sword: true
shovel: true
@@ -36,4 +68,4 @@ enabled:
armor-damage: true
dropped-by: true
config-version: 1
config-version: 2