mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-28 07:20:24 +01:00
Leashable API
This commit is contained in:
parent
d72defffba
commit
52acc18e4f
4 changed files with 72 additions and 31 deletions
|
@ -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().removeLeash();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ import org.bukkit.craftbukkit.CraftServer;
|
||||||
import org.bukkit.entity.Boat;
|
import org.bukkit.entity.Boat;
|
||||||
import org.bukkit.entity.Entity;
|
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) {
|
public CraftBoat(CraftServer server, AbstractBoat entity) {
|
||||||
super(server, entity);
|
super(server, entity);
|
||||||
|
|
|
@ -784,43 +784,17 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isLeashed() {
|
public boolean isLeashed() {
|
||||||
if (!(this.getHandle() instanceof Mob)) {
|
return false; // Paper - implement in CraftMob & PaperLeashable
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return ((Mob) this.getHandle()).getLeashHolder() != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Entity getLeashHolder() throws IllegalStateException {
|
public Entity getLeashHolder() throws IllegalStateException {
|
||||||
Preconditions.checkState(this.isLeashed(), "Entity not leashed");
|
throw new IllegalStateException("Entity not leashed"); // Paper - implement in CraftMob & PaperLeashable
|
||||||
return ((Mob) this.getHandle()).getLeashHolder().getBukkitEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean unleash() {
|
|
||||||
if (!this.isLeashed()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
((Mob) this.getHandle()).removeLeash();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setLeashHolder(Entity holder) {
|
public boolean setLeashHolder(Entity holder) {
|
||||||
if (this.getHandle().generation || (this.getHandle() instanceof WitherBoss) || !(this.getHandle() instanceof Mob)) {
|
return false; // Paper - implement in CraftMob & PaperLeashable
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -11,7 +11,7 @@ import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.entity.Mob;
|
import org.bukkit.entity.Mob;
|
||||||
import org.bukkit.loot.LootTable;
|
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) {
|
public CraftMob(CraftServer server, net.minecraft.world.entity.Mob entity) {
|
||||||
super(server, entity);
|
super(server, entity);
|
||||||
paperPathfinder = new com.destroystokyo.paper.entity.PaperPathfinder(entity); // Paper - Mob Pathfinding API
|
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);
|
return getHandle().getExperienceReward((net.minecraft.server.level.ServerLevel) this.getHandle().level(), null);
|
||||||
}
|
}
|
||||||
// Paper end
|
// 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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue