Skip to content

Commit

Permalink
fix: don't spam the server with Start Fall Flying packets
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Aug 22, 2024
1 parent 270afd3 commit 3ac1039
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
import ru.octol1ttle.flightassistant.DrawHelper;
import ru.octol1ttle.flightassistant.alerts.impl.AlertSoundData;
import ru.octol1ttle.flightassistant.alerts.api.BaseAlert;
import ru.octol1ttle.flightassistant.alerts.api.IECAMAlert;
import ru.octol1ttle.flightassistant.alerts.impl.AlertSoundData;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.config.IndicatorConfig;
Expand All @@ -18,7 +18,7 @@ public class ElytraHealthLowAlert extends BaseAlert implements IECAMAlert {

@Override
public boolean isTriggered() {
return data.elytraHealth != null && data.elytraHealth.getInUnits(IndicatorConfig.ElytraHealthDisplayUnits.PERCENTAGE) <= 5.0f;
return data.elytraData != null && data.elytraData.getHealth(IndicatorConfig.ElytraHealthDisplayUnits.PERCENTAGE) <= 5.0f;
}

@Override
Expand All @@ -31,4 +31,4 @@ public int render(TextRenderer textRenderer, DrawContext context, int x, int y,
return DrawHelper.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.elytra_health_low"), x, y,
FAConfig.indicator().warningColor, highlight && data.isFlying());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.octol1ttle.flightassistant.computers.impl;

import com.google.common.collect.Iterables;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -40,7 +39,7 @@ public class AirDataComputer implements ITickableComputer {
public float flightPitch;
public float flightYaw;
public float groundLevel;
public @Nullable ElytraHealth elytraHealth = null;
public @Nullable AirDataComputer.ElytraData elytraData = null;
public boolean isCurrentChunkLoaded;

public AirDataComputer(MinecraftClient mc) {
Expand All @@ -56,7 +55,7 @@ public void tick() {
groundLevel = computeGroundLevel();
flightPitch = computeFlightPitch(velocity, pitch());
flightYaw = computeFlightYaw(velocity, yaw());
elytraHealth = computeElytraHealth();
elytraData = computeElytraData();
}

public boolean canAutomationsActivate() {
Expand Down Expand Up @@ -113,10 +112,15 @@ private float computeFlightYaw(Vec3d velocity, float yaw) {
return validate(FAMathHelper.toDegrees(Math.atan2(-velocity.x, velocity.z)), 180.0f);
}

private ElytraHealth computeElytraHealth() {
for (ItemStack stack : Iterables.concat(player().getArmorItems(), player().getHandItems())) {
private ElytraData computeElytraData() {
for (ItemStack stack : player().getArmorItems()) {
if (Items.ELYTRA.equals(stack.getItem())) {
return new ElytraHealth(stack.copy());
return new ElytraData(stack.copy(), true);
}
}
for (ItemStack stack : player().getHandItems()) {
if (Items.ELYTRA.equals(stack.getItem())) {
return new ElytraData(stack.copy(), false);
}
}

Expand Down Expand Up @@ -242,34 +246,36 @@ public void reset() {
flightYaw = 0.0f;
roll = 0.0f;
groundLevel = 0;
elytraHealth = null;
elytraData = null;
isCurrentChunkLoaded = true;
}

public static class ElytraHealth {
public static class ElytraData {
private final ItemStack stack;
private final boolean canFallFly;

public ElytraHealth(ItemStack stack) {
public ElytraData(ItemStack stack, boolean canFallFly) {
if (!Items.ELYTRA.equals(stack.getItem())) {
throw new IllegalStateException("Attempted to initialize ElytraHealth, but the ItemStack does not contain an Elytra");
throw new IllegalStateException("Attempted to initialize ElytraData, but the ItemStack does not contain an Elytra");
}
this.stack = stack;
this.canFallFly = canFallFly;
}

public float getInUnits(IndicatorConfig.ElytraHealthDisplayUnits units) {
public float getHealth(IndicatorConfig.ElytraHealthDisplayUnits units) {
float remaining = (stack.getMaxDamage() - 1) - stack.getDamage();
return switch (units) {
case REMAINING_DURABILITY -> validate(remaining, 0.0f, stack.getMaxDamage());
case PERCENTAGE -> validate(remaining / stack.getMaxDamage() * 100.0f, 0.0f, 100.0f);
};
}

public Text format(IndicatorConfig.ElytraHealthDisplayUnits units) {
public Text formatHealth(IndicatorConfig.ElytraHealthDisplayUnits units) {
if (!stack.isDamageable()) {
return Text.translatable("short.flightassistant.infinite");
}

MutableText text = DrawHelper.asText("%s", MathHelper.ceil(getInUnits(units)));
MutableText text = DrawHelper.asText("%s", MathHelper.ceil(getHealth(units)));
if (units == IndicatorConfig.ElytraHealthDisplayUnits.PERCENTAGE) {
text.append("%");
}
Expand All @@ -278,7 +284,7 @@ public Text format(IndicatorConfig.ElytraHealthDisplayUnits units) {
}

public boolean isUsable() {
return stack.getMaxDamage() - stack.getDamage() > 1;
return canFallFly && stack.getMaxDamage() - stack.getDamage() > 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.octol1ttle.flightassistant.computers.impl.safety;

import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.MinecraftVersion;
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;
import ru.octol1ttle.flightassistant.MinecraftProtocolVersions;
Expand All @@ -10,15 +11,21 @@

public class ElytraStateController implements ITickableComputer {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private boolean syncedState;
private boolean changesPending;
private TriState syncedState;

@Override
public void tick() {
if (syncedState != data.isFlying() || data.player().isOnGround()) {
changesPending = false;
if (data.player().isOnGround()) {
syncedState = TriState.DEFAULT;
return;
}
if (syncedState != TriState.DEFAULT) {
if (syncedState.get() != data.isFlying()) {
syncedState = TriState.DEFAULT;
}
return;
}
if (!isAvailable() || changesPending || !data.canAutomationsActivate(false)) {
if (!isAvailable() || !data.canAutomationsActivate(false)) {
return;
}

Expand All @@ -28,7 +35,7 @@ public void tick() {
}

boolean flying = data.isFlying() || data.player().getAbilities().allowFlying;
boolean hasUsableElytra = data.elytraHealth != null && data.elytraHealth.isUsable();
boolean hasUsableElytra = data.elytraData != null && data.elytraData.isUsable();
boolean notLookingToClutch = data.pitch() > -70.0f;
boolean unsafeFallDistance = data.fallDistance() > 3.0f;
if (FAConfig.computer().openElytraAutomatically && unsafeFallDistance && !flying && hasUsableElytra && notLookingToClutch) {
Expand All @@ -38,9 +45,8 @@ public void tick() {
}

private void sendSwitchState() {
syncedState = data.isFlying();
syncedState = TriState.of(data.isFlying());
data.player().networkHandler.sendPacket(new ClientCommandC2SPacket(data.player(), ClientCommandC2SPacket.Mode.START_FALL_FLYING));
changesPending = true;
}

public static boolean isAvailable() {
Expand All @@ -54,7 +60,6 @@ public String getFaultTextBaseKey() {

@Override
public void reset() {
syncedState = false;
changesPending = false;
syncedState = TriState.DEFAULT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void render(DrawContext context, TextRenderer textRenderer) {
int x = dim.xMid;
int y = dim.bFrame;

if (FAConfig.indicator().showElytraHealth && data.elytraHealth != null) {
if (FAConfig.indicator().showElytraHealth && data.elytraData != null) {
Color color;
float percentage = data.elytraHealth.getInUnits(IndicatorConfig.ElytraHealthDisplayUnits.PERCENTAGE);
float percentage = data.elytraData.getHealth(IndicatorConfig.ElytraHealthDisplayUnits.PERCENTAGE);
if (percentage <= 5.0f) {
color = FAConfig.indicator().warningColor;
} else {
Expand All @@ -37,7 +37,7 @@ public void render(DrawContext context, TextRenderer textRenderer) {
DrawHelper.drawBorder(context, x - 3, y - 2, 30, color);
DrawHelper.drawText(textRenderer, context, Text.translatable("short.flightassistant.elytra"), x - 10, y, color);

DrawHelper.drawText(textRenderer, context, data.elytraHealth.format(FAConfig.indicator().elytraHealthUnits), x, y, color);
DrawHelper.drawText(textRenderer, context, data.elytraData.formatHealth(FAConfig.indicator().elytraHealthUnits), x, y, color);
}
}

Expand Down

0 comments on commit 3ac1039

Please sign in to comment.