Skip to content

Commit

Permalink
Fix freezing not being applied correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 3, 2024
1 parent 141b21b commit d48c79b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
78 changes: 78 additions & 0 deletions common/src/main/java/toughasnails/mixin/MixinLivingEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright 2023, the Glitchfiend Team.
* All rights reserved.
******************************************************************************/
package toughasnails.mixin;

import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Attackable;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import toughasnails.api.potion.TANEffects;
import toughasnails.api.temperature.ITemperature;
import toughasnails.api.temperature.TemperatureHelper;
import toughasnails.api.temperature.TemperatureLevel;
import toughasnails.core.ToughAsNails;

@Mixin(LivingEntity.class)
public abstract class MixinLivingEntity extends Entity implements Attackable
{
public MixinLivingEntity(EntityType<?> $$0, Level $$1) {
super($$0, $$1);
}

@Redirect(method="aiStep", at=@At(value="INVOKE", target="Lnet/minecraft/world/entity/LivingEntity;setTicksFrozen(I)V"))
public void onAiStep_setTicksFrozen(LivingEntity instance, int ticks)
{
if (!((Object)this instanceof Player))
{
this.setTicksFrozen(ticks);
}
}

@Inject(method="aiStep", at=@At(value="INVOKE", target="Lnet/minecraft/world/entity/LivingEntity;getTicksFrozen()I"))
public void onAiStep_getTicksFrozen(CallbackInfo ci)
{
// Only apply to players
if (!((Object)this instanceof Player))
{
return;
}

Player player = (Player)(Object)this;
ITemperature data = TemperatureHelper.getTemperatureData(player);
int prevTicksFrozen = player.getTicksFrozen();

if (!player.hasEffect(TANEffects.ICE_RESISTANCE))
{
if (data.getLevel() == TemperatureLevel.ICY && data.getExtremityDelayTicks() == 0)
{
// Add 2 to the ticksRequiredToFreeze to cause damage
player.setTicksFrozen(Math.min(player.getTicksRequiredToFreeze() + 2, player.getTicksFrozen() + 2));
}
else if (this.isInPowderSnow && this.canFreeze())
{
this.setTicksFrozen(Math.min(this.getTicksRequiredToFreeze(), player.getTicksFrozen() + 1));
}
}
else
{
// Set frozen ticks to 0 if ice resistance is active
player.setTicksFrozen(0);
}

// If the ticksFrozen hasn't changed, do melting
if (prevTicksFrozen == player.getTicksFrozen())
{
this.setTicksFrozen(Math.max(0, player.getTicksFrozen() - 2));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,10 @@ public static void onPlayerTick(Player player)
// Don't perform extremity effects in creative or spectator modes
if (!player.isCreative() && !player.isSpectator())
{
// Freeze the player if they're icy
if (!player.hasEffect(TANEffects.ICE_RESISTANCE) && data.getLevel() == TemperatureLevel.ICY && data.getExtremityDelayTicks() == 0)
{
int frozenTicks = player.getTicksFrozen();
int ticksToFreeze = player.getTicksRequiredToFreeze() + 2; // Add 2 to cause damage

if (frozenTicks < ticksToFreeze)
player.setTicksFrozen(Math.min(ticksToFreeze, player.getTicksFrozen() + 2));
}

// Increase hyperthermia ticks if hot
if (!player.hasEffect(MobEffects.FIRE_RESISTANCE) && data.getLevel() == TemperatureLevel.HOT && data.getExtremityDelayTicks() == 0)
{
data.setHyperthermiaTicks(Math.min(ticksToHyperthermia, hyperthermicTicks + 1));

if (player.getTicksFrozen() > 0)
player.setTicksFrozen(Math.max(0, player.getTicksFrozen() - 2));
}
else data.setHyperthermiaTicks(Math.max(0, hyperthermicTicks - 2));
}
Expand All @@ -130,12 +117,6 @@ public static void onPlayerTick(Player player)
data.setHyperthermiaTicks(Math.max(0, hyperthermicTicks - 2));
}

// Reset frozen ticks with ice resistance. This is mainly to avoid the effects of powdered snow.
if (player.hasEffect(TANEffects.ICE_RESISTANCE) && player.getTicksFrozen() > 0)
{
player.setTicksFrozen(0);
}

removeHeatExhaustion(player);
tryAddHeatExhaustion(player);

Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/toughasnails.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"MixinFoodData",
"MixinItem",
"MixinLivingEntity",
"MixinPlayer",
"MixinServerLevel",
"MixinServerPlayer"
Expand Down

0 comments on commit d48c79b

Please sign in to comment.