ServerRecipeBook.java

net.minecraft.stats.ServerRecipeBook

信息

  • 全限定名:net.minecraft.stats.ServerRecipeBook
  • 类型:public class
  • 包:net.minecraft.stats
  • 源码路径:src/main/java/net/minecraft/stats/ServerRecipeBook.java
  • 起始行号:L27
  • 继承:RecipeBook
  • 职责:

    TODO

字段/常量

  • RECIPE_BOOK_TAG

    • 类型: String
    • 修饰符: public static final
    • 源码定位: L28
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L29
    • 说明:

      TODO

  • displayResolver

    • 类型: ServerRecipeBook.DisplayResolver
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • known

    • 类型: Set<ResourceKey<Recipe<?>>>
    • 修饰符: protected final
    • 源码定位: L31
    • 说明:

      TODO

  • highlight

    • 类型: Set<ResourceKey<Recipe<?>>>
    • 修饰符: protected final
    • 源码定位: L33
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.stats.ServerRecipeBook.DisplayResolver

    • 类型: interface
    • 修饰符: public
    • 源码定位: L148
    • 说明:

      TODO

  • net.minecraft.stats.ServerRecipeBook.Packed

    • 类型: record
    • 修饰符: public
    • 源码定位: L152
    • 说明:

      TODO

构造器

public ServerRecipeBook(ServerRecipeBook.DisplayResolver displayResolver) @ L36

  • 构造器名:ServerRecipeBook
  • 源码定位:L36
  • 修饰符:public

参数:

  • displayResolver: ServerRecipeBook.DisplayResolver

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void add(ResourceKey<Recipe<?>> id) @ L40

  • 方法名:add
  • 源码定位:L40
  • 返回类型:void
  • 修饰符:public

参数:

  • id: ResourceKey<Recipe<?>>

说明:

TODO

public boolean contains(ResourceKey<Recipe<?>> id) @ L44

  • 方法名:contains
  • 源码定位:L44
  • 返回类型:boolean
  • 修饰符:public

参数:

  • id: ResourceKey<Recipe<?>>

说明:

TODO

public void remove(ResourceKey<Recipe<?>> id) @ L48

  • 方法名:remove
  • 源码定位:L48
  • 返回类型:void
  • 修饰符:public

参数:

  • id: ResourceKey<Recipe<?>>

说明:

TODO

public void removeHighlight(ResourceKey<Recipe<?>> id) @ L53

  • 方法名:removeHighlight
  • 源码定位:L53
  • 返回类型:void
  • 修饰符:public

参数:

  • id: ResourceKey<Recipe<?>>

说明:

TODO

private void addHighlight(ResourceKey<Recipe<?>> id) @ L57

  • 方法名:addHighlight
  • 源码定位:L57
  • 返回类型:void
  • 修饰符:private

参数:

  • id: ResourceKey<Recipe<?>>

说明:

TODO

public int addRecipes(Collection<RecipeHolder<?>> recipes, ServerPlayer player) @ L61

  • 方法名:addRecipes
  • 源码定位:L61
  • 返回类型:int
  • 修饰符:public

参数:

  • recipes: Collection<RecipeHolder<?>>
  • player: ServerPlayer

说明:

TODO

public int removeRecipes(Collection<RecipeHolder<?>> recipes, ServerPlayer player) @ L84

  • 方法名:removeRecipes
  • 源码定位:L84
  • 返回类型:int
  • 修饰符:public

参数:

  • recipes: Collection<RecipeHolder<?>>
  • player: ServerPlayer

说明:

TODO

private void loadRecipes(List<ResourceKey<Recipe<?>>> recipes, Consumer<ResourceKey<Recipe<?>>> recipeAddingMethod, Predicate<ResourceKey<Recipe<?>>> validator) @ L102

  • 方法名:loadRecipes
  • 源码定位:L102
  • 返回类型:void
  • 修饰符:private

参数:

  • recipes: List<ResourceKey<Recipe<?>>>
  • recipeAddingMethod: Consumer<ResourceKey<Recipe<?>>>
  • validator: Predicate<ResourceKey<Recipe<?>>>

说明:

TODO

public void sendInitialRecipeBook(ServerPlayer player) @ L114

  • 方法名:sendInitialRecipeBook
  • 源码定位:L114
  • 返回类型:void
  • 修饰符:public

参数:

  • player: ServerPlayer

说明:

TODO

public void copyOverData(ServerRecipeBook bookToCopy) @ L125

  • 方法名:copyOverData
  • 源码定位:L125
  • 返回类型:void
  • 修饰符:public

参数:

  • bookToCopy: ServerRecipeBook

说明:

TODO

public ServerRecipeBook.Packed pack() @ L129

  • 方法名:pack
  • 源码定位:L129
  • 返回类型:ServerRecipeBook.Packed
  • 修饰符:public

参数:

说明:

TODO

private void apply(ServerRecipeBook.Packed packed) @ L133

  • 方法名:apply
  • 源码定位:L133
  • 返回类型:void
  • 修饰符:private

参数:

  • packed: ServerRecipeBook.Packed

说明:

TODO

public void loadUntrusted(ServerRecipeBook.Packed packed, Predicate<ResourceKey<Recipe<?>>> validator) @ L141

  • 方法名:loadUntrusted
  • 源码定位:L141
  • 返回类型:void
  • 修饰符:public

参数:

  • packed: ServerRecipeBook.Packed
  • validator: Predicate<ResourceKey<Recipe<?>>>

说明:

TODO

代码

public class ServerRecipeBook extends RecipeBook {
    public static final String RECIPE_BOOK_TAG = "recipeBook";
    private static final Logger LOGGER = LogUtils.getLogger();
    private final ServerRecipeBook.DisplayResolver displayResolver;
    @VisibleForTesting
    protected final Set<ResourceKey<Recipe<?>>> known = Sets.newIdentityHashSet();
    @VisibleForTesting
    protected final Set<ResourceKey<Recipe<?>>> highlight = Sets.newIdentityHashSet();
 
    public ServerRecipeBook(ServerRecipeBook.DisplayResolver displayResolver) {
        this.displayResolver = displayResolver;
    }
 
    public void add(ResourceKey<Recipe<?>> id) {
        this.known.add(id);
    }
 
    public boolean contains(ResourceKey<Recipe<?>> id) {
        return this.known.contains(id);
    }
 
    public void remove(ResourceKey<Recipe<?>> id) {
        this.known.remove(id);
        this.highlight.remove(id);
    }
 
    public void removeHighlight(ResourceKey<Recipe<?>> id) {
        this.highlight.remove(id);
    }
 
    private void addHighlight(ResourceKey<Recipe<?>> id) {
        this.highlight.add(id);
    }
 
    public int addRecipes(Collection<RecipeHolder<?>> recipes, ServerPlayer player) {
        List<ClientboundRecipeBookAddPacket.Entry> recipesToAdd = new ArrayList<>();
 
        for (RecipeHolder<?> recipe : recipes) {
            ResourceKey<Recipe<?>> id = recipe.id();
            if (!this.known.contains(id) && !recipe.value().isSpecial()) {
                this.add(id);
                this.addHighlight(id);
                this.displayResolver
                    .displaysForRecipe(
                        id, display -> recipesToAdd.add(new ClientboundRecipeBookAddPacket.Entry(display, recipe.value().showNotification(), true))
                    );
                CriteriaTriggers.RECIPE_UNLOCKED.trigger(player, recipe);
            }
        }
 
        if (!recipesToAdd.isEmpty()) {
            player.connection.send(new ClientboundRecipeBookAddPacket(recipesToAdd, false));
        }
 
        return recipesToAdd.size();
    }
 
    public int removeRecipes(Collection<RecipeHolder<?>> recipes, ServerPlayer player) {
        List<RecipeDisplayId> recipesToRemove = Lists.newArrayList();
 
        for (RecipeHolder<?> recipe : recipes) {
            ResourceKey<Recipe<?>> id = recipe.id();
            if (this.known.contains(id)) {
                this.remove(id);
                this.displayResolver.displaysForRecipe(id, display -> recipesToRemove.add(display.id()));
            }
        }
 
        if (!recipesToRemove.isEmpty()) {
            player.connection.send(new ClientboundRecipeBookRemovePacket(recipesToRemove));
        }
 
        return recipesToRemove.size();
    }
 
    private void loadRecipes(
        List<ResourceKey<Recipe<?>>> recipes, Consumer<ResourceKey<Recipe<?>>> recipeAddingMethod, Predicate<ResourceKey<Recipe<?>>> validator
    ) {
        for (ResourceKey<Recipe<?>> recipe : recipes) {
            if (!validator.test(recipe)) {
                LOGGER.error("Tried to load unrecognized recipe: {} removed now.", recipe);
            } else {
                recipeAddingMethod.accept(recipe);
            }
        }
    }
 
    public void sendInitialRecipeBook(ServerPlayer player) {
        player.connection.send(new ClientboundRecipeBookSettingsPacket(this.getBookSettings().copy()));
        List<ClientboundRecipeBookAddPacket.Entry> recipesToSend = new ArrayList<>(this.known.size());
 
        for (ResourceKey<Recipe<?>> id : this.known) {
            this.displayResolver.displaysForRecipe(id, r -> recipesToSend.add(new ClientboundRecipeBookAddPacket.Entry(r, false, this.highlight.contains(id))));
        }
 
        player.connection.send(new ClientboundRecipeBookAddPacket(recipesToSend, true));
    }
 
    public void copyOverData(ServerRecipeBook bookToCopy) {
        this.apply(bookToCopy.pack());
    }
 
    public ServerRecipeBook.Packed pack() {
        return new ServerRecipeBook.Packed(this.bookSettings.copy(), List.copyOf(this.known), List.copyOf(this.highlight));
    }
 
    private void apply(ServerRecipeBook.Packed packed) {
        this.known.clear();
        this.highlight.clear();
        this.bookSettings.replaceFrom(packed.settings);
        this.known.addAll(packed.known);
        this.highlight.addAll(packed.highlight);
    }
 
    public void loadUntrusted(ServerRecipeBook.Packed packed, Predicate<ResourceKey<Recipe<?>>> validator) {
        this.bookSettings.replaceFrom(packed.settings);
        this.loadRecipes(packed.known, this.known::add, validator);
        this.loadRecipes(packed.highlight, this.highlight::add, validator);
    }
 
    @FunctionalInterface
    public interface DisplayResolver {
        void displaysForRecipe(ResourceKey<Recipe<?>> id, Consumer<RecipeDisplayEntry> output);
    }
 
    public record Packed(RecipeBookSettings settings, List<ResourceKey<Recipe<?>>> known, List<ResourceKey<Recipe<?>>> highlight) {
        public static final Codec<ServerRecipeBook.Packed> CODEC = RecordCodecBuilder.create(
            i -> i.group(
                    RecipeBookSettings.MAP_CODEC.forGetter(ServerRecipeBook.Packed::settings),
                    Recipe.KEY_CODEC.listOf().fieldOf("recipes").forGetter(ServerRecipeBook.Packed::known),
                    Recipe.KEY_CODEC.listOf().fieldOf("toBeDisplayed").forGetter(ServerRecipeBook.Packed::highlight)
                )
                .apply(i, ServerRecipeBook.Packed::new)
        );
    }
}

引用的其他类