Skip to content

Commit

Permalink
added load and unload command to the world manage
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Sep 22, 2023
1 parent 561bf1c commit b8a6771
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 35 deletions.
151 changes: 121 additions & 30 deletions src/main/java/tk/pandadev/nextron/commands/WorldCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import tk.pandadev.nextron.Main;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -31,43 +37,104 @@ protected void execute(CommandSender sender, String label, String[] args) {
}
Player player = (Player) (sender);

if (args.length == 2 && args[0].equalsIgnoreCase("tp")){
if (args.length == 2 && args[0].equalsIgnoreCase("tp")) {
List<World> worlds = Bukkit.getWorlds();
List<String> world_names = new ArrayList<>();
for (World world : worlds){
for (World world : worlds) {
world_names.add(world.getName());
}

if (!world_names.contains(args[1])){player.sendMessage(Main.getPrefix() + Text.get("world.error")); return;}
if (!world_names.contains(args[1])) {
player.sendMessage(Main.getPrefix() + Text.get("world.error"));
return;
}

for (World world : worlds){
if (world.getName().equals(args[1])){
for (World world : worlds) {
if (world.getName().equals(args[1])) {
player.teleport(world.getSpawnLocation());
player.sendMessage(Main.getPrefix() + Text.get("world.success").replace("%w", world.getName()));
}
}
}

if (args.length == 3 && args[0].equalsIgnoreCase("create")){
if (args.length >= 2 && args[0].equalsIgnoreCase("create")) {
WorldCreator wc = new WorldCreator(args[1]);

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

String seed = args[2];
wc.seed(seed.hashCode());
if (args.length == 3) {
String seed = args[2];
wc.seed(seed.hashCode());
}

player.sendMessage(Main.getPrefix() + Text.get("world.create.start").replace("%w", args[1]));
Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), wc::createWorld);
player.sendMessage(Main.getPrefix() + Text.get("world.create.finished").replace("%w", args[1]));
Bukkit.getScheduler().runTask(Main.getInstance(), () -> {
wc.createWorld();
player.sendMessage(Main.getPrefix() + Text.get("world.create.finished").replace("%w", args[1]));
});
}

if (args.length == 2 && args[0].equalsIgnoreCase("delete")) {
String worldName = args[1];
if (worldName.equals("world")) {
player.sendMessage(Main.getPrefix() + Text.get("world.delete.default.error"));
return;
}
World world = Bukkit.getWorld(worldName);
if (world == null) {
player.sendMessage(Main.getPrefix() + Text.get("world.delete.error").replace("%w", worldName));
return;
}
List<World> worlds = Bukkit.getWorlds();
worlds.remove(world);
World newWorld = Bukkit.getWorld("world");
if (newWorld == null && !worlds.isEmpty()) {
newWorld = worlds.get(new Random().nextInt(worlds.size()));
}
if (newWorld != null) {
for (Player p : world.getPlayers()) {
p.teleport(newWorld.getSpawnLocation());
}
}

player.sendMessage(Main.getPrefix() + Text.get("world.delete.start").replace("%w", worldName));
if (Bukkit.unloadWorld(world, true)) {
deleteWorld(new File(Bukkit.getServer().getWorldContainer().getAbsolutePath(), worldName));
player.sendMessage(Main.getPrefix() + Text.get("world.delete.finished").replace("%w", worldName));
} else {
player.sendMessage(Main.getPrefix() + Text.get("world.delete.error").replace("%w", worldName));
}
}

if (args.length == 2 && args[0].equalsIgnoreCase("load")) {
String worldName = args[1];
File worldFolder = new File(Bukkit.getServer().getWorldContainer().getAbsolutePath(), worldName);
if (!worldFolder.exists()) {
player.sendMessage(Main.getPrefix() + Text.get("world.load.notexist").replace("%w", worldName));
return;
}
World world = Bukkit.getWorld(worldName);
if (world == null) {
player.sendMessage(Main.getPrefix() + Text.get("world.load.start").replace("%w", worldName));
WorldCreator wc = new WorldCreator(worldName);
Bukkit.getScheduler().runTaskAsynchronously(Main.getInstance(), wc::createWorld);
player.sendMessage(Main.getPrefix() + Text.get("world.load.success").replace("%w", worldName));
} else {
player.sendMessage(Main.getPrefix() + Text.get("world.load.error").replace("%w", worldName));
}
}

if (args.length == 2 && args[0].equalsIgnoreCase("unload")) {
String worldName = args[1];
if (worldName.equals("world")) {
player.sendMessage(Main.getPrefix() + Text.get("world.unload.default.error"));
return;
}
World world = Bukkit.getWorld(worldName);
if (world != null) {
List<World> worlds = Bukkit.getWorlds();
worlds.remove(world);
worlds.remove(world);;
World newWorld = Bukkit.getWorld("world");
if (newWorld == null && !worlds.isEmpty()) {
newWorld = worlds.get(new Random().nextInt(worlds.size()));
Expand All @@ -78,26 +145,36 @@ protected void execute(CommandSender sender, String label, String[] args) {
}
}

player.sendMessage(Main.getPrefix() + Text.get("world.delete.start").replace("%w", worldName));
Bukkit.unloadWorld(world, true);
deleteWorld(new File(Bukkit.getServer().getWorldContainer().getAbsolutePath(), worldName));
player.sendMessage(Main.getPrefix() + Text.get("world.delete.finished").replace("%w", worldName));
player.sendMessage(Main.getPrefix() + Text.get("world.unload.start").replace("%w", worldName));
if (Bukkit.unloadWorld(world, true)) {
player.sendMessage(Main.getPrefix() + Text.get("world.unload.success").replace("%w", worldName));
} else {
player.sendMessage(Main.getPrefix() + Text.get("world.unload.error").replace("%w", worldName));
}
} else {
player.sendMessage(Main.getPrefix() + Text.get("world.delete.error").replace("%w", worldName));
player.sendMessage(Main.getPrefix() + Text.get("world.unload.error").replace("%w", worldName));
}
}
}

public void deleteWorld(File path) {
if (path.exists()) {
File files[] = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteWorld(files[i]);
} else {
files[i].delete();
Path directory = path.toPath();
try {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}

Expand All @@ -110,27 +187,41 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
list.add("tp");
list.add("create");
list.add("delete");
list.add("load");
list.add("unload");
}

if (args.length == 3 && args[0].equalsIgnoreCase("create")){
if (args.length == 3 && args[0].equalsIgnoreCase("create")) {
list.add("<seed>");
}
if (args.length == 2 && args[0].equalsIgnoreCase("create")){
if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
list.add("<name>");
}

if (args.length == 2 && args[0].equalsIgnoreCase("tp")){
if (args.length == 2 && args[0].equalsIgnoreCase("tp")) {
List<World> worlds = Bukkit.getWorlds();

for (World world : worlds){
for (World world : worlds) {
list.add(world.getName());
}
}

if (args.length == 2 && args[0].equalsIgnoreCase("delete")){
if (args.length == 2 && args[0].equalsIgnoreCase("delete")) {
List<World> worlds = Bukkit.getWorlds();

for (World world : worlds) {
list.add(world.getName());
}
}

if (args.length == 2 && args[0].equalsIgnoreCase("load")) {
list.add("<world>");
}

if (args.length == 2 && args[0].equalsIgnoreCase("unload")) {
List<World> worlds = Bukkit.getWorlds();

for (World world : worlds){
for (World world : worlds) {
list.add(world.getName());
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@
"world.delete.start": "§7Die Welt mit dem namen §a%w §7wird gelöscht",
"world.delete.finished": "§7Die Welt mit dem namen §a%w §7wurde erfolgriche gelöscht",
"world.delete.error": "§7Keine Welt gefunden mit dem namen §a%w",
"world.load.start": "§7Die Welt §a%w §7wird geladen",
"world.load.success": "§7Die Welt §a%w §7wurde erfolgreich geladen",
"world.load.error": "§cDie Welt §6%w §ckonnte nicht geladen werden",
"world.load.notexist": "§cDie Welt §6%w §cexistiert nicht",
"world.unload.start": "§7Die Welt §a%w §7wird entladen",
"world.unload.success": "§7Die Welt §a%w §7wurde entladen",
"world.unload.error": "§cDie Welt §6%w §ckonnte nicht entladen werden",
"world.delete.default.error": "§cDie standart Welt 'world' kann nicht gelöscht werden",
"world.unload.default.error": "§cDie standart Welt 'world' kann nicht entladen werden",

"setspawn.success": "§7Der spawn wurde erfolgreich and deiner position gesetzt",
"setspawn.error": "§cEs befindet sich kein spawn in der config setze einen mit §7/setspawn",
Expand Down
19 changes: 14 additions & 5 deletions src/main/resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

"maingui.vanish.lore.1": "§8Controls whether a fake join/quit",
"maingui.vanish.lore.2": "§8message will be sent",
"maingui.vanish.lore.3": "§8when you enter or leave the vanish.",
"maingui.vanish.lore.3": "§8when you enter or leave the vanish",
"maingui.vanish.lore.4": "§aLeft-click §c>> §7Toggle",


"maingui.tpa.lore.1": "§8Controls whether other players",
"maingui.tpa.lore.2": "§8can send you a tpa requests or not.",
"maingui.tpa.lore.2": "§8can send you a tpa requests or not",
"maingui.tpa.lore.3": "§aLeft-click §c>> §7Toggle",


"maingui.feedback.lore.1": "§8Controls whether a message should",
"maingui.feedback.lore.2": "§8be sent after a command gets executed",
"maingui.feedback.lore.3": "§8for example 'Gamemode set to creative'.",
"maingui.feedback.lore.3": "§8for example 'Gamemode set to creative'",
"maingui.feedback.lore.4": "§8Errors and important messages",
"maingui.feedback.lore.5": "§8will still be displayed.",
"maingui.feedback.lore.5": "§8will still be displayed",
"maingui.feedback.lore.6": "§aLeft-click §c>> §7Toggle",


Expand Down Expand Up @@ -171,9 +171,18 @@
"world.error": "§cThis world doesn't exist",
"world.create.start": "§7World with the name §a%w §7is being created",
"world.create.finished": "§7Successfully created world with the name §a%w",
"world.delete.start": "§7Started deletion of thw world §a%w",
"world.delete.start": "§7Started deletion of the world §a%w",
"world.delete.finished": "§7Successfully deleted world with the name §a%w",
"world.delete.error": "§7No world found with the name §a%w",
"world.load.start": "§7Starting to load world §a%w",
"world.load.success": "§7World §a%w §7has been successfully loaded",
"world.load.error": "§cWorld §6%w §ccould not be loaded It may already be loaded or does not exist",
"world.load.notexist": "§cWorld §6%w §cdoes not exist",
"world.unload.start": "§7Starting to unload world §a%w",
"world.unload.success": "§7World §a%w §7has been successfully unloaded",
"world.unload.error": "§cWorld §6%w §ccould not be unloaded It may not be loaded or does not exist",
"world.delete.default.error": "§cThe default world 'world' cannot be deleted",
"world.unload.default.error": "§cThe default world 'world' cannot be unloaded",

"setspawn.success": "§7Successfully set spawn at your position",
"setspawn.error": "§cThere is no spawn in the config set one with §6/setspawn",
Expand Down

0 comments on commit b8a6771

Please sign in to comment.