Solved Multiworld support with config

Discussion in 'Plugin Development' started by Hex_27, Sep 30, 2014.

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

    Hex_27

    I'm trying to make a plugin in which when a player sleeps in a bed, he will get regen. I'm done with that part of the code, and I've added permissions, but right now, I want to add a config that will allow the server owner to configure which worlds he wants the plugin to work in. How do I do this?
    code(With a half-try on world getting):
    Show Spoiler

    package me.leothepro.bedheal;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    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.event.player.PlayerBedEnterEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Main extends JavaPlugin implements Listener{

    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
    private Main main;

    public Main(Main instance) {
    main = instance;
    }

    //Note there is no need to make a nested class
    //If you go back and look at what you had, you can work out what we mean.
    @EventHandler
    public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
    Player p = event.getPlayer();
    World world = p.getWorld();
    String worldname = world.getName().toLowerCase().trim();
    if (worldname.equalsIgnoreCase("nether")){
    if (p.hasPermission("bedheal.use"))
    {
    //regen Potion Effect (20 ticks = 1 second)
    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 1));
    } else {
    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1, 1));
    }
    }
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player) {
    Player player = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("bedheal reload")){
    if(player.hasPermission("bedheal.reload")){
    main.reloadConfig();
    player.sendMessage(ChatColor.BOLD + "bedheal Reloaded");
    }else{
    player.sendMessage(ChatColor.RED + "You don't have the permission!");
    }
    }

    }

    return false;
    }
    }
     
  2. Hex_27
    i would let the owner store the world names where it has to be enabled in a list, i am not quiet sure how to do that but you should find an answer with Google.
    then when just check if the world where the Player is sleeping is in the list
     
  3. Offline

    Nateb1121

    I'd do something like this:

    • PlayerEnterBedEvent
    • Check to see if world is in the config
    • Do the logic to do whatever it is you want
    So, just change your existing code to check whether the name of the world is in the config instead of checking if the name is 'nether'. Should be pretty simple providing you know Java and aren't expecting me to just spoon feed you code.


    Side note,
    Code:
    if(cmd.getName().equalsIgnoreCase("bedheal reload")){
    I'm pretty sure that won't work how you're thinking it's going to work. Commands are passed in
    /<Command> <arg> <arg> ... form. You should be checking your args for the word 'reload'.

    Happy coding.
     
  4. Offline

    Hex_27

    I've successfully gotten the world where the player is sleeping, but I don't know how to get a world from a config
     
  5. Offline

    fireblast709

  6. Offline

    Hex_27

    I would be trying it right now... but there's people in my server so I dw to kick them... ._.

    I just tested this code:
    Show Spoiler

    package me.leothepro.bedheal;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.event.player.PlayerBedEnterEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Main extends JavaPlugin implements Listener{

    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
    private Main main;

    public Main(Main instance) {
    main = instance;
    }
    @EventHandler
    public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
    Player p = event.getPlayer();
    String world = p.getLocation().getWorld().getName();
    String healedmsg = getConfig().getString("healed-message");
    if(getConfig().getStringList("enabled-worlds").contains(world)){
    if (p.hasPermission("bedheal.use"))
    {
    //regen Potion Effect (20 ticks = 1 second)
    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 1));
    p.sendMessage(ChatColor.BLUE + healedmsg);
    } else {
    }
    }
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player) {
    Player player = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("bhreload")||cmd.getName().equalsIgnoreCase("bedhealreload")){
    if(player.hasPermission("bedheal.reload")){
    main.reloadConfig();
    player.sendMessage(ChatColor.BOLD + "bedheal Reloaded");
    }else{
    player.sendMessage(ChatColor.RED + "You don't have the permission!");
    }
    }else if(cmd.getName().equalsIgnoreCase("bh")||cmd.getName().equalsIgnoreCase("bedheal")){
    player.sendMessage(ChatColor.BOLD + "Commands:");
    player.sendMessage(ChatColor.BOLD + "bhreload");
    }

    }

    return false;
    }
    }

    My console says that the plugin is an abnormal type.

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

    fireblast709

    Hex_27 your Main class should only have the no arg constructor (if any at all). Also, remove the private Main main, this field is the same as 'this'.
     
  8. Offline

    Hex_27

    Thanks guys. I finally got the code right. It works now(The reloading command doesn't, but meh next time...). Here's my code:
    Show Spoiler

    package me.leothepro.bedheal;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.event.player.PlayerBedEnterEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Main extends JavaPlugin implements Listener{

    public void onEnable(){
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    getConfig().options().copyDefaults(true);
    saveConfig();
    }

    @EventHandler
    public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
    Player p = event.getPlayer();
    String world = p.getLocation().getWorld().getName();
    String healedmsg = getConfig().getString("healed-message");
    if(getConfig().getStringList("enabled-worlds").contains(world)){
    if (p.hasPermission("bedheal.use"))
    {
    //regen Potion Effect (20 ticks = 1 second)
    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 1));
    p.sendMessage(ChatColor.BLUE + healedmsg);
    } else {
    }
    }
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player) {
    Player player = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("bhreload")||cmd.getName().equalsIgnoreCase("bedhealreload")){
    if(player.hasPermission("bedheal.reload")){
    this.reloadConfig();
    player.sendMessage(ChatColor.BOLD + "bedheal Reloaded");
    }else{
    player.sendMessage(ChatColor.RED + "You don't have the permission!");
    }
    }else if(cmd.getName().equalsIgnoreCase("bh")||cmd.getName().equalsIgnoreCase("bedheal")){
    player.sendMessage(ChatColor.BOLD + "Commands:");
    player.sendMessage(ChatColor.BOLD + "bhreload");
    }

    }

    return false;
    }
    }

    I'll add you all to my credits list for my plugin! Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page