| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 8 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntLists;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.List;
|
| 12 | import java.util.function.Function;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.minecraft.commands.ParserUtils;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.util.StringRepresentable;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import net.minecraft.world.entity.EquipmentSlot;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class SlotRanges {
|
| 22 | public static final int MOB_INVENTORY_SLOT_OFFSET = 300;
|
| 23 | public static final int MOB_INVENTORY_SIZE = 8;
|
| 24 | private static final List<SlotRange> SLOTS = Util.make(new ArrayList<>(), values -> {
|
| 25 | addSingleSlot(values, "contents", 0);
|
| 26 | addSlotRange(values, "container.", 0, 54);
|
| 27 | addSlotRange(values, "hotbar.", 0, 9);
|
| 28 | addSlotRange(values, "inventory.", 9, 27);
|
| 29 | addSlotRange(values, "enderchest.", 200, 27);
|
| 30 | addSlotRange(values, "mob.inventory.", 300, 8);
|
| 31 | addSlotRange(values, "horse.", 500, 15);
|
| 32 | int mainhand = EquipmentSlot.MAINHAND.getIndex(98);
|
| 33 | int offhand = EquipmentSlot.OFFHAND.getIndex(98);
|
| 34 | addSingleSlot(values, "weapon", mainhand);
|
| 35 | addSingleSlot(values, "weapon.mainhand", mainhand);
|
| 36 | addSingleSlot(values, "weapon.offhand", offhand);
|
| 37 | addSlots(values, "weapon.*", mainhand, offhand);
|
| 38 | mainhand = EquipmentSlot.HEAD.getIndex(100);
|
| 39 | offhand = EquipmentSlot.CHEST.getIndex(100);
|
| 40 | int legs = EquipmentSlot.LEGS.getIndex(100);
|
| 41 | int feet = EquipmentSlot.FEET.getIndex(100);
|
| 42 | int body = EquipmentSlot.BODY.getIndex(105);
|
| 43 | addSingleSlot(values, "armor.head", mainhand);
|
| 44 | addSingleSlot(values, "armor.chest", offhand);
|
| 45 | addSingleSlot(values, "armor.legs", legs);
|
| 46 | addSingleSlot(values, "armor.feet", feet);
|
| 47 | addSingleSlot(values, "armor.body", body);
|
| 48 | addSlots(values, "armor.*", mainhand, offhand, legs, feet, body);
|
| 49 | addSingleSlot(values, "saddle", EquipmentSlot.SADDLE.getIndex(106));
|
| 50 | addSingleSlot(values, "horse.chest", 499);
|
| 51 | addSingleSlot(values, "player.cursor", 499);
|
| 52 | addSlotRange(values, "player.crafting.", 500, 4);
|
| 53 | });
|
| 54 | private static final DynamicCommandExceptionType ERROR_UNKNOWN = new DynamicCommandExceptionType(id -> Component.translatableEscape("slot.unknown", id));
|
| 55 | public static final Codec<SlotRange> CODEC = StringRepresentable.fromValues(() -> SLOTS.toArray(SlotRange[]::new));
|
| 56 | private static final Function<String, @Nullable SlotRange> NAME_LOOKUP = StringRepresentable.createNameLookup(SLOTS.toArray(SlotRange[]::new));
|
| 57 |
|
| 58 | private static SlotRange create(final String name, final int id) {
|
| 59 | return SlotRange.of(name, IntLists.singleton(id));
|
| 60 | }
|
| 61 |
|
| 62 | private static SlotRange create(final String name, final IntList ids) {
|
| 63 | return SlotRange.of(name, IntLists.unmodifiable(ids));
|
| 64 | }
|
| 65 |
|
| 66 | private static SlotRange create(final String name, final int... ids) {
|
| 67 | return SlotRange.of(name, IntList.of(ids));
|
| 68 | }
|
| 69 |
|
| 70 | private static void addSingleSlot(final List<SlotRange> output, final String name, final int id) {
|
| 71 | output.add(create(name, id));
|
| 72 | }
|
| 73 |
|
| 74 | private static void addSlotRange(final List<SlotRange> output, final String prefix, final int offset, final int size) {
|
| 75 | IntList allSlots = new IntArrayList(size);
|
| 76 |
|
| 77 | for (int i = 0; i < size; i++) {
|
| 78 | int slotId = offset + i;
|
| 79 | output.add(create(prefix + i, slotId));
|
| 80 | allSlots.add(slotId);
|
| 81 | }
|
| 82 |
|
| 83 | output.add(create(prefix + "*", allSlots));
|
| 84 | }
|
| 85 |
|
| 86 | private static void addSlots(final List<SlotRange> output, final String name, final int... values) {
|
| 87 | output.add(create(name, values));
|
| 88 | }
|
| 89 |
|
| 90 | public static @Nullable SlotRange nameToIds(final String name) {
|
| 91 | return NAME_LOOKUP.apply(name);
|
| 92 | }
|
| 93 |
|
| 94 | public static Stream<String> allNames() {
|
| 95 | return SLOTS.stream().map(StringRepresentable::getSerializedName);
|
| 96 | }
|
| 97 |
|
| 98 | public static Stream<String> singleSlotNames() {
|
| 99 | return SLOTS.stream().filter(e -> e.size() == 1).map(StringRepresentable::getSerializedName);
|
| 100 | }
|
| 101 |
|
| 102 | public static @Nullable SlotRange tryRead(final StringReader reader) {
|
| 103 | String name = ParserUtils.readWhile(reader, c -> c != ' ');
|
| 104 | return nameToIds(name);
|
| 105 | }
|
| 106 |
|
| 107 | public static SlotRange read(final StringReader reader) throws CommandSyntaxException {
|
| 108 | String name = ParserUtils.readWhile(reader, c -> c != ' ');
|
| 109 | SlotRange result = nameToIds(name);
|
| 110 | if (result == null) {
|
| 111 | throw ERROR_UNKNOWN.createWithContext(reader, name);
|
| 112 | } else {
|
| 113 | return result;
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|