| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.HolderLookup;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.nbt.CompoundTag;
|
| 10 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.sounds.SoundEvent;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.sounds.SoundSource;
|
| 15 | import net.minecraft.tags.BlockTags;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.util.RandomSource;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.effect.MobEffectInstance;
|
| 20 | import net.minecraft.world.effect.MobEffects;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.EntityReference;
|
| 23 | import net.minecraft.world.entity.LivingEntity;
|
| 24 | import net.minecraft.world.entity.monster.Enemy;
|
| 25 | import net.minecraft.world.entity.player.Player;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.block.state.BlockState;
|
| 28 | import net.minecraft.world.level.storage.ValueInput;
|
| 29 | import net.minecraft.world.level.storage.ValueOutput;
|
| 30 | import net.minecraft.world.phys.AABB;
|
| 31 | import net.minecraft.world.phys.Vec3;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | public class ConduitBlockEntity extends BlockEntity {
|
| 35 | private static final int BLOCK_REFRESH_RATE = 2;
|
| 36 | private static final int EFFECT_DURATION = 13;
|
| 37 | private static final float ROTATION_SPEED = -0.0375F;
|
| 38 | private static final int MIN_ACTIVE_SIZE = 16;
|
| 39 | private static final int MIN_KILL_SIZE = 42;
|
| 40 | private static final int KILL_RANGE = 8;
|
| 41 | public int tickCount;
|
| 42 | private float activeRotation;
|
| 43 | private boolean isActive;
|
| 44 | private boolean isHunting;
|
| 45 | private final List<BlockPos> effectBlocks = Lists.newArrayList();
|
| 46 | private @Nullable EntityReference<LivingEntity> destroyTarget;
|
| 47 | private long nextAmbientSoundActivation;
|
| 48 |
|
| 49 | public ConduitBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 50 | super(BlockEntityTypes.CONDUIT, worldPosition, blockState);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void loadAdditional(final ValueInput input) {
|
| 55 | super.loadAdditional(input);
|
| 56 | this.destroyTarget = EntityReference.read(input, "Target");
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void saveAdditional(final ValueOutput output) {
|
| 61 | super.saveAdditional(output);
|
| 62 | EntityReference.store(this.destroyTarget, output, "Target");
|
| 63 | }
|
| 64 |
|
| 65 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 66 | return ClientboundBlockEntityDataPacket.create(this);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 71 | return this.saveCustomOnly(registries);
|
| 72 | }
|
| 73 |
|
| 74 | public static void clientTick(final Level level, final BlockPos pos, final BlockState state, final ConduitBlockEntity entity) {
|
| 75 | entity.tickCount++;
|
| 76 | long gameTime = level.getGameTime();
|
| 77 | List<BlockPos> effectBlocks = entity.effectBlocks;
|
| 78 | if (gameTime % 40L == 0L) {
|
| 79 | entity.isActive = updateShape(level, pos, effectBlocks);
|
| 80 | updateHunting(entity, effectBlocks);
|
| 81 | }
|
| 82 |
|
| 83 | LivingEntity destroyTarget = EntityReference.getLivingEntity(entity.destroyTarget, level);
|
| 84 | animationTick(level, pos, effectBlocks, destroyTarget, entity.tickCount);
|
| 85 | if (entity.isActive()) {
|
| 86 | entity.activeRotation++;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public static void serverTick(final Level level, final BlockPos pos, final BlockState state, final ConduitBlockEntity entity) {
|
| 91 | entity.tickCount++;
|
| 92 | long gameTime = level.getGameTime();
|
| 93 | List<BlockPos> effectBlocks = entity.effectBlocks;
|
| 94 | if (gameTime % 40L == 0L) {
|
| 95 | boolean active = updateShape(level, pos, effectBlocks);
|
| 96 | if (active != entity.isActive) {
|
| 97 | SoundEvent event = active ? SoundEvents.CONDUIT_ACTIVATE : SoundEvents.CONDUIT_DEACTIVATE;
|
| 98 | level.playSound(null, pos, event, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 99 | }
|
| 100 |
|
| 101 | entity.isActive = active;
|
| 102 | updateHunting(entity, effectBlocks);
|
| 103 | if (active) {
|
| 104 | applyEffects(level, pos, effectBlocks);
|
| 105 | updateAndAttackTarget((ServerLevel)level, pos, state, entity, effectBlocks.size() >= 42);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | if (entity.isActive()) {
|
| 110 | if (gameTime % 80L == 0L) {
|
| 111 | level.playSound(null, pos, SoundEvents.CONDUIT_AMBIENT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 112 | }
|
| 113 |
|
| 114 | if (gameTime > entity.nextAmbientSoundActivation) {
|
| 115 | entity.nextAmbientSoundActivation = gameTime + 60L + level.getRandom().nextInt(40);
|
| 116 | level.playSound(null, pos, SoundEvents.CONDUIT_AMBIENT_SHORT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | private static void updateHunting(final ConduitBlockEntity entity, final List<BlockPos> effectBlocks) {
|
| 122 | entity.setHunting(effectBlocks.size() >= 42);
|
| 123 | }
|
| 124 |
|
| 125 | private static boolean updateShape(final Level level, final BlockPos worldPosition, final List<BlockPos> effectBlocks) {
|
| 126 | effectBlocks.clear();
|
| 127 |
|
| 128 | for (int ox = -1; ox <= 1; ox++) {
|
| 129 | for (int oy = -1; oy <= 1; oy++) {
|
| 130 | for (int oz = -1; oz <= 1; oz++) {
|
| 131 | BlockPos testPos = worldPosition.offset(ox, oy, oz);
|
| 132 | if (!level.isWaterAt(testPos)) {
|
| 133 | return false;
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | for (int ox = -2; ox <= 2; ox++) {
|
| 140 | for (int oy = -2; oy <= 2; oy++) {
|
| 141 | for (int oz = -2; oz <= 2; oz++) {
|
| 142 | int ax = Math.abs(ox);
|
| 143 | int ay = Math.abs(oy);
|
| 144 | int az = Math.abs(oz);
|
| 145 | if ((ax > 1 || ay > 1 || az > 1) && (ox == 0 && (ay == 2 || az == 2) || oy == 0 && (ax == 2 || az == 2) || oz == 0 && (ax == 2 || ay == 2))) {
|
| 146 | BlockPos testPos = worldPosition.offset(ox, oy, oz);
|
| 147 | BlockState testBlock = level.getBlockState(testPos);
|
| 148 | if (testBlock.is(BlockTags.CONDUIT_EFFECT_BLOCK)) {
|
| 149 | effectBlocks.add(testPos);
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return effectBlocks.size() >= 16;
|
| 157 | }
|
| 158 |
|
| 159 | private static void applyEffects(final Level level, final BlockPos worldPosition, final List<BlockPos> effectBlocks) {
|
| 160 | int activeSize = effectBlocks.size();
|
| 161 | int effectRange = activeSize / 7 * 16;
|
| 162 | int x = worldPosition.getX();
|
| 163 | int y = worldPosition.getY();
|
| 164 | int z = worldPosition.getZ();
|
| 165 | AABB bb = new AABB(x, y, z, x + 1, y + 1, z + 1).inflate(effectRange).expandTowards(0.0, level.getHeight(), 0.0);
|
| 166 | List<Player> players = level.getEntitiesOfClass(Player.class, bb);
|
| 167 | if (!players.isEmpty()) {
|
| 168 | for (Player player : players) {
|
| 169 | if (worldPosition.closerThan(player.blockPosition(), effectRange) && player.isInWaterOrRain()) {
|
| 170 | player.addEffect(new MobEffectInstance(MobEffects.CONDUIT_POWER, 260, 0, true, true));
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private static void updateAndAttackTarget(
|
| 177 | final ServerLevel level, final BlockPos worldPosition, final BlockState blockState, final ConduitBlockEntity entity, final boolean isActive
|
| 178 | ) {
|
| 179 | EntityReference<LivingEntity> newDestroyTarget = updateDestroyTarget(entity.destroyTarget, level, worldPosition, isActive);
|
| 180 | LivingEntity targetEntity = EntityReference.getLivingEntity(newDestroyTarget, level);
|
| 181 | if (targetEntity != null) {
|
| 182 | level.playSound(null, targetEntity.getX(), targetEntity.getY(), targetEntity.getZ(), SoundEvents.CONDUIT_ATTACK_TARGET, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 183 | targetEntity.hurtServer(level, level.damageSources().magic(), 4.0F);
|
| 184 | }
|
| 185 |
|
| 186 | if (!Objects.equals(newDestroyTarget, entity.destroyTarget)) {
|
| 187 | entity.destroyTarget = newDestroyTarget;
|
| 188 | level.sendBlockUpdated(worldPosition, blockState, blockState, 2);
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | private static @Nullable EntityReference<LivingEntity> updateDestroyTarget(
|
| 193 | final @Nullable EntityReference<LivingEntity> target, final ServerLevel level, final BlockPos pos, final boolean isActive
|
| 194 | ) {
|
| 195 | if (!isActive) {
|
| 196 | return null;
|
| 197 | }
|
| 198 |
|
| 199 | if (target == null) {
|
| 200 | return selectNewTarget(level, pos);
|
| 201 | }
|
| 202 |
|
| 203 | LivingEntity targetEntity = EntityReference.getLivingEntity(target, level);
|
| 204 | return targetEntity != null && targetEntity.isAlive() && pos.closerThan(targetEntity.blockPosition(), 8.0) ? target : null;
|
| 205 | }
|
| 206 |
|
| 207 | private static @Nullable EntityReference<LivingEntity> selectNewTarget(final ServerLevel level, final BlockPos pos) {
|
| 208 | List<LivingEntity> candidates = level.getEntitiesOfClass(
|
| 209 | LivingEntity.class, getDestroyRangeAABB(pos), input -> input instanceof Enemy && input.isInWaterOrRain()
|
| 210 | );
|
| 211 | return candidates.isEmpty() ? null : EntityReference.of(Util.getRandom(candidates, level.getRandom()));
|
| 212 | }
|
| 213 |
|
| 214 | private static AABB getDestroyRangeAABB(final BlockPos worldPosition) {
|
| 215 | return new AABB(worldPosition).inflate(8.0);
|
| 216 | }
|
| 217 |
|
| 218 | private static void animationTick(
|
| 219 | final Level level, final BlockPos worldPosition, final List<BlockPos> effectBlocks, final @Nullable Entity destroyTarget, final int tickCount
|
| 220 | ) {
|
| 221 | RandomSource random = level.getRandom();
|
| 222 | double hh = Mth.sin((tickCount + 35) * 0.1F) / 2.0F + 0.5F;
|
| 223 | hh = (hh * hh + hh) * 0.3F;
|
| 224 | Vec3 particleEnd = new Vec3(worldPosition.getX() + 0.5, worldPosition.getY() + 1.5 + hh, worldPosition.getZ() + 0.5);
|
| 225 |
|
| 226 | for (BlockPos pos : effectBlocks) {
|
| 227 | if (random.nextInt(50) == 0) {
|
| 228 | BlockPos delta = pos.subtract(worldPosition);
|
| 229 | float dx = -0.5F + random.nextFloat() + delta.getX();
|
| 230 | float dy = -2.0F + random.nextFloat() + delta.getY();
|
| 231 | float dz = -0.5F + random.nextFloat() + delta.getZ();
|
| 232 | level.addParticle(ParticleTypes.NAUTILUS, particleEnd.x, particleEnd.y, particleEnd.z, dx, dy, dz);
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | if (destroyTarget != null) {
|
| 237 | Vec3 targetPosition = new Vec3(destroyTarget.getX(), destroyTarget.getEyeY(), destroyTarget.getZ());
|
| 238 | float randx = (-0.5F + random.nextFloat()) * (3.0F + destroyTarget.getBbWidth());
|
| 239 | float randy = -1.0F + random.nextFloat() * destroyTarget.getBbHeight();
|
| 240 | float randz = (-0.5F + random.nextFloat()) * (3.0F + destroyTarget.getBbWidth());
|
| 241 | Vec3 velocity = new Vec3(randx, randy, randz);
|
| 242 | level.addParticle(ParticleTypes.NAUTILUS, targetPosition.x, targetPosition.y, targetPosition.z, velocity.x, velocity.y, velocity.z);
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | public boolean isActive() {
|
| 247 | return this.isActive;
|
| 248 | }
|
| 249 |
|
| 250 | public boolean isHunting() {
|
| 251 | return this.isHunting;
|
| 252 | }
|
| 253 |
|
| 254 | private void setHunting(final boolean hunting) {
|
| 255 | this.isHunting = hunting;
|
| 256 | }
|
| 257 |
|
| 258 | public float getActiveRotation(final float a) {
|
| 259 | return (this.activeRotation + a) * -0.0375F;
|
| 260 | }
|
| 261 | }
|
| 262 |
|