PlaceRecipeHelper.java
net.minecraft.recipebook.PlaceRecipeHelper
信息
- 全限定名:net.minecraft.recipebook.PlaceRecipeHelper
- 类型:public interface
- 包:net.minecraft.recipebook
- 源码路径:src/main/java/net/minecraft/recipebook/PlaceRecipeHelper.java
- 起始行号:L8
- 职责:
TODO
字段/常量
- 无
内部类/嵌套类型
net.minecraft.recipebook.PlaceRecipeHelper.Output- 类型:
interface - 修饰符:
public - 源码定位:
L56 - 说明:
TODO
- 类型:
构造器
- 无
方法
下面的方法块按源码顺序生成。
static <T> void placeRecipe(int gridWidth, int gridHeight, Recipe<?> recipe, Iterable<T> entries, PlaceRecipeHelper.Output<T> output) @ L9
- 方法名:placeRecipe
- 源码定位:L9
- 返回类型:
void - 修饰符:static
参数:
- gridWidth: int
- gridHeight: int
- recipe: Recipe<?>
- entries: Iterable
- output: PlaceRecipeHelper.Output
说明:
TODO
static <T> void placeRecipe(int gridWidth, int gridHeight, int recipeWidth, int recipeHeight, Iterable<T> entries, PlaceRecipeHelper.Output<T> output) @ L17
- 方法名:placeRecipe
- 源码定位:L17
- 返回类型:
void - 修饰符:static
参数:
- gridWidth: int
- gridHeight: int
- recipeWidth: int
- recipeHeight: int
- entries: Iterable
- output: PlaceRecipeHelper.Output
说明:
TODO
代码
public interface PlaceRecipeHelper {
static <T> void placeRecipe(int gridWidth, int gridHeight, Recipe<?> recipe, Iterable<T> entries, PlaceRecipeHelper.Output<T> output) {
if (recipe instanceof ShapedRecipe shapedRecipe) {
placeRecipe(gridWidth, gridHeight, shapedRecipe.getWidth(), shapedRecipe.getHeight(), entries, output);
} else {
placeRecipe(gridWidth, gridHeight, gridWidth, gridHeight, entries, output);
}
}
static <T> void placeRecipe(int gridWidth, int gridHeight, int recipeWidth, int recipeHeight, Iterable<T> entries, PlaceRecipeHelper.Output<T> output) {
Iterator<T> iterator = entries.iterator();
int gridIndex = 0;
for (int gridYPos = 0; gridYPos < gridHeight; gridYPos++) {
boolean shouldCenterRecipe = recipeHeight < gridHeight / 2.0F;
int startPosCenterRecipe = Mth.floor(gridHeight / 2.0F - recipeHeight / 2.0F);
if (shouldCenterRecipe && startPosCenterRecipe > gridYPos) {
gridIndex += gridWidth;
gridYPos++;
}
for (int gridXPos = 0; gridXPos < gridWidth; gridXPos++) {
if (!iterator.hasNext()) {
return;
}
shouldCenterRecipe = recipeWidth < gridWidth / 2.0F;
startPosCenterRecipe = Mth.floor(gridWidth / 2.0F - recipeWidth / 2.0F);
int totalRecipeWidthInGrid = recipeWidth;
boolean addIngredientToSlot = gridXPos < recipeWidth;
if (shouldCenterRecipe) {
totalRecipeWidthInGrid = startPosCenterRecipe + recipeWidth;
addIngredientToSlot = startPosCenterRecipe <= gridXPos && gridXPos < startPosCenterRecipe + recipeWidth;
}
if (addIngredientToSlot) {
output.addItemToSlot(iterator.next(), gridIndex, gridXPos, gridYPos);
} else if (totalRecipeWidthInGrid == gridXPos) {
gridIndex += gridWidth - gridXPos;
break;
}
gridIndex++;
}
}
}
@FunctionalInterface
public interface Output<T> {
void addItemToSlot(T item, int gridIndex, int gridXPos, int gridYPos);
}
}