mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2026-01-27 07:55:59 +00:00
144 lines
5.2 KiB
Java
144 lines
5.2 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 net.kyori.adventure.text.Component;
|
|
import org.bukkit.GameMode;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.block.BlockDispenseLootEvent;
|
|
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;
|
|
import java.util.Objects;
|
|
|
|
public class BlockDispenseEvent implements Listener {
|
|
|
|
private final ToolStats toolStats;
|
|
|
|
public BlockDispenseEvent(ToolStats toolStats) {
|
|
this.toolStats = toolStats;
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
public void onDispense(BlockDispenseLootEvent event) {
|
|
Player player = event.getPlayer();
|
|
if (player == null) {
|
|
return;
|
|
}
|
|
|
|
if (toolStats.config.getStringList("blacklist-worlds").contains(player.getWorld().getName())) {
|
|
return;
|
|
}
|
|
if (player.getGameMode() == GameMode.CREATIVE && !toolStats.config.getBoolean("allow-creative")) {
|
|
return;
|
|
}
|
|
|
|
List<ItemStack> loot = event.getDispensedLoot();
|
|
// probably won't ever happen
|
|
if (loot.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
List<ItemStack> newLoot = new ArrayList<>();
|
|
for (ItemStack lootItem : loot) {
|
|
ItemStack newLootItem = lootItem.clone();
|
|
Material lootItemMaterial = newLootItem.getType();
|
|
// if the item is one we want, do stuff
|
|
if (toolStats.itemChecker.isValidItem(lootItemMaterial)) {
|
|
newLootItem = addLootedOrigin(newLootItem, player);
|
|
}
|
|
|
|
// if the item returned null, add the original item
|
|
newLoot.add(Objects.requireNonNullElse(newLootItem, lootItem));
|
|
}
|
|
event.setDispensedLoot(newLoot);
|
|
}
|
|
|
|
/**
|
|
* Adds lore to newly generated items.
|
|
*
|
|
* @param itemStack The item to add lore to.
|
|
* @param owner The player that found the item.
|
|
* @return The item with the lore.
|
|
*/
|
|
private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) {
|
|
ItemStack newItem = itemStack.clone();
|
|
ItemMeta meta = itemStack.getItemMeta();
|
|
if (meta == null) {
|
|
return null;
|
|
}
|
|
long timeCreated = System.currentTimeMillis();
|
|
Date finalDate;
|
|
if (toolStats.config.getBoolean("normalize-time-creation")) {
|
|
finalDate = toolStats.numberFormat.normalizeTime(timeCreated);
|
|
timeCreated = finalDate.getTime();
|
|
}
|
|
PersistentDataContainer container = meta.getPersistentDataContainer();
|
|
|
|
if (container.has(toolStats.toolStatsKeys.getTimeCreated(), PersistentDataType.LONG) || container.has(toolStats.toolStatsKeys.getItemOwner(), new UUIDDataType())) {
|
|
return null;
|
|
}
|
|
|
|
// get the current lore the item
|
|
List<Component> lore;
|
|
if (meta.hasLore()) {
|
|
lore = meta.lore();
|
|
} else {
|
|
lore = new ArrayList<>();
|
|
}
|
|
|
|
// if creation date is enabled, add it
|
|
Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 2, newItem);
|
|
if (creationDate != null) {
|
|
container.set(toolStats.toolStatsKeys.getTimeCreated(), PersistentDataType.LONG, timeCreated);
|
|
container.set(toolStats.toolStatsKeys.getOriginType(), PersistentDataType.INTEGER, 2);
|
|
lore.add(creationDate);
|
|
meta.lore(lore);
|
|
}
|
|
|
|
// if ownership is enabled, add it
|
|
Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 2, newItem);
|
|
if (itemOwner != null) {
|
|
container.set(toolStats.toolStatsKeys.getItemOwner(), new UUIDDataType(), owner.getUniqueId());
|
|
container.set(toolStats.toolStatsKeys.getOriginType(), PersistentDataType.INTEGER, 2);
|
|
lore.add(itemOwner);
|
|
meta.lore(lore);
|
|
}
|
|
|
|
// if hash is enabled, add it
|
|
if (toolStats.config.getBoolean("generate-hash-for-items")) {
|
|
String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated);
|
|
container.set(toolStats.toolStatsKeys.getHash(), PersistentDataType.STRING, hash);
|
|
}
|
|
|
|
newItem.setItemMeta(meta);
|
|
return newItem;
|
|
}
|
|
}
|