mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2025-12-06 06:41:44 +00:00
much better item checking
This commit is contained in:
@@ -58,11 +58,6 @@ 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;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -30,7 +31,6 @@ import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BlocksMined implements Listener {
|
||||
@@ -56,8 +56,7 @@ public class BlocksMined implements Listener {
|
||||
return;
|
||||
}
|
||||
// only check certain items
|
||||
String itemName = heldItem.getType().toString().toLowerCase();
|
||||
if (Arrays.stream(toolStats.mineItems).noneMatch(itemName::contains)) {
|
||||
if (!ItemChecker.isMineTool(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// if it's an item we want, update the stats
|
||||
|
||||
@@ -18,6 +18,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.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
@@ -54,27 +55,26 @@ public class CraftItem implements Listener {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||
// only check for items we want
|
||||
for (String x : toolStats.allValidItems) {
|
||||
if (name.contains(x)) {
|
||||
// if the player shift clicks, send them this warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.crafting");
|
||||
if (configMessage != null) {
|
||||
if (configMessage.length() != 0) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
// test the item before setting it
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
// set the result
|
||||
event.setCurrentItem(newItem);
|
||||
// only check certain items
|
||||
if (!ItemChecker.isValidItem(itemStack.getType())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if the player shift clicks, send them this warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.crafting");
|
||||
if (configMessage != null) {
|
||||
if (configMessage.length() != 0) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
// test the item before setting it
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
// set the result
|
||||
event.setCurrentItem(newItem);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.*;
|
||||
@@ -68,9 +69,8 @@ public class EntityDamage implements Listener {
|
||||
if (heldItem == null || heldItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
// check items we want
|
||||
String itemName = heldItem.getType().toString().toLowerCase();
|
||||
if (Arrays.stream(toolStats.meleeItems).noneMatch(itemName::contains)) {
|
||||
// only check certain items
|
||||
if (!ItemChecker.isMeleeWeapon(heldItem.getType())) {
|
||||
return;
|
||||
}
|
||||
// a player is killing another player
|
||||
@@ -131,7 +131,7 @@ public class EntityDamage implements Listener {
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (isArmor(armor.getType().toString().toLowerCase(Locale.ROOT))) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
@@ -154,7 +154,7 @@ public class EntityDamage implements Listener {
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (isArmor(armor.getType().toString().toLowerCase(Locale.ROOT))) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
@@ -177,7 +177,7 @@ public class EntityDamage implements Listener {
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
for (ItemStack armor : inventory.getArmorContents()) {
|
||||
if (armor != null) {
|
||||
if (isArmor(armor.getType().toString().toLowerCase(Locale.ROOT))) {
|
||||
if (ItemChecker.isArmor(armor.getType())) {
|
||||
updateArmorDamage(armor, event.getFinalDamage());
|
||||
}
|
||||
}
|
||||
@@ -371,14 +371,4 @@ public class EntityDamage implements Listener {
|
||||
}
|
||||
itemStack.setItemMeta(meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is an armor piece.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is an armor piece.
|
||||
*/
|
||||
private boolean isArmor(String itemType) {
|
||||
return itemType.endsWith("_helmet") || itemType.endsWith("_chestplate") || itemType.endsWith("_leggings") || itemType.endsWith("_boots");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -46,11 +47,8 @@ public class EntityDeath implements Listener {
|
||||
// if it's a mob we are tracking that matters
|
||||
if (toolStats.mobKill.trackedMobs.contains(livingEntityUUID)) {
|
||||
for (ItemStack current : event.getDrops()) {
|
||||
String name = current.getType().toString().toLowerCase(Locale.ROOT);
|
||||
for (String item : toolStats.allValidItems) {
|
||||
if (name.contains(item)) {
|
||||
addLore(current, livingEntity.getName());
|
||||
}
|
||||
if (ItemChecker.isValidItem(current.getType())) {
|
||||
addLore(current, livingEntity.getName());
|
||||
}
|
||||
}
|
||||
toolStats.mobKill.trackedMobs.remove(livingEntityUUID);
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@@ -39,7 +40,6 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class GenerateLoot implements Listener {
|
||||
|
||||
@@ -86,13 +86,10 @@ public class GenerateLoot implements Listener {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||
for (String x : toolStats.allValidItems) {
|
||||
if (name.contains(x)) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
if (ItemChecker.isValidItem(itemStack.getType())) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,13 +107,10 @@ public class GenerateLoot implements Listener {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||
for (String x : toolStats.allValidItems) {
|
||||
if (name.contains(x)) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
if (ItemChecker.isValidItem(itemStack.getType())) {
|
||||
ItemStack newItem = addLore(itemStack, player);
|
||||
if (newItem != null) {
|
||||
chestInv.setItem(i, newItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Item;
|
||||
@@ -34,7 +35,6 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PlayerFish implements Listener {
|
||||
|
||||
@@ -67,10 +67,8 @@ public class PlayerFish implements Listener {
|
||||
return;
|
||||
}
|
||||
ItemStack caughtItem = ((Item) event.getCaught()).getItemStack();
|
||||
for (String x : toolStats.allValidItems) {
|
||||
if (caughtItem.getType().toString().toLowerCase(Locale.ROOT).contains(x)) {
|
||||
addNewLore(caughtItem, player);
|
||||
}
|
||||
if (ItemChecker.isValidItem(caughtItem.getType())) {
|
||||
addNewLore(caughtItem, player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
package lol.hyper.toolstats.events;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
import lol.hyper.toolstats.UUIDDataType;
|
||||
import lol.hyper.toolstats.tools.ItemChecker;
|
||||
import lol.hyper.toolstats.tools.UUIDDataType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
@@ -37,7 +38,6 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class VillagerTrade implements Listener {
|
||||
|
||||
@@ -66,22 +66,20 @@ public class VillagerTrade implements Listener {
|
||||
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
|
||||
ItemStack item = event.getCurrentItem();
|
||||
// only check items we want
|
||||
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()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.trading");
|
||||
if (configMessage != null) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
ItemStack newItem = addLore(item, player);
|
||||
if (newItem != null) {
|
||||
// this gets delayed since villager inventories suck for no reason
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
|
||||
return;
|
||||
if (ItemChecker.isValidItem(item.getType())) {
|
||||
// if the player shift clicks, show the warning
|
||||
if (event.isShiftClick()) {
|
||||
String configMessage = toolStats.config.getString("messages.shift-click-warning.trading");
|
||||
if (configMessage != null) {
|
||||
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&', configMessage));
|
||||
}
|
||||
}
|
||||
ItemStack newItem = addLore(item, player);
|
||||
if (newItem != null) {
|
||||
// this gets delayed since villager inventories suck for no reason
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> event.setCurrentItem(newItem), 5);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
71
src/main/java/lol/hyper/toolstats/tools/ItemChecker.java
Normal file
71
src/main/java/lol/hyper/toolstats/tools/ItemChecker.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.tools;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
public class ItemChecker {
|
||||
|
||||
private static final String[] validItems = { "pickaxe", "sword", "shovel", "axe", "hoe", "bow", "helmet", "chestplate", "leggings", "boots", "fishing" };
|
||||
private static final String[] validArmor = { "helmet", "chestplate", "leggings", "boots" };
|
||||
private static final String[] validMelee = {"sword", "trident", "axe"};
|
||||
private static final String[] validMine = { "pickaxe", "axe", "hoe", "shovel", "shear" };
|
||||
|
||||
/**
|
||||
* Check if item is an armor piece.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is an armor piece.
|
||||
*/
|
||||
public static boolean isArmor(Material itemType) {
|
||||
return Arrays.stream(validArmor).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a tool or armor piece we want to track.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item something we want to track.
|
||||
*/
|
||||
public static boolean isValidItem(Material itemType) {
|
||||
return Arrays.stream(validItems).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a melee weapon.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is a melee weapon.
|
||||
*/
|
||||
public static boolean isMeleeWeapon(Material itemType) {
|
||||
return Arrays.stream(validMelee).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if item is a mining tool.
|
||||
*
|
||||
* @param itemType The item type, not name.
|
||||
* @return If the item is a mining tool.
|
||||
*/
|
||||
public static boolean isMineTool(Material itemType) {
|
||||
return Arrays.stream(validMine).anyMatch(type -> itemType.toString().toLowerCase(Locale.ROOT).contains(type));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user