| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.Iterator;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.IntFunction;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.util.ByIdMap;
|
| 12 | import net.minecraft.util.StringRepresentable;
|
| 13 |
|
| 14 | public enum EquipmentSlotGroup implements StringRepresentable, Iterable<EquipmentSlot> {
|
| 15 | ANY(0, "any", slot -> true),
|
| 16 | MAINHAND(1, "mainhand", EquipmentSlot.MAINHAND),
|
| 17 | OFFHAND(2, "offhand", EquipmentSlot.OFFHAND),
|
| 18 | HAND(3, "hand", slot -> slot.getType() == EquipmentSlot.Type.HAND),
|
| 19 | FEET(4, "feet", EquipmentSlot.FEET),
|
| 20 | LEGS(5, "legs", EquipmentSlot.LEGS),
|
| 21 | CHEST(6, "chest", EquipmentSlot.CHEST),
|
| 22 | HEAD(7, "head", EquipmentSlot.HEAD),
|
| 23 | ARMOR(8, "armor", EquipmentSlot::isArmor),
|
| 24 | BODY(9, "body", EquipmentSlot.BODY),
|
| 25 | SADDLE(10, "saddle", EquipmentSlot.SADDLE);
|
| 26 |
|
| 27 | public static final IntFunction<EquipmentSlotGroup> BY_ID = ByIdMap.continuous(s -> s.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 28 | public static final Codec<EquipmentSlotGroup> CODEC = StringRepresentable.fromEnum(EquipmentSlotGroup::values);
|
| 29 | public static final StreamCodec<ByteBuf, EquipmentSlotGroup> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, s -> s.id);
|
| 30 | private final int id;
|
| 31 | private final String key;
|
| 32 | private final Predicate<EquipmentSlot> predicate;
|
| 33 | private final List<EquipmentSlot> slots;
|
| 34 |
|
| 35 | EquipmentSlotGroup(final int id, final String key, final Predicate<EquipmentSlot> predicate) {
|
| 36 | this.id = id;
|
| 37 | this.key = key;
|
| 38 | this.predicate = predicate;
|
| 39 | this.slots = EquipmentSlot.VALUES.stream().filter(predicate).toList();
|
| 40 | }
|
| 41 |
|
| 42 | EquipmentSlotGroup(final int id, final String key, final EquipmentSlot slot) {
|
| 43 | this(id, key, s -> s == slot);
|
| 44 | }
|
| 45 |
|
| 46 | public static EquipmentSlotGroup bySlot(final EquipmentSlot slot) {
|
| 47 | return switch (slot) {
|
| 48 | case MAINHAND -> MAINHAND;
|
| 49 | case OFFHAND -> OFFHAND;
|
| 50 | case FEET -> FEET;
|
| 51 | case LEGS -> LEGS;
|
| 52 | case CHEST -> CHEST;
|
| 53 | case HEAD -> HEAD;
|
| 54 | case BODY -> BODY;
|
| 55 | case SADDLE -> SADDLE;
|
| 56 | };
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public String getSerializedName() {
|
| 61 | return this.key;
|
| 62 | }
|
| 63 |
|
| 64 | public boolean test(final EquipmentSlot slot) {
|
| 65 | return this.predicate.test(slot);
|
| 66 | }
|
| 67 |
|
| 68 | public List<EquipmentSlot> slots() {
|
| 69 | return this.slots;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public Iterator<EquipmentSlot> iterator() {
|
| 74 | return this.slots.iterator();
|
| 75 | }
|
| 76 | }
|
| 77 |
|