Skip to content

Commit

Permalink
Mostly finish implementing attribute system
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Jul 9, 2024
1 parent d62f79e commit 796c37d
Show file tree
Hide file tree
Showing 25 changed files with 145 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void onPacketReceive(final PacketReceiveEvent event) {
// 27/12/2023 - Dynamic values for more than just one entity type?
// 28/12/2023 - Player-only is fine
// 30/12/2023 - Expansions differ in 1.9+
final float scale = (float) packetEntity.getAttribute(Attributes.GENERIC_SCALE).get();
final float scale = (float) packetEntity.getAttributeValue(Attributes.GENERIC_SCALE);
if (targetVector.y > (minVerticalDisplacement * scale) && targetVector.y < (maxVerticalDisplacement * scale)
&& Math.abs(targetVector.x) < (maxHorizontalDisplacement * scale)
&& Math.abs(targetVector.z) < (maxHorizontalDisplacement * scale)) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ac/grim/grimac/checks/impl/combat/Reach.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private boolean isKnownInvalid(PacketEntity reachEntity) {
if (reachEntity.getType() == EntityTypes.END_CRYSTAL) {
targetBox = new SimpleCollisionBox(reachEntity.trackedServerPosition.getPos().subtract(1, 0, 1), reachEntity.trackedServerPosition.getPos().add(1, 2, 1));
}
return ReachUtils.getMinReachToBox(player, targetBox) > player.compensatedEntities.getSelf().getAttribute(Attributes.PLAYER_ENTITY_INTERACTION_RANGE).get();
return ReachUtils.getMinReachToBox(player, targetBox) > player.compensatedEntities.getSelf().getAttributeValue(Attributes.PLAYER_ENTITY_INTERACTION_RANGE);
}
}

Expand Down Expand Up @@ -200,7 +200,7 @@ private String checkReach(PacketEntity reachEntity, Vector3d from, boolean isPre
}

// +3 would be 3 + 3 = 6, which is the pre-1.20.5 behaviour, preventing "Missed Hitbox"
final double distance = player.compensatedEntities.getSelf().getAttribute(Attributes.PLAYER_ENTITY_INTERACTION_RANGE).get() + 3;
final double distance = player.compensatedEntities.getSelf().getAttributeValue(Attributes.PLAYER_ENTITY_INTERACTION_RANGE) + 3;
for (Vector lookVec : possibleLookDirs) {
for (double eye : player.getPossibleEyeHeights()) {
Vector eyePos = new Vector(from.getX(), from.getY() + eye, from.getZ());
Expand All @@ -224,7 +224,7 @@ private String checkReach(PacketEntity reachEntity, Vector3d from, boolean isPre
if (minDistance == Double.MAX_VALUE) {
cancelBuffer = 1;
return "Missed hitbox";
} else if (minDistance > player.compensatedEntities.getSelf().getAttribute(Attributes.PLAYER_ENTITY_INTERACTION_RANGE).get()) {
} else if (minDistance > player.compensatedEntities.getSelf().getAttributeValue(Attributes.PLAYER_ENTITY_INTERACTION_RANGE)) {
cancelBuffer = 1;
return String.format("%.5f", minDistance) + " blocks";
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ac/grim/grimac/player/GrimPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,14 @@ public void trackBaseTickAddition(Vector vector) {
public float getMaxUpStep() {
final PacketEntitySelf self = compensatedEntities.getSelf();
final PacketEntity riding = self.getRiding();
if (riding == null) return (float) self.getAttribute(Attributes.GENERIC_STEP_HEIGHT).get();
if (riding == null) return (float) self.getAttributeValue(Attributes.GENERIC_STEP_HEIGHT);

if (riding.isBoat()) {
return 0f;
}

// Pigs, horses, striders, and other vehicles all have 1 stepping height by default
return (float) riding.getAttribute(Attributes.GENERIC_STEP_HEIGHT).get();
return (float) riding.getAttributeValue(Attributes.GENERIC_STEP_HEIGHT);
}

public void sendTransaction() {
Expand Down Expand Up @@ -551,7 +551,7 @@ public CompensatedInventory getInventory() {

public List<Double> getPossibleEyeHeights() { // We don't return sleeping eye height
if (getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_14)) { // Elytra, sneaking (1.14), standing
final float scale = (float) compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_SCALE).get();
final float scale = (float) compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_SCALE);
return Arrays.asList(0.4 * scale, 1.27 * scale, 1.62 * scale);
} else if (getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_9)) { // Elytra, sneaking, standing
return Arrays.asList(0.4, 1.54, 1.62);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void check(PositionUpdate update) {
SimpleCollisionBox interTruePositions = riding.getPossibleCollisionBoxes();

// We shrink the expanded bounding box to what the packet positions can be, for a smaller box
final float scale = (float) riding.getAttribute(Attributes.GENERIC_SCALE).get();
final float scale = (float) riding.getAttributeValue(Attributes.GENERIC_SCALE);
float width = BoundingBoxSize.getWidth(player, riding) * scale;
float height = BoundingBoxSize.getHeight(player, riding) * scale;
interTruePositions.expand(-width, 0, -width);
Expand Down Expand Up @@ -238,7 +238,7 @@ private void check(PositionUpdate update) {
if (player.isInBed) return;

if (!player.compensatedEntities.getSelf().inVehicle()) {
player.speed = player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get();
player.speed = player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_MOVEMENT_SPEED);
if (player.hasGravity != player.playerEntityHasGravity) {
player.pointThreeEstimator.updatePlayerGravity();
}
Expand Down Expand Up @@ -444,18 +444,8 @@ private void check(PositionUpdate update) {
} else if (riding == null) {
wasChecked = true;

// Depth strider was added in 1.8
if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_8)) {
player.depthStriderLevel = (float) player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_WATER_MOVEMENT_EFFICIENCY).get();
} else {
player.depthStriderLevel = 0;
}

if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_19)) {
player.sneakingSpeedMultiplier = (float) player.compensatedEntities.getSelf().getAttribute(Attributes.PLAYER_SNEAKING_SPEED).get();
} else {
player.sneakingSpeedMultiplier = 0.3F;
}
player.depthStriderLevel = (float) player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_WATER_MOVEMENT_EFFICIENCY);
player.sneakingSpeedMultiplier = (float) player.compensatedEntities.getSelf().getAttributeValue(Attributes.PLAYER_SNEAKING_SPEED);

// This is wrong and the engine was not designed around stuff like this
player.verticalCollision = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static boolean canEnterPose(GrimPlayer player, Pose pose, double x, doubl
}

protected static SimpleCollisionBox getBoundingBoxForPose(GrimPlayer player, Pose pose, double x, double y, double z) {
final float scale = (float) player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_SCALE).get();
final float scale = (float) player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_SCALE);
final float width = pose.width * scale;
final float height = pose.height * scale;
float radius = width / 2.0F;
Expand Down Expand Up @@ -145,7 +145,7 @@ public void updatePowderSnow() {
if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_16_4)) return;

// The client first desync's this attribute
final ValuedAttribute playerSpeed = player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_MOVEMENT_SPEED);
final ValuedAttribute playerSpeed = player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get();
playerSpeed.property().get().getModifiers().removeIf(modifier -> modifier.getUUID().equals(CompensatedEntities.SNOW_MODIFIER_UUID) || modifier.getName().getKey().equals("powder_snow"));
playerSpeed.recalculate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void tickFireworksBox() {
if (entity == null) continue;

SimpleCollisionBox entityBox = entity.getPossibleCollisionBoxes();
final float scale = (float) entity.getAttribute(Attributes.GENERIC_SCALE).get();
final float scale = (float) entity.getAttributeValue(Attributes.GENERIC_SCALE);
float width = BoundingBoxSize.getWidth(player, entity) * scale;
float height = BoundingBoxSize.getHeight(player, entity) * scale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.github.retrooper.packetevents.protocol.world.states.type.StateType;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
import com.github.retrooper.packetevents.util.Vector3d;
import org.bukkit.Bukkit;
import org.bukkit.util.Vector;

public class MovementTicker {
Expand Down Expand Up @@ -308,8 +309,8 @@ public void doNormalMove(float blockFriction) {

public void livingEntityTravel() {
double playerGravity = player.compensatedEntities.getSelf().getRiding() == null
? player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_GRAVITY).get()
: player.compensatedEntities.getSelf().getRiding().getAttribute(Attributes.GENERIC_GRAVITY).get();
? player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_GRAVITY)
: player.compensatedEntities.getSelf().getRiding().getAttributeValue(Attributes.GENERIC_GRAVITY);

boolean isFalling = player.actualMovement.getY() <= 0.0;
if (isFalling && player.compensatedEntities.getSlowFallingAmplifier() != null) {
Expand Down Expand Up @@ -343,6 +344,7 @@ public void livingEntityTravel() {

if (player.depthStriderLevel > 0.0F) {
final float divisor = player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_21) ? 1.0F : 3.0F;
Bukkit.broadcastMessage("level: " + player.depthStriderLevel);
swimFriction += (0.54600006F - swimFriction) * player.depthStriderLevel / divisor;
swimSpeed += (player.speed - swimSpeed) * player.depthStriderLevel / divisor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public MovementTickerHorse(GrimPlayer player) {

if (!horsePacket.hasSaddle) return;

player.speed = horsePacket.getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get();
player.speed = horsePacket.getAttributeValue(Attributes.GENERIC_MOVEMENT_SPEED);

// Setup player inputs
float horizInput = player.vehicleData.vehicleHorizontal * 0.5F;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public MovementTickerPig(GrimPlayer player) {
@Override
public float getSteeringSpeed() { // Vanilla multiples by 0.225f
PacketEntityRideable pig = (PacketEntityRideable) player.compensatedEntities.getSelf().getRiding();
return (float) pig.getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get() * 0.225f;
return (float) pig.getAttributeValue(Attributes.GENERIC_MOVEMENT_SPEED) * 0.225f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void livingEntityAIStep() {
@Override
public float getSteeringSpeed() {
PacketEntityStrider strider = (PacketEntityStrider) player.compensatedEntities.getSelf().getRiding();
return (float) strider.getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get() * (strider.isShaking ? 0.23F : 0.55F);
return (float) strider.getAttributeValue(Attributes.GENERIC_MOVEMENT_SPEED) * (strider.isShaking ? 0.23F : 0.55F);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public Vector handleStartingVelocityUncertainty(GrimPlayer player, VectorData ve
// We can't simulate the player's Y velocity, unknown number of ticks with a gravity change
// Feel free to simulate all 104857600000000000000000000 possibilities!
if (!player.pointThreeEstimator.canPredictNextVerticalMovement()) {
minVector.setY(minVector.getY() - player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_GRAVITY).get());
minVector.setY(minVector.getY() - player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_GRAVITY));
}

// Hidden slime block bounces by missing idle tick and 0.03
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Vector getElytraMovement(GrimPlayer player, Vector vector, Vector
// So we actually use the player's actual movement to get the gravity/slow falling status
// However, this is wrong with elytra movement because players can control vertical movement after gravity is calculated
// Yeah, slow falling needs a refactor in grim.
double recalculatedGravity = player.compensatedEntities.getSelf().getAttribute(Attributes.GENERIC_GRAVITY).get();
double recalculatedGravity = player.compensatedEntities.getSelf().getAttributeValue(Attributes.GENERIC_GRAVITY);
if (player.clientVelocity.getY() <= 0 && player.compensatedEntities.getSlowFallingAmplifier() != null) {
recalculatedGravity = player.getClientVersion().isOlderThan(ClientVersion.V_1_20_5) ? 0.01 : Math.min(recalculatedGravity, 0.01);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static Set<VectorData> handleJumps(GrimPlayer player, Set<VectorData> pos
//
// There's a float/double error causing 1e-8 imprecision if anyone wants to debug it
if (player.vehicleData.horseJump > 0.0F && !player.vehicleData.horseJumping && player.lastOnGround) {
double d0 = horse.getAttribute(Attributes.GENERIC_JUMP_STRENGTH).get() * player.vehicleData.horseJump * JumpPower.getPlayerJumpFactor(player);
double d0 = horse.getAttributeValue(Attributes.GENERIC_JUMP_STRENGTH) * player.vehicleData.horseJump * JumpPower.getPlayerJumpFactor(player);
double d1;

// This doesn't even work because vehicle jump boost has (likely) been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public void set(Vector3i position, WrappedBlockState state) {
for (PacketEntity entity : player.compensatedEntities.entityMap.values()) {
SimpleCollisionBox interpBox = entity.getPossibleCollisionBoxes();

final double scale = entity.getAttribute(Attributes.GENERIC_SCALE).get();
final double scale = entity.getAttributeValue(Attributes.GENERIC_SCALE);
double width = BoundingBoxSize.getWidth(player, entity) * scale;
double height = BoundingBoxSize.getHeight(player, entity) * scale;
double interpWidth = Math.max(interpBox.maxX - interpBox.minX, interpBox.maxZ - interpBox.minZ);
Expand Down Expand Up @@ -657,7 +657,7 @@ public Vector getClickedLocation() {
SimpleCollisionBox box = new SimpleCollisionBox(getPlacedAgainstBlockLocation());
Vector look = ReachUtils.getLook(player, player.xRot, player.yRot);

final double distance = player.compensatedEntities.getSelf().getAttribute(Attributes.PLAYER_BLOCK_INTERACTION_RANGE).get() + 3;
final double distance = player.compensatedEntities.getSelf().getAttributeValue(Attributes.PLAYER_BLOCK_INTERACTION_RANGE) + 3;
Vector eyePos = new Vector(player.x, player.y + player.getEyeHeight(), player.z);
Vector endReachPos = eyePos.clone().add(new Vector(look.getX() * distance, look.getY() * distance, look.getZ() * distance));
Vector intercept = ReachUtils.calculateIntercept(box, eyePos, endReachPos).getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand All @@ -29,8 +28,8 @@ public final class ValuedAttribute {

// BiFunction of <Old, New, Output>
// This allows us to rewrite the value based on client & server version
private BiFunction<Double, Double, Double> rewriteFunction;
private Function<Double, Double> getRewriteFunction;
private BiFunction<Double, Double, Double> setRewriter;
private Function<Double, Double> getRewriter;

private ValuedAttribute(Attribute attribute, double defaultValue, double min, double max) {
if (defaultValue < min || defaultValue > max) {
Expand All @@ -42,15 +41,15 @@ private ValuedAttribute(Attribute attribute, double defaultValue, double min, do
this.value = defaultValue;
this.min = min;
this.max = max;
this.getRewriteFunction = DEFAULT_GET_REWRITE;
this.getRewriter = DEFAULT_GET_REWRITE;
}

public static ValuedAttribute ranged(Attribute attribute, double defaultValue, double min, double max) {
return new ValuedAttribute(attribute, defaultValue, min, max);
}

public ValuedAttribute withRewriter(BiFunction<Double, Double, Double> rewriteFunction) {
this.rewriteFunction = rewriteFunction;
public ValuedAttribute withSetRewriter(BiFunction<Double, Double, Double> rewriteFunction) {
this.setRewriter = rewriteFunction;
return this;
}

Expand All @@ -60,8 +59,8 @@ public ValuedAttribute withRewriter(BiFunction<Double, Double, Double> rewriteFu
* @param requiredVersion the required version for the attribute
* @return this instance for chaining
*/
public ValuedAttribute versionedRewriter(GrimPlayer player, ClientVersion requiredVersion) {
withRewriter((oldValue, newValue) -> {
public ValuedAttribute requiredVersion(GrimPlayer player, ClientVersion requiredVersion) {
withSetRewriter((oldValue, newValue) -> {
if (player.getClientVersion().isOlderThan(requiredVersion)) {
return oldValue;
}
Expand All @@ -71,7 +70,7 @@ public ValuedAttribute versionedRewriter(GrimPlayer player, ClientVersion requir
}

public ValuedAttribute withGetRewriter(Function<Double, Double> getRewriteFunction) {
this.getRewriteFunction = getRewriteFunction;
this.getRewriter = getRewriteFunction;
return this;
}

Expand All @@ -84,7 +83,7 @@ public void reset() {
}

public double get() {
return getRewriteFunction.apply(this.value);
return getRewriter.apply(this.value);
}

public void override(double value) {
Expand Down Expand Up @@ -124,8 +123,8 @@ public double with(WrapperPlayServerUpdateAttributes.Property property) {
}

double newValue = GrimMath.clampFloat((float) d1, (float) min, (float) max);
if (rewriteFunction != null) {
newValue = rewriteFunction.apply(this.value, newValue);
if (setRewriter != null) {
newValue = setRewriter.apply(this.value, newValue);
}

if (newValue < min || newValue > max) {
Expand Down
Loading

0 comments on commit 796c37d

Please sign in to comment.