Search in sources :

Example 1 with WChestData

use of com.bgsoftware.wildchests.objects.data.WChestData in project WildChests by BG-Software-LLC.

the class ChestUtils method tryCraftChest.

public static void tryCraftChest(Chest chest) {
    Inventory[] pages = chest.getPages();
    Iterator<Map.Entry<Recipe, List<RecipeUtils.RecipeIngredient>>> recipes = ((WChestData) chest.getData()).getRecipeIngredients();
    List<ItemStack> toAdd = new ArrayList<>();
    while (recipes.hasNext()) {
        Map.Entry<Recipe, List<RecipeUtils.RecipeIngredient>> recipe = recipes.next();
        if (recipe.getValue().isEmpty())
            continue;
        int amountOfRecipes = Integer.MAX_VALUE;
        int pageSize = pages[0].getSize();
        Map<RecipeUtils.RecipeIngredient, List<Integer>> slots = new HashMap<>();
        for (RecipeUtils.RecipeIngredient ingredient : recipe.getValue()) {
            for (int i = 0; i < pages.length; i++) {
                // Count items returns a list of slots and the total amount of the items in the slots.
                Pair<List<Integer>, Integer> countResult = RecipeUtils.countItems(ingredient, pages[i], i * pageSize);
                amountOfRecipes = Math.min(amountOfRecipes, countResult.value / ingredient.getAmount());
                slots.put(ingredient, countResult.key);
            }
        }
        if (amountOfRecipes > 0) {
            // We manually removing the items
            for (Map.Entry<RecipeUtils.RecipeIngredient, List<Integer>> entry : slots.entrySet()) {
                int amountToRemove = entry.getKey().getAmount() * amountOfRecipes;
                for (int slot : entry.getValue()) {
                    int page = slot / pageSize;
                    slot = slot % pageSize;
                    ItemStack itemStack = pages[page].getItem(slot);
                    if (itemStack.getAmount() > amountToRemove) {
                        itemStack.setAmount(itemStack.getAmount() - amountToRemove);
                        break;
                    } else {
                        amountToRemove -= itemStack.getAmount();
                        pages[page].setItem(slot, new ItemStack(Material.AIR));
                    }
                }
            }
            ItemStack result = recipe.getKey().getResult().clone();
            result.setAmount(result.getAmount() * amountOfRecipes);
            toAdd.add(result);
            NotifierTask.addCrafting(chest.getPlacer(), result, result.getAmount());
        }
    }
    List<ItemStack> toDrop = new ArrayList<>(chest.addItems(toAdd.toArray(new ItemStack[] {})).values());
    if (!toDrop.isEmpty()) {
        for (ItemStack itemStack : toDrop) ItemUtils.dropItem(chest.getLocation(), itemStack);
    }
}
Also used : Recipe(org.bukkit.inventory.Recipe) WChestData(com.bgsoftware.wildchests.objects.data.WChestData) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory)

Example 2 with WChestData

use of com.bgsoftware.wildchests.objects.data.WChestData in project WildChests by BG-Software-LLC.

the class CommandInfo method perform.

@Override
public void perform(WildChestsPlugin plugin, CommandSender sender, String[] args) {
    ChestData chestData = plugin.getChestsManager().getChestData(args[1]);
    if (chestData == null) {
        Locale.INVALID_CHEST.send(sender, args[1]);
        return;
    }
    // Header
    Locale.CHEST_INFO_HEADER.send(sender);
    // Sections which applied to all chests
    Locale.CHEST_INFO_NAME.send(sender, chestData.getName(), ((WChestData) chestData).getItemRaw().getItemMeta().getDisplayName());
    Locale.CHEST_INFO_TYPE.send(sender, chestData.getChestType());
    Locale.CHEST_INFO_SIZE.send(sender, chestData.getDefaultSize());
    Locale.CHEST_INFO_DEFAULT_TITLE.send(sender, chestData.getDefaultTitle());
    Locale.CHEST_INFO_SELL_MODE.send(sender, chestData.isSellMode());
    // Optional sections
    if (chestData.isAutoCrafter())
        Locale.CHEST_INFO_RECIPES.send(sender, getRecipesAsString(chestData.getRecipes()));
    // Footer
    Locale.CHEST_INFO_FOOTER.send(sender);
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) WChestData(com.bgsoftware.wildchests.objects.data.WChestData)

Aggregations

WChestData (com.bgsoftware.wildchests.objects.data.WChestData)2 ChestData (com.bgsoftware.wildchests.api.objects.data.ChestData)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 Recipe (org.bukkit.inventory.Recipe)1