org.bukkit.inventory.CraftingInventory Java Examples

The following examples show how to use org.bukkit.inventory.CraftingInventory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private PrepareItemCraftEvent mockPreCraftingEvent(ItemStack item) {
    Player player = server.addPlayer();

    CraftingInventory inv = Mockito.mock(CraftingInventory.class);
    MutableObject result = new MutableObject(new ItemStack(Material.EMERALD));

    Mockito.doAnswer(invocation -> {
        ItemStack argument = invocation.getArgument(0);
        result.setValue(argument);
        return null;
    }).when(inv).setResult(Mockito.any());

    Mockito.when(inv.getResult()).thenAnswer(invocation -> result.getValue());
    Mockito.when(inv.getContents()).thenReturn(new ItemStack[] { null, null, item, null, null, null, null, null, null });

    InventoryView view = player.openInventory(inv);
    PrepareItemCraftEvent event = new PrepareItemCraftEvent(inv, view, false);

    listener.onPrepareCraft(event);
    return event;
}
 
Example #2
Source File: ArmorListener.java    From ViaVersion with MIT License 6 votes vote down vote up
@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);
        }
    }
}
 
Example #3
Source File: ItemStacker.java    From TabooLib with MIT License 6 votes vote down vote up
/**
 * 添加并拆分,但不合并
 * 返回值为是否添加完成
 *
 * desc = 快捷栏逆向添加,用于工作台拟真,会忽略 start 参数
 */
public static boolean addItemAndSplit(ItemStack item, Inventory inventory, int start, boolean desc) {
    int size = inventory instanceof PlayerInventory || inventory instanceof CraftingInventory ? 36 : inventory.getSize();
    if (desc) {
        // 8 ~ 0
        for (int i = 8; i >= 0; i--) {
            if (check(item, inventory, i)) {
                return true;
            }
        }
    }
    // 9 ~ 36
    for (int i = desc ? start + 9 : start; i < size; i++) {
        if (check(item, inventory, i)) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: ItemStacker.java    From TabooLib with MIT License 6 votes vote down vote up
/**
 * 合并物品,不新增
 */
public static AddResult addItemAndMerge(ItemStack item, Inventory inventory, Integer[] ignore) {
    boolean changed = false;
    int count = item.getAmount();
    int size = inventory instanceof PlayerInventory || inventory instanceof CraftingInventory ? 36 : inventory.getSize();
    for (int i = 0; i < size; i++) {
        if (ArrayUtil.contains(ignore, i)) {
            continue;
        }
        ItemStack inventoryItem = inventory.getItem(i);
        if (!item.isSimilar(inventoryItem)) {
            continue;
        }
        while (count > 0 && inventoryItem.getAmount() < item.getType().getMaxStackSize()) {
            changed = true;
            inventoryItem.setAmount(inventoryItem.getAmount() + 1);
            count--;
        }
        if (count == 0) {
            return new AddResult(count, changed);
        }
    }
    return new AddResult(count, changed);
}
 
Example #5
Source File: CraftingMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void removeDisabledRecipe(PrepareItemCraftEvent event) {
  CraftingInventory crafting = event.getInventory();
  ItemStack result = crafting.getResult();
  if (result == null) {
    return;
  }

  for (SingleMaterialMatcher disabled : disabledRecipes) {
    if (disabled.matches(result)) {
      crafting.setResult(null);
      break;
    }
  }
}
 
Example #6
Source File: TestVanillaMachinesListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private CraftItemEvent mockCraftingEvent(ItemStack item) {
    Recipe recipe = new ShapedRecipe(new NamespacedKey(plugin, "test_recipe"), new ItemStack(Material.EMERALD));
    Player player = server.addPlayer();

    CraftingInventory inv = Mockito.mock(CraftingInventory.class);
    Mockito.when(inv.getContents()).thenReturn(new ItemStack[] { item, null, null, null, null, null, null, null, null });

    InventoryView view = player.openInventory(inv);
    CraftItemEvent event = new CraftItemEvent(recipe, view, SlotType.RESULT, 9, ClickType.LEFT, InventoryAction.PICKUP_ALL);

    listener.onCraft(event);
    return event;
}
 
Example #7
Source File: MainListener.java    From ArmorStandTools with MIT License 5 votes vote down vote up
@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;
        }
    }
}
 
Example #8
Source File: InventorySlot.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(@Nullable Event e, boolean debug) {
	InventoryHolder holder = invi.getHolder();
	
	if (holder instanceof BlockState)
		holder = new BlockInventoryHolder((BlockState) holder);
	
	if (invi.getHolder() != null) {
		if (invi instanceof CraftingInventory) // 4x4 crafting grid is contained in player too!
			return "crafting slot " + index + " of " + Classes.toString(holder);
		
		return "inventory slot " + index + " of " + Classes.toString(holder);
	}
	return "inventory slot " + index + " of " + Classes.toString(invi);
}
 
Example #9
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler
public void onLick(final InventoryClickEvent event){
	//LOL TO THE METHOD NAME
	if(event.getClickedInventory() != null && event.getClickedInventory().getType().equals(InventoryType.WORKBENCH)){
		//System.out.println(event.getAction());
		switch(event.getAction()){
		case PLACE_ONE:
		case PLACE_SOME:
		case PLACE_ALL:
			ItemStack to = event.getInventory().getItem(event.getSlot()) != null ? event.getInventory().getItem(event.getSlot()).clone() : null;
			if(to != null){
				
				final CraftingInventory i = (CraftingInventory) event.getInventory();
				ProRecipes.getPlugin().getServer().getScheduler().runTaskLater(ProRecipes.getPlugin(), new Runnable(){

					@Override
					public void run() {
						//System.out.println("Calling prepare!");
						callPrepare(i, event.getView());
						
					}
					
				}, 0);
				
			}
		}
	}
	
}
 
Example #10
Source File: CraftItemListener.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPrepare(PrepareItemCraftEvent event) {
	if (MineTinker.getPlugin().getConfig().getBoolean("ModifiersCanBeUsedForCrafting")) return;
	CraftingInventory inv = event.getInventory();
	for (ItemStack is : inv.getMatrix()) {
		if (is == null) continue;
		if (modManager.isModifierItem(is)) {
			inv.setResult(null);
			break;
		}
	}
}
 
Example #11
Source File: ItemCraftListener.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler
public void onItemCraft(PrepareItemCraftEvent event) {
    try {
        final CraftingInventory inventory = event.getInventory();
        if (inventory.getResult() == null) return;

        for (ItemStack itemStack : inventory.getContents()) {
            if (Utils.getCrystals(itemStack) == 0) continue;
            inventory.setResult(null);
            return;
        }
    } catch (Exception e) {
        IridiumSkyblock.getInstance().sendErrorMessage(e);
    }
}
 
Example #12
Source File: PrepareItemCraftEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The crafting inventory on which the recipe was formed.
 */
@Override
public CraftingInventory getInventory() {
    return matrix;
}
 
Example #13
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
public void callPrepare(CraftingInventory inv, InventoryView v){
	PrepareItemCraftEvent event = new PrepareItemCraftEvent(inv, v, false);
	ProRecipes.getPlugin().getServer().getPluginManager().callEvent(event);
}
 
Example #14
Source File: EventListener.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 
 * 
 * Will only be called for shaped and shapeless
 * @param event
 */


@EventHandler(priority = EventPriority.MONITOR)
public void workbenchCraft(WorkbenchCraftEvent event){
	CraftingInventory inventory = (CraftingInventory)event.getInventory();
	
	if(event.isCancelled()){
		inventory.setResult((new ItemStack(Material.AIR)));
		return;
	}
	
	inventory.setResult(event.getResult());
	inventory.setItem(0, event.getResult());
	
	ProRecipes.getPlugin().incrementRecipesCrafted(event.getRecipe().getType());
	
}
 
Example #15
Source File: PrepareItemCraftEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public PrepareItemCraftEvent(CraftingInventory what, InventoryView view, boolean isRepair) {
    super(view);
    this.matrix = what;
    this.repair = isRepair;
}
 
Example #16
Source File: CraftItemEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public CraftingInventory getInventory() {
    return (CraftingInventory) super.getInventory();
}
 
Example #17
Source File: ConvertToolListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onCraft(PrepareItemCraftEvent event) {
	CraftingInventory inv = event.getInventory();

	boolean canConvert = false;
	World world = null;

	HumanEntity humanEntity = null;

	for (HumanEntity human : inv.getViewers()) {
		if (human.hasPermission("minetinker.tool.create")) {
			canConvert = true;
			world = human.getWorld();
			humanEntity = human;
			break;
		}
	}

	if (!canConvert) {
		return;
	}

	if (Lists.WORLDS.contains(world.getName())) {
		return;
	}

	int recipeItems = 0;
	ItemStack lastItem = null;

	for (ItemStack item : inv.getMatrix()) {
		if (item != null && item.getType() != Material.AIR) {
			recipeItems += 1;
			lastItem = item;
		}
	}

	if (recipeItems == 1) {
		if (modManager.isArmorViable(lastItem) || modManager.isToolViable(lastItem) || modManager.isWandViable(lastItem)) {
			inv.setResult(new ItemStack(Material.AIR, 1));
			return;
		}

		if (ToolType.isMaterialCompatible(lastItem.getType())) {
			inv.setResult(lastItem);
			modManager.convertItemStack(event.getInventory().getResult(), humanEntity);
			inv.getResult().setAmount(1);
		}
	}
}
 
Example #18
Source File: VaultOperations.java    From PlayerVaults with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Open a player's own vault.
 *
 * @param player The player to open to.
 * @param arg    The vault number to open.
 * @return Whether or not the player was allowed to open it.
 */
public static boolean openOwnVault(Player player, String arg) {
    if (isLocked()) {
        return false;
    }
    if (player.isSleeping() || player.isDead() || !player.isOnline()) {
        return false;
    }
    int number;
    try {
        number = Integer.parseInt(arg);
        if (number < 1) {
            return false;
        }
    } catch (NumberFormatException nfe) {
        player.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER.toString());
        return false;
    }

    if (checkPerms(player, number)) {
        if (EconomyOperations.payToOpen(player, number)) {
            Inventory inv = VaultManager.getInstance().loadOwnVault(player, number, getMaxVaultSize(player));
            if (inv == null) {
                PlayerVaults.debug(String.format("Failed to open null vault %d for %s. This is weird.", number, player.getName()));
                return false;
            }

            player.openInventory(inv);

            // Check if the inventory was actually opened
            if (player.getOpenInventory().getTopInventory() instanceof CraftingInventory || player.getOpenInventory().getTopInventory() == null) {
                PlayerVaults.debug(String.format("Cancelled opening vault %s for %s from an outside source.", arg, player.getName()));
                return false; // inventory open event was cancelled.
            }

            VaultViewInfo info = new VaultViewInfo(player.getUniqueId().toString(), number);
            PlayerVaults.getInstance().getOpenInventories().put(info.toString(), inv);

            player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_VAULT.toString().replace("%v", arg));
            return true;
        } else {
            player.sendMessage(Lang.TITLE.toString() + Lang.INSUFFICIENT_FUNDS);
            return false;
        }
    } else {
        player.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
    }
    return false;
}
 
Example #19
Source File: VaultOperations.java    From PlayerVaults with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Open another player's vault.
 *
 * @param player     The player to open to.
 * @param vaultOwner The name of the vault owner.
 * @param arg        The vault number to open.
 * @return Whether or not the player was allowed to open it.
 */
public static boolean openOtherVault(Player player, String vaultOwner, String arg) {
    if (isLocked()) {
        return false;
    }

    if (player.isSleeping() || player.isDead() || !player.isOnline()) {
        return false;
    }

    long time = System.currentTimeMillis();

    int number = 0;
    try {
        number = Integer.parseInt(arg);
        if (number < 1) {
            player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
            return false;
        }
    } catch (NumberFormatException nfe) {
        player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
    }

    Inventory inv = VaultManager.getInstance().loadOtherVault(vaultOwner, number, getMaxVaultSize(vaultOwner));
    String name = vaultOwner;
    try {
        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(vaultOwner));
        name = offlinePlayer.getName();
    } catch (Exception e) {
        // not a player
    }

    if (inv == null) {
        player.sendMessage(Lang.TITLE.toString() + Lang.VAULT_DOES_NOT_EXIST.toString());
    } else {
        player.openInventory(inv);

        // Check if the inventory was actually opened
        if (player.getOpenInventory().getTopInventory() instanceof CraftingInventory || player.getOpenInventory().getTopInventory() == null) {
            PlayerVaults.debug(String.format("Cancelled opening vault %s for %s from an outside source.", arg, player.getName()));
            return false; // inventory open event was cancelled.
        }
        player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", name));
        PlayerVaults.debug("opening other vault", time);

        // Need to set ViewInfo for a third party vault for the opening player.
        VaultViewInfo info = new VaultViewInfo(vaultOwner, number);
        PlayerVaults.getInstance().getInVault().put(player.getUniqueId().toString(), info);
        PlayerVaults.getInstance().getOpenInventories().put(player.getUniqueId().toString(), inv);
        return true;
    }

    PlayerVaults.debug("opening other vault returning false", time);
    return false;
}