Skip to content

Commit

Permalink
Add methods for fake player spawning (#1479)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Jan 13, 2025
1 parent 96aab3d commit 3899957
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.mojang.datafixers.util.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
Expand Down Expand Up @@ -96,6 +97,14 @@ public Packet teleportEntityPacket(int entityId, Location location) {
return new PlPacket(packet);
}

@Override
public Packet updateHeadRotation(int entityId, Location location) {
PacketContainer packet = PlPacket.PL.createPacket(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
packet.getIntegers().write(0, entityId);
packet.getBytes().write(0, (byte) (location.getYaw() * 256 / 360));
return new PlPacket(packet);
}

@Override
public Packet entityMount(int entityId, int vehicleId) {
PacketContainer packet = PlPacket.PL.createPacket(PacketType.Play.Server.MOUNT);
Expand All @@ -106,6 +115,17 @@ public Packet entityMount(int entityId, int vehicleId) {
return new PlPacket(packet);
}

@Override
public Packet entityEquipment(
int entityId, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet) {
List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> e = new ArrayList<>();
if (helmet != null) e.add(Pair.of(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(helmet)));
if (chest != null) e.add(Pair.of(EquipmentSlot.CHEST, CraftItemStack.asNMSCopy(chest)));
if (legs != null) e.add(Pair.of(EquipmentSlot.LEGS, CraftItemStack.asNMSCopy(legs)));
if (feet != null) e.add(Pair.of(EquipmentSlot.FEET, CraftItemStack.asNMSCopy(feet)));
return new ModernPacket<>(new ClientboundSetEquipmentPacket(entityId, e));
}

@Override
public Packet entityHeadEquipment(int entityId, ItemStack helmet) {
var equipment = List.of(Pair.of(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(helmet)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import static tc.oc.pgm.util.platform.Supports.Variant.SPORTPAPER;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.v1_8_R3.DataWatcher;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.PacketPlayOutAttachEntity;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityHeadRotation;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityMetadata;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntity;
Expand Down Expand Up @@ -82,15 +86,38 @@ public Packet teleportEntityPacket(int entityId, Location location) {
true)); // On Ground + Height Correction
}

private static final EntityMock ENTITY_MOCK = new EntityMock();

@Override
public Packet updateHeadRotation(int entityId, Location location) {
return new SpPacket<>(new PacketPlayOutEntityHeadRotation(
ENTITY_MOCK.withId(entityId), (byte) (location.getYaw() * 256.0F / 360.0F)));
}

@Override
public Packet entityMount(int entityId, int vehicleId) {
return new SpPacket<>(new PacketPlayOutAttachEntity(entityId, vehicleId, false));
}

@Override
public Packet entityEquipment(
int entityId, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet) {
List<Packet> packets = new ArrayList<>();
if (helmet != null) packets.add(makeEquipment(entityId, EquipmentSlot.HEAD, helmet));
if (chest != null) packets.add(makeEquipment(entityId, EquipmentSlot.CHEST, chest));
if (legs != null) packets.add(makeEquipment(entityId, EquipmentSlot.LEGS, legs));
if (feet != null) packets.add(makeEquipment(entityId, EquipmentSlot.FEET, feet));
return Packet.of(packets.toArray(Packet[]::new));
}

@Override
public Packet entityHeadEquipment(int entityId, ItemStack helmet) {
return new SpPacket<>(new PacketPlayOutEntityEquipment(
entityId, EquipmentSlot.HEAD.ordinal(), CraftItemStack.asNMSCopy(helmet)));
return makeEquipment(entityId, EquipmentSlot.HEAD, helmet);
}

private Packet makeEquipment(int entityId, EquipmentSlot slot, ItemStack item) {
return new SpPacket<>(
new PacketPlayOutEntityEquipment(entityId, slot.ordinal(), CraftItemStack.asNMSCopy(item)));
}

@Override
Expand All @@ -100,4 +127,31 @@ public Packet entityMetadataPacket(int entityId, Entity entity, boolean complete
((CraftEntity) entity).getHandle().getDataWatcher(),
complete)); // true = all values, false = only dirty values
}

private static class EntityMock extends net.minecraft.server.v1_8_R3.Entity {
private int id;

public EntityMock() {
super(null);
}

@Override
public int getId() {
return id;
}

public EntityMock withId(int id) {
this.id = id;
return this;
}

@Override
protected void h() {}

@Override
protected void a(NBTTagCompound nbtTagCompound) {}

@Override
protected void b(NBTTagCompound nbtTagCompound) {}
}
}
43 changes: 43 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/packets/EntityPackets.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package tc.oc.pgm.util.nms.packets;

import static tc.oc.pgm.util.nms.NMSHacks.NMS_HACKS;
import static tc.oc.pgm.util.nms.Packets.TAB_PACKETS;
import static tc.oc.pgm.util.nms.PlayerUtils.PLAYER_UTILS;

import java.util.List;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.util.nms.EnumPlayerInfoAction;

public interface EntityPackets {

Expand Down Expand Up @@ -48,6 +54,38 @@ default Packet spawnFreezeEntity(Player player, int entityId, boolean legacy) {
}
}

default FakeEntity fakePlayer(Player original, ChatColor color) {
UUID uuid = UUID.randomUUID();
String team = uuid.toString().substring(0, 16);
// Add a reset to differentiate it from the real player name
String playerName = color + original.getName();
return new FakeEntity.Impl(allocateEntityId()) {
@Override
public Packet spawn(Location location, Vector velocity) {
var tabInfo = TAB_PACKETS.createPlayerInfoPacket(EnumPlayerInfoAction.ADD_PLAYER);
tabInfo.addPlayerInfo(uuid, playerName, 0, PLAYER_UTILS.getPlayerSkin(original), null);

return Packet.of(
tabInfo,
TAB_PACKETS.spawnPlayerPacket(entityId(), uuid, location, original),
TAB_PACKETS.teamCreatePacket(
team, team, color + "", "", false, false, List.of(playerName)));
}

@Override
public Packet teleport(Location location) {
return Packet.of(super.teleport(location), updateHeadRotation(entityId(), location));
}

@Override
public Packet destroy() {
var info = TAB_PACKETS.createPlayerInfoPacket(EnumPlayerInfoAction.REMOVE_PLAYER);
info.addPlayerInfo(uuid);
return Packet.of(info, super.destroy(), TAB_PACKETS.teamRemovePacket(team));
}
};
}

Packet spawnArmorStand(Location loc, int entityId, Vector velocity);

Packet spawnWitherSkull(Location location, int entityId, Vector velocity);
Expand All @@ -56,8 +94,13 @@ default Packet spawnFreezeEntity(Player player, int entityId, boolean legacy) {

Packet teleportEntityPacket(int entityId, Location location);

Packet updateHeadRotation(int entityId, Location location);

Packet entityMount(int entityId, int vehicleId);

Packet entityEquipment(
int entityId, ItemStack helmet, ItemStack chest, ItemStack leggings, ItemStack boots);

Packet entityHeadEquipment(int entityId, ItemStack helmet);

Packet entityMetadataPacket(int entityId, Entity entity, boolean complete);
Expand Down
4 changes: 4 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/packets/FakeEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ default Packet wearHead(ItemStack item) {
return ENTITIES.entityHeadEquipment(entityId(), item);
}

default Packet wear(ItemStack head, ItemStack chest, ItemStack legs, ItemStack boots) {
return ENTITIES.entityEquipment(entityId(), head, chest, legs, boots);
}

abstract class Impl implements FakeEntity {
private final int entityId;

Expand Down
13 changes: 5 additions & 8 deletions util/src/main/java/tc/oc/pgm/util/nms/packets/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ default void broadcast() {
}

static Packet of(Packet... packets) {
switch (packets.length) {
case 0:
return NoOpPacket.INSTANCE;
case 1:
return packets[0];
default:
return new CompoundPacket(packets);
}
return switch (packets.length) {
case 0 -> NoOpPacket.INSTANCE;
case 1 -> packets[0];
default -> new CompoundPacket(packets);
};
}

class NoOpPacket implements Packet {
Expand Down

0 comments on commit 3899957

Please sign in to comment.