Files
ToolStats/src/main/java/lol/hyper/toolstats/events/CreativeEvent.java
2025-05-06 20:29:27 -04:00

149 lines
5.4 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.toolstats.ToolStats;
import lol.hyper.toolstats.tools.UUIDDataType;
import net.kyori.adventure.text.Component;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class CreativeEvent implements Listener {
private final ToolStats toolStats;
public CreativeEvent(ToolStats toolStats) {
this.toolStats = toolStats;
}
@EventHandler
public void onCreativeEvent(InventoryCreativeEvent event) {
Player player = (Player) event.getWhoClicked();
// make sure they are in creative mode
if (player.getGameMode() != GameMode.CREATIVE) {
return;
}
ItemStack spawnedItem = event.getCursor();
if (!toolStats.itemChecker.isValidItem(spawnedItem.getType())) {
return;
}
ItemMeta spawnedItemMeta = spawnedItem.getItemMeta();
if (spawnedItemMeta == null) {
return;
}
// if the item already has an origin set, don't add it again
// this is needed since you can spam click an item and the event will fire again
PersistentDataContainer container = spawnedItemMeta.getPersistentDataContainer();
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
return;
}
// add the tags to the item
ItemStack newItem = addCreativeOrigin(spawnedItem, player);
if (newItem != null) {
event.setCursor(newItem);
}
}
/**
* Adds spawned in tags to item.
*
* @param itemStack The item add item to.
* @param owner The player spawning in.
* @return A copy of the item with the tags + lore.
*/
private ItemStack addCreativeOrigin(ItemStack itemStack, Player owner) {
ItemStack newSpawnedItem = itemStack.clone();
ItemMeta meta = newSpawnedItem.getItemMeta();
if (meta == null) {
toolStats.logger.warning(itemStack + " does NOT have any meta! Unable to update stats.");
return null;
}
// get the current time
long timeCreated = System.currentTimeMillis();
Date finalDate;
if (toolStats.config.getBoolean("normalize-time-creation")) {
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
timeCreated = finalDate.getTime();
} else {
finalDate = new Date(timeCreated);
}
PersistentDataContainer container = meta.getPersistentDataContainer();
// if the item already has the tag
// this is to prevent duplicate tags
if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) {
return null;
}
// get the current lore the item
List<Component> lore;
if (meta.hasLore()) {
lore = meta.lore();
} else {
lore = new ArrayList<>();
}
if (toolStats.configTools.checkConfig(itemStack.getType(), "spawned-in-on")) {
container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated);
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
String date = toolStats.numberFormat.formatDate(finalDate);
Component newLine = toolStats.configTools.formatLore("spawned-in.spawned-on", "{date}", date);
if (newLine == null) {
return null;
}
lore.add(newLine);
meta.lore(lore);
}
if (toolStats.configTools.checkConfig(itemStack.getType(), "spawned-in-by")) {
container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId());
container.set(toolStats.originType, PersistentDataType.INTEGER, 6);
Component newLine = toolStats.configTools.formatLore("spawned-in.spawned-by", "{player}", owner.getName());
if (newLine == null) {
return null;
}
lore.add(newLine);
meta.lore(lore);
}
// if hash is enabled, add it
if (toolStats.config.getBoolean("generate-hash-for-items")) {
String hash = toolStats.hashMaker.makeHash(newSpawnedItem.getType(), owner.getUniqueId(), timeCreated);
container.set(toolStats.hash, PersistentDataType.STRING, hash);
}
newSpawnedItem.setItemMeta(meta);
return newSpawnedItem;
}
}