简单世界管理脚本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$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

很抱歉我的英语不好!

请帮帮我!

现在我需要在这里输入一些东西,因为我不能发布这个代码文本比例

在@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对我的积极帮助!

最新更新