| 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.data.BlockFamily;
|
| 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 WeatheringCopperCollection<T>(WeatheringCopperCollection.ByState<T> weathering, WeatheringCopperCollection.ByState<T> waxed) {
|
| 16 | public static final WeatheringCopperCollection.ByState<WeatheringCopper.WeatherState> STATES = new WeatheringCopperCollection.ByState<>(
|
| 17 | WeatheringCopper.WeatherState.UNAFFECTED,
|
| 18 | WeatheringCopper.WeatherState.EXPOSED,
|
| 19 | WeatheringCopper.WeatherState.WEATHERED,
|
| 20 | WeatheringCopper.WeatherState.OXIDIZED
|
| 21 | );
|
| 22 | public static final WeatheringCopperCollection<String> PREFIXES = new WeatheringCopperCollection<>(
|
| 23 | new WeatheringCopperCollection.ByState<>("", "exposed_", "weathered_", "oxidized_"),
|
| 24 | new WeatheringCopperCollection.ByState<>("waxed_", "waxed_exposed_", "waxed_weathered_", "waxed_oxidized_")
|
| 25 | );
|
| 26 |
|
| 27 | public static WeatheringCopperCollection<String> prefixWithState(final WeatheringCopperCollection<String> ids) {
|
| 28 | return zipMap(PREFIXES, ids, (state, id) -> state + id);
|
| 29 | }
|
| 30 |
|
| 31 | public static WeatheringCopperCollection<String> create(final String name) {
|
| 32 | return same(WeatheringCopperCollection.ByState.create(name));
|
| 33 | }
|
| 34 |
|
| 35 | public static WeatheringCopperCollection<String> same(final WeatheringCopperCollection.ByState<String> byState) {
|
| 36 | return new WeatheringCopperCollection<>(byState, byState);
|
| 37 | }
|
| 38 |
|
| 39 | public static <WaxedBlock extends Block, WeatheringBlock extends Block & WeatheringCopper, Id> WeatheringCopperCollection<Block> registerBlocks(
|
| 40 | final WeatheringCopperCollection<Id> ids,
|
| 41 | final TriFunction<Id, Function<BlockBehaviour.Properties, Block>, BlockBehaviour.Properties, Block> register,
|
| 42 | final BiFunction<WeatheringCopper.WeatherState, BlockBehaviour.Properties, WaxedBlock> waxedBlockFactory,
|
| 43 | final BiFunction<WeatheringCopper.WeatherState, BlockBehaviour.Properties, WeatheringBlock> weatheringFactory,
|
| 44 | final Function<WeatheringCopper.WeatherState, BlockBehaviour.Properties> propertiesSupplier
|
| 45 | ) {
|
| 46 | return ids.apply(
|
| 47 | weatheringIds -> zipMap(STATES, weatheringIds, (state, id) -> register.apply(id, p -> weatheringFactory.apply(state, p), propertiesSupplier.apply(state))),
|
| 48 | waxedIds -> zipMap(STATES, waxedIds, (state, id) -> register.apply(id, p -> waxedBlockFactory.apply(state, p), propertiesSupplier.apply(state)))
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | public static <Id> WeatheringCopperCollection<Item> registerItems(
|
| 53 | final WeatheringCopperCollection<Id> ids, final WeatheringCopperCollection<Block> blocks, final BiFunction<Id, Block, Item> itemFactory
|
| 54 | ) {
|
| 55 | return zipMap(ids, blocks, itemFactory);
|
| 56 | }
|
| 57 |
|
| 58 | public static WeatheringCopperCollection<BlockFamily> createFamily(
|
| 59 | final BiFunction<String, WeatheringCopper.WeatherState, BlockFamily> waxedProvider,
|
| 60 | final BiFunction<String, WeatheringCopper.WeatherState, BlockFamily> weatheringProvider
|
| 61 | ) {
|
| 62 | return PREFIXES.apply(
|
| 63 | weatheringPrefixes -> zipMap(weatheringPrefixes, STATES, weatheringProvider), waxedPrefixes -> zipMap(waxedPrefixes, STATES, waxedProvider)
|
| 64 | );
|
| 65 | }
|
| 66 |
|
| 67 | public List<T> asList() {
|
| 68 | Builder<T> builder = ImmutableList.builderWithExpectedSize(8);
|
| 69 | this.forEach(builder::add);
|
| 70 | return builder.build();
|
| 71 | }
|
| 72 |
|
| 73 | public void forEach(final Consumer<T> consumer) {
|
| 74 | this.weathering.forEach(consumer);
|
| 75 | this.waxed.forEach(consumer);
|
| 76 | }
|
| 77 |
|
| 78 | public <U> WeatheringCopperCollection<U> map(final Function<T, U> mapper) {
|
| 79 | return new WeatheringCopperCollection<>(this.weathering.map(mapper), this.waxed.map(mapper));
|
| 80 | }
|
| 81 |
|
| 82 | public <U> WeatheringCopperCollection<U> apply(final Function<WeatheringCopperCollection.ByState<T>, WeatheringCopperCollection.ByState<U>> mapper) {
|
| 83 | return this.apply(mapper, mapper);
|
| 84 | }
|
| 85 |
|
| 86 | public <U> WeatheringCopperCollection<U> apply(
|
| 87 | final Function<WeatheringCopperCollection.ByState<T>, WeatheringCopperCollection.ByState<U>> weatheringMapper,
|
| 88 | final Function<WeatheringCopperCollection.ByState<T>, WeatheringCopperCollection.ByState<U>> waxedMapper
|
| 89 | ) {
|
| 90 | return new WeatheringCopperCollection<>(weatheringMapper.apply(this.weathering), waxedMapper.apply(this.waxed));
|
| 91 | }
|
| 92 |
|
| 93 | public static <T, U> void zipApply(final WeatheringCopperCollection<T> first, final WeatheringCopperCollection<U> second, final BiConsumer<T, U> consumer) {
|
| 94 | zipApply(first.weathering, second.weathering, consumer);
|
| 95 | zipApply(first.waxed, second.waxed, consumer);
|
| 96 | }
|
| 97 |
|
| 98 | public static <T, U, R> WeatheringCopperCollection<R> zipMap(
|
| 99 | final WeatheringCopperCollection<T> first, final WeatheringCopperCollection<U> second, final BiFunction<T, U, R> operation
|
| 100 | ) {
|
| 101 | return new WeatheringCopperCollection<>(zipMap(first.weathering, second.weathering, operation), zipMap(first.waxed, second.waxed, operation));
|
| 102 | }
|
| 103 |
|
| 104 | public void zipUnwaxedWaxed(final BiConsumer<T, T> consumer) {
|
| 105 | zipApply(this.weathering, this.waxed, consumer);
|
| 106 | }
|
| 107 |
|
| 108 | public static <T, U> void zipApply(
|
| 109 | final WeatheringCopperCollection.ByState<T> first, final WeatheringCopperCollection.ByState<U> second, final BiConsumer<T, U> consumer
|
| 110 | ) {
|
| 111 | consumer.accept(first.unaffected, second.unaffected);
|
| 112 | consumer.accept(first.exposed, second.exposed);
|
| 113 | consumer.accept(first.weathered, second.weathered);
|
| 114 | consumer.accept(first.oxidized, second.oxidized);
|
| 115 | }
|
| 116 |
|
| 117 | public static <T, U, R> WeatheringCopperCollection.ByState<R> zipMap(
|
| 118 | final WeatheringCopperCollection.ByState<T> first, final WeatheringCopperCollection.ByState<U> second, final BiFunction<T, U, R> operation
|
| 119 | ) {
|
| 120 | return new WeatheringCopperCollection.ByState<>(
|
| 121 | operation.apply(first.unaffected, second.unaffected),
|
| 122 | operation.apply(first.exposed, second.exposed),
|
| 123 | operation.apply(first.weathered, second.weathered),
|
| 124 | operation.apply(first.oxidized, second.oxidized)
|
| 125 | );
|
| 126 | }
|
| 127 |
|
| 128 | public record ByState<T>(T unaffected, T exposed, T weathered, T oxidized) {
|
| 129 | public static <T> WeatheringCopperCollection.ByState<T> create(final T value) {
|
| 130 | return new WeatheringCopperCollection.ByState<>(value, value, value, value);
|
| 131 | }
|
| 132 |
|
| 133 | public <U> WeatheringCopperCollection.ByState<U> map(final Function<T, U> mapper) {
|
| 134 | return new WeatheringCopperCollection.ByState<>(
|
| 135 | mapper.apply(this.unaffected), mapper.apply(this.exposed), mapper.apply(this.weathered), mapper.apply(this.oxidized)
|
| 136 | );
|
| 137 | }
|
| 138 |
|
| 139 | public T pick(final WeatheringCopper.WeatherState state) {
|
| 140 | return (T)(switch (state) {
|
| 141 | case UNAFFECTED -> this.unaffected;
|
| 142 | case EXPOSED -> this.exposed;
|
| 143 | case WEATHERED -> this.weathered;
|
| 144 | case OXIDIZED -> this.oxidized;
|
| 145 | });
|
| 146 | }
|
| 147 |
|
| 148 | public void forEach(final Consumer<T> consumer) {
|
| 149 | consumer.accept(this.unaffected);
|
| 150 | consumer.accept(this.exposed);
|
| 151 | consumer.accept(this.weathered);
|
| 152 | consumer.accept(this.oxidized);
|
| 153 | }
|
| 154 |
|
| 155 | public void progressMapping(final BiConsumer<T, T> consumer) {
|
| 156 | consumer.accept(this.unaffected, this.exposed);
|
| 157 | consumer.accept(this.exposed, this.weathered);
|
| 158 | consumer.accept(this.weathered, this.oxidized);
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|