Search in sources :

Example 66 with PotionEffect

use of org.bukkit.potion.PotionEffect in project Glowstone by GlowstoneMC.

the class LivingEntityStore method load.

// these tags that apply to living entities only are documented as global:
// - short "Air"
// - string "CustomName"
// - bool "CustomNameVisible"
// todo: the following tags
// - float "AbsorptionAmount"
// - short "HurtTime"
// - int "HurtByTimestamp"
// - short "DeathTime"
// - bool "PersistenceRequired"
// - bool "Leashed"
// - compound "Leash"
// on ActiveEffects, bool "ShowParticles"
@Override
public void load(T entity, CompoundTag compound) {
    super.load(entity, compound);
    if (compound.isShort("Air")) {
        entity.setRemainingAir(compound.getShort("Air"));
    }
    if (compound.isString("CustomName")) {
        entity.setCustomName(compound.getString("CustomName"));
    }
    if (compound.isByte("CustomNameVisible")) {
        entity.setCustomNameVisible(compound.getBool("CustomNameVisible"));
    }
    if (compound.isFloat("HealF")) {
        entity.setHealth(compound.getFloat("HealF"));
    } else if (compound.isShort("Health")) {
        entity.setHealth(compound.getShort("Health"));
    }
    if (compound.isShort("AttackTime")) {
        entity.setNoDamageTicks(compound.getShort("AttackTime"));
    }
    if (compound.isByte("FallFlying")) {
        entity.setFallFlying(compound.getBool("FallFlying"));
    }
    if (compound.isList("ActiveEffects", TagType.COMPOUND)) {
        for (CompoundTag effect : compound.getCompoundList("ActiveEffects")) {
            // should really always have every field, but be forgiving if possible
            if (!effect.isByte("Id") || !effect.isInt("Duration")) {
                continue;
            }
            PotionEffectType type = PotionEffectType.getById(effect.getByte("Id"));
            int duration = effect.getInt("Duration");
            if (type == null || duration < 0) {
                continue;
            }
            int amplifier = 0;
            boolean ambient = false;
            if (compound.isByte("Amplifier")) {
                amplifier = compound.getByte("Amplifier");
            }
            if (compound.isByte("Ambient")) {
                ambient = compound.getBool("Ambient");
            }
            // bool "ShowParticles"
            entity.addPotionEffect(new PotionEffect(type, duration, amplifier, ambient), true);
        }
    }
    EntityEquipment equip = entity.getEquipment();
    if (equip != null) {
        if (compound.isList("Equipment", TagType.COMPOUND)) {
            List<CompoundTag> list = compound.getCompoundList("Equipment");
            if (list.size() == 5) {
                equip.setItemInHand(NbtSerialization.readItem(list.get(0)));
                equip.setBoots(NbtSerialization.readItem(list.get(1)));
                equip.setLeggings(NbtSerialization.readItem(list.get(2)));
                equip.setChestplate(NbtSerialization.readItem(list.get(3)));
                equip.setHelmet(NbtSerialization.readItem(list.get(4)));
            }
        }
        if (compound.isList("DropChances", TagType.FLOAT)) {
            List<Float> list = compound.getList("DropChances", TagType.FLOAT);
            if (list.size() == 5) {
                equip.setItemInHandDropChance(list.get(0));
                equip.setBootsDropChance(list.get(1));
                equip.setLeggingsDropChance(list.get(2));
                equip.setChestplateDropChance(list.get(3));
                equip.setHelmetDropChance(list.get(4));
            }
        }
    }
    if (compound.isByte("CanPickUpLoot")) {
        entity.setCanPickupItems(compound.getBool("CanPickUpLoot"));
    }
    if (compound.isList("Attributes", TagType.COMPOUND)) {
        List<CompoundTag> attributes = compound.getCompoundList("Attributes");
        AttributeManager am = entity.getAttributeManager();
        for (CompoundTag tag : attributes) {
            if (tag.isString("Name") && tag.isDouble("Base")) {
                List<Modifier> modifiers = null;
                if (tag.isList("Modifiers", TagType.COMPOUND)) {
                    modifiers = new ArrayList<>();
                    List<CompoundTag> modifierTags = tag.getCompoundList("Modifiers");
                    for (CompoundTag modifierTag : modifierTags) {
                        if (modifierTag.isDouble("Amount") && modifierTag.isString("Name") && modifierTag.isInt("Operation") && modifierTag.isLong("UUIDLeast") && modifierTag.isLong("UUIDMost")) {
                            modifiers.add(new Modifier(modifierTag.getString("Name"), new UUID(modifierTag.getLong("UUIDLeast"), modifierTag.getLong("UUIDMost")), modifierTag.getDouble("Amount"), (byte) modifierTag.getInt("Operation")));
                        }
                    }
                }
                am.setProperty(tag.getString("Name"), tag.getDouble("Base"), modifiers);
            }
        }
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) AttributeManager(net.glowstone.entity.AttributeManager) EntityEquipment(org.bukkit.inventory.EntityEquipment) Modifier(net.glowstone.entity.AttributeManager.Modifier) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 67 with PotionEffect

use of org.bukkit.potion.PotionEffect 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 68 with PotionEffect

use of org.bukkit.potion.PotionEffect in project Essentials by drtshock.

the class ItemDb method serialize.

@Override
public String serialize(ItemStack is) {
    String mat = is.getType().name();
    if (is.getData().getData() != 0) {
        mat = mat + ":" + is.getData().getData();
    }
    int quantity = is.getAmount();
    // Add space AFTER you add something. We can trim at end.
    StringBuilder sb = new StringBuilder();
    sb.append(mat).append(" ").append(quantity).append(" ");
    // ItemMeta applies to anything.
    if (is.hasItemMeta()) {
        ItemMeta meta = is.getItemMeta();
        if (meta.hasDisplayName()) {
            sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" ");
        }
        if (meta.hasLore()) {
            sb.append("lore:");
            boolean first = true;
            for (String s : meta.getLore()) {
                // to do this since we need each line separated by |
                if (!first) {
                    sb.append("|");
                }
                first = false;
                sb.append(s.replaceAll(" ", "_"));
            }
            sb.append(" ");
        }
        if (meta.hasEnchants()) {
            for (Enchantment e : meta.getEnchants().keySet()) {
                sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" ");
            }
        }
    }
    switch(is.getType()) {
        case WRITTEN_BOOK:
            // Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order.
            // Interesting as I didn't see a way to do pages or chapters.
            BookMeta bookMeta = (BookMeta) is.getItemMeta();
            if (bookMeta.hasTitle()) {
                sb.append("title:").append(bookMeta.getTitle()).append(" ");
            }
            if (bookMeta.hasAuthor()) {
                sb.append("author:").append(bookMeta.getAuthor()).append(" ");
            }
            // Only other thing it could have is lore but that's done up there ^^^
            break;
        case ENCHANTED_BOOK:
            EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta();
            for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) {
                sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
            }
            break;
        case FIREWORK:
            // Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order.
            FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta();
            if (fireworkMeta.hasEffects()) {
                for (FireworkEffect effect : fireworkMeta.getEffects()) {
                    if (effect.getColors() != null && !effect.getColors().isEmpty()) {
                        sb.append("color:");
                        boolean first = true;
                        for (Color c : effect.getColors()) {
                            if (!first) {
                                // same thing as above.
                                sb.append(",");
                            }
                            sb.append(c.toString());
                            first = false;
                        }
                        sb.append(" ");
                    }
                    sb.append("shape: ").append(effect.getType().name()).append(" ");
                    if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) {
                        sb.append("fade:");
                        boolean first = true;
                        for (Color c : effect.getFadeColors()) {
                            if (!first) {
                                // same thing as above.
                                sb.append(",");
                            }
                            sb.append(c.toString());
                            first = false;
                        }
                        sb.append(" ");
                    }
                }
                sb.append("power: ").append(fireworkMeta.getPower()).append(" ");
            }
            break;
        case POTION:
            Potion potion = Potion.fromItemStack(is);
            for (PotionEffect e : potion.getEffects()) {
                // long but needs to be effect:speed power:2 duration:120 in that order.
                sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
            }
            break;
        case SKULL_ITEM:
            // item stack with meta
            SkullMeta skullMeta = (SkullMeta) is.getItemMeta();
            if (skullMeta != null && skullMeta.hasOwner()) {
                sb.append("player:").append(skullMeta.getOwner()).append(" ");
            }
            break;
        case LEATHER_HELMET:
        case LEATHER_CHESTPLATE:
        case LEATHER_LEGGINGS:
        case LEATHER_BOOTS:
            LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta();
            int rgb = leatherArmorMeta.getColor().asRGB();
            sb.append("color:").append(rgb).append(" ");
            break;
        case BANNER:
            BannerMeta bannerMeta = (BannerMeta) is.getItemMeta();
            if (bannerMeta != null) {
                int basecolor = bannerMeta.getBaseColor().getColor().asRGB();
                sb.append("basecolor:").append(basecolor).append(" ");
                for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) {
                    String type = p.getPattern().getIdentifier();
                    int color = p.getColor().getColor().asRGB();
                    sb.append(type).append(",").append(color).append(" ");
                }
            }
            break;
        case SHIELD:
            // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795
            BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta();
            Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState();
            int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB();
            sb.append("basecolor:").append(basecolor).append(" ");
            for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) {
                String type = p.getPattern().getIdentifier();
                int color = p.getColor().getColor().asRGB();
                sb.append(type).append(",").append(color).append(" ");
            }
            break;
    }
    return sb.toString().trim().replaceAll("ยง", "&");
}
Also used : Potion(org.bukkit.potion.Potion) PotionEffect(org.bukkit.potion.PotionEffect) Banner(org.bukkit.block.Banner) Color(org.bukkit.Color) FireworkEffect(org.bukkit.FireworkEffect) Enchantment(org.bukkit.enchantments.Enchantment)

Aggregations

PotionEffect (org.bukkit.potion.PotionEffect)68 Player (org.bukkit.entity.Player)17 ArrayList (java.util.ArrayList)12 PotionEffectType (org.bukkit.potion.PotionEffectType)11 EventHandler (org.bukkit.event.EventHandler)10 Entity (org.bukkit.entity.Entity)8 LivingEntity (org.bukkit.entity.LivingEntity)7 ItemStack (org.bukkit.inventory.ItemStack)7 Projectile (org.bukkit.entity.Projectile)6 Location (org.bukkit.Location)4 Minigame (au.com.mineauz.minigames.minigame.Minigame)3 List (java.util.List)3 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)3 LoadoutAddon (au.com.mineauz.minigames.minigame.modules.LoadoutModule.LoadoutAddon)2 MinorPowerPowerStance (com.magmaguy.elitemobs.powerstances.MinorPowerPowerStance)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 Element (net.aufdemrand.denizencore.objects.Element)2