| 1 | package net.minecraft.world.level.block.entity.vault;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Set;
|
| 6 | import java.util.UUID;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.HolderLookup;
|
| 10 | import net.minecraft.core.particles.ParticleOptions;
|
| 11 | import net.minecraft.core.particles.ParticleTypes;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.NbtOps;
|
| 14 | import net.minecraft.network.protocol.Packet;
|
| 15 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.sounds.SoundEvent;
|
| 20 | import net.minecraft.sounds.SoundEvents;
|
| 21 | import net.minecraft.sounds.SoundSource;
|
| 22 | import net.minecraft.stats.Stats;
|
| 23 | import net.minecraft.util.Mth;
|
| 24 | import net.minecraft.util.RandomSource;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import net.minecraft.world.entity.player.Player;
|
| 27 | import net.minecraft.world.item.ItemInstance;
|
| 28 | import net.minecraft.world.item.ItemStack;
|
| 29 | import net.minecraft.world.level.Level;
|
| 30 | import net.minecraft.world.level.block.VaultBlock;
|
| 31 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 32 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 33 | import net.minecraft.world.level.block.state.BlockState;
|
| 34 | import net.minecraft.world.level.storage.ValueInput;
|
| 35 | import net.minecraft.world.level.storage.ValueOutput;
|
| 36 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 37 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 38 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 39 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 40 | import net.minecraft.world.phys.Vec3;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 |
|
| 43 | public class VaultBlockEntity extends BlockEntity {
|
| 44 | private final VaultServerData serverData = new VaultServerData();
|
| 45 | private final VaultSharedData sharedData = new VaultSharedData();
|
| 46 | private final VaultClientData clientData = new VaultClientData();
|
| 47 | private VaultConfig config = VaultConfig.DEFAULT;
|
| 48 |
|
| 49 | public VaultBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 50 | super(BlockEntityTypes.VAULT, worldPosition, blockState);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public @Nullable Packet<ClientGamePacketListener> getUpdatePacket() {
|
| 55 | return ClientboundBlockEntityDataPacket.create(this);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 60 | return Util.make(
|
| 61 | new CompoundTag(), tag -> tag.store("shared_data", VaultSharedData.CODEC, registries.createSerializationContext(NbtOps.INSTANCE), this.sharedData)
|
| 62 | );
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void saveAdditional(final ValueOutput output) {
|
| 67 | super.saveAdditional(output);
|
| 68 | output.store("config", VaultConfig.CODEC, this.config);
|
| 69 | output.store("shared_data", VaultSharedData.CODEC, this.sharedData);
|
| 70 | output.store("server_data", VaultServerData.CODEC, this.serverData);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void loadAdditional(final ValueInput input) {
|
| 75 | super.loadAdditional(input);
|
| 76 | input.read("server_data", VaultServerData.CODEC).ifPresent(this.serverData::set);
|
| 77 | this.config = input.read("config", VaultConfig.CODEC).orElse(VaultConfig.DEFAULT);
|
| 78 | input.read("shared_data", VaultSharedData.CODEC).ifPresent(this.sharedData::set);
|
| 79 | }
|
| 80 |
|
| 81 | public @Nullable VaultServerData getServerData() {
|
| 82 | return this.level != null && !this.level.isClientSide() ? this.serverData : null;
|
| 83 | }
|
| 84 |
|
| 85 | public VaultSharedData getSharedData() {
|
| 86 | return this.sharedData;
|
| 87 | }
|
| 88 |
|
| 89 | public VaultClientData getClientData() {
|
| 90 | return this.clientData;
|
| 91 | }
|
| 92 |
|
| 93 | public VaultConfig getConfig() {
|
| 94 | return this.config;
|
| 95 | }
|
| 96 |
|
| 97 | @VisibleForTesting
|
| 98 | public void setConfig(final VaultConfig config) {
|
| 99 | this.config = config;
|
| 100 | }
|
| 101 |
|
| 102 | public static final class Client {
|
| 103 | private static final int PARTICLE_TICK_RATE = 20;
|
| 104 | private static final float IDLE_PARTICLE_CHANCE = 0.5F;
|
| 105 | private static final float AMBIENT_SOUND_CHANCE = 0.02F;
|
| 106 | private static final int ACTIVATION_PARTICLE_COUNT = 20;
|
| 107 | private static final int DEACTIVATION_PARTICLE_COUNT = 20;
|
| 108 |
|
| 109 | public static void tick(
|
| 110 | final Level clientLevel, final BlockPos pos, final BlockState blockState, final VaultClientData clientData, final VaultSharedData sharedData
|
| 111 | ) {
|
| 112 | clientData.updateDisplayItemSpin();
|
| 113 | if (clientLevel.getGameTime() % 20L == 0L) {
|
| 114 | emitConnectionParticlesForNearbyPlayers(clientLevel, pos, blockState, sharedData);
|
| 115 | }
|
| 116 |
|
| 117 | emitIdleParticles(clientLevel, pos, sharedData, blockState.getValue(VaultBlock.OMINOUS) ? ParticleTypes.SOUL_FIRE_FLAME : ParticleTypes.SMALL_FLAME);
|
| 118 | playIdleSounds(clientLevel, pos, sharedData);
|
| 119 | }
|
| 120 |
|
| 121 | public static void emitActivationParticles(
|
| 122 | final Level clientLevel, final BlockPos pos, final BlockState blockState, final VaultSharedData sharedData, final ParticleOptions flameParticle
|
| 123 | ) {
|
| 124 | emitConnectionParticlesForNearbyPlayers(clientLevel, pos, blockState, sharedData);
|
| 125 | RandomSource random = clientLevel.getRandom();
|
| 126 |
|
| 127 | for (int i = 0; i < 20; i++) {
|
| 128 | Vec3 particlePos = randomPosInsideCage(pos, random);
|
| 129 | clientLevel.addParticle(ParticleTypes.SMOKE, particlePos.x(), particlePos.y(), particlePos.z(), 0.0, 0.0, 0.0);
|
| 130 | clientLevel.addParticle(flameParticle, particlePos.x(), particlePos.y(), particlePos.z(), 0.0, 0.0, 0.0);
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | public static void emitDeactivationParticles(final Level clientLevel, final BlockPos pos, final ParticleOptions flameParticle) {
|
| 135 | RandomSource random = clientLevel.getRandom();
|
| 136 |
|
| 137 | for (int i = 0; i < 20; i++) {
|
| 138 | Vec3 particlePos = randomPosCenterOfCage(pos, random);
|
| 139 | Vec3 dir = new Vec3(random.nextGaussian() * 0.02, random.nextGaussian() * 0.02, random.nextGaussian() * 0.02);
|
| 140 | clientLevel.addParticle(flameParticle, particlePos.x(), particlePos.y(), particlePos.z(), dir.x(), dir.y(), dir.z());
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | private static void emitIdleParticles(final Level clientLevel, final BlockPos pos, final VaultSharedData sharedData, final ParticleOptions flameParticle) {
|
| 145 | RandomSource random = clientLevel.getRandom();
|
| 146 | if (random.nextFloat() <= 0.5F) {
|
| 147 | Vec3 particlePos = randomPosInsideCage(pos, random);
|
| 148 | clientLevel.addParticle(ParticleTypes.SMOKE, particlePos.x(), particlePos.y(), particlePos.z(), 0.0, 0.0, 0.0);
|
| 149 | if (shouldDisplayActiveEffects(sharedData)) {
|
| 150 | clientLevel.addParticle(flameParticle, particlePos.x(), particlePos.y(), particlePos.z(), 0.0, 0.0, 0.0);
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | private static void emitConnectionParticlesForPlayer(final Level level, final Vec3 flyTowards, final Player player) {
|
| 156 | RandomSource random = level.getRandom();
|
| 157 | Vec3 direction = flyTowards.vectorTo(player.position().add(0.0, player.getBbHeight() / 2.0F, 0.0));
|
| 158 | int particleCount = Mth.nextInt(random, 2, 5);
|
| 159 |
|
| 160 | for (int i = 0; i < particleCount; i++) {
|
| 161 | Vec3 randomDirection = direction.offsetRandom(random, 1.0F);
|
| 162 | level.addParticle(
|
| 163 | ParticleTypes.VAULT_CONNECTION, flyTowards.x(), flyTowards.y(), flyTowards.z(), randomDirection.x(), randomDirection.y(), randomDirection.z()
|
| 164 | );
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | private static void emitConnectionParticlesForNearbyPlayers(
|
| 169 | final Level level, final BlockPos pos, final BlockState blockState, final VaultSharedData sharedData
|
| 170 | ) {
|
| 171 | Set<UUID> connectedPlayers = sharedData.getConnectedPlayers();
|
| 172 | if (!connectedPlayers.isEmpty()) {
|
| 173 | Vec3 keyholePos = keyholePos(pos, blockState.getValue(VaultBlock.FACING));
|
| 174 |
|
| 175 | for (UUID uuid : connectedPlayers) {
|
| 176 | Player player = level.getPlayerByUUID(uuid);
|
| 177 | if (player != null && isWithinConnectionRange(pos, sharedData, player)) {
|
| 178 | emitConnectionParticlesForPlayer(level, keyholePos, player);
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | private static boolean isWithinConnectionRange(final BlockPos vaultPos, final VaultSharedData sharedData, final Player player) {
|
| 185 | return player.blockPosition().distSqr(vaultPos) <= Mth.square(sharedData.connectedParticlesRange());
|
| 186 | }
|
| 187 |
|
| 188 | private static void playIdleSounds(final Level clientLevel, final BlockPos pos, final VaultSharedData sharedData) {
|
| 189 | if (shouldDisplayActiveEffects(sharedData)) {
|
| 190 | RandomSource random = clientLevel.getRandom();
|
| 191 | if (random.nextFloat() <= 0.02F) {
|
| 192 | clientLevel.playLocalSound(pos, SoundEvents.VAULT_AMBIENT, SoundSource.BLOCKS, random.nextFloat() * 0.25F + 0.75F, random.nextFloat() + 0.5F, false);
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | public static boolean shouldDisplayActiveEffects(final VaultSharedData sharedData) {
|
| 198 | return sharedData.hasDisplayItem();
|
| 199 | }
|
| 200 |
|
| 201 | private static Vec3 randomPosCenterOfCage(final BlockPos blockPos, final RandomSource random) {
|
| 202 | return Vec3.atLowerCornerOf(blockPos).add(Mth.nextDouble(random, 0.4, 0.6), Mth.nextDouble(random, 0.4, 0.6), Mth.nextDouble(random, 0.4, 0.6));
|
| 203 | }
|
| 204 |
|
| 205 | private static Vec3 randomPosInsideCage(final BlockPos blockPos, final RandomSource random) {
|
| 206 | return Vec3.atLowerCornerOf(blockPos).add(Mth.nextDouble(random, 0.1, 0.9), Mth.nextDouble(random, 0.25, 0.75), Mth.nextDouble(random, 0.1, 0.9));
|
| 207 | }
|
| 208 |
|
| 209 | private static Vec3 keyholePos(final BlockPos blockPos, final Direction blockFacing) {
|
| 210 | return Vec3.atBottomCenterOf(blockPos).add(blockFacing.getStepX() * 0.5, 1.75, blockFacing.getStepZ() * 0.5);
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public static final class Server {
|
| 215 | private static final int UNLOCKING_DELAY_TICKS = 14;
|
| 216 | private static final int DISPLAY_CYCLE_TICK_RATE = 20;
|
| 217 | private static final int INSERT_FAIL_SOUND_BUFFER_TICKS = 15;
|
| 218 |
|
| 219 | public static void tick(
|
| 220 | final ServerLevel serverLevel,
|
| 221 | final BlockPos pos,
|
| 222 | final BlockState blockState,
|
| 223 | final VaultConfig config,
|
| 224 | final VaultServerData serverData,
|
| 225 | final VaultSharedData sharedData
|
| 226 | ) {
|
| 227 | VaultState currentState = blockState.getValue(VaultBlock.STATE);
|
| 228 | if (shouldCycleDisplayItem(serverLevel.getGameTime(), currentState)) {
|
| 229 | cycleDisplayItemFromLootTable(serverLevel, currentState, config, sharedData, pos);
|
| 230 | }
|
| 231 |
|
| 232 | BlockState nextBlockState = blockState;
|
| 233 | if (serverLevel.getGameTime() >= serverData.stateUpdatingResumesAt()) {
|
| 234 | nextBlockState = nextBlockState.setValue(VaultBlock.STATE, currentState.tickAndGetNext(serverLevel, pos, config, serverData, sharedData));
|
| 235 | if (blockState != nextBlockState) {
|
| 236 | setVaultState(serverLevel, pos, blockState, nextBlockState, config, sharedData);
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | if (serverData.isDirty || sharedData.isDirty) {
|
| 241 | VaultBlockEntity.setChanged(serverLevel, pos, blockState);
|
| 242 | if (sharedData.isDirty) {
|
| 243 | serverLevel.sendBlockUpdated(pos, blockState, nextBlockState, 2);
|
| 244 | }
|
| 245 |
|
| 246 | serverData.isDirty = false;
|
| 247 | sharedData.isDirty = false;
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | public static void tryInsertKey(
|
| 252 | final ServerLevel serverLevel,
|
| 253 | final BlockPos pos,
|
| 254 | final BlockState blockState,
|
| 255 | final VaultConfig config,
|
| 256 | final VaultServerData serverData,
|
| 257 | final VaultSharedData sharedData,
|
| 258 | final Player player,
|
| 259 | final ItemStack stackToInsert
|
| 260 | ) {
|
| 261 | VaultState vaultState = blockState.getValue(VaultBlock.STATE);
|
| 262 | if (canEjectReward(config, vaultState)) {
|
| 263 | if (!isValidToInsert(config, stackToInsert)) {
|
| 264 | playInsertFailSound(serverLevel, serverData, pos, SoundEvents.VAULT_INSERT_ITEM_FAIL);
|
| 265 | } else if (serverData.hasRewardedPlayer(player)) {
|
| 266 | playInsertFailSound(serverLevel, serverData, pos, SoundEvents.VAULT_REJECT_REWARDED_PLAYER);
|
| 267 | } else {
|
| 268 | List<ItemStack> itemsToEject = resolveItemsToEject(serverLevel, config, pos, player, stackToInsert);
|
| 269 | if (!itemsToEject.isEmpty()) {
|
| 270 | player.awardStat(Stats.ITEM_USED.get(stackToInsert.getItem()));
|
| 271 | stackToInsert.consume(config.keyItem().getCount(), player);
|
| 272 | unlock(serverLevel, blockState, pos, config, serverData, sharedData, itemsToEject);
|
| 273 | serverData.addToRewardedPlayers(player);
|
| 274 | sharedData.updateConnectedPlayersWithinRange(serverLevel, pos, serverData, config, config.deactivationRange());
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | private static void setVaultState(
|
| 281 | final ServerLevel serverLevel,
|
| 282 | final BlockPos pos,
|
| 283 | final BlockState currentBlockState,
|
| 284 | final BlockState newBlockState,
|
| 285 | final VaultConfig config,
|
| 286 | final VaultSharedData sharedData
|
| 287 | ) {
|
| 288 | VaultState currentVaultState = currentBlockState.getValue(VaultBlock.STATE);
|
| 289 | VaultState newVaultState = newBlockState.getValue(VaultBlock.STATE);
|
| 290 | serverLevel.setBlockAndUpdate(pos, newBlockState);
|
| 291 | currentVaultState.onTransition(serverLevel, pos, newVaultState, config, sharedData, newBlockState.getValue(VaultBlock.OMINOUS));
|
| 292 | }
|
| 293 |
|
| 294 | static void cycleDisplayItemFromLootTable(
|
| 295 | final ServerLevel serverLevel, final VaultState vaultState, final VaultConfig config, final VaultSharedData sharedData, final BlockPos pos
|
| 296 | ) {
|
| 297 | if (!canEjectReward(config, vaultState)) {
|
| 298 | sharedData.setDisplayItem(ItemStack.EMPTY);
|
| 299 | } else {
|
| 300 | ItemStack displayItem = getRandomDisplayItemFromLootTable(serverLevel, pos, config.overrideLootTableToDisplay().orElse(config.lootTable()));
|
| 301 | sharedData.setDisplayItem(displayItem);
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | private static ItemStack getRandomDisplayItemFromLootTable(final ServerLevel serverLevel, final BlockPos pos, final ResourceKey<LootTable> lootTableId) {
|
| 306 | LootTable lootTable = serverLevel.getServer().reloadableRegistries().getLootTable(lootTableId);
|
| 307 | LootParams params = new LootParams.Builder(serverLevel).withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos)).create(LootContextParamSets.VAULT);
|
| 308 | List<ItemStack> results = lootTable.getRandomItems(params, serverLevel.getRandom());
|
| 309 | return results.isEmpty() ? ItemStack.EMPTY : Util.getRandom(results, serverLevel.getRandom());
|
| 310 | }
|
| 311 |
|
| 312 | private static void unlock(
|
| 313 | final ServerLevel serverLevel,
|
| 314 | final BlockState blockState,
|
| 315 | final BlockPos pos,
|
| 316 | final VaultConfig config,
|
| 317 | final VaultServerData serverData,
|
| 318 | final VaultSharedData sharedData,
|
| 319 | final List<ItemStack> itemsToEject
|
| 320 | ) {
|
| 321 | serverData.setItemsToEject(itemsToEject);
|
| 322 | sharedData.setDisplayItem(serverData.getNextItemToEject());
|
| 323 | serverData.pauseStateUpdatingUntil(serverLevel.getGameTime() + 14L);
|
| 324 | setVaultState(serverLevel, pos, blockState, blockState.setValue(VaultBlock.STATE, VaultState.UNLOCKING), config, sharedData);
|
| 325 | }
|
| 326 |
|
| 327 | private static List<ItemStack> resolveItemsToEject(
|
| 328 | final ServerLevel serverLevel, final VaultConfig config, final BlockPos pos, final Player player, final ItemInstance insertedStack
|
| 329 | ) {
|
| 330 | LootTable lootTable = serverLevel.getServer().reloadableRegistries().getLootTable(config.lootTable());
|
| 331 | LootParams params = new LootParams.Builder(serverLevel)
|
| 332 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
| 333 | .withLuck(player.getLuck())
|
| 334 | .withParameter(LootContextParams.THIS_ENTITY, player)
|
| 335 | .withParameter(LootContextParams.TOOL, insertedStack)
|
| 336 | .create(LootContextParamSets.VAULT);
|
| 337 | return lootTable.getRandomItems(params);
|
| 338 | }
|
| 339 |
|
| 340 | private static boolean canEjectReward(final VaultConfig config, final VaultState vaultState) {
|
| 341 | return !config.keyItem().isEmpty() && vaultState != VaultState.INACTIVE;
|
| 342 | }
|
| 343 |
|
| 344 | private static boolean isValidToInsert(final VaultConfig config, final ItemStack stackToInsert) {
|
| 345 | return ItemStack.isSameItemSameComponents(stackToInsert, config.keyItem()) && stackToInsert.getCount() >= config.keyItem().getCount();
|
| 346 | }
|
| 347 |
|
| 348 | private static boolean shouldCycleDisplayItem(final long gameTime, final VaultState vaultState) {
|
| 349 | return gameTime % 20L == 0L && vaultState == VaultState.ACTIVE;
|
| 350 | }
|
| 351 |
|
| 352 | private static void playInsertFailSound(final ServerLevel serverLevel, final VaultServerData serverData, final BlockPos pos, final SoundEvent sound) {
|
| 353 | if (serverLevel.getGameTime() >= serverData.getLastInsertFailTimestamp() + 15L) {
|
| 354 | serverLevel.playSound(null, pos, sound, SoundSource.BLOCKS);
|
| 355 | serverData.setLastInsertFailTimestamp(serverLevel.getGameTime());
|
| 356 | }
|
| 357 | }
|
| 358 | }
|
| 359 | }
|
| 360 |
|