Give a player a custom potion

Discussion in 'Spigot Plugin Development' started by Hello1231, Sep 13, 2019.

  1. Hello, I am trying to make a splash potion of levitation, but it acts like a splash water bottle. Everything else about it works, just not the effect. Here is the code:
    Code (Text):
    ItemStack pot1 = new ItemStack(Material.SPLASH_POTION, 1);
                        PotionMeta meta = (PotionMeta) pot1.getItemMeta();
                        meta.setColor(Color.WHITE);
                        pot1.setItemMeta(meta);
                        meta.addCustomEffect(new PotionEffect(PotionEffectType.LEVITATION, 10, 0), true);
                        meta.setDisplayName(ChatColor.WHITE + "Potion Of Levitation");
                        List<String> potLore = new ArrayList<String>();
                        potLore.add(ChatColor.translateAlternateColorCodes('&', "&e10 Seconds Of Levitation"));
                        meta.setLore(potLore);
                        pot1.setItemMeta(meta);
                        player.getInventory().addItem(pot1);
    Any Ideas? Thanks.:)
     
  2. try to set the itemmeta only one time
     
    • Like Like x 1
  3. Changed it, still not working.:( Thanks anyway, don't know how I didn't see that.(y)
     
  4. Mmm potion meta works with Material.POTION, if you want to launch an splash_potion use another method, but, i don't know how this don't works,mmmm try to change the values of time and amplifier of potion
     
    • Useful Useful x 1
  5. Just make the item stack into the potion itself?

    Add the code of the potion as a short
    Code (Text):
    (short)16398;
     
    • Like Like x 1
  6. but if it want to change the velocity of the levitation?
     
    • Like Like x 1
  7. I may have found a workaround!
    I used this:
    Code (Text):

    ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
    String command = "minecraft:give " + player.getName() + " minecraft:splash_potion{CustomPotionEffects:[{Id:25,Duration:200}],CustomPotionColor:16777215,display:{Name:\"\\\"Potion Of Levitation\\\"\"}}";
    Bukkit.dispatchCommand(console, command);
     
    I got the command from https://minecraft.tools/en/potion.php
    It works.:):):)
     
  8. maybe depends of commands it's not the best way :/
     
    • Agree Agree x 2
  9. Try with this

    Code (Java):

                Player pl = event.getPlayer();
                ItemStack poti = new ItemStack(Material.SPLASH_POTION);
                PotionMeta meta = (PotionMeta)poti.getItemMeta();
                meta.setColor(Color.FUCHSIA);
                meta.addCustomEffect(new PotionEffect(PotionEffectType.POISON, this.val$to, this.val$to), true);
                poti.setItemMeta((ItemMeta)meta);
                ThrownPotion pot = (ThrownPotion)pl.getWorld().spawn(pl.getLocation().add(0,0,1), (Class)ThrownPotion.class);
                pot.setItem(poti);
    Or If you want add potion effect to an Player
    Code (Java):

    Player p = event.getPlayer();
    p.addPotionEffects(new PotionEffect(PotionEffectType.LEVITATION, 10, 10, true);
     
    • Like Like x 1
  10. Tried this.
    Code (Text):
     val$to
    cannot be resolved or is not a field, and it says "Type safety: The expression of type Class needs unchecked conversion to conform to Class" for
    Code (Text):
      <Entity> pl.getWorld().spawn(pl.getLocation().add(0,0,1), (Class)ThrownPotion.class);
    Was I supposed to replace those with something? Sorry if the problem is my limited knowledge of coding.:confused:
     
  11. The val$to is in place of the values needed for the constructors.

    You don't need to cast with (Class)ThrownPotion.class iirc, so it should work just fine as ThrownPotion.class for the parameter.
     
    • Useful Useful x 1
  12. it's the true, dont need to cast with (class) use only ThrownPotion.class
     
    • Like Like x 1
  13. New Code:
    Code (Text):
    ItemStack poti = new ItemStack(Material.SPLASH_POTION);
                        PotionMeta meta = (PotionMeta)poti.getItemMeta();
                        meta.setColor(Color.WHITE);
                        meta.addCustomEffect(new PotionEffect(PotionEffectType.LEVITATION, 10, 1), true);
                        poti.setItemMeta((ItemMeta)meta);
                        ThrownPotion pot = (ThrownPotion)player.getWorld().spawn(player.getLocation().add(0,0,1), ThrownPotion.class);
                        pot.setItem(poti);
                        player.getInventory().addItem(poti);
    It does not work correctly in these ways:
    1. The effect does not apply when splashed
    2. The potion is thrown below my feet when I recieve the item
    3. The splash doesn't completly line up with the potion position wise, but that's not a big deal.

    Once again it's probably just my coding knowledge, Sorry and thanks.:)
     
  14. so after staring at your code, and testing it, I came to the conclusion, your duration is too short.
    You have a duration of 10 ticks, so of course you're not going to levitate.
    I tried your same code, but I removed the entity part you have there, and changed the duration to 40 ticks (2 seconds) and it works perfectly.

    Code (Java):
    ItemStack item = new ItemStack(Material.SPLASH_POTION);
    PotionMeta meta = ((PotionMeta) item.getItemMeta());
    meta.setColor(Color.WHITE);
    meta.addCustomEffect(new PotionEffect(PotionEffectType.LEVITATION, 40, 2), true); //<-- 40 ticks whereas you had 10 ticks
    item.setItemMeta(meta);
    player.getInventory().addItem(item);
     
    • Winner Winner x 1
  15. This code worked perfectly!!!!!:):):):):):):):):):):):) Thanks, everyone!!!!!!!!!!!!!!!!!!!!!!!:alien:
     
    • Friendly Friendly x 1
  16. I'd rather say that the "amplifier" needs to be something other than 0. The OP had the amplifier as 0 so that (quite obviously) results in no actual effect.
     
  17. In my code test code, I had 2, but in game it showed up as 3.
    If I'm not mistaken amp 0 = level 1, amp 1 = level 2 and so on. I think this is because amplifier mean its amplifying something. Therefor an amplifier of 0 would not amplify the base effect, therefor it would be level 1
    I could be mistaken.
     
  18. If I amplify (i.e multiply) something by 0, I always get 0. If I've got a guitar amp and set it to 0, surely there will be no sound?
    But you could be right as well, I've not tested this specifically.
     
  19. In a guitars defence, if you set the amp to 0, you would still hear the sound of you strumming the strings, it would be almost inaudible, but you would still hear something, turning the amp up to 1 would amplify the sound to be heard via the speaker/amp.

    Potions have a base effect, and then you can amplify that effect to make it stronger so I think the base effect would be amp 0.

    Just read the McWiki, and it says this
    ""
    Amplifier: The amplifier of the effect, with level I having value 0. Negative levels are discussed here. Optional, and defaults to level I.
    ""
    So I guess it ain't just a Spigot thing, but also how vanilla MC works.
     
    • Winner Winner x 1
  20. Computers start counting at 0 so...