@EventHandler public void onPrepareAnvil(PrepareAnvilEvent event) { ItemStack itemStack = event.getInventory().getItem(0); if(itemStack != null) { EnhancedItem item = EnhancedItemUtil.getItem(itemStack); if(item != null) { if(!Boolean.parseBoolean(item.getProperty("canModify"))) { event.setResult(new ItemStack(Material.AIR)); ((Player)event.getView().getPlayer()).updateInventory(); } } } }
@Override public boolean init(Expression<?>[] e, int arg1, Kleenean arg2, ParseResult arg3) { if (!ScriptLoader.isCurrentEvent(PrepareAnvilEvent.class)) { return false; } else { return true; } }
@Override @Nullable protected Number[] get(Event e) { try { AnvilInventory a = ((PrepareAnvilEvent) e).getInventory(); return new Number[] { a.getRepairCost() }; } catch (NullPointerException ex) { return new Number[] { 0 }; } }
@Override public boolean init(Expression<?>[] e, int arg1, Kleenean arg2, ParseResult arg3) { if (!ScriptLoader.isCurrentEvent(PrepareAnvilEvent.class)) { Skript.error("You can not use anvil inventory expression in any event but anvil prepare event!"); return false; } return true; }
@EventHandler public void onCombine(final PrepareAnvilEvent event) { final ItemStack first = event.getInventory().getItem(0); final ItemStack second = event.getInventory().getItem(1); if (isSingle(first) && isSingle(second)) { final ItemStack result = select(first, second, true).clone(); final EnchantmentMerger merger = getMerger(event.getInventory()); if (merger.getCustomCost() != 0 || merger.getVanillaCost() != 0) { event.setResult(merger.apply(result)); event.getInventory().setRepairCost(merger.getCost()); } } if (isPresent(event.getResult())) { String text = event.getInventory().getRenameText(); final ItemStack primary = select(first, second, true); if (primary.hasItemMeta()) { final ItemMeta primaryMeta = primary.getItemMeta(); if (primaryMeta.hasDisplayName()) { final String withoutColorChar = primaryMeta.getDisplayName().replace("" + ChatColor.COLOR_CHAR, ""); if (withoutColorChar.equals(event.getInventory().getRenameText())) { text = primaryMeta.getDisplayName(); } } } final ItemMeta meta = event.getResult().getItemMeta(); meta.setDisplayName(colored ? TextFormatter.colorString(text) : text); event.getResult().setItemMeta(meta); } }
@Override @Nullable protected AnvilInventory[] get(Event e) { return new AnvilInventory[]{((PrepareAnvilEvent)e).getInventory()}; }
public static PrepareAnvilEvent callPrepareAnvilEvent(InventoryView view, ItemStack item) { PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item).clone()); event.getView().getPlayer().getServer().getPluginManager().callEvent(event); event.getInventory().setItem(2, event.getResult()); return event; }