Enabling a plugin in a certain world using the config.yml

Discussion in 'Plugin Development' started by Techno, Jun 12, 2014.

Thread Status:
Not open for further replies.
  1. As the title says I have a config.yml setup and I'm trying to make it so that the plugin only works with the world stated in the config..

    Main.java:
    Code:
    package me.Bill4788.DoubleJumpa;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    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.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerToggleFlightEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
     
    getConfig().options().copyDefaults(true);
    saveConfig();
     
    }
     
    // Commands
    public boolean onCommand(CommandSender sender, Command command) {
     
    Player p = (Player) sender;
     
    if (command.getName().equalsIgnoreCase("uphigh")) {
    p.setAllowFlight(true);
    p.sendMessage(ChatColor.BLUE + "Fly little bird!");
    }
     
    return true;
    }
     
    // Events
     
    @EventHandler
    public void onPlayerToggleFlight(PlayerToggleFlightEvent e) {
    Player pe = e.getPlayer();
     
    if (pe.getGameMode() == GameMode.CREATIVE)
    return;
     
    e.setCancelled(true);
    pe.setAllowFlight(false);
    pe.setFlying(false);
    pe.setVelocity(pe.getLocation().getDirection().multiply(1.5).setY(1));
    }
     
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent move) {
    Player p = move.getPlayer();
     
    if ((p.getGameMode() != GameMode.CREATIVE) && (p.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) 
      && (!p.isFlying()))
    p.setAllowFlight(true);
    }
    }
    
    config.yml:
    Code:
    # Config
    worldname:
    
     
  2. Create a if statement with everything that you want to trigger if the player is in the right world in it, and check if the name of the world the player is in is equal to the one you have in your config. And if it's a list you have in your config, you just check of the name of the players world is in the list.
     
  3. Well, the triggers are events, it's a double jump plugin..
     
  4. Offline

    MCForger

    Techno
    Before you do anything with an event, check the world of the player. If the world is not in list the of allowed worlds then simply call return so you do not execute any of your code.
     
  5. MCForger that was what I was trying to say, but I couldn't get the words :(
     
  6. Like:
    Code:
    if (!e.getPlayer().getWorld().equals(getConfig().getString("worldname")))
    return;
    
    bump

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

    1Rogue

    If you use something like a command handler which will handle all commands before delegating it, yes; You would do something along those lines.
     
  8. I don't really know how to do that.. Haha, is there a way to do it easier?
     
  9. Offline

    1Rogue


    Well if you want to be messy, then you would have to do the check before following through with the execution of any commands.
     
  10. But see, there events, not commands..
     
  11. Offline

    1Rogue

    Same concept then
     
  12. Could you give me an example?
     
  13. Offline

    1Rogue

     
  14. Okay, so:
    Code:
    @EventHandler
    public void onPlayerToggleFlight(PlayerToggleFlightEvent e) {
    if (!e.getPlayer().getWorld().equals(getConfig().getString("worldname")))
    return;
     
    Player pe = e.getPlayer();
     
    if (pe.getGameMode() == GameMode.CREATIVE)
    return;
     
    e.setCancelled(true);
    pe.setAllowFlight(false);
    pe.setFlying(false);
    pe.setVelocity(pe.getLocation().getDirection().multiply(1.5).setY(1));
     
    }
     
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent move) {
    if (!move.getPlayer().getWorld().equals(getConfig().getString("worldname")))
    return;
     
    Player p = move.getPlayer();
     
    if ((p.getGameMode() != GameMode.CREATIVE) && (p.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) 
      && (!p.isFlying()))
    p.setAllowFlight(true);
    }
    
     
  15. Offline

    1Rogue

    Essentially, yes. You can optimize it how you like (caching the world name, using a list instead of a single string value, etc), but that's the messy gist of it.
     
  16. That doesn't work unfortunately..
     
  17. Offline

    Necrodoom

    Doesnt work how? You get an error? If doesnt trigger? Did you debug the variables?
     
  18. Doesn't trigger..
     
  19. Offline

    Necrodoom

    Did you debug the variables to see why?
     
  20. Umm, how do I do that? (Sorry for nooby questions)
     
  21. Offline

    Necrodoom

    Print the variables involved to console.
     
  22. Another nooby question, how do I do that?
     
  23. Offline

    Necrodoom

  24. Offline

    xdags

    Code:
    e.getPlayer().getWorld().equals(getConfig().getString("worldname"))
    You're comparing the World object to a String (which it isn't), so it'll return false every time.
    You want to be using 'getWorld().getName()' to compare the name of the world against the value in your config.
     
  25. I've changed the events to
    Code:
     if (e.getPlayer().getWorld().getName() == getConfig().getString("worldname"))
    return; 
    But then in the config.yml when I change 'world' to something else, it still works, which its not... So.. worldname: world (works) and worldname: somethingelse (does too, even though there is no world called 'somethingelse')
     
  26. Offline

    xdags

    Ah sorry, you do still need the '!' at the start of that if statement.
    I didn't include it in the snippet that I took to avoid confusing matters with double negatives.
     
  27. Offline

    1Rogue

    Don't use "==" to compare strings, use .equals()
     
  28. I changed it to:
    Code:
    package me.Bill4788.DoubleJumpa;
     
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerToggleFlightEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin implements Listener {
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
     
    getConfig().options().copyDefaults(true);
    getConfig().addDefault("worldname", null);
    saveConfig();
     
    }
     
    // Events
     
    @EventHandler
    public void onPlayerToggleFlight(PlayerToggleFlightEvent e) {
    if (!(e.getPlayer().getWorld().getName().equals(getConfig().getString("worldname"))))
    return;
     
    Player pe = e.getPlayer();
     
    if (pe.getGameMode() == GameMode.CREATIVE)
    return;
     
    e.setCancelled(true);
    pe.setAllowFlight(false);
    pe.setFlying(false);
    pe.setVelocity(pe.getLocation().getDirection().multiply(1.5).setY(1));
    }
     
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent move) {
    if (!(move.getPlayer().getWorld().getName().equals(getConfig().getString("worldname"))))
    return;
     
    Player p = move.getPlayer();
     
    if ((p.getGameMode() != GameMode.CREATIVE) && (p.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) 
      && (!p.isFlying()))
    p.setAllowFlight(true);
    }
    }
    
    The error: When worldname in the config is 'world' (my world on the server) it works, when i change it to something else like 'abcd' it lets the player fly..
     
Thread Status:
Not open for further replies.

Share This Page