| 1 | package net.minecraft.world.level.block.entity.trialspawner;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 10 | import net.minecraft.nbt.CompoundTag;
|
| 11 | import net.minecraft.resources.ResourceKey;
|
| 12 | import net.minecraft.util.random.WeightedList;
|
| 13 | import net.minecraft.world.entity.EntityType;
|
| 14 | import net.minecraft.world.item.component.TypedEntityData;
|
| 15 | import net.minecraft.world.level.SpawnData;
|
| 16 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 17 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 18 |
|
| 19 | public record TrialSpawnerConfig(
|
| 20 | int spawnRange,
|
| 21 | float totalMobs,
|
| 22 | float simultaneousMobs,
|
| 23 | float totalMobsAddedPerPlayer,
|
| 24 | float simultaneousMobsAddedPerPlayer,
|
| 25 | int ticksBetweenSpawn,
|
| 26 | WeightedList<SpawnData> spawnPotentialsDefinition,
|
| 27 | WeightedList<ResourceKey<LootTable>> lootTablesToEject,
|
| 28 | ResourceKey<LootTable> itemsToDropWhenOminous
|
| 29 | ) {
|
| 30 | public static final TrialSpawnerConfig DEFAULT = builder().build();
|
| 31 | public static final Codec<TrialSpawnerConfig> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 32 | i -> i.group(
|
| 33 | Codec.intRange(1, 128).optionalFieldOf("spawn_range", DEFAULT.spawnRange).forGetter(TrialSpawnerConfig::spawnRange),
|
| 34 | Codec.floatRange(0.0F, Float.MAX_VALUE).optionalFieldOf("total_mobs", DEFAULT.totalMobs).forGetter(TrialSpawnerConfig::totalMobs),
|
| 35 | Codec.floatRange(0.0F, Float.MAX_VALUE).optionalFieldOf("simultaneous_mobs", DEFAULT.simultaneousMobs).forGetter(TrialSpawnerConfig::simultaneousMobs),
|
| 36 | Codec.floatRange(0.0F, Float.MAX_VALUE)
|
| 37 | .optionalFieldOf("total_mobs_added_per_player", DEFAULT.totalMobsAddedPerPlayer)
|
| 38 | .forGetter(TrialSpawnerConfig::totalMobsAddedPerPlayer),
|
| 39 | Codec.floatRange(0.0F, Float.MAX_VALUE)
|
| 40 | .optionalFieldOf("simultaneous_mobs_added_per_player", DEFAULT.simultaneousMobsAddedPerPlayer)
|
| 41 | .forGetter(TrialSpawnerConfig::simultaneousMobsAddedPerPlayer),
|
| 42 | Codec.intRange(0, Integer.MAX_VALUE).optionalFieldOf("ticks_between_spawn", DEFAULT.ticksBetweenSpawn).forGetter(TrialSpawnerConfig::ticksBetweenSpawn),
|
| 43 | SpawnData.LIST_CODEC.optionalFieldOf("spawn_potentials", WeightedList.of()).forGetter(TrialSpawnerConfig::spawnPotentialsDefinition),
|
| 44 | WeightedList.codec(LootTable.KEY_CODEC).optionalFieldOf("loot_tables_to_eject", DEFAULT.lootTablesToEject).forGetter(TrialSpawnerConfig::lootTablesToEject),
|
| 45 | LootTable.KEY_CODEC.optionalFieldOf("items_to_drop_when_ominous", DEFAULT.itemsToDropWhenOminous).forGetter(TrialSpawnerConfig::itemsToDropWhenOminous)
|
| 46 | )
|
| 47 | .apply(i, TrialSpawnerConfig::new)
|
| 48 | );
|
| 49 | public static final Codec<Holder<TrialSpawnerConfig>> CODEC = RegistryCodecs.holder(Registries.TRIAL_SPAWNER_CONFIG, DIRECT_CODEC);
|
| 50 |
|
| 51 | public int calculateTargetTotalMobs(final int additionalPlayers) {
|
| 52 | return (int)Math.floor(this.totalMobs + this.totalMobsAddedPerPlayer * additionalPlayers);
|
| 53 | }
|
| 54 |
|
| 55 | public int calculateTargetSimultaneousMobs(final int additionalPlayers) {
|
| 56 | return (int)Math.floor(this.simultaneousMobs + this.simultaneousMobsAddedPerPlayer * additionalPlayers);
|
| 57 | }
|
| 58 |
|
| 59 | public long ticksBetweenItemSpawners() {
|
| 60 | return 160L;
|
| 61 | }
|
| 62 |
|
| 63 | public static TrialSpawnerConfig.Builder builder() {
|
| 64 | return new TrialSpawnerConfig.Builder();
|
| 65 | }
|
| 66 |
|
| 67 | public TrialSpawnerConfig withSpawning(final EntityType<?> type) {
|
| 68 | CompoundTag tag = new CompoundTag();
|
| 69 | tag.putString("id", BuiltInRegistries.ENTITY_TYPE.getKey(type).toString());
|
| 70 | SpawnData spawnData = new SpawnData(tag, Optional.empty(), Optional.empty());
|
| 71 | return new TrialSpawnerConfig(
|
| 72 | this.spawnRange,
|
| 73 | this.totalMobs,
|
| 74 | this.simultaneousMobs,
|
| 75 | this.totalMobsAddedPerPlayer,
|
| 76 | this.simultaneousMobsAddedPerPlayer,
|
| 77 | this.ticksBetweenSpawn,
|
| 78 | WeightedList.of(spawnData),
|
| 79 | this.lootTablesToEject,
|
| 80 | this.itemsToDropWhenOminous
|
| 81 | );
|
| 82 | }
|
| 83 |
|
| 84 | public TrialSpawnerConfig withSpawning(final TypedEntityData<EntityType<?>> entityData) {
|
| 85 | CompoundTag tag = new CompoundTag();
|
| 86 | SpawnData spawnData = new SpawnData(tag, Optional.empty(), Optional.empty());
|
| 87 | entityData.loadInto(spawnData, BuiltInRegistries.ENTITY_TYPE);
|
| 88 | return new TrialSpawnerConfig(
|
| 89 | this.spawnRange,
|
| 90 | this.totalMobs,
|
| 91 | this.simultaneousMobs,
|
| 92 | this.totalMobsAddedPerPlayer,
|
| 93 | this.simultaneousMobsAddedPerPlayer,
|
| 94 | this.ticksBetweenSpawn,
|
| 95 | WeightedList.of(spawnData),
|
| 96 | this.lootTablesToEject,
|
| 97 | this.itemsToDropWhenOminous
|
| 98 | );
|
| 99 | }
|
| 100 |
|
| 101 | public static class Builder {
|
| 102 | private int spawnRange = 4;
|
| 103 | private float totalMobs = 6.0F;
|
| 104 | private float simultaneousMobs = 2.0F;
|
| 105 | private float totalMobsAddedPerPlayer = 2.0F;
|
| 106 | private float simultaneousMobsAddedPerPlayer = 1.0F;
|
| 107 | private int ticksBetweenSpawn = 40;
|
| 108 | private WeightedList<SpawnData> spawnPotentialsDefinition = WeightedList.of();
|
| 109 | private WeightedList<ResourceKey<LootTable>> lootTablesToEject = WeightedList.<ResourceKey<LootTable>>builder()
|
| 110 | .add(BuiltInLootTables.SPAWNER_TRIAL_CHAMBER_CONSUMABLES)
|
| 111 | .add(BuiltInLootTables.SPAWNER_TRIAL_CHAMBER_KEY)
|
| 112 | .build();
|
| 113 | private ResourceKey<LootTable> itemsToDropWhenOminous = BuiltInLootTables.SPAWNER_TRIAL_ITEMS_TO_DROP_WHEN_OMINOUS;
|
| 114 |
|
| 115 | public TrialSpawnerConfig.Builder spawnRange(final int spawnRange) {
|
| 116 | this.spawnRange = spawnRange;
|
| 117 | return this;
|
| 118 | }
|
| 119 |
|
| 120 | public TrialSpawnerConfig.Builder totalMobs(final float totalMobs) {
|
| 121 | this.totalMobs = totalMobs;
|
| 122 | return this;
|
| 123 | }
|
| 124 |
|
| 125 | public TrialSpawnerConfig.Builder simultaneousMobs(final float simultaneousMobs) {
|
| 126 | this.simultaneousMobs = simultaneousMobs;
|
| 127 | return this;
|
| 128 | }
|
| 129 |
|
| 130 | public TrialSpawnerConfig.Builder totalMobsAddedPerPlayer(final float totalMobsAddedPerPlayer) {
|
| 131 | this.totalMobsAddedPerPlayer = totalMobsAddedPerPlayer;
|
| 132 | return this;
|
| 133 | }
|
| 134 |
|
| 135 | public TrialSpawnerConfig.Builder simultaneousMobsAddedPerPlayer(final float simultaneousMobsAddedPerPlayer) {
|
| 136 | this.simultaneousMobsAddedPerPlayer = simultaneousMobsAddedPerPlayer;
|
| 137 | return this;
|
| 138 | }
|
| 139 |
|
| 140 | public TrialSpawnerConfig.Builder ticksBetweenSpawn(final int ticksBetweenSpawn) {
|
| 141 | this.ticksBetweenSpawn = ticksBetweenSpawn;
|
| 142 | return this;
|
| 143 | }
|
| 144 |
|
| 145 | public TrialSpawnerConfig.Builder spawnPotentialsDefinition(final WeightedList<SpawnData> spawnPotentialsDefinition) {
|
| 146 | this.spawnPotentialsDefinition = spawnPotentialsDefinition;
|
| 147 | return this;
|
| 148 | }
|
| 149 |
|
| 150 | public TrialSpawnerConfig.Builder lootTablesToEject(final WeightedList<ResourceKey<LootTable>> lootTablesToEject) {
|
| 151 | this.lootTablesToEject = lootTablesToEject;
|
| 152 | return this;
|
| 153 | }
|
| 154 |
|
| 155 | public TrialSpawnerConfig.Builder itemsToDropWhenOminous(final ResourceKey<LootTable> itemsToDropWhenOminous) {
|
| 156 | this.itemsToDropWhenOminous = itemsToDropWhenOminous;
|
| 157 | return this;
|
| 158 | }
|
| 159 |
|
| 160 | public TrialSpawnerConfig build() {
|
| 161 | return new TrialSpawnerConfig(
|
| 162 | this.spawnRange,
|
| 163 | this.totalMobs,
|
| 164 | this.simultaneousMobs,
|
| 165 | this.totalMobsAddedPerPlayer,
|
| 166 | this.simultaneousMobsAddedPerPlayer,
|
| 167 | this.ticksBetweenSpawn,
|
| 168 | this.spawnPotentialsDefinition,
|
| 169 | this.lootTablesToEject,
|
| 170 | this.itemsToDropWhenOminous
|
| 171 | );
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|