Solved Setting potion effects from config list

Discussion in 'Plugin Development' started by Brixishuge, Nov 29, 2013.

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

    Brixishuge

    Is there any way, to set PotionEffect from list to an player at same time? I know how to set exact one, but how to set list of them?

    Code example please!

    Thanks in advance! :D
     
  2. Brixishuge
    Code:java
    1. for (String s: getConfig().getStringList("effects")) {
    2. PotionEffectType effect = null;
    3.  
    4. try {
    5. effect = PotionEffectType.getByName(s);
    6. } catch (Exception ex) {
    7. ex.printStackTrace();
    8. break;
    9. }
    10.  
    11. player.addPotionEffect(new PotionEffect(effect, duration, amplifier));
    12. }
    And your config would look something like this (example):
    Code:
    effects:
    - INVISIBILITY
    - JUMP
    - ETC
    You can modify the code to add the duration and amplifier in the config if you want.
     
  3. Offline

    felixfritz

    If you have a string list, you could use a for-loop for that.
    Code:java
    1. for(String effectString : config.getStringList("your.string.list") {
    2. PotionEffectType type = PotionEffectType.getByName(effectString.toUpperCase());
    3.  
    4. //if type is null that means that the type could not be interpreted
    5. if(type == null) {
    6. System.err.println(effectString + " could not be interpreted into a correct PotionEffectType.");
    7. continue;
    8. }
    9.  
    10. //if the player already has the potion effect, make sure to remove it at first
    11. if(player.hasPotionEffect(type))
    12. player.removePotionEffect(type);
    13.  
    14. //create the effect
    15. PotionEffect effect = type.createEffect(20 * 30 /* duration in ticks */, 1 /* strength */);
    16.  
    17. //apply effect to the player
    18. player.addPotionEffect(effect);
    19. }

    This version is not entierly bullet proof: if you want to include duration and strength as a variable that can be changed in the config file, you'll have to find a system to maybe split it using a "," or something.

    Also, if the player for instance started out with speed III and you want to add the speed effect of the same type, only with a lower strength, it would remove that effect no matter what. You'd have to check in the second if-statement then, if the strength of the potion effect type is stronger than 1 or something, if you want to include that.
     
  4. Offline

    Brixishuge

    felixfritz I'm doing it on similiar way, but will this:

    player.addPotionEffect(effect)
    add whole list of effects?

    Because I already have other part of code, which works with one effect name, is it same with list of effects? I just add list?
     
  5. Offline

    felixfritz

    If you have the list, you can add the effects one by one in a for-loop, just as I showed you.
     
  6. Offline

    Brixishuge

    felixfritz It works perfectly, thanks man. Btw. do you have any idea on how to get duration and amplifier for every effect? :/
     
  7. Offline

    felixfritz

    You could maybe design your configuration file somewhat like this:
    Code:
    effects:
      - JUMP,30,2
    So the first number is the amount of time and the second one is the strength. Then you could split the string and interpret everything. This is, what you could put in your for-loop.
    Code:java
    1. //make sure there are no whitespaces in the "effectString"
    2. String[] values = effectString.replaceAll(" ").split(",");
    3.  
    4. //create PotionEffectType
    5. PotionEffectType type;
    6. //set default duration to 30 seconds
    7. int duration = 30;
    8. //set default strength to 1
    9. int strength = 1;
    10.  
    11. //get type
    12. type = PotionEffectType.getByName(values[0].toUpperCase());
    13.  
    14. //check, if type is null
    15.  
    16. //set duration
    17. if(values.length > 1)
    18. duration = Integer.parseInt(values[1]);
    19.  
    20. //set strength
    21. if(values.length > 2)
    22. strength = Integer.parseInt(values[2]);
    23.  
    24. //check, if player already has potiontype
    25.  
    26. //create the effect
    27. PotionEffect effect = type.createEffect(duration * 20, strength);
    28. player.addPotionEffect(effect);
     
    BDKing88 and Brixishuge like this.
  8. Offline

    Brixishuge

    felixfritz I've done it in similiar way, anyway, thanks!
     
Thread Status:
Not open for further replies.

Share This Page