Skip to content

Commit fe8052f

Browse files
committed
add origin tag to existing items
1 parent 884bdef commit fe8052f

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public final class ToolStats extends JavaPlugin {
103103
public VillagerTrade villagerTrade;
104104
public CommandToolStats commandToolStats;
105105
public ItemLore itemLore;
106+
public InventoryOpen inventoryOpen;
107+
public NumberFormat numberFormat;
106108

107109
public final Logger logger = this.getLogger();
108110
public final File configFile = new File(this.getDataFolder(), "config.yml");
@@ -111,7 +113,6 @@ public final class ToolStats extends JavaPlugin {
111113

112114
private BukkitAudiences adventure;
113115

114-
public NumberFormat numberFormat;
115116

116117
@Override
117118
public void onEnable() {
@@ -134,6 +135,7 @@ public void onEnable() {
134135
villagerTrade = new VillagerTrade(this);
135136
commandToolStats = new CommandToolStats(this);
136137
itemLore = new ItemLore(this);
138+
inventoryOpen = new InventoryOpen(this);
137139

138140
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
139141
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
@@ -146,6 +148,7 @@ public void onEnable() {
146148
Bukkit.getServer().getPluginManager().registerEvents(playerInteract, this);
147149
Bukkit.getServer().getPluginManager().registerEvents(sheepShear, this);
148150
Bukkit.getServer().getPluginManager().registerEvents(villagerTrade, this);
151+
Bukkit.getServer().getPluginManager().registerEvents(inventoryOpen, this);
149152

150153
this.getCommand("toolstats").setExecutor(commandToolStats);
151154

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* This file is part of ToolStats.
3+
*
4+
* ToolStats is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* ToolStats is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.toolstats.events;
19+
20+
import lol.hyper.toolstats.ToolStats;
21+
import lol.hyper.toolstats.tools.ItemChecker;
22+
import org.bukkit.Bukkit;
23+
import org.bukkit.Material;
24+
import org.bukkit.event.EventHandler;
25+
import org.bukkit.event.Listener;
26+
import org.bukkit.event.inventory.InventoryOpenEvent;
27+
import org.bukkit.inventory.Inventory;
28+
import org.bukkit.inventory.ItemStack;
29+
import org.bukkit.inventory.meta.ItemMeta;
30+
import org.bukkit.persistence.PersistentDataContainer;
31+
import org.bukkit.persistence.PersistentDataType;
32+
33+
import java.util.List;
34+
35+
public class InventoryOpen implements Listener {
36+
37+
private final ToolStats toolStats;
38+
39+
public InventoryOpen(ToolStats toolStats) {
40+
this.toolStats = toolStats;
41+
}
42+
43+
@EventHandler
44+
public void onOpen(InventoryOpenEvent event) {
45+
if (event.isCancelled()) {
46+
return;
47+
}
48+
49+
Bukkit.getScheduler().runTaskLater(toolStats, ()-> {
50+
Inventory inventory = event.getInventory();
51+
for (ItemStack itemStack : inventory) {
52+
if (itemStack == null) {
53+
continue;
54+
}
55+
ItemMeta itemMeta = itemStack.getItemMeta();
56+
if (itemMeta == null) {
57+
continue;
58+
}
59+
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
60+
// ignore any items that already have the origin tag
61+
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
62+
continue;
63+
}
64+
// ignore items that are not the right type
65+
if (!ItemChecker.isValidItem(itemStack.getType())) {
66+
continue;
67+
}
68+
69+
ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
70+
if (newMeta == null) {
71+
continue;
72+
}
73+
itemStack.setItemMeta(newMeta);
74+
}
75+
},1);
76+
}
77+
78+
/**
79+
* Determine an item's origin based on lore.
80+
*
81+
* @param itemMeta The item's meta.
82+
* @param elytra If they item is an elytra.
83+
* @return The new item meta with the new origin tag. Returns null if origin cannot be determined.
84+
*/
85+
private ItemMeta getOrigin(ItemMeta itemMeta, boolean elytra) {
86+
List<String> lore;
87+
if (!itemMeta.hasLore()) {
88+
return null;
89+
}
90+
lore = itemMeta.getLore();
91+
Integer origin = null;
92+
93+
for (String line : lore) {
94+
// this is the worst code I have ever written
95+
String createdBy = toolStats.getLoreFromConfig("created.created-by", false);
96+
String createdOn = toolStats.getLoreFromConfig("created.created-by", false);
97+
String caughtBy = toolStats.getLoreFromConfig("created.created-by", false);
98+
String lootedBy = toolStats.getLoreFromConfig("created.created-by", false);
99+
String foundBy = toolStats.getLoreFromConfig("created.created-by", false);
100+
String tradedBy = toolStats.getLoreFromConfig("created.created-by", false);
101+
102+
if (createdBy != null && line.contains(createdBy)) {
103+
origin = 0;
104+
}
105+
if (createdOn != null && line.contains(createdOn)) {
106+
origin = 0;
107+
}
108+
if (caughtBy != null && line.contains(caughtBy)) {
109+
origin = 5;
110+
}
111+
if (lootedBy != null && line.contains(lootedBy)) {
112+
origin = 2;
113+
}
114+
// because the config changed, "found-by" was being used for ALL looted items
115+
// this includes elytras, so we have to check for this mistake
116+
if (foundBy != null && line.contains(foundBy)) {
117+
if (elytra) {
118+
origin = 4;
119+
} else {
120+
origin = 5;
121+
}
122+
}
123+
if (tradedBy != null && line.contains(tradedBy)) {
124+
origin = 3;
125+
}
126+
}
127+
128+
if (origin == null) {
129+
return null;
130+
}
131+
132+
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
133+
container.set(toolStats.originType, PersistentDataType.INTEGER, origin);
134+
return itemMeta;
135+
}
136+
}

0 commit comments

Comments
 (0)