1.3.0 beta 4

This commit is contained in:
Tyler Murphy 2021-08-27 20:32:50 -04:00
parent 0154a34f1d
commit d04d86f60f
6 changed files with 72 additions and 54 deletions

View file

@ -44,7 +44,9 @@ public class CommandHandler {
COMMAND_REGISTER.get("about").execute(sender, null); COMMAND_REGISTER.get("about").execute(sender, null);
} }
} else { } 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."); sender.sendMessage(errorPrefix + "You are not allowed to run this command.");
} else { } else {
try { try {

View file

@ -2,6 +2,7 @@ package net.tylermurphy.hideAndSeek;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -10,7 +11,10 @@ public class CommandTabCompleter{
public static List<String> handleTabComplete(CommandSender sender, Command command, String label, String[] args) { public static List<String> handleTabComplete(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1) { if(args.length == 1) {
return new ArrayList<String>(CommandHandler.COMMAND_REGISTER.keySet()); return new ArrayList<String>(CommandHandler.COMMAND_REGISTER.keySet())
.stream()
.filter(handle -> sender.hasPermission("hideandseek."+handle.toLowerCase()))
.collect(Collectors.toList());
} else if(args.length > 1) { } else if(args.length > 1) {
if(!CommandHandler.COMMAND_REGISTER.containsKey(args[0].toLowerCase())) { if(!CommandHandler.COMMAND_REGISTER.containsKey(args[0].toLowerCase())) {
return null; return null;

View file

@ -57,7 +57,8 @@ public class Store {
interactableTrapdoors, interactableTrapdoors,
interactableFencegate, interactableFencegate,
worldborderEnabled = false, worldborderEnabled = false,
decreaseBorder = false; decreaseBorder = false,
runningBackup = false;
public static int public static int
minPlayers, minPlayers,

View file

@ -3,12 +3,19 @@ package net.tylermurphy.hideAndSeek.commands;
import static net.tylermurphy.hideAndSeek.Store.*; import static net.tylermurphy.hideAndSeek.Store.*;
import java.io.File; 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.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;
import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.util.Functions;
import net.tylermurphy.hideAndSeek.util.ICommand; import net.tylermurphy.hideAndSeek.util.ICommand;
public class SaveMap implements ICommand { public class SaveMap implements ICommand {
@ -18,23 +25,63 @@ public class SaveMap implements ICommand {
sender.sendMessage(errorPrefix + "Please set spawn location first"); sender.sendMessage(errorPrefix + "Please set spawn location first");
return; 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(); Bukkit.getServer().getWorld(spawnWorld).save();
File current = new File(Main.root+File.separator+spawnWorld); BukkitRunnable runnable = new BukkitRunnable() {
if(current.exists()) { public void run() {
File destenation = new File(Main.root+File.separator+"hideandseek_"+spawnWorld); File current = new File(Main.root+File.separator+spawnWorld);
if(destenation.exists()) { if(current.exists()) {
deleteDirectory(destenation); File temp_destenation = new File(Main.root+File.separator+"temp_hideandseek_"+spawnWorld);
destenation.mkdir(); 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"); runnable.runTaskAsynchronously(Main.plugin);
} else { runningBackup = true;
sender.sendMessage(errorPrefix + "Coudnt find current map");
}
} }
boolean deleteDirectory(File directoryToBeDeleted) { private static void copyFileStructure(File source, File target){
try {
ArrayList<String> 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(); File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) { if (allContents != null) {
for (File file : allContents) { for (File file : allContents) {

View file

@ -49,6 +49,7 @@ public class EventListener implements Listener {
event.getPlayer().teleport(new Location(Bukkit.getWorld("hideandseek_"+spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ())); event.getPlayer().teleport(new Location(Bukkit.getWorld("hideandseek_"+spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ()));
} else if(status.equals("Setup") || status.equals("Standby")) { } else if(status.equals("Setup") || status.equals("Standby")) {
Hider.addEntry(event.getPlayer().getName()); Hider.addEntry(event.getPlayer().getName());
event.getPlayer().setGameMode(GameMode.ADVENTURE);
event.getPlayer().teleport(new Location(Bukkit.getWorld(spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ())); event.getPlayer().teleport(new Location(Bukkit.getWorld(spawnWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ()));
} }
playerList.put(event.getPlayer().getName(), event.getPlayer()); playerList.put(event.getPlayer().getName(), event.getPlayer());

View file

@ -2,14 +2,7 @@ package net.tylermurphy.hideAndSeek.util;
import static net.tylermurphy.hideAndSeek.Store.*; 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.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -109,36 +102,6 @@ public class Functions {
} }
} }
public static void copyFileStructure(File source, File target){
try {
ArrayList<String> 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){ public static void unloadMap(String mapname){
if(Bukkit.getServer().unloadWorld(Bukkit.getServer().getWorld(mapname), false)){ if(Bukkit.getServer().unloadWorld(Bukkit.getServer().getWorld(mapname), false)){
Main.plugin.getLogger().info("Successfully unloaded " + mapname); Main.plugin.getLogger().info("Successfully unloaded " + mapname);