Paper/patches/server/1010-Leashable-API.patch
Spottedleaf 8c5b837e05 Rework async chunk api implementation
Firstly, the old methods all routed to the CompletableFuture method.
However, the CF method could not guarantee that if the caller
was off-main that the future would be "completed" on-main. Since
the callback methods used the CF one, this meant that the callback
methods did not guarantee that the callbacks were to be called on
the main thread.

Now, all methods route to getChunkAtAsync(x, z, gen, urgent, cb)
so that the methods with the callback are guaranteed to invoke
the callback on the main thread. The CF behavior remains unchanged;
it may still appear to complete on main if invoked off-main.

Secondly, remove the scheduleOnMain invocation in the async
chunk completion. This unnecessarily delays the callback
by 1 tick.

Thirdly, add getChunksAtAsync(minX, minZ, maxX, maxZ, ...) which
will load chunks within an area. This method is provided as a helper
as keeping all chunks loaded within an area can be complicated to
implement for plugins (due to the lacking ticket API), and is
already implemented internally anyways.

Fourthly, remove the ticket addition that occured with getChunkAt
and getChunkAtAsync. The ticket addition may delay the unloading
of the chunk unnecessarily. It also fixes a very rare timing bug
where the future/callback would be completed after the chunk
unloads.
2024-11-18 23:00:59 -08:00

162 lines
6.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Sat, 22 Jun 2024 21:17:54 +0200
Subject: [PATCH] Leashable API
diff --git a/src/main/java/io/papermc/paper/entity/PaperLeashable.java b/src/main/java/io/papermc/paper/entity/PaperLeashable.java
new file mode 100644
index 0000000000000000000000000000000000000000..a9ddf9a4a07cd29833f38d7e5f42b2b14ec98f78
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/PaperLeashable.java
@@ -0,0 +1,50 @@
+package io.papermc.paper.entity;
+
+import com.google.common.base.Preconditions;
+import net.minecraft.world.entity.Leashable;
+import org.bukkit.craftbukkit.entity.CraftEntity;
+import org.bukkit.entity.Entity;
+
+public interface PaperLeashable extends io.papermc.paper.entity.Leashable {
+
+ Leashable getHandle();
+
+ @Override
+ default boolean isLeashed() {
+ return this.getHandle().getLeashHolder() != null;
+ }
+
+ @Override
+ default Entity getLeashHolder() throws IllegalStateException {
+ Preconditions.checkState(this.isLeashed(), "Entity not leashed");
+ return this.getHandle().getLeashHolder().getBukkitEntity();
+ }
+
+ private boolean unleash() {
+ if (!this.isLeashed()) {
+ return false;
+ }
+
+ this.getHandle().dropLeash(true, false);
+ return true;
+ }
+
+ @Override
+ default boolean setLeashHolder(Entity holder) {
+ if (this.getHandle() instanceof net.minecraft.world.entity.Entity entity && entity.generation) {
+ return false;
+ }
+
+ if (holder == null) {
+ return this.unleash();
+ }
+
+ if (holder.isDead()) {
+ return false;
+ }
+
+ this.unleash();
+ this.getHandle().setLeashedTo(((CraftEntity) holder).getHandle(), true);
+ return true;
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java
index ff82dc98478a8ac564bdbf4ec58da612e5f6c2ce..2a2839c31989d127739d829159a8b6e5b9a5210b 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java
@@ -8,7 +8,7 @@ import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Entity;
-public abstract class CraftBoat extends CraftVehicle implements Boat {
+public abstract class CraftBoat extends CraftVehicle implements Boat, io.papermc.paper.entity.PaperLeashable { // Paper - Leashable API
public CraftBoat(CraftServer server, AbstractBoat entity) {
super(server, entity);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
index f16067b674118a47735ad22797988d50b4415040..d0c409f4efad289e3e325f44b500fc72589d89d4 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -784,43 +784,17 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
@Override
public boolean isLeashed() {
- if (!(this.getHandle() instanceof Mob)) {
- return false;
- }
- return ((Mob) this.getHandle()).getLeashHolder() != null;
+ return false; // Paper - implement in CraftMob & PaperLeashable
}
@Override
public Entity getLeashHolder() throws IllegalStateException {
- Preconditions.checkState(this.isLeashed(), "Entity not leashed");
- return ((Mob) this.getHandle()).getLeashHolder().getBukkitEntity();
- }
-
- private boolean unleash() {
- if (!this.isLeashed()) {
- return false;
- }
- ((Mob) this.getHandle()).dropLeash(true, false);
- return true;
+ throw new IllegalStateException("Entity not leashed"); // Paper - implement in CraftMob & PaperLeashable
}
@Override
public boolean setLeashHolder(Entity holder) {
- if (this.getHandle().generation || (this.getHandle() instanceof WitherBoss) || !(this.getHandle() instanceof Mob)) {
- return false;
- }
-
- if (holder == null) {
- return this.unleash();
- }
-
- if (holder.isDead()) {
- return false;
- }
-
- this.unleash();
- ((Mob) this.getHandle()).setLeashedTo(((CraftEntity) holder).getHandle(), true);
- return true;
+ return false; // Paper - implement in CraftMob & PaperLeashable
}
@Override
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
index 95d7015a61098d1d22a501124d6bb8fba1516fe3..778a9d3f8bfe5dba59e1e655e4eeb8822678b8cf 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java
@@ -11,7 +11,7 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.loot.LootTable;
-public abstract class CraftMob extends CraftLivingEntity implements Mob {
+public abstract class CraftMob extends CraftLivingEntity implements Mob, io.papermc.paper.entity.PaperLeashable { // Paper - Leashable API
public CraftMob(CraftServer server, net.minecraft.world.entity.Mob entity) {
super(server, entity);
paperPathfinder = new com.destroystokyo.paper.entity.PaperPathfinder(entity); // Paper - Mob Pathfinding API
@@ -175,4 +175,21 @@ public abstract class CraftMob extends CraftLivingEntity implements Mob {
return getHandle().getExperienceReward((net.minecraft.server.level.ServerLevel) this.getHandle().level(), null);
}
// Paper end
+
+ // Paper start - Leashable API
+ @Override
+ public boolean isLeashed() {
+ return io.papermc.paper.entity.PaperLeashable.super.isLeashed();
+ }
+
+ @Override
+ public org.bukkit.entity.Entity getLeashHolder() throws IllegalStateException {
+ return io.papermc.paper.entity.PaperLeashable.super.getLeashHolder();
+ }
+
+ @Override
+ public boolean setLeashHolder(final org.bukkit.entity.Entity holder) {
+ return io.papermc.paper.entity.PaperLeashable.super.setLeashHolder(holder);
+ }
+ // Paper end - Leashable API
}