| 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.datafixers.types.Type;
|
| 7 | import com.mojang.serialization.Dynamic;
|
| 8 | import java.util.HashMap;
|
| 9 | import java.util.Map;
|
| 10 |
|
| 11 | public class PlayerEquipmentFix extends DataFix {
|
| 12 | private static final Map<Integer, String> SLOT_TRANSLATIONS = Map.of(100, "feet", 101, "legs", 102, "chest", 103, "head", -106, "offhand");
|
| 13 |
|
| 14 | public PlayerEquipmentFix(final Schema outputSchema) {
|
| 15 | super(outputSchema, true);
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | protected TypeRewriteRule makeRule() {
|
| 20 | Type<?> oldPlayerType = this.getInputSchema().getTypeRaw(References.PLAYER);
|
| 21 | Type<?> newPlayerType = this.getOutputSchema().getTypeRaw(References.PLAYER);
|
| 22 | return this.writeFixAndRead("Player Equipment Fix", oldPlayerType, newPlayerType, tag -> {
|
| 23 | Map<Dynamic<?>, Dynamic<?>> equipment = new HashMap<>();
|
| 24 | tag = tag.update("Inventory", inventory -> inventory.createList(inventory.asStream().filter(item -> {
|
| 25 | int inventorySlot = item.get("Slot").asInt(-1);
|
| 26 | String equipmentSlot = SLOT_TRANSLATIONS.get(inventorySlot);
|
| 27 | if (equipmentSlot != null) {
|
| 28 | equipment.put(inventory.createString(equipmentSlot), item.remove("Slot"));
|
| 29 | }
|
| 30 |
|
| 31 | return equipmentSlot == null;
|
| 32 | })));
|
| 33 | return tag.set("equipment", tag.createMap(equipment));
|
| 34 | });
|
| 35 | }
|
| 36 | }
|
| 37 |
|