Is it possible?

Discussion in 'Plugin Development' started by xXShadowGuy3Xx, Jun 30, 2013.

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

    xXShadowGuy3Xx

    Is it possible to substitute a potion effect type for a variable?


    somthing like
    Show Spoiler
    Code:
    player.addPotionEffect(new PotionEffect(Variable1, 18000, 2));



    Then Variable1 would be in the config and set by a server owner, So if the owner put in the config Jumpboost, then the player would get Jumpboost upon typing that command, or if the owner put Slow then the player would get Slowness when the command is typed
     
  2. Offline

    MP5K

    Hello xXShadowGuy3Xx,
    yes it is:
    Code:java
    1. new PotionEffect(PotionEffectType.getByName("POTION NAME HERE"), 0, 0);

    but make sure that the EffectType is not null.
     
  3. Offline

    xXShadowGuy3Xx

    Thanks for the help, trying it out now!

    MP5K
    How can i make sure the effect type is not null?


    Here is what i have in my command executer:
    Show Spoiler

    Code:java
    1. package com.gmail.yyamiyyugi;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.potion.PotionEffect;
    8. import org.bukkit.potion.PotionEffectType;
    9.  
    10. public class SlashPotionCommandExecuter implements CommandExecutor {
    11.  
    12. private Main plugin;
    13.  
    14. public SlashPotionCommandExecuter(Main plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label,
    20. String[] args) {
    21. String C1 = plugin.getConfig().getString("Command1");
    22.  
    23. String B1 = plugin.getConfig().getString("Bonus1");
    24.  
    25. String D1 = plugin.getConfig().getString("Downside1");
    26.  
    27. int B1T = plugin.getConfig().getInt("Bonus1Ticks");
    28. int D1T = plugin.getConfig().getInt("Downside1Ticks");
    29.  
    30. PotionEffect Bonus1 = new PotionEffect(PotionEffectType.getByName(B1),
    31. B1T, 1);
    32. PotionEffect Downside1 = new PotionEffect(
    33. PotionEffectType.getByName(D1), D1T, 1);
    34.  
    35. // CommandSender player = sender;
    36. Player player = (Player) sender;
    37.  
    38. if (cmd.getName().equalsIgnoreCase("SP")) {
    39. if (args.length == 0) {
    40. sender.sendMessage("Missing args!");
    41. }
    42. if (args.length == 1) {
    43. if (args[0].equalsIgnoreCase(C1)) {
    44. player.addPotionEffect(Bonus1);
    45. player.addPotionEffect(Downside1);
    46. }
    47.  
    48. }
    49.  
    50. }
    51.  
    52. return false;
    53. }
    54.  
    55. }



    What do I need to add for it not to be null without messing up the ability to config the potion effects?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  4. Offline

    MP5K

    what if "D1" is something like: "Minecraft" or "Lol" is there a potion effect called "Lol"?
     
  5. Offline

    xXShadowGuy3Xx

    MP5K
    So I need to add an if statement checking what D1 is?
     
  6. Offline

    themuteoneS

    Not exactly. Just assuming that if you put something not valid it returned null though this can be modified if it returned an exception:

    Code:java
    1. PotionEffect e = null;
    2. e = PotionEffectType.getByName("POTION NAME HERE");
    3. if(e==null) {
    4. //Deal with the error
    5. }

    Or if it throws an exception on error you could try:
    Code:java
    1. PotionEffect e = null;
    2. try {
    3. e = PotionEffectType.getByName("POTION NAME HERE");
    4. }
    5. catch(Exception e) {
    6. //Deal with the error
    7. }

    I'm actually not sure how that function in particular deals with an invalid argument. It may be either of the two solution above. I recommend checking in the online javadocs to see what is says upon invaid potion name.

    EDIT: I just checked and it says that it returnes null if type is not found. http://jd.bukkit.org/rb/apidocs/org/bukkit/potion/PotionEffectType.html#getByName(java.lang.String)
     
  7. Offline

    xXShadowGuy3Xx

    Thank you so much for the help! I was able to work everything out!
     
Thread Status:
Not open for further replies.

Share This Page