Search in sources :

Example 46 with ItemStack

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

the class PlayerBreaksItemScriptEvent method onPlayerItemBreak.

@EventHandler
public void onPlayerItemBreak(PlayerItemBreakEvent event) {
    if (dEntity.isNPC(event.getPlayer())) {
        return;
    }
    item = new dItem(event.getBrokenItem());
    this.event = event;
    fire();
    if (cancelled) {
        final Player player = event.getPlayer();
        final ItemStack itemstack = event.getBrokenItem();
        itemstack.setAmount(itemstack.getAmount() + 1);
        new BukkitRunnable() {

            public void run() {
                itemstack.setDurability(itemstack.getType().getMaxDurability());
                player.updateInventory();
            }
        }.runTaskLater(DenizenAPI.getCurrentInstance(), 1);
    }
}
Also used : Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 47 with ItemStack

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

the class Particle_v1_8_R3 method playFor.

@Override
public <T> void playFor(Player player, Location location, int count, Vector offset, double extra, T data) {
    int[] dataArray;
    if (data instanceof ItemStack) {
        dataArray = new int[] { ((ItemStack) data).getType().getId(), ((ItemStack) data).getDurability() };
    } else if (data instanceof MaterialData) {
        dataArray = new int[] { ((MaterialData) data).getItemTypeId() + (((MaterialData) data).getData() << 12) };
    }
    PacketHelper_v1_8_R3.sendPacket(player, new PacketPlayOutWorldParticles(particle, true, (float) location.getX(), (float) location.getY(), (float) location.getZ(), (float) offset.getX(), (float) offset.getY(), (float) offset.getZ(), (float) extra, count));
}
Also used : PacketPlayOutWorldParticles(net.minecraft.server.v1_8_R3.PacketPlayOutWorldParticles) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack)

Example 48 with ItemStack

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

the class InventoryScriptContainer method getInventoryFrom.

public dInventory getInventoryFrom(dPlayer player, dNPC npc) {
    // TODO: Clean all this code!
    dInventory inventory = null;
    BukkitTagContext context = new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this));
    try {
        if (contains("INVENTORY")) {
            if (InventoryType.valueOf(getString("INVENTORY").toUpperCase()) != null) {
                inventory = new dInventory(InventoryType.valueOf(getString("INVENTORY").toUpperCase()));
                if (contains("TITLE")) {
                    inventory.setTitle(TagManager.tag(getString("TITLE"), context));
                }
                inventory.setIdentifiers("script", getName());
            } else {
                dB.echoError("Invalid inventory type specified. Assuming \"CHEST\"");
            }
        }
        int size = 0;
        if (contains("SIZE")) {
            if (inventory != null && !getInventoryType().name().equalsIgnoreCase("CHEST")) {
                dB.echoError("You can only set the size of chest inventories!");
            } else {
                size = aH.getIntegerFrom(TagManager.tag(getString("SIZE"), context));
                if (size == 0) {
                    dB.echoError("Inventory size can't be 0. Assuming default of inventory type...");
                }
                if (size % 9 != 0) {
                    size = (int) Math.ceil(size / 9) * 9;
                    dB.echoError("Inventory size must be a multiple of 9! Rounding up to " + size + "...");
                }
                if (size < 0) {
                    size = size * -1;
                    dB.echoError("Inventory size must be a positive number! Inverting to " + size + "...");
                }
                inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
                inventory.setIdentifiers("script", getName());
            }
        }
        if (size == 0) {
            size = getInventoryType().getDefaultSize();
        }
        boolean[] filledSlots = new boolean[size];
        if (contains("SLOTS")) {
            ItemStack[] finalItems = new ItemStack[size];
            int itemsAdded = 0;
            for (String items : getStringList("SLOTS")) {
                items = TagManager.tag(items, context).trim();
                if (items.isEmpty()) {
                    continue;
                }
                if (!items.startsWith("[") || !items.endsWith("]")) {
                    dB.echoError("Inventory script \"" + getName() + "\" has an invalid slots line: [" + items + "]... Ignoring it");
                    continue;
                }
                String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
                for (String item : itemsInLine) {
                    if (contains("DEFINITIONS." + item)) {
                        dItem def = dItem.valueOf(TagManager.tag(getString("DEFINITIONS." + item), context), player, npc);
                        if (def == null) {
                            dB.echoError("Invalid definition '" + item + "' in inventory script '" + getName() + "'" + "... Ignoring it and assuming \"AIR\"");
                            finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        } else {
                            finalItems[itemsAdded] = def.getItemStack();
                        }
                    } else if (dItem.matches(item)) {
                        finalItems[itemsAdded] = dItem.valueOf(item, player, npc).getItemStack();
                    } else {
                        finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        if (!item.isEmpty()) {
                            dB.echoError("Inventory script \"" + getName() + "\" has an invalid slot item: [" + item + "]... Ignoring it and assuming \"AIR\"");
                        }
                    }
                    filledSlots[itemsAdded] = !item.isEmpty();
                    itemsAdded++;
                }
            }
            if (inventory == null) {
                size = finalItems.length % 9 == 0 ? finalItems.length : Math.round(finalItems.length / 9) * 9;
                inventory = new dInventory(size == 0 ? 9 : size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
            }
            inventory.setContents(finalItems);
        }
        if (contains("PROCEDURAL ITEMS")) {
            // TODO: Document this feature!
            if (inventory == null) {
                size = InventoryType.CHEST.getDefaultSize();
                inventory = new dInventory(size, contains("TITLE") ? TagManager.tag(getString("TITLE"), context) : "Chest");
            }
            List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "PROCEDURAL ITEMS");
            if (!entries.isEmpty()) {
                long id = DetermineCommand.getNewId();
                ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
                InstantQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId("INV_SCRIPT_ITEM_PROC"));
                queue.addEntries(entries);
                queue.setReqId(id);
                if (contains("DEFINITIONS")) {
                    YamlConfiguration section = getConfigurationSection("DEFINITIONS");
                    for (StringHolder string : section.getKeys(false)) {
                        String definition = string.str;
                        queue.addDefinition(definition, section.getString(definition));
                    }
                }
                queue.start();
                if (DetermineCommand.hasOutcome(id)) {
                    dList list = dList.valueOf(DetermineCommand.getOutcome(id).get(0));
                    if (list != null) {
                        int x = 0;
                        for (dItem item : list.filter(dItem.class)) {
                            while (x < filledSlots.length && filledSlots[x]) {
                                x++;
                            }
                            if (x >= filledSlots.length || filledSlots[x]) {
                                break;
                            }
                            inventory.setSlots(x, item.getItemStack());
                            filledSlots[x] = true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        dB.echoError("Woah! An exception has been called with this inventory script!");
        dB.echoError(e);
        inventory = null;
    }
    if (inventory != null) {
        InventoryScriptHelper.tempInventoryScripts.put(inventory.getInventory(), getName());
    }
    return inventory;
}
Also used : net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dInventory(net.aufdemrand.denizen.objects.dInventory) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) YamlConfiguration(net.aufdemrand.denizencore.utilities.YamlConfiguration) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) StringHolder(net.aufdemrand.denizencore.utilities.text.StringHolder) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) InstantQueue(net.aufdemrand.denizencore.scripts.queues.core.InstantQueue) ItemStack(org.bukkit.inventory.ItemStack)

Example 49 with ItemStack

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

the class ItemScriptHelper method processSpecialRecipes.

// Compare a crafting matrix with all stored special recipes right
// after a click or drag has been made in it
public boolean processSpecialRecipes(final CraftingInventory inventory, final Player player) {
    // Store the current matrix
    ItemStack[] matrix1 = inventory.getMatrix();
    for (int i = 0; i < matrix1.length; i++) {
        matrix1[i] = matrix1[i] == null ? new ItemStack(Material.AIR) : matrix1[i].clone();
    }
    // Get the result of the special recipe that this matrix matches,
    // if any
    dItem result1 = getSpecialRecipeResult(matrix1, player);
    boolean returnme = result1 != null;
    // Run a task 1 tick later than the event from which this method
    // was called, to check the new state of the CraftingInventory's matrix
    Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

        @Override
        public void run() {
            // Store the current matrix
            ItemStack[] matrix = inventory.getMatrix();
            for (int i = 0; i < matrix.length; i++) {
                matrix[i] = matrix[i] == null ? new ItemStack(Material.AIR) : matrix[i].clone();
            }
            // Get the result of the special recipe that this matrix matches,
            // if any
            dItem result = getSpecialRecipeResult(matrix, player);
            // Proceed only if the result was not null
            if (result != null) {
                dList recipeList = new dList();
                for (ItemStack item : matrix) {
                    if (item != null) {
                        recipeList.add(new dItem(item).identify());
                    } else {
                        recipeList.add(new dItem(Material.AIR).identify());
                    }
                }
                ItemRecipeFormedScriptEvent event = ItemRecipeFormedScriptEvent.instance;
                event.result = result;
                event.recipe = recipeList;
                event.inventory = inventory;
                event.player = dPlayer.mirrorBukkitPlayer(player);
                event.cancelled = false;
                event.resultChanged = false;
                event.fire();
                if (event.cancelled) {
                    inventory.setResult(null);
                } else {
                    // If this was a valid match, set the crafting's result
                    inventory.setResult(event.result.getItemStack());
                }
                // Update the player's inventory
                player.updateInventory();
            }
        }
    }, 2);
    return returnme;
}
Also used : net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) ItemRecipeFormedScriptEvent(net.aufdemrand.denizen.events.player.ItemRecipeFormedScriptEvent) ItemStack(org.bukkit.inventory.ItemStack)

Example 50 with ItemStack

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

the class PlayEffectCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Extract objects from ScriptEntry
    List<dLocation> locations = (List<dLocation>) scriptEntry.getObject("location");
    List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
    Effect effect = (Effect) scriptEntry.getObject("effect");
    Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
    Element iconcrack = scriptEntry.getElement("iconcrack");
    Element iconcrack_data = scriptEntry.getElement("iconcrack_data");
    Element iconcrack_type = scriptEntry.getElement("iconcrack_type");
    Element radius = scriptEntry.getElement("radius");
    Element data = scriptEntry.getElement("data");
    Element qty = scriptEntry.getElement("qty");
    dLocation offset = scriptEntry.getdObject("offset");
    // Report to dB
    dB.report(scriptEntry, getName(), (effect != null ? aH.debugObj("effect", effect.getName()) : particleEffect != null ? aH.debugObj("special effect", particleEffect.getName()) : iconcrack_type.debug() + iconcrack.debug() + (iconcrack_data != null ? iconcrack_data.debug() : "")) + aH.debugObj("locations", locations.toString()) + (targets != null ? aH.debugObj("targets", targets.toString()) : "") + radius.debug() + data.debug() + qty.debug() + offset.debug());
    for (dLocation location : locations) {
        // Slightly increase the location's Y so effects don't seem to come out of the ground
        location.add(0, 1, 0);
        // Play the Bukkit effect the number of times specified
        if (effect != null) {
            for (int n = 0; n < qty.asInt(); n++) {
                if (targets != null) {
                    for (dPlayer player : targets) {
                        if (player.isValid() && player.isOnline()) {
                            effect.playFor(player.getPlayerEntity(), location, data.asInt());
                        }
                    }
                } else {
                    effect.play(location, data.asInt(), radius.asInt());
                }
            }
        } else // Play a ParticleEffect
        if (particleEffect != null) {
            float osX = (float) offset.getX();
            float osY = (float) offset.getY();
            float osZ = (float) offset.getZ();
            List<Player> players = new ArrayList<Player>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (dPlayer player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            for (Player player : players) {
                particleEffect.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat());
            }
        } else // Play an iconcrack (item break) effect
        {
            List<Player> players = new ArrayList<Player>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (dPlayer player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            // TODO: better this all
            if (iconcrack_type.asString().equalsIgnoreCase("iconcrack")) {
                ItemStack itemStack = new ItemStack(iconcrack.asInt(), 1, (short) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
                Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("ITEM_CRACK");
                for (Player player : players) {
                    particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), itemStack);
                }
            } else if (iconcrack_type.asString().equalsIgnoreCase("blockcrack")) {
                MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
                Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_CRACK");
                for (Player player : players) {
                    particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
                }
            } else {
                // blockdust
                MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
                Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_DUST");
                for (Player player : players) {
                    particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
                }
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Player(org.bukkit.entity.Player) Element(net.aufdemrand.denizencore.objects.Element) Particle(net.aufdemrand.denizen.nms.interfaces.Particle) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Effect(net.aufdemrand.denizen.nms.interfaces.Effect) MaterialData(org.bukkit.material.MaterialData) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)1420 Player (org.bukkit.entity.Player)270 EventHandler (org.bukkit.event.EventHandler)197 ItemMeta (org.bukkit.inventory.meta.ItemMeta)192 ArrayList (java.util.ArrayList)151 Inventory (org.bukkit.inventory.Inventory)127 Material (org.bukkit.Material)103 Location (org.bukkit.Location)96 PlayerInventory (org.bukkit.inventory.PlayerInventory)84 Entity (org.bukkit.entity.Entity)67 Block (org.bukkit.block.Block)54 LivingEntity (org.bukkit.entity.LivingEntity)51 TagCompound (de.keyle.knbt.TagCompound)47 HashMap (java.util.HashMap)47 Map (java.util.Map)46 Vector (org.bukkit.util.Vector)44 Enchantment (org.bukkit.enchantments.Enchantment)41 SkullMeta (org.bukkit.inventory.meta.SkullMeta)41 Mage (com.elmakers.mine.bukkit.api.magic.Mage)39 Test (org.junit.Test)38