Search in sources :

Example 1 with ShapedRecipe

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

the class Translator_v0_11 method processCrafting.

public void processCrafting(WindowSetSlotPacket packet) {
    if (!(this.getSession().getPlayer() instanceof Player)) {
        return;
    }
    int realSlot = 0;
    if (packet.slot < 27) {
        realSlot = packet.slot + 9;
    } else if (packet.slot >= 27) {
        realSlot = packet.slot - 27;
    }
    ItemStack item = this.getSession().getPlayer().getInventory().getItem(realSlot);
    if (item == null) {
        item = new ItemStack(Material.AIR);
    } else if (item.getAmount() <= 0) {
        this.getSession().getPlayer().getInventory().setItem(realSlot, null);
        return;
    }
    System.out.println("FROM " + item.toString() + "to (ITEM=" + packet.item.id + ",CNT=" + packet.item.count + ")");
    if (packet.item.count < 0) {
        this.getSession().sendInventory();
        return;
    }
    if (item.getTypeId() == 0 && packet.item.id == 0) {
        this.getSession().sendInventory();
        //No changes
        return;
    }
    if (item.getTypeId() == packet.item.id && item.getAmount() == packet.item.count && item.getDurability() == packet.item.meta) {
        this.getSession().sendInventory();
        //No changes
        return;
    }
    if ((item.getTypeId() != 0 && packet.item.id == 0) || (item.getTypeId() != 0 && (item.getTypeId() != packet.item.id)) || (item.getAmount() > (packet.item.count & 0xFF))) {
        this.getSession().sendInventory();
        //Decreasing item, ignore
        return;
    }
    int amount = packet.item.count - (item.getTypeId() == 0 ? 0 : item.getAmount());
    ItemStack result = new ItemStack(packet.item.id, amount, packet.item.meta);
    List<Recipe> recipes = this.getSession().getServer().getCraftingManager().getRecipesFor(result);
    if (recipes.size() <= 0) {
        return;
    }
    //System.out.println("CRAFTING FOR: " + result.toString() + ", recipes count: " + recipes.size());
    if (packet.windowID == PEWindowConstantID.PLAYER_INVENTORY && recipes.size() > 4) {
        //Can not craft more than 4 recipes in a player inventory
        this.getSession().sendInventory();
        return;
    }
    ItemList items = new ItemList(this.getSession().getPlayer().getInventory());
    //List all ways to craft
    for (Recipe recipe : recipes) {
        if (recipe instanceof ShapedRecipe) {
            ShapedRecipe shaped = (ShapedRecipe) recipe;
            boolean faild = false;
            for (String itemChar : shaped.getShape()) {
                ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
                if (ingredient == null) {
                    continue;
                }
                if (!items.tryToRemove(ingredient)) {
                    faild = true;
                    break;
                }
            }
            if (!faild) {
                //Apply changes
                for (String itemChar : shaped.getShape()) {
                    ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
                    if (ingredient == null) {
                        continue;
                    }
                    this.getSession().getPlayer().getInventory().remove(ingredient);
                }
            //System.out.println("CRAFT SUCCESS! ");
            } else {
                continue;
            }
            this.getSession().getPlayer().getInventory().addItem(result);
            this.getSession().sendInventory();
            return;
        }
    }
    //System.out.println("FAILD TO CRAFT! ");
    this.getSession().sendInventory();
}
Also used : Player(org.bukkit.entity.Player) Recipe(org.bukkit.inventory.Recipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ItemList(org.dragonet.inventory.ItemList) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 2 with ShapedRecipe

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

the class Commandcondense method getStackOnRecipeMatch.

private Collection<ItemStack> getStackOnRecipeMatch(final Recipe recipe, final ItemStack stack) {
    final Collection<ItemStack> inputList;
    if (recipe instanceof ShapedRecipe) {
        ShapedRecipe sRecipe = (ShapedRecipe) recipe;
        inputList = sRecipe.getIngredientMap().values();
    } else if (recipe instanceof ShapelessRecipe) {
        ShapelessRecipe slRecipe = (ShapelessRecipe) recipe;
        inputList = slRecipe.getIngredientList();
    } else {
        return null;
    }
    boolean match = true;
    Iterator<ItemStack> iter = inputList.iterator();
    while (iter.hasNext()) {
        ItemStack inputSlot = iter.next();
        if (inputSlot == null) {
            iter.remove();
            continue;
        }
        if (inputSlot.getDurability() == Short.MAX_VALUE) {
            inputSlot.setDurability((short) 0);
        }
        if (!inputSlot.isSimilar(stack)) {
            match = false;
        }
    }
    if (match) {
        return inputList;
    }
    return null;
}
Also used : ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with ShapedRecipe

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

the class WindowItemsPacketTranslator method removeItems.

/**
     * Remove enough items from the given item list to form the given recipe.
     * @param items The items to remove the ingredients from.
     * @param recipe A recipe known to match the items.
     */
public void removeItems(ItemStack[] items, Recipe recipe) {
    ItemList lst = new ItemList(items);
    if (recipe instanceof ShapedRecipe) {
        ShapedRecipe shaped = (ShapedRecipe) recipe;
        for (String itemChar : shaped.getShape()) {
            ItemStack ingredient = shaped.getIngredientMap().get(new Character(itemChar.charAt(0)));
            if (ingredient == null) {
                continue;
            }
            lst.tryToRemove(ingredient);
        }
    }
    for (int i = 0; i < items.length; i++) {
        items[i] = lst.getItems().get(i);
    }
}
Also used : ItemList(org.dragonet.inventory.ItemList) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with ShapedRecipe

use of org.bukkit.inventory.ShapedRecipe 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)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)4 ShapedRecipe (org.bukkit.inventory.ShapedRecipe)4 Recipe (org.bukkit.inventory.Recipe)2 ShapelessRecipe (org.bukkit.inventory.ShapelessRecipe)2 ItemList (org.dragonet.inventory.ItemList)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 Enchantment (org.bukkit.enchantments.Enchantment)1 Player (org.bukkit.entity.Player)1 FurnaceRecipe (org.bukkit.inventory.FurnaceRecipe)1 PEBinaryWriter (org.dragonet.utilities.io.PEBinaryWriter)1