MobEffectIdFix .java

net.minecraft.util.datafix.fixes.MobEffectIdFix (class, 198 lines): members: MOB_EFFECT_INSTANCE_CARRIER_ITEMS, ID_MAP, makeRule. strings: "Ambient".

net.minecraft.util.datafix.fixesclass198 linescommonextends DataFix8.6 KB

API Surface (1)

TypeRewriteRule makeRule()

Fields & Constants (2)

ID_MAP
MOB_EFFECT_INSTANCE_CARRIER_ITEMS

String literals (71)

"minecraft:speed"
"minecraft:slowness"
"minecraft:haste"
"minecraft:mining_fatigue"
"minecraft:strength"
"minecraft:instant_health"
"minecraft:instant_damage"
"minecraft:jump_boost"
"minecraft:nausea"
"minecraft:regeneration"
"minecraft:resistance"
"minecraft:fire_resistance"
"minecraft:water_breathing"
"minecraft:invisibility"
"minecraft:blindness"
"minecraft:night_vision"
"minecraft:hunger"
"minecraft:weakness"
"minecraft:poison"
"minecraft:wither"
"minecraft:health_boost"
"minecraft:absorption"
"minecraft:saturation"
"minecraft:glowing"

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

MobEffectIdFix.java198 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.mojang.datafixers.DSL;
4
import com.mojang.datafixers.DataFix;
5
import com.mojang.datafixers.OpticFinder;
6
import com.mojang.datafixers.TypeRewriteRule;
7
import com.mojang.datafixers.Typed;
8
import com.mojang.datafixers.DSL.TypeReference;
9
import com.mojang.datafixers.schemas.Schema;
10
import com.mojang.datafixers.types.Type;
11
import com.mojang.datafixers.util.Pair;
12
import com.mojang.serialization.Dynamic;
13
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
14
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
15
import java.util.Optional;
16
import java.util.Set;
17
import java.util.function.Function;
18
import java.util.stream.Stream;
19
import net.minecraft.util.Util;
20
import net.minecraft.util.datafix.schemas.NamespacedSchema;
21
22
public class MobEffectIdFix extends DataFix {
23
	private static final Int2ObjectMap<String> ID_MAP = Util.make(new Int2ObjectOpenHashMap<>(), m -> {
24
		m.put(1, "minecraft:speed");
25
		m.put(2, "minecraft:slowness");
26
		m.put(3, "minecraft:haste");
27
		m.put(4, "minecraft:mining_fatigue");
28
		m.put(5, "minecraft:strength");
29
		m.put(6, "minecraft:instant_health");
30
		m.put(7, "minecraft:instant_damage");
31
		m.put(8, "minecraft:jump_boost");
32
		m.put(9, "minecraft:nausea");
33
		m.put(10, "minecraft:regeneration");
34
		m.put(11, "minecraft:resistance");
35
		m.put(12, "minecraft:fire_resistance");
36
		m.put(13, "minecraft:water_breathing");
37
		m.put(14, "minecraft:invisibility");
38
		m.put(15, "minecraft:blindness");
39
		m.put(16, "minecraft:night_vision");
40
		m.put(17, "minecraft:hunger");
41
		m.put(18, "minecraft:weakness");
42
		m.put(19, "minecraft:poison");
43
		m.put(20, "minecraft:wither");
44
		m.put(21, "minecraft:health_boost");
45
		m.put(22, "minecraft:absorption");
46
		m.put(23, "minecraft:saturation");
47
		m.put(24, "minecraft:glowing");
48
		m.put(25, "minecraft:levitation");
49
		m.put(26, "minecraft:luck");
50
		m.put(27, "minecraft:unluck");
51
		m.put(28, "minecraft:slow_falling");
52
		m.put(29, "minecraft:conduit_power");
53
		m.put(30, "minecraft:dolphins_grace");
54
		m.put(31, "minecraft:bad_omen");
55
		m.put(32, "minecraft:hero_of_the_village");
56
		m.put(33, "minecraft:darkness");
57
	});
58
	private static final Set<String> MOB_EFFECT_INSTANCE_CARRIER_ITEMS = Set.of(
59
		"minecraft:potion", "minecraft:splash_potion", "minecraft:lingering_potion", "minecraft:tipped_arrow"
60
	);
61
62
	public MobEffectIdFix(final Schema outputSchema) {
63
		super(outputSchema, false);
64
	}
65
66
	private static <T> Optional<Dynamic<T>> getAndConvertMobEffectId(final Dynamic<T> obj, final String fieldName) {
67
		return obj.get(fieldName).asNumber().result().map(id -> ID_MAP.get(id.intValue())).map(obj::createString);
68
	}
69
70
	private static <T> Dynamic<T> updateMobEffectIdField(final Dynamic<T> input, final String oldFieldName, final Dynamic<T> output, final String newFieldName) {
71
		Optional<Dynamic<T>> mappedId = getAndConvertMobEffectId(input, oldFieldName);
72
		return output.replaceField(oldFieldName, newFieldName, mappedId);
73
	}
74
75
	private static <T> Dynamic<T> updateMobEffectIdField(final Dynamic<T> input, final String oldFieldName, final String newFieldName) {
76
		return updateMobEffectIdField(input, oldFieldName, input, newFieldName);
77
	}
78
79
	private static <T> Dynamic<T> updateMobEffectInstance(Dynamic<T> input) {
80
		input = updateMobEffectIdField(input, "Id", "id");
81
		input = input.renameField("Ambient", "ambient");
82
		input = input.renameField("Amplifier", "amplifier");
83
		input = input.renameField("Duration", "duration");
84
		input = input.renameField("ShowParticles", "show_particles");
85
		input = input.renameField("ShowIcon", "show_icon");
86
		Optional<Dynamic<T>> hiddenEffect = input.get("HiddenEffect").result().map(MobEffectIdFix::updateMobEffectInstance);
87
		return input.replaceField("HiddenEffect", "hidden_effect", hiddenEffect);
88
	}
89
90
	private static <T> Dynamic<T> updateMobEffectInstanceList(final Dynamic<T> input, final String oldField, final String newField) {
91
		Optional<Dynamic<T>> newValue = input.get(oldField)
92
			.asStreamOpt()
93
			.result()
94
			.map(effects -> input.createList(effects.map(MobEffectIdFix::updateMobEffectInstance)));
95
		return input.replaceField(oldField, newField, newValue);
96
	}
97
98
	private static <T> Dynamic<T> updateSuspiciousStewEntry(final Dynamic<T> input, Dynamic<T> output) {
99
		output = updateMobEffectIdField(input, "EffectId", output, "id");
100
		Optional<Dynamic<T>> duration = input.get("EffectDuration").result();
101
		return output.replaceField("EffectDuration", "duration", duration);
102
	}
103
104
	private static <T> Dynamic<T> updateSuspiciousStewEntry(final Dynamic<T> input) {
105
		return updateSuspiciousStewEntry(input, input);
106
	}
107
108
	private Typed<?> updateNamedChoice(final Typed<?> input, final TypeReference typeReference, final String name, final Function<Dynamic<?>, Dynamic<?>> function) {
109
		Type<?> oldType = this.getInputSchema().getChoiceType(typeReference, name);
110
		Type<?> newType = this.getOutputSchema().getChoiceType(typeReference, name);
111
		return input.updateTyped(DSL.namedChoice(name, oldType), newType, typedTag -> typedTag.update(DSL.remainderFinder(), function));
112
	}
113
114
	private TypeRewriteRule blockEntityFixer() {
115
		Type<?> blockEntityType = this.getInputSchema().getType(References.BLOCK_ENTITY);
116
		return this.fixTypeEverywhereTyped(
117
			"BlockEntityMobEffectIdFix", blockEntityType, input -> this.updateNamedChoice(input, References.BLOCK_ENTITY, "minecraft:beacon", tag -> {
118
				tag = updateMobEffectIdField(tag, "Primary", "primary_effect");
119
				return updateMobEffectIdField(tag, "Secondary", "secondary_effect");
120
			})
121
		);
122
	}
123
124
	private static <T> Dynamic<T> fixMooshroomTag(Dynamic<T> entityTag) {
125
		Dynamic<T> initialEntry = entityTag.emptyMap();
126
		Dynamic<T> entry = updateSuspiciousStewEntry(entityTag, initialEntry);
127
		if (!entry.equals(initialEntry)) {
128
			entityTag = entityTag.set("stew_effects", entityTag.createList(Stream.of(entry)));
129
		}
130
131
		return entityTag.remove("EffectId").remove("EffectDuration");
132
	}
133
134
	private static <T> Dynamic<T> fixArrowTag(final Dynamic<T> data) {
135
		return updateMobEffectInstanceList(data, "CustomPotionEffects", "custom_potion_effects");
136
	}
137
138
	private static <T> Dynamic<T> fixAreaEffectCloudTag(final Dynamic<T> data) {
139
		return updateMobEffectInstanceList(data, "Effects", "effects");
140
	}
141
142
	private static Dynamic<?> updateLivingEntityTag(final Dynamic<?> data) {
143
		return updateMobEffectInstanceList(data, "ActiveEffects", "active_effects");
144
	}
145
146
	private TypeRewriteRule entityFixer() {
147
		Type<?> entityType = this.getInputSchema().getType(References.ENTITY);
148
		return this.fixTypeEverywhereTyped("EntityMobEffectIdFix", entityType, input -> {
149
			input = this.updateNamedChoice(input, References.ENTITY, "minecraft:mooshroom", MobEffectIdFix::fixMooshroomTag);
150
			input = this.updateNamedChoice(input, References.ENTITY, "minecraft:arrow", MobEffectIdFix::fixArrowTag);
151
			input = this.updateNamedChoice(input, References.ENTITY, "minecraft:area_effect_cloud", MobEffectIdFix::fixAreaEffectCloudTag);
152
			return input.update(DSL.remainderFinder(), MobEffectIdFix::updateLivingEntityTag);
153
		});
154
	}
155
156
	private TypeRewriteRule playerFixer() {
157
		Type<?> playerType = this.getInputSchema().getType(References.PLAYER);
158
		return this.fixTypeEverywhereTyped("PlayerMobEffectIdFix", playerType, input -> input.update(DSL.remainderFinder(), MobEffectIdFix::updateLivingEntityTag));
159
	}
160
161
	private static <T> Dynamic<T> fixSuspiciousStewTag(final Dynamic<T> tag) {
162
		Optional<Dynamic<T>> effectsList = tag.get("Effects").asStreamOpt().result().map(list -> tag.createList(list.map(MobEffectIdFix::updateSuspiciousStewEntry)));
163
		return tag.replaceField("Effects", "effects", effectsList);
164
	}
165
166
	private TypeRewriteRule itemStackFixer() {
167
		OpticFinder<Pair<String, String>> idF = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
168
		Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
169
		OpticFinder<?> tagF = itemStackType.findField("tag");
170
		return this.fixTypeEverywhereTyped(
171
			"ItemStackMobEffectIdFix",
172
			itemStackType,
173
			input -> {
174
				Optional<Pair<String, String>> idOpt = input.getOptional(idF);
175
				if (idOpt.isPresent()) {
176
					String id = idOpt.get().getSecond();
177
					if (id.equals("minecraft:suspicious_stew")) {
178
						return input.updateTyped(tagF, itemTag -> itemTag.update(DSL.remainderFinder(), MobEffectIdFix::fixSuspiciousStewTag));
179
					}
180
181
					if (MOB_EFFECT_INSTANCE_CARRIER_ITEMS.contains(id)) {
182
						return input.updateTyped(
183
							tagF, itemTag -> itemTag.update(DSL.remainderFinder(), tag -> updateMobEffectInstanceList(tag, "CustomPotionEffects", "custom_potion_effects"))
184
						);
185
					}
186
				}
187
188
				return input;
189
			}
190
		);
191
	}
192
193
	@Override
194
	protected TypeRewriteRule makeRule() {
195
		return TypeRewriteRule.seq(this.blockEntityFixer(), this.entityFixer(), this.playerFixer(), this.itemStackFixer());
196
	}
197
}
198

Decompiled Minecraft 26.3 source of MobEffectIdFix (net/minecraft/util/datafix/fixes/MobEffectIdFix.java) with Mojang official mappings, stored verbatim on the site.