Search in sources :

Example 1 with MetaItemStack

use of com.earth2me.essentials.MetaItemStack in project Essentials by drtshock.

the class Commandpotion method run.

@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final ItemStack stack = user.getItemInHand();
    if (args.length == 0) {
        final Set<String> potionslist = new TreeSet<>();
        for (Map.Entry<String, PotionEffectType> entry : Potions.entrySet()) {
            final String potionName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
            if (potionslist.contains(potionName) || (user.isAuthorized("essentials.potion." + potionName))) {
                potionslist.add(entry.getKey());
            }
        }
        throw new NotEnoughArgumentsException(tl("potions", StringUtil.joinList(potionslist.toArray())));
    }
    if (stack.getType() == Material.POTION) {
        PotionMeta pmeta = (PotionMeta) stack.getItemMeta();
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("clear")) {
                pmeta.clearCustomEffects();
                stack.setItemMeta(pmeta);
            } else if (args[0].equalsIgnoreCase("apply") && user.isAuthorized("essentials.potion.apply")) {
                for (PotionEffect effect : pmeta.getCustomEffects()) {
                    effect.apply(user.getBase());
                }
            } else if (args.length < 3) {
                throw new NotEnoughArgumentsException();
            } else {
                final MetaItemStack mStack = new MetaItemStack(stack);
                for (String arg : args) {
                    mStack.addPotionMeta(user.getSource(), true, arg, ess);
                }
                if (mStack.completePotion()) {
                    pmeta = (PotionMeta) mStack.getItemStack().getItemMeta();
                    stack.setItemMeta(pmeta);
                } else {
                    user.sendMessage(tl("invalidPotion"));
                    throw new NotEnoughArgumentsException();
                }
            }
        }
    } else {
        throw new Exception(tl("holdPotion"));
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) TreeSet(java.util.TreeSet) PotionEffectType(org.bukkit.potion.PotionEffectType) PotionMeta(org.bukkit.inventory.meta.PotionMeta) MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) MetaItemStack(com.earth2me.essentials.MetaItemStack)

Example 2 with MetaItemStack

use of com.earth2me.essentials.MetaItemStack in project Essentials by drtshock.

the class Commanditem method run.

@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    if (args.length < 1) {
        throw new NotEnoughArgumentsException();
    }
    ItemStack stack = ess.getItemDb().get(args[0]);
    final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
    if (ess.getSettings().permissionBasedItemSpawn() ? (!user.isAuthorized("essentials.itemspawn.item-all") && !user.isAuthorized("essentials.itemspawn.item-" + itemname) && !user.isAuthorized("essentials.itemspawn.item-" + stack.getTypeId())) : (!user.isAuthorized("essentials.itemspawn.exempt") && !user.canSpawnItem(stack.getTypeId()))) {
        throw new Exception(tl("cantSpawnItem", itemname));
    }
    try {
        if (args.length > 1 && Integer.parseInt(args[1]) > 0) {
            stack.setAmount(Integer.parseInt(args[1]));
        } else if (ess.getSettings().getDefaultStackSize() > 0) {
            stack.setAmount(ess.getSettings().getDefaultStackSize());
        } else if (ess.getSettings().getOversizedStackSize() > 0 && user.isAuthorized("essentials.oversizedstacks")) {
            stack.setAmount(ess.getSettings().getOversizedStackSize());
        }
    } catch (NumberFormatException e) {
        throw new NotEnoughArgumentsException();
    }
    MetaItemStack metaStack = new MetaItemStack(stack);
    if (!metaStack.canSpawn(ess)) {
        throw new Exception(tl("unableToSpawnItem", itemname));
    }
    if (args.length > 2) {
        final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe");
        metaStack.parseStringMeta(user.getSource(), allowUnsafe, args, 2, ess);
        stack = metaStack.getItemStack();
    }
    if (stack.getType() == Material.AIR) {
        throw new Exception(tl("cantSpawnItem", "Air"));
    }
    final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
    user.sendMessage(tl("itemSpawn", stack.getAmount(), displayName));
    if (user.isAuthorized("essentials.oversizedstacks")) {
        InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), stack);
    } else {
        InventoryWorkaround.addItems(user.getBase().getInventory(), stack);
    }
    user.getBase().updateInventory();
}
Also used : MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) MetaItemStack(com.earth2me.essentials.MetaItemStack)

Example 3 with MetaItemStack

use of com.earth2me.essentials.MetaItemStack in project Essentials by drtshock.

the class Commandenchant method run.

//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final ItemStack stack = user.getItemInHand();
    if (stack == null || stack.getType() == Material.AIR) {
        throw new Exception(tl("nothingInHand"));
    }
    if (args.length == 0) {
        final Set<String> enchantmentslist = new TreeSet<>();
        for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet()) {
            final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
            if (enchantmentslist.contains(enchantmentName) || (user.isAuthorized("essentials.enchantments." + enchantmentName) && entry.getValue().canEnchantItem(stack))) {
                enchantmentslist.add(entry.getKey());
            //enchantmentslist.add(enchantmentName);
            }
        }
        throw new NotEnoughArgumentsException(tl("enchantments", StringUtil.joinList(enchantmentslist.toArray())));
    }
    int level = -1;
    if (args.length > 1) {
        try {
            level = Integer.parseInt(args[1]);
        } catch (NumberFormatException ex) {
            level = -1;
        }
    }
    final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe");
    final MetaItemStack metaStack = new MetaItemStack(stack);
    final Enchantment enchantment = metaStack.getEnchantment(user, args[0]);
    metaStack.addEnchantment(user.getSource(), allowUnsafe, enchantment, level);
    InventoryWorkaround.setItemInMainHand(user.getBase(), metaStack.getItemStack());
    user.getBase().updateInventory();
    final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
    if (level == 0) {
        user.sendMessage(tl("enchantmentRemoved", enchantmentName.replace('_', ' ')));
    } else {
        user.sendMessage(tl("enchantmentApplied", enchantmentName.replace('_', ' ')));
    }
}
Also used : MetaItemStack(com.earth2me.essentials.MetaItemStack) TreeSet(java.util.TreeSet) MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) Map(java.util.Map)

Example 4 with MetaItemStack

use of com.earth2me.essentials.MetaItemStack in project Essentials by drtshock.

the class Commandgive method run.

@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
    if (args.length < 2) {
        throw new NotEnoughArgumentsException();
    }
    ItemStack stack = ess.getItemDb().get(args[1]);
    final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
    if (sender.isPlayer() && (ess.getSettings().permissionBasedItemSpawn() ? (!ess.getUser(sender.getPlayer()).isAuthorized("essentials.itemspawn.item-all") && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.itemspawn.item-" + itemname) && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.itemspawn.item-" + stack.getTypeId())) : (!ess.getUser(sender.getPlayer()).isAuthorized("essentials.itemspawn.exempt") && !ess.getUser(sender.getPlayer()).canSpawnItem(stack.getTypeId())))) {
        throw new Exception(tl("cantSpawnItem", itemname));
    }
    final User giveTo = getPlayer(server, sender, args, 0);
    try {
        if (args.length > 3 && NumberUtil.isInt(args[2]) && NumberUtil.isInt(args[3])) {
            stack.setAmount(Integer.parseInt(args[2]));
            stack.setDurability(Short.parseShort(args[3]));
        } else if (args.length > 2 && Integer.parseInt(args[2]) > 0) {
            stack.setAmount(Integer.parseInt(args[2]));
        } else if (ess.getSettings().getDefaultStackSize() > 0) {
            stack.setAmount(ess.getSettings().getDefaultStackSize());
        } else if (ess.getSettings().getOversizedStackSize() > 0 && giveTo.isAuthorized("essentials.oversizedstacks")) {
            stack.setAmount(ess.getSettings().getOversizedStackSize());
        }
    } catch (NumberFormatException e) {
        throw new NotEnoughArgumentsException();
    }
    MetaItemStack metaStack = new MetaItemStack(stack);
    if (!metaStack.canSpawn(ess)) {
        throw new Exception(tl("unableToSpawnItem", itemname));
    }
    if (args.length > 3) {
        boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
        if (allowUnsafe && sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.enchantments.allowunsafe")) {
            allowUnsafe = false;
        }
        int metaStart = NumberUtil.isInt(args[3]) ? 4 : 3;
        if (args.length > metaStart) {
            metaStack.parseStringMeta(sender, allowUnsafe, args, metaStart, ess);
        }
        stack = metaStack.getItemStack();
    }
    if (stack.getType() == Material.AIR) {
        throw new Exception(tl("cantSpawnItem", "Air"));
    }
    final String itemName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
    sender.sendMessage(tl("giveSpawn", stack.getAmount(), itemName, giveTo.getDisplayName()));
    Map<Integer, ItemStack> leftovers;
    if (giveTo.isAuthorized("essentials.oversizedstacks")) {
        leftovers = InventoryWorkaround.addOversizedItems(giveTo.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), stack);
    } else {
        leftovers = InventoryWorkaround.addItems(giveTo.getBase().getInventory(), stack);
    }
    boolean isDropItemsIfFull = ess.getSettings().isDropItemsIfFull();
    for (ItemStack item : leftovers.values()) {
        if (isDropItemsIfFull) {
            World w = giveTo.getWorld();
            w.dropItemNaturally(giveTo.getLocation(), item);
        } else {
            sender.sendMessage(tl("giveSpawnFailure", item.getAmount(), itemName, giveTo.getDisplayName()));
        }
    }
    giveTo.getBase().updateInventory();
}
Also used : User(com.earth2me.essentials.User) MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) World(org.bukkit.World) MetaItemStack(com.earth2me.essentials.MetaItemStack)

Example 5 with MetaItemStack

use of com.earth2me.essentials.MetaItemStack in project Essentials by drtshock.

the class Commandfirework method run.

@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final ItemStack stack = user.getBase().getItemInHand();
    if (stack.getType() == Material.FIREWORK) {
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("clear")) {
                FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                fmeta.clearEffects();
                stack.setItemMeta(fmeta);
                user.sendMessage(tl("fireworkEffectsCleared"));
            } else if (args.length > 1 && (args[0].equalsIgnoreCase("power") || (args[0].equalsIgnoreCase("p")))) {
                FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                try {
                    int power = Integer.parseInt(args[1]);
                    fmeta.setPower(power > 3 ? 4 : power);
                } catch (NumberFormatException e) {
                    throw new Exception(tl("invalidFireworkFormat", args[1], args[0]));
                }
                stack.setItemMeta(fmeta);
            } else if ((args[0].equalsIgnoreCase("fire") || (args[0].equalsIgnoreCase("f"))) && user.isAuthorized("essentials.firework.fire")) {
                int amount = 1;
                boolean direction = false;
                if (args.length > 1) {
                    if (NumberUtil.isInt(args[1])) {
                        final int serverLimit = ess.getSettings().getSpawnMobLimit();
                        amount = Integer.parseInt(args[1]);
                        if (amount > serverLimit) {
                            amount = serverLimit;
                            user.sendMessage(tl("mobSpawnLimit"));
                        }
                    } else {
                        direction = true;
                    }
                }
                for (int i = 0; i < amount; i++) {
                    Firework firework = (Firework) user.getWorld().spawnEntity(user.getLocation(), EntityType.FIREWORK);
                    FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                    if (direction) {
                        final Vector vector = user.getBase().getEyeLocation().getDirection().multiply(0.070);
                        if (fmeta.getPower() > 1) {
                            fmeta.setPower(1);
                        }
                        firework.setVelocity(vector);
                    }
                    firework.setFireworkMeta(fmeta);
                }
            } else {
                final MetaItemStack mStack = new MetaItemStack(stack);
                for (String arg : args) {
                    try {
                        mStack.addFireworkMeta(user.getSource(), true, arg, ess);
                    } catch (Exception e) {
                        user.sendMessage(tl("fireworkSyntax"));
                        throw e;
                    }
                }
                if (mStack.isValidFirework()) {
                    FireworkMeta fmeta = (FireworkMeta) mStack.getItemStack().getItemMeta();
                    FireworkEffect effect = mStack.getFireworkBuilder().build();
                    if (fmeta.getEffects().size() > 0 && !user.isAuthorized("essentials.firework.multiple")) {
                        throw new Exception(tl("multipleCharges"));
                    }
                    fmeta.addEffect(effect);
                    stack.setItemMeta(fmeta);
                } else {
                    user.sendMessage(tl("fireworkSyntax"));
                    throw new Exception(tl("fireworkColor"));
                }
            }
        } else {
            throw new NotEnoughArgumentsException();
        }
    } else {
        throw new Exception(tl("holdFirework"));
    }
}
Also used : Firework(org.bukkit.entity.Firework) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) MetaItemStack(com.earth2me.essentials.MetaItemStack) FireworkEffect(org.bukkit.FireworkEffect)

Aggregations

MetaItemStack (com.earth2me.essentials.MetaItemStack)5 ItemStack (org.bukkit.inventory.ItemStack)5 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 User (com.earth2me.essentials.User)1 FireworkEffect (org.bukkit.FireworkEffect)1 World (org.bukkit.World)1 Enchantment (org.bukkit.enchantments.Enchantment)1 Firework (org.bukkit.entity.Firework)1 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)1 PotionMeta (org.bukkit.inventory.meta.PotionMeta)1 PotionEffect (org.bukkit.potion.PotionEffect)1 PotionEffectType (org.bukkit.potion.PotionEffectType)1 Vector (org.bukkit.util.Vector)1