mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2025-12-06 06:41:44 +00:00
add origin tag to existing items
This commit is contained in:
@@ -103,6 +103,8 @@ public final class ToolStats extends JavaPlugin {
|
||||
public VillagerTrade villagerTrade;
|
||||
public CommandToolStats commandToolStats;
|
||||
public ItemLore itemLore;
|
||||
public InventoryOpen inventoryOpen;
|
||||
public NumberFormat numberFormat;
|
||||
|
||||
public final Logger logger = this.getLogger();
|
||||
public final File configFile = new File(this.getDataFolder(), "config.yml");
|
||||
@@ -111,7 +113,6 @@ public final class ToolStats extends JavaPlugin {
|
||||
|
||||
private BukkitAudiences adventure;
|
||||
|
||||
public NumberFormat numberFormat;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -134,6 +135,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
villagerTrade = new VillagerTrade(this);
|
||||
commandToolStats = new CommandToolStats(this);
|
||||
itemLore = new ItemLore(this);
|
||||
inventoryOpen = new InventoryOpen(this);
|
||||
|
||||
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
|
||||
@@ -146,6 +148,7 @@ public final class ToolStats extends JavaPlugin {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(playerInteract, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(sheepShear, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(villagerTrade, this);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(inventoryOpen, this);
|
||||
|
||||
this.getCommand("toolstats").setExecutor(commandToolStats);
|
||||
|
||||
|
||||
136
src/main/java/lol/hyper/toolstats/events/InventoryOpen.java
Normal file
136
src/main/java/lol/hyper/toolstats/events/InventoryOpen.java
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.tools.ItemChecker;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class InventoryOpen implements Listener {
|
||||
|
||||
private final ToolStats toolStats;
|
||||
|
||||
public InventoryOpen(ToolStats toolStats) {
|
||||
this.toolStats = toolStats;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onOpen(InventoryOpenEvent event) {
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskLater(toolStats, ()-> {
|
||||
Inventory inventory = event.getInventory();
|
||||
for (ItemStack itemStack : inventory) {
|
||||
if (itemStack == null) {
|
||||
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;
|
||||
}
|
||||
|
||||
ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
|
||||
if (newMeta == null) {
|
||||
continue;
|
||||
}
|
||||
itemStack.setItemMeta(newMeta);
|
||||
}
|
||||
},1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine an item's origin based on lore.
|
||||
*
|
||||
* @param itemMeta The item's meta.
|
||||
* @param elytra If they item is an elytra.
|
||||
* @return The new item meta with the new origin tag. Returns null if origin cannot be determined.
|
||||
*/
|
||||
private ItemMeta getOrigin(ItemMeta itemMeta, boolean elytra) {
|
||||
List<String> lore;
|
||||
if (!itemMeta.hasLore()) {
|
||||
return null;
|
||||
}
|
||||
lore = itemMeta.getLore();
|
||||
Integer origin = null;
|
||||
|
||||
for (String line : lore) {
|
||||
// this is the worst code I have ever written
|
||||
String createdBy = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
String createdOn = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
String caughtBy = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
String lootedBy = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
String foundBy = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
String tradedBy = toolStats.getLoreFromConfig("created.created-by", false);
|
||||
|
||||
if (createdBy != null && line.contains(createdBy)) {
|
||||
origin = 0;
|
||||
}
|
||||
if (createdOn != null && line.contains(createdOn)) {
|
||||
origin = 0;
|
||||
}
|
||||
if (caughtBy != null && line.contains(caughtBy)) {
|
||||
origin = 5;
|
||||
}
|
||||
if (lootedBy != null && line.contains(lootedBy)) {
|
||||
origin = 2;
|
||||
}
|
||||
// because the config changed, "found-by" was being used for ALL looted items
|
||||
// this includes elytras, so we have to check for this mistake
|
||||
if (foundBy != null && line.contains(foundBy)) {
|
||||
if (elytra) {
|
||||
origin = 4;
|
||||
} else {
|
||||
origin = 5;
|
||||
}
|
||||
}
|
||||
if (tradedBy != null && line.contains(tradedBy)) {
|
||||
origin = 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (origin == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
|
||||
container.set(toolStats.originType, PersistentDataType.INTEGER, origin);
|
||||
return itemMeta;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user