| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.HolderLookup;
|
| 15 | import net.minecraft.core.component.DataComponentGetter;
|
| 16 | import net.minecraft.core.component.DataComponentMap;
|
| 17 | import net.minecraft.core.component.DataComponents;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.nbt.CompoundTag;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.chat.ComponentSerialization;
|
| 22 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 23 | import net.minecraft.server.level.ServerPlayer;
|
| 24 | import net.minecraft.sounds.SoundEvent;
|
| 25 | import net.minecraft.sounds.SoundEvents;
|
| 26 | import net.minecraft.sounds.SoundSource;
|
| 27 | import net.minecraft.tags.BlockTags;
|
| 28 | import net.minecraft.util.ARGB;
|
| 29 | import net.minecraft.world.LockCode;
|
| 30 | import net.minecraft.world.MenuProvider;
|
| 31 | import net.minecraft.world.Nameable;
|
| 32 | import net.minecraft.world.effect.MobEffect;
|
| 33 | import net.minecraft.world.effect.MobEffectInstance;
|
| 34 | import net.minecraft.world.effect.MobEffects;
|
| 35 | import net.minecraft.world.entity.player.Inventory;
|
| 36 | import net.minecraft.world.entity.player.Player;
|
| 37 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 38 | import net.minecraft.world.inventory.BeaconMenu;
|
| 39 | import net.minecraft.world.inventory.ContainerData;
|
| 40 | import net.minecraft.world.inventory.ContainerLevelAccess;
|
| 41 | import net.minecraft.world.level.Level;
|
| 42 | import net.minecraft.world.level.block.BeaconBeamBlock;
|
| 43 | import net.minecraft.world.level.block.Blocks;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 46 | import net.minecraft.world.level.storage.ValueInput;
|
| 47 | import net.minecraft.world.level.storage.ValueOutput;
|
| 48 | import net.minecraft.world.phys.AABB;
|
| 49 | import net.minecraft.world.phys.Vec3;
|
| 50 | import org.jspecify.annotations.Nullable;
|
| 51 |
|
| 52 | public class BeaconBlockEntity extends BlockEntity implements MenuProvider, Nameable, BeaconBeamOwner {
|
| 53 | private static final int MAX_LEVELS = 4;
|
| 54 | private static final int LEVELS_NEEDED_FOR_SECONDARY = 4;
|
| 55 | public static final List<List<Holder<MobEffect>>> BEACON_EFFECTS = List.of(
|
| 56 | List.of(MobEffects.SPEED, MobEffects.HASTE),
|
| 57 | List.of(MobEffects.RESISTANCE, MobEffects.JUMP_BOOST),
|
| 58 | List.of(MobEffects.STRENGTH),
|
| 59 | List.of(MobEffects.REGENERATION)
|
| 60 | );
|
| 61 | private static final Set<Holder<MobEffect>> VALID_EFFECTS = BEACON_EFFECTS.stream().flatMap(Collection::stream).collect(Collectors.toSet());
|
| 62 | public static final int DATA_LEVELS = 0;
|
| 63 | public static final int DATA_PRIMARY = 1;
|
| 64 | public static final int DATA_SECONDARY = 2;
|
| 65 | public static final int NUM_DATA_VALUES = 3;
|
| 66 | private static final int BLOCKS_CHECK_PER_TICK = 10;
|
| 67 | private static final Component DEFAULT_NAME = Component.translatable("container.beacon");
|
| 68 | private static final String TAG_PRIMARY = "primary_effect";
|
| 69 | private static final String TAG_SECONDARY = "secondary_effect";
|
| 70 | private List<BeaconBeamOwner.Section> beamSections = new ArrayList<>();
|
| 71 | private List<BeaconBeamOwner.Section> checkingBeamSections = new ArrayList<>();
|
| 72 | private int levels;
|
| 73 | private int lastCheckY;
|
| 74 | private @Nullable Holder<MobEffect> primaryPower;
|
| 75 | private @Nullable Holder<MobEffect> secondaryPower;
|
| 76 | private @Nullable Component name;
|
| 77 | private LockCode lockKey = LockCode.NO_LOCK;
|
| 78 | private final ContainerData dataAccess = new ContainerData() {
|
| 79 | @Override
|
| 80 | public int get(final int dataId) {
|
| 81 | return switch (dataId) {
|
| 82 | case 0 -> BeaconBlockEntity.this.levels;
|
| 83 | case 1 -> BeaconMenu.encodeEffect(BeaconBlockEntity.this.primaryPower);
|
| 84 | case 2 -> BeaconMenu.encodeEffect(BeaconBlockEntity.this.secondaryPower);
|
| 85 | default -> 0;
|
| 86 | };
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void set(final int dataId, final int value) {
|
| 91 | switch (dataId) {
|
| 92 | case 0:
|
| 93 | BeaconBlockEntity.this.levels = value;
|
| 94 | break;
|
| 95 | case 1:
|
| 96 | if (!BeaconBlockEntity.this.level.isClientSide() && !BeaconBlockEntity.this.beamSections.isEmpty()) {
|
| 97 | BeaconBlockEntity.playSound(BeaconBlockEntity.this.level, BeaconBlockEntity.this.worldPosition, SoundEvents.BEACON_POWER_SELECT);
|
| 98 | }
|
| 99 |
|
| 100 | BeaconBlockEntity.this.primaryPower = BeaconBlockEntity.filterEffect(BeaconMenu.decodeEffect(value));
|
| 101 | break;
|
| 102 | case 2:
|
| 103 | BeaconBlockEntity.this.secondaryPower = BeaconBlockEntity.filterEffect(BeaconMenu.decodeEffect(value));
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public int getCount() {
|
| 109 | return 3;
|
| 110 | }
|
| 111 | };
|
| 112 |
|
| 113 | private static @Nullable Holder<MobEffect> filterEffect(final @Nullable Holder<MobEffect> effect) {
|
| 114 | return VALID_EFFECTS.contains(effect) ? effect : null;
|
| 115 | }
|
| 116 |
|
| 117 | public BeaconBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 118 | super(BlockEntityTypes.BEACON, worldPosition, blockState);
|
| 119 | }
|
| 120 |
|
| 121 | public static void tick(final Level level, final BlockPos pos, final BlockState selfState, final BeaconBlockEntity entity) {
|
| 122 | int x = pos.getX();
|
| 123 | int y = pos.getY();
|
| 124 | int z = pos.getZ();
|
| 125 | BlockPos checkPos;
|
| 126 | if (entity.lastCheckY < y) {
|
| 127 | checkPos = pos;
|
| 128 | entity.checkingBeamSections = Lists.newArrayList();
|
| 129 | entity.lastCheckY = checkPos.getY() - 1;
|
| 130 | } else {
|
| 131 | checkPos = new BlockPos(x, entity.lastCheckY + 1, z);
|
| 132 | }
|
| 133 |
|
| 134 | BeaconBeamOwner.Section lastBeamSection = entity.checkingBeamSections.isEmpty()
|
| 135 | ? null
|
| 136 | : entity.checkingBeamSections.get(entity.checkingBeamSections.size() - 1);
|
| 137 | int lastSetBlock = level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z);
|
| 138 |
|
| 139 | for (int i = 0; i < 10 && checkPos.getY() <= lastSetBlock; i++) {
|
| 140 | BlockState state = level.getBlockState(checkPos);
|
| 141 | if (state.getBlock() instanceof BeaconBeamBlock beaconBeamBlock) {
|
| 142 | int color = beaconBeamBlock.getColor().getTextureDiffuseColor();
|
| 143 | if (entity.checkingBeamSections.size() <= 1) {
|
| 144 | lastBeamSection = new BeaconBeamOwner.Section(color);
|
| 145 | entity.checkingBeamSections.add(lastBeamSection);
|
| 146 | } else if (lastBeamSection != null) {
|
| 147 | if (color == lastBeamSection.getColor()) {
|
| 148 | lastBeamSection.increaseHeight();
|
| 149 | } else {
|
| 150 | lastBeamSection = new BeaconBeamOwner.Section(ARGB.average(lastBeamSection.getColor(), color));
|
| 151 | entity.checkingBeamSections.add(lastBeamSection);
|
| 152 | }
|
| 153 | }
|
| 154 | } else {
|
| 155 | if (lastBeamSection == null || state.getLightDampening() >= 15 && !state.is(Blocks.BEDROCK)) {
|
| 156 | entity.checkingBeamSections.clear();
|
| 157 | entity.lastCheckY = lastSetBlock;
|
| 158 | break;
|
| 159 | }
|
| 160 |
|
| 161 | lastBeamSection.increaseHeight();
|
| 162 | }
|
| 163 |
|
| 164 | checkPos = checkPos.above();
|
| 165 | entity.lastCheckY++;
|
| 166 | }
|
| 167 |
|
| 168 | int previousLevels = entity.levels;
|
| 169 | if (level.getGameTime() % 80L == 0L) {
|
| 170 | if (!entity.beamSections.isEmpty()) {
|
| 171 | entity.levels = updateBase(level, x, y, z);
|
| 172 | }
|
| 173 |
|
| 174 | if (entity.levels > 0 && !entity.beamSections.isEmpty()) {
|
| 175 | applyEffects(level, pos, entity.levels, entity.primaryPower, entity.secondaryPower);
|
| 176 | playSound(level, pos, SoundEvents.BEACON_AMBIENT);
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | if (entity.lastCheckY >= lastSetBlock) {
|
| 181 | entity.lastCheckY = level.getMinY() - 1;
|
| 182 | boolean wasActive = previousLevels > 0;
|
| 183 | entity.beamSections = entity.checkingBeamSections;
|
| 184 | if (!level.isClientSide()) {
|
| 185 | boolean isActive = entity.levels > 0;
|
| 186 | if (!wasActive && isActive) {
|
| 187 | playSound(level, pos, SoundEvents.BEACON_ACTIVATE);
|
| 188 |
|
| 189 | for (ServerPlayer player : level.getEntitiesOfClass(ServerPlayer.class, new AABB(x, y, z, x, y - 4, z).inflate(10.0, 5.0, 10.0))) {
|
| 190 | CriteriaTriggers.CONSTRUCT_BEACON.trigger(player, entity.levels);
|
| 191 | }
|
| 192 | } else if (wasActive && !isActive) {
|
| 193 | playSound(level, pos, SoundEvents.BEACON_DEACTIVATE);
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | private static int updateBase(final Level level, final int x, final int y, final int z) {
|
| 200 | int levels = 0;
|
| 201 |
|
| 202 | for (int step = 1; step <= 4; levels = step++) {
|
| 203 | int ly = y - step;
|
| 204 | if (ly < level.getMinY()) {
|
| 205 | break;
|
| 206 | }
|
| 207 |
|
| 208 | boolean isOk = true;
|
| 209 |
|
| 210 | for (int lx = x - step; lx <= x + step && isOk; lx++) {
|
| 211 | for (int lz = z - step; lz <= z + step; lz++) {
|
| 212 | if (!level.getBlockState(new BlockPos(lx, ly, lz)).is(BlockTags.BEACON_BASE_BLOCKS)) {
|
| 213 | isOk = false;
|
| 214 | break;
|
| 215 | }
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | if (!isOk) {
|
| 220 | break;
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | return levels;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public void setRemoved() {
|
| 229 | playSound(this.level, this.worldPosition, SoundEvents.BEACON_DEACTIVATE);
|
| 230 | super.setRemoved();
|
| 231 | }
|
| 232 |
|
| 233 | public static boolean validateEffects(final @Nullable Holder<MobEffect> primary, final @Nullable Holder<MobEffect> secondary, final int levels) {
|
| 234 | if (secondary != null && levels < 4) {
|
| 235 | return false;
|
| 236 | } else {
|
| 237 | int primaryLevel = getRequiredLevelsFor(primary);
|
| 238 | int secondaryLevel = getRequiredLevelsFor(secondary);
|
| 239 | if (primaryLevel > levels || secondaryLevel > levels) {
|
| 240 | return false;
|
| 241 | } else {
|
| 242 | return primaryLevel >= 4 ? false : secondaryLevel == 0 || secondaryLevel >= 4 || primary.equals(secondary);
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | private static int getRequiredLevelsFor(final @Nullable Holder<MobEffect> effect) {
|
| 248 | if (effect == null) {
|
| 249 | return 0;
|
| 250 | }
|
| 251 |
|
| 252 | for (int i = 0; i < BEACON_EFFECTS.size(); i++) {
|
| 253 | List<Holder<MobEffect>> effectsForLevel = BEACON_EFFECTS.get(i);
|
| 254 | if (effectsForLevel.contains(effect)) {
|
| 255 | return i + 1;
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | return Integer.MAX_VALUE;
|
| 260 | }
|
| 261 |
|
| 262 | private static void applyEffects(
|
| 263 | final Level level,
|
| 264 | final BlockPos worldPosition,
|
| 265 | final int levels,
|
| 266 | final @Nullable Holder<MobEffect> primaryPower,
|
| 267 | final @Nullable Holder<MobEffect> secondaryPower
|
| 268 | ) {
|
| 269 | if (!level.isClientSide() && primaryPower != null) {
|
| 270 | double range = levels * 10 + 10;
|
| 271 | int baseAmp = 0;
|
| 272 | if (levels >= 4 && Objects.equals(primaryPower, secondaryPower)) {
|
| 273 | baseAmp = 1;
|
| 274 | }
|
| 275 |
|
| 276 | int durationTicks = (9 + levels * 2) * 20;
|
| 277 | AABB bb = new AABB(worldPosition).inflate(range).expandTowards(0.0, level.getHeight(), 0.0);
|
| 278 | List<Player> players = level.getEntitiesOfClass(Player.class, bb);
|
| 279 |
|
| 280 | for (Player player : players) {
|
| 281 | player.addEffect(new MobEffectInstance(primaryPower, durationTicks, baseAmp, true, true));
|
| 282 | }
|
| 283 |
|
| 284 | if (levels >= 4 && !Objects.equals(primaryPower, secondaryPower) && secondaryPower != null) {
|
| 285 | for (Player player : players) {
|
| 286 | player.addEffect(new MobEffectInstance(secondaryPower, durationTicks, 0, true, true));
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | public static void playSound(final Level level, final BlockPos worldPosition, final SoundEvent event) {
|
| 293 | level.playSound(null, worldPosition, event, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 294 | }
|
| 295 |
|
| 296 | @Override
|
| 297 | public List<BeaconBeamOwner.Section> getBeamSections() {
|
| 298 | return this.levels == 0 ? ImmutableList.of() : this.beamSections;
|
| 299 | }
|
| 300 |
|
| 301 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 302 | return ClientboundBlockEntityDataPacket.create(this);
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 307 | return this.saveCustomOnly(registries);
|
| 308 | }
|
| 309 |
|
| 310 | private static void storeEffect(final ValueOutput output, final String field, final @Nullable Holder<MobEffect> effect) {
|
| 311 | if (effect != null) {
|
| 312 | effect.unwrapKey().ifPresent(key -> output.putString(field, key.identifier().toString()));
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | private static @Nullable Holder<MobEffect> loadEffect(final ValueInput input, final String field) {
|
| 317 | return input.read(field, BuiltInRegistries.MOB_EFFECT.holderByNameCodec()).filter(VALID_EFFECTS::contains).orElse(null);
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | protected void loadAdditional(final ValueInput input) {
|
| 322 | super.loadAdditional(input);
|
| 323 | this.primaryPower = loadEffect(input, "primary_effect");
|
| 324 | this.secondaryPower = loadEffect(input, "secondary_effect");
|
| 325 | this.name = parseCustomNameSafe(input, "CustomName");
|
| 326 | this.lockKey = LockCode.fromTag(input);
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | protected void saveAdditional(final ValueOutput output) {
|
| 331 | super.saveAdditional(output);
|
| 332 | storeEffect(output, "primary_effect", this.primaryPower);
|
| 333 | storeEffect(output, "secondary_effect", this.secondaryPower);
|
| 334 | output.putInt("Levels", this.levels);
|
| 335 | output.storeNullable("CustomName", ComponentSerialization.CODEC, this.name);
|
| 336 | this.lockKey.addToTag(output);
|
| 337 | }
|
| 338 |
|
| 339 | public void setCustomName(final @Nullable Component name) {
|
| 340 | this.name = name;
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | public @Nullable Component getCustomName() {
|
| 345 | return this.name;
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 350 | if (this.lockKey.canUnlock(player)) {
|
| 351 | return new BeaconMenu(containerId, inventory, this.dataAccess, ContainerLevelAccess.create(this.level, this.getBlockPos()));
|
| 352 | }
|
| 353 |
|
| 354 | BaseContainerBlockEntity.sendChestLockedNotifications(Vec3.atCenterOf(this.getBlockPos()), player, this.getDisplayName());
|
| 355 | return null;
|
| 356 | }
|
| 357 |
|
| 358 | @Override
|
| 359 | public Component getDisplayName() {
|
| 360 | return this.getName();
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public Component getName() {
|
| 365 | return this.name != null ? this.name : DEFAULT_NAME;
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 370 | super.applyImplicitComponents(components);
|
| 371 | this.name = components.get(DataComponents.CUSTOM_NAME);
|
| 372 | this.lockKey = components.getOrDefault(DataComponents.LOCK, LockCode.NO_LOCK);
|
| 373 | }
|
| 374 |
|
| 375 | @Override
|
| 376 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 377 | super.collectImplicitComponents(components);
|
| 378 | components.set(DataComponents.CUSTOM_NAME, this.name);
|
| 379 | if (!this.lockKey.equals(LockCode.NO_LOCK)) {
|
| 380 | components.set(DataComponents.LOCK, this.lockKey);
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | @Override
|
| 385 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 386 | output.discard("CustomName");
|
| 387 | output.discard("lock");
|
| 388 | }
|
| 389 |
|
| 390 | @Override
|
| 391 | public void setLevel(final Level level) {
|
| 392 | super.setLevel(level);
|
| 393 | this.lastCheckY = level.getMinY() - 1;
|
| 394 | }
|
| 395 | }
|
| 396 |
|