下面列出了怎么用net.minecraft.inventory.InventoryCrafting的API类实例代码及写法,或者点击链接到github查看源代码。
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting craftMatrix, World worldIn)
{
for (IRecipe irecipe : this.recipes)
{
if (irecipe.matches(craftMatrix, worldIn))
{
return irecipe.getRemainingItems(craftMatrix);
}
}
NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(craftMatrix.getSizeInventory(), ItemStack.EMPTY);
/*for (int i = 0; i < nonnulllist.size(); ++i)
{
nonnulllist.set(i, craftMatrix.getStackInSlot(i));
}*/
return nonnulllist;
}
@Nonnull
@Override
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
ItemStack resultStack = getRecipeOutput();
ItemStack facadeStack = ItemStack.EMPTY;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack itemStack = inv.getStackInSlot(i);
if (FacadeIngredient.INSTANCE.apply(itemStack)) {
facadeStack = itemStack;
}
}
if (!facadeStack.isEmpty()) {
FacadeItem.setFacadeStack(resultStack, facadeStack);
}
return resultStack;
}
@Override
public ItemStack getCraftingResult(InventoryCrafting crafting) {
// Collect dyeable item and dyes.
ItemStack dyeable = ItemStack.EMPTY;;
List<ItemStack> dyes = new ArrayList<ItemStack>();
for (int i = 0; i < crafting.getSizeInventory(); i++) {
ItemStack stack = crafting.getStackInSlot(i);
if (stack.isEmpty()) continue;
else if (DyeUtils.isDye(stack)) dyes.add(stack);
else if (!(stack.getItem() instanceof IDyeableItem)) return ItemStack.EMPTY;
else if (!((IDyeableItem)stack.getItem()).canDye(stack)) return ItemStack.EMPTY;
else if (!dyeable.isEmpty()) return ItemStack.EMPTY;
else dyeable = stack.copy();
}
if (dyes.isEmpty()) return ItemStack.EMPTY;
// Caclulate and set resulting item's color.
int oldColor = NbtUtils.get(dyeable, -1, "display", "color");
int newColor = DyeUtils.getColorFromDyes(oldColor, dyes);
NbtUtils.set(dyeable, newColor, "display", "color");
return dyeable;
}
@Override
public boolean matches(InventoryCrafting inventoryCrafting, World world){
boolean hasDrone = false, hasPCB = false;
for(int i = 0; i < inventoryCrafting.getSizeInventory(); i++) {
ItemStack stack = inventoryCrafting.getStackInSlot(i);
if(stack != null) {
if(stack.getItem() == Itemss.logisticsDrone) {
if(!hasDrone) hasDrone = true;
else return false;
} else if(stack.getItem() == Itemss.printedCircuitBoard) {
if(!hasPCB) hasPCB = true;
else return false;
}
}
}
return hasDrone && hasPCB;
}
@Test
public void test_useUpItem()
{
ShapedOreRecipe recipe = new DamageableShapedOreRecipe(new ResourceLocation("group"),
new int[] {60}, new ItemStack(Blocks.DIRT),
"A",
'A', new ItemStack(Items.WOODEN_SWORD));
InventoryCrafting inv = new InventoryCrafting(new Container()
{
@Override
public boolean canInteractWith(EntityPlayer playerIn)
{
return false;
}
}, 3, 3);
inv.setInventorySlotContents(0, new ItemStack(Items.WOODEN_SWORD));
assertTrue(recipe.matches(inv, null));
NonNullList<ItemStack> remaining = recipe.getRemainingItems(inv);
assertTrue(remaining.get(0).isEmpty());
}
public ItemStack getCraftingResult(InventoryCrafting ic) {
for (int row = 0; row < 2; row++) {
if (!offsetMatchesDyes(ic, 0, row))
continue;
ItemStack freqowner = ic.getStackInRowAndColumn(1, row + 1);
int freq = freqowner.getItemDamage() & 0xFFF;
int colour1 = recolour(0, row, freq, ic);
int colour2 = recolour(1, row, freq, ic);
int colour3 = recolour(2, row, freq, ic);
ItemStack result = InventoryUtils.copyStack(freqowner, 1);
result.setItemDamage(EnderStorageManager.getFreqFromColours(colour3, colour2, colour1) | freqowner.getItemDamage() & 0xF000);
return result;
}
return null;
}
@Override
public boolean matches(InventoryCrafting grid, World world) {
boolean flag = false;
for (int i = 0; i < grid.getSizeInventory(); i++) {
ItemStack slot = grid.getStackInSlot(i);
if (slot != null && slot.getItem() == Item.getItemFromBlock(ModBlocks.banner)) {
if (flag)
return false;
if (TileEntityBanner.getPatterns(slot) >= 6)
return false;
flag = true;
}
}
if (!flag)
return false;
else
return getPattern(grid) != null;
}
@Override
@Nonnull
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
ItemStack jar = ItemStack.EMPTY;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack.getItem() == ModItems.JAR_ITEM) {
if (stack.getItemDamage() == 2)
jar = stack;
}
}
if (jar.isEmpty()) return ItemStack.EMPTY;
ItemStack jamJar = new ItemStack(ModItems.JAR_ITEM);
NBTHelper.setInt(jamJar, NBTConstants.NBT.FAIRY_COLOR, NBTHelper.getInt(jar, NBTConstants.NBT.FAIRY_COLOR, 0xFFFFFF));
jamJar.setItemDamage(1);
return jamJar;
}
@Override
@Nonnull
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
ItemStack foundStaff = ItemStack.EMPTY;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack.getItem() instanceof IPearlSwappable) {
if (stack.getItemDamage() == 1) {
foundStaff = stack;
break;
}
}
}
if (foundStaff.isEmpty()) return ItemStack.EMPTY;
ItemStack infusedPearl = new ItemStack(ModItems.PEARL_NACRE);
SpellUtils.copySpell(foundStaff, infusedPearl);
return infusedPearl;
}
@Override
public boolean matches(InventoryCrafting inventory, World world){
boolean gaugeFound = false;
boolean canisterFound = false;
for(int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack stack = inventory.getStackInSlot(i);
if(stack != null) {
if(stack.getItem() == Itemss.pressureGauge) {
if(gaugeFound) return false;
gaugeFound = true;
} else if(stack.getItem() == Itemss.airCanister) {
if(canisterFound) return false;
canisterFound = true;
} else return false;
}
}
return gaugeFound && canisterFound;
}
@Override
public boolean matches(InventoryCrafting inventorycrafting, World world)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
ItemStack itemstack = inventorycrafting.getStackInRowAndColumn(i, j);
if (itemstack == null) continue;
if (itemstack.getItem() == Item.getItemFromBlock(Blocks.wool))
{
ItemStack itemstack1 = inventorycrafting.getStackInRowAndColumn(i, j + 1);
if (itemstack1 != null && itemstack1.getItem() == Items.string)
{
return true;
}
return false;
}
return false;
}
}
return false;
}
@Override
public InventoryCrafting getCraftingGrid(){
InventoryCrafting invCrafting = new InventoryCrafting(new Container(){
@Override
public boolean canInteractWith(EntityPlayer p_75145_1_){
return false;
}
}, 3, 3);
for(int y = 0; y < 3; y++) {
ProgWidgetItemFilter itemFilter = (ProgWidgetItemFilter)getConnectedParameters()[y];
for(int x = 0; x < 3 && itemFilter != null; x++) {
invCrafting.setInventorySlotContents(y * 3 + x, itemFilter.getFilter());
itemFilter = (ProgWidgetItemFilter)itemFilter.getConnectedParameters()[0];
}
}
return invCrafting;
}
@Nonnull
@Override
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
ItemStack pearl = ItemStack.EMPTY;
ItemStack staff = ItemStack.EMPTY;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack.getItem() instanceof IPearlSwappable) {
if (stack.getItemDamage() == 0)
staff = stack;
}
if (stack.getItem() == ModItems.PEARL_NACRE)
if (NBTHelper.getBoolean(stack, "infused", false))
pearl = stack;
}
ItemStack newStaff = staff.copy();
SpellUtils.copySpell(pearl, newStaff);
newStaff.setItemDamage(1);
return newStaff;
}
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
NonNullList<ItemStack> remainingItems = ForgeHooks.defaultRecipeGetRemainingItems(inv);
ItemStack gluestick;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack.getItem() == Items.SLIME_BALL) {
gluestick = stack.copy();
remainingItems.set(i, gluestick);
break;
}
}
return remainingItems;
}
public BlueprintResult(InventoryCrafting inv)
{
this.ware = null;
this.canCraft = process(inv);
if (canCraft)
{
remaining = new ItemStack[9];
remaining[wareStack] = ItemStack.copyItemStack(ware);
output = ItemBlueprint.getBlueprintForItem(ware);
}
else
{
remaining = new ItemStack[9];
output = null;
}
}
@Override
public ItemStack getCraftingResult(InventoryCrafting invCrafting){
ItemStack potion = null;
ItemStack ammo = null;
for(int i = 0; i < invCrafting.getSizeInventory(); i++) {
ItemStack stack = invCrafting.getStackInSlot(i);
if(stack != null) {
if(stack.getItem() == Items.potionitem) {
potion = stack;
} else {
ammo = stack;
}
}
}
ammo = ammo.copy();
ItemGunAmmo.setPotion(ammo, potion);
return ammo;
}
private boolean offsetMatchesDyes(InventoryCrafting ic, int col, int row) {
if (!stackMatches(ic.getStackInRowAndColumn(col + 1, row + 1), Item.getItemFromBlock(EnderStorage.blockEnderChest)))
return false;
boolean hasDye = false;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++) {
//ignore chest slot
if(i == row + 1 && j == col + 1)
continue;
ItemStack stack = ic.getStackInRowAndColumn(j, i);
if(i == row && getDyeType(stack) >= 0) {
hasDye = true;
continue;
}
if(stack != null)
return false;
}
return hasDye;
}
@Override
public boolean matches(InventoryCrafting inv, World world) {
boolean glassesFound = false;
boolean targetFound = false;
for (ItemStack itemStack : InventoryUtils.asIterable(inv)) {
if (itemStack != null) {
if (itemStack.getItem() instanceof ItemGlasses) {
if (glassesFound) return false;
glassesFound = true;
} else if (isSuitableItem(itemStack)) {
if (targetFound) return false;
targetFound = true;
} else return false;
}
}
return glassesFound && targetFound;
}
@SubscribeEvent
public void onCrafting(PlayerEvent.ItemCraftedEvent event) {
EntityPlayer player = event.player;
ItemStack item = event.crafting;
IInventory craftMatrix = event.craftMatrix;
if(craftMatrix instanceof InventoryCrafting){
InventoryCrafting craftMatrix1 = (InventoryCrafting) craftMatrix;
IRecipe recipe = ForgeRegistries.RECIPES.getValue(new ResourceLocation(TofuMain.MODID, "soymilk_cloth"));
if(recipe!=null){
if(!item.isEmpty()&&recipe.matches(craftMatrix1, player.world))
player.inventory.addItemStackToInventory(new ItemStack(ItemLoader.material,1,11));
}
}
}
@Override
public ItemStack getCraftingResult(InventoryCrafting inventory){
if(!matches(inventory, null)) return null;
ItemStack output = getRecipeOutput();
for(int i = 0; i < inventory.getSizeInventory(); i++) {
if(inventory.getStackInSlot(i) != null && inventory.getStackInSlot(i).getItem() == Itemss.airCanister) {
output.setItemDamage(inventory.getStackInSlot(i).getItemDamage());
}
}
return output;
}
@Override
public boolean matches(InventoryCrafting p_77569_1_, World p_77569_2_) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
ItemStack toCheck = p_77569_1_.getStackInRowAndColumn(y,x);
ItemStack ref = this.charToStackMap.get(this.shape[x].toCharArray()[y]);
if (!BW_Util.areStacksEqualOrNull(toCheck,ref))
return false;
}
}
return true;
}
private InventoryCrafting getCraftMatrix() {
InventoryCrafting craftMatrix = new InventoryCrafting(new Container()
{
@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
return true;
}
}, 3, 3);
for (int i = 0; i < 9; i++)
craftMatrix.setInventorySlotContents(i, items[i]);
return craftMatrix;
}
public static ItemStack callPreCraftEvent(InventoryCrafting matrix, ItemStack result, InventoryView lastCraftView, boolean isRepair) {
CraftInventoryCrafting inventory = new CraftInventoryCrafting(matrix, matrix.resultInventory);
inventory.setResult(CraftItemStack.asCraftMirror(result));
PrepareItemCraftEvent event = new PrepareItemCraftEvent(inventory, lastCraftView, isRepair);
Bukkit.getPluginManager().callEvent(event);
org.bukkit.inventory.ItemStack bitem = event.getInventory().getResult();
return CraftItemStack.asNMSCopy(bitem);
}
@Override
protected boolean checkMatch(InventoryCrafting inv, int startX, int startY, boolean mirror)
{
int[] amounts = getAmounts(mirror);
for (int x = 0; x < MAX_CRAFT_GRID_WIDTH; x++)
{
for (int y = 0; y < MAX_CRAFT_GRID_HEIGHT; y++)
{
int subX = x - startX;
int subY = y - startY;
int damage = 0;
Ingredient target = Ingredient.EMPTY;
if (subX >= 0 && subY >= 0 && subX < width && subY < height)
{
damage = amounts[subX + width * subY];
if (mirror)
{
target = input.get(width - subX - 1 + subY * width);
} else
{
target = input.get(subX + subY * width);
}
}
ItemStack slot = inv.getStackInRowAndColumn(x, y);
if (!target.apply(slot) || damage > slot.getMaxDamage() - slot.getItemDamage() + 1)
{
return false;
}
}
}
return true;
}
public static ItemStack findMatchingRecipe(Iterable<IRecipe> recipes, InventoryCrafting craftMatrix, World worldIn)
{
for (IRecipe irecipe : recipes)
{
if (irecipe.matches(craftMatrix, worldIn))
{
return irecipe.getCraftingResult(craftMatrix);
}
}
return ItemStack.EMPTY;
}
public Optional<CustomWoodType> inferMaterial(InventoryCrafting ic) {
for (int r = 0; r < ic.getWidth(); r++) {
for (int c = 0; c < ic.getHeight(); c++) {
final ItemStack stack = ic.getStackInRowAndColumn(r, c);
final Optional<CustomWoodType> material = CustomWoodTypeRegistry.getFromStack(stack);
if (material.isPresent()) {
return material;
}
}
}
return Optional.empty();
}
private void doTest(boolean mirror, boolean enoughDamage)
{
ShapedOreRecipe recipe = new DamageableShapedOreRecipe(new ResourceLocation("group"),
new int[] {0, 0, enoughDamage ? 5 : 5000, 0}, new ItemStack(Blocks.DIRT),
"AA", "BA",
'A', new ItemStack(Blocks.DIRT),
'B', new ItemStack(Items.WOODEN_SWORD))
.setMirrored(mirror);
InventoryCrafting inv = new InventoryCrafting(new Container()
{
@Override
public boolean canInteractWith(EntityPlayer playerIn)
{
return false;
}
}, 3, 3);
inv.setInventorySlotContents(4, new ItemStack(Blocks.DIRT));
inv.setInventorySlotContents(5, new ItemStack(Blocks.DIRT));
inv.setInventorySlotContents(mirror ? 8 : 7, new ItemStack(Items.WOODEN_SWORD));
inv.setInventorySlotContents(mirror ? 7 : 8, new ItemStack(Blocks.DIRT));
assertSame(enoughDamage, recipe.matches(inv, null));
if (enoughDamage)
{
NonNullList<ItemStack> remaining = recipe.getRemainingItems(inv);
assertSame(Items.WOODEN_SWORD, remaining.get(mirror ? 8 : 7).getItem());
assertEquals(5, remaining.get(mirror ? 8 : 7).getItemDamage());
}
}
@Override
public boolean matches(@Nonnull InventoryCrafting inv, @Nonnull World worldIn) {
boolean hasHalo = false;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stack = inv.getStackInSlot(i);
if (stack.getItem() == ModItems.FAKE_HALO) {
if (hasHalo) return false;
hasHalo = true;
} else if (!stack.isEmpty()) return false;
}
return hasHalo;
}
@Override
public ItemStack getCraftingResult(InventoryCrafting ic) {
final ItemStack result = super.getCraftingResult(ic);
final Optional<CustomWoodType> material = inferMaterial(ic);
if (material.isPresent()) {
final NBTTagCompound tag = StackHelper.getTag(result);
material.get().writeToNBT(tag);
result.setTagCompound(tag);
}
return result;
}
@Override
public boolean matches(InventoryCrafting invCrafting, World world){
int itemCount = 0;
boolean foundPotion = false;
boolean foundAmmo = false;
for(int i = 0; i < invCrafting.getSizeInventory(); i++) {
ItemStack stack = invCrafting.getStackInSlot(i);
if(stack != null) {
itemCount++;
if(stack.getItem() == Items.potionitem) foundPotion = true;
if(stack.getItem() == Itemss.gunAmmo) foundAmmo = true;
}
}
return foundPotion && foundAmmo && itemCount == 2;
}