| 1 | package net.minecraft.world.level.block.entity.vault;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.SharedConstants;
|
| 8 | import net.minecraft.resources.ResourceKey;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.Items;
|
| 11 | import net.minecraft.world.level.block.entity.trialspawner.PlayerDetector;
|
| 12 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 13 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 14 |
|
| 15 | public record VaultConfig(
|
| 16 | ResourceKey<LootTable> lootTable,
|
| 17 | double activationRange,
|
| 18 | double deactivationRange,
|
| 19 | ItemStack keyItem,
|
| 20 | Optional<ResourceKey<LootTable>> overrideLootTableToDisplay,
|
| 21 | PlayerDetector playerDetector,
|
| 22 | PlayerDetector.EntitySelector entitySelector
|
| 23 | ) {
|
| 24 | public static final String TAG_NAME = "config";
|
| 25 | public static final VaultConfig DEFAULT = new VaultConfig();
|
| 26 | public static final Codec<VaultConfig> CODEC = RecordCodecBuilder.<VaultConfig>create(
|
| 27 | i -> i.group(
|
| 28 | LootTable.KEY_CODEC.lenientOptionalFieldOf("loot_table", DEFAULT.lootTable()).forGetter(VaultConfig::lootTable),
|
| 29 | Codec.DOUBLE.lenientOptionalFieldOf("activation_range", DEFAULT.activationRange()).forGetter(VaultConfig::activationRange),
|
| 30 | Codec.DOUBLE.lenientOptionalFieldOf("deactivation_range", DEFAULT.deactivationRange()).forGetter(VaultConfig::deactivationRange),
|
| 31 | ItemStack.lenientOptionalFieldOf("key_item").forGetter(VaultConfig::keyItem),
|
| 32 | LootTable.KEY_CODEC.lenientOptionalFieldOf("override_loot_table_to_display").forGetter(VaultConfig::overrideLootTableToDisplay)
|
| 33 | )
|
| 34 | .apply(i, VaultConfig::new)
|
| 35 | )
|
| 36 | .validate(VaultConfig::validate);
|
| 37 |
|
| 38 | private VaultConfig() {
|
| 39 | this(
|
| 40 | BuiltInLootTables.TRIAL_CHAMBERS_REWARD,
|
| 41 | 4.0,
|
| 42 | 4.5,
|
| 43 | new ItemStack(Items.TRIAL_KEY),
|
| 44 | Optional.empty(),
|
| 45 | PlayerDetector.INCLUDING_CREATIVE_PLAYERS,
|
| 46 | PlayerDetector.EntitySelector.SELECT_FROM_LEVEL
|
| 47 | );
|
| 48 | }
|
| 49 |
|
| 50 | public VaultConfig(
|
| 51 | final ResourceKey<LootTable> lootTable,
|
| 52 | final double activationRange,
|
| 53 | final double deactivationRange,
|
| 54 | final ItemStack keyItem,
|
| 55 | final Optional<ResourceKey<LootTable>> overrideDisplayItems
|
| 56 | ) {
|
| 57 | this(lootTable, activationRange, deactivationRange, keyItem, overrideDisplayItems, DEFAULT.playerDetector(), DEFAULT.entitySelector());
|
| 58 | }
|
| 59 |
|
| 60 | public PlayerDetector playerDetector() {
|
| 61 | return SharedConstants.DEBUG_VAULT_DETECTS_SHEEP_AS_PLAYERS ? PlayerDetector.SHEEP : this.playerDetector;
|
| 62 | }
|
| 63 |
|
| 64 | private DataResult<VaultConfig> validate() {
|
| 65 | return this.activationRange > this.deactivationRange
|
| 66 | ? DataResult.error(() -> "Activation range must (" + this.activationRange + ") be less or equal to deactivation range (" + this.deactivationRange + ")")
|
| 67 | : DataResult.success(this);
|
| 68 | }
|
| 69 | }
|
| 70 |
|