Skip to content

Commit

Permalink
Replicate vanilla caching behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 15, 2024
1 parent 061bfb6 commit 1be78d4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@
import io.netty.handler.traffic.GlobalTrafficShapingHandler;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
import org.geysermc.mcprotocollib.protocol.data.ProtocolState;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundClientTickEndPacket;
import org.slf4j.Logger;
import org.slf4j.MDC;
Expand Down Expand Up @@ -254,25 +253,12 @@ public void sendPacket(Packet packet) {
session.send(packet);
}

public GameMode getEntityGameMode(UUID uuid) {
if (uuid.equals(dataManager.localPlayer().uuid())) {
return dataManager.gameModeState().localPlayerMode();
}

var profile = dataManager.playerListState().entries().get(uuid);
if (profile == null) {
return GameMode.SURVIVAL;
}

return profile.getGameMode();
}

public GameProfile getEntityProfile(UUID uuid) {
public PlayerListEntry getEntityProfile(UUID uuid) {
var profile = dataManager.playerListState().entries().get(uuid);
if (profile == null) {
return null;
throw new IllegalStateException("Profile not found for " + uuid);
}

return profile.getProfile();
return profile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;

@Getter
@Setter
public abstract class AbstractClientPlayer extends Player {
private final BotConnection connection;
private PlayerListEntry playerListEntry;

public AbstractClientPlayer(BotConnection connection, Level level, GameProfile gameProfile) {
super(level, level.levelData().spawnPos(), level.levelData().spawnAngle(), gameProfile);
Expand All @@ -36,11 +38,19 @@ public AbstractClientPlayer(BotConnection connection, Level level, GameProfile g

@Override
public boolean isSpectator() {
return connection.getEntityGameMode(uuid) == GameMode.SPECTATOR;
return getPlayerListEntry().getGameMode() == GameMode.SPECTATOR;
}

@Override
public boolean isCreative() {
return connection.getEntityGameMode(uuid) == GameMode.CREATIVE;
return getPlayerListEntry().getGameMode() == GameMode.CREATIVE;
}

private PlayerListEntry getPlayerListEntry() {
if (playerListEntry == null) {
playerListEntry = connection.getEntityProfile(uuid);
}

return playerListEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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));
return new RemotePlayer(connection, level, connection.getEntityProfile(uuid).getProfile());
} else if (entityType.livingEntity()) {
// TODO: Implement entity inventories
return new LivingEntity(entityType, level) {
Expand Down

0 comments on commit 1be78d4

Please sign in to comment.