下面列出了怎么用net.minecraftforge.common.crafting.IShapedRecipe的API类实例代码及写法,或者点击链接到github查看源代码。
public StandardRecipePage(String title, String description, @Nonnull ItemStack resultingItem) {
addComponent(new GuiComponentSprite(75, 40, iconArrow));
addComponent(new GuiComponentItemStackSpinner(140, 30, resultingItem));
{
final IRecipe recipe = RecipeUtils.getFirstRecipeForItemStack(resultingItem);
if (recipe != null) {
final ItemStack[][] input = RecipeUtils.getFullRecipeInput(recipe);
if (input != null) {
final int width = (recipe instanceof IShapedRecipe)? ((IShapedRecipe)recipe).getRecipeWidth() : 3;
addComponent(new GuiComponentCraftingGrid(10, 20, input, width, iconCraftingGrid));
}
}
}
{
String translatedTitle = TranslationUtils.translateToLocal(title);
final GuiComponentLabel titleLabel = new GuiComponentLabel(0, 0, translatedTitle);
titleLabel.setScale(BookScaleConfig.getPageTitleScale());
addComponent(new GuiComponentHCenter(0, 2, getWidth()).addComponent(titleLabel));
}
{
String translatedDescription = TranslationUtils.translateToLocal(description).replaceAll("\\\\n", "\n");
GuiComponentLabel lblDescription = new GuiComponentLabel(10, 80, getWidth() - 5, 200, translatedDescription);
lblDescription.setScale(BookScaleConfig.getPageContentScale());
lblDescription.setAdditionalLineHeight(BookScaleConfig.getRecipePageSeparator());
addComponent(lblDescription);
}
}
/**
* @author qouteall
* @reason https://github.com/FabricMC/Mixin/issues/15
*/
@Overwrite
default void alignRecipeToGrid(
int gridWidth, int gridHeight, int gridOutputSlot,
Recipe<?> recipe, Iterator<?> inputs, int amount
) {
int width = gridWidth;
int height = gridHeight;
// change start
if (recipe instanceof IShapedRecipe) {
IShapedRecipe<?> shapedRecipe = (IShapedRecipe<?>) recipe;
width = shapedRecipe.getRecipeWidth();
height = shapedRecipe.getRecipeHeight();
}
// change end
int slot = 0;
for (int y = 0; y < gridHeight; ++y) {
if (slot == gridOutputSlot) {
++slot;
}
boolean bl = (float) height < (float) gridHeight / 2.0F;
int m = MathHelper.floor((float) gridHeight / 2.0F - (float) height / 2.0F);
if (bl && m > y) {
slot += gridWidth;
++y;
}
for (int x = 0; x < gridWidth; ++x) {
if (!inputs.hasNext()) {
return;
}
bl = (float) width < (float) gridWidth / 2.0F;
m = MathHelper.floor((float) gridWidth / 2.0F - (float) width / 2.0F);
int o = width;
boolean bl2 = x < width;
if (bl) {
o = m + width;
bl2 = m <= x && x < m + width;
}
if (bl2) {
this.acceptAlignedInput(inputs, slot, amount, y, x);
} else if (o == x) {
slot += gridWidth - x;
break;
}
++slot;
}
}
}