Skip to content

Commit a54436a

Browse files
committed
Implement spawn bounds check in teleport
1 parent 9934c17 commit a54436a

File tree

13 files changed

+169
-11
lines changed

13 files changed

+169
-11
lines changed

paper-api/src/main/java/org/bukkit/event/entity/EntityTeleportEvent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ public Location getFrom() {
4444
* Sets the location that this entity moved from
4545
*
4646
* @param from New location this entity moved from
47+
* @throws IllegalArgumentException if the location is not finite
4748
*/
4849
public void setFrom(@NotNull Location from) {
50+
from.checkFinite();
4951
this.from = from.clone();
5052
}
5153

@@ -63,9 +65,15 @@ public Location getTo() {
6365
* Sets the location that this entity moved to
6466
*
6567
* @param to New Location this entity moved to
68+
* @throws IllegalArgumentException if the location is not finite
6669
*/
6770
public void setTo(@Nullable Location to) {
68-
this.to = to != null ? to.clone() : null;
71+
if (to != null) {
72+
to.checkFinite();
73+
this.to = to.clone();
74+
} else {
75+
this.to = null;
76+
}
6977
}
7078

7179
@Override

paper-api/src/main/java/org/bukkit/event/player/PlayerMoveEvent.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void setFrom(@NotNull Location from) {
5353
*
5454
* @return Location the player moved to
5555
*/
56-
@NotNull // Paper
56+
@NotNull
5757
public Location getTo() {
5858
return to;
5959
}
@@ -139,7 +139,8 @@ public void setCancelled(boolean cancel) {
139139

140140
private void validateLocation(@NotNull Location loc) {
141141
Preconditions.checkArgument(loc != null, "Cannot use null location!");
142-
Preconditions.checkArgument(loc.getWorld() != null, "Cannot use null location with null world!");
142+
Preconditions.checkArgument(loc.getWorld() != null, "Cannot use location with null world!");
143+
loc.checkFinite();
143144
}
144145

145146
@NotNull

paper-server/patches/sources/net/minecraft/server/level/ServerPlayer.java.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,9 @@
764764
+ final org.bukkit.event.player.PlayerTeleportEvent tpEvent;
765765
+ // Paper start - gateway-specific teleport event
766766
+ if (this.portalProcess != null && this.portalProcess.isSamePortal(((net.minecraft.world.level.block.EndGatewayBlock) net.minecraft.world.level.block.Blocks.END_GATEWAY)) && this.level().getBlockEntity(this.portalProcess.getEntryPosition()) instanceof net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity theEndGatewayBlockEntity) {
767-
+ tpEvent = new com.destroystokyo.paper.event.player.PlayerTeleportEndGatewayEvent(this.getBukkitEntity(), enter, exit.clone(), new org.bukkit.craftbukkit.block.CraftEndGateway(this.level().getWorld(), theEndGatewayBlockEntity));
767+
+ tpEvent = new io.papermc.paper.event.player.PaperPlayerTeleportEndGatewayEvent(this.getBukkitEntity(), enter, exit.clone(), new org.bukkit.craftbukkit.block.CraftEndGateway(this.level().getWorld(), theEndGatewayBlockEntity));
768768
+ } else {
769-
+ tpEvent = new org.bukkit.event.player.PlayerTeleportEvent(this.getBukkitEntity(), enter, exit.clone(), teleportTransition.cause());
769+
+ tpEvent = new io.papermc.paper.event.player.PaperPlayerTeleportEvent(this.getBukkitEntity(), enter, exit.clone(), teleportTransition.cause());
770770
+ }
771771
+ // Paper end - gateway-specific teleport event
772772
+ org.bukkit.Bukkit.getServer().getPluginManager().callEvent(tpEvent);

paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@
10321032
+ final io.papermc.paper.entity.TeleportFlag.Relative flag = org.bukkit.craftbukkit.entity.CraftEntity.deltaRelativeToAPI(relativeArgument);
10331033
+ if (flag != null) relativeFlags.add(flag);
10341034
+ }
1035-
+ PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause, java.util.Set.copyOf(relativeFlags));
1035+
+ PlayerTeleportEvent event = new io.papermc.paper.event.player.PaperPlayerTeleportEvent(player, from.clone(), to.clone(), cause, java.util.Set.copyOf(relativeFlags));
10361036
+ // Paper end - Teleport API
10371037
+ this.cserver.getPluginManager().callEvent(event);
10381038
+

paper-server/patches/sources/net/minecraft/world/entity/Entity.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@
14811481
+ // Paper start - gateway-specific teleport event
14821482
+ final org.bukkit.event.entity.EntityTeleportEvent teleEvent;
14831483
+ if (this.portalProcess != null && this.portalProcess.isSamePortal(((net.minecraft.world.level.block.EndGatewayBlock) Blocks.END_GATEWAY)) && this.level.getBlockEntity(this.portalProcess.getEntryPosition()) instanceof net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity theEndGatewayBlockEntity) {
1484-
+ teleEvent = new com.destroystokyo.paper.event.entity.EntityTeleportEndGatewayEvent(this.getBukkitEntity(), this.getBukkitEntity().getLocation(), to, new org.bukkit.craftbukkit.block.CraftEndGateway(to.getWorld(), theEndGatewayBlockEntity));
1484+
+ teleEvent = new io.papermc.paper.event.entity.PaperEntityTeleportEndGatewayEvent(this.getBukkitEntity(), this.getBukkitEntity().getLocation(), to, new org.bukkit.craftbukkit.block.CraftEndGateway(to.getWorld(), theEndGatewayBlockEntity));
14851485
+ teleEvent.callEvent();
14861486
+ } else {
14871487
+ teleEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTeleportEvent(this, to);

paper-server/patches/sources/net/minecraft/world/entity/LivingEntity.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@
18781878
+
18791879
+ if (flag) {
18801880
+ if (!(this instanceof ServerPlayer)) {
1881-
+ org.bukkit.event.entity.EntityTeleportEvent teleport = new org.bukkit.event.entity.EntityTeleportEvent(this.getBukkitEntity(), new Location(this.level().getWorld(), x1, y1, z1), new Location(this.level().getWorld(), x, d, z));
1881+
+ org.bukkit.event.entity.EntityTeleportEvent teleport = new io.papermc.paper.event.entity.PaperEntityTeleportEvent(this.getBukkitEntity(), new Location(this.level().getWorld(), x1, y1, z1), new Location(this.level().getWorld(), x, d, z));
18821882
+ this.level().getCraftServer().getPluginManager().callEvent(teleport);
18831883
+ if (!teleport.isCancelled() && teleport.getTo() != null) { // Paper
18841884
+ Location to = teleport.getTo();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.papermc.paper.event.entity;
2+
3+
import com.destroystokyo.paper.event.entity.EntityTeleportEndGatewayEvent;
4+
import com.google.common.base.Preconditions;
5+
import org.bukkit.Location;
6+
import org.bukkit.block.EndGateway;
7+
import org.bukkit.craftbukkit.util.CraftLocation;
8+
import org.bukkit.entity.Entity;
9+
import org.jetbrains.annotations.ApiStatus;
10+
import org.jspecify.annotations.NullMarked;
11+
import org.jspecify.annotations.Nullable;
12+
13+
@NullMarked
14+
public class PaperEntityTeleportEndGatewayEvent extends EntityTeleportEndGatewayEvent {
15+
16+
@ApiStatus.Internal
17+
public PaperEntityTeleportEndGatewayEvent(final Entity entity, final Location from, final Location to, final EndGateway gateway) {
18+
super(entity, from, to, gateway);
19+
}
20+
21+
@Override
22+
public void setTo(final @Nullable Location to) {
23+
if (to != null) {
24+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(to), "destination Location is not in spawnable bounds");
25+
}
26+
super.setTo(to);
27+
}
28+
29+
@Override
30+
public void setFrom(final Location from) {
31+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(from), "origin Location is not in spawnable bounds");
32+
super.setFrom(from);
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.papermc.paper.event.entity;
2+
3+
import com.google.common.base.Preconditions;
4+
import org.bukkit.Location;
5+
import org.bukkit.craftbukkit.util.CraftLocation;
6+
import org.bukkit.entity.Entity;
7+
import org.bukkit.event.entity.EntityTeleportEvent;
8+
import org.jetbrains.annotations.ApiStatus;
9+
import org.jspecify.annotations.NullMarked;
10+
import org.jspecify.annotations.Nullable;
11+
12+
@NullMarked
13+
public class PaperEntityTeleportEvent extends EntityTeleportEvent {
14+
15+
@ApiStatus.Internal
16+
public PaperEntityTeleportEvent(final Entity entity, final Location from, @Nullable final Location to) {
17+
super(entity, from, to);
18+
}
19+
20+
@Override
21+
public void setTo(@Nullable final Location to) {
22+
if (to != null) {
23+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(to), "destination Location is not in spawnable bounds");
24+
}
25+
super.setTo(to);
26+
}
27+
28+
@Override
29+
public void setFrom(final Location from) {
30+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(from), "origin Location is not in spawnable bounds");
31+
super.setFrom(from);
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.papermc.paper.event.player;
2+
3+
import com.destroystokyo.paper.event.player.PlayerTeleportEndGatewayEvent;
4+
import com.google.common.base.Preconditions;
5+
import org.bukkit.Location;
6+
import org.bukkit.block.EndGateway;
7+
import org.bukkit.craftbukkit.util.CraftLocation;
8+
import org.bukkit.entity.Player;
9+
import org.jetbrains.annotations.ApiStatus;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
@NullMarked
13+
public class PaperPlayerTeleportEndGatewayEvent extends PlayerTeleportEndGatewayEvent {
14+
15+
@ApiStatus.Internal
16+
public PaperPlayerTeleportEndGatewayEvent(final Player player, final Location from, final Location to, final EndGateway gateway) {
17+
super(player, from, to, gateway);
18+
}
19+
20+
@Override
21+
public void setTo(final Location to) {
22+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(to), "destination Location is not in spawnable bounds");
23+
super.setTo(to);
24+
}
25+
26+
@Override
27+
public void setFrom(final Location from) {
28+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(from), "origin Location is not in spawnable bounds");
29+
super.setFrom(from);
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.papermc.paper.event.player;
2+
3+
import com.google.common.base.Preconditions;
4+
import io.papermc.paper.entity.TeleportFlag;
5+
import org.bukkit.Location;
6+
import org.bukkit.craftbukkit.util.CraftLocation;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.event.player.PlayerTeleportEvent;
9+
import org.jetbrains.annotations.ApiStatus;
10+
import org.jspecify.annotations.NullMarked;
11+
import org.jspecify.annotations.Nullable;
12+
import java.util.Set;
13+
14+
@NullMarked
15+
public class PaperPlayerTeleportEvent extends PlayerTeleportEvent {
16+
17+
@ApiStatus.Internal
18+
public PaperPlayerTeleportEvent(final Player player, final Location from, final @Nullable Location to) {
19+
super(player, from, to);
20+
}
21+
22+
@ApiStatus.Internal
23+
public PaperPlayerTeleportEvent(final Player player, final Location from, final @Nullable Location to, final TeleportCause cause) {
24+
super(player, from, to, cause);
25+
}
26+
27+
@ApiStatus.Internal
28+
public PaperPlayerTeleportEvent(final Player player, final Location from, final @Nullable Location to, final TeleportCause cause, final Set<TeleportFlag.Relative> teleportFlags) {
29+
super(player, from, to, cause, teleportFlags);
30+
}
31+
32+
@Override
33+
public void setTo(final Location to) {
34+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(to), "destination Location is not in spawnable bounds");
35+
super.setTo(to);
36+
}
37+
38+
@Override
39+
public void setFrom(final Location from) {
40+
Preconditions.checkArgument(CraftLocation.isInSpawnableBounds(from), "origin Location is not in spawnable bounds");
41+
super.setFrom(from);
42+
}
43+
}

0 commit comments

Comments
 (0)