unify item arrays & date formats

This commit is contained in:
hyperdefined
2022-03-01 23:32:30 -05:00
parent 61cf44a407
commit b9907eedec
10 changed files with 46 additions and 46 deletions

View File

@@ -32,6 +32,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Logger; import java.util.logging.Logger;
public final class ToolStats extends JavaPlugin { public final class ToolStats extends JavaPlugin {
@@ -55,6 +57,13 @@ public final class ToolStats extends JavaPlugin {
// used for tracking new elytras // used for tracking new elytras
public final NamespacedKey newElytra = new NamespacedKey(this, "new"); public final NamespacedKey newElytra = new NamespacedKey(this, "new");
public final String[] allValidItems = {
"pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing"
};
public final String[] meleeItems = {"sword", "trident", "axe"};
public final String[] mineItems = {"pickaxe", "axe", "hoe", "shovel", "shear"};
public final SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public BlocksMined blocksMined; public BlocksMined blocksMined;
public ChunkPopulate chunkPopulate; public ChunkPopulate chunkPopulate;
public CraftItem craftItem; public CraftItem craftItem;
@@ -144,7 +153,8 @@ public final class ToolStats extends JavaPlugin {
/** /**
* Checks the config to see if we want to show lore on certain items. * Checks the config to see if we want to show lore on certain items.
* @param itemStack The item to check. *
* @param itemStack The item 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 allow lore or not.
*/ */
@@ -200,8 +210,9 @@ public final class ToolStats extends JavaPlugin {
/** /**
* Gets the lore message from the config. * Gets the lore message from the config.
*
* @param configName The config name, "messages." is already in front. * @param configName The config name, "messages." is already in front.
* @param raw If you want the raw message with the formatting codes and placeholders. * @param raw If you want the raw message with the formatting codes and placeholders.
* @return The lore message. * @return The lore message.
*/ */
public String getLoreFromConfig(String configName, boolean raw) { public String getLoreFromConfig(String configName, boolean raw) {

View File

@@ -36,7 +36,6 @@ import java.util.List;
public class BlocksMined implements Listener { public class BlocksMined implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
private final String[] validTools = {"pickaxe", "axe", "hoe", "shovel", "shear"};
public BlocksMined(ToolStats toolStats) { public BlocksMined(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -59,7 +58,7 @@ public class BlocksMined implements Listener {
} }
// only check certain items // only check certain items
String itemName = heldItem.getType().toString().toLowerCase(); String itemName = heldItem.getType().toString().toLowerCase();
if (Arrays.stream(validTools).noneMatch(itemName::contains)) { if (Arrays.stream(toolStats.mineItems).noneMatch(itemName::contains)) {
return; return;
} }
// if it's an item we want, update the stats // if it's an item we want, update the stats

View File

@@ -30,16 +30,11 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
public class CraftItem implements Listener { public class CraftItem implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
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 CraftItem(ToolStats toolStats) { public CraftItem(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -57,7 +52,7 @@ public class CraftItem implements Listener {
} }
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT); String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
// only check for items we want // only check for items we want
for (String x : validItems) { for (String x : toolStats.allValidItems) {
if (name.contains(x)) { if (name.contains(x)) {
// if the player shift clicks, send them this warning // if the player shift clicks, send them this warning
if (event.isShiftClick()) { if (event.isShiftClick()) {
@@ -80,8 +75,9 @@ public class CraftItem implements Listener {
/** /**
* Adds crafted tags to item. * Adds crafted tags to item.
*
* @param itemStack The item add item to. * @param itemStack The item add item to.
* @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 addLore(ItemStack itemStack, Player owner) {
@@ -127,7 +123,7 @@ public class CraftItem implements Listener {
} }
// do we add the lore based on the config? // do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "created-date")) { if (toolStats.checkConfig(itemStack, "created-date")) {
lore.add(createdOnRaw.replace("{date}", format.format(finalDate))); lore.add(createdOnRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
} }
if (toolStats.checkConfig(itemStack, "created-by")) { if (toolStats.checkConfig(itemStack, "created-by")) {
lore.add(createdByRaw.replace("{player}", owner.getName())); lore.add(createdByRaw.replace("{player}", owner.getName()));

View File

@@ -39,7 +39,6 @@ import java.util.*;
public class EntityDamage implements Listener { public class EntityDamage implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
private final String[] validTools = {"sword", "trident", "axe"};
private final DecimalFormat decimalFormat = new DecimalFormat("0.00"); private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
public final Set<UUID> trackedMobs = new HashSet<>(); public final Set<UUID> trackedMobs = new HashSet<>();
@@ -71,7 +70,7 @@ public class EntityDamage implements Listener {
} }
// check items we want // check items we want
String itemName = heldItem.getType().toString().toLowerCase(); String itemName = heldItem.getType().toString().toLowerCase();
if (Arrays.stream(validTools).noneMatch(itemName::contains)) { if (Arrays.stream(toolStats.meleeItems).noneMatch(itemName::contains)) {
return; return;
} }
// a player is killing another player // a player is killing another player
@@ -176,6 +175,7 @@ public class EntityDamage implements Listener {
/** /**
* Updates a weapon's player kills. * Updates a weapon's player kills.
*
* @param itemStack The item to update. * @param itemStack The item to update.
* @return A copy of the item. * @return A copy of the item.
*/ */
@@ -238,6 +238,7 @@ public class EntityDamage implements Listener {
/** /**
* Updates a weapon's mob kills. * Updates a weapon's mob kills.
*
* @param itemStack The item to update. * @param itemStack The item to update.
* @return A copy of the item. * @return A copy of the item.
*/ */
@@ -300,8 +301,9 @@ public class EntityDamage implements Listener {
/** /**
* Updates a player's armor damage stats. * Updates a player's armor damage stats.
*
* @param itemStack The armor piece. * @param itemStack The armor piece.
* @param damage How much damage is being added. * @param damage How much damage is being added.
*/ */
private void updateArmorDamage(ItemStack itemStack, double damage) { private void updateArmorDamage(ItemStack itemStack, double damage) {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
@@ -360,6 +362,7 @@ public class EntityDamage implements Listener {
/** /**
* Check if item is an armor piece. * Check if item is an armor piece.
*
* @param itemType The item type, not name. * @param itemType The item type, not name.
* @return If the item is an armor piece. * @return If the item is an armor piece.
*/ */

View File

@@ -47,7 +47,7 @@ public class EntityDeath implements Listener {
if (toolStats.mobKill.trackedMobs.contains(livingEntityUUID)) { if (toolStats.mobKill.trackedMobs.contains(livingEntityUUID)) {
for (ItemStack current : event.getDrops()) { for (ItemStack current : event.getDrops()) {
String name = current.getType().toString().toLowerCase(Locale.ROOT); String name = current.getType().toString().toLowerCase(Locale.ROOT);
for (String item : toolStats.craftItem.validItems) { for (String item : toolStats.allValidItems) {
if (name.contains(item)) { if (name.contains(item)) {
addLore(current, livingEntity.getName()); addLore(current, livingEntity.getName());
} }
@@ -59,8 +59,9 @@ public class EntityDeath implements Listener {
/** /**
* Adds "drop by" tag to item. * Adds "drop by" tag to item.
*
* @param itemStack The item to add lore to. * @param itemStack The item to add lore to.
* @param mob The mob or player name. * @param mob The mob or player name.
*/ */
private void addLore(ItemStack itemStack, String mob) { private void addLore(ItemStack itemStack, String mob) {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();

View File

@@ -43,10 +43,6 @@ import java.util.Locale;
public class GenerateLoot implements Listener { public class GenerateLoot implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
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) { public GenerateLoot(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -88,20 +84,21 @@ public class GenerateLoot implements Listener {
continue; continue;
} }
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT); String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
for (String x : validItems) { for (String x : toolStats.allValidItems) {
if (name.contains(x)) { if (name.contains(x)) {
chestInv.setItem(i, addLore(itemStack, player)); chestInv.setItem(i, addLore(itemStack, player));
} }
} }
} }
},1); }, 1);
} }
/** /**
* Adds lore to newly generated items. * Adds lore to newly generated items.
*
* @param itemStack The item to add lore to. * @param itemStack The item to add lore to.
* @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 addLore(ItemStack itemStack, Player owner) {
@@ -137,7 +134,7 @@ public class GenerateLoot implements Listener {
lore = new ArrayList<>(); lore = new ArrayList<>();
} }
if (toolStats.checkConfig(newItem, "looted-tag")) { if (toolStats.checkConfig(newItem, "looted-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", format.format(finalDate))); lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName())); lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
} }
meta.setLore(lore); meta.setLore(lore);

View File

@@ -30,16 +30,13 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
public class PickupItem implements Listener { public class PickupItem implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public PickupItem(ToolStats toolStats) { public PickupItem(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -69,8 +66,9 @@ public class PickupItem implements Listener {
/** /**
* Adds "looted by" tags for elytras. * Adds "looted by" tags for elytras.
*
* @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 void addLore(ItemStack itemStack, Player owner) { private void addLore(ItemStack itemStack, Player owner) {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
@@ -100,7 +98,7 @@ public class PickupItem implements Listener {
lore = new ArrayList<>(); lore = new ArrayList<>();
} }
if (toolStats.config.getBoolean("enabled.elytra-tag")) { if (toolStats.config.getBoolean("enabled.elytra-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", format.format(finalDate))); lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName())); lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
} }
meta.setLore(lore); meta.setLore(lore);

View File

@@ -39,10 +39,6 @@ import java.util.Locale;
public class PlayerFish implements Listener { public class PlayerFish implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
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) { public PlayerFish(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -68,7 +64,7 @@ public class PlayerFish implements Listener {
return; return;
} }
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack(); ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
for (String x : validItems) { for (String x : toolStats.allValidItems) {
if (caughtItem.getType().toString().toLowerCase(Locale.ROOT).contains(x)) { if (caughtItem.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
addNewLore(caughtItem, player); addNewLore(caughtItem, player);
} }
@@ -77,6 +73,7 @@ public class PlayerFish implements Listener {
/** /**
* Updates a fishing rod's count. * Updates a fishing rod's count.
*
* @param itemStack The fishing rod to update. * @param itemStack The fishing rod to update.
*/ */
private void updateFishCount(ItemStack itemStack) { private void updateFishCount(ItemStack itemStack) {
@@ -135,8 +132,9 @@ public class PlayerFish implements Listener {
/** /**
* Adds "caught by" tags to newly fished items. * Adds "caught by" tags to newly fished items.
*
* @param itemStack The item to add lore to. * @param itemStack The item to add lore to.
* @param owner The player who caught the item. * @param owner The player who caught the item.
*/ */
private void addNewLore(ItemStack itemStack, Player owner) { private void addNewLore(ItemStack itemStack, Player owner) {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
@@ -170,7 +168,7 @@ public class PlayerFish implements Listener {
lore = new ArrayList<>(); lore = new ArrayList<>();
} }
if (toolStats.checkConfig(itemStack, "fished-tag")) { if (toolStats.checkConfig(itemStack, "fished-tag")) {
lore.add(caughtOnLoreRaw.replace("{date}", format.format(finalDate))); lore.add(caughtOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(caughtByLoreRaw.replace("{player}", owner.getName())); lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
} }
meta.setLore(lore); meta.setLore(lore);

View File

@@ -66,6 +66,7 @@ public class SheepShear implements Listener {
/** /**
* Adds tags to shears. * Adds tags to shears.
*
* @param itemStack The shears. * @param itemStack The shears.
*/ */
private void addLore(ItemStack itemStack) { private void addLore(ItemStack itemStack) {

View File

@@ -33,7 +33,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@@ -42,10 +41,6 @@ import java.util.Locale;
public class VillagerTrade implements Listener { public class VillagerTrade implements Listener {
private final ToolStats toolStats; private final ToolStats toolStats;
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) { public VillagerTrade(ToolStats toolStats) {
this.toolStats = toolStats; this.toolStats = toolStats;
@@ -63,7 +58,7 @@ public class VillagerTrade implements Listener {
if (event.getSlotType() == InventoryType.SlotType.RESULT) { if (event.getSlotType() == InventoryType.SlotType.RESULT) {
ItemStack item = event.getCurrentItem(); ItemStack item = event.getCurrentItem();
// only check items we want // only check items we want
for (String x : validItems) { for (String x : toolStats.allValidItems) {
if (item.getType().toString().toLowerCase(Locale.ROOT).contains(x)) { if (item.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
// if the player shift clicks show the warning // if the player shift clicks show the warning
if (event.isShiftClick()) { if (event.isShiftClick()) {
@@ -78,7 +73,7 @@ public class VillagerTrade implements Listener {
} }
// this gets delayed since villager inventories suck for no reason // this gets delayed since villager inventories suck for no reason
// if you don't delay this it doesn't work idk // if you don't delay this it doesn't work idk
Bukkit.getScheduler().runTaskLater(toolStats, ()-> event.setCurrentItem(newItem), 5); Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
} }
} }
} }
@@ -87,8 +82,9 @@ public class VillagerTrade implements Listener {
/** /**
* Adds "traded by" tags to item. * Adds "traded by" tags to item.
*
* @param itemStack The item to add lore. * @param itemStack The item to add lore.
* @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 itemStack, Player owner) { private ItemStack addLore(ItemStack itemStack, Player owner) {
@@ -123,7 +119,7 @@ public class VillagerTrade implements Listener {
lore = new ArrayList<>(); lore = new ArrayList<>();
} }
if (toolStats.checkConfig(itemStack, "traded-tag")) { if (toolStats.checkConfig(itemStack, "traded-tag")) {
lore.add(tradedOnLoreRaw.replace("{date}", format.format(finalDate))); lore.add(tradedOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(tradedByLoreRaw.replace("{player}", owner.getName())); lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
} }
meta.setLore(lore); meta.setLore(lore);