Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360package net.codexarchonic.infinity.entity.custom;
import net.codexarchonic.infinity.block.custom.AntBlock;import net.codexarchonic.infinity.util.VersionMethods;import net.codexarchonic.infinity.util.core.NbtUtils;import net.codexarchonic.infinity.util.var.BishopBattle;import net.minecraft.core.BlockPos;import net.minecraft.core.Direction;import net.minecraft.nbt.CompoundTag;import net.minecraft.resources.ResourceKey;import net.minecraft.server.level.ServerLevel;import net.minecraft.sounds.SoundEvent;import net.minecraft.sounds.SoundEvents;import net.minecraft.tags.FluidTags;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.damagesource.DamageSource;import net.minecraft.world.entity.Entity;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.entity.Mob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.Goal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;import net.minecraft.world.entity.monster.Monster;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Block;import net.minecraft.world.level.block.state.BlockState;import net.minecraft.world.level.material.FluidState;//? if >1.21.4 {import net.minecraft.world.level.storage.ValueInput;import net.minecraft.world.level.storage.ValueOutput;//?}import net.minecraft.world.level.storage.loot.LootParams;import net.minecraft.world.level.storage.loot.LootTable;import net.minecraft.world.phys.Vec3;import net.minecraft.world.phys.shapes.CollisionContext;import net.minecraft.world.phys.shapes.VoxelShape;import org.jspecify.annotations.Nullable;
import java.util.Optional;import java.util.function.BiConsumer;import java.util.function.Function;
public class AntEntity extends AbstractChessFigure { @Nullable protected BlockPos lastChangedPos; private Direction direction; private boolean dropsLoot;
public AntEntity(EntityType<? extends AbstractChessFigure> entityType, Level world) { super(entityType, world); direction = Direction.EAST; dropsLoot = true; }
@Override public boolean isBlackOrWhite() { return true; } //? if <1.21.2 { /*@Override public boolean shouldDropLoot() { return dropsLoot; } *///?} else { @Override protected boolean dropFromLootTable(ServerLevel level, ResourceKey<LootTable> lootTable, Function<LootParams.Builder, LootParams> paramsBuilder, BiConsumer<ServerLevel, ItemStack> dropConsumer) { if (dropsLoot) return super.dropFromLootTable(level, lootTable, paramsBuilder, dropConsumer); return false; } //?}
public void addToBattle(BishopBattle battle) { battle.addEntity(this); dropsLoot = false; }
@Override protected SoundEvent getAmbientSound() { return null; } @Override protected SoundEvent getHurtSound(DamageSource source) { return SoundEvents.SILVERFISH_HURT; } @Override protected SoundEvent getDeathSound() { return SoundEvents.SILVERFISH_DEATH; }
@Override protected void registerGoals() { targetSelector.addGoal(2, new AntBattleGoal<>(this, Player.class, true)); goalSelector.addGoal(3, new AntBlockRecolorGoal(this)); super.registerGoals(); } @Override protected void initRegularGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(5, new WanderConditionalGoal(this, 1.0)); goalSelector.addGoal(6, new LookAtEntityConditionalGoal(this, Player.class, 6.0F)); goalSelector.addGoal(7, new LookAroundConditionalGoal(this)); }
public static AttributeSupplier.Builder createAttributes() { return Monster.createMonsterAttributes() .add(Attributes.MOVEMENT_SPEED, 0.1f) .add(Attributes.MAX_HEALTH, 6); }
@Override public void readAdditionalSaveData( //? if >1.21.2 { ValueInput //?} else { /*CompoundTag *///?} nbt) { super.readAdditionalSaveData(nbt); this.dropsLoot = NbtUtils.getBoolean(nbt, "dropsLoot", true); this.direction = switch(NbtUtils.getString(nbt, "direction")) { case "N" -> Direction.NORTH; case "W" -> Direction.WEST; case "S" -> Direction.SOUTH; default -> Direction.EAST; }; //? if >1.21.2 { this.lastChangedPos = nbt.read("last_changed_pos", BlockPos.CODEC).orElse(lastChangedPos); //?} else { /*if (nbt.contains("last_changed_pos")) { CompoundTag pos = NbtUtils.getCompound(nbt, "last_changed_pos"); this.lastChangedPos = new BlockPos(NbtUtils.getInt(pos, "x"), NbtUtils.getInt(nbt,"y"), NbtUtils.getInt(nbt, "z")); } *///?} }
//~ if >1.21.2 'CompoundTag'->'ValueOutput' { @Override public void addAdditionalSaveData(ValueOutput nbt) { super.addAdditionalSaveData(nbt); nbt.putBoolean("dropsLoot", dropsLoot); nbt.putString("direction", switch(this.direction) { case NORTH -> "N"; case WEST -> "W"; case SOUTH -> "S"; default -> "E"; }); if (lastChangedPos != null) { //? if >1.21.2 { nbt.store("last_changed_pos", BlockPos.CODEC, lastChangedPos); //?} else { /*ValueOutput pos = new ValueOutput(); pos.putInt("x", lastChangedPos.getX()); pos.putInt("y", lastChangedPos.getY()); pos.putInt("z", lastChangedPos.getZ()); nbt.put("last_changed_pos", pos); *///?} } } //~}
@Override public boolean shouldPursueRegularGoals() { Level w = level(); Optional<BlockPos> bp = mainSupportingBlockPos; return (bp.isEmpty() || isInBattle() || !AntBlock.isSafeToRecolor(w, bp.get())) && super.shouldPursueRegularGoals(); }
//? if >1.21 { @Override public boolean canBeLeashed() { return !isInBattle(); } //?} @Override public boolean canStandOnFluid(FluidState state) { return state.is(FluidTags.WATER); }
public static VoxelShape getWaterCollisionShape(int level) { return Block.box(0.0, 0.0, 0.0, 16.0, level, 16.0); }
@Override public void aiStep() { super.aiStep(); //~ if >26 'this.fluidHeight.getDouble'->'getFluidHeight' if (!this.firstTick && this.fluidHeight.getDouble(FluidTags.WATER) > 0.0) { CollisionContext shapeContext = CollisionContext.of(this); if (shapeContext.isAbove(getWaterCollisionShape(15), this.blockPosition(), true) && !this.level().getFluidState(this.blockPosition().above()).is(FluidTags.WATER)) { this.setOnGround(true); } else { this.setDeltaMovement(this.getDeltaMovement().scale(0.5).add(0.0, 0.05, 0.0)); } } }
@Override public InteractionResult mobInteract(Player player, InteractionHand hand) { if (!this.isVehicle() //? if >1.21 && this.getAttributeValue(Attributes.SCALE) > 2 && player.getItemInHand(hand).isEmpty()) { this.putPlayerOnBack(player); return VersionMethods.sidedSuccess(this.level().isClientSide()); } return super.mobInteract(player, hand); } protected void putPlayerOnBack(Player player) { if (!this.level().isClientSide()) { player.setYRot(this.getYRot()); player.setXRot(this.getXRot()); player.startRiding(this); } } @Override protected Vec3 getRiddenInput(Player controllingPlayer, Vec3 movementInput) { float f = controllingPlayer.zza; return new Vec3(controllingPlayer.xxa * 0.5f, 0, f < 0 ? f*0.25f : f); } @Override protected float getRiddenSpeed(Player controllingPlayer) { return (float)this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 0.8f; } @Nullable @Override public LivingEntity getControllingPassenger() { if (this.getFirstPassenger() instanceof Player player) { return player; } return super.getControllingPassenger(); } @Override protected void positionRider(Entity passenger, MoveFunction positionUpdater) { super.positionRider(passenger, positionUpdater); if (passenger instanceof LivingEntity) { ((LivingEntity)passenger).yBodyRot = this.yBodyRot; } } @Override protected void tickRidden(Player controllingPlayer, Vec3 movementInput) { super.tickRidden(controllingPlayer, movementInput); this.setRot(controllingPlayer.getYRot(),controllingPlayer.getXRot() * 0.5F); this.yRotO = this.yBodyRot = this.yHeadRot = this.getYRot(); }
public static class AntBattleGoal<T extends LivingEntity> extends NearestAttackableTargetGoal<T> { public AntBattleGoal(Mob mob, Class<T> targetClass, boolean checkVisibility) { super(mob, targetClass, checkVisibility); }
@Override public boolean canUse() { if (mob instanceof AbstractChessFigure e && !e.isInBattle()) return false; return super.canUse(); } }
public static class AntBlockRecolorGoal extends Goal { private final AntEntity mob; @Nullable private BlockPos targetPos;
public AntBlockRecolorGoal(AntEntity mob) { this.mob = mob; }
@Override public boolean canUse() { if (mob.shouldPursueRegularGoals()) return false; Optional<BlockPos> bp = mob.mainSupportingBlockPos; if (bp.isEmpty() || bp.get().equals(mob.lastChangedPos)) return false; mob.lastChangedPos = bp.get();
BlockState down = mob.level().getBlockState(bp.get()); AntBlock.Clockwiseness cw = AntBlock.getCW(down.getBlock()); if (cw == null) return false;
Direction direction = mob.direction; Direction direction2 = cw.equals(AntBlock.Clockwiseness.CW) ? direction.getClockWise() : direction.getCounterClockWise();
Block newBlock = AntBlock.recolor(down.getBlock(), cw.equals(AntBlock.Clockwiseness.CCW)); if (newBlock == null) return false;
targetPos = mob.blockPosition().relative(direction2); mob.level().setBlock(bp.get(), newBlock.withPropertiesOf(down), 19); mob.direction = direction2; return true; }
@Override public boolean canContinueToUse() { return false; }
@Override public void start() { if (targetPos == null) return; Vec3 v = Vec3.atBottomCenterOf(targetPos); mob.randomTeleport(v.x, v.y, v.z, false); mob.setYRot(mob.direction.toYRot()); } }
public static class WanderConditionalGoal extends WaterAvoidingRandomStrollGoal { private final AntEntity mob1; public WanderConditionalGoal(AntEntity pathAwareEntity, double d) { super(pathAwareEntity, d); mob1 = pathAwareEntity; }
@Override public boolean canUse() { return super.canUse() && mob1.shouldPursueRegularGoals(); } }
public static class LookAroundConditionalGoal extends RandomLookAroundGoal { private final AntEntity mob1; public LookAroundConditionalGoal(AntEntity mob) { super(mob); mob1 = mob; }
@Override public boolean canUse() { return super.canUse() && mob1.shouldPursueRegularGoals(); } }
public static class LookAtEntityConditionalGoal extends LookAtPlayerGoal { private final AntEntity mob1;
public LookAtEntityConditionalGoal(AntEntity mob, Class<? extends LivingEntity> targetType, float range) { super(mob, targetType, range); mob1 = mob; }
@Override public boolean canUse() { return super.canUse() && mob1.shouldPursueRegularGoals(); } }}