PotionEffects not setting

Discussion in 'Plugin Development' started by Ross Gosling, Jan 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Ross Gosling

    I setup a function that when called, creates a potion and returns an item stack.
    The potion effects I am trying to apply do not appear on the potion in game.

    Code:
        public ItemStack InvisibilityPotion(int number) {
         
            //Makes the item look a certain colour
            Potion potion = new Potion(PotionType.FIRE_RESISTANCE);
            potion.getEffects().clear();
         
            //Sets effects
            potion.getEffects().add(new PotionEffect(PotionEffectType.INVISIBILITY, (int) 0, (int) 600));
            potion.getEffects().add(new PotionEffect(PotionEffectType.WEAKNESS, (int) 9, (int) 600));
         
            //Sets display name
            ItemStack stack = potion.toItemStack(number);
            ItemMeta stackMeta = stack.getItemMeta();
            stackMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Invisibility Potion");
         
            //Sets item lore
            ArrayList<String> loreText = new ArrayList<String>();
            loreText.add(ChatColor.YELLOW + "Consumed for a brief period of invisibility");
            stackMeta.setLore(loreText);
     
            stack.setItemMeta(stackMeta);
         
            return stack;
     
        }
    [​IMG]
     
  2. Offline

    Ross Gosling

  3. Offline

    xTrollxDudex

  4. Offline

    Niknea

    Ross Gosling seems like you made an item stack that was a fire potion, and called it invisibly potion, did you want it to be a invisibly potion?
     
  5. Offline

    Ross Gosling

    @xTrollxDudex, Yeah, it's added to the player's inventory by calling this method.

    @Niknea, I create a fire potion, then it's suppose to clear the effects and apply new ones, i'm after a custom potion with the looks of a fire potion.

    I would've though both of these would be very obvious with a picture of the potion and code that is commented saying exactly what each stage is doing...
     
  6. Offline

    Monkey_Swag

    When you drink it, does it act as a fire resistance potion? Or does it just say it's a fire res potion but gives you invisibility?
     
  7. Offline

    Ross Gosling

    It acts just like an ordinary Fire Resist, It can't seem to clear the effects or add the new ones to the stack :/
     
  8. Offline

    xTigerRebornx

    Ross Gosling
    Code:
     @Deprecated
    316        public short toDamageValue() {
    317            short damage;
    318            if (type == PotionType.WATER) {
    319                return 0;
    320            } else if (type == null) {
    321                // Without this, mundanePotion.toDamageValue() would return 0
    322                damage = (short) (name == 0 ? 8192 : name);
    323            } else {
    324                damage = (short) (level - 1);
    325                damage <<= TIER_SHIFT;
    326                damage |= (short) type.getDamageValue();
    327            }
    328            if (splash) {
    329                damage |= SPLASH_BIT;
    330            }
    331            if (extended) {
    332                damage |= EXTENDED_BIT;
    333            }
    334            return damage;
    335        }
    Code for Potion's method of getting the damage value. It reads the type you specified when you created, I believe.
     
  9. Offline

    Ross Gosling


    How can I incorporate this into my code correctly?
     
  10. Offline

    xTigerRebornx

    Ross Gosling
    Try something along these lines?
    Code:
    ItemStack s = new ItemStack(Material.POTION);
    Potion pot = Potion.fromItemStack(s);
    pot.setType(PotionType.FIRE_RESISTANCE);
    pot.getEffects().clear();
    // Do rest here
    pot.apply(s);
     
  11. Offline

    Ross Gosling

    Now it doesnt give the player any potion and says:
    "Console: java.lang.IllegalArgumentException: Water bottles don't have a level!"
     
  12. Offline

    xTigerRebornx

  13. Offline

    Ross Gosling

    xTigerRebornx
    Code:
        public ItemStack healingPotion(int number, String type) {
       
            int size = 0;
            int restoredHP = 0;
            PotionType potionType = null;
       
            if(type.equalsIgnoreCase("small")) {
                size = 0;
                restoredHP = 2;
                potionType = PotionType.REGEN;
            }
       
            else if(type.equalsIgnoreCase("medium")) {
                size = 1;
                restoredHP = 4;
                potionType = PotionType.INSTANT_HEAL;
            }
       
            else if(type.equalsIgnoreCase("large")) {
                size = 2;
                restoredHP = 6;
                potionType = PotionType.STRENGTH;
            }
       
            //Makes the item look a certain colour
            ItemStack stack = new ItemStack(Material.POTION, number);
            Potion potion = Potion.fromItemStack(stack);
            potion.setType(potionType);
            potion.getEffects().clear();
       
            //Sets effects
            potion.getEffects().add(new PotionEffect(PotionEffectType.HEAL, (int) 1, size));
            potion.getEffects().add(new PotionEffect(PotionEffectType.WEAKNESS, (int) 100, (int) 1));
            potion.getEffects().add(new PotionEffect(PotionEffectType.SLOW, (int) 60, (int) 1));
            potion.apply(stack);
       
            //Sets display name
            ItemMeta stackMeta = stack.getItemMeta();
            stackMeta.setDisplayName(ChatColor.LIGHT_PURPLE + type + " Healing Potion");
       
            //Sets item lore
            ArrayList<String> loreText = new ArrayList<String>();
            loreText.add(ChatColor.YELLOW + "Consumed to restore " + restoredHP + " health");
            stackMeta.setLore(loreText);
     
            stack.setItemMeta(stackMeta);
       
            return stack;
     
        }
    Within the console it just says:
    12:03:12 AM
    CONSOLE:
    java.lang.IllegalArgumentException: Water bottles don't have a level!
     
  14. Offline

    xTigerRebornx

    Ross Gosling I need a line number, it wouldn't just say that one line.
     
  15. Offline

    Ross Gosling

    xTigerRebornx I tested again and this time it actually generated a proper error:
    The context I use my potions in is that when someone dies they recieve a set of items.

    Code:
    Could not pass event PlayerRespawnEvent to CustomGoslingPlugin v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.moveToWorld(PlayerList.java:469) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.moveToWorld(PlayerList.java:408) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1111) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInClientCommand.a(SourceFile:50) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInClientCommand.handle(SourceFile:8) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:651) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:541) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:453) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
    Caused by: java.lang.IllegalArgumentException: Water bottles don't have a level!
        at org.apache.commons.lang.Validate.isTrue(Validate.java:157) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.potion.Potion.<init>(Potion.java:78) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.potion.Potion.fromDamage(Potion.java:394) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at org.bukkit.potion.Potion.fromItemStack(Potion.java:409) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        at me.x3DGosling.CustomGoslingPlugin.MakeItemStack.healingPotion(MakeItemStack.java:294) ~[?:?]
        at me.x3DGosling.CustomGoslingPlugin.MiscSystem.giveTestKit(MiscSystem.java:124) ~[?:?]
        at me.x3DGosling.CustomGoslingPlugin.Main.onPlayerRespawn(Main.java:1427) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-21-gcf51e92-b2938jnks]
        ... 15 more
     
  16. Offline

    xTigerRebornx

    Ross Gosling Sorry, haven't used potions much before, try this
    Code:
    ItemStack item = new ItemStack(Material.POTION, number);
    Potion potion = new Potion(PotionType.<Your Type>);
         
    // Do your stuff here
    potion.apply(item); 
     
  17. Offline

    Ross Gosling

    xTigerRebornx

    It no longer has the error but the same problem as it did first of all, the code is currently as follows:
    Code:
        public ItemStack invisibilityPotion(int number) {
           
            //Makes the item look a certain colour
            ItemStack stack = new ItemStack(Material.POTION, number);
            Potion potion = new Potion(PotionType.FIRE_RESISTANCE);
            potion.getEffects().clear();
           
            //Sets effects
            potion.getEffects().add(new PotionEffect(PotionEffectType.INVISIBILITY, (int) 600, (int) 0));
            potion.getEffects().add(new PotionEffect(PotionEffectType.WEAKNESS, (int) 600, (int) 9));
           
            potion.apply(stack);
           
            //Sets display name
            ItemMeta stackMeta = stack.getItemMeta();
            stackMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Invisibility Potion");
           
            //Sets item lore
            ArrayList<String> loreText = new ArrayList<String>();
            loreText.add(ChatColor.YELLOW + "Consumed for a brief period of invisibility");
            stackMeta.setLore(loreText);
     
            stack.setItemMeta(stackMeta);
           
            return stack;
       
        }
     
  18. Offline

    xTigerRebornx

    Ross Gosling Try:
    Code:
    ItemStack stack = new ItemStack(Material.POTION, number);
    Potion potion = new Potion(PotionType.FIRE_RESISTANCE);
    potion.apply(stack);
     
    Potion potion2 = Potion.fromItemStack(stack);
    potion2.getEffects().clear()
    //Add effects
    potion2.apply(stack);
    If not, then i'm not sure
     
  19. Offline

    TheAJ471

    Ross Gosling
    This should work.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player player = (Player) sender;
    3. if(commandLabel.equalsIgnoreCase("potion")){
    4. for (PotionEffect effect : player.getActivePotionEffects())
    5. player.removePotionEffect(effect.getType());
    6. player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 1000000, 1));
    7.  
     
  20. Offline

    Ross Gosling

    xTigerRebornx
    Still nothing, this is so screwed up, this should be working if this made any sense!

    TheAJ471
    That is changing the effects on the player and not those set on a potion...
     
  21. Offline

    xTigerRebornx

    Ross Gosling Try giving them a potion with just the effects cleared?
     
  22. Offline

    Ross Gosling

Thread Status:
Not open for further replies.

Share This Page