Skip to content

Commit a55cea4

Browse files
committed
more work
1 parent d0fcd4f commit a55cea4

File tree

4 files changed

+155
-77
lines changed

4 files changed

+155
-77
lines changed

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public final class ToolStats extends JavaPlugin {
105105
public CommandToolStats commandToolStats;
106106
public ItemLore itemLore;
107107
public InventoryOpen inventoryOpen;
108+
public PlayerJoin playerJoin;
108109
public NumberFormat numberFormat;
109110

110111
public final Logger logger = this.getLogger();
@@ -139,6 +140,7 @@ public void onEnable() {
139140
commandToolStats = new CommandToolStats(this);
140141
itemLore = new ItemLore(this);
141142
inventoryOpen = new InventoryOpen(this);
143+
playerJoin = new PlayerJoin(this);
142144

143145
Bukkit.getServer().getPluginManager().registerEvents(blocksMined, this);
144146
Bukkit.getServer().getPluginManager().registerEvents(chunkPopulate, this);
@@ -152,6 +154,7 @@ public void onEnable() {
152154
Bukkit.getServer().getPluginManager().registerEvents(sheepShear, this);
153155
Bukkit.getServer().getPluginManager().registerEvents(villagerTrade, this);
154156
Bukkit.getServer().getPluginManager().registerEvents(inventoryOpen, this);
157+
Bukkit.getServer().getPluginManager().registerEvents(playerJoin, this);
155158

156159
this.getCommand("toolstats").setExecutor(commandToolStats);
157160

@@ -310,25 +313,14 @@ public BukkitAudiences getAdventure() {
310313
}
311314

312315
public void scheduleEntity(BukkitRunnable runnable, Entity entity, int delay) {
313-
if (Bukkit.getServer().getVersion().contains("Folia")) {
314-
morePaperLib.scheduling().entitySpecificScheduler(entity).runDelayed(runnable, null, delay);
315-
} else {
316-
runnable.runTaskLater(this, delay);
317-
}
316+
morePaperLib.scheduling().entitySpecificScheduler(entity).runDelayed(runnable, null, delay);
318317
}
319318

320319
public void scheduleGlobal(BukkitRunnable runnable, int delay) {
321-
if (Bukkit.getServer().getVersion().contains("Folia")) {
322-
morePaperLib.scheduling().globalRegionalScheduler().runDelayed(runnable, delay);
323-
} else {
324-
runnable.runTaskLater(this, delay);
325-
}
320+
morePaperLib.scheduling().globalRegionalScheduler().runDelayed(runnable, delay);
326321
}
322+
327323
public void scheduleRegion(BukkitRunnable runnable, World world, Chunk chunk, int delay) {
328-
if (Bukkit.getServer().getVersion().contains("Folia")) {
329-
morePaperLib.scheduling().regionSpecificScheduler(world, chunk.getX(), chunk.getZ()).runDelayed(runnable, delay);
330-
} else {
331-
runnable.runTaskLater(this, delay);
332-
}
324+
morePaperLib.scheduling().regionSpecificScheduler(world, chunk.getX(), chunk.getZ()).runDelayed(runnable, delay);
333325
}
334326
}

src/main/java/lol/hyper/toolstats/events/InventoryOpen.java

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.ItemChecker;
22+
import org.bukkit.Location;
2223
import org.bukkit.Material;
2324
import org.bukkit.entity.Player;
2425
import org.bukkit.event.EventHandler;
@@ -47,8 +48,6 @@ public void onOpen(InventoryOpenEvent event) {
4748
return;
4849
}
4950

50-
Player player = (Player) event.getPlayer();
51-
5251
Inventory inventory = event.getInventory();
5352
for (ItemStack itemStack : inventory) {
5453
if (itemStack == null) {
@@ -68,7 +67,7 @@ public void onOpen(InventoryOpenEvent event) {
6867
continue;
6968
}
7069

71-
ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
70+
ItemMeta newMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
7271
if (newMeta == null) {
7372
continue;
7473
}
@@ -78,66 +77,11 @@ public void run() {
7877
itemStack.setItemMeta(newMeta);
7978
}
8079
};
81-
toolStats.scheduleEntity(runnable, player, 1);
82-
}
83-
}
84-
85-
/**
86-
* Determine an item's origin based on lore.
87-
*
88-
* @param itemMeta The item's meta.
89-
* @param elytra If they item is an elytra.
90-
* @return The new item meta with the new origin tag. Returns null if origin cannot be determined.
91-
*/
92-
private ItemMeta getOrigin(ItemMeta itemMeta, boolean elytra) {
93-
List<String> lore;
94-
if (!itemMeta.hasLore()) {
95-
return null;
96-
}
97-
lore = itemMeta.getLore();
98-
Integer origin = null;
99-
100-
for (String line : lore) {
101-
// this is the worst code I have ever written
102-
String createdBy = toolStats.getLoreFromConfig("created.created-by", false);
103-
String createdOn = toolStats.getLoreFromConfig("created.created-on", false);
104-
String caughtBy = toolStats.getLoreFromConfig("fished.caught-by", false);
105-
String lootedBy = toolStats.getLoreFromConfig("looted.looted-by", false);
106-
String foundBy = toolStats.getLoreFromConfig("looted.found-by", false);
107-
String tradedBy = toolStats.getLoreFromConfig("traded.traded-by", false);
108-
109-
if (createdBy != null && line.contains(createdBy)) {
110-
origin = 0;
111-
}
112-
if (createdOn != null && line.contains(createdOn)) {
113-
origin = 0;
80+
Location location = inventory.getLocation();
81+
// only run for actual inventories
82+
if (location != null) {
83+
toolStats.scheduleRegion(runnable, location.getWorld(), location.getChunk(), 1);
11484
}
115-
if (caughtBy != null && line.contains(caughtBy)) {
116-
origin = 5;
117-
}
118-
if (lootedBy != null && line.contains(lootedBy)) {
119-
origin = 2;
120-
}
121-
// because the config changed, "found-by" was being used for ALL looted items
122-
// this includes elytras, so we have to check for this mistake
123-
if (foundBy != null && line.contains(foundBy)) {
124-
if (elytra) {
125-
origin = 4;
126-
} else {
127-
origin = 5;
128-
}
129-
}
130-
if (tradedBy != null && line.contains(tradedBy)) {
131-
origin = 3;
132-
}
133-
}
134-
135-
if (origin == null) {
136-
return null;
13785
}
138-
139-
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
140-
container.set(toolStats.originType, PersistentDataType.INTEGER, origin);
141-
return itemMeta;
14286
}
14387
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.Location;
23+
import org.bukkit.Material;
24+
import org.bukkit.entity.Player;
25+
import org.bukkit.event.EventHandler;
26+
import org.bukkit.event.Listener;
27+
import org.bukkit.event.player.PlayerJoinEvent;
28+
import org.bukkit.inventory.Inventory;
29+
import org.bukkit.inventory.ItemStack;
30+
import org.bukkit.inventory.meta.ItemMeta;
31+
import org.bukkit.persistence.PersistentDataContainer;
32+
import org.bukkit.persistence.PersistentDataType;
33+
import org.bukkit.scheduler.BukkitRunnable;
34+
35+
public class PlayerJoin implements Listener {
36+
37+
private final ToolStats toolStats;
38+
39+
public PlayerJoin(ToolStats toolStats) {
40+
this.toolStats = toolStats;
41+
}
42+
43+
@EventHandler
44+
public void onJoin(PlayerJoinEvent event) {
45+
Player player = event.getPlayer();
46+
47+
Inventory inventory = player.getInventory();
48+
for (ItemStack itemStack : inventory) {
49+
if (itemStack == null) {
50+
continue;
51+
}
52+
ItemMeta itemMeta = itemStack.getItemMeta();
53+
if (itemMeta == null) {
54+
continue;
55+
}
56+
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
57+
// ignore any items that already have the origin tag
58+
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
59+
continue;
60+
}
61+
// ignore items that are not the right type
62+
if (!ItemChecker.isValidItem(itemStack.getType())) {
63+
continue;
64+
}
65+
66+
ItemMeta newMeta = toolStats.itemLore.getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
67+
if (newMeta == null) {
68+
continue;
69+
}
70+
BukkitRunnable runnable = new BukkitRunnable() {
71+
@Override
72+
public void run() {
73+
itemStack.setItemMeta(newMeta);
74+
}
75+
};
76+
Location location = inventory.getLocation();
77+
// only run for actual inventories
78+
if (location != null) {
79+
toolStats.scheduleRegion(runnable, location.getWorld(), location.getChunk(), 1);
80+
}
81+
}
82+
}
83+
}

src/main/java/lol/hyper/toolstats/tools/ItemLore.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,63 @@ public List<String> addNewOwner(ItemMeta itemMeta, String playerName, String for
138138
newLore.add(itemOwner.replace("{player}", playerName));
139139
return newLore;
140140
}
141+
142+
/**
143+
* Determine an item's origin based on lore.
144+
*
145+
* @param itemMeta The item's meta.
146+
* @param elytra If they item is an elytra.
147+
* @return The new item meta with the new origin tag. Returns null if origin cannot be determined.
148+
*/
149+
public ItemMeta getOrigin(ItemMeta itemMeta, boolean elytra) {
150+
List<String> lore;
151+
if (!itemMeta.hasLore()) {
152+
return null;
153+
}
154+
lore = itemMeta.getLore();
155+
Integer origin = null;
156+
157+
for (String line : lore) {
158+
// this is the worst code I have ever written
159+
String createdBy = toolStats.getLoreFromConfig("created.created-by", false);
160+
String createdOn = toolStats.getLoreFromConfig("created.created-on", false);
161+
String caughtBy = toolStats.getLoreFromConfig("fished.caught-by", false);
162+
String lootedBy = toolStats.getLoreFromConfig("looted.looted-by", false);
163+
String foundBy = toolStats.getLoreFromConfig("looted.found-by", false);
164+
String tradedBy = toolStats.getLoreFromConfig("traded.traded-by", false);
165+
166+
if (createdBy != null && line.contains(createdBy)) {
167+
origin = 0;
168+
}
169+
if (createdOn != null && line.contains(createdOn)) {
170+
origin = 0;
171+
}
172+
if (caughtBy != null && line.contains(caughtBy)) {
173+
origin = 5;
174+
}
175+
if (lootedBy != null && line.contains(lootedBy)) {
176+
origin = 2;
177+
}
178+
// because the config changed, "found-by" was being used for ALL looted items
179+
// this includes elytras, so we have to check for this mistake
180+
if (foundBy != null && line.contains(foundBy)) {
181+
if (elytra) {
182+
origin = 4;
183+
} else {
184+
origin = 5;
185+
}
186+
}
187+
if (tradedBy != null && line.contains(tradedBy)) {
188+
origin = 3;
189+
}
190+
}
191+
192+
if (origin == null) {
193+
return null;
194+
}
195+
196+
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
197+
container.set(toolStats.originType, PersistentDataType.INTEGER, origin);
198+
return itemMeta;
199+
}
141200
}

0 commit comments

Comments
 (0)