| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.BiConsumer;
|
| 7 | import java.util.function.BiFunction;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import java.util.function.Function;
|
| 10 | import net.minecraft.world.item.DyeColor;
|
| 11 | import net.minecraft.world.item.Item;
|
| 12 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 13 | import org.apache.commons.lang3.function.TriFunction;
|
| 14 |
|
| 15 | public record ColorCollection<T>(
|
| 16 | T white, T orange, T magenta, T lightBlue, T yellow, T lime, T pink, T gray, T lightGray, T cyan, T purple, T blue, T brown, T green, T red, T black
|
| 17 | ) {
|
| 18 | public static final ColorCollection<DyeColor> VALUES = new ColorCollection<>(
|
| 19 | DyeColor.WHITE,
|
| 20 | DyeColor.ORANGE,
|
| 21 | DyeColor.MAGENTA,
|
| 22 | DyeColor.LIGHT_BLUE,
|
| 23 | DyeColor.YELLOW,
|
| 24 | DyeColor.LIME,
|
| 25 | DyeColor.PINK,
|
| 26 | DyeColor.GRAY,
|
| 27 | DyeColor.LIGHT_GRAY,
|
| 28 | DyeColor.CYAN,
|
| 29 | DyeColor.PURPLE,
|
| 30 | DyeColor.BLUE,
|
| 31 | DyeColor.BROWN,
|
| 32 | DyeColor.GREEN,
|
| 33 | DyeColor.RED,
|
| 34 | DyeColor.BLACK
|
| 35 | );
|
| 36 | public static final ColorCollection<String> NAMES = VALUES.map(DyeColor::getName);
|
| 37 |
|
| 38 | public static <T> ColorCollection<T> create(final T value) {
|
| 39 | return new ColorCollection<>(value, value, value, value, value, value, value, value, value, value, value, value, value, value, value, value);
|
| 40 | }
|
| 41 |
|
| 42 | public static <B extends Block, Id> ColorCollection<Block> registerBlocks(
|
| 43 | final ColorCollection<Id> ids,
|
| 44 | final TriFunction<Id, Function<BlockBehaviour.Properties, Block>, BlockBehaviour.Properties, Block> register,
|
| 45 | final BiFunction<DyeColor, BlockBehaviour.Properties, B> colorBlockFactory,
|
| 46 | final Function<DyeColor, BlockBehaviour.Properties> propertiesSupplier
|
| 47 | ) {
|
| 48 | return zipMap(VALUES, ids, (color, id) -> register.apply(id, p -> colorBlockFactory.apply(color, p), propertiesSupplier.apply(color)));
|
| 49 | }
|
| 50 |
|
| 51 | public static <Id> ColorCollection<Item> registerBlockItems(
|
| 52 | final ColorCollection<Id> ids, final ColorCollection<Block> blocks, final TriFunction<Id, Block, DyeColor, Item> itemFactory
|
| 53 | ) {
|
| 54 | return zipMap(VALUES, ids, (color, id) -> itemFactory.apply(id, blocks.pick(color), color));
|
| 55 | }
|
| 56 |
|
| 57 | public static <Id> ColorCollection<Item> registerItems(final ColorCollection<Id> ids, final BiFunction<Id, DyeColor, Item> itemFactory) {
|
| 58 | return zipMap(VALUES, ids, (color, id) -> itemFactory.apply(id, color));
|
| 59 | }
|
| 60 |
|
| 61 | public static ColorCollection<String> prefixWithColor(final ColorCollection<String> ids) {
|
| 62 | return zipMap(NAMES, ids, (color, id) -> color + "_" + id);
|
| 63 | }
|
| 64 |
|
| 65 | public List<T> asList() {
|
| 66 | Builder<T> builder = ImmutableList.builderWithExpectedSize(16);
|
| 67 | this.forEach(builder::add);
|
| 68 | return builder.build();
|
| 69 | }
|
| 70 |
|
| 71 | public void forEach(final Consumer<T> consumer) {
|
| 72 | consumer.accept(this.white);
|
| 73 | consumer.accept(this.orange);
|
| 74 | consumer.accept(this.magenta);
|
| 75 | consumer.accept(this.lightBlue);
|
| 76 | consumer.accept(this.yellow);
|
| 77 | consumer.accept(this.lime);
|
| 78 | consumer.accept(this.pink);
|
| 79 | consumer.accept(this.gray);
|
| 80 | consumer.accept(this.lightGray);
|
| 81 | consumer.accept(this.cyan);
|
| 82 | consumer.accept(this.purple);
|
| 83 | consumer.accept(this.blue);
|
| 84 | consumer.accept(this.brown);
|
| 85 | consumer.accept(this.green);
|
| 86 | consumer.accept(this.red);
|
| 87 | consumer.accept(this.black);
|
| 88 | }
|
| 89 |
|
| 90 | public T pick(final DyeColor dyeColor) {
|
| 91 | return (T)(switch (dyeColor) {
|
| 92 | case WHITE -> this.white;
|
| 93 | case ORANGE -> this.orange;
|
| 94 | case MAGENTA -> this.magenta;
|
| 95 | case LIGHT_BLUE -> this.lightBlue;
|
| 96 | case YELLOW -> this.yellow;
|
| 97 | case LIME -> this.lime;
|
| 98 | case PINK -> this.pink;
|
| 99 | case GRAY -> this.gray;
|
| 100 | case LIGHT_GRAY -> this.lightGray;
|
| 101 | case CYAN -> this.cyan;
|
| 102 | case PURPLE -> this.purple;
|
| 103 | case BLUE -> this.blue;
|
| 104 | case BROWN -> this.brown;
|
| 105 | case GREEN -> this.green;
|
| 106 | case RED -> this.red;
|
| 107 | case BLACK -> this.black;
|
| 108 | });
|
| 109 | }
|
| 110 |
|
| 111 | public <U> ColorCollection<U> map(final Function<T, U> mapper) {
|
| 112 | return new ColorCollection<>(
|
| 113 | mapper.apply(this.white),
|
| 114 | mapper.apply(this.orange),
|
| 115 | mapper.apply(this.magenta),
|
| 116 | mapper.apply(this.lightBlue),
|
| 117 | mapper.apply(this.yellow),
|
| 118 | mapper.apply(this.lime),
|
| 119 | mapper.apply(this.pink),
|
| 120 | mapper.apply(this.gray),
|
| 121 | mapper.apply(this.lightGray),
|
| 122 | mapper.apply(this.cyan),
|
| 123 | mapper.apply(this.purple),
|
| 124 | mapper.apply(this.blue),
|
| 125 | mapper.apply(this.brown),
|
| 126 | mapper.apply(this.green),
|
| 127 | mapper.apply(this.red),
|
| 128 | mapper.apply(this.black)
|
| 129 | );
|
| 130 | }
|
| 131 |
|
| 132 | public static <T, U> void zipApply(final ColorCollection<T> first, final ColorCollection<U> second, final BiConsumer<T, U> consumer) {
|
| 133 | consumer.accept(first.white(), second.white());
|
| 134 | consumer.accept(first.orange(), second.orange());
|
| 135 | consumer.accept(first.magenta(), second.magenta());
|
| 136 | consumer.accept(first.lightBlue(), second.lightBlue());
|
| 137 | consumer.accept(first.yellow(), second.yellow());
|
| 138 | consumer.accept(first.lime(), second.lime());
|
| 139 | consumer.accept(first.pink(), second.pink());
|
| 140 | consumer.accept(first.gray(), second.gray());
|
| 141 | consumer.accept(first.lightGray(), second.lightGray());
|
| 142 | consumer.accept(first.cyan(), second.cyan());
|
| 143 | consumer.accept(first.purple(), second.purple());
|
| 144 | consumer.accept(first.blue(), second.blue());
|
| 145 | consumer.accept(first.brown(), second.brown());
|
| 146 | consumer.accept(first.green(), second.green());
|
| 147 | consumer.accept(first.red(), second.red());
|
| 148 | consumer.accept(first.black(), second.black());
|
| 149 | }
|
| 150 |
|
| 151 | public static <T, U, R> ColorCollection<R> zipMap(final ColorCollection<T> first, final ColorCollection<U> second, final BiFunction<T, U, R> operation) {
|
| 152 | return new ColorCollection<>(
|
| 153 | operation.apply(first.white(), second.white()),
|
| 154 | operation.apply(first.orange(), second.orange()),
|
| 155 | operation.apply(first.magenta(), second.magenta()),
|
| 156 | operation.apply(first.lightBlue(), second.lightBlue()),
|
| 157 | operation.apply(first.yellow(), second.yellow()),
|
| 158 | operation.apply(first.lime(), second.lime()),
|
| 159 | operation.apply(first.pink(), second.pink()),
|
| 160 | operation.apply(first.gray(), second.gray()),
|
| 161 | operation.apply(first.lightGray(), second.lightGray()),
|
| 162 | operation.apply(first.cyan(), second.cyan()),
|
| 163 | operation.apply(first.purple(), second.purple()),
|
| 164 | operation.apply(first.blue(), second.blue()),
|
| 165 | operation.apply(first.brown(), second.brown()),
|
| 166 | operation.apply(first.green(), second.green()),
|
| 167 | operation.apply(first.red(), second.red()),
|
| 168 | operation.apply(first.black(), second.black())
|
| 169 | );
|
| 170 | }
|
| 171 | }
|
| 172 |
|