| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.Typed;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.datafixers.util.Pair;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import com.mojang.serialization.DynamicOps;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import net.minecraft.nbt.CompoundTag;
|
| 15 | import net.minecraft.nbt.NbtOps;
|
| 16 | import net.minecraft.nbt.Tag;
|
| 17 | import net.minecraft.nbt.TagParser;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | public class TrialSpawnerConfigInRegistryFix extends NamedEntityFix {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 |
|
| 24 | public TrialSpawnerConfigInRegistryFix(final Schema outputSchema) {
|
| 25 | super(outputSchema, false, "TrialSpawnerConfigInRegistryFix", References.BLOCK_ENTITY, "minecraft:trial_spawner");
|
| 26 | }
|
| 27 |
|
| 28 | public Dynamic<?> fixTag(final Dynamic<Tag> input) {
|
| 29 | Optional<Dynamic<Tag>> normalConfig = input.get("normal_config").result();
|
| 30 | if (normalConfig.isEmpty()) {
|
| 31 | return input;
|
| 32 | }
|
| 33 |
|
| 34 | Optional<Dynamic<Tag>> ominousConfig = input.get("ominous_config").result();
|
| 35 | if (ominousConfig.isEmpty()) {
|
| 36 | return input;
|
| 37 | }
|
| 38 |
|
| 39 | Identifier registryLocation = TrialSpawnerConfigInRegistryFix.VanillaTrialChambers.CONFIGS_TO_KEY.get(Pair.of(normalConfig.get(), ominousConfig.get()));
|
| 40 | return registryLocation == null
|
| 41 | ? input
|
| 42 | : input.set("normal_config", input.createString(registryLocation.withSuffix("/normal").toString()))
|
| 43 | .set("ominous_config", input.createString(registryLocation.withSuffix("/ominous").toString()));
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected Typed<?> fix(final Typed<?> entity) {
|
| 48 | return entity.update(DSL.remainderFinder(), input -> {
|
| 49 | DynamicOps<?> inputType = input.getOps();
|
| 50 | Dynamic<?> result = this.fixTag(input.convert(NbtOps.INSTANCE));
|
| 51 | return result.convert(inputType);
|
| 52 | });
|
| 53 | }
|
| 54 |
|
| 55 | private static final class VanillaTrialChambers {
|
| 56 | public static final Map<Pair<Dynamic<Tag>, Dynamic<Tag>>, Identifier> CONFIGS_TO_KEY = new HashMap<>();
|
| 57 |
|
| 58 | private static void register(final Identifier location, final String normalNbt, final String ominousNbt) {
|
| 59 | try {
|
| 60 | CompoundTag normalTag = parse(normalNbt);
|
| 61 | CompoundTag ominousTag = parse(ominousNbt);
|
| 62 | CompoundTag ominousMergedTag = normalTag.copy().merge(ominousTag);
|
| 63 | CompoundTag ominousMergedTagDefaultsOmitted = removeDefaults(ominousMergedTag.copy());
|
| 64 | Dynamic<Tag> dynamicNormal = asDynamic(normalTag);
|
| 65 | CONFIGS_TO_KEY.put(Pair.of(dynamicNormal, asDynamic(ominousTag)), location);
|
| 66 | CONFIGS_TO_KEY.put(Pair.of(dynamicNormal, asDynamic(ominousMergedTag)), location);
|
| 67 | CONFIGS_TO_KEY.put(Pair.of(dynamicNormal, asDynamic(ominousMergedTagDefaultsOmitted)), location);
|
| 68 | } catch (RuntimeException e) {
|
| 69 | throw new IllegalStateException("Failed to parse NBT for " + location, e);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | private static Dynamic<Tag> asDynamic(final CompoundTag normalTag) {
|
| 74 | return new Dynamic<>(NbtOps.INSTANCE, normalTag);
|
| 75 | }
|
| 76 |
|
| 77 | private static CompoundTag parse(final String nbt) {
|
| 78 | try {
|
| 79 | return TagParser.parseCompoundFully(nbt);
|
| 80 | } catch (CommandSyntaxException e) {
|
| 81 | throw new IllegalArgumentException("Failed to parse Trial Spawner NBT config: " + nbt, e);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | private static CompoundTag removeDefaults(final CompoundTag tag) {
|
| 86 | if (tag.getIntOr("spawn_range", 0) == 4) {
|
| 87 | tag.remove("spawn_range");
|
| 88 | }
|
| 89 |
|
| 90 | if (tag.getFloatOr("total_mobs", 0.0F) == 6.0F) {
|
| 91 | tag.remove("total_mobs");
|
| 92 | }
|
| 93 |
|
| 94 | if (tag.getFloatOr("simultaneous_mobs", 0.0F) == 2.0F) {
|
| 95 | tag.remove("simultaneous_mobs");
|
| 96 | }
|
| 97 |
|
| 98 | if (tag.getFloatOr("total_mobs_added_per_player", 0.0F) == 2.0F) {
|
| 99 | tag.remove("total_mobs_added_per_player");
|
| 100 | }
|
| 101 |
|
| 102 | if (tag.getFloatOr("simultaneous_mobs_added_per_player", 0.0F) == 1.0F) {
|
| 103 | tag.remove("simultaneous_mobs_added_per_player");
|
| 104 | }
|
| 105 |
|
| 106 | if (tag.getIntOr("ticks_between_spawn", 0) == 40) {
|
| 107 | tag.remove("ticks_between_spawn");
|
| 108 | }
|
| 109 |
|
| 110 | return tag;
|
| 111 | }
|
| 112 |
|
| 113 | static {
|
| 114 | register(
|
| 115 | Identifier.withDefaultNamespace("trial_chamber/breeze"),
|
| 116 | "{simultaneous_mobs: 1.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:breeze\"}}, weight: 1}], ticks_between_spawn: 20, total_mobs: 2.0f, total_mobs_added_per_player: 1.0f}",
|
| 117 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 2.0f, total_mobs: 4.0f}"
|
| 118 | );
|
| 119 | register(
|
| 120 | Identifier.withDefaultNamespace("trial_chamber/melee/husk"),
|
| 121 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:husk\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 122 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:husk\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 123 | );
|
| 124 | register(
|
| 125 | Identifier.withDefaultNamespace("trial_chamber/melee/spider"),
|
| 126 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:spider\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 127 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],simultaneous_mobs: 4.0f, total_mobs: 12.0f}"
|
| 128 | );
|
| 129 | register(
|
| 130 | Identifier.withDefaultNamespace("trial_chamber/melee/zombie"),
|
| 131 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:zombie\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 132 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:zombie\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 133 | );
|
| 134 | register(
|
| 135 | Identifier.withDefaultNamespace("trial_chamber/ranged/poison_skeleton"),
|
| 136 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 137 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 138 | );
|
| 139 | register(
|
| 140 | Identifier.withDefaultNamespace("trial_chamber/ranged/skeleton"),
|
| 141 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 142 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 143 | );
|
| 144 | register(
|
| 145 | Identifier.withDefaultNamespace("trial_chamber/ranged/stray"),
|
| 146 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 147 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 148 | );
|
| 149 | register(
|
| 150 | Identifier.withDefaultNamespace("trial_chamber/slow_ranged/poison_skeleton"),
|
| 151 | "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}}, weight: 1}], ticks_between_spawn: 160}",
|
| 152 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:bogged\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 153 | );
|
| 154 | register(
|
| 155 | Identifier.withDefaultNamespace("trial_chamber/slow_ranged/skeleton"),
|
| 156 | "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}}, weight: 1}], ticks_between_spawn: 160}",
|
| 157 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {id: \"minecraft:skeleton\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 158 | );
|
| 159 | register(
|
| 160 | Identifier.withDefaultNamespace("trial_chamber/slow_ranged/stray"),
|
| 161 | "{simultaneous_mobs: 4.0f, simultaneous_mobs_added_per_player: 2.0f, spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}}, weight: 1}], ticks_between_spawn: 160}",
|
| 162 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}],spawn_potentials: [{data: {entity: {id: \"minecraft:stray\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_ranged\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 163 | );
|
| 164 | register(
|
| 165 | Identifier.withDefaultNamespace("trial_chamber/small_melee/baby_zombie"),
|
| 166 | "{simultaneous_mobs: 2.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {IsBaby: 1b, id: \"minecraft:zombie\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 167 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], spawn_potentials: [{data: {entity: {IsBaby: 1b, id: \"minecraft:zombie\"}, equipment: {loot_table: \"minecraft:equipment/trial_chamber_melee\", slot_drop_chances: 0.0f}}, weight: 1}]}"
|
| 168 | );
|
| 169 | register(
|
| 170 | Identifier.withDefaultNamespace("trial_chamber/small_melee/cave_spider"),
|
| 171 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:cave_spider\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 172 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}"
|
| 173 | );
|
| 174 | register(
|
| 175 | Identifier.withDefaultNamespace("trial_chamber/small_melee/silverfish"),
|
| 176 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {id: \"minecraft:silverfish\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 177 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}"
|
| 178 | );
|
| 179 | register(
|
| 180 | Identifier.withDefaultNamespace("trial_chamber/small_melee/slime"),
|
| 181 | "{simultaneous_mobs: 3.0f, simultaneous_mobs_added_per_player: 0.5f, spawn_potentials: [{data: {entity: {Size: 1, id: \"minecraft:slime\"}}, weight: 3}, {data: {entity: {Size: 2, id: \"minecraft:slime\"}}, weight: 1}], ticks_between_spawn: 20}",
|
| 182 | "{loot_tables_to_eject: [{data: \"minecraft:spawners/ominous/trial_chamber/key\", weight: 3}, {data: \"minecraft:spawners/ominous/trial_chamber/consumables\", weight: 7}], simultaneous_mobs: 4.0f, total_mobs: 12.0f}"
|
| 183 | );
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|