use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.
the class RecipeRegistry method findRecipe.
@Nullable
public IArtisanRecipe findRecipe(int playerExperience, int playerLevels, boolean isPlayerCreative, ItemStack[] tools, ICraftingMatrixStackHandler craftingMatrix, @Nullable FluidStack fluidStack, ISecondaryIngredientMatcher secondaryIngredientMatcher, EnumTier tier, Map<ResourceLocation, IRequirementContext> requirementContextMap) {
// If the recipe list is empty, short-circuit.
if (this.recipeList.isEmpty()) {
return null;
}
// Next, check the last recipe first.
IArtisanRecipe lastRecipe = this.recipeList.get(this.recipeList.size() - 1);
boolean lastRecipeMatches = lastRecipe.matches(requirementContextMap, playerExperience, playerLevels, isPlayerCreative, tools, craftingMatrix, fluidStack, secondaryIngredientMatcher, tier);
if (lastRecipeMatches) {
return lastRecipe;
}
// Next, loop through the remaining recipes in reverse.
for (int i = this.recipeList.size() - 2; i >= 0; i--) {
IArtisanRecipe recipe = this.recipeList.get(i);
boolean matches = recipe.matches(requirementContextMap, playerExperience, playerLevels, isPlayerCreative, tools, craftingMatrix, fluidStack, secondaryIngredientMatcher, tier);
if (matches) {
// If the recipe matches, move it to the end of the list. This ensures that the
// recipe will be checked faster next time, increasing performance for shift +
// click crafting operations.
//
// Worst case remove: O(n) for re-indexing.
// Worst case add: O(1) because we're adding to the end of the list.
this.recipeList.remove(i);
this.recipeList.add(recipe);
return recipe;
}
}
// Finally, if no recipe was matched, return null.
return null;
}
use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.
the class TileEntityBase method onTakeResult.
public void onTakeResult(EntityPlayer player) {
IArtisanRecipe recipe = this.getRecipe(player);
if (recipe == null) {
return;
}
recipe.doCraft(this.getCraftingContext(player));
this.markDirty();
if (!this.world.isRemote) {
this.notifyBlockUpdate();
}
}
use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.
the class Container method transferStackInSlot.
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) {
ItemStack itemStackCopy = ItemStack.EMPTY;
Slot slot = this.inventorySlots.get(slotIndex);
if (slot != null && slot.getHasStack()) {
ItemStack itemStack = slot.getStack();
itemStackCopy = itemStack.copy();
if (this.isSlotIndexResult(slotIndex)) {
// Result
// This is executed on both the client and server for each craft. If the crafting
// grid has multiple, complete recipes, this will be executed for each complete
// recipe.
IArtisanRecipe recipe = this.tile.getRecipe(playerIn);
if (recipe == null) {
return ItemStack.EMPTY;
}
if (recipe.hasMultipleWeightedOutputs()) {
// it has been retrieved.
return ItemStack.EMPTY;
}
if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
return ItemStack.EMPTY;
}
itemStack.getItem().onCreated(itemStack, this.world, playerIn);
slot.onSlotChange(itemStack, itemStackCopy);
} else if (this.isSlotIndexInventory(slotIndex)) {
if (this.swapTools(slotIndex)) {
// swapped tools
return ItemStack.EMPTY;
}
if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
return ItemStack.EMPTY;
}
} else if (this.isSlotIndexHotbar(slotIndex)) {
if (this.swapTools(slotIndex)) {
// swapped tools
return ItemStack.EMPTY;
}
if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false)) {
return ItemStack.EMPTY;
}
} else if (this.isSlotIndexToolbox(slotIndex)) {
if (this.swapTools(slotIndex)) {
// swapped tools
return ItemStack.EMPTY;
}
if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
return ItemStack.EMPTY;
}
} else if (this.isSlotIndexTool(slotIndex)) {
if (this.canPlayerUseToolbox()) {
if (!this.mergeToolbox(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
return ItemStack.EMPTY;
}
} else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
return ItemStack.EMPTY;
}
} else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
// All others: crafting matrix, secondary output
return ItemStack.EMPTY;
}
if (itemStack.isEmpty()) {
slot.putStack(ItemStack.EMPTY);
} else {
slot.onSlotChanged();
}
if (itemStack.getCount() == itemStackCopy.getCount()) {
return ItemStack.EMPTY;
}
ItemStack itemStack2 = slot.onTake(playerIn, itemStack);
if (slotIndex == 0) {
playerIn.dropItem(itemStack2, false);
}
}
return itemStackCopy;
}
use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.
the class Container method updateRecipeOutput.
public void updateRecipeOutput() {
if (this.tile == null) {
return;
}
IArtisanRecipe recipe = this.tile.getRecipe(this.player);
if (recipe != null) {
ICraftingContext context = this.tile.getCraftingContext(this.player);
this.resultHandler.setStackInSlot(0, recipe.getBaseOutput(context).toItemStack());
} else {
this.resultHandler.setStackInSlot(0, ItemStack.EMPTY);
}
}
use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.
the class PluginJEI method register.
@Override
public void register(IModRegistry registry) {
for (EnumTier tier : EnumTier.values()) {
if (!ModuleWorktablesConfig.isTierEnabled(tier)) {
continue;
}
for (String name : ArtisanAPI.getWorktableNames()) {
registry.addRecipeCatalyst(this.getWorktableAsItemStack(name, tier), PluginJEI.createUID(name, tier));
}
for (String name : ArtisanAPI.getWorktableNames()) {
registry.handleRecipes(ArtisanRecipe.class, JEIRecipeWrapper::new, PluginJEI.createUID(name, tier));
}
IRecipeTransferRegistry transferRegistry = registry.getRecipeTransferRegistry();
for (String name : ArtisanAPI.getWorktableNames()) {
transferRegistry.addRecipeTransferHandler(new JEIRecipeTransferInfoWorktable(name, PluginJEI.createUID(name, tier), tier));
}
for (String name : ArtisanAPI.getWorktableNames()) {
List<IArtisanRecipe> recipeList = new ArrayList<>();
RecipeRegistry recipeRegistry = ArtisanAPI.getWorktableRecipeRegistry(name);
recipeList = recipeRegistry.getRecipeListByTier(recipeList, tier);
registry.addRecipes(recipeList, PluginJEI.createUID(name, tier));
}
}
}
Aggregations