|
| 1 | +package net.coreprotect.paper.listener; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.UUID; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | + |
| 7 | +import org.bukkit.Location; |
| 8 | +import org.bukkit.block.Block; |
| 9 | +import org.bukkit.block.BlockState; |
| 10 | +import org.bukkit.entity.Entity; |
| 11 | +import org.bukkit.event.EventHandler; |
| 12 | +import org.bukkit.event.EventPriority; |
| 13 | +import org.bukkit.event.Listener; |
| 14 | +import org.bukkit.inventory.InventoryHolder; |
| 15 | +import org.bukkit.scheduler.BukkitTask; |
| 16 | + |
| 17 | +import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent; |
| 18 | +import net.coreprotect.CoreProtect; |
| 19 | +import net.coreprotect.config.Config; |
| 20 | +import net.coreprotect.listener.player.InventoryChangeListener; |
| 21 | + |
| 22 | +public final class CopperGolemChestListener implements Listener { |
| 23 | + |
| 24 | + private static final String COPPER_GOLEM_NAME = "COPPER_GOLEM"; |
| 25 | + private static final String USERNAME = "#copper_golem"; |
| 26 | + private static final long DELAY_TICKS = 50L; |
| 27 | + |
| 28 | + private final CoreProtect plugin; |
| 29 | + private final Map<UUID, PendingTransaction> pendingTransactions = new ConcurrentHashMap<>(); |
| 30 | + |
| 31 | + public CopperGolemChestListener(CoreProtect plugin) { |
| 32 | + this.plugin = plugin; |
| 33 | + } |
| 34 | + |
| 35 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 36 | + public void onValidate(ItemTransportingEntityValidateTargetEvent event) { |
| 37 | + if (!event.isAllowed()) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + Entity entity = event.getEntity(); |
| 42 | + if (entity == null || entity.getType() == null || !COPPER_GOLEM_NAME.equals(entity.getType().name())) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + Block block = event.getBlock(); |
| 47 | + if (block == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + BlockState blockState = block.getState(); |
| 52 | + if (!(blockState instanceof InventoryHolder)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + Location location = blockState.getLocation(); |
| 57 | + if (location == null || location.getWorld() == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (!Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + scheduleTransaction(entity, location); |
| 66 | + } |
| 67 | + |
| 68 | + private void scheduleTransaction(Entity entity, Location location) { |
| 69 | + UUID entityId = entity.getUniqueId(); |
| 70 | + PendingTransaction pendingTransaction = pendingTransactions.remove(entityId); |
| 71 | + if (pendingTransaction != null) { |
| 72 | + pendingTransaction.cancel(); |
| 73 | + } |
| 74 | + |
| 75 | + Location targetLocation = location.clone(); |
| 76 | + PendingTransaction scheduled = new PendingTransaction(); |
| 77 | + BukkitTask task = plugin.getServer().getScheduler().runTaskLater(plugin, () -> { |
| 78 | + if (!pendingTransactions.remove(entityId, scheduled)) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + Entity trackedEntity = plugin.getServer().getEntity(entityId); |
| 83 | + if (trackedEntity == null || !trackedEntity.isValid()) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + InventoryChangeListener.inventoryTransaction(USERNAME, targetLocation, null); |
| 88 | + }, DELAY_TICKS); |
| 89 | + |
| 90 | + scheduled.setTask(task); |
| 91 | + pendingTransactions.put(entityId, scheduled); |
| 92 | + } |
| 93 | + |
| 94 | + private static final class PendingTransaction { |
| 95 | + |
| 96 | + private BukkitTask task; |
| 97 | + |
| 98 | + private void cancel() { |
| 99 | + if (task != null) { |
| 100 | + task.cancel(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void setTask(BukkitTask task) { |
| 105 | + this.task = task; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments