| 1 | package net.minecraft.world.level.block.entity.vault;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.util.StringRepresentable;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public enum VaultState implements StringRepresentable {
|
| 14 | INACTIVE("inactive", VaultState.LightLevel.HALF_LIT) {
|
| 15 | @Override
|
| 16 | protected void onEnter(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous) {
|
| 17 | sharedData.setDisplayItem(ItemStack.EMPTY);
|
| 18 | serverLevel.levelEvent(3016, pos, isOminous ? 1 : 0);
|
| 19 | }
|
| 20 | },
|
| 21 | ACTIVE("active", VaultState.LightLevel.LIT) {
|
| 22 | @Override
|
| 23 | protected void onEnter(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous) {
|
| 24 | if (!sharedData.hasDisplayItem()) {
|
| 25 | VaultBlockEntity.Server.cycleDisplayItemFromLootTable(serverLevel, this, config, sharedData, pos);
|
| 26 | }
|
| 27 |
|
| 28 | serverLevel.levelEvent(3015, pos, isOminous ? 1 : 0);
|
| 29 | }
|
| 30 | },
|
| 31 | UNLOCKING("unlocking", VaultState.LightLevel.LIT) {
|
| 32 | @Override
|
| 33 | protected void onEnter(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous) {
|
| 34 | serverLevel.playSound(null, pos, SoundEvents.VAULT_INSERT_ITEM, SoundSource.BLOCKS);
|
| 35 | }
|
| 36 | },
|
| 37 | EJECTING("ejecting", VaultState.LightLevel.LIT) {
|
| 38 | @Override
|
| 39 | protected void onEnter(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous) {
|
| 40 | serverLevel.playSound(null, pos, SoundEvents.VAULT_OPEN_SHUTTER, SoundSource.BLOCKS);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void onExit(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData) {
|
| 45 | serverLevel.playSound(null, pos, SoundEvents.VAULT_CLOSE_SHUTTER, SoundSource.BLOCKS);
|
| 46 | }
|
| 47 | };
|
| 48 |
|
| 49 | private static final int UPDATE_CONNECTED_PLAYERS_TICK_RATE = 20;
|
| 50 | private static final int DELAY_BETWEEN_EJECTIONS_TICKS = 20;
|
| 51 | private static final int DELAY_AFTER_LAST_EJECTION_TICKS = 20;
|
| 52 | private static final int DELAY_BEFORE_FIRST_EJECTION_TICKS = 20;
|
| 53 | private final String stateName;
|
| 54 | private final VaultState.LightLevel lightLevel;
|
| 55 |
|
| 56 | VaultState(final String stateName, final VaultState.LightLevel lightLevel) {
|
| 57 | this.stateName = stateName;
|
| 58 | this.lightLevel = lightLevel;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public String getSerializedName() {
|
| 63 | return this.stateName;
|
| 64 | }
|
| 65 |
|
| 66 | public int lightLevel() {
|
| 67 | return this.lightLevel.value;
|
| 68 | }
|
| 69 |
|
| 70 | public VaultState tickAndGetNext(
|
| 71 | final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultServerData serverData, final VaultSharedData sharedData
|
| 72 | ) {
|
| 73 | return switch (this) {
|
| 74 | case INACTIVE -> updateStateForConnectedPlayers(serverLevel, pos, config, serverData, sharedData, config.activationRange());
|
| 75 | case ACTIVE -> updateStateForConnectedPlayers(serverLevel, pos, config, serverData, sharedData, config.deactivationRange());
|
| 76 | case UNLOCKING -> {
|
| 77 | serverData.pauseStateUpdatingUntil(serverLevel.getGameTime() + 20L);
|
| 78 | yield EJECTING;
|
| 79 | }
|
| 80 | case EJECTING -> {
|
| 81 | if (serverData.getItemsToEject().isEmpty()) {
|
| 82 | serverData.markEjectionFinished();
|
| 83 | yield updateStateForConnectedPlayers(serverLevel, pos, config, serverData, sharedData, config.deactivationRange());
|
| 84 | } else {
|
| 85 | float ejectionSoundProgress = serverData.ejectionProgress();
|
| 86 | ejectResultItem(serverLevel, pos, serverData.popNextItemToEject(), ejectionSoundProgress);
|
| 87 | sharedData.setDisplayItem(serverData.getNextItemToEject());
|
| 88 | boolean isLastEjection = serverData.getItemsToEject().isEmpty();
|
| 89 | int ejectionDelay = isLastEjection ? 20 : 20;
|
| 90 | serverData.pauseStateUpdatingUntil(serverLevel.getGameTime() + ejectionDelay);
|
| 91 | yield EJECTING;
|
| 92 | }
|
| 93 | }
|
| 94 | };
|
| 95 | }
|
| 96 |
|
| 97 | private static VaultState updateStateForConnectedPlayers(
|
| 98 | final ServerLevel serverLevel,
|
| 99 | final BlockPos pos,
|
| 100 | final VaultConfig config,
|
| 101 | final VaultServerData serverData,
|
| 102 | final VaultSharedData sharedData,
|
| 103 | final double activationRange
|
| 104 | ) {
|
| 105 | sharedData.updateConnectedPlayersWithinRange(serverLevel, pos, serverData, config, activationRange);
|
| 106 | serverData.pauseStateUpdatingUntil(serverLevel.getGameTime() + 20L);
|
| 107 | return sharedData.hasConnectedPlayers() ? ACTIVE : INACTIVE;
|
| 108 | }
|
| 109 |
|
| 110 | public void onTransition(
|
| 111 | final ServerLevel serverLevel, final BlockPos pos, final VaultState to, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous
|
| 112 | ) {
|
| 113 | this.onExit(serverLevel, pos, config, sharedData);
|
| 114 | to.onEnter(serverLevel, pos, config, sharedData, isOminous);
|
| 115 | }
|
| 116 |
|
| 117 | protected void onEnter(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData, final boolean isOminous) {
|
| 118 | }
|
| 119 |
|
| 120 | protected void onExit(final ServerLevel serverLevel, final BlockPos pos, final VaultConfig config, final VaultSharedData sharedData) {
|
| 121 | }
|
| 122 |
|
| 123 | private static void ejectResultItem(final ServerLevel serverLevel, final BlockPos pos, final ItemStack itemToEject, final float ejectionSoundProgress) {
|
| 124 | DefaultDispenseItemBehavior.spawnItem(serverLevel, itemToEject, 2, Direction.UP, Vec3.atBottomCenterOf(pos).relative(Direction.UP, 1.2));
|
| 125 | serverLevel.levelEvent(3017, pos, 0);
|
| 126 | serverLevel.playSound(null, pos, SoundEvents.VAULT_EJECT_ITEM, SoundSource.BLOCKS, 1.0F, 0.8F + 0.4F * ejectionSoundProgress);
|
| 127 | }
|
| 128 |
|
| 129 | private enum LightLevel {
|
| 130 | HALF_LIT(6),
|
| 131 | LIT(12);
|
| 132 |
|
| 133 | private final int value;
|
| 134 |
|
| 135 | LightLevel(final int value) {
|
| 136 | this.value = value;
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|