Search code examples
javapluginsminecraftbukkit

Simple world management script SPIGOT-API


I'm trying to learn code spigot plugins and i wanted to make simple world management plugin. First i want to say i'm very beginner at java.

Here is my code:

package world.paahdinMC.fi;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;

public class WorldCmd extends JavaPlugin implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        switch (label.toLowerCase()) {
            case "world":
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    if (Bukkit.getWorld(args[0]) != null) {
                        Location loc = new Location(Bukkit.getWorld(args[0]), 0, 255, 0);
                        p.teleport(loc);    
                    }
                    else {
                        p.sendMessage("World does not exist!");
                    }
                }
            case "cworld":
                if (Bukkit.getWorld(args[0]) == null) {
                    createWorld(args);
                }
                else {
                    sender.sendMessage("This name is alredy in use!");  
                    }
            case "rmworld":
                World delete = Bukkit.getWorld(args[0]);
                File deleteFolder = delete.getWorldFolder();
                deleteWorld(deleteFolder, sender);
                
            default:
                
        }
        return false;
    }
    public void createWorld(String[] args) {
        WorldCreator wc = new WorldCreator(args[0]);

        wc.environment(Environment.NORMAL);
        wc.type(WorldType.NORMAL);

        wc.createWorld();
    }
    public boolean deleteWorld(File path, CommandSender sender) {
        if(path.exists()) {
            File files[] = path.listFiles();
            for(int i=0; i<files.length; i++) {
                if(files[i].isDirectory()) {
                    deleteWorld(files[i], null);
                } else {
                    files[i].delete();
                }
            }
            sender.sendMessage("Finished!");
        }
        else {
            sender.sendMessage("World does not exist!");
        }
        return(path.delete());
    }
}

And here is my errors what do i get:

/cworld toimisko

[16:26:06 INFO]: Preparing start region for dimension minecraft:toimisko
[16:26:07 INFO]: Loaded 0 spawn chunks for world toimisko
[16:26:07 INFO]: Preparing spawn area: 0%
[16:26:07 INFO]: Preparing spawn area: 0%
[16:26:07 INFO]: Preparing spawn area: 0%
[16:26:08 INFO]: Time elapsed: 1503 ms
[16:26:08 INFO]: [WorldGuard] Default configuration file written: config_world.yml
[16:26:08 INFO]: [WorldGuard] Default configuration file written: blacklist.txt
[16:26:08 INFO]: [WorldGuard] (toimisko) TNT ignition is PERMITTED.
[16:26:08 INFO]: [WorldGuard] (toimisko) Lighters are PERMITTED.
[16:26:08 INFO]: [WorldGuard] (toimisko) Lava fire is PERMITTED.
[16:26:08 INFO]: [WorldGuard] (toimisko) Fire spread is UNRESTRICTED.
[16:26:08 INFO]: [WorldGuard] Loaded configuration for world 'toimisko'
[16:26:08 WARN]: Unexpected exception while parsing console command "cworld toimisko"
org.bukkit.command.CommandException: Unhandled exception executing command 'cworld' in plugin Worlds v1.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:807) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:769) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:411) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:378) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1208) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:996) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:173) ~[patched_1.16.5.jar:git-Paper-438]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.NullPointerException
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:68) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:63) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.onCommand(WorldCmd.java:43) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.16.5.jar:git-Paper-438]
    ... 9 more



/world toimisko

[16:27:40 INFO]: Tämän niminen maailma on olemassa jo!
[16:27:40 WARN]: Unexpected exception while parsing console command "world toimisko"
org.bukkit.command.CommandException: Unhandled exception executing command 'world' in plugin Worlds v1.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:807) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:769) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:411) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:378) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1208) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:996) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:173) ~[patched_1.16.5.jar:git-Paper-438]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.NullPointerException
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:68) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:63) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.onCommand(WorldCmd.java:43) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.16.5.jar:git-Paper-438]
    ... 9 more




/rmworld toimisko

[16:28:24 WARN]: Unexpected exception while parsing console command "rmworld toimisko"
org.bukkit.command.CommandException: Unhandled exception executing command 'rmworld' in plugin Worlds v1.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:807) ~[patched_1.16.5.jar:git-Paper-438]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:769) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:411) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:378) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1208) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:996) ~[patched_1.16.5.jar:git-Paper-438]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:173) ~[patched_1.16.5.jar:git-Paper-438]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.NullPointerException
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:68) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.deleteWorld(WorldCmd.java:63) ~[?:?]
    at world.paahdinMC.fi.WorldCmd.onCommand(WorldCmd.java:43) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.16.5.jar:git-Paper-438]
    ... 9 more

Sorry about bad English!

Please help me!

Now i need to type here something because i can't post this with that code text ratio.


Solution

  • With @RIVERMAN2010 big help i found the solution! First i didn't have break keyword and i didn't unload the world.

    Here is the final code:

    import java.io.File;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.World.Environment;
    import org.bukkit.WorldCreator;
    import org.bukkit.WorldType;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class WorldCmd extends JavaPlugin implements CommandExecutor {
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            switch (label.toLowerCase()) {
                case "world":
                    if (sender instanceof Player) {
                        Player p = (Player) sender;
                        if (Bukkit.getWorld(args[0]) != null) {
                            Location loc = new Location(Bukkit.getWorld(args[0]), 0, 255, 0);
                            p.teleport(loc);    
                        }
                        else {
                            p.sendMessage("World does not exist!!");
                        }
                    }
                    break;
                case "cworld":
                    if (Bukkit.getWorld(args[0]) == null) {
                        createWorld(args);
                    }
                    else {
                        if (sender != null) {
                        sender.sendMessage("This world exist alredy!"); 
                        }
                    }
                    break;
                    
                case "rmworld":
                    for(Player players : Bukkit.getOnlinePlayers()){
                        if(players.getWorld().getName().equals(args[0])) {
                        players.kickPlayer("World removed!");
                        }
                    }
                    World delete = Bukkit.getWorld(args[0]);
                    Bukkit.unloadWorld(args[0], true);
                    delete.getWorldFolder().delete();
                    File deleteFolder = delete.getWorldFolder();
                    deleteWorld(deleteFolder, sender);
                    break;
                    
                default:
                    
            }
            return false;
        }
        private void createWorld(String[] args) {
            WorldCreator wc = new WorldCreator(args[0]);
    
            wc.environment(Environment.NORMAL);
            wc.type(WorldType.NORMAL);
    
            wc.createWorld();
        }
        public boolean deleteWorld(File path, CommandSender sender) {
            if(path.exists()) {
                File files[] = path.listFiles();
                for(int i=0; i<files.length; i++) {
                    if(files[i].isDirectory()) {
                        deleteWorld(files[i], null);
                    } else {
                        files[i].delete();
                    }
                }
                if (sender != null) {
                sender.sendMessage("Ready!");
                }
            }
            else {
                if (sender != null) {
                    sender.sendMessage("World does not exist!");    
                }
            }
            return(path.delete());
        }
    }
    

    Very big thank you for @RIVERMAN2010 for helping me actively!