|
| 1 | +package turniplabs.halplibe.mixin.deathcause; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.expression.Definition; |
| 4 | +import com.llamalad7.mixinextras.expression.Expression; |
| 5 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 6 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 7 | + |
| 8 | +import net.minecraft.core.Global; |
| 9 | +import net.minecraft.core.entity.Mob; |
| 10 | +import net.minecraft.core.net.command.TextFormatting; |
| 11 | +import net.minecraft.core.world.World; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.spongepowered.asm.mixin.Mixin; |
| 14 | +import org.spongepowered.asm.mixin.Unique; |
| 15 | +import org.spongepowered.asm.mixin.injection.At; |
| 16 | +import turniplabs.halplibe.helper.EnvironmentHelper; |
| 17 | +import turniplabs.halplibe.helper.network.NetworkHandler; |
| 18 | +import turniplabs.halplibe.util.deathcause.DeathCause; |
| 19 | +import turniplabs.halplibe.util.deathcause.DeathCauseMixinInterface; |
| 20 | +import turniplabs.halplibe.util.deathcause.DeathCauseNetworkMessage; |
| 21 | + |
| 22 | +@Mixin(Mob.class) |
| 23 | +public class MobMixin implements DeathCauseMixinInterface { |
| 24 | + |
| 25 | + @Unique |
| 26 | + @Nullable DeathCause deathCause = null; |
| 27 | + |
| 28 | + @Definition(id = "world", field = "Lnet/minecraft/core/entity/Mob;world:Lnet/minecraft/core/world/World;") |
| 29 | + @Definition(id = "sendGlobalMessageTranslated", method = "Lnet/minecraft/core/world/World;sendGlobalMessageTranslated(Lnet/minecraft/core/net/command/TextFormatting$Base;Ljava/lang/String;[Ljava/lang/String;)V") |
| 30 | + @Expression("this.world.sendGlobalMessageTranslated(?, ?, ?)") |
| 31 | + @WrapOperation(method = "onDeath", at = @At("MIXINEXTRAS:EXPRESSION")) |
| 32 | + public void hijackDeathMessage(World world, TextFormatting.Base format, String key, String[] args, Operation<Void> original) { |
| 33 | + if (this.deathCause == null) { |
| 34 | + original.call(world, format, key, args); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (!EnvironmentHelper.isClientWorld()) { |
| 39 | + NetworkHandler.sendToAllPlayers(new DeathCauseNetworkMessage(this.deathCause)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void halplibe$setDeathCause(DeathCause deathCause) { |
| 45 | + this.deathCause = deathCause; |
| 46 | + } |
| 47 | +} |
0 commit comments