Skip to content

Commit

Permalink
Use setAttribute method
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Aug 12, 2024
1 parent 00e0b94 commit 1a9612a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,15 @@ public Optional<ValuedAttribute> getAttribute(Attribute attribute) {
return Optional.ofNullable(attributeMap.get(attribute));
}

public void setAttribute(Attribute attribute, double value) {
ValuedAttribute property = getAttribute(attribute).orElse(null);
if (property == null) throw new IllegalArgumentException("Cannot set attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
property.override(value);
}

public double getAttributeValue(Attribute attribute) {
return getAttribute(attribute).map(ValuedAttribute::get)
.orElseThrow(() -> new IllegalArgumentException("No such attribute exists on entity " + getType().getName().toString() + "!"));
.orElseThrow(() -> new IllegalArgumentException("Cannot get attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!"));
}

public void resetAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class PacketEntityCamel extends PacketEntityHorse {
public PacketEntityCamel(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, float xRot) {
super(player, uuid, type, x, y, z, xRot);

getAttribute(Attributes.GENERIC_JUMP_STRENGTH).get().override(0.42f);
getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get().override(0.09f);
getAttribute(Attributes.GENERIC_STEP_HEIGHT).get().override(1.5f);
setAttribute(Attributes.GENERIC_JUMP_STRENGTH, 0.42f);
setAttribute(Attributes.GENERIC_MOVEMENT_SPEED, 0.09f);
setAttribute(Attributes.GENERIC_STEP_HEIGHT, 1.5f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class PacketEntityHorse extends PacketEntityTrackXRot {

public PacketEntityHorse(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z, float xRot) {
super(player, uuid, type, x, y, z, xRot);
getAttribute(Attributes.GENERIC_STEP_HEIGHT).get().override(1.0f);
setAttribute(Attributes.GENERIC_STEP_HEIGHT, 1.0f);

final boolean preAttribute = player.getClientVersion().isOlderThan(ClientVersion.V_1_20_5);
trackAttribute(ValuedAttribute.ranged(Attributes.GENERIC_JUMP_STRENGTH, 0.7, 0, preAttribute ? 2 : 32));
trackAttribute(ValuedAttribute.ranged(Attributes.GENERIC_MOVEMENT_SPEED, 0.225f, 0, 1024));

if (EntityTypes.isTypeInstanceOf(type, EntityTypes.CHESTED_HORSE)) {
getAttribute(Attributes.GENERIC_JUMP_STRENGTH).get().override(0.5);
getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get().override(0.175f);
setAttribute(Attributes.GENERIC_JUMP_STRENGTH, 0.5);
setAttribute(Attributes.GENERIC_MOVEMENT_SPEED, 0.175f);
}

if (type == EntityTypes.ZOMBIE_HORSE || type == EntityTypes.SKELETON_HORSE) {
getAttribute(Attributes.GENERIC_MOVEMENT_SPEED).get().override(0.2f);
setAttribute(Attributes.GENERIC_MOVEMENT_SPEED, 0.2f);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PacketEntityRideable extends PacketEntity {

public PacketEntityRideable(GrimPlayer player, UUID uuid, EntityType type, double x, double y, double z) {
super(player, uuid, type, x, y, z);
getAttribute(Attributes.GENERIC_STEP_HEIGHT).get().override(1.0f);
setAttribute(Attributes.GENERIC_STEP_HEIGHT, 1.0f);
trackAttribute(ValuedAttribute.ranged(Attributes.GENERIC_MOVEMENT_SPEED, 0.1f, 0, 1024));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PacketEntitySelf(GrimPlayer player, PacketEntitySelf old) {
protected void initAttributes(GrimPlayer player) {
super.initAttributes(player);
if (player.getClientVersion().isOlderThan(ClientVersion.V_1_8)) {
getAttribute(Attributes.GENERIC_STEP_HEIGHT).get().override(0.5f);
setAttribute(Attributes.GENERIC_STEP_HEIGHT, 0.5f);
}

final ValuedAttribute movementSpeed = ValuedAttribute.ranged(Attributes.GENERIC_MOVEMENT_SPEED, 0.1f, 0, 1024);
Expand Down

0 comments on commit 1a9612a

Please sign in to comment.