diff options
Diffstat (limited to 'src/main/java/ganarchy/chewstuff/mixin')
-rw-r--r-- | src/main/java/ganarchy/chewstuff/mixin/DesyncFix.java | 30 | ||||
-rw-r--r-- | src/main/java/ganarchy/chewstuff/mixin/PotionMix.java | 67 |
2 files changed, 97 insertions, 0 deletions
diff --git a/src/main/java/ganarchy/chewstuff/mixin/DesyncFix.java b/src/main/java/ganarchy/chewstuff/mixin/DesyncFix.java new file mode 100644 index 0000000..7a62a41 --- /dev/null +++ b/src/main/java/ganarchy/chewstuff/mixin/DesyncFix.java @@ -0,0 +1,30 @@ +package ganarchy.chewstuff.mixin; + +import ganarchy.chewstuff.ChewComponents; +import net.minecraft.server.network.ServerPlayerEntity; +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.callback.CallbackInfo; + +/** + * The mixin for server player entities. + */ +@Mixin(ServerPlayerEntity.class) +public class DesyncFix { + /** + * Checks and corrects for desyncs. + */ + @Inject( + at = @At("HEAD"), + method = + "onStatusEffectUpgraded(Lnet/minecraft/entity/effect/StatusEffectInstance;ZLnet/minecraft/entity/Entity;)V" + ) + private void chewstuff_onDesync(CallbackInfo cbinfo) { + var self = (ServerPlayerEntity) (Object) this; + var maybeInfo = ChewComponents.CHEW.maybeGet(self); + maybeInfo.ifPresent(info -> { + info.sendUpdate = true; + }); + } +} diff --git a/src/main/java/ganarchy/chewstuff/mixin/PotionMix.java b/src/main/java/ganarchy/chewstuff/mixin/PotionMix.java new file mode 100644 index 0000000..5ed97ab --- /dev/null +++ b/src/main/java/ganarchy/chewstuff/mixin/PotionMix.java @@ -0,0 +1,67 @@ +package ganarchy.chewstuff.mixin; + +import ganarchy.chewstuff.ChewComponents; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.effect.StatusEffectInstance; +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.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +/** + * The mixin for status effect instances. + */ +@Mixin(StatusEffectInstance.class) +public class PotionMix { + /** + * Checks the apply effect and cancels it when we're force-ticking it. + */ + @Inject( + at = @At("HEAD"), + method = "applyUpdateEffect(Lnet/minecraft/entity/LivingEntity;)V", + cancellable = true + ) + private void chewstuff_checkApplyEffect( + LivingEntity entity, CallbackInfo cbinfo + ) { + StatusEffectInstance self = (StatusEffectInstance) (Object) this; + var maybeInfo = ChewComponents.CHEW.maybeGet(entity); + maybeInfo.ifPresent(info -> { + var effect = self.getEffectType(); + if (info.effects.getOrDefault(effect, 0) <= 0) { + return; + } + if (info.applied.contains(effect)) { + info.applied.remove(effect); + cbinfo.cancel(); + } else { + info.applied.add(effect); + } + }); + } + + /** + * Checks for effect ticks. + */ + @Inject( + at = @At("HEAD"), + method = + "update(Lnet/minecraft/entity/LivingEntity;Ljava/lang/Runnable;)Z" + ) + private void chewstuff_checkUpdate( + LivingEntity entity, + Runnable runnable, + CallbackInfoReturnable<Boolean> cbinfo + ) { + StatusEffectInstance self = (StatusEffectInstance) (Object) this; + var maybeInfo = ChewComponents.CHEW.maybeGet(entity); + maybeInfo.ifPresent(info -> { + var effect = self.getEffectType(); + if (info.effects.getOrDefault(effect, 0) <= 0) { + return; + } + info.effects.computeIfPresent(effect, (k, v) -> v - 1); + }); + } +} |