|
| 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.hyperlib.datatypes.UUIDDataType; |
| 21 | +import lol.hyper.toolstats.ToolStats; |
| 22 | +import net.kyori.adventure.text.Component; |
| 23 | +import org.bukkit.Material; |
| 24 | +import org.bukkit.entity.Player; |
| 25 | +import org.bukkit.event.EventHandler; |
| 26 | +import org.bukkit.event.EventPriority; |
| 27 | +import org.bukkit.event.Listener; |
| 28 | +import org.bukkit.event.block.BlockDispenseLootEvent; |
| 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 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Date; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Objects; |
| 38 | + |
| 39 | +public class BlockDispenseEvent implements Listener { |
| 40 | + |
| 41 | + private final ToolStats toolStats; |
| 42 | + |
| 43 | + public BlockDispenseEvent(ToolStats toolStats) { |
| 44 | + this.toolStats = toolStats; |
| 45 | + } |
| 46 | + |
| 47 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 48 | + public void onDispense(BlockDispenseLootEvent event) { |
| 49 | + Player player = event.getPlayer(); |
| 50 | + |
| 51 | + if (player == null) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + List<ItemStack> loot = event.getDispensedLoot(); |
| 56 | + // probably won't ever happen |
| 57 | + if (loot.isEmpty()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + List<ItemStack> newLoot = new ArrayList<>(); |
| 62 | + for (ItemStack lootItem : loot) { |
| 63 | + ItemStack newLootItem = lootItem.clone(); |
| 64 | + Material lootItemMaterial = newLootItem.getType(); |
| 65 | + // if the item is one we want, do stuff |
| 66 | + if (toolStats.itemChecker.isValidItem(lootItemMaterial)) { |
| 67 | + newLootItem = addLootedOrigin(newLootItem, player); |
| 68 | + } |
| 69 | + |
| 70 | + // if the item returned null, add the original item |
| 71 | + newLoot.add(Objects.requireNonNullElse(newLootItem, lootItem)); |
| 72 | + } |
| 73 | + event.setDispensedLoot(newLoot); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Adds lore to newly generated items. |
| 78 | + * |
| 79 | + * @param itemStack The item to add lore to. |
| 80 | + * @param owner The player that found the item. |
| 81 | + * @return The item with the lore. |
| 82 | + */ |
| 83 | + private ItemStack addLootedOrigin(ItemStack itemStack, Player owner) { |
| 84 | + ItemStack newItem = itemStack.clone(); |
| 85 | + ItemMeta meta = itemStack.getItemMeta(); |
| 86 | + if (meta == null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + long timeCreated = System.currentTimeMillis(); |
| 90 | + Date finalDate; |
| 91 | + if (toolStats.config.getBoolean("normalize-time-creation")) { |
| 92 | + finalDate = toolStats.numberFormat.normalizeTime(timeCreated); |
| 93 | + timeCreated = finalDate.getTime(); |
| 94 | + } |
| 95 | + PersistentDataContainer container = meta.getPersistentDataContainer(); |
| 96 | + |
| 97 | + if (container.has(toolStats.timeCreated, PersistentDataType.LONG) || container.has(toolStats.itemOwner, PersistentDataType.LONG)) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + // get the current lore the item |
| 102 | + List<Component> lore; |
| 103 | + if (meta.hasLore()) { |
| 104 | + lore = meta.lore(); |
| 105 | + } else { |
| 106 | + lore = new ArrayList<>(); |
| 107 | + } |
| 108 | + |
| 109 | + // if creation date is enabled, add it |
| 110 | + Component creationDate = toolStats.itemLore.formatCreationTime(timeCreated, 2, newItem); |
| 111 | + if (creationDate != null) { |
| 112 | + container.set(toolStats.timeCreated, PersistentDataType.LONG, timeCreated); |
| 113 | + container.set(toolStats.originType, PersistentDataType.INTEGER, 2); |
| 114 | + lore.add(creationDate); |
| 115 | + meta.lore(lore); |
| 116 | + } |
| 117 | + |
| 118 | + // if ownership is enabled, add it |
| 119 | + Component itemOwner = toolStats.itemLore.formatOwner(owner.getName(), 2, newItem); |
| 120 | + if (itemOwner != null) { |
| 121 | + container.set(toolStats.itemOwner, new UUIDDataType(), owner.getUniqueId()); |
| 122 | + container.set(toolStats.originType, PersistentDataType.INTEGER, 2); |
| 123 | + lore.add(itemOwner); |
| 124 | + meta.lore(lore); |
| 125 | + } |
| 126 | + |
| 127 | + // if hash is enabled, add it |
| 128 | + if (toolStats.config.getBoolean("generate-hash-for-items")) { |
| 129 | + String hash = toolStats.hashMaker.makeHash(newItem.getType(), owner.getUniqueId(), timeCreated); |
| 130 | + container.set(toolStats.hash, PersistentDataType.STRING, hash); |
| 131 | + } |
| 132 | + |
| 133 | + newItem.setItemMeta(meta); |
| 134 | + return newItem; |
| 135 | + } |
| 136 | +} |
0 commit comments