Skip to content

Commit

Permalink
Add sitting player pose
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Jan 4, 2024
1 parent 85b1f46 commit af0ca77
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
import com.mojang.authlib.properties.Property;
import com.mojang.datafixers.util.Pair;
import de.oliver.fancylib.ReflectionUtils;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.NpcAttribute;
import de.oliver.fancynpcs.api.NpcData;
import de.oliver.fancynpcs.api.events.NpcSpawnEvent;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
import io.papermc.paper.adventure.PaperAdventure;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import me.dave.chatcolorhandler.ModernChatColorHandler;
import net.minecraft.Optionull;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.RemoteChatSession;
import net.minecraft.network.protocol.game.*;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
Expand All @@ -46,6 +47,7 @@ public class Npc_1_19_4 extends Npc {
private final String localName;
private final UUID uuid;
private Entity npc;
private Display.TextDisplay sittingVehicle;

public Npc_1_19_4(NpcData data) {
super(data);
Expand Down Expand Up @@ -131,6 +133,12 @@ public void remove(Player player) {
ClientboundRemoveEntitiesPacket removeEntitiesPacket = new ClientboundRemoveEntitiesPacket(npc.getId());
serverPlayer.connection.send(removeEntitiesPacket);

// remove sitting vehicle
if (sittingVehicle != null) {
ClientboundRemoveEntitiesPacket removeSittingVehiclePacket = new ClientboundRemoveEntitiesPacket(sittingVehicle.getId());
serverPlayer.connection.send(removeSittingVehiclePacket);
}

isVisibleForPlayer.put(serverPlayer.getUUID(), false);
}

Expand Down Expand Up @@ -229,6 +237,11 @@ public void update(Player player) {
if (data.isSpawnEntity() && data.getLocation() != null) {
move(player);
}

NpcAttribute playerPoseAttr = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(org.bukkit.entity.EntityType.PLAYER, "pose");
if (data.getAttributes().containsKey(playerPoseAttr) && data.getAttributes().get(playerPoseAttr).equals("sitting")) {
setSitting(serverPlayer);
}
}

@Override
Expand Down Expand Up @@ -268,16 +281,20 @@ public void move(Player player) {
serverPlayer.connection.send(rotateHeadPacket);
}

private ClientboundPlayerInfoUpdatePacket.Entry getEntry(ServerPlayer npcPlayer) {
return new ClientboundPlayerInfoUpdatePacket.Entry(
npcPlayer.getUUID(),
npcPlayer.getGameProfile(),
data.isShowInTab(),
69,
npcPlayer.gameMode.getGameModeForPlayer(),
npcPlayer.getTabListDisplayName(),
Optionull.map(npcPlayer.getChatSession(), RemoteChatSession::asData)
);
public void setSitting(ServerPlayer serverPlayer) {
if (sittingVehicle == null) {
sittingVehicle = new Display.TextDisplay(EntityType.TEXT_DISPLAY, ((CraftWorld) data.getLocation().getWorld()).getHandle());
}

sittingVehicle.setPos(data.getLocation().x(), data.getLocation().y(), data.getLocation().z());

ClientboundAddEntityPacket addEntityPacket = new ClientboundAddEntityPacket(sittingVehicle);
serverPlayer.connection.send(addEntityPacket);

sittingVehicle.passengers = ImmutableList.of(npc);

ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(sittingVehicle);
serverPlayer.connection.send(packet);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static List<NpcAttribute> getAllAttributes() {

attributes.add(new NpcAttribute(
"pose",
List.of("standing", "crouching", "sleeping", "swimming"),
List.of("standing", "crouching", "sleeping", "swimming", "sitting"),
List.of(EntityType.PLAYER),
PlayerAttributes::setPose
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.mojang.authlib.properties.Property;
import com.mojang.datafixers.util.Pair;
import de.oliver.fancylib.ReflectionUtils;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.NpcAttribute;
import de.oliver.fancynpcs.api.NpcData;
import de.oliver.fancynpcs.api.events.NpcSpawnEvent;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
Expand All @@ -19,6 +21,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
Expand All @@ -44,6 +47,7 @@ public class Npc_1_20_1 extends Npc {
private final String localName;
private final UUID uuid;
private Entity npc;
private Display.TextDisplay sittingVehicle;

public Npc_1_20_1(NpcData data) {
super(data);
Expand Down Expand Up @@ -131,6 +135,12 @@ public void remove(Player player) {
ClientboundRemoveEntitiesPacket removeEntitiesPacket = new ClientboundRemoveEntitiesPacket(npc.getId());
serverPlayer.connection.send(removeEntitiesPacket);

// remove sitting vehicle
if (sittingVehicle != null) {
ClientboundRemoveEntitiesPacket removeSittingVehiclePacket = new ClientboundRemoveEntitiesPacket(sittingVehicle.getId());
serverPlayer.connection.send(removeSittingVehiclePacket);
}

isVisibleForPlayer.put(serverPlayer.getUUID(), false);
}

Expand Down Expand Up @@ -232,6 +242,11 @@ public void update(Player player) {
if (data.isSpawnEntity() && data.getLocation() != null) {
move(player);
}

NpcAttribute playerPoseAttr = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(org.bukkit.entity.EntityType.PLAYER, "pose");
if (data.getAttributes().containsKey(playerPoseAttr) && data.getAttributes().get(playerPoseAttr).equals("sitting")) {
setSitting(serverPlayer);
}
}

@Override
Expand Down Expand Up @@ -291,6 +306,22 @@ private ClientboundPlayerInfoUpdatePacket removeListed(ClientboundPlayerInfoUpda
return playerInfoUpdatePacket;
}

public void setSitting(ServerPlayer serverPlayer) {
if (sittingVehicle == null) {
sittingVehicle = new Display.TextDisplay(EntityType.TEXT_DISPLAY, ((CraftWorld) data.getLocation().getWorld()).getHandle());
}

sittingVehicle.setPos(data.getLocation().x(), data.getLocation().y(), data.getLocation().z());

ClientboundAddEntityPacket addEntityPacket = new ClientboundAddEntityPacket(sittingVehicle);
serverPlayer.connection.send(addEntityPacket);

sittingVehicle.passengers = ImmutableList.of(npc);

ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(sittingVehicle);
serverPlayer.connection.send(packet);
}

@Override
public float getEyeHeight() {
return npc.getEyeHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static List<NpcAttribute> getAllAttributes() {

attributes.add(new NpcAttribute(
"pose",
List.of("standing", "crouching", "sleeping", "swimming"),
List.of("standing", "crouching", "sleeping", "swimming", "sitting"),
List.of(EntityType.PLAYER),
PlayerAttributes::setPose
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.mojang.authlib.properties.Property;
import com.mojang.datafixers.util.Pair;
import de.oliver.fancylib.ReflectionUtils;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.NpcAttribute;
import de.oliver.fancynpcs.api.NpcData;
import de.oliver.fancynpcs.api.events.NpcSpawnEvent;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
Expand All @@ -22,6 +24,7 @@
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
Expand All @@ -47,6 +50,7 @@ public class Npc_1_20_2 extends Npc {
private final String localName;
private final UUID uuid;
private Entity npc;
private Display.TextDisplay sittingVehicle;

public Npc_1_20_2(NpcData data) {
super(data);
Expand Down Expand Up @@ -132,6 +136,12 @@ public void remove(Player player) {
ClientboundRemoveEntitiesPacket removeEntitiesPacket = new ClientboundRemoveEntitiesPacket(npc.getId());
serverPlayer.connection.send(removeEntitiesPacket);

// remove sitting vehicle
if (sittingVehicle != null) {
ClientboundRemoveEntitiesPacket removeSittingVehiclePacket = new ClientboundRemoveEntitiesPacket(sittingVehicle.getId());
serverPlayer.connection.send(removeSittingVehiclePacket);
}

isVisibleForPlayer.put(serverPlayer.getUUID(), false);
}

Expand Down Expand Up @@ -230,6 +240,11 @@ public void update(Player player) {
if (data.isSpawnEntity() && data.getLocation() != null) {
move(player);
}

NpcAttribute playerPoseAttr = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(org.bukkit.entity.EntityType.PLAYER, "pose");
if (data.getAttributes().containsKey(playerPoseAttr) && data.getAttributes().get(playerPoseAttr).equals("sitting")) {
setSitting(serverPlayer);
}
}

@Override
Expand Down Expand Up @@ -281,6 +296,22 @@ private ClientboundPlayerInfoUpdatePacket.Entry getEntry(ServerPlayer npcPlayer)
);
}

public void setSitting(ServerPlayer serverPlayer) {
if (sittingVehicle == null) {
sittingVehicle = new Display.TextDisplay(EntityType.TEXT_DISPLAY, ((CraftWorld) data.getLocation().getWorld()).getHandle());
}

sittingVehicle.setPos(data.getLocation().x(), data.getLocation().y(), data.getLocation().z());

ClientboundAddEntityPacket addEntityPacket = new ClientboundAddEntityPacket(sittingVehicle);
serverPlayer.connection.send(addEntityPacket);

sittingVehicle.passengers = ImmutableList.of(npc);

ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(sittingVehicle);
serverPlayer.connection.send(packet);
}

@Override
public float getEyeHeight() {
return npc.getEyeHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static List<NpcAttribute> getAllAttributes() {

attributes.add(new NpcAttribute(
"pose",
List.of("standing", "crouching", "sleeping", "swimming"),
List.of("standing", "crouching", "sleeping", "swimming", "sitting"),
List.of(EntityType.PLAYER),
PlayerAttributes::setPose
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.mojang.authlib.properties.Property;
import com.mojang.datafixers.util.Pair;
import de.oliver.fancylib.ReflectionUtils;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.NpcAttribute;
import de.oliver.fancynpcs.api.NpcData;
import de.oliver.fancynpcs.api.events.NpcSpawnEvent;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
Expand All @@ -22,6 +24,7 @@
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
Expand All @@ -47,6 +50,7 @@ public class Npc_1_20_4 extends Npc {
private final String localName;
private final UUID uuid;
private Entity npc;
private Display.TextDisplay sittingVehicle;

public Npc_1_20_4(NpcData data) {
super(data);
Expand Down Expand Up @@ -132,6 +136,12 @@ public void remove(Player player) {
ClientboundRemoveEntitiesPacket removeEntitiesPacket = new ClientboundRemoveEntitiesPacket(npc.getId());
serverPlayer.connection.send(removeEntitiesPacket);

// remove sitting vehicle
if (sittingVehicle != null) {
ClientboundRemoveEntitiesPacket removeSittingVehiclePacket = new ClientboundRemoveEntitiesPacket(sittingVehicle.getId());
serverPlayer.connection.send(removeSittingVehiclePacket);
}

isVisibleForPlayer.put(serverPlayer.getUUID(), false);
}

Expand Down Expand Up @@ -230,6 +240,11 @@ public void update(Player player) {
if (data.isSpawnEntity() && data.getLocation() != null) {
move(player);
}

NpcAttribute playerPoseAttr = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(org.bukkit.entity.EntityType.PLAYER, "pose");
if (data.getAttributes().containsKey(playerPoseAttr) && data.getAttributes().get(playerPoseAttr).equals("sitting")) {
setSitting(serverPlayer);
}
}

@Override
Expand Down Expand Up @@ -281,6 +296,22 @@ private ClientboundPlayerInfoUpdatePacket.Entry getEntry(ServerPlayer npcPlayer)
);
}

public void setSitting(ServerPlayer serverPlayer) {
if (sittingVehicle == null) {
sittingVehicle = new Display.TextDisplay(EntityType.TEXT_DISPLAY, ((CraftWorld) data.getLocation().getWorld()).getHandle());
}

sittingVehicle.setPos(data.getLocation().x(), data.getLocation().y(), data.getLocation().z());

ClientboundAddEntityPacket addEntityPacket = new ClientboundAddEntityPacket(sittingVehicle);
serverPlayer.connection.send(addEntityPacket);

sittingVehicle.passengers = ImmutableList.of(npc);

ClientboundSetPassengersPacket packet = new ClientboundSetPassengersPacket(sittingVehicle);
serverPlayer.connection.send(packet);
}

@Override
public float getEyeHeight() {
return npc.getEyeHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static List<NpcAttribute> getAllAttributes() {

attributes.add(new NpcAttribute(
"pose",
List.of("standing", "crouching", "sleeping", "swimming"),
List.of("standing", "crouching", "sleeping", "swimming", "sitting"),
List.of(EntityType.PLAYER),
PlayerAttributes::setPose
));
Expand Down

0 comments on commit af0ca77

Please sign in to comment.