Skip to content

Commit 5276dc5

Browse files
committed
Added logging support for copper golem chest transactions
1 parent 49c0f16 commit 5276dc5

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
<dependency>
217217
<groupId>io.papermc.paper</groupId>
218218
<artifactId>paper-api</artifactId>
219-
<version>1.21.1-R0.1-SNAPSHOT</version>
219+
<version>1.21.10-R0.1-SNAPSHOT</version>
220220
<scope>provided</scope>
221221
</dependency>
222222
<dependency>

src/main/java/net/coreprotect/listener/ListenerHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import net.coreprotect.listener.world.PortalCreateListener;
5656
import net.coreprotect.listener.world.StructureGrowListener;
5757
import net.coreprotect.paper.listener.BlockPreDispenseListener;
58+
import net.coreprotect.paper.listener.CopperGolemChestListener;
5859
import net.coreprotect.paper.listener.PaperChatListener;
5960

6061
public final class ListenerHandler {
@@ -72,6 +73,14 @@ public ListenerHandler(CoreProtect plugin) {
7273
BlockPreDispenseListener.useBlockPreDispenseEvent = false;
7374
}
7475

76+
try {
77+
Class.forName("io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent"); // Paper 1.21.10+
78+
pluginManager.registerEvents(new CopperGolemChestListener(plugin), plugin);
79+
}
80+
catch (Exception e) {
81+
// Ignore registration failures to remain compatible with older servers.
82+
}
83+
7584
// Block Listeners
7685
pluginManager.registerEvents(new BlockBreakListener(), plugin);
7786
pluginManager.registerEvents(new BlockBurnListener(), plugin);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

src/main/java/net/coreprotect/worldedit/WorldEditBlockState.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,9 @@ public BlockState copy(Location location) {
196196
return null;
197197
}
198198

199+
@Override
200+
public boolean isSuffocating() {
201+
return false;
202+
}
203+
199204
}

0 commit comments

Comments
 (0)