Configuration File problem

Discussion in 'Spigot Plugin Development' started by Mariozgr8, Apr 21, 2017.

  1. Hi, so I am trying to create a plugin that will set a location called spawn in the world. And you can set this location spawn to your current location by typing /setspawn. But when I type /spawn in the server after typing
    /setspawn, I get Internal server error.
    This is my code:
    Code (Text):
    name: SpawnTest
    main: me.mariozgr8.spawntest.MainClass
    version: 1.0
    commands:
      spawn:
        usage: /spawn (player)
        description: Teleport the player to the spawn location of the world.
      setspawn:
        usage: /setspawn
        description: Set the spawn location of the world
    Code (Text):
    package me.mariozgr8.spawntest;

    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;

    public class MainClass extends JavaPlugin {
       
        @Override
        public void onEnable(){
            getLogger().info("SpawnTest has been enabled!");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        @Override
        public void onDisable(){
            getLogger().info("SpawnTest has been disabled!");
            saveConfig();
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED+"This command can only be performed by a player!");
                return true;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("setspawn"))
            {
                if(args.length >= 1)
                {
                    p.sendMessage(ChatColor.AQUA+"Not enought arguments!");
                    return true;
                }
                getConfig().createSection("spawn");
                getConfig().set("spawn.world", p.getLocation().getWorld().toString());
                getConfig().set("spawn.x", p.getLocation().getX());
                getConfig().set("spawn.y", p.getLocation().getY());
                getConfig().set("spawn.z", p.getLocation().getZ());
                saveConfig();
                p.sendMessage(ChatColor.AQUA+"Spawn set to your location!");
                return true;
               
            }
            if(cmd.getName().equalsIgnoreCase("spawn"))
            {
                double x = getConfig().getConfigurationSection("spawn").getDouble("spawn.x");
                double y = getConfig().getConfigurationSection("spawn").getDouble("spawn.y");
                double z = getConfig().getConfigurationSection("spawn").getDouble("spawn.z");
                World w = Bukkit.getServer().getWorld(getConfig().getConfigurationSection("spawn").getString("spawn.world"));
                if(args.length == 0)
                {
                    p.teleport(new Location(w, x, y, z));
                    p.sendMessage(ChatColor.AQUA+"Teleporting to spawn....");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if( target ==  null )
                {
                    p.sendMessage(ChatColor.AQUA+"Cannot find "+args[0]);
                    return true;
                }
                target.teleport(new Location(w, x, y, z));
                target.sendMessage(ChatColor.AQUA+"Teleported to spawn by "+p.getName());
                p.sendMessage(args[0]+" teleported to spawn!");
                return true;
            }
            return false;
        }
    }
     
    Can anyone please tell me what is wrong with my code ? Thanks for helping :)
     
  2. Code (Text):
    World w = Bukkit.getServer().getWorld(getConfig().getConfigurationSection("spawn").getString("spawn.world"));
    This might be your problem, your are getting the configurationSection spawn and the you do it again with spawn.world
    Also, try to load the position in the onEnable plugin and save it in a HashMap. It's much quicker than reading everytime a flatfile
     
  3. Use JSON to store data such as this. Faster for the computer to read and process.