This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
Twine/src/main/java/com/thebrokenrail/twine/mixin/MixinMobEntity.java

90 lines
4.5 KiB
Java
Raw Normal View History

2020-06-14 03:04:21 +00:00
package com.thebrokenrail.twine.mixin;
import com.thebrokenrail.twine.component.StageDataComponent;
import com.thebrokenrail.twine.entity.ExplodeArtificialBlockGoal;
import com.thebrokenrail.twine.entity.FleeEndRodGoal;
2020-06-14 03:04:21 +00:00
import com.thebrokenrail.twine.entity.FollowPassiveEntityGoal;
import com.thebrokenrail.twine.entity.StandOnGlowingObsidianGoal;
2020-06-15 21:31:15 +00:00
import com.thebrokenrail.twine.util.StageUtil;
2020-06-14 03:04:21 +00:00
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.goal.FollowTargetGoal;
import net.minecraft.entity.ai.goal.GoalSelector;
2020-06-16 00:30:22 +00:00
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.MobEntityWithAi;
import net.minecraft.entity.mob.Monster;
2020-06-14 03:04:21 +00:00
import net.minecraft.entity.passive.IronGolemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
2020-06-15 20:29:28 +00:00
import net.minecraft.util.math.BlockPos;
2020-06-14 03:04:21 +00:00
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
2020-06-14 03:04:21 +00:00
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2020-06-14 03:04:21 +00:00
import java.util.Random;
2020-06-14 03:04:21 +00:00
@SuppressWarnings("ConstantConditions")
@Mixin(MobEntity.class)
public class MixinMobEntity {
@Shadow
@Final
protected GoalSelector targetSelector;
@Shadow
@Final
protected GoalSelector goalSelector;
@Inject(at = @At("RETURN"), method = "<init>")
public void initGoals(EntityType<? extends MobEntity> entityType, World world, CallbackInfo info) {
if (world != null && !world.isClient()) {
if (this instanceof Monster) {
if ((Object) this instanceof MobEntityWithAi) {
goalSelector.add(1, new StandOnGlowingObsidianGoal((MobEntityWithAi) (Object) this));
goalSelector.add(3, new FleeEndRodGoal((MobEntityWithAi) (Object) this));
if ((Object) this instanceof CreeperEntity) {
goalSelector.add(3, new ExplodeArtificialBlockGoal((MobEntityWithAi) (Object) this));
}
2020-06-14 03:04:21 +00:00
}
targetSelector.add(2, new FollowPassiveEntityGoal((MobEntity) (Object) this));
} else if ((Object) this instanceof IronGolemEntity) {
targetSelector.add(3, new FollowTargetGoal<>((IronGolemEntity) (Object) this, PlayerEntity.class, 10, true, false, entity -> {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((IronGolemEntity) (Object) this).getEntityWorld());
2020-06-15 20:29:28 +00:00
BlockPos blockPos = ((IronGolemEntity) (Object) this).getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) (((IronGolemEntity) (Object) this).getEntityWorld()), blockPos);
2020-06-14 03:04:21 +00:00
2020-06-15 21:31:15 +00:00
return stage >= StageUtil.IRON_GOLEMS_TARGET_PLAYERS && !((IronGolemEntity) (Object) this).isPlayerCreated();
2020-06-14 03:04:21 +00:00
}));
}
}
}
@Inject(at = @At("HEAD"), method = "isInDaylight", cancellable = true)
public void isInDaylight(CallbackInfoReturnable<Boolean> info) {
2020-06-15 20:29:28 +00:00
if (!((MobEntity) (Object) this).getEntityWorld().isClient()) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((MobEntity) (Object) this).getEntityWorld());
BlockPos blockPos = ((MobEntity) (Object) this).getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) ((MobEntity) (Object) this).getEntityWorld(), blockPos);
2020-06-15 21:31:15 +00:00
if (stage >= StageUtil.MOBS_NO_LONGER_BURN_IN_SUNLIGHT) {
2020-06-15 20:29:28 +00:00
info.setReturnValue(false);
}
}
}
@Redirect(at = @At(value = "INVOKE", target = "Ljava/util/Random;nextFloat()F", ordinal = 0), method = "initEquipment", allow = 1)
public float initEquipment(Random random) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((MobEntity) (Object) this).getEntityWorld());
BlockPos blockPos = ((MobEntity) (Object) this).getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) ((MobEntity) (Object) this).getEntityWorld(), blockPos);
2020-06-15 21:31:15 +00:00
if (stage >= StageUtil.MOBS_GUARANTEED_ARMOR) {
return -1f;
} else {
return random.nextFloat();
}
}
2020-06-14 03:04:21 +00:00
}