How do I make it so you can edit which world in the config?

Discussion in 'Plugin Development' started by DulexZach, Sep 3, 2013.

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

    DulexZach

    Okay, so I have recently been working on an update for my plugin, Hub. And I am wondering, how do I make it so you can set the hub in a different world?
    Example: Hub, only saves the coords to the world, "world". So when you do /Hub, no matter what world you're in, it will take you to, "world". But I want to make it so that when you do /Hub, and you have your world in the configuration file set to something like "hub", it will take you to the coordinates in the world "hub", and won't take you back to world.

    Code:

    Code:java
    1. package me.dulexzach.spawnleave;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.plugin.PluginDescriptionFile;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class spawnleave extends JavaPlugin implements Listener{
    19.  
    20. public final Logger logger = Logger.getLogger("Minecraft");
    21.  
    22. @Override
    23. public void onDisable(){
    24.  
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info("[" + pdfFile.getName() + "] "+ " Has been DISABLED!");
    27.  
    28. }
    29.  
    30. @Override
    31. public void onEnable() {
    32.  
    33. getConfig().options().copyDefaults(true);
    34. saveConfig();
    35.  
    36. getServer().getPluginManager().registerEvents(this,this);
    37. PluginDescriptionFile pdfFile = this.getDescription();
    38.  
    39. this.logger.info("[" + pdfFile.getName() + "] "+ " Has been ENABLED!");
    40.  
    41. }
    42.  
    43.  
    44. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    45. Player player = (Player) sender;
    46.  
    47. if(label.equalsIgnoreCase("hreload")){
    48. if(player.hasPermission("hub.reload")) {
    49. reloadConfig();
    50.  
    51. sender.sendMessage(ChatColor.GREEN + "Successfully Reloaded!");
    52. }
    53. }
    54.  
    55. if(label.equalsIgnoreCase("hub")){
    56. if(player.hasPermission("hub.hub")) {
    57.  
    58. player.teleport(new Location(Bukkit.getWorld("world"), this.getConfig().getInt("X"), this.getConfig().getInt("Y"), this.getConfig().getInt("Z")));
    59.  
    60. }
    61. }
    62.  
    63. if(label.equalsIgnoreCase("sethub")){
    64. if(player.hasPermission("hub.set")) {
    65.  
    66. double value = player.getLocation().getX();
    67. double value2 = player.getLocation().getY();
    68. double value3 = player.getLocation().getZ();
    69. getConfig().set("X", value);
    70. getConfig().set("Y", value2);
    71. getConfig().set("Z", value3);
    72. saveConfig();
    73.  
    74. sender.sendMessage(ChatColor.GREEN + "Hub Successfully Set!");
    75. }
    76. }
    77. return false;
    78. }
    79.  
    80.  
    81.  
    82. @EventHandler
    83. public void onPlayerQuit(PlayerQuitEvent e){
    84. Player player = e.getPlayer();
    85. if(!player.hasPermission("hub.bypass")) {
    86. player.teleport(new Location(Bukkit.getWorld("world"), this.getConfig().getInt("X"), this.getConfig().getInt("Y"), this.getConfig().getInt("Z")));
    87. }
    88. }
    89.  
    90. }


    I want my configuration file to have something where it says

    world: [world]

    and you're actually able to enter the world to where you'll teleport to.

    Thanks
     
  2. Offline

    Avery246813579

    Use this:
    Code:
    public void getHub(Player player){
        World world = player.getWorld();
        String worldName = world.getName();
        getConfig().Set(world, worldName)
    }
     
    public World getWorld(){
    String world getConfig().getString("world");
    this.getServer().getWorld(world);
     
    if(world != null){
    return world
    }
    }
     
  3. Offline

    DulexZach


    Doesn't work, I get errors on world on the line
    "String world getConfig().getString("world");"
    "return world;"
    "getConfig().set(world, worldName);"

    Still can't find anything. Please help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. I think this is what you want
    Code:java
    1.  
    2. public void saveWorld(World world) {
    3. this.getConfig().set("world", world.getName());
    4. }
    5.  
    6. public World loadWorld() {
    7. String w = this.getConfig().get("world");
    8. World world = this.getServer().getWorld(w);
    9. return world;
    10. }
    11.  

    You should check if world is null when you load it though (Means that the world doesn't exist)
     
  5. Offline

    DulexZach


    Doesn't give me any errors (except the .get after getConfig, but I changed it to getString which fixed the problem), but now when I put world into the getWorld() it gives me an error, help?
    Code:java
    1. player.teleport(new Location(Bukkit.getWorld(), this.getConfig().getInt("X"), this.getConfig().getInt("Y"), this.getConfig().getInt("Z")));
     
  6. Offline

    Avery246813579

    I forgot to tell you that I wrote it on my phone so there might be errors XD
     
  7. Offline

    DulexZach

    Oh wow, this was actually really easy to do... I just came up with my own code which I don't feel like copying and pasting ATM
     
  8. Offline

    CeramicTitan

    DulexZach

    Code:java
    1. player.teleport(new Location(loadWorld(), this.getConfig().getInt(loadWorld().getName()+".X"), this.getConfig().getInt(loadWorld().getName()+".Y"), this.getConfig().getInt(loadWorld().getName()+".Z")));



    Set your config out like this:

    Code:
    world-name
        X: x-coordinate
        Y: y-coordinate
        Z: z-coordinate
        Pitch: (Optional)
        Yaw: (Optional)
     
Thread Status:
Not open for further replies.

Share This Page