Skip to content

Commit

Permalink
Potion refactor port, fixes #1654
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Aug 17, 2024
1 parent e62323b commit 2cc3212
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void onPacketReceive(PacketReceiveEvent event) {
public void onPredictionComplete(final PredictionComplete predictionComplete) {
if (!predictionComplete.isChecked()) return;

if (player.compensatedEntities.getSelf().potionsMap != null && player.compensatedEntities.getSelf().potionsMap.containsKey(BLINDNESS)) {
if (player.compensatedEntities.getSelf().hasPotionEffect(BLINDNESS)) {
if (player.isSprinting && !startedSprintingBeforeBlind) {
if (flagWithSetback()) alert("");
} else reward();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Setter;
import org.bukkit.util.Vector;

import java.util.OptionalInt;
import java.util.Set;

/**
Expand Down Expand Up @@ -436,9 +437,10 @@ public double getAdditionalVerticalUncertainty(VectorData vector) {
}

private double iterateGravity(GrimPlayer player, double y) {
if (player.compensatedEntities.getLevitationAmplifier() != null) {
final OptionalInt levitation = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.LEVITATION);
if (levitation.isPresent()) {
// This supports both positive and negative levitation
y += (0.05 * (player.compensatedEntities.getLevitationAmplifier() + 1) - y * 0.2);
y += (0.05 * (levitation.getAsInt() + 1) - y * 0.2);
} else if (player.hasGravity) {
// Simulate gravity
y -= player.gravity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.potion.PotionTypes;
import com.github.retrooper.packetevents.protocol.world.states.defaulttags.BlockTags;
import com.github.retrooper.packetevents.protocol.world.states.type.StateType;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
Expand Down Expand Up @@ -330,7 +331,7 @@ public void livingEntityTravel() {
: player.compensatedEntities.getSelf().getRiding().getAttributeValue(Attributes.GENERIC_GRAVITY);

boolean isFalling = player.actualMovement.getY() <= 0.0;
if (isFalling && player.compensatedEntities.getSlowFallingAmplifier() != null) {
if (isFalling && player.compensatedEntities.getSlowFallingAmplifier().isPresent()) {
playerGravity = player.getClientVersion().isOlderThan(ClientVersion.V_1_20_5) ? 0.01 : Math.min(playerGravity, 0.01);
// Set fall distance to 0 if the player has slow falling
player.fallDistance = 0;
Expand Down Expand Up @@ -365,7 +366,7 @@ public void livingEntityTravel() {
swimSpeed += (player.speed - swimSpeed) * player.depthStriderLevel / divisor;
}

if (player.compensatedEntities.getDolphinsGraceAmplifier() != null) {
if (player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.DOLPHINS_GRACE).isPresent()) {
swimFriction = 0.96F;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static Vector getElytraMovement(GrimPlayer player, Vector vector, Vector
// 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().getAttributeValue(Attributes.GENERIC_GRAVITY);
if (player.clientVelocity.getY() <= 0 && player.compensatedEntities.getSlowFallingAmplifier() != null) {
if (player.clientVelocity.getY() <= 0 && player.compensatedEntities.getSlowFallingAmplifier().isPresent()) {
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 @@ -9,18 +9,21 @@
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.potion.PotionTypes;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
import org.bukkit.util.Vector;

import java.util.HashSet;
import java.util.OptionalInt;
import java.util.Set;

public class PredictionEngineNormal extends PredictionEngine {

public static void staticVectorEndOfTick(GrimPlayer player, Vector vector) {
double adjustedY = vector.getY();
if (player.compensatedEntities.getLevitationAmplifier() != null) {
adjustedY += (0.05 * (player.compensatedEntities.getLevitationAmplifier() + 1) - vector.getY()) * 0.2;
final OptionalInt levitation = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.LEVITATION);
if (levitation.isPresent()) {
adjustedY += (0.05 * (levitation.getAsInt() + 1) - vector.getY()) * 0.2;
// Reset fall distance with levitation
player.fallDistance = 0;
} else if (player.hasGravity) {
Expand All @@ -43,7 +46,8 @@ public void addJumpsToPossibilities(GrimPlayer player, Set<VectorData> existingV
// If the player didn't try to jump
// And 0.03 didn't affect onGround status
// The player cannot jump
if (((player.compensatedEntities.getJumpAmplifier() == null || player.compensatedEntities.getJumpAmplifier() >= 0) && player.onGround) || !player.lastOnGround)
final OptionalInt jumpBoost = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.JUMP_BOOST);
if (((!jumpBoost.isPresent() || jumpBoost.getAsInt() >= 0) && player.onGround) || !player.lastOnGround)
return;

JumpPower.jumpFromGround(player, jump);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import ac.grim.grimac.utils.data.packetentity.PacketEntityHorse;
import ac.grim.grimac.utils.nmsutil.JumpPower;
import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.protocol.potion.PotionTypes;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.Set;

public class PredictionEngineRideableUtils {
Expand Down Expand Up @@ -39,8 +41,9 @@ public static Set<VectorData> handleJumps(GrimPlayer player, Set<VectorData> pos
// broken ever since vehicle control became client sided
//
// But plugins can still send this, so support it anyways
if (player.compensatedEntities.getJumpAmplifier() != null) {
d1 = d0 + ((player.compensatedEntities.getJumpAmplifier() + 1) * 0.1F);
final OptionalInt jumpBoost = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.JUMP_BOOST);
if (jumpBoost.isPresent()) {
d1 = d0 + ((jumpBoost.getAsInt() + 1) * 0.1F);
} else {
d1 = d0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.OptionalInt;
import java.util.UUID;
import java.util.Map;
import java.util.Optional;
Expand All @@ -52,7 +53,7 @@ public class PacketEntity extends TypedPacketEntity {
private ReachInterpolationData oldPacketLocation;
private ReachInterpolationData newPacketLocation;

public HashMap<PotionType, Integer> potionsMap = null;
private Map<PotionType, Integer> potionsMap = null;
protected final Map<Attribute, ValuedAttribute> attributeMap = new IdentityHashMap<>();

public PacketEntity(GrimPlayer player, EntityType type) {
Expand Down Expand Up @@ -194,6 +195,15 @@ public PacketEntity getRiding() {
return riding;
}

public OptionalInt getPotionEffectLevel(PotionType effect) {
final Integer amplifier = potionsMap == null ? null : potionsMap.get(effect);
return amplifier == null ? OptionalInt.empty() : OptionalInt.of(amplifier);
}

public boolean hasPotionEffect(PotionType effect) {
return potionsMap != null && potionsMap.containsKey(effect);
}

public void addPotionEffect(PotionType effect, int amplifier) {
if (potionsMap == null) {
potionsMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public boolean inVehicle() {

@Override
public void addPotionEffect(PotionType effect, int amplifier) {
if (effect == PotionTypes.BLINDNESS && (potionsMap == null || !potionsMap.containsKey(PotionTypes.BLINDNESS))) {
if (effect == PotionTypes.BLINDNESS && !hasPotionEffect(PotionTypes.BLINDNESS)) {
player.checkManager.getPostPredictionCheck(NoSlowE.class).startedSprintingBeforeBlind = player.isSprinting;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,20 @@ public void removeEntity(int entityID) {
}
}

public Integer getJumpAmplifier() {
return getPotionLevelForPlayer(PotionTypes.JUMP_BOOST);
public OptionalInt getSlowFallingAmplifier() {
return player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_12_2) ? OptionalInt.empty() : getPotionLevelForPlayer(PotionTypes.SLOW_FALLING);
}

public Integer getLevitationAmplifier() {
return getPotionLevelForPlayer(PotionTypes.LEVITATION);
public OptionalInt getPotionLevelForPlayer(PotionType type) {
return getEntityInControl().getPotionEffectLevel(type);
}

public Integer getSlowFallingAmplifier() {
return player.getClientVersion().isOlderThanOrEquals(ClientVersion.V_1_12_2) ? null : getPotionLevelForPlayer(PotionTypes.SLOW_FALLING);
public boolean hasPotionEffect(PotionType type) {
return getEntityInControl().hasPotionEffect(type);
}

public Integer getDolphinsGraceAmplifier() {
return getPotionLevelForPlayer(PotionTypes.DOLPHINS_GRACE);
}

public Integer getPotionLevelForPlayer(PotionType type) {
PacketEntity desiredEntity = playerEntity.getRiding() != null ? playerEntity.getRiding() : playerEntity;

HashMap<PotionType, Integer> effects = desiredEntity.potionsMap;
if (effects == null) return null;

return effects.get(type);
public PacketEntity getEntityInControl() {
return playerEntity.getRiding() != null ? playerEntity.getRiding() : playerEntity;
}

public void updateAttributes(int entityID, List<WrapperPlayServerUpdateAttributes.Property> objects) {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/ac/grim/grimac/utils/nmsutil/BlockBreakSpeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.protocol.component.ComponentTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentTypes;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
Expand All @@ -18,7 +17,8 @@
import com.github.retrooper.packetevents.protocol.world.states.defaulttags.BlockTags;
import com.github.retrooper.packetevents.protocol.world.states.type.StateTypes;
import com.github.retrooper.packetevents.util.Vector3i;
import org.bukkit.Bukkit;

import java.util.OptionalInt;

public class BlockBreakSpeed {
public static double getBlockDamage(GrimPlayer player, Vector3i position) {
Expand Down Expand Up @@ -131,29 +131,29 @@ public static double getBlockDamage(GrimPlayer player, Vector3i position) {
}
}

Integer digSpeed = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.HASTE);
Integer conduit = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.CONDUIT_POWER);
OptionalInt digSpeed = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.HASTE);
OptionalInt conduit = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.CONDUIT_POWER);

if (digSpeed != null || conduit != null) {
int hasteLevel = Math.max(digSpeed == null ? 0 : digSpeed, conduit == null ? 0 : conduit);
speedMultiplier *= 1.0F + (0.2F * (hasteLevel + 1));
if (digSpeed.isPresent() || conduit.isPresent()) {
int hasteLevel = Math.max(!digSpeed.isPresent() ? 0 : digSpeed.getAsInt(), !conduit.isPresent() ? 0 : conduit.getAsInt());
speedMultiplier *= (float) (1 + (0.2 * (hasteLevel + 1)));
}

Integer miningFatigue = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.MINING_FATIGUE);
OptionalInt miningFatigue = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.MINING_FATIGUE);

if (miningFatigue != null) {
switch (miningFatigue) {
if (miningFatigue.isPresent()) {
switch (miningFatigue.getAsInt()) {
case 0:
speedMultiplier *= 0.3F;
speedMultiplier *= 0.3f;
break;
case 1:
speedMultiplier *= 0.09F;
speedMultiplier *= 0.09f;
break;
case 2:
speedMultiplier *= 0.0027F;
speedMultiplier *= 0.0027f;
break;
default:
speedMultiplier *= 0.00081F;
speedMultiplier *= 0.00081f;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ac/grim/grimac/utils/nmsutil/Collisions.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public static void handleInsideBlocks(GrimPlayer player) {
StateType blockType = block.getType();

if (blockType == StateTypes.COBWEB) {
if (player.compensatedEntities.getSelf().potionsMap.containsKey(PotionTypes.WEAVING)) {
if (player.compensatedEntities.hasPotionEffect(PotionTypes.WEAVING)) {
player.stuckSpeedMultiplier = new Vector(0.5, 0.25, 0.5);
} else {
player.stuckSpeedMultiplier = new Vector(0.25, 0.05000000074505806, 0.25);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ac/grim/grimac/utils/nmsutil/JumpPower.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import ac.grim.grimac.player.GrimPlayer;
import com.github.retrooper.packetevents.protocol.attribute.Attributes;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.potion.PotionTypes;
import com.github.retrooper.packetevents.util.Vector3d;
import org.bukkit.util.Vector;

import java.util.OptionalInt;

public class JumpPower {
public static void jumpFromGround(GrimPlayer player, Vector vector) {
float jumpPower = getJumpPower(player);

if (player.compensatedEntities.getJumpAmplifier() != null) {
jumpPower += 0.1f * (player.compensatedEntities.getJumpAmplifier() + 1);
final OptionalInt jumpBoost = player.compensatedEntities.getPotionLevelForPlayer(PotionTypes.JUMP_BOOST);
if (jumpBoost.isPresent()) {
jumpPower += 0.1f * (jumpBoost.getAsInt() + 1);
}

if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_20_5) && jumpPower <= 1.0E-5F) return;
Expand Down

0 comments on commit 2cc3212

Please sign in to comment.