Skip to content

Commit

Permalink
Handle rare cases of missing entity profile data before spawning remo…
Browse files Browse the repository at this point in the history
…te players

Some servers have broken protocol and do that. 🤷
Probably something with Citizens
  • Loading branch information
AlexProgrammerDE committed Feb 2, 2025
1 parent 039c57f commit 4091133
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,15 @@ public void onBorderWarningBlocks(ClientboundSetBorderWarningDistancePacket pack

@EventHandler
public void onEntitySpawn(ClientboundAddEntityPacket packet) {
var entityState = EntityFactory.createEntity(
connection,
EntityType.REGISTRY.getById(packet.getType().ordinal()),
currentLevel(),
packet.getUuid());
entityState.fromAddEntityPacket(packet);

entityTrackerState.addEntity(entityState);
EntityFactory.createEntity(
connection,
EntityType.REGISTRY.getById(packet.getType().ordinal()),
currentLevel(),
packet.getUuid())
.ifPresent(entityState -> {
entityState.fromAddEntityPacket(packet);
entityTrackerState.addEntity(entityState);
});
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
import java.util.UUID;

public class EntityFactory {
public static Entity createEntity(BotConnection connection, EntityType entityType, Level level, UUID uuid) {
if (entityType.playerEntity()) {
return new RemotePlayer(connection, level, connection.getEntityProfile(uuid).orElseThrow().getProfile());
public static Optional<Entity> createEntity(BotConnection connection, EntityType entityType, Level level, UUID uuid) {
if (entityType == EntityType.PLAYER) {
return connection.getEntityProfile(uuid).map(
playerListEntry -> new RemotePlayer(connection, level, playerListEntry.getProfile()));
} else if (entityType.livingEntity()) {
// TODO: Implement entity inventories
return new LivingEntity(entityType, level) {
return Optional.of(new LivingEntity(entityType, level) {
@Override
public Optional<SFItemStack> getItemBySlot(EquipmentSlot slot) {
return Optional.empty();
Expand All @@ -42,17 +43,17 @@ public Optional<SFItemStack> getItemBySlot(EquipmentSlot slot) {
@Override
public void setItemSlot(EquipmentSlot slot, @Nullable SFItemStack item) {
}
};
});
} else if (entityType.boatEntity()) {
return new AbstractBoat(entityType, level);
return Optional.of(new AbstractBoat(entityType, level));
} else if (entityType.minecartEntity()) {
return new AbstractMinecart(entityType, level);
return Optional.of(new AbstractMinecart(entityType, level));
} else if (entityType.windChargeEntity()) {
return new AbstractWindCharge(entityType, level);
return Optional.of(new AbstractWindCharge(entityType, level));
} else if (entityType.shulkerEntity()) {
return new Shulker(entityType, level);
return Optional.of(new Shulker(entityType, level));
} else {
return new Entity(entityType, level);
return Optional.of(new Entity(entityType, level));
}
}
}

0 comments on commit 4091133

Please sign in to comment.