| 1 | package net.minecraft.stats;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import io.netty.buffer.ByteBuf;
|
| 8 | import java.util.function.UnaryOperator;
|
| 9 | import net.minecraft.network.FriendlyByteBuf;
|
| 10 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 11 | import net.minecraft.network.codec.StreamCodec;
|
| 12 | import net.minecraft.world.inventory.RecipeBookType;
|
| 13 |
|
| 14 | public final class RecipeBookSettings {
|
| 15 | public static final StreamCodec<FriendlyByteBuf, RecipeBookSettings> STREAM_CODEC = StreamCodec.composite(
|
| 16 | RecipeBookSettings.TypeSettings.STREAM_CODEC,
|
| 17 | o -> o.crafting,
|
| 18 | RecipeBookSettings.TypeSettings.STREAM_CODEC,
|
| 19 | o -> o.furnace,
|
| 20 | RecipeBookSettings.TypeSettings.STREAM_CODEC,
|
| 21 | o -> o.blastFurnace,
|
| 22 | RecipeBookSettings.TypeSettings.STREAM_CODEC,
|
| 23 | o -> o.smoker,
|
| 24 | RecipeBookSettings::new
|
| 25 | );
|
| 26 | public static final MapCodec<RecipeBookSettings> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 27 | i -> i.group(
|
| 28 | RecipeBookSettings.TypeSettings.CRAFTING_MAP_CODEC.forGetter(o -> o.crafting),
|
| 29 | RecipeBookSettings.TypeSettings.FURNACE_MAP_CODEC.forGetter(o -> o.furnace),
|
| 30 | RecipeBookSettings.TypeSettings.BLAST_FURNACE_MAP_CODEC.forGetter(o -> o.blastFurnace),
|
| 31 | RecipeBookSettings.TypeSettings.SMOKER_MAP_CODEC.forGetter(o -> o.smoker)
|
| 32 | )
|
| 33 | .apply(i, RecipeBookSettings::new)
|
| 34 | );
|
| 35 | private RecipeBookSettings.TypeSettings crafting;
|
| 36 | private RecipeBookSettings.TypeSettings furnace;
|
| 37 | private RecipeBookSettings.TypeSettings blastFurnace;
|
| 38 | private RecipeBookSettings.TypeSettings smoker;
|
| 39 |
|
| 40 | public RecipeBookSettings() {
|
| 41 | this(
|
| 42 | RecipeBookSettings.TypeSettings.DEFAULT,
|
| 43 | RecipeBookSettings.TypeSettings.DEFAULT,
|
| 44 | RecipeBookSettings.TypeSettings.DEFAULT,
|
| 45 | RecipeBookSettings.TypeSettings.DEFAULT
|
| 46 | );
|
| 47 | }
|
| 48 |
|
| 49 | private RecipeBookSettings(
|
| 50 | final RecipeBookSettings.TypeSettings crafting,
|
| 51 | final RecipeBookSettings.TypeSettings furnace,
|
| 52 | final RecipeBookSettings.TypeSettings blastFurnace,
|
| 53 | final RecipeBookSettings.TypeSettings smoker
|
| 54 | ) {
|
| 55 | this.crafting = crafting;
|
| 56 | this.furnace = furnace;
|
| 57 | this.blastFurnace = blastFurnace;
|
| 58 | this.smoker = smoker;
|
| 59 | }
|
| 60 |
|
| 61 | @VisibleForTesting
|
| 62 | public RecipeBookSettings.TypeSettings getSettings(final RecipeBookType type) {
|
| 63 | return switch (type) {
|
| 64 | case CRAFTING -> this.crafting;
|
| 65 | case FURNACE -> this.furnace;
|
| 66 | case BLAST_FURNACE -> this.blastFurnace;
|
| 67 | case SMOKER -> this.smoker;
|
| 68 | };
|
| 69 | }
|
| 70 |
|
| 71 | private void updateSettings(final RecipeBookType recipeBookType, final UnaryOperator<RecipeBookSettings.TypeSettings> operator) {
|
| 72 | switch (recipeBookType) {
|
| 73 | case CRAFTING:
|
| 74 | this.crafting = operator.apply(this.crafting);
|
| 75 | break;
|
| 76 | case FURNACE:
|
| 77 | this.furnace = operator.apply(this.furnace);
|
| 78 | break;
|
| 79 | case BLAST_FURNACE:
|
| 80 | this.blastFurnace = operator.apply(this.blastFurnace);
|
| 81 | break;
|
| 82 | case SMOKER:
|
| 83 | this.smoker = operator.apply(this.smoker);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public boolean isOpen(final RecipeBookType type) {
|
| 88 | return this.getSettings(type).open;
|
| 89 | }
|
| 90 |
|
| 91 | public void setOpen(final RecipeBookType type, final boolean open) {
|
| 92 | this.updateSettings(type, s -> s.setOpen(open));
|
| 93 | }
|
| 94 |
|
| 95 | public boolean isFiltering(final RecipeBookType type) {
|
| 96 | return this.getSettings(type).filtering;
|
| 97 | }
|
| 98 |
|
| 99 | public void setFiltering(final RecipeBookType type, final boolean filtering) {
|
| 100 | this.updateSettings(type, s -> s.setFiltering(filtering));
|
| 101 | }
|
| 102 |
|
| 103 | public RecipeBookSettings copy() {
|
| 104 | return new RecipeBookSettings(this.crafting, this.furnace, this.blastFurnace, this.smoker);
|
| 105 | }
|
| 106 |
|
| 107 | public void replaceFrom(final RecipeBookSettings other) {
|
| 108 | this.crafting = other.crafting;
|
| 109 | this.furnace = other.furnace;
|
| 110 | this.blastFurnace = other.blastFurnace;
|
| 111 | this.smoker = other.smoker;
|
| 112 | }
|
| 113 |
|
| 114 | public record TypeSettings(boolean open, boolean filtering) {
|
| 115 | public static final RecipeBookSettings.TypeSettings DEFAULT = new RecipeBookSettings.TypeSettings(false, false);
|
| 116 | public static final MapCodec<RecipeBookSettings.TypeSettings> CRAFTING_MAP_CODEC = codec("isGuiOpen", "isFilteringCraftable");
|
| 117 | public static final MapCodec<RecipeBookSettings.TypeSettings> FURNACE_MAP_CODEC = codec("isFurnaceGuiOpen", "isFurnaceFilteringCraftable");
|
| 118 | public static final MapCodec<RecipeBookSettings.TypeSettings> BLAST_FURNACE_MAP_CODEC = codec(
|
| 119 | "isBlastingFurnaceGuiOpen", "isBlastingFurnaceFilteringCraftable"
|
| 120 | );
|
| 121 | public static final MapCodec<RecipeBookSettings.TypeSettings> SMOKER_MAP_CODEC = codec("isSmokerGuiOpen", "isSmokerFilteringCraftable");
|
| 122 | public static final StreamCodec<ByteBuf, RecipeBookSettings.TypeSettings> STREAM_CODEC = StreamCodec.composite(
|
| 123 | ByteBufCodecs.BOOL,
|
| 124 | RecipeBookSettings.TypeSettings::open,
|
| 125 | ByteBufCodecs.BOOL,
|
| 126 | RecipeBookSettings.TypeSettings::filtering,
|
| 127 | RecipeBookSettings.TypeSettings::new
|
| 128 | );
|
| 129 |
|
| 130 | @Override
|
| 131 | public String toString() {
|
| 132 | return "[open=" + this.open + ", filtering=" + this.filtering + "]";
|
| 133 | }
|
| 134 |
|
| 135 | public RecipeBookSettings.TypeSettings setOpen(final boolean open) {
|
| 136 | return new RecipeBookSettings.TypeSettings(open, this.filtering);
|
| 137 | }
|
| 138 |
|
| 139 | public RecipeBookSettings.TypeSettings setFiltering(final boolean filtering) {
|
| 140 | return new RecipeBookSettings.TypeSettings(this.open, filtering);
|
| 141 | }
|
| 142 |
|
| 143 | private static MapCodec<RecipeBookSettings.TypeSettings> codec(final String openFieldName, final String filteringFieldName) {
|
| 144 | return RecordCodecBuilder.mapCodec(
|
| 145 | i -> i.group(
|
| 146 | Codec.BOOL.optionalFieldOf(openFieldName, false).forGetter(RecipeBookSettings.TypeSettings::open),
|
| 147 | Codec.BOOL.optionalFieldOf(filteringFieldName, false).forGetter(RecipeBookSettings.TypeSettings::filtering)
|
| 148 | )
|
| 149 | .apply(i, RecipeBookSettings.TypeSettings::new)
|
| 150 | );
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|