Use new metadata

This commit is contained in:
fullwall 2024-04-28 20:23:59 +08:00
parent 194198a9ce
commit efd3ec54f5
16 changed files with 81 additions and 37 deletions

View File

@ -2,9 +2,11 @@ package net.citizensnpcs.trait;
import org.bukkit.Location;
import org.bukkit.block.Bed;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
@ -36,30 +38,22 @@ public class SleepTrait extends Trait {
}
return;
}
if (SUPPORT_BLOCKDATA == null) {
try {
SUPPORT_BLOCKDATA = true;
at.getBlock().getBlockData();
} catch (NoSuchMethodError e) {
SUPPORT_BLOCKDATA = false;
}
}
if (npc.getEntity() instanceof Player) {
Player player = (Player) npc.getEntity();
if (!SUPPORT_BLOCKSTATE) {
NMS.sleep(player, true);
} else {
npc.data().set(NPC.Metadata.ENTITY_POSE, "SLEEPING");
if (SUPPORT_BLOCKDATA) {
try {
if (SUPPORT_BLOCKDATA && at.getBlock().getBlockData() instanceof Bed
|| at.getBlock().getState() instanceof Bed) {
if (at.getBlock().getBlockData() instanceof Bed || at.getBlock().getState() instanceof Bed) {
player.sleep(at, true);
} else {
NMS.sleep(player, true);
}
} catch (Throwable t) {
SUPPORT_BLOCKSTATE = false;
SUPPORT_BLOCKDATA = false;
NMS.sleep(player, true);
}
} else {
NMS.sleep(player, true);
}
sleeping = true;
} else if (npc.getEntity() instanceof Villager) {
@ -73,6 +67,7 @@ public class SleepTrait extends Trait {
}
private void wakeup() {
npc.data().remove(NPC.Metadata.ENTITY_POSE);
if (npc.getEntity() instanceof Player) {
NMS.sleep((Player) npc.getEntity(), false);
} else if (npc.getEntity() instanceof Villager) {
@ -82,5 +77,12 @@ public class SleepTrait extends Trait {
}
private static Boolean SUPPORT_BLOCKDATA = null;
private static boolean SUPPORT_BLOCKSTATE = true;
static {
try {
Block.class.getMethod("getBlockData");
SUPPORT_BLOCKDATA = true;
} catch (NoSuchMethodException | SecurityException e) {
SUPPORT_BLOCKDATA = false;
}
}
}

View File

@ -40,7 +40,9 @@ import net.citizensnpcs.api.util.BoundingBox;
import net.citizensnpcs.api.util.EntityDim;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.MirrorTrait;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.trait.versioned.ArmadilloTrait.ArmadilloState;
import net.citizensnpcs.trait.versioned.CamelTrait.CamelPose;
import net.citizensnpcs.trait.versioned.SnifferTrait.SnifferState;
@ -262,7 +264,9 @@ public interface NMSBridge {
public void setSitting(Tameable tameable, boolean sitting);
public default void setSneaking(Entity entity, boolean sneaking) {
if (entity instanceof Player) {
if (entity instanceof NPCHolder) {
((NPCHolder) entity).getNPC().getOrAddTrait(SneakTrait.class).setSneaking(sneaking);
} else if (entity instanceof Player) {
((Player) entity).setSneaking(sneaking);
}
}

View File

@ -47,6 +47,7 @@ import net.minecraft.server.v1_14_R1.DamageSource;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityHuman;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EntityPose;
import net.minecraft.server.v1_14_R1.EnumGamemode;
import net.minecraft.server.v1_14_R1.EnumItemSlot;
import net.minecraft.server.v1_14_R1.EnumProtocolDirection;
@ -357,7 +358,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
dB();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
dB();
}
}
public void setMoveDestination(double x, double y, double z, double speed) {

View File

@ -1500,9 +1500,10 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(Player player, boolean sleep) {
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
EntityPose pose = sleep ? EntityPose.SLEEPING : EntityPose.STANDING;
try {
ENTITY_SETPOSE_METHOD.invoke(getHandle(player), sleep ? EntityPose.SLEEPING : EntityPose.STANDING);
ENTITY_SETPOSE.invoke(getHandle(entity), pose);
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -46,6 +46,7 @@ import net.minecraft.server.v1_15_R1.DamageSource;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityHuman;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.EntityPose;
import net.minecraft.server.v1_15_R1.EnumGamemode;
import net.minecraft.server.v1_15_R1.EnumItemSlot;
import net.minecraft.server.v1_15_R1.EnumProtocolDirection;
@ -403,7 +404,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
dX();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
dX();
}
}
public void updateAI() {

View File

@ -1549,9 +1549,10 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(Player player, boolean sleep) {
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
EntityPose pose = sleep ? EntityPose.SLEEPING : EntityPose.STANDING;
try {
ENTITY_SETPOSE_METHOD.invoke(getHandle(player), sleep ? EntityPose.SLEEPING : EntityPose.STANDING);
ENTITY_SETPOSE.invoke(getHandle(entity), pose);
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -45,6 +45,7 @@ import net.minecraft.server.v1_16_R3.DamageSource;
import net.minecraft.server.v1_16_R3.Entity;
import net.minecraft.server.v1_16_R3.EntityHuman;
import net.minecraft.server.v1_16_R3.EntityPlayer;
import net.minecraft.server.v1_16_R3.EntityPose;
import net.minecraft.server.v1_16_R3.EnumGamemode;
import net.minecraft.server.v1_16_R3.EnumItemSlot;
import net.minecraft.server.v1_16_R3.EnumProtocolDirection;
@ -368,7 +369,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
}
eu();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(EntityPose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
eu();
}
}
private void updatePackets(boolean navigating) {

View File

@ -1573,9 +1573,10 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(Player player, boolean sleep) {
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
EntityPose pose = sleep ? EntityPose.SLEEPING : EntityPose.STANDING;
try {
ENTITY_SETPOSE_METHOD.invoke(getHandle(player), sleep ? EntityPose.SLEEPING : EntityPose.STANDING);
ENTITY_SETPOSE_METHOD.invoke(getHandle(entity), pose);
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -48,6 +48,7 @@ import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
@ -161,7 +162,11 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
updatePlayerPose();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
updatePlayerPose();
}
}
@Override

View File

@ -1567,8 +1567,8 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(org.bukkit.entity.Player player, boolean sleeping) {
getHandle(player).setPose(sleeping ? Pose.SLEEPING : Pose.STANDING);
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
getHandle(entity).setPose(sleep ? Pose.SLEEPING : Pose.STANDING);
}
@Override

View File

@ -49,6 +49,7 @@ import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
@ -162,7 +163,11 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
updatePlayerPose();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
updatePlayerPose();
}
}
@Override

View File

@ -1575,8 +1575,8 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(org.bukkit.entity.Player player, boolean sleeping) {
getHandle(player).setPose(sleeping ? Pose.SLEEPING : Pose.STANDING);
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
getHandle(entity).setPose(sleep ? Pose.SLEEPING : Pose.STANDING);
}
@Override

View File

@ -49,6 +49,7 @@ import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.tags.TagKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
@ -155,7 +156,11 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
updatePlayerPose();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
updatePlayerPose();
}
}
@Override

View File

@ -1774,8 +1774,8 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(org.bukkit.entity.Player player, boolean sleeping) {
getHandle(player).setPose(sleeping ? Pose.SLEEPING : Pose.STANDING);
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
getHandle(entity).setPose(sleep ? Pose.SLEEPING : Pose.STANDING);
}
@Override

View File

@ -49,6 +49,7 @@ import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.tags.TagKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
@ -157,7 +158,11 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
entity.playerTouch(this);
}
}
updatePlayerPose();
if (npc.data().has(NPC.Metadata.ENTITY_POSE)) {
setPose(Pose.valueOf(npc.data().get(NPC.Metadata.ENTITY_POSE)));
} else {
updatePlayerPose();
}
}
@Override

View File

@ -1770,8 +1770,8 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sleep(org.bukkit.entity.Player player, boolean sleeping) {
getHandle(player).setPose(sleeping ? Pose.SLEEPING : Pose.STANDING);
public void sleep(org.bukkit.entity.Player entity, boolean sleep) {
getHandle(entity).setPose(sleep ? Pose.SLEEPING : Pose.STANDING);
}
@Override