| 1 | package net.minecraft.stats;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.function.Consumer;
|
| 14 | import java.util.function.Predicate;
|
| 15 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundRecipeBookAddPacket;
|
| 17 | import net.minecraft.network.protocol.game.ClientboundRecipeBookRemovePacket;
|
| 18 | import net.minecraft.network.protocol.game.ClientboundRecipeBookSettingsPacket;
|
| 19 | import net.minecraft.resources.ResourceKey;
|
| 20 | import net.minecraft.server.level.ServerPlayer;
|
| 21 | import net.minecraft.world.item.crafting.Recipe;
|
| 22 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 23 | import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
|
| 24 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | public class ServerRecipeBook extends RecipeBook {
|
| 28 | public static final String RECIPE_BOOK_TAG = "recipeBook";
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private final ServerRecipeBook.DisplayResolver displayResolver;
|
| 31 | @VisibleForTesting
|
| 32 | protected final Set<ResourceKey<Recipe<?>>> known = Sets.newIdentityHashSet();
|
| 33 | @VisibleForTesting
|
| 34 | protected final Set<ResourceKey<Recipe<?>>> highlight = Sets.newIdentityHashSet();
|
| 35 |
|
| 36 | public ServerRecipeBook(final ServerRecipeBook.DisplayResolver displayResolver) {
|
| 37 | this.displayResolver = displayResolver;
|
| 38 | }
|
| 39 |
|
| 40 | public void add(final ResourceKey<Recipe<?>> id) {
|
| 41 | this.known.add(id);
|
| 42 | }
|
| 43 |
|
| 44 | public boolean contains(final ResourceKey<Recipe<?>> id) {
|
| 45 | return this.known.contains(id);
|
| 46 | }
|
| 47 |
|
| 48 | public void remove(final ResourceKey<Recipe<?>> id) {
|
| 49 | this.known.remove(id);
|
| 50 | this.highlight.remove(id);
|
| 51 | }
|
| 52 |
|
| 53 | public void removeHighlight(final ResourceKey<Recipe<?>> id) {
|
| 54 | this.highlight.remove(id);
|
| 55 | }
|
| 56 |
|
| 57 | private void addHighlight(final ResourceKey<Recipe<?>> id) {
|
| 58 | this.highlight.add(id);
|
| 59 | }
|
| 60 |
|
| 61 | public int addRecipes(final Collection<RecipeHolder<?>> recipes, final ServerPlayer player) {
|
| 62 | List<ClientboundRecipeBookAddPacket.Entry> recipesToAdd = new ArrayList<>();
|
| 63 |
|
| 64 | for (RecipeHolder<?> recipe : recipes) {
|
| 65 | ResourceKey<Recipe<?>> id = recipe.id();
|
| 66 | if (!this.known.contains(id) && !recipe.value().isSpecial()) {
|
| 67 | this.add(id);
|
| 68 | this.addHighlight(id);
|
| 69 | this.displayResolver
|
| 70 | .displaysForRecipe(id, display -> recipesToAdd.add(new ClientboundRecipeBookAddPacket.Entry(display, recipe.value().showNotification(), true)));
|
| 71 | CriteriaTriggers.RECIPE_UNLOCKED.trigger(player, recipe);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | if (!recipesToAdd.isEmpty()) {
|
| 76 | player.connection.send(new ClientboundRecipeBookAddPacket(recipesToAdd, false));
|
| 77 | }
|
| 78 |
|
| 79 | return recipesToAdd.size();
|
| 80 | }
|
| 81 |
|
| 82 | public int removeRecipes(final Collection<RecipeHolder<?>> recipes, final ServerPlayer player) {
|
| 83 | List<RecipeDisplayId> recipesToRemove = Lists.newArrayList();
|
| 84 |
|
| 85 | for (RecipeHolder<?> recipe : recipes) {
|
| 86 | ResourceKey<Recipe<?>> id = recipe.id();
|
| 87 | if (this.known.contains(id)) {
|
| 88 | this.remove(id);
|
| 89 | this.displayResolver.displaysForRecipe(id, display -> recipesToRemove.add(display.id()));
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (!recipesToRemove.isEmpty()) {
|
| 94 | player.connection.send(new ClientboundRecipeBookRemovePacket(recipesToRemove));
|
| 95 | }
|
| 96 |
|
| 97 | return recipesToRemove.size();
|
| 98 | }
|
| 99 |
|
| 100 | private void loadRecipes(
|
| 101 | final List<ResourceKey<Recipe<?>>> recipes, final Consumer<ResourceKey<Recipe<?>>> recipeAddingMethod, final Predicate<ResourceKey<Recipe<?>>> validator
|
| 102 | ) {
|
| 103 | for (ResourceKey<Recipe<?>> recipe : recipes) {
|
| 104 | if (!validator.test(recipe)) {
|
| 105 | LOGGER.error("Tried to load unrecognized recipe: {} removed now.", recipe);
|
| 106 | } else {
|
| 107 | recipeAddingMethod.accept(recipe);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | public void sendInitialRecipeBook(final ServerPlayer player) {
|
| 113 | player.connection.send(new ClientboundRecipeBookSettingsPacket(this.getBookSettings().copy()));
|
| 114 | List<ClientboundRecipeBookAddPacket.Entry> recipesToSend = new ArrayList<>(this.known.size());
|
| 115 |
|
| 116 | for (ResourceKey<Recipe<?>> id : this.known) {
|
| 117 | this.displayResolver.displaysForRecipe(id, r -> recipesToSend.add(new ClientboundRecipeBookAddPacket.Entry(r, false, this.highlight.contains(id))));
|
| 118 | }
|
| 119 |
|
| 120 | player.connection.send(new ClientboundRecipeBookAddPacket(recipesToSend, true));
|
| 121 | }
|
| 122 |
|
| 123 | public void copyOverData(final ServerRecipeBook bookToCopy) {
|
| 124 | this.apply(bookToCopy.pack());
|
| 125 | }
|
| 126 |
|
| 127 | public ServerRecipeBook.Packed pack() {
|
| 128 | return new ServerRecipeBook.Packed(this.bookSettings.copy(), List.copyOf(this.known), List.copyOf(this.highlight));
|
| 129 | }
|
| 130 |
|
| 131 | private void apply(final ServerRecipeBook.Packed packed) {
|
| 132 | this.known.clear();
|
| 133 | this.highlight.clear();
|
| 134 | this.bookSettings.replaceFrom(packed.settings);
|
| 135 | this.known.addAll(packed.known);
|
| 136 | this.highlight.addAll(packed.highlight);
|
| 137 | }
|
| 138 |
|
| 139 | public void loadUntrusted(final ServerRecipeBook.Packed packed, final Predicate<ResourceKey<Recipe<?>>> validator) {
|
| 140 | this.bookSettings.replaceFrom(packed.settings);
|
| 141 | this.loadRecipes(packed.known, this.known::add, validator);
|
| 142 | this.loadRecipes(packed.highlight, this.highlight::add, validator);
|
| 143 | }
|
| 144 |
|
| 145 | @FunctionalInterface
|
| 146 | public interface DisplayResolver {
|
| 147 | void displaysForRecipe(ResourceKey<Recipe<?>> id, Consumer<RecipeDisplayEntry> output);
|
| 148 | }
|
| 149 |
|
| 150 | public record Packed(RecipeBookSettings settings, List<ResourceKey<Recipe<?>>> known, List<ResourceKey<Recipe<?>>> highlight) {
|
| 151 | public static final Codec<ServerRecipeBook.Packed> CODEC = RecordCodecBuilder.create(
|
| 152 | i -> i.group(
|
| 153 | RecipeBookSettings.MAP_CODEC.forGetter(ServerRecipeBook.Packed::settings),
|
| 154 | Recipe.KEY_CODEC.listOf().fieldOf("recipes").forGetter(ServerRecipeBook.Packed::known),
|
| 155 | Recipe.KEY_CODEC.listOf().fieldOf("toBeDisplayed").forGetter(ServerRecipeBook.Packed::highlight)
|
| 156 | )
|
| 157 | .apply(i, ServerRecipeBook.Packed::new)
|
| 158 | );
|
| 159 | }
|
| 160 | }
|
| 161 |
|