| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFix;
|
| 4 | import com.mojang.datafixers.TypeRewriteRule;
|
| 5 | import com.mojang.datafixers.schemas.Schema;
|
| 6 | import com.mojang.serialization.Dynamic;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.stream.Stream;
|
| 9 |
|
| 10 | public class FoodToConsumableFix extends DataFix {
|
| 11 | public FoodToConsumableFix(final Schema outputSchema) {
|
| 12 | super(outputSchema, true);
|
| 13 | }
|
| 14 |
|
| 15 | @Override
|
| 16 | protected TypeRewriteRule makeRule() {
|
| 17 | return this.writeFixAndRead(
|
| 18 | "Food to consumable fix",
|
| 19 | this.getInputSchema().getType(References.DATA_COMPONENTS),
|
| 20 | this.getOutputSchema().getType(References.DATA_COMPONENTS),
|
| 21 | components -> {
|
| 22 | Optional<? extends Dynamic<?>> foodComponent = components.get("minecraft:food").result();
|
| 23 | if (foodComponent.isPresent()) {
|
| 24 | float eatSeconds = foodComponent.get().get("eat_seconds").asFloat(1.6F);
|
| 25 | Stream<? extends Dynamic<?>> effects = foodComponent.get().get("effects").asStream();
|
| 26 | Stream<? extends Dynamic<?>> onConsumeEffects = effects.map(
|
| 27 | effect -> effect.emptyMap()
|
| 28 | .set("type", effect.createString("minecraft:apply_effects"))
|
| 29 | .set("effects", effect.createList(effect.get("effect").result().stream()))
|
| 30 | .set("probability", effect.createFloat(effect.get("probability").asFloat(1.0F)))
|
| 31 | );
|
| 32 | components = Dynamic.copyField((Dynamic<?>)foodComponent.get(), "using_converts_to", components, "minecraft:use_remainder");
|
| 33 | components = components.set("minecraft:food", foodComponent.get().remove("eat_seconds").remove("effects").remove("using_converts_to"));
|
| 34 | return components.set(
|
| 35 | "minecraft:consumable",
|
| 36 | components.emptyMap().set("consume_seconds", components.createFloat(eatSeconds)).set("on_consume_effects", components.createList(onConsumeEffects))
|
| 37 | );
|
| 38 | } else {
|
| 39 | return components;
|
| 40 | }
|
| 41 | }
|
| 42 | );
|
| 43 | }
|
| 44 | }
|
| 45 |
|