Search in sources :

Example 1 with ItemStack

use of org.bukkit.inventory.ItemStack in project Core by iConomy.

the class InventoryDB method readInventory.

private ItemStack[] readInventory(String name) {
    try {
        NBTInputStream in = new NBTInputStream(new FileInputStream(new File(dataDir, name + ".dat")));
        CompoundTag tag = (CompoundTag) in.readTag();
        in.close();
        ListTag inventory = (ListTag) tag.getValue().get("Inventory");
        ItemStack[] stacks = new ItemStack[40];
        for (int i = 0; i < inventory.getValue().size(); ++i) {
            CompoundTag item = (CompoundTag) inventory.getValue().get(i);
            byte count = ((ByteTag) item.getValue().get("Count")).getValue();
            byte slot = ((ByteTag) item.getValue().get("Slot")).getValue();
            short damage = ((ShortTag) item.getValue().get("Damage")).getValue();
            short id = ((ShortTag) item.getValue().get("id")).getValue();
            stacks[slot] = new ItemStack(id, count, damage);
        }
        return stacks;
    } catch (IOException ex) {
        iConomy.Server.getLogger().log(Level.WARNING, "[iCo/InvDB] error reading inventory {0}: {1}", new Object[] { name, ex.getMessage() });
        return null;
    }
}
Also used : IOException(java.io.IOException) ListTag(com.iCo6.util.nbt.ListTag) FileInputStream(java.io.FileInputStream) ShortTag(com.iCo6.util.nbt.ShortTag) ByteTag(com.iCo6.util.nbt.ByteTag) NBTInputStream(com.iCo6.util.nbt.NBTInputStream) ItemStack(org.bukkit.inventory.ItemStack) File(java.io.File) CompoundTag(com.iCo6.util.nbt.CompoundTag)

Example 2 with ItemStack

use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.

the class ContainerEntry method rebuild.

@Override
public boolean rebuild(Block block) {
    if (!(block instanceof InventoryHolder))
        return false;
    Inventory inv = ((InventoryHolder) block.getState()).getInventory();
    List<HashMap<String, Integer>> ops = InventoryUtil.interpretDifferenceString(data);
    //Handle the additions
    if (ops.size() > 0) {
        for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(0))) inv.addItem(stack);
    }
    //Handle subtractions
    if (ops.size() > 1) {
        for (ItemStack stack : InventoryUtil.uncompressInventory(ops.get(1))) inv.removeItem(stack);
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 3 with ItemStack

use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.

the class MonitorEntityListener method onEntityDeath.

/**
	 * Uses the lastAttacker field in the players {@link PlayerSession} to log the death and cause
	 */
@HawkEvent(dataType = { DataType.PVP_DEATH, DataType.MOB_DEATH, DataType.OTHER_DEATH, DataType.ENTITY_KILL })
public void onEntityDeath(EntityDeathEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof Player) {
        //Player death
        Player victim = (Player) entity;
        //Mob or PVP death
        if (victim.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) (victim.getLastDamageCause())).getDamager();
            if (damager instanceof Player) {
                if (!Config.isLogged(DataType.PVP_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.PVP_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            } else {
                if (!Config.isLogged(DataType.MOB_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.MOB_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            }
        //Other death
        } else {
            if (!Config.isLogged(DataType.OTHER_DEATH) && !Config.LogDeathDrops)
                return;
            EntityDamageEvent dEvent = victim.getLastDamageCause();
            String cause = dEvent == null ? "Unknown" : victim.getLastDamageCause().getCause().name();
            String[] words = cause.split("_");
            for (int i = 0; i < words.length; i++) words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
            cause = Util.join(Arrays.asList(words), " ");
            DataManager.addEntry(new DataEntry(victim, DataType.OTHER_DEATH, victim.getLocation(), cause));
        }
        //Log item drops
        if (Config.LogDeathDrops) {
            String data = null;
            for (ItemStack stack : event.getDrops()) {
                if (stack.getData() != null)
                    data = stack.getAmount() + "x " + stack.getTypeId() + ":" + stack.getData().getData();
                else
                    data = stack.getAmount() + "x " + stack.getTypeId();
                DataManager.addEntry(new DataEntry(victim, DataType.ITEM_DROP, victim.getLocation(), data));
            }
        }
    } else {
        //Mob Death
        if (!Config.isLogged(DataType.ENTITY_KILL))
            return;
        if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
            //Only interested in player kills
            if (!(damager instanceof Player))
                return;
            Player player = (Player) damager;
            DataManager.addEntry(new DataEntry(player, DataType.ENTITY_KILL, entity.getLocation(), Util.getEntityName(entity)));
        }
    }
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ItemStack(org.bukkit.inventory.ItemStack) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 4 with ItemStack

use of org.bukkit.inventory.ItemStack in project HawkEye by oliverwoodings.

the class BlockUtil method itemStringToStack.

/**
	 * Converts an item string into an ItemStack
	 * @param item item string representing the material and data
	 * @param amount
	 * @return an ItemStack
	 */
public static ItemStack itemStringToStack(String item, Integer amount) {
    String[] itemArr = item.split(":");
    ItemStack stack = new ItemStack(Integer.parseInt(itemArr[0]), amount);
    if (itemArr.length > 1)
        stack.setData(new MaterialData(Integer.parseInt(itemArr[0]), Byte.parseByte(itemArr[1])));
    return stack;
}
Also used : MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack)

Example 5 with ItemStack

use of org.bukkit.inventory.ItemStack in project TotalFreedomMod by TotalFreedom.

the class Command_dispfill method run.

@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) {
    if (args.length == 2) {
        int radius;
        try {
            radius = Math.max(5, Math.min(25, Integer.parseInt(args[0])));
        } catch (NumberFormatException ex) {
            sender.sendMessage("Invalid radius.");
            return true;
        }
        final List<ItemStack> items = new ArrayList<>();
        final String[] itemsRaw = StringUtils.split(args[1], ",");
        for (final String searchItem : itemsRaw) {
            Material material = Material.matchMaterial(searchItem);
            if (material == null) {
                try {
                    material = DepreciationAggregator.getMaterial(Integer.parseInt(searchItem));
                } catch (NumberFormatException ex) {
                }
            }
            if (material != null) {
                items.add(new ItemStack(material, 64));
            } else {
                sender.sendMessage("Skipping invalid item: " + searchItem);
            }
        }
        final ItemStack[] itemsArray = items.toArray(new ItemStack[items.size()]);
        int affected = 0;
        final Location centerLocation = playerSender.getLocation();
        final Block centerBlock = centerLocation.getBlock();
        for (int xOffset = -radius; xOffset <= radius; xOffset++) {
            for (int yOffset = -radius; yOffset <= radius; yOffset++) {
                for (int zOffset = -radius; zOffset <= radius; zOffset++) {
                    final Block targetBlock = centerBlock.getRelative(xOffset, yOffset, zOffset);
                    if (targetBlock.getLocation().distanceSquared(centerLocation) < (radius * radius)) {
                        if (targetBlock.getType().equals(Material.DISPENSER)) {
                            sender.sendMessage("Filling dispenser @ " + FUtil.formatLocation(targetBlock.getLocation()));
                            setDispenserContents(targetBlock, itemsArray);
                            affected++;
                        }
                    }
                }
            }
        }
        sender.sendMessage("Done. " + affected + " dispenser(s) filled.");
    } else {
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)1282 Player (org.bukkit.entity.Player)262 EventHandler (org.bukkit.event.EventHandler)183 ItemMeta (org.bukkit.inventory.meta.ItemMeta)167 ArrayList (java.util.ArrayList)130 Inventory (org.bukkit.inventory.Inventory)115 Location (org.bukkit.Location)88 Material (org.bukkit.Material)87 PlayerInventory (org.bukkit.inventory.PlayerInventory)84 Entity (org.bukkit.entity.Entity)63 Block (org.bukkit.block.Block)52 TagCompound (de.keyle.knbt.TagCompound)47 Map (java.util.Map)44 LivingEntity (org.bukkit.entity.LivingEntity)44 HashMap (java.util.HashMap)40 Mage (com.elmakers.mine.bukkit.api.magic.Mage)39 Enchantment (org.bukkit.enchantments.Enchantment)38 SkullMeta (org.bukkit.inventory.meta.SkullMeta)38 Vector (org.bukkit.util.Vector)35 Test (org.junit.Test)32