From 6da54bd440b1a1ec3e0625ec0216bf94b51d1260 Mon Sep 17 00:00:00 2001 From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com> Date: Sat, 17 Aug 2024 23:17:18 +0200 Subject: [PATCH] Leashable API (#10961) --- patches/api/Fix-upstream-javadocs.patch | 13 ++ patches/api/Leashable-API.patch | 74 +++++++++++ patches/server/Leashable-API.patch | 161 ++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 patches/api/Leashable-API.patch create mode 100644 patches/server/Leashable-API.patch diff --git a/patches/api/Fix-upstream-javadocs.patch b/patches/api/Fix-upstream-javadocs.patch index b920b411ba..d533919a02 100644 --- a/patches/api/Fix-upstream-javadocs.patch +++ b/patches/api/Fix-upstream-javadocs.patch @@ -444,6 +444,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 * * @return whether the item frame is visible or not */ +diff --git a/src/main/java/org/bukkit/entity/LivingEntity.java b/src/main/java/org/bukkit/entity/LivingEntity.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/entity/LivingEntity.java ++++ b/src/main/java/org/bukkit/entity/LivingEntity.java +@@ -0,0 +0,0 @@ public interface LivingEntity extends Attributable, Damageable, ProjectileSource + /** + * Sets the leash on this entity to be held by the supplied entity. + *

+- * This method has no effect on EnderDragons, Withers, Players, or Bats. ++ * This method has no effect on players. + * Non-living entities excluding leashes will not persist as leash + * holders. + * diff --git a/src/main/java/org/bukkit/entity/Mob.java b/src/main/java/org/bukkit/entity/Mob.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/entity/Mob.java diff --git a/patches/api/Leashable-API.patch b/patches/api/Leashable-API.patch new file mode 100644 index 0000000000..990111eb50 --- /dev/null +++ b/patches/api/Leashable-API.patch @@ -0,0 +1,74 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com> +Date: Sat, 22 Jun 2024 21:11:58 +0200 +Subject: [PATCH] Leashable API + + +diff --git a/src/main/java/io/papermc/paper/entity/Leashable.java b/src/main/java/io/papermc/paper/entity/Leashable.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/entity/Leashable.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.entity; ++ ++import org.bukkit.entity.Entity; ++import org.checkerframework.checker.nullness.qual.NonNull; ++import org.checkerframework.checker.nullness.qual.Nullable; ++ ++/** ++ * Represents an entity that can be leashed. ++ */ ++public interface Leashable extends Entity { ++ ++ /** ++ * Returns whether the entity is currently leashed. ++ * ++ * @return whether the entity is leashed ++ */ ++ boolean isLeashed(); ++ ++ /** ++ * Gets the entity that is currently leading this entity. ++ * ++ * @return the entity holding the leash ++ * @throws IllegalStateException if not currently leashed ++ */ ++ @NonNull Entity getLeashHolder() throws IllegalStateException; ++ ++ /** ++ * Sets the leash on this entity to be held by the supplied entity. ++ *

++ * This method has no effect on players. ++ * ++ * @param holder the entity to leash this entity to, or {@code null} to unleash ++ * @return whether the operation was successful ++ */ ++ boolean setLeashHolder(@Nullable Entity holder); ++} +diff --git a/src/main/java/org/bukkit/entity/Boat.java b/src/main/java/org/bukkit/entity/Boat.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/entity/Boat.java ++++ b/src/main/java/org/bukkit/entity/Boat.java +@@ -0,0 +0,0 @@ import org.jetbrains.annotations.NotNull; + /** + * Represents a boat entity. + */ +-public interface Boat extends Vehicle { ++public interface Boat extends Vehicle, io.papermc.paper.entity.Leashable { // Paper - Leashable API + + /** + * Gets the wood type of the boat. +diff --git a/src/main/java/org/bukkit/entity/Mob.java b/src/main/java/org/bukkit/entity/Mob.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/entity/Mob.java ++++ b/src/main/java/org/bukkit/entity/Mob.java +@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable; + /** + * Represents a Mob. Mobs are living entities with simple AI. + */ +-public interface Mob extends LivingEntity, Lootable { ++public interface Mob extends LivingEntity, Lootable, io.papermc.paper.entity.Leashable { // Paper - Leashable API + + // Paper start + @Override diff --git a/patches/server/Leashable-API.patch b/patches/server/Leashable-API.patch new file mode 100644 index 0000000000..86faf80b76 --- /dev/null +++ b/patches/server/Leashable-API.patch @@ -0,0 +1,161 @@ +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..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/entity/PaperLeashable.java +@@ -0,0 +0,0 @@ ++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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java ++++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftBoat.java +@@ -0,0 +0,0 @@ import org.bukkit.craftbukkit.CraftServer; + import org.bukkit.entity.Boat; + import org.bukkit.entity.Entity; + +-public class CraftBoat extends CraftVehicle implements Boat { ++public class CraftBoat extends CraftVehicle implements Boat, io.papermc.paper.entity.PaperLeashable { // Paper - Leashable API + + public CraftBoat(CraftServer server, net.minecraft.world.entity.vehicle.Boat 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java ++++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java ++++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMob.java +@@ -0,0 +0,0 @@ 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 +@@ -0,0 +0,0 @@ public abstract class CraftMob extends CraftLivingEntity implements Mob { + return getHandle().getExperienceReward((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 + }