ClientRecipeBook.java

net.minecraft.client.ClientRecipeBook

信息

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

    TODO

字段/常量

  • known

    • 类型: Map<RecipeDisplayId,RecipeDisplayEntry>
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • highlight

    • 类型: Set<RecipeDisplayId>
    • 修饰符: private final
    • 源码定位: L28
    • 说明:

      TODO

  • collectionsByTab

    • 类型: Map<ExtendedRecipeBookCategory,List<RecipeCollection>>
    • 修饰符: private
    • 源码定位: L29
    • 说明:

      TODO

  • allCollections

    • 类型: List<RecipeCollection>
    • 修饰符: private
    • 源码定位: L30
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

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

public void add(RecipeDisplayEntry display) @ L32

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

参数:

  • display: RecipeDisplayEntry

说明:

TODO

public void remove(RecipeDisplayId id) @ L36

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

参数:

  • id: RecipeDisplayId

说明:

TODO

public void clear() @ L41

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

参数:

说明:

TODO

public boolean willHighlight(RecipeDisplayId recipe) @ L46

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

参数:

  • recipe: RecipeDisplayId

说明:

TODO

public void removeHighlight(RecipeDisplayId id) @ L50

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

参数:

  • id: RecipeDisplayId

说明:

TODO

public void addHighlight(RecipeDisplayId id) @ L54

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

参数:

  • id: RecipeDisplayId

说明:

TODO

public void rebuildCollections() @ L58

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

参数:

说明:

TODO

private static Map<RecipeBookCategory,List<List<RecipeDisplayEntry>>> categorizeAndGroupRecipes(Iterable<RecipeDisplayEntry> recipes) @ L82

  • 方法名:categorizeAndGroupRecipes
  • 源码定位:L82
  • 返回类型:Map<RecipeBookCategory,List<List>>
  • 修饰符:private static

参数:

  • recipes: Iterable

说明:

TODO

public List<RecipeCollection> getCollections() @ L106

  • 方法名:getCollections
  • 源码定位:L106
  • 返回类型:List
  • 修饰符:public

参数:

说明:

TODO

public List<RecipeCollection> getCollection(ExtendedRecipeBookCategory category) @ L110

  • 方法名:getCollection
  • 源码定位:L110
  • 返回类型:List
  • 修饰符:public

参数:

  • category: ExtendedRecipeBookCategory

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class ClientRecipeBook extends RecipeBook {
    private final Map<RecipeDisplayId, RecipeDisplayEntry> known = new HashMap<>();
    private final Set<RecipeDisplayId> highlight = new HashSet<>();
    private Map<ExtendedRecipeBookCategory, List<RecipeCollection>> collectionsByTab = Map.of();
    private List<RecipeCollection> allCollections = List.of();
 
    public void add(RecipeDisplayEntry display) {
        this.known.put(display.id(), display);
    }
 
    public void remove(RecipeDisplayId id) {
        this.known.remove(id);
        this.highlight.remove(id);
    }
 
    public void clear() {
        this.known.clear();
        this.highlight.clear();
    }
 
    public boolean willHighlight(RecipeDisplayId recipe) {
        return this.highlight.contains(recipe);
    }
 
    public void removeHighlight(RecipeDisplayId id) {
        this.highlight.remove(id);
    }
 
    public void addHighlight(RecipeDisplayId id) {
        this.highlight.add(id);
    }
 
    public void rebuildCollections() {
        Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> recipeListsByCategory = categorizeAndGroupRecipes(this.known.values());
        Map<ExtendedRecipeBookCategory, List<RecipeCollection>> byCategory = new HashMap<>();
        Builder<RecipeCollection> all = ImmutableList.builder();
        recipeListsByCategory.forEach(
            (category, categoryRecipes) -> byCategory.put(
                category, categoryRecipes.stream().map(RecipeCollection::new).peek(all::add).collect(ImmutableList.toImmutableList())
            )
        );
 
        for (SearchRecipeBookCategory searchCategory : SearchRecipeBookCategory.values()) {
            byCategory.put(
                searchCategory,
                searchCategory.includedCategories()
                    .stream()
                    .flatMap(subCategory -> byCategory.getOrDefault(subCategory, List.of()).stream())
                    .collect(ImmutableList.toImmutableList())
            );
        }
 
        this.collectionsByTab = Map.copyOf(byCategory);
        this.allCollections = all.build();
    }
 
    private static Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> categorizeAndGroupRecipes(Iterable<RecipeDisplayEntry> recipes) {
        Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> result = new HashMap<>();
        Table<RecipeBookCategory, Integer, List<RecipeDisplayEntry>> multiItemGroups = HashBasedTable.create();
 
        for (RecipeDisplayEntry entry : recipes) {
            RecipeBookCategory category = entry.category();
            OptionalInt groupId = entry.group();
            if (groupId.isEmpty()) {
                result.computeIfAbsent(category, key -> new ArrayList<>()).add(List.of(entry));
            } else {
                List<RecipeDisplayEntry> groupRecipes = multiItemGroups.get(category, groupId.getAsInt());
                if (groupRecipes == null) {
                    groupRecipes = new ArrayList<>();
                    multiItemGroups.put(category, groupId.getAsInt(), groupRecipes);
                    result.computeIfAbsent(category, key -> new ArrayList<>()).add(groupRecipes);
                }
 
                groupRecipes.add(entry);
            }
        }
 
        return result;
    }
 
    public List<RecipeCollection> getCollections() {
        return this.allCollections;
    }
 
    public List<RecipeCollection> getCollection(ExtendedRecipeBookCategory category) {
        return this.collectionsByTab.getOrDefault(category, Collections.emptyList());
    }
}

引用的其他类