Search in sources :

Example 1 with FurnaceRecipe

use of org.bukkit.inventory.FurnaceRecipe in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptHelper method scriptReload.

@EventHandler
public void scriptReload(ScriptReloadEvent event) {
    for (Map.Entry<ItemScriptContainer, List<String>> entry : recipes_to_register.entrySet()) {
        ItemScriptContainer container = entry.getKey();
        List<String> recipeList = entry.getValue();
        // Process all tags in list
        for (int n = 0; n < recipeList.size(); n++) {
            recipeList.set(n, TagManager.tag(recipeList.get(n), new BukkitTagContext(container.player, container.npc, false, null, dB.shouldDebug(container), new dScript(container))));
        }
        // Store every ingredient in a List
        List<dItem> ingredients = new ArrayList<dItem>();
        boolean shouldRegister = true;
        recipeLoop: for (String recipeRow : recipeList) {
            String[] elements = recipeRow.split("\\|", 3);
            for (String element : elements) {
                dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
                if (ingredient == null) {
                    dB.echoError("Invalid dItem ingredient, recipe will not be registered for item script '" + container.getName() + "': " + element);
                    shouldRegister = false;
                    break recipeLoop;
                }
                ingredients.add(ingredient);
            }
        }
        // will be checked manually inside ItemScriptHelper
        if (shouldRegister) {
            ItemScriptContainer.specialrecipesMap.put(container, ingredients);
        }
    }
    for (Map.Entry<ItemScriptContainer, String> entry : shapeless_to_register.entrySet()) {
        ItemScriptContainer container = entry.getKey();
        String string = entry.getValue();
        String list = TagManager.tag(string, new BukkitTagContext(container.player, container.npc, false, null, dB.shouldDebug(container), new dScript(container)));
        List<dItem> ingredients = new ArrayList<dItem>();
        boolean shouldRegister = true;
        for (String element : dList.valueOf(list)) {
            dItem ingredient = dItem.valueOf(element.replaceAll("[iImM]@", ""));
            if (ingredient == null) {
                dB.echoError("Invalid dItem ingredient, shapeless recipe will not be registered for item script '" + container.getName() + "': " + element);
                shouldRegister = false;
                break;
            }
            ingredients.add(ingredient);
        }
        if (shouldRegister) {
            ItemScriptContainer.shapelessRecipesMap.put(container, ingredients);
        }
    }
    for (Map.Entry<ItemScriptContainer, String> entry : furnace_to_register.entrySet()) {
        dItem furnace_item = dItem.valueOf(entry.getValue());
        if (furnace_item == null) {
            dB.echoError("Invalid item '" + entry.getValue() + "'");
            continue;
        }
        FurnaceRecipe recipe = new FurnaceRecipe(entry.getKey().getItemFrom().getItemStack(), furnace_item.getMaterial().getMaterial(), furnace_item.getItemStack().getDurability());
        Bukkit.getServer().addRecipe(recipe);
    }
    recipes_to_register.clear();
    shapeless_to_register.clear();
    furnace_to_register.clear();
}
Also used : ArrayList(java.util.ArrayList) FurnaceRecipe(org.bukkit.inventory.FurnaceRecipe) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) ArrayList(java.util.ArrayList) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventHandler(org.bukkit.event.EventHandler)

Example 2 with FurnaceRecipe

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

FurnaceRecipe (org.bukkit.inventory.FurnaceRecipe)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)1 Enchantment (org.bukkit.enchantments.Enchantment)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1 Recipe (org.bukkit.inventory.Recipe)1 ShapedRecipe (org.bukkit.inventory.ShapedRecipe)1 ShapelessRecipe (org.bukkit.inventory.ShapelessRecipe)1 PEBinaryWriter (org.dragonet.utilities.io.PEBinaryWriter)1