Citizens2/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/entity/FoxController.java

238 lines
7.7 KiB
Java
Raw Normal View History

package net.citizensnpcs.nms.v1_16_R3.entity;
2020-06-25 11:24:32 +02:00
import java.lang.invoke.MethodHandle;
2020-06-25 11:24:32 +02:00
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftFox;
2020-06-25 11:24:32 +02:00
import org.bukkit.entity.Fox;
import org.bukkit.util.Vector;
2023-02-22 17:06:13 +01:00
2020-06-25 11:24:32 +02:00
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_16_R3.util.ForwardingNPCHolder;
import net.citizensnpcs.nms.v1_16_R3.util.NMSBoundingBox;
import net.citizensnpcs.nms.v1_16_R3.util.NMSImpl;
2020-06-25 11:24:32 +02:00
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.versioned.FoxTrait;
import net.citizensnpcs.util.NMS;
2020-06-25 11:24:32 +02:00
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_16_R3.AxisAlignedBB;
import net.minecraft.server.v1_16_R3.BlockPosition;
import net.minecraft.server.v1_16_R3.DamageSource;
import net.minecraft.server.v1_16_R3.DataWatcherObject;
import net.minecraft.server.v1_16_R3.Entity;
import net.minecraft.server.v1_16_R3.EntityBoat;
import net.minecraft.server.v1_16_R3.EntityFox;
import net.minecraft.server.v1_16_R3.EntityMinecartAbstract;
import net.minecraft.server.v1_16_R3.EntityTypes;
2023-06-25 16:37:30 +02:00
import net.minecraft.server.v1_16_R3.EnumPistonReaction;
import net.minecraft.server.v1_16_R3.FluidType;
import net.minecraft.server.v1_16_R3.IBlockData;
import net.minecraft.server.v1_16_R3.NBTTagCompound;
import net.minecraft.server.v1_16_R3.SoundEffect;
import net.minecraft.server.v1_16_R3.Tag;
import net.minecraft.server.v1_16_R3.Vec3D;
import net.minecraft.server.v1_16_R3.World;
2020-06-25 11:24:32 +02:00
public class FoxController extends MobEntityController {
public FoxController() {
super(EntityFoxNPC.class);
}
@Override
public Fox getBukkitEntity() {
return (Fox) super.getBukkitEntity();
}
public static class EntityFoxNPC extends EntityFox implements NPCHolder {
private final CitizensNPC npc;
public EntityFoxNPC(EntityTypes<? extends EntityFox> types, World world) {
this(types, world, null);
}
public EntityFoxNPC(EntityTypes<? extends EntityFox> types, World world, NPC npc) {
super(types, world);
this.npc = (CitizensNPC) npc;
}
@Override
public void a(AxisAlignedBB bb) {
super.a(NMSBoundingBox.makeBB(npc, bb));
}
2020-06-25 11:24:32 +02:00
@Override
public void a(DataWatcherObject<?> datawatcherobject) {
if (npc == null) {
super.a(datawatcherobject);
return;
2020-06-25 11:24:32 +02:00
}
NMSImpl.checkAndUpdateHeight(this, datawatcherobject, super::a);
2020-06-25 11:24:32 +02:00
}
@Override
protected void a(double d0, boolean flag, IBlockData block, BlockPosition blockposition) {
if (npc == null || !npc.isFlyable()) {
super.a(d0, flag, block, blockposition);
}
}
2022-11-13 16:33:18 +01:00
@Override
public void a(float strength, double dx, double dz) {
2023-11-05 09:35:22 +01:00
NMS.callKnockbackEvent(npc, strength, dx, dz, evt -> super.a((float) evt.getStrength(),
evt.getKnockbackVector().getX(), evt.getKnockbackVector().getZ()));
2022-11-13 16:33:18 +01:00
}
@Override
public boolean a(Tag<FluidType> tag, double d0) {
2023-11-05 09:35:22 +01:00
if (npc == null)
return super.a(tag, d0);
Vec3D old = getMot().add(0, 0, 0);
boolean res = super.a(tag, d0);
if (!npc.isPushableByFluids()) {
setMot(old);
}
return res;
2022-11-13 16:33:18 +01:00
}
2020-06-25 11:24:32 +02:00
@Override
public boolean b(float f, float f1) {
2023-11-05 09:35:22 +01:00
if (npc == null || !npc.isFlyable())
2020-06-25 11:24:32 +02:00
return super.b(f, f1);
return false;
}
@Override
public int bP() {
return NMS.getFallDistance(npc, super.bP());
}
2020-06-25 11:24:32 +02:00
@Override
public void checkDespawn() {
if (npc == null) {
super.checkDespawn();
}
}
@Override
public void collide(net.minecraft.server.v1_16_R3.Entity entity) {
2020-06-25 11:24:32 +02:00
// this method is called by both the entities involved - cancelling
// it will not stop the NPC from moving.
super.collide(entity);
if (npc != null) {
Util.callCollisionEvent(npc, entity.getBukkitEntity());
}
}
@Override
public boolean d(NBTTagCompound save) {
return npc == null ? super.d(save) : false;
}
2023-10-31 18:49:01 +01:00
@Override
public float dJ() {
return NMS.getJumpPower(npc, super.dJ());
}
@Override
public void g(Vec3D vec3d) {
if (npc == null || !npc.isFlyable()) {
super.g(vec3d);
} else {
NMSImpl.flyingMoveLogic(this, vec3d);
}
}
2020-06-25 11:24:32 +02:00
@Override
public CraftEntity getBukkitEntity() {
if (npc != null && !(super.getBukkitEntity() instanceof NPCHolder)) {
NMSImpl.setBukkitEntity(this, new FoxNPC(this));
}
return super.getBukkitEntity();
}
@Override
public NPC getNPC() {
return npc;
}
2023-07-03 18:43:58 +02:00
@Override
public EnumPistonReaction getPushReaction() {
return Util.callPistonPushEvent(npc) ? EnumPistonReaction.IGNORE : super.getPushReaction();
}
2020-06-25 11:24:32 +02:00
@Override
protected SoundEffect getSoundAmbient() {
return NMSImpl.getSoundEffect(npc, super.getSoundAmbient(), NPC.Metadata.AMBIENT_SOUND);
2020-06-25 11:24:32 +02:00
}
@Override
protected SoundEffect getSoundDeath() {
return NMSImpl.getSoundEffect(npc, super.getSoundDeath(), NPC.Metadata.DEATH_SOUND);
2020-06-25 11:24:32 +02:00
}
@Override
protected SoundEffect getSoundHurt(DamageSource damagesource) {
return NMSImpl.getSoundEffect(npc, super.getSoundHurt(damagesource), NPC.Metadata.HURT_SOUND);
2020-06-25 11:24:32 +02:00
}
@Override
2020-08-12 16:21:12 +02:00
public void i(double x, double y, double z) {
Vector vector = Util.callPushEvent(npc, x, y, z);
if (vector != null) {
2020-08-12 16:21:12 +02:00
super.i(vector.getX(), vector.getY(), vector.getZ());
2020-06-25 11:24:32 +02:00
}
}
@Override
public boolean isClimbing() {
2023-11-05 09:35:22 +01:00
if (npc == null || !npc.isFlyable())
2020-06-25 11:24:32 +02:00
return super.isClimbing();
2023-11-05 09:35:22 +01:00
else
2020-06-25 11:24:32 +02:00
return false;
}
@Override
public boolean isLeashed() {
2023-02-13 17:46:30 +01:00
return NMSImpl.isLeashed(npc, super::isLeashed, this);
2020-06-25 11:24:32 +02:00
}
@Override
public void mobTick() {
super.mobTick();
if (npc != null) {
NMSImpl.updateMinecraftAIState(npc, this);
npc.update();
FoxTrait ft = npc.getTraitNullable(FoxTrait.class);
if (ft != null) {
try {
SET_FACEPLANTED.invoke(this, ft.isFaceplanted());
} catch (Throwable e) {
e.printStackTrace();
}
w(ft.isInterested());
u(ft.isPouncing());
}
2020-06-25 11:24:32 +02:00
}
}
@Override
protected boolean n(Entity entity) {
2023-11-05 09:35:22 +01:00
if (npc != null && (entity instanceof EntityBoat || entity instanceof EntityMinecartAbstract))
2022-12-10 06:29:05 +01:00
return !npc.isProtected();
2020-06-25 11:24:32 +02:00
return super.n(entity);
}
private static final MethodHandle SET_FACEPLANTED = NMS.getMethodHandle(EntityFox.class, "x", true,
boolean.class);
2020-06-25 11:24:32 +02:00
}
public static class FoxNPC extends CraftFox implements ForwardingNPCHolder {
2020-06-25 11:24:32 +02:00
public FoxNPC(EntityFoxNPC entity) {
super((CraftServer) Bukkit.getServer(), entity);
}
}
}