Files
ToolStats/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java
2026-04-05 20:22:30 -04:00

118 lines
5.0 KiB
Java

/*
* 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.hyperlib.datatypes.UUIDDataType;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.UUID;
public class InventoryOpen implements Listener {
private final ToolStats toolStats;
public InventoryOpen(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler(ignoreCancelled = true)
public void onOpen(InventoryOpenEvent event) {
Inventory inventory = event.getInventory();
InventoryHolder holder = inventory.getHolder();
boolean isBlockInventory = holder instanceof BlockInventoryHolder || holder instanceof DoubleChest;
if (!(inventory.getHolder() instanceof BlockInventoryHolder)) {
// ignore not real inventories
return;
}
Player player = (Player) event.getPlayer();
if (!toolStats.configTools.checkWorld(player.getWorld().getName())) {
return;
}
for (ItemStack itemStack : inventory) {
if (itemStack == null) {
continue;
}
// ignore items that are not the right type
if (!toolStats.itemChecker.isValidItem(itemStack.getType())) {
continue;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
continue;
}
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
if (toolStats.config.getBoolean("tokens.enabled")) {
// if the token system is on and the item doesn't have stat keys
if (toolStats.itemChecker.keyCheck(container) && !container.has(toolStats.toolStatsKeys.getTokenType())) {
// add the tokens
String newTokens = toolStats.itemChecker.addTokensToExisting(itemStack);
if (newTokens == null) {
return;
}
container.set(toolStats.toolStatsKeys.getTokenApplied(), PersistentDataType.STRING, newTokens);
itemStack.setItemMeta(itemMeta);
}
}
// generate a hash if the item doesn't have one (and enabled)
// if hashes are disabled and the item has one, remove it.
if (toolStats.config.getBoolean("generate-hash-for-items")) {
if (!container.has(toolStats.toolStatsKeys.getHash(), PersistentDataType.STRING)) {
UUID owner = null;
// get the current owner if there is one.
if (container.has(toolStats.toolStatsKeys.getItemOwner(), new UUIDDataType())) {
owner = container.get(toolStats.toolStatsKeys.getItemOwner(), new UUIDDataType());
}
// if there is no owner, use the player holding it
if (owner == null) {
owner = player.getUniqueId();
}
Long timestamp = container.get(toolStats.toolStatsKeys.getTimeCreated(), PersistentDataType.LONG);
if (timestamp == null) {
// if there is no time created, use now
timestamp = System.currentTimeMillis();
}
String hash = toolStats.hashMaker.makeHash(itemStack.getType(), owner, timestamp);
container.set(toolStats.toolStatsKeys.getHash(), PersistentDataType.STRING, hash);
itemStack.setItemMeta(itemMeta);
}
} else {
// if hashes are disabled but the item has one, remove it.
if (container.has(toolStats.toolStatsKeys.getHash(), PersistentDataType.STRING)) {
container.remove(toolStats.toolStatsKeys.getHash());
itemStack.setItemMeta(itemMeta);
}
}
}
}
}