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.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Logger;
public final class ToolStats extends JavaPlugin {
@@ -55,6 +57,13 @@ public final class ToolStats extends JavaPlugin {
// used for tracking new elytras
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 ChunkPopulate chunkPopulate;
public CraftItem craftItem;
@@ -144,6 +153,7 @@ public final class ToolStats extends JavaPlugin {
/**
* Checks the config to see if we want to show lore on certain items.
*
* @param itemStack The item to check.
* @param configName The config we are checking under.
* @return If we want to allow lore or not.
@@ -200,6 +210,7 @@ public final class ToolStats extends JavaPlugin {
/**
* Gets the lore message from the config.
*
* @param configName The config name, "messages." is already in front.
* @param raw If you want the raw message with the formatting codes and placeholders.
* @return The lore message.

View File

@@ -36,7 +36,6 @@ import java.util.List;
public class BlocksMined implements Listener {
private final ToolStats toolStats;
private final String[] validTools = {"pickaxe", "axe", "hoe", "shovel", "shear"};
public BlocksMined(ToolStats toolStats) {
this.toolStats = toolStats;
@@ -59,7 +58,7 @@ public class BlocksMined implements Listener {
}
// only check certain items
String itemName = heldItem.getType().toString().toLowerCase();
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
if (Arrays.stream(toolStats.mineItems).noneMatch(itemName::contains)) {
return;
}
// 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.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.*;
public class CraftItem implements Listener {
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) {
this.toolStats = toolStats;
@@ -57,7 +52,7 @@ public class CraftItem implements Listener {
}
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
// only check for items we want
for (String x : validItems) {
for (String x : toolStats.allValidItems) {
if (name.contains(x)) {
// if the player shift clicks, send them this warning
if (event.isShiftClick()) {
@@ -80,6 +75,7 @@ public class CraftItem implements Listener {
/**
* Adds crafted tags to item.
*
* @param itemStack The item add item to.
* @param owner The player crafting.
* @return A copy of the item with the tags + lore.
@@ -127,7 +123,7 @@ public class CraftItem implements Listener {
}
// do we add the lore based on the config?
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")) {
lore.add(createdByRaw.replace("{player}", owner.getName()));

View File

@@ -39,7 +39,6 @@ import java.util.*;
public class EntityDamage implements Listener {
private final ToolStats toolStats;
private final String[] validTools = {"sword", "trident", "axe"};
private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
public final Set<UUID> trackedMobs = new HashSet<>();
@@ -71,7 +70,7 @@ public class EntityDamage implements Listener {
}
// check items we want
String itemName = heldItem.getType().toString().toLowerCase();
if (Arrays.stream(validTools).noneMatch(itemName::contains)) {
if (Arrays.stream(toolStats.meleeItems).noneMatch(itemName::contains)) {
return;
}
// a player is killing another player
@@ -176,6 +175,7 @@ public class EntityDamage implements Listener {
/**
* Updates a weapon's player kills.
*
* @param itemStack The item to update.
* @return A copy of the item.
*/
@@ -238,6 +238,7 @@ public class EntityDamage implements Listener {
/**
* Updates a weapon's mob kills.
*
* @param itemStack The item to update.
* @return A copy of the item.
*/
@@ -300,6 +301,7 @@ public class EntityDamage implements Listener {
/**
* Updates a player's armor damage stats.
*
* @param itemStack The armor piece.
* @param damage How much damage is being added.
*/
@@ -360,6 +362,7 @@ public class EntityDamage implements Listener {
/**
* Check if item is an armor piece.
*
* @param itemType The item type, not name.
* @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)) {
for (ItemStack current : event.getDrops()) {
String name = current.getType().toString().toLowerCase(Locale.ROOT);
for (String item : toolStats.craftItem.validItems) {
for (String item : toolStats.allValidItems) {
if (name.contains(item)) {
addLore(current, livingEntity.getName());
}
@@ -59,6 +59,7 @@ public class EntityDeath implements Listener {
/**
* Adds "drop by" tag to item.
*
* @param itemStack The item to add lore to.
* @param mob The mob or player name.
*/

View File

@@ -43,10 +43,6 @@ import java.util.Locale;
public class GenerateLoot implements Listener {
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) {
this.toolStats = toolStats;
@@ -88,7 +84,7 @@ public class GenerateLoot implements Listener {
continue;
}
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
for (String x : validItems) {
for (String x : toolStats.allValidItems) {
if (name.contains(x)) {
chestInv.setItem(i, addLore(itemStack, player));
}
@@ -100,6 +96,7 @@ public class GenerateLoot implements Listener {
/**
* Adds lore to newly generated items.
*
* @param itemStack The item to add lore to.
* @param owner The player that found the item.
* @return The item with the lore.
@@ -137,7 +134,7 @@ public class GenerateLoot implements Listener {
lore = new ArrayList<>();
}
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()));
}
meta.setLore(lore);

View File

@@ -30,16 +30,13 @@ 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 PickupItem implements Listener {
private final ToolStats toolStats;
private final SimpleDateFormat format = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
public PickupItem(ToolStats toolStats) {
this.toolStats = toolStats;
@@ -69,6 +66,7 @@ public class PickupItem implements Listener {
/**
* Adds "looted by" tags for elytras.
*
* @param itemStack The elytra to add lore to.
* @param owner The player who found it.
*/
@@ -100,7 +98,7 @@ public class PickupItem implements Listener {
lore = new ArrayList<>();
}
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()));
}
meta.setLore(lore);

View File

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

View File

@@ -66,6 +66,7 @@ public class SheepShear implements Listener {
/**
* Adds tags to shears.
*
* @param itemStack The shears.
*/
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.PersistentDataType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -42,10 +41,6 @@ import java.util.Locale;
public class VillagerTrade implements Listener {
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) {
this.toolStats = toolStats;
@@ -63,7 +58,7 @@ public class VillagerTrade implements Listener {
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
ItemStack item = event.getCurrentItem();
// only check items we want
for (String x : validItems) {
for (String x : toolStats.allValidItems) {
if (item.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
// if the player shift clicks show the warning
if (event.isShiftClick()) {
@@ -87,6 +82,7 @@ public class VillagerTrade implements Listener {
/**
* Adds "traded by" tags to item.
*
* @param itemStack The item to add lore.
* @param owner The player who traded.
* @return The item with lore.
@@ -123,7 +119,7 @@ public class VillagerTrade implements Listener {
lore = new ArrayList<>();
}
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()));
}
meta.setLore(lore);