| 1 | package net.minecraft.world.level.block.entity.vault;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.UUID;
|
| 13 | import net.minecraft.core.UUIDUtil;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 |
|
| 18 | public class VaultServerData {
|
| 19 | public static final String TAG_NAME = "server_data";
|
| 20 | public static final Codec<VaultServerData> CODEC = RecordCodecBuilder.create(
|
| 21 | i -> i.group(
|
| 22 | UUIDUtil.CODEC_LINKED_SET.lenientOptionalFieldOf("rewarded_players", Set.of()).forGetter(vault -> vault.rewardedPlayers),
|
| 23 | Codec.LONG.lenientOptionalFieldOf("state_updating_resumes_at", 0L).forGetter(vault -> vault.stateUpdatingResumesAt),
|
| 24 | ItemStack.CODEC.listOf().lenientOptionalFieldOf("items_to_eject", List.of()).forGetter(vault -> vault.itemsToEject),
|
| 25 | Codec.INT.lenientOptionalFieldOf("total_ejections_needed", 0).forGetter(vault -> vault.totalEjectionsNeeded)
|
| 26 | )
|
| 27 | .apply(i, VaultServerData::new)
|
| 28 | );
|
| 29 | private static final int MAX_REWARD_PLAYERS = 128;
|
| 30 | private final Set<UUID> rewardedPlayers = new ObjectLinkedOpenHashSet<>();
|
| 31 | private long stateUpdatingResumesAt;
|
| 32 | private final List<ItemStack> itemsToEject = new ObjectArrayList<>();
|
| 33 | private long lastInsertFailTimestamp;
|
| 34 | private int totalEjectionsNeeded;
|
| 35 | boolean isDirty;
|
| 36 |
|
| 37 | public VaultServerData(final Set<UUID> rewardedPlayers, final long stateUpdatingResumesAt, final List<ItemStack> itemsToEject, final int totalEjectionsNeeded) {
|
| 38 | this.rewardedPlayers.addAll(rewardedPlayers);
|
| 39 | this.stateUpdatingResumesAt = stateUpdatingResumesAt;
|
| 40 | this.itemsToEject.addAll(itemsToEject);
|
| 41 | this.totalEjectionsNeeded = totalEjectionsNeeded;
|
| 42 | }
|
| 43 |
|
| 44 | public VaultServerData() {
|
| 45 | }
|
| 46 |
|
| 47 | void setLastInsertFailTimestamp(final long lastInsertFailTimestamp) {
|
| 48 | this.lastInsertFailTimestamp = lastInsertFailTimestamp;
|
| 49 | }
|
| 50 |
|
| 51 | long getLastInsertFailTimestamp() {
|
| 52 | return this.lastInsertFailTimestamp;
|
| 53 | }
|
| 54 |
|
| 55 | Set<UUID> getRewardedPlayers() {
|
| 56 | return this.rewardedPlayers;
|
| 57 | }
|
| 58 |
|
| 59 | boolean hasRewardedPlayer(final Player player) {
|
| 60 | return this.rewardedPlayers.contains(player.getUUID());
|
| 61 | }
|
| 62 |
|
| 63 | @VisibleForTesting
|
| 64 | public void addToRewardedPlayers(final Player player) {
|
| 65 | this.rewardedPlayers.add(player.getUUID());
|
| 66 | if (this.rewardedPlayers.size() > 128) {
|
| 67 | Iterator<UUID> iterator = this.rewardedPlayers.iterator();
|
| 68 | if (iterator.hasNext()) {
|
| 69 | iterator.next();
|
| 70 | iterator.remove();
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | this.markChanged();
|
| 75 | }
|
| 76 |
|
| 77 | long stateUpdatingResumesAt() {
|
| 78 | return this.stateUpdatingResumesAt;
|
| 79 | }
|
| 80 |
|
| 81 | void pauseStateUpdatingUntil(final long stateUpdatingResumesAt) {
|
| 82 | this.stateUpdatingResumesAt = stateUpdatingResumesAt;
|
| 83 | this.markChanged();
|
| 84 | }
|
| 85 |
|
| 86 | List<ItemStack> getItemsToEject() {
|
| 87 | return this.itemsToEject;
|
| 88 | }
|
| 89 |
|
| 90 | void markEjectionFinished() {
|
| 91 | this.totalEjectionsNeeded = 0;
|
| 92 | this.markChanged();
|
| 93 | }
|
| 94 |
|
| 95 | void setItemsToEject(final List<ItemStack> newItemsToEject) {
|
| 96 | this.itemsToEject.clear();
|
| 97 | this.itemsToEject.addAll(newItemsToEject);
|
| 98 | this.totalEjectionsNeeded = this.itemsToEject.size();
|
| 99 | this.markChanged();
|
| 100 | }
|
| 101 |
|
| 102 | ItemStack getNextItemToEject() {
|
| 103 | return this.itemsToEject.isEmpty() ? ItemStack.EMPTY : Objects.requireNonNullElse(this.itemsToEject.get(this.itemsToEject.size() - 1), ItemStack.EMPTY);
|
| 104 | }
|
| 105 |
|
| 106 | ItemStack popNextItemToEject() {
|
| 107 | if (this.itemsToEject.isEmpty()) {
|
| 108 | return ItemStack.EMPTY;
|
| 109 | }
|
| 110 |
|
| 111 | this.markChanged();
|
| 112 | return Objects.requireNonNullElse(this.itemsToEject.remove(this.itemsToEject.size() - 1), ItemStack.EMPTY);
|
| 113 | }
|
| 114 |
|
| 115 | void set(final VaultServerData from) {
|
| 116 | this.stateUpdatingResumesAt = from.stateUpdatingResumesAt();
|
| 117 | this.itemsToEject.clear();
|
| 118 | this.itemsToEject.addAll(from.itemsToEject);
|
| 119 | this.rewardedPlayers.clear();
|
| 120 | this.rewardedPlayers.addAll(from.rewardedPlayers);
|
| 121 | }
|
| 122 |
|
| 123 | private void markChanged() {
|
| 124 | this.isDirty = true;
|
| 125 | }
|
| 126 |
|
| 127 | public float ejectionProgress() {
|
| 128 | return this.totalEjectionsNeeded == 1 ? 1.0F : 1.0F - Mth.inverseLerp(this.getItemsToEject().size(), 1.0F, this.totalEjectionsNeeded);
|
| 129 | }
|
| 130 | }
|
| 131 |
|