Skip to content

Commit 8a1759b

Browse files
committed
remove/add hashes when enabled/disabled
1 parent b4be9eb commit 8a1759b

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public final class ToolStats extends JavaPlugin {
112112
*/
113113
public final NamespacedKey originType = new NamespacedKey(this, "origin");
114114

115-
public final int CONFIG_VERSION = 9;
115+
public final int CONFIG_VERSION = 10;
116116
public final Logger logger = this.getLogger();
117117
public final File configFile = new File(this.getDataFolder(), "config.yml");
118118
public boolean tokens = false;

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

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

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.UUIDDataType;
22-
import org.bukkit.Bukkit;
23-
import org.bukkit.Location;
22+
import org.bukkit.entity.Player;
2423
import org.bukkit.event.EventHandler;
2524
import org.bukkit.event.Listener;
2625
import org.bukkit.event.inventory.InventoryOpenEvent;
@@ -47,7 +46,7 @@ public void onOpen(InventoryOpenEvent event) {
4746
}
4847

4948
Inventory inventory = event.getInventory();
50-
Location location = event.getInventory().getLocation();
49+
Player player = (Player) event.getPlayer();
5150
for (ItemStack itemStack : inventory) {
5251
if (itemStack == null) {
5352
continue;
@@ -75,31 +74,35 @@ public void onOpen(InventoryOpenEvent event) {
7574
}
7675
}
7776

78-
// generate a hash if the item doesn't have one (if it's enabled in the config)
77+
// generate a hash if the item doesn't have one (and enabled)
78+
// if hashes are disabled and the item has one, remove it.
7979
if (toolStats.config.getBoolean("generate-hash-for-items")) {
8080
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
81-
// make sure the item has an owner
82-
if (!container.has(toolStats.itemOwner, new UUIDDataType())) {
83-
continue;
81+
UUID owner = null;
82+
// get the current owner if there is one.
83+
if (container.has(toolStats.itemOwner, new UUIDDataType())) {
84+
owner = container.get(toolStats.itemOwner, new UUIDDataType());
8485
}
85-
UUID owner = container.get(toolStats.itemOwner, new UUIDDataType());
86+
// if there is no owner, use the player holding it
8687
if (owner == null) {
87-
continue;
88+
owner = player.getUniqueId();
8889
}
8990
Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG);
9091
if (timestamp == null) {
91-
continue;
92+
// if there is no time created, use now
93+
timestamp = System.currentTimeMillis();
9294
}
9395
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
9496
container.set(toolStats.hash, PersistentDataType.STRING, hash);
97+
itemStack.setItemMeta(itemMeta);
98+
}
99+
} else {
100+
// if hashes are disabled but the item has one, remove it.
101+
if (container.has(toolStats.hash, PersistentDataType.STRING)) {
102+
container.remove(toolStats.hash);
103+
itemStack.setItemMeta(itemMeta);
95104
}
96105
}
97-
ItemMeta clone = itemMeta.clone();
98-
if (location != null) {
99-
Bukkit.getRegionScheduler().runDelayed(toolStats, location, scheduledTask -> itemStack.setItemMeta(clone), 1);
100-
}
101-
102-
103106
}
104107
}
105108
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,33 @@ public void onJoin(PlayerJoinEvent event) {
7272
}
7373

7474
// generate a hash if the item doesn't have one
75-
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
76-
// make sure the item has an owner
77-
if (!container.has(toolStats.itemOwner, new UUIDDataType())) {
78-
continue;
79-
}
80-
UUID owner = container.get(toolStats.itemOwner, new UUIDDataType());
81-
if (owner == null) {
82-
continue;
75+
if (toolStats.config.getBoolean("generate-hash-for-items")) {
76+
if (!container.has(toolStats.hash, PersistentDataType.STRING)) {
77+
UUID owner = null;
78+
// get the current owner if there is one.
79+
if (container.has(toolStats.itemOwner, new UUIDDataType())) {
80+
owner = container.get(toolStats.itemOwner, new UUIDDataType());
81+
}
82+
// if there is no owner, use the player holding it
83+
if (owner == null) {
84+
owner = player.getUniqueId();
85+
}
86+
Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG);
87+
if (timestamp == null) {
88+
// if there is no time created, use now
89+
timestamp = System.currentTimeMillis();
90+
}
91+
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
92+
container.set(toolStats.hash, PersistentDataType.STRING, hash);
93+
itemStack.setItemMeta(itemMeta);
8394
}
84-
Long timestamp = container.get(toolStats.timeCreated, PersistentDataType.LONG);
85-
if (timestamp == null) {
86-
continue;
95+
} else {
96+
// if hashes are disabled but the item has one, remove it.
97+
if (container.has(toolStats.hash, PersistentDataType.STRING)) {
98+
container.remove(toolStats.hash);
99+
itemStack.setItemMeta(itemMeta);
87100
}
88-
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
89-
container.set(toolStats.hash, PersistentDataType.STRING, hash);
90101
}
91-
ItemMeta clone = itemMeta.clone();
92-
player.getScheduler().runDelayed(toolStats, scheduledTask -> itemStack.setItemMeta(clone), null, 1);
93102
}
94103
}
95104
}

0 commit comments

Comments
 (0)