Skip to content

Commit ec686f7

Browse files
authored
Add ItemTransportingEntityValidateTargetEvent closes #13114 (#13115)
1 parent 311810f commit ec686f7

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.papermc.paper.event.entity;
2+
3+
import org.bukkit.block.Block;
4+
import org.bukkit.entity.CopperGolem;
5+
import org.bukkit.entity.Entity;
6+
import org.bukkit.event.HandlerList;
7+
import org.bukkit.event.entity.EntityEvent;
8+
import org.jetbrains.annotations.ApiStatus;
9+
import org.jspecify.annotations.NullMarked;
10+
11+
/**
12+
* Called when an item-transporting entity (typically a {@link CopperGolem},
13+
* although other entities may be possible through non-API means)
14+
* is searching for or validating an existing target container block for
15+
* taking or depositing items.
16+
*
17+
* <p>This may be called multiple times per entity per tick, so listeners
18+
* should be careful to implement checks in an efficient manner.</p>
19+
*/
20+
@NullMarked
21+
public class ItemTransportingEntityValidateTargetEvent extends EntityEvent {
22+
protected static final HandlerList HANDLER_LIST = new HandlerList();
23+
24+
private final Block block;
25+
private boolean allowed = true;
26+
27+
@ApiStatus.Internal
28+
public ItemTransportingEntityValidateTargetEvent(
29+
final Entity entity,
30+
final Block block
31+
) {
32+
super(entity);
33+
this.block = block;
34+
}
35+
36+
/**
37+
* Sets if the entity is allowed to use {@link #getBlock()} as a target.
38+
*
39+
* @param allowed whether the target is allowed
40+
*/
41+
public void setAllowed(boolean allowed) {
42+
this.allowed = allowed;
43+
}
44+
45+
/**
46+
* Gets if the entity is allowed to use {@link #getBlock()} as a target.
47+
*
48+
* @return true if the target is allowed
49+
*/
50+
public boolean isAllowed() {
51+
return this.allowed;
52+
}
53+
54+
/**
55+
* Gets the target block the entity is validating.
56+
*
57+
* @return the target block
58+
*/
59+
public Block getBlock() {
60+
return this.block;
61+
}
62+
63+
public static HandlerList getHandlerList() {
64+
return HANDLER_LIST;
65+
}
66+
67+
@Override
68+
public HandlerList getHandlers() {
69+
return HANDLER_LIST;
70+
}
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- a/net/minecraft/world/entity/ai/behavior/TransportItemsBetweenContainers.java
2+
+++ b/net/minecraft/world/entity/ai/behavior/TransportItemsBetweenContainers.java
3+
@@ -323,7 +_,10 @@
4+
} else {
5+
boolean flag1 = this.isWantedBlock(mob, transportItemTarget.state)
6+
&& !this.isPositionAlreadyVisited(visited, unreachable, transportItemTarget, level)
7+
- && !this.isContainerLocked(transportItemTarget);
8+
+ // Paper start - ItemTransportingEntityValidateTargetEvent
9+
+ && !this.isContainerLocked(transportItemTarget)
10+
+ && org.bukkit.craftbukkit.event.CraftEventFactory.callTransporterValidateTarget(mob, level, transportItemTarget.pos);
11+
+ // Paper end - ItemTransportingEntityValidateTargetEvent
12+
return flag1 ? transportItemTarget : null;
13+
}
14+
}

paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.papermc.paper.connection.HorriblePlayerLoginEventHack;
1919
import io.papermc.paper.connection.PlayerConnection;
2020
import io.papermc.paper.event.connection.PlayerConnectionValidateLoginEvent;
21+
import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent;
2122
import net.minecraft.core.BlockPos;
2223
import net.minecraft.core.Direction;
2324
import net.minecraft.network.Connection;
@@ -38,6 +39,7 @@
3839
import net.minecraft.world.entity.Entity;
3940
import net.minecraft.world.entity.Leashable;
4041
import net.minecraft.world.entity.Mob;
42+
import net.minecraft.world.entity.PathfinderMob;
4143
import net.minecraft.world.entity.animal.AbstractFish;
4244
import net.minecraft.world.entity.animal.AbstractGolem;
4345
import net.minecraft.world.entity.animal.Animal;
@@ -2112,4 +2114,13 @@ public static Component handleLoginResult(PlayerList.LoginResult result, PlayerC
21122114

21132115
return disconnectReason;
21142116
}
2117+
2118+
public static boolean callTransporterValidateTarget(final PathfinderMob mob, final Level level, final BlockPos transportItemTarget) {
2119+
if (ItemTransportingEntityValidateTargetEvent.getHandlerList().getRegisteredListeners().length == 0) {
2120+
return true; // No listeners, skip event creation
2121+
}
2122+
final ItemTransportingEntityValidateTargetEvent event = new ItemTransportingEntityValidateTargetEvent(mob.getBukkitEntity(), CraftBlock.at(level, transportItemTarget));
2123+
event.callEvent();
2124+
return event.isAllowed();
2125+
}
21152126
}

0 commit comments

Comments
 (0)