Potion effects in world guard regions

Discussion in 'Plugin Development' started by xXMaTTHDXx, Jun 1, 2013.

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

    xXMaTTHDXx

    I want to make a plugin so people can add custom potion effects when players walk into world gaurd regions, how do I do this.
     
  2. Offline

    GodzOfMadness

  3. Offline

    xXMaTTHDXx

    I dont understand, It says I cant use getRegion because it needs a protected region
     
  4. Offline

    GodzOfMadness

    xXMaTTHDXx
    Code:
    @EventHandler
    public void onRegionEnter(RegionEnterEvent e)
    {
      e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 60, 0));
    }
     
  5. Offline

    xXMaTTHDXx

    How can I make It so they can set the type of potion effect??
    Like to the region when they set it?
     
  6. Offline

    GodzOfMadness

    xXMaTTHDXx get the config and get the values to make the potion effect.
     
  7. Offline

    xXMaTTHDXx

    I dont quite understand sorry! could you show me what you mean or a example or something?
     
  8. Offline

    GodzOfMadness

    xXMaTTHDXx If you don't know how to use bukkit's configuration API then click here. It will show you how to setup everything. Then you basically set the defaults to what you need to input for the parameters of the PotionEffect constructor. It takes PotionEffectType, int, int and for PotionEffectType it has a method called getByName so basically in the config you would ask for a string, int, and int
     
  9. Offline

    xXMaTTHDXx

    oh ok I think I understand let me try it and see what happens. So you mean change the potion effect to a string and the type and time into a int and put it into a config?

    GodzOfMadness Ok Im doing a potion effect list, when the player walks into the region they can set the effect into the config
    Code:
    @EventHandler
        public void onRegionEnter(RegionEnterEvent e)
        {
            Player player = e.getPlayer();
            for (Object o : getConfig().getStringList("potionlist"))
            {
                String type = (String) o;
                player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(type), 200, 1), true);
                }
        }
    }
    I hope this will work, I don't know what to do to the config though.

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

    GodzOfMadness

    xXMaTTHDXx It will work, but if you have an incorrect PotionEffectType name then it will throw an exception I believe
     
  11. Offline

    xXMaTTHDXx

    yeah, will this code make it so they can define different potion types in different regions?
     
  12. Offline

    GodzOfMadness

    xXMaTTHDXx You can add another parameter so like
    Code:
    regions:
      region1:
      region: test
      potion: REGENERATION
    and read it like that, or yo can remove the region: tag and just use it like the name already(where region1: is)
     
  13. Offline

    xXMaTTHDXx

    How do I do it so when they define the region it does into the config and shows up like that? so like all regions are saved into the config like that, so you can go in and choose the effect
     
  14. Offline

    GodzOfMadness

    xXMaTTHDXx
    loop through it like
    for(String region : config.getConfigurationSection("regions").getKeys(false)){
    if(region.equals(regionTheirEntering)){
    add the potion effect from the path config.get("regions." + region + ".<valueToGet>");
    }
    }
     
  15. Offline

    xXMaTTHDXx

    But How will it save the regions to the config?

    Code:
    package me.xXMaTTHDXx.PotionZones;
     
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    import com.mewin.WGRegionEvents.events.RegionEnterEvent;
     
    public class Zones extends JavaPlugin implements Listener{
        public static Zones plugin;
        Logger logger = Logger.getLogger("xXMaTTHDXx");
     
     
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now disabled");
     
        }
     
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
     
     
        @EventHandler
        public void onRegionEnter(RegionEnterEvent e)
        {
            Player player = e.getPlayer();
            for(String region : getConfig().getConfigurationSection("regions").getKeys(false)){
                if(region.equals("regions")){
                getConfig().get("regions." + region + ".<valueToGet>");
             
        }
    }
    }
    }
    
    This is what I have so far, do you think it will work?

    [EDIT]: Where it says valueToGet I have ".potions"

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

    GodzOfMadness

    xXMaTTHDXx You can either add them manually or set them like make a command
    so say the command is /reg
    /reg takes two arguments, the region name and the potion effect name
    so /reg region REGENERATION
    take those values and set it like so
    config.set("regions." + region + ".potion", args[1]);
    and that sets it as the format i put above.
     
  17. Offline

    xXMaTTHDXx

    OMG thank you that clears It up for me!
    [EDIT]: I dont understand how this works into world guard but ok haha
     
  18. Offline

    GodzOfMadness

    xXMaTTHDXx you need to replace <valueToGet> with the values for adding the potion effect. Also, your if statement is saying, if I am entering the region "regions" then give that potion effect!
     
  19. Offline

    xXMaTTHDXx

    Hm I dont know if I understand I am trying to understand what you mean, but I am kind of on the same page
     
  20. Offline

    GodzOfMadness

    xXMaTTHDXx Basically do what you did before but replace that line getConfig().get(..) with
    player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(getConfig().getString("regions." + region + ".potion"), 200, 1), true);
     
  21. Offline

    xXMaTTHDXx

    Ok, add make a command to add the region right?
     
  22. Offline

    GodzOfMadness

    xXMaTTHDXx Yea, like how I explained above for how the command would work.
     
  23. Offline

    xXMaTTHDXx

    Code:
    package me.xXMaTTHDXx.PotionZones;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    import com.mewin.WGRegionEvents.events.RegionEnterEvent;
     
    public class Zones extends JavaPlugin implements Listener{
        public static Zones plugin;
        Logger logger = Logger.getLogger("xXMaTTHDXx");
     
     
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now disabled");
     
        }
     
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
            Player player = (Player) sender;
            String region = args[0];
            if(player.hasPermission("zone.set")){
                if(cmd.getName().equalsIgnoreCase("reg")){
                    player.sendMessage("Usage /reg <RegionName> <Effect>");
                } else if(args.length == 1) {
                    getConfig().set("regions." + region + ".potion", args[1]);
     
                }
            }
     
            return false;
     
     
        }
        @EventHandler
        public void onRegionEnter(RegionEnterEvent e)
        {
            Player player = e.getPlayer();
            for (Object o : getConfig().getStringList("potionlist"))
            {
                String type = (String) o;
                player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(type), 200, 1), true);
                }
        }
    }
    
    GodzOfMadness like this?
     
  24. Offline

    GodzOfMadness

    xXMaTTHDXx Your putting an else if statement on if the command is "reg" so after the if statement checking if the command is reg check and see if args.length == 0 and then attach the else if statement for args.length == 1 at the end curly brace for the if statement checking if args.length == 0
    Also change back your regen method because that was using the new function your making with the command,
    for(String region : getConfig().getConfigurationSection("regions").getKeys(false)){
    if(region.equals("<input region their entering here>")){
    player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(getConfig().getString("regions." + region + ".potion"), 200, 1), true);

    }
     
  25. Offline

    xXMaTTHDXx

    whoa GodzOfMadness um I dont know what you mean can you show me a example I didnt follow your words
     
  26. Offline

    GodzOfMadness

    xXMaTTHDXx
    Replace
    Code:
            for (Object o : getConfig().getStringList("potionlist"))
            {
                String type = (String) o;
                player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(type), 200, 1), true);
                }
    with
    Code:
    for(String region : getConfig().getConfigurationSection("regions").getKeys(false)){
    if(region.equals("<input region their entering here>")){
    player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(getConfig().getString("regions." + region + ".potion"), 200, 1), true);
     
    }
    
    then create another if statement after the if statement checking if it's the command being executed
    the if statement should be like if(args.length == 0){
    now add the else if(args.length == 1) at the end of the curly brace of if(args.length == 0
     
  27. Offline

    xXMaTTHDXx

    Code:
    package me.xXMaTTHDXx.PotionZones;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    import com.mewin.WGRegionEvents.events.RegionEnterEvent;
     
    public class Zones extends JavaPlugin implements Listener{
        public static Zones plugin;
        Logger logger = Logger.getLogger("xXMaTTHDXx");
     
     
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now disabled");
     
        }
     
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() +"version" + pdfFile.getVersion() + "is now enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
            Player player = (Player) sender;
            String region = args[0];
            if(player.hasPermission("zone.set")){
                if(cmd.getName().equalsIgnoreCase("reg")){
                    player.sendMessage("Usage /reg <RegionName> <Effect>");
                    if(args.length == 0) {
                    }else if(args.length == 1){
                        getConfig().set("regions." + region + ".potion", args[1]);
                    }
                }
            }
     
     
     
            return false;
     
     
        }
        @EventHandler
        public void onRegionEnter(RegionEnterEvent e)
        {
            Player player = e.getPlayer();
            for(String region : getConfig().getConfigurationSection("regions").getKeys(false)){
                if(region.equals("<input region their entering here>")){
                    player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(getConfig().getString("regions." + region + ".potion")), 200, 1, true));
     
                }
            }
        }
    
    GodzOfMadness
     
  28. Offline

    GodzOfMadness

    xXMaTTHDXx Just replace <input region their entering here> with the region their entering or a certain region. Then just move the player.sendMessage method inside the if statement checking if args.length == 0 and you should be set.
     
  29. Offline

    xXMaTTHDXx

  30. Offline

    GodzOfMadness

    xXMaTTHDXx try looking through here to get an idea on how to get what region the player is in
     
Thread Status:
Not open for further replies.

Share This Page