简单的世界管理脚本 SPIGOT-API

Simple world management script SPIGOT-API

我正在尝试学习代码插口插件,我想制作简单的世界管理插件。 首先我想说我在 java.

方面还很新手

这是我的代码:

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());
    }
}

这是我的错误,我得到了什么:

/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[=12=](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[=12=](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[=12=](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

抱歉英语不好!

请帮帮我!

现在我需要在这里输入一些东西,因为我不能 post 使用那个代码文本比例。

在@RIVERMAN2010 的大力帮助下,我找到了解决方案! 首先我没有 break 关键字,我也没有卸载世界。

这是最终代码:

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());
    }
}

非常感谢@RIVERMAN2010 的积极帮助!