| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.IntFunction;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.component.DataComponentLookup;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.server.level.ServerLevel;
|
| 18 | import net.minecraft.util.ARGB;
|
| 19 | import net.minecraft.util.ByIdMap;
|
| 20 | import net.minecraft.util.StringRepresentable;
|
| 21 | import net.minecraft.world.item.crafting.CraftingInput;
|
| 22 | import net.minecraft.world.item.crafting.CraftingRecipe;
|
| 23 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 24 | import net.minecraft.world.item.crafting.RecipeType;
|
| 25 | import net.minecraft.world.level.material.MapColor;
|
| 26 | import org.jetbrains.annotations.Contract;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public enum DyeColor implements StringRepresentable {
|
| 30 | WHITE(0, "white", 16383998, MapColor.SNOW, MapColor.TERRACOTTA_WHITE, 15790320, 16777215),
|
| 31 | ORANGE(1, "orange", 16351261, MapColor.COLOR_ORANGE, MapColor.TERRACOTTA_ORANGE, 15435844, 16738335),
|
| 32 | MAGENTA(2, "magenta", 13061821, MapColor.COLOR_MAGENTA, MapColor.TERRACOTTA_MAGENTA, 12801229, 16711935),
|
| 33 | LIGHT_BLUE(3, "light_blue", 3847130, MapColor.COLOR_LIGHT_BLUE, MapColor.TERRACOTTA_LIGHT_BLUE, 6719955, 10141901),
|
| 34 | YELLOW(4, "yellow", 16701501, MapColor.COLOR_YELLOW, MapColor.TERRACOTTA_YELLOW, 14602026, 16776960),
|
| 35 | LIME(5, "lime", 8439583, MapColor.COLOR_LIGHT_GREEN, MapColor.TERRACOTTA_LIGHT_GREEN, 4312372, 12582656),
|
| 36 | PINK(6, "pink", 15961002, MapColor.COLOR_PINK, MapColor.TERRACOTTA_PINK, 14188952, 16738740),
|
| 37 | GRAY(7, "gray", 4673362, MapColor.COLOR_GRAY, MapColor.TERRACOTTA_GRAY, 4408131, 8421504),
|
| 38 | LIGHT_GRAY(8, "light_gray", 10329495, MapColor.COLOR_LIGHT_GRAY, MapColor.TERRACOTTA_LIGHT_GRAY, 11250603, 13882323),
|
| 39 | CYAN(9, "cyan", 1481884, MapColor.COLOR_CYAN, MapColor.TERRACOTTA_CYAN, 2651799, 65535),
|
| 40 | PURPLE(10, "purple", 8991416, MapColor.COLOR_PURPLE, MapColor.TERRACOTTA_PURPLE, 8073150, 10494192),
|
| 41 | BLUE(11, "blue", 3949738, MapColor.COLOR_BLUE, MapColor.TERRACOTTA_BLUE, 2437522, 255),
|
| 42 | BROWN(12, "brown", 8606770, MapColor.COLOR_BROWN, MapColor.TERRACOTTA_BROWN, 5320730, 9127187),
|
| 43 | GREEN(13, "green", 6192150, MapColor.COLOR_GREEN, MapColor.TERRACOTTA_GREEN, 3887386, 65280),
|
| 44 | RED(14, "red", 11546150, MapColor.COLOR_RED, MapColor.TERRACOTTA_RED, 11743532, 16711680),
|
| 45 | BLACK(15, "black", 1908001, MapColor.COLOR_BLACK, MapColor.TERRACOTTA_BLACK, 1973019, 0);
|
| 46 |
|
| 47 | public static final List<DyeColor> VALUES = List.of(values());
|
| 48 | private static final IntFunction<DyeColor> BY_ID = ByIdMap.continuous(DyeColor::getId, VALUES.toArray(DyeColor[]::new), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 49 | private static final Int2ObjectOpenHashMap<DyeColor> BY_FIREWORK_COLOR = new Int2ObjectOpenHashMap<>(
|
| 50 | VALUES.stream().collect(Collectors.toMap(v -> v.fireworkColor, v -> (DyeColor)v))
|
| 51 | );
|
| 52 | public static final StringRepresentable.EnumCodec<DyeColor> CODEC = StringRepresentable.fromEnum(DyeColor::values);
|
| 53 | public static final StreamCodec<ByteBuf, DyeColor> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, DyeColor::getId);
|
| 54 | @Deprecated
|
| 55 | public static final Codec<DyeColor> LEGACY_ID_CODEC = Codec.BYTE.xmap(DyeColor::byId, color -> (byte)color.id);
|
| 56 | private final int id;
|
| 57 | private final String name;
|
| 58 | private final MapColor mapColor;
|
| 59 | private final MapColor terracottaColor;
|
| 60 | private final int textureDiffuseColor;
|
| 61 | private final int fireworkColor;
|
| 62 | private final int textColor;
|
| 63 |
|
| 64 | DyeColor(
|
| 65 | final int id,
|
| 66 | final String name,
|
| 67 | final int textureDiffuseColor,
|
| 68 | final MapColor mapColor,
|
| 69 | final MapColor terracottaColor,
|
| 70 | final int fireworkColor,
|
| 71 | final int textColor
|
| 72 | ) {
|
| 73 | this.id = id;
|
| 74 | this.name = name;
|
| 75 | this.mapColor = mapColor;
|
| 76 | this.terracottaColor = terracottaColor;
|
| 77 | this.textColor = ARGB.opaque(textColor);
|
| 78 | this.textureDiffuseColor = ARGB.opaque(textureDiffuseColor);
|
| 79 | this.fireworkColor = fireworkColor;
|
| 80 | }
|
| 81 |
|
| 82 | public int getId() {
|
| 83 | return this.id;
|
| 84 | }
|
| 85 |
|
| 86 | public String getName() {
|
| 87 | return this.name;
|
| 88 | }
|
| 89 |
|
| 90 | public int getTextureDiffuseColor() {
|
| 91 | return this.textureDiffuseColor;
|
| 92 | }
|
| 93 |
|
| 94 | public MapColor getMapColor() {
|
| 95 | return this.mapColor;
|
| 96 | }
|
| 97 |
|
| 98 | public MapColor getTerracottaColor() {
|
| 99 | return this.terracottaColor;
|
| 100 | }
|
| 101 |
|
| 102 | public int getFireworkColor() {
|
| 103 | return this.fireworkColor;
|
| 104 | }
|
| 105 |
|
| 106 | public int getTextColor() {
|
| 107 | return this.textColor;
|
| 108 | }
|
| 109 |
|
| 110 | public static DyeColor byId(final int id) {
|
| 111 | return BY_ID.apply(id);
|
| 112 | }
|
| 113 |
|
| 114 | @Contract("_,!null->!null;_,null->_")
|
| 115 | public static @Nullable DyeColor byName(final String name, final @Nullable DyeColor def) {
|
| 116 | DyeColor result = CODEC.byName(name);
|
| 117 | return result != null ? result : def;
|
| 118 | }
|
| 119 |
|
| 120 | public static @Nullable DyeColor byFireworkColor(final int color) {
|
| 121 | return BY_FIREWORK_COLOR.get(color);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public String toString() {
|
| 126 | return this.name;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public String getSerializedName() {
|
| 131 | return this.name;
|
| 132 | }
|
| 133 |
|
| 134 | public static DyeColor getMixedColor(final ServerLevel level, final DyeColor dyeColor1, final DyeColor dyeColor2) {
|
| 135 | DyeColor mixedColor = findColorMixInRecipes(level, dyeColor1, dyeColor2);
|
| 136 | if (mixedColor != null) {
|
| 137 | return mixedColor;
|
| 138 | } else {
|
| 139 | return level.getRandom().nextBoolean() ? dyeColor1 : dyeColor2;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private static @Nullable DyeColor findColorMixInRecipes(final ServerLevel level, final DyeColor dyeColor1, final DyeColor dyeColor2) {
|
| 144 | DataComponentLookup<Item> itemComponents = level.registryAccess().lookupOrThrow(Registries.ITEM).componentLookup();
|
| 145 | Collection<Holder<Item>> dye1Items = itemComponents.findAll(DataComponents.DYE, dyeColor1);
|
| 146 | if (dye1Items.isEmpty()) {
|
| 147 | return null;
|
| 148 | }
|
| 149 |
|
| 150 | Collection<Holder<Item>> dye2Items = itemComponents.findAll(DataComponents.DYE, dyeColor2);
|
| 151 | if (dye2Items.isEmpty()) {
|
| 152 | return null;
|
| 153 | }
|
| 154 |
|
| 155 | for (Holder<Item> dye1Item : dye1Items) {
|
| 156 | for (Holder<Item> dye2Item : dye2Items) {
|
| 157 | CraftingInput input = CraftingInput.of(2, 1, List.of(new ItemStack(dye1Item), new ItemStack(dye2Item)));
|
| 158 | Optional<RecipeHolder<CraftingRecipe>> foundRecipe = level.recipeAccess().getRecipeFor(RecipeType.CRAFTING, input, level);
|
| 159 | if (foundRecipe.isPresent()) {
|
| 160 | ItemStack craftingResult = foundRecipe.get().value().assemble(input);
|
| 161 | DyeColor craftedDyeColor = craftingResult.get(DataComponents.DYE);
|
| 162 | if (craftedDyeColor != null) {
|
| 163 | return craftedDyeColor;
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | return null;
|
| 170 | }
|
| 171 | }
|
| 172 |
|