Skip to content

Commit

Permalink
Fix npc's not spawning on modern versions
Browse files Browse the repository at this point in the history
  • Loading branch information
retrooper committed Dec 26, 2023
1 parent 3a5aa40 commit df6d7fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.player.*;
import com.github.retrooper.packetevents.protocol.world.Location;
Expand Down Expand Up @@ -86,10 +87,17 @@ public void spawn(Object channel) {
PacketEvents.getAPI().getProtocolManager().sendPacket(channel, playerInfo);

//TODO Later if we want entity metadata, its not supported on newer server versions though(confirm if its mandatory on older versions)
WrapperPlayServerSpawnPlayer spawnPlayer = new WrapperPlayServerSpawnPlayer(getId(),
getProfile().getUUID(),
getLocation());
PacketEvents.getAPI().getProtocolManager().sendPacket(channel, spawnPlayer);

PacketWrapper<?> spawnPacket;
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2)) {
spawnPacket = new WrapperPlayServerSpawnEntity(getId(), getProfile().getUUID(), EntityTypes.PLAYER, getLocation(), getLocation().getYaw(), 0, null);
}
else {
spawnPacket = new WrapperPlayServerSpawnPlayer(getId(),
getProfile().getUUID(),
getLocation());
}
PacketEvents.getAPI().getProtocolManager().sendPacket(channel, spawnPacket);

//Create team
if (getNameColor() != null || getPrefixName() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.util.MathUtil;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -60,6 +62,19 @@ public WrapperPlayServerSpawnEntity(int entityID, Optional<UUID> uuid, EntityTyp
this.velocity = velocity;
}

public WrapperPlayServerSpawnEntity(int entityID, @Nullable UUID uuid, EntityType entityType, Location location, float headYaw, int data, @Nullable Vector3d velocity) {
super(PacketType.Play.Server.SPAWN_ENTITY);
this.entityID = entityID;
this.uuid = Optional.ofNullable(uuid);
this.entityType = entityType;
this.position = location.getPosition();
this.pitch = location.getPitch();
this.yaw = location.getYaw();
this.headYaw = headYaw;
this.data = data;
this.velocity = Optional.ofNullable(velocity);
}

@Override
public void read() {
entityID = readVarInt();
Expand Down

0 comments on commit df6d7fa

Please sign in to comment.