Search in sources :

Example 16 with Recipe

use of org.bukkit.inventory.Recipe in project Essentials by EssentialsX.

the class Commandcondense method getCondenseType.

private SimpleRecipe getCondenseType(final ItemStack stack) {
    if (condenseList.containsKey(stack)) {
        return condenseList.get(stack);
    }
    final Iterator<Recipe> intr = ess.getServer().recipeIterator();
    List<SimpleRecipe> bestRecipes = new ArrayList<>();
    while (intr.hasNext()) {
        final Recipe recipe = intr.next();
        final Collection<ItemStack> recipeItems = getStackOnRecipeMatch(recipe, stack);
        if (recipeItems != null && (recipeItems.size() == 4 || recipeItems.size() == 9) && (recipeItems.size() > recipe.getResult().getAmount())) {
            final ItemStack input = stack.clone();
            input.setAmount(recipeItems.size());
            final SimpleRecipe newRecipe = new SimpleRecipe(recipe.getResult(), input);
            bestRecipes.add(newRecipe);
        }
    }
    if (!bestRecipes.isEmpty()) {
        if (bestRecipes.size() > 1) {
            Collections.sort(bestRecipes, SimpleRecipeComparator.INSTANCE);
        }
        SimpleRecipe recipe = bestRecipes.get(0);
        condenseList.put(stack, recipe);
        return recipe;
    }
    condenseList.put(stack, null);
    return null;
}
Also used : ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) Recipe(org.bukkit.inventory.Recipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 17 with Recipe

use of org.bukkit.inventory.Recipe in project Essentials by drtshock.

the class Commandcondense method getCondenseType.

private SimpleRecipe getCondenseType(final ItemStack stack) {
    if (condenseList.containsKey(stack)) {
        return condenseList.get(stack);
    }
    final Iterator<Recipe> intr = ess.getServer().recipeIterator();
    List<SimpleRecipe> bestRecipes = new ArrayList<>();
    while (intr.hasNext()) {
        final Recipe recipe = intr.next();
        final Collection<ItemStack> recipeItems = getStackOnRecipeMatch(recipe, stack);
        if (recipeItems != null && (recipeItems.size() == 4 || recipeItems.size() == 9) && (recipeItems.size() > recipe.getResult().getAmount())) {
            final ItemStack input = stack.clone();
            input.setAmount(recipeItems.size());
            final SimpleRecipe newRecipe = new SimpleRecipe(recipe.getResult(), input);
            bestRecipes.add(newRecipe);
        }
    }
    if (!bestRecipes.isEmpty()) {
        if (bestRecipes.size() > 1) {
            Collections.sort(bestRecipes, SimpleRecipeComparator.INSTANCE);
        }
        SimpleRecipe recipe = bestRecipes.get(0);
        condenseList.put(stack, recipe);
        return recipe;
    }
    condenseList.put(stack, null);
    return null;
}
Also used : ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) Recipe(org.bukkit.inventory.Recipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 18 with Recipe

use of org.bukkit.inventory.Recipe in project Dragonet-Legacy by DragonetMC.

the class CraftingDataPacket method encode.

@Override
public void encode() {
    enchants = 0;
    try {
        setChannel(NetworkChannel.CHANNEL_WORLD_EVENTS);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        for (Object o : recipies) {
            if (Recipe.class.isAssignableFrom(o.getClass())) {
                Recipe r = (Recipe) o;
                if (ShapelessRecipe.class.isInstance(r)) {
                    writer.writeInt(ENTRY_SHAPELESS);
                    ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                    PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                    /* Write data */
                    {
                        ewriter.writeInt(((ShapelessRecipe) r).getIngredientList().size());
                        for (ItemStack stack : ((ShapelessRecipe) r).getIngredientList()) {
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(stack));
                        }
                        ewriter.writeInt(1);
                        PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(r.getResult()));
                    }
                    writer.writeInt(ebos.toByteArray().length);
                    writer.write(ebos.toByteArray());
                } else if (FurnaceRecipe.class.isInstance(r)) {
                    FurnaceRecipe f = (FurnaceRecipe) r;
                    if (f.getResult().getDurability() != 0) {
                        writer.writeInt(ENTRY_FURNACE_DATA);
                        ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                        PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                        /* Write data */
                        {
                            ewriter.writeInt(f.getInput().getTypeId() << 16 | f.getInput().getDurability());
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(f.getResult()));
                        }
                        writer.writeInt(ebos.toByteArray().length);
                        writer.write(ebos.toByteArray());
                    } else {
                        writer.writeInt(ENTRY_FURNACE);
                        ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                        PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                        /* Write data */
                        {
                            ewriter.writeInt(f.getInput().getTypeId());
                            PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(f.getResult()));
                        }
                        writer.writeInt(ebos.toByteArray().length);
                        writer.write(ebos.toByteArray());
                    }
                } else if (ShapedRecipe.class.isInstance(r)) {
                    ShapedRecipe sr = (ShapedRecipe) r;
                    writer.writeInt(ENTRY_SHAPED);
                    ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                    PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                    /* Write data */
                    {
                        ewriter.writeInt(sr.getShape()[0].length());
                        ewriter.writeInt(sr.getShape().length);
                        for (int pos = 0; pos > sr.getShape()[0].length(); pos++) {
                            for (String line : sr.getShape()) {
                                ItemStack stack = sr.getIngredientMap().get(line.charAt(pos));
                                PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(stack));
                            }
                        }
                        ewriter.writeInt(1);
                        PEInventorySlot.writeSlot(ewriter, PEInventorySlot.fromItemStack(sr.getResult()));
                        long mostUUID = sr.getResult().getTypeId() << 32 | sr.getResult().getDurability() << 16 | (sr.getResult().getAmount() & 0xFFFF);
                        UUID uid = new UUID(mostUUID, mostUUID);
                        ewriter.writeUUID(uid);
                    }
                    writer.writeInt(ebos.toByteArray().length);
                    writer.write(ebos.toByteArray());
                } else {
                    writer.writeInt(-1);
                    writer.writeInt(0);
                }
            } else if (Enchantment.class.isAssignableFrom(o.getClass())) {
                Enchantment e = (Enchantment) o;
                writer.writeInt(ENTRY_ENCHANT);
                ByteArrayOutputStream ebos = new ByteArrayOutputStream();
                PEBinaryWriter ewriter = new PEBinaryWriter(ebos);
                /* Write data */
                {
                    ewriter.writeInt(enchants);
                    enchants++;
                    ewriter.writeInt(e.getId());
                    // HACK: Always return this.
                    ewriter.writeInt(e.getMaxLevel());
                    // HACK: Always 1 for now. TO BE FIXED!
                    ewriter.writeInt(1);
                    ewriter.writeString(e.getName());
                }
                writer.writeInt(ebos.toByteArray().length);
                writer.write(ebos.toByteArray());
            }
        }
        writer.writeInt(recipies.size());
        this.setData(bos.toByteArray());
    } catch (IOException e) {
    }
}
Also used : PEBinaryWriter(org.dragonet.utilities.io.PEBinaryWriter) ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) Recipe(org.bukkit.inventory.Recipe) FurnaceRecipe(org.bukkit.inventory.FurnaceRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FurnaceRecipe(org.bukkit.inventory.FurnaceRecipe) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) UUID(java.util.UUID) Enchantment(org.bukkit.enchantments.Enchantment)

Example 19 with Recipe

use of org.bukkit.inventory.Recipe in project Dragonet-Legacy by DragonetMC.

the class CraftingEventPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(CraftingEventPacket packet) {
    // TODO: From ln.2561
    if (!getSession().getOpenedWindows().contains(packet.windowId & 0xFF)) {
        WindowClosePacket pkClose = new WindowClosePacket();
        pkClose.windowID = packet.windowId;
        getSession().send(pkClose);
        return null;
    }
    int targetID = (int) ((packet.uuid.getMostSignificantBits() >> 32) & 0xFFFFFFFF);
    short targetDura = (short) ((packet.uuid.getMostSignificantBits() >> 16) | 0xFFFF);
    int amount = (int) (packet.uuid.getMostSignificantBits() & 0xFFFF);
    // Client wants to craft this
    ItemStack target = new ItemStack(targetID, amount, targetDura);
    Recipe[] r = getSession().getServer().getCraftingManager().getRecipesFor(target).toArray(new Recipe[0]);
    // If faild, resend the inventory.
    return null;
}
Also used : WindowClosePacket(org.dragonet.net.packet.minecraft.WindowClosePacket) Recipe(org.bukkit.inventory.Recipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 20 with Recipe

use of org.bukkit.inventory.Recipe in project Glowstone by GlowstoneMC.

the class FurnaceEntity method burn.

/**
 * Advances the cooking process for the tick.
 */
// TODO: Change block on burning
public void burn() {
    GlowFurnaceInventory inv = (GlowFurnaceInventory) getInventory();
    boolean sendChange = false;
    if (burnTime > 0) {
        burnTime--;
        sendChange = true;
    }
    boolean isBurnable = isBurnable();
    if (cookTime > 0 && isBurnable) {
        cookTime++;
        sendChange = true;
    } else if (burnTime != 0) {
        cookTime = 0;
        sendChange = true;
    }
    if (cookTime == 0 && isBurnable) {
        cookTime = 1;
        sendChange = true;
    }
    if (burnTime == 0) {
        if (isBurnable) {
            FuelManager fm = ((GlowServer) ServerProvider.getServer()).getFuelManager();
            FurnaceBurnEvent burnEvent = new FurnaceBurnEvent(block, inv.getFuel(), fm.getFuelTime(inv.getFuel().getType()));
            EventFactory.getInstance().callEvent(burnEvent);
            if (!burnEvent.isCancelled() && burnEvent.isBurning()) {
                burnTime = (short) burnEvent.getBurnTime();
                burnTimeFuel = burnTime;
                if (inv.getFuel().getAmount() == 1) {
                    if (inv.getFuel().getType().equals(Material.LAVA_BUCKET)) {
                        inv.setFuel(new ItemStack(Material.BUCKET));
                    } else {
                        inv.setFuel(null);
                    }
                } else {
                    inv.getFuel().setAmount(inv.getFuel().getAmount() - 1);
                }
                sendChange = true;
            } else if (cookTime != 0) {
                if (cookTime % 2 == 0) {
                    cookTime = (short) (cookTime - 2);
                } else {
                    cookTime--;
                }
                sendChange = true;
            }
        } else if (cookTime != 0) {
            if (cookTime % 2 == 0) {
                cookTime = (short) (cookTime - 2);
            } else {
                cookTime--;
            }
            sendChange = true;
        }
    }
    if (cookTime == 200) {
        RecipeManager rm = ((GlowServer) ServerProvider.getServer()).getRecipeManager();
        Recipe recipe = rm.getRecipe(inv);
        if (recipe != null) {
            FurnaceSmeltEvent smeltEvent = new FurnaceSmeltEvent(block, inv.getSmelting(), recipe.getResult());
            EventFactory.getInstance().callEvent(smeltEvent);
            if (!smeltEvent.isCancelled()) {
                if (inv.getSmelting().getType().equals(Material.SPONGE) && inv.getSmelting().getData().getData() == 1 && inv.getFuel() != null && inv.getFuel().getType().equals(Material.BUCKET) && inv.getFuel().getAmount() == 1) {
                    inv.setFuel(new ItemStack(Material.WATER_BUCKET));
                }
                if (inv.getResult() == null || inv.getResult().getType().equals(Material.AIR)) {
                    inv.setResult(smeltEvent.getResult());
                } else if (inv.getResult().getType().equals(smeltEvent.getResult().getType())) {
                    inv.getResult().setAmount(inv.getResult().getAmount() + smeltEvent.getResult().getAmount());
                }
                if (inv.getSmelting().getAmount() == 1) {
                    inv.setSmelting(null);
                } else {
                    inv.getSmelting().setAmount(inv.getSmelting().getAmount() - 1);
                }
            }
            cookTime = 0;
            sendChange = true;
        }
    }
    inv.getViewersSet().forEach(human -> {
        human.setWindowProperty(Property.BURN_TIME, burnTime);
        human.setWindowProperty(Property.TICKS_FOR_CURRENT_FUEL, burnTimeFuel);
        human.setWindowProperty(Property.COOK_TIME, cookTime);
        human.setWindowProperty(Property.TICKS_FOR_CURRENT_SMELTING, 200);
    });
    if (!isBurnable && burnTime == 0 && cookTime == 0) {
        getState().getBlock().getWorld().cancelPulse(getState().getBlock());
        sendChange = true;
    }
    if (sendChange) {
        updateInRange();
    }
}
Also used : FurnaceBurnEvent(org.bukkit.event.inventory.FurnaceBurnEvent) GlowFurnaceInventory(net.glowstone.inventory.GlowFurnaceInventory) Recipe(org.bukkit.inventory.Recipe) RecipeManager(net.glowstone.datapack.RecipeManager) GlowServer(net.glowstone.GlowServer) ItemStack(org.bukkit.inventory.ItemStack) FurnaceSmeltEvent(org.bukkit.event.inventory.FurnaceSmeltEvent) FuelManager(net.glowstone.datapack.FuelManager)

Aggregations

Recipe (org.bukkit.inventory.Recipe)20 ItemStack (org.bukkit.inventory.ItemStack)15 ShapedRecipe (org.bukkit.inventory.ShapedRecipe)7 ShapelessRecipe (org.bukkit.inventory.ShapelessRecipe)5 EventHandler (org.bukkit.event.EventHandler)4 GlowServer (net.glowstone.GlowServer)3 RecipeManager (net.glowstone.datapack.RecipeManager)3 HumanEntity (org.bukkit.entity.HumanEntity)3 Player (org.bukkit.entity.Player)3 CraftingInventory (org.bukkit.inventory.CraftingInventory)3 Trade (com.earth2me.essentials.Trade)2 MCRecipe (com.laytonsmith.abstraction.MCRecipe)2 ArrayList (java.util.ArrayList)2 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)2 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)2 FuelManager (net.glowstone.datapack.FuelManager)2 GlowFurnaceInventory (net.glowstone.inventory.GlowFurnaceInventory)2 CraftItemEvent (org.bukkit.event.inventory.CraftItemEvent)2 MagicRecipe (com.elmakers.mine.bukkit.magic.MagicRecipe)1 ItemActionInfo (com.gamingmesh.jobs.actions.ItemActionInfo)1