From d04d86f60fe68cb92e2b1f267c522fc6b0af6bc1 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Fri, 27 Aug 2021 20:32:50 -0400 Subject: [PATCH] 1.3.0 beta 4 --- .../hideAndSeek/CommandHandler.java | 4 +- .../hideAndSeek/CommandTabCompleter.java | 6 +- .../net/tylermurphy/hideAndSeek/Store.java | 3 +- .../hideAndSeek/commands/SaveMap.java | 75 +++++++++++++++---- .../hideAndSeek/events/EventListener.java | 1 + .../hideAndSeek/util/Functions.java | 37 --------- 6 files changed, 72 insertions(+), 54 deletions(-) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/CommandHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/CommandHandler.java index 1e62238..ade208d 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/CommandHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/CommandHandler.java @@ -44,7 +44,9 @@ public class CommandHandler { COMMAND_REGISTER.get("about").execute(sender, null); } } else { - if(permissionsRequired && !sender.hasPermission("hideandseek."+args[0].toLowerCase())) { + if(!args[0].toLowerCase().equals("about") && !args[0].toLowerCase().equals("help") && runningBackup) { + sender.sendMessage(errorPrefix + "Map save is currently in progress. Try again later."); + } else if(permissionsRequired && !sender.hasPermission("hideandseek."+args[0].toLowerCase())) { sender.sendMessage(errorPrefix + "You are not allowed to run this command."); } else { try { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/CommandTabCompleter.java b/src/main/java/net/tylermurphy/hideAndSeek/CommandTabCompleter.java index 771c817..cec59af 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/CommandTabCompleter.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/CommandTabCompleter.java @@ -2,6 +2,7 @@ package net.tylermurphy.hideAndSeek; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -10,7 +11,10 @@ public class CommandTabCompleter{ public static List handleTabComplete(CommandSender sender, Command command, String label, String[] args) { if(args.length == 1) { - return new ArrayList(CommandHandler.COMMAND_REGISTER.keySet()); + return new ArrayList(CommandHandler.COMMAND_REGISTER.keySet()) + .stream() + .filter(handle -> sender.hasPermission("hideandseek."+handle.toLowerCase())) + .collect(Collectors.toList()); } else if(args.length > 1) { if(!CommandHandler.COMMAND_REGISTER.containsKey(args[0].toLowerCase())) { return null; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Store.java b/src/main/java/net/tylermurphy/hideAndSeek/Store.java index 2da0c5f..b7350a6 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Store.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Store.java @@ -57,7 +57,8 @@ public class Store { interactableTrapdoors, interactableFencegate, worldborderEnabled = false, - decreaseBorder = false; + decreaseBorder = false, + runningBackup = false; public static int minPlayers, diff --git a/src/main/java/net/tylermurphy/hideAndSeek/commands/SaveMap.java b/src/main/java/net/tylermurphy/hideAndSeek/commands/SaveMap.java index c5b753f..37ead8a 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/commands/SaveMap.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/commands/SaveMap.java @@ -3,12 +3,19 @@ package net.tylermurphy.hideAndSeek.commands; import static net.tylermurphy.hideAndSeek.Store.*; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Arrays; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.scheduler.BukkitRunnable; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.util.Functions; import net.tylermurphy.hideAndSeek.util.ICommand; public class SaveMap implements ICommand { @@ -18,23 +25,63 @@ public class SaveMap implements ICommand { sender.sendMessage(errorPrefix + "Please set spawn location first"); return; } - sender.sendMessage(warningPrefix + "This command may lag the server"); + sender.sendMessage(messagePrefix + "Starting map save"); + sender.sendMessage(warningPrefix + "All commands will be disabled when the save is in progress. Do not turn off the server."); Bukkit.getServer().getWorld(spawnWorld).save(); - File current = new File(Main.root+File.separator+spawnWorld); - if(current.exists()) { - File destenation = new File(Main.root+File.separator+"hideandseek_"+spawnWorld); - if(destenation.exists()) { - deleteDirectory(destenation); - destenation.mkdir(); + BukkitRunnable runnable = new BukkitRunnable() { + public void run() { + File current = new File(Main.root+File.separator+spawnWorld); + if(current.exists()) { + File temp_destenation = new File(Main.root+File.separator+"temp_hideandseek_"+spawnWorld); + File destenation = new File(Main.root+File.separator+"hideandseek_"+spawnWorld); + copyFileStructure(current, temp_destenation); + if(destenation.exists()) { + deleteDirectory(destenation); + destenation.mkdir(); + } + temp_destenation.renameTo(destenation); + sender.sendMessage(messagePrefix + "Map save complete"); + runningBackup = false; + } else { + sender.sendMessage(errorPrefix + "Coudnt find current map"); + } } - Functions.copyFileStructure(current, destenation); - sender.sendMessage(messagePrefix + "Map save complete"); - } else { - sender.sendMessage(errorPrefix + "Coudnt find current map"); - } + }; + runnable.runTaskAsynchronously(Main.plugin); + runningBackup = true; } - boolean deleteDirectory(File directoryToBeDeleted) { + private static void copyFileStructure(File source, File target){ + try { + ArrayList ignore = new ArrayList<>(Arrays.asList("uid.dat", "session.lock")); + if(!ignore.contains(source.getName())) { + if(source.isDirectory()) { + if(!target.exists()) + if (!target.mkdirs()) + throw new IOException("Couldn't create world directory!"); + String files[] = source.list(); + for (String file : files) { + File srcFile = new File(source, file); + File destFile = new File(target, file); + copyFileStructure(srcFile, destFile); + } + } else { + InputStream in = new FileInputStream(source); + OutputStream out = new FileOutputStream(target); + byte[] buffer = new byte[1024]; + int length; + while ((length = in.read(buffer)) > 0) + out.write(buffer, 0, length); + in.close(); + out.close(); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private boolean deleteDirectory(File directoryToBeDeleted) { File[] allContents = directoryToBeDeleted.listFiles(); if (allContents != null) { for (File file : allContents) { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/events/EventListener.java b/src/main/java/net/tylermurphy/hideAndSeek/events/EventListener.java index 0c70d8f..46cf676 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/events/EventListener.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/events/EventListener.java @@ -49,6 +49,7 @@ public class EventListener implements Listener { event.getPlayer().teleport(new Location(Bukkit.getWorld("hideandseek_"+spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ())); } else if(status.equals("Setup") || status.equals("Standby")) { Hider.addEntry(event.getPlayer().getName()); + event.getPlayer().setGameMode(GameMode.ADVENTURE); event.getPlayer().teleport(new Location(Bukkit.getWorld(spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ())); } playerList.put(event.getPlayer().getName(), event.getPlayer()); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Functions.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Functions.java index c064839..57ae441 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Functions.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/Functions.java @@ -2,14 +2,7 @@ package net.tylermurphy.hideAndSeek.util; import static net.tylermurphy.hideAndSeek.Store.*; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.bukkit.Bukkit; @@ -109,36 +102,6 @@ public class Functions { } } - public static void copyFileStructure(File source, File target){ - try { - ArrayList ignore = new ArrayList<>(Arrays.asList("uid.dat", "session.lock")); - if(!ignore.contains(source.getName())) { - if(source.isDirectory()) { - if(!target.exists()) - if (!target.mkdirs()) - throw new IOException("Couldn't create world directory!"); - String files[] = source.list(); - for (String file : files) { - File srcFile = new File(source, file); - File destFile = new File(target, file); - copyFileStructure(srcFile, destFile); - } - } else { - InputStream in = new FileInputStream(source); - OutputStream out = new FileOutputStream(target); - byte[] buffer = new byte[1024]; - int length; - while ((length = in.read(buffer)) > 0) - out.write(buffer, 0, length); - in.close(); - out.close(); - } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - public static void unloadMap(String mapname){ if(Bukkit.getServer().unloadWorld(Bukkit.getServer().getWorld(mapname), false)){ Main.plugin.getLogger().info("Successfully unloaded " + mapname);