Skip to content

Commit

Permalink
Implement more metadata methods and fix log annoyance
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 20, 2024
1 parent 90fc4f5 commit 3a1b64e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public class ClientConsoleCommandSource implements CommandSource {

@Override
public void sendMessage(Level level, Component message) {
log.atLevel(level).log(() -> ChatMessageLogger.ANSI_MESSAGE_SERIALIZER.serialize(message));
log.atLevel(level).setMessage("{}").addArgument(() -> ChatMessageLogger.ANSI_MESSAGE_SERIALIZER.serialize(message)).log();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@
@RequiredArgsConstructor
public class EntityMetadataState {
private final EntityType entityType;
private final Int2ObjectMap<EntityMetadata<?, ?>> metadataStore = new Int2ObjectOpenHashMap<>();
private final Int2ObjectMap<Object> metadataStore = new Int2ObjectOpenHashMap<>();

public void setMetadata(EntityMetadata<?, ?> metadata) {
this.metadataStore.put(metadata.getId(), metadata);
this.metadataStore.put(metadata.getId(), metadata.getValue());
}

public <T> void setMetadata(NamedEntityData namedEntityData, MetadataType<T> metadataType, T value) {
assert metadataType != null;

this.metadataStore.put(namedEntityData.networkId(), value);
}

public <T> T getMetadataThrown(NamedEntityData namedEntityData, MetadataType<T> metadataType) {
return getMetadata(namedEntityData, metadataType).orElseThrow(() -> new IllegalArgumentException("Metadata not found"));
}

public <T> Optional<T> getMetadata(NamedEntityData namedEntityData, MetadataType<T> metadataType) {
Expand All @@ -47,16 +57,14 @@ public <T> Optional<T> getMetadata(NamedEntityData namedEntityData, MetadataType

@SuppressWarnings("unchecked")
public <T> Optional<T> getMetadata(int id, MetadataType<T> metadataType) {
assert metadataType != null;

var metadata = this.metadataStore.get(id);
if (metadata == null) {
return Optional.empty();
}

if (metadata.getId() != metadataType.getId()) {
throw new IllegalArgumentException("Metadata type mismatch");
}

return Optional.of((T) metadata.getValue());
return Optional.of((T) metadata);
}

public Map<String, ?> toNamedMap() {
Expand All @@ -74,7 +82,7 @@ public <T> Optional<T> getMetadata(int id, MetadataType<T> metadataType) {
.forEach(namedData -> {
var metadata = this.metadataStore.get(namedData.networkId());
if (metadata != null) {
namedMap.put(namedData.key(), metadata.getValue());
namedMap.put(namedData.key(), metadata);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.protocol.bot.state.Level;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.auth.GameProfile;

@Getter
@Setter
public abstract class AbstractClientPlayer extends Player {
public AbstractClientPlayer(Level level, GameProfile gameProfile) {
super(level, gameProfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
*/
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.data.AttributeType;
import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.data.FluidType;
import com.soulfiremc.server.data.TagKey;
import com.soulfiremc.server.data.*;
import com.soulfiremc.server.protocol.bot.state.EntityAttributeState;
import com.soulfiremc.server.protocol.bot.state.EntityEffectState;
import com.soulfiremc.server.protocol.bot.state.EntityMetadataState;
Expand All @@ -37,6 +34,7 @@
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.data.game.entity.EntityEvent;
import org.geysermc.mcprotocollib.protocol.data.game.entity.RotationOrigin;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataType;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.ObjectData;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddEntityPacket;

Expand All @@ -50,15 +48,15 @@
@Setter
public abstract class Entity {
public static final float BREATHING_DISTANCE_BELOW_EYES = 0.11111111F;
private final EntityAttributeState attributeState = new EntityAttributeState();
private final EntityEffectState effectState = new EntityEffectState();
private final Set<TagKey<FluidType>> fluidOnEyes = new HashSet<>();
private final EntityType entityType;
private final EntityMetadataState metadataState;
public float fallDistance;
protected final EntityAttributeState attributeState = new EntityAttributeState();
protected final EntityEffectState effectState = new EntityEffectState();
protected final Set<TagKey<FluidType>> fluidOnEyes = new HashSet<>();
protected final EntityType entityType;
protected final EntityMetadataState metadataState;
protected float fallDistance;
protected UUID uuid;
protected ObjectData data;
private int entityId;
protected int entityId;
protected Level level;
protected double x;
protected double y;
Expand Down Expand Up @@ -272,4 +270,17 @@ public final Vector3d calculateViewVector(float xRot, float yRot) {
var m = MathHelper.sin(h);
return Vector3d.from(k * l, -m, (double) (j * l));
}

protected boolean getSharedFlag(int flag) {
return (this.metadataState.getMetadataThrown(NamedEntityData.ENTITY__SHARED_FLAGS, MetadataType.BYTE) & 1 << flag) != 0;
}

protected void setSharedFlag(int flag, boolean set) {
byte b = this.metadataState.getMetadataThrown(NamedEntityData.ENTITY__SHARED_FLAGS, MetadataType.BYTE);
if (set) {
this.metadataState.setMetadata(NamedEntityData.ENTITY__SHARED_FLAGS, MetadataType.BYTE, (byte) (b | 1 << flag));
} else {
this.metadataState.setMetadata(NamedEntityData.ENTITY__SHARED_FLAGS, MetadataType.BYTE, (byte) (b & ~(1 << flag)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.data.NamedEntityData;
import com.soulfiremc.server.protocol.bot.state.Level;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataType;

@Getter
@Setter
public abstract class LivingEntity extends Entity {
public LivingEntity(EntityType entityType, Level level) {
super(entityType, level);
Expand Down Expand Up @@ -51,4 +57,15 @@ public boolean isSleeping() {
}

public abstract boolean isUnderWater();

protected void setLivingEntityFlag(int key, boolean value) {
int j = this.metadataState.getMetadataThrown(NamedEntityData.LIVING_ENTITY__LIVING_ENTITY_FLAGS, MetadataType.BYTE);
if (value) {
j |= key;
} else {
j &= ~key;
}

this.metadataState.setMetadata(NamedEntityData.LIVING_ENTITY__LIVING_ENTITY_FLAGS, MetadataType.BYTE, (byte) j);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.protocol.BotConnection;
import com.soulfiremc.server.protocol.bot.model.AbilitiesData;
import com.soulfiremc.server.protocol.bot.state.Level;
import com.soulfiremc.server.util.MathHelper;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
Expand All @@ -38,7 +37,6 @@
@Setter
@EqualsAndHashCode(callSuper = true)
public class LocalPlayer extends AbstractClientPlayer {
private final AbilitiesData abilitiesData = new AbilitiesData();
private final BotConnection connection;
private boolean showReducedDebug;
private int opPermissionLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.data.FluidTags;
import com.soulfiremc.server.protocol.bot.model.AbilitiesData;
import com.soulfiremc.server.protocol.bot.state.Level;
import com.soulfiremc.server.util.MathHelper;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.mcprotocollib.auth.GameProfile;

@Getter
@Setter
public abstract class Player extends LivingEntity {
private final AbilitiesData abilitiesData = new AbilitiesData();
protected final GameProfile gameProfile;
protected boolean wasUnderwater = false;

Expand Down

0 comments on commit 3a1b64e

Please sign in to comment.