private boolean matrixMatch(CraftingInventory craftingInventory, int maxColumns, int maxRows, boolean mirrored) { for (int c = 0; c < 3; c++) { for (int r = 0; r < 3; r++) { int colNum = c - maxColumns; int rowNum = r - maxRows; CraftingIngredient choiceIngredient = InventoryUtils::isEmptyStack; if (colNum >= 0 && rowNum >= 0 && colNum < width && rowNum < heigth) { if (mirrored) { choiceIngredient = ingredients.get(width - 1 - colNum + rowNum * width); } else { choiceIngredient = ingredients.get(colNum + rowNum * width); } } if (!choiceIngredient.isIngredient(craftingInventory.getItem(colNum + rowNum * width))) { return false; } } } return true; }
/** * {@inheritDoc} */ @Override public List<? extends ItemStack> getLeftOverItems(CraftingInventory craftingInventory) { ItemStack[] matrix = craftingInventory.getMatrix(); List<ItemStack> leftOver = new ArrayList<>(matrix.length); for (int i = 0; i < matrix.length; i++) { ItemStack itemStack = matrix[i]; if (InventoryUtils.isEmptyStack(itemStack)) { leftOver.add(null); continue; } ItemStack clone = itemStack.clone(); MaterialData craftingResult = InventoryUtils.getSingleIngredientResult(itemStack.getData()); if (craftingResult.getItemType() != Material.AIR) { clone.setData(craftingResult); leftOver.add(clone); continue; } } craftingInventory.setContents(leftOver.toArray(new ItemStack[matrix.length])); return leftOver; }
/** * {@inheritDoc} */ @Override public boolean matches(CraftingInventory craftingInventory, World world) { final List<ChoiceIngredient> ingredients = new ArrayList<>(this.ingredients); final List<ItemStack> contents = Arrays.asList(craftingInventory.getMatrix()) .stream().filter(i -> !InventoryUtils.isEmptyStack(i)) .collect(Collectors.toList()); for (ItemStack stack : contents) { boolean match = false; for (int ingredientIndex = 0; ingredientIndex < ingredients.size(); ingredientIndex++) { ChoiceIngredient ingredient = ingredients.get(ingredientIndex); if (ingredient.isIngredient(stack)) { ingredients.remove(ingredientIndex); match = true; break; } } //there was no matching ingredient for the current ItemStack if (!match) return false; } //return true if there are no unused ingredients left over return ingredients.isEmpty(); }
@EventHandler public void onPrepareCraft(PrepareItemCraftEvent e) { CraftingInventory inv = e.getInventory(); ItemStack result = inv.getResult(); if(result != null && result.getType() != Material.GLASS_BOTTLE) { List<ItemStack> bottles = Arrays.asList(inv.getMatrix()); Iterator<ItemStack> it = bottles.iterator(); while(it.hasNext()) { ItemStack bottle = it.next(); if(bottle == null) continue; if(bottle.getType().equals(Material.POTION)) { if(!checkWaterBottle(bottle)) { inv.setResult(null); return; } } } } }
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onPrepareCrowbarCraft(PrepareItemCraftEvent event) { CraftingInventory inventory = event.getInventory(); if (event.isRepair() && event.getRecipe().getResult().getType() == Crowbar.CROWBAR_TYPE) { int endFrameUses = 0; int spawnerUses = 0; boolean changed = false; ItemStack[] matrix = inventory.getMatrix(); for (ItemStack ingredient : matrix) { Optional<Crowbar> crowbarOptional = Crowbar.fromStack(ingredient); if (crowbarOptional.isPresent()) { Crowbar crowbar = crowbarOptional.get(); spawnerUses += crowbar.getSpawnerUses(); endFrameUses += crowbar.getEndFrameUses(); changed = true; } } // Finally update the result. if (changed) { inventory.setResult(new Crowbar(spawnerUses, endFrameUses).getItemIfPresent()); } } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent e) { HumanEntity human = e.getWhoClicked(); if (human instanceof Player && e.getInventory() instanceof CraftingInventory) { final Player player = (Player) human; if (e.getCurrentItem() != null) { if (ArmorType.isArmor(e.getCurrentItem().getTypeId())) { sendDelayedArmorUpdate(player); return; } } if (e.getRawSlot() >= 5 && e.getRawSlot() <= 8) { sendDelayedArmorUpdate(player); } } }
/** * {@inheritDoc} */ @Override public boolean matches(CraftingInventory craftingInventory, World world) { int width, heigth; //check boundaries for the crafting inventory InventoryType type = craftingInventory.getType(); switch(type) { case CRAFTING: width = heigth = 2; break; case WORKBENCH: width = heigth = 3; break; default: return false; //unknown crafting inventory type. } for (int w = 0; w <= width - this.width; w++) { for (int h = 0; h <= heigth - this.heigth; h++) { if (matrixMatch(craftingInventory, w, h, true)) { return true; } if (matrixMatch(craftingInventory, w, h, false)) { return true; } } } return false; }
@Override public boolean matches(CraftingInventory craftingInventory, World world) { CraftWorld cWorld = (CraftWorld) world; WorldServer nmsWorld = cWorld.getHandle(); InventoryCrafting nmsCraftingInventory = getNmsCraftingInventory(craftingInventory); return nmsRecipe.a(nmsCraftingInventory, nmsWorld); }
@Override public List<CraftItemStack> getLeftOverItems(CraftingInventory craftingInventory) { InventoryCrafting nmsCraftingInventory = getNmsCraftingInventory(craftingInventory); return nmsRecipe.b(nmsCraftingInventory).stream() .map(CraftItemStack::asCraftMirror) .collect(Collectors.toList()); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.WOOD_SWORD || recipe.getResult().getType() == Material.STONE_SWORD || recipe.getResult().getType() == Material.IRON_SWORD || recipe.getResult().getType() == Material.GOLD_SWORD || recipe.getResult().getType() == Material.DIAMOND_SWORD) inventory.setResult(new ItemStack(Material.AIR)); }
private void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.WOOD_SWORD) inventory.setResult(new ItemStack(this.getMaterial(Material.WOOD_SWORD))); else if (recipe.getResult().getType() == Material.WOOD_PICKAXE) inventory.setResult(new ItemStack(this.getMaterial(Material.WOOD_PICKAXE))); else if (recipe.getResult().getType() == Material.WOOD_AXE) inventory.setResult(new ItemStack(this.getMaterial(Material.WOOD_AXE))); else if (recipe.getResult().getType() == Material.WOOD_SPADE) inventory.setResult(new ItemStack(this.getMaterial(Material.WOOD_SPADE))); }
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) public void onPrepareCraft(PrepareItemCraftEvent event) { if (event.getInventory().getHolder() instanceof Player) { CraftingInventory inventory = event.getInventory(); Player player = (Player) inventory.getHolder(); if (isBottledExperience(inventory.getResult())) { int exp = new ExperienceManager(player).getCurrentExp(); inventory.setResult(exp > 0 ? createExpBottle(exp) : new ItemStack(Material.AIR, 1)); } } }
@EventHandler (priority = EventPriority.HIGHEST) public void onPrepareItemCraft (PrepareItemCraftEvent e) { if (e.getViewers().size() < 1) return; World world = e.getViewers().get(0).getWorld(); if(!isEnabled(world)) return; CraftingInventory inv = e.getInventory(); ItemStack result = inv.getResult(); if(result != null && denied.contains(result.getType())) inv.setResult(null); }
@EventHandler public void onCraftItem(CraftItemEvent event) { if (event.isCancelled()) return; final Player p = (Player) event.getWhoClicked(); CraftingInventory inventory = event.getInventory(); for(ItemStack is : inventory.getContents()) { if(ArmorStandTool.isTool(is)) { event.setCancelled(true); //noinspection deprecation p.updateInventory(); return; } } }
/** * - Workaround to fix the crafting grid being not updated when the item is * taken from the grid. * <p> * - Used to prevent our blocks to be renamed using an anvil. * <p> * - Used to allow our tools to be enchanted (ensure the name is kept). */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onInventoryClick(final InventoryClickEvent ev) { if (ev.getInventory() instanceof CraftingInventory && ev.getSlot() == 0) { InventoryUtils.updateInventoryLater(ev.getInventory()); } // Disable Anvil overrides if not using item names. else if (ev.getInventory() instanceof AnvilInventory && BBConfig.USE_ITEM_NAMES_FALLBACK.get()) { ItemStack item = ev.getInventory().getItem(0); ItemStack result = ev.getInventory().getItem(2); if (item == null) return; BelovedBlock block = BelovedBlocks.getBelovedBlocksManager().getFromItem(item); // Items cannot be renamed if(block != null) { ev.getInventory().setItem(2, new ItemStack(Material.AIR, 0)); return; } BelovedTool tool = BelovedBlocks.getToolsManager().getFromItem(item); // Players can add enchantments to the tools. if(tool != null) { ItemUtils.setDisplayName(result, tool.getDisplayName()); ev.getInventory().setItem(2, result); } } }
@EventHandler public void onPrepare(PrepareItemCraftEvent e) { CraftingInventory inv = e.getInventory(); if(inv.getRecipe().getResult().hasItemMeta()) { if(inv.getRecipe().getResult().getItemMeta().hasDisplayName()) { if(inv.getRecipe().getResult().getItemMeta().getDisplayName().equalsIgnoreCase("crate")) { if(inv.getItem(5) != null) { if(inv.getItem(5).hasItemMeta()) { if(inv.getItem(5).getItemMeta().hasDisplayName()) { if(pl.isCrate(inv.getItem(5).getItemMeta().getDisplayName())) { Crate crate = pl.getCrate(inv.getItem(5).getItemMeta().getDisplayName()); if(crate.needsKey()) { if(inv.getItem(5).getType() == (crate.getType() == CrateType.NORMAL ? Material.GOLD_INGOT : Material.DIAMOND)) { inv.setResult(crate.getKeyItem()); } else { inv.setResult(null); } } else { inv.setResult(null); } } else { inv.setResult(null); } } else { inv.setResult(null); } } else { inv.setResult(null); } } } } } }
@Override public boolean validateCrafting(CraftingInventory inventory) { for (ItemStack stack : inventory.getMatrix()) { if (stack != null && stack.getType() == Material.ENCHANTED_BOOK) { EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta(); if (meta.getStoredEnchantLevel(Enchantment.SILK_TOUCH) < 1) { return false; } } } return true; }
@EventHandler(priority = EventPriority.NORMAL) public void onPrepareItemCraft(PrepareItemCraftEvent event) { boolean isUpgradeRecipe = RecipeUtil.areEqual(event.getRecipe(), ResurrectionScroll.getResurrectionScrollUpgradeRecipe()); if (!isUpgradeRecipe) { return; } CraftingInventory inventory = event.getInventory(); boolean resurrectionScrollFound = false; int tier = 0; for (ItemStack itemStack : inventory.getMatrix()) { if (itemStack == null) { continue; } if (ItemUtils.isResurrectionScroll(itemStack)) { resurrectionScrollFound = true; tier = Tier.getTier(itemStack).toNumerical(); } } if (!resurrectionScrollFound || tier >= 3) { inventory.setResult(null); return; } inventory.setResult(ResurrectionScroll.getResurrectionScroll(1, tier + 1)); }
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent event) { final Inventory top = event.getView().getTopInventory(); if(top instanceof CraftingInventory && event.getWhoClicked() instanceof Player) { final Player player = (Player) event.getWhoClicked(); Bukkit.getScheduler().scheduleSyncDelayedTask(ItemExchangePlugin.instance, new Runnable() { @SuppressWarnings("deprecation") public void run() { CraftingInventory inv = (CraftingInventory) top; List<ExchangeRule> exchangeRules = new ArrayList<ExchangeRule>(); for(ItemStack item : inv.getMatrix()) { if(item != null && item.getType() != Material.AIR) { try { exchangeRules.add(ExchangeRule.parseRuleBlock(item)); } catch(ExchangeRuleParseException e) { try { exchangeRules.addAll(Arrays.asList(ExchangeRule.parseBulkRuleBlock(item))); } catch(ExchangeRuleParseException e2) { return; } } } } if(exchangeRules.size() > 0) { inv.setResult(ExchangeRule.toBulkItemStack(exchangeRules)); player.updateInventory(); } } }); } }
@EventHandler public void onCraftItem(CraftItemEvent event){ Player player = (Player)event.getInventory().getHolder(); EpicPlayer epicPlayer = EpicSystem.getEpicPlayer(player.getUniqueId()); List<EpicQuestTask> taskList = epicPlayer.getTasksByType(TaskTypes.REPAIR_ITEM); for(EpicQuestTask task : taskList){ if(event.getInventory().getType() == InventoryType.CRAFTING){ CraftingInventory craftInventory = (CraftingInventory)event.getInventory(); ItemStack[] content = craftInventory.getContents(); String itemNeeded = task.getTaskID(); Material craftedItem = content[0].getType(); if(craftedItem != Material.matchMaterial(itemNeeded)) return; int itemsFound = 0; for(int i = 1; i < 5; i++){ if(content[i].getType() == craftedItem) itemsFound++; } if(itemsFound == 2) task.ProgressTask(1, epicPlayer, true); return; } } }
/** * {@inheritDoc} */ @Override public ItemStack craftItem(CraftingInventory craftingInventory) { return result == null ? null : result.clone(); }
@Override public CraftItemStack craftItem(CraftingInventory craftingInventory) { InventoryCrafting nmsCraftingInventory = getNmsCraftingInventory(craftingInventory); return CraftItemStack.asCraftMirror(nmsRecipe.craftItem(nmsCraftingInventory)); }
public static InventoryCrafting getNmsCraftingInventory(CraftingInventory bukkitInventory) { return (InventoryCrafting) ReflectionUtil.getDeclaredFieldValue(bukkitInventory, "inventory"); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.ENCHANTMENT_TABLE) inventory.setResult(new ItemStack(Material.AIR)); }
private void onCraftItem(Recipe recipe, CraftingInventory inventory, HumanEntity human) { if (recipe.getResult().getType() == Material.WORKBENCH && this.crafters.contains(human.getUniqueId())) inventory.setResult(new ItemStack(Material.AIR, 1)); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.SPECKLED_MELON && inventory.contains(Material.GOLD_NUGGET)) inventory.setResult(new ItemStack(Material.AIR)); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.GOLDEN_APPLE && recipe.getResult().getDurability() == 1) inventory.setResult(new ItemStack(Material.AIR)); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.FLINT_AND_STEEL) inventory.setResult(new ItemStack(Material.AIR)); }
private static void onCraftItem(Recipe recipe, CraftingInventory inventory) { if (recipe.getResult().getType() == Material.BOW) inventory.setResult(new ItemStack(Material.AIR)); }
@EventHandler public void onCraftItem(CraftItemEvent event) { Entity player = event.getWhoClicked(); if(!(player instanceof Player)) { return; } Player p = (Player) player; String playerName = p.getName(); Location location = p.getLocation(); CraftingInventory inventory = event.getInventory(); ItemStack stack = inventory.getRecipe().getResult(); ItemStack[] ingredients = inventory.getMatrix(); // StringBuilder sb = new StringBuilder(); // for(ItemStack i : ingredients) // { // if(i == null) // sb.append("null"); // else // sb.append(i.getType() + ":" + i.getData().getData()); // sb.append("-"); // } // System.out.println(sb); int craftAmount = Utils.getCraftAmount(event); int nbCrafts = craftAmount/stack.getAmount(); ArrayList<JSONableItem> items = new ArrayList<>(); for(ItemStack is : ingredients) { if(is != null && is.getType() != Material.AIR) items.add(new JSONableItem(is.getType().toString(), is.getData().getData())); } CraftObsel craftObsel = new CraftObsel( new Date().getTime(), location.getBlockX(), location.getBlockY(), location.getBlockZ(), location.getWorld().getName(), playerName, stack.getType().toString(), stack.getData().getData(), stack.getAmount(), items, nbCrafts); // PickupItemObsel obsel = new PickupItemObsel( // new Date().getTime(), // stack.getType().toString(), // craftAmount, // stack.getData().getData(), // location.getBlockX(), // location.getBlockY(), // location.getBlockZ(), // location.getWorld().getName(), // playerName); JSONObselManager.getInstance().addObsel(craftObsel); }
@Override public CraftingInventory getInventory() { return (CraftingInventory) super.getInventory(); }
public PrepareItemCraftEvent(CraftingInventory what, InventoryView view, boolean isRepair) { super(view); this.matrix = what; this.repair = isRepair; }
/** * @return The crafting inventory on which the recipe was formed. */ @Override public CraftingInventory getInventory() { return matrix; }
@Override public boolean matches(CraftingInventory inventory) { return false; }