Skip to content

Commit

Permalink
Implement more entity methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 21, 2024
1 parent acc7ca2 commit 6a74509
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public static JsonObject generateBlock(Block block) {
blockDesc.addProperty("requiresCorrectToolForDrops", true);
}

blockDesc.addProperty("fluidType",
BuiltInRegistries.FLUID.getKey(defaultState.getFluidState().getType()).toString());

if (defaultState.hasOffsetFunction()) {
var offsetData = new JsonObject();

Expand Down Expand Up @@ -84,6 +81,16 @@ public static JsonObject generateBlock(Block block) {
stateDesc.addProperty("default", true);
}

var fluidStateDesc = new JsonObject();
var fluidState = state.getFluidState();
fluidStateDesc.addProperty("type", BuiltInRegistries.FLUID.getKey(fluidState.getType()).toString());
fluidStateDesc.addProperty("amount", fluidState.getAmount());
if (fluidState.isSource()) {
fluidStateDesc.addProperty("source", true);
}

stateDesc.add("fluidState", fluidStateDesc);

var propertiesDesc = new JsonObject();
for (var property : state.getProperties()) {
var value = state.getValue(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void postConstruct() {
literal("collect")
.then(argument("block", new TagBasedArgumentType<BlockType, BlockTagResolvable>(
key -> tags -> block -> block.key().equals(key),
key -> tags -> block -> tags.isValueInTag(block, key),
key -> tags -> block -> tags.is(block, key),
BlockType.REGISTRY.values().stream().map(BlockType::key).toList(),
BlockTags.TAGS
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ private Iterable<Attribute.Modifier> getModifiersOrEmpty(ModifierOperation opera
return modifiers.values().stream().filter(modifier -> modifier.operation() == operation)
::iterator;
}

public void addModifier(Attribute.Modifier modifier) {
modifiers.put(modifier.id(), modifier);
}

public void removeModifier(Key id) {
modifiers.remove(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void handleTagData(Map<Key, Map<Key, int[]>> updateTags) {
tags.putAll(updateTags);
}

public <T extends RegistryValue<T>> boolean isValueInTag(T value, TagKey<T> tagKey) {
public <T extends RegistryValue<T>> boolean is(T value, TagKey<T> tagKey) {
return Arrays.stream(getValuesOfTag(tagKey)).anyMatch(t -> t == value.id());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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.metadata.Pose;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.ObjectData;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddEntityPacket;

Expand All @@ -47,6 +48,13 @@
@Getter
@Setter
public abstract class Entity {
protected static final int FLAG_ONFIRE = 0;
protected static final int FLAG_GLOWING = 6;
protected static final int FLAG_FALL_FLYING = 7;
private static final int FLAG_SHIFT_KEY_DOWN = 1;
private static final int FLAG_SPRINTING = 3;
private static final int FLAG_SWIMMING = 4;
private static final int FLAG_INVISIBLE = 5;
public static final float BREATHING_DISTANCE_BELOW_EYES = 0.11111111F;
protected final EntityAttributeState attributeState = new EntityAttributeState();
protected final EntityEffectState effectState = new EntityEffectState();
Expand Down Expand Up @@ -75,6 +83,7 @@ public abstract class Entity {
protected boolean minorHorizontalCollision;
protected boolean isInPowderSnow;
protected boolean wasInPowderSnow;
protected boolean wasTouchingWater;
public boolean noPhysics;

public Entity(EntityType entityType, Level level) {
Expand Down Expand Up @@ -179,7 +188,7 @@ public boolean isEyeInFluid(TagKey<FluidType> fluid) {
var breathingPos = eyePos.sub(0, BREATHING_DISTANCE_BELOW_EYES, 0);
var breathingCoords = breathingPos.toInt();

return level.tagsState().isValueInTag(level.getBlockState(breathingCoords).blockType().fluidType(), fluid);
return level.tagsState().is(level.getBlockState(breathingCoords).blockType().fluidType(), fluid);
}

/**
Expand Down Expand Up @@ -292,10 +301,6 @@ public boolean isNoGravity() {
return this.metadataState.getMetadata(NamedEntityData.ENTITY__NO_GRAVITY, MetadataType.BOOLEAN);
}

public void setNoGravity(boolean noGravity) {
this.metadataState.setMetadata(NamedEntityData.ENTITY__NO_GRAVITY, MetadataType.BOOLEAN, noGravity);
}

protected double getDefaultGravity() {
return 0.0;
}
Expand All @@ -310,4 +315,104 @@ protected void applyGravity() {
this.setDeltaMovement(this.getDeltaMovement().add(0.0, -d, 0.0));
}
}

public Pose getPose() {
return this.metadataState.getMetadata(NamedEntityData.ENTITY__POSE, MetadataType.POSE);
}

public void setPose(Pose pose) {
this.metadataState.setMetadata(NamedEntityData.ENTITY__POSE, MetadataType.POSE, pose);
}

public boolean hasPose(Pose pose) {
return this.getPose() == pose;
}

public boolean isShiftKeyDown() {
return this.getSharedFlag(FLAG_SHIFT_KEY_DOWN);
}

public void setShiftKeyDown(boolean keyDown) {
this.setSharedFlag(FLAG_SHIFT_KEY_DOWN, keyDown);
}

public boolean isSteppingCarefully() {
return this.isShiftKeyDown();
}

public boolean isSuppressingBounce() {
return this.isShiftKeyDown();
}

public boolean isDiscrete() {
return this.isShiftKeyDown();
}

public boolean isDescending() {
return this.isShiftKeyDown();
}

public boolean isCrouching() {
return this.hasPose(Pose.SNEAKING);
}

public boolean isSprinting() {
return this.getSharedFlag(FLAG_SPRINTING);
}

public void setSprinting(boolean sprinting) {
this.setSharedFlag(FLAG_SPRINTING, sprinting);
}

public boolean isSwimming() {
return this.getSharedFlag(FLAG_SWIMMING);
}

public void setSwimming(boolean swimming) {
this.setSharedFlag(FLAG_SWIMMING, swimming);
}

public boolean isVisuallySwimming() {
return this.hasPose(Pose.SWIMMING);
}

public boolean isVisuallyCrawling() {
return this.isVisuallySwimming() && !this.isInWater();
}

public boolean isInWater() {
return this.wasTouchingWater;
}

private boolean isInRain() {
return false; // TODO
}

private boolean isInBubbleColumn() {
return this.level().getBlockState(blockPos()).blockType().equals(BlockType.BUBBLE_COLUMN);
}

public boolean isInWaterOrRain() {
return this.isInWater() || this.isInRain();
}

public boolean isInWaterRainOrBubble() {
return this.isInWater() || this.isInRain() || this.isInBubbleColumn();
}

public boolean isInWaterOrBubble() {
return this.isInWater() || this.isInBubbleColumn();
}

public void updateSwimming() {
if (this.isSwimming()) {
this.setSwimming(this.isSprinting() && this.isInWater());
} else {
this.setSwimming(
this.isSprinting() && this.isUnderWater() && this.level.tagsState().is(this.level().getBlockState(blockPos()).blockType().fluidType(), FluidTags.WATER)
);
}
}

public abstract boolean isUnderWater();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
*/
package com.soulfiremc.server.protocol.bot.state.entity;

import com.soulfiremc.server.data.AttributeType;
import com.soulfiremc.server.data.EffectType;
import com.soulfiremc.server.data.EntityType;
import com.soulfiremc.server.data.NamedEntityData;
import com.soulfiremc.server.data.*;
import com.soulfiremc.server.protocol.bot.state.Level;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataType;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.Pose;

@Getter
@Setter
public abstract class LivingEntity extends Entity {
private static final Key SPEED_MODIFIER_POWDER_SNOW_ID = Key.key("powder_snow");
private static final Key SPRINTING_MODIFIER_ID = Key.key("sprinting");
private static final Attribute.Modifier SPEED_MODIFIER_SPRINTING = new Attribute.Modifier(
SPRINTING_MODIFIER_ID, 0.3F, ModifierOperation.ADD_MULTIPLIED_TOTAL
);

public LivingEntity(EntityType entityType, Level level) {
super(entityType, level);
}
Expand Down Expand Up @@ -80,4 +85,27 @@ protected double getEffectiveGravity() {
var bl = this.getDeltaMovement().getY() <= 0.0;
return bl && this.effectState().hasEffect(EffectType.SLOW_FALLING) ? Math.min(this.getGravity(), 0.01) : this.getGravity();
}

@Override
public void setSprinting(boolean sprinting) {
super.setSprinting(sprinting);
var lv = this.attributeState.getOrCreateAttribute(AttributeType.MOVEMENT_SPEED);
lv.removeModifier(SPEED_MODIFIER_SPRINTING.id());
if (sprinting) {
lv.addModifier(SPEED_MODIFIER_SPRINTING);
}
}

public boolean isSuppressingSlidingDownLadder() {
return this.isShiftKeyDown();
}

public boolean isFallFlying() {
return this.getSharedFlag(FLAG_FALL_FLYING);
}

@Override
public boolean isVisuallySwimming() {
return super.isVisuallySwimming() || !this.isFallFlying() && this.hasPose(Pose.FALL_FLYING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class LocalPlayer extends AbstractClientPlayer {
private boolean wasShiftKeyDown = false;
private boolean wasSprinting = false;
private boolean noPhysics = false;
private boolean crouching;
private int positionReminder = 0;
private ServerboundPlayerInputPacket lastSentInput = new ServerboundPlayerInputPacket(false, false, false, false, false, false, false);

Expand Down Expand Up @@ -206,4 +207,18 @@ public float height() {
public boolean isUnderWater() {
return this.wasUnderwater;
}

@Override
public boolean isShiftKeyDown() {
return connection.controlState().sneaking();
}

@Override
public boolean isCrouching() {
return this.crouching;
}

public boolean isMovingSlowly() {
return this.isCrouching() || this.isVisuallyCrawling();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public void tick() {
this.setPosition(x, this.y(), z);
}
}

@Override
public boolean isSwimming() {
return !this.abilitiesData.flying && !this.isSpectator() && super.isSwimming();
}
}

0 comments on commit 6a74509

Please sign in to comment.