diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e2c43dd --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + EasyMapReset + me.d3sox.easymapreset + 1.0.4-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.spigotmc + spigot-api + 1.8.8-R0.1-SNAPSHOT + provided + + + org.projectlombok + lombok + LATEST + + + commons-io + commons-io + LATEST + + + + \ No newline at end of file diff --git a/src/main/java/me/d3sox/easymapreset/EasyMapReset.java b/src/main/java/me/d3sox/easymapreset/EasyMapReset.java new file mode 100644 index 0000000..b79260e --- /dev/null +++ b/src/main/java/me/d3sox/easymapreset/EasyMapReset.java @@ -0,0 +1,123 @@ +package me.d3sox.easymapreset; + +import me.d3sox.easymapreset.utils.FileManager; +import me.d3sox.easymapreset.utils.WorldUtils; +import org.apache.commons.io.FileUtils; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.server.ServerCommandEvent; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; + +public class EasyMapReset extends JavaPlugin implements Listener { + + private final String PREFIX = "§5EasyMapReset §8» §7"; + private File mapFolder; + private FileManager fileManager; + private WorldUtils worldUtils; + + @Override + public void onEnable() { + this.fileManager = new FileManager(this, "settings.yml"); + + this.mapFolder = new File(this.getDataFolder(), "mapsBackup"); + if (!this.mapFolder.exists()) { + if (!this.mapFolder.mkdir()) { + Bukkit.getConsoleSender().sendMessage(PREFIX + "Could not create mapsBackup folder! Check permissions!"); + } + } + + if (!this.fileManager.is("worlds")) { + this.fileManager.set("worlds", Collections.singletonList("changeme")); + } + if (!this.fileManager.is("kickPlayers")) { + this.fileManager.set("kickPlayers", false); + } + if (!this.fileManager.is("kickReason")) { + this.fileManager.set("kickReason", "&b[World unloaded]"); + } + if (!this.fileManager.is("teleportMessage")) { + this.fileManager.set("teleportMessage", "&b[World unloaded]"); + } + if (!this.fileManager.is("forceBackup")) { + this.fileManager.set("forceBackup", false); + } + + boolean forceBackup = this.fileManager.getBoolean("forceBackup"); + + this.worldUtils = new WorldUtils(this.fileManager.getBoolean("kickPlayers"), + this.fileManager.getString("kickReason").replace('&', '§'), + this.fileManager.getString("teleportMessage").replace('&', '§')); + + int loaded = 0; + for (String s : this.fileManager.getStringList("worlds")) { + File world = new File(Bukkit.getWorldContainer(), s); + File backupExisting = new File(this.mapFolder, s); + if (!forceBackup && backupExisting.exists()) { + Bukkit.getConsoleSender().sendMessage(PREFIX + "Found a backup for world §b" + s + "§7! If you want to backup the map every time set §bforceBackup §7to true"); + } else { + try { + FileUtils.copyDirectoryToDirectory(world, this.mapFolder); + loaded++; + } catch (IOException e) { + Bukkit.getConsoleSender().sendMessage(PREFIX + "World §b" + s + " §7couldn't be saved. You deleted the mapsBackup folder or the given world does not exist."); + } + } + + this.worldUtils.rollback(s); + } + this.getServer().getPluginManager().registerEvents(this, this); + if (loaded > 0) + Bukkit.getConsoleSender().sendMessage(PREFIX + "Successfully saved §b" + loaded + " §7world" + (loaded > 1 ? "s" : "") + "!"); + } + + @Override + public void onDisable() { + int restored = 0; + for (String s : this.fileManager.getStringList("worlds")) { + if (Bukkit.getWorld(s) != null) { + File world = new File(this.mapFolder, s); + if (world.exists()) { + try { + FileUtils.copyDirectoryToDirectory(world, Bukkit.getWorldContainer()); + restored++; + } catch (IOException e) { + Bukkit.getConsoleSender().sendMessage(PREFIX + "World §b" + s + " §7couldn't be restored."); + } + } else { + Bukkit.getConsoleSender().sendMessage(PREFIX + "No save found for world §b" + s + "§7! Please restart the server once."); + } + this.worldUtils.unloadMap(s); + } else { + Bukkit.getConsoleSender().sendMessage(PREFIX + "World §b" + s + " §7does not exist."); + } + } + if (restored > 0) + Bukkit.getConsoleSender().sendMessage(PREFIX + "Successfully restored §b" + restored + " §7world" + (restored > 1 ? "s" : "") + "!"); + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void on(ServerCommandEvent e) { + if (e.getCommand().toLowerCase().startsWith("easymapreset")) { + e.setCancelled(true); + e.getSender().sendMessage(PREFIX + "This server is running §bEasyMapReset §7version §b" + + this.getDescription().getVersion() + " §7by §b" + this.getDescription().getAuthors().get(0)); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void on(PlayerCommandPreprocessEvent e) { + if (e.getMessage().toLowerCase().startsWith("/easymapreset")) { + e.setCancelled(true); + e.getPlayer().sendMessage(PREFIX + "This server is running §bEasyMapReset §7version §b" + + this.getDescription().getVersion() + " §7by §b" + this.getDescription().getAuthors().get(0)); + } + } + +} diff --git a/src/main/java/me/d3sox/easymapreset/utils/FileManager.java b/src/main/java/me/d3sox/easymapreset/utils/FileManager.java new file mode 100644 index 0000000..bd1c29c --- /dev/null +++ b/src/main/java/me/d3sox/easymapreset/utils/FileManager.java @@ -0,0 +1,151 @@ +package me.d3sox.easymapreset.utils; + +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class FileManager { + + private File file; + @Getter + private FileConfiguration config; + private Plugin plugin; + + public FileManager(Plugin plugin, String config) { + this.plugin = plugin; + this.file = new File(plugin.getDataFolder(), config); + this.config = YamlConfiguration.loadConfiguration(this.file); + this.config.options().header("Configuration for " + this.plugin.getDescription().getName() + " version " + this.plugin.getDescription().getVersion()); + this.saveConfig(); + } + + private void saveConfig() { + try { + this.config.save(this.file); + } catch (IOException ignored) { + } + } + + private String getPluginName() { + return this.plugin.getDescription().getName(); + } + + public List getStringList(String name) { + return this.config.getStringList(this.getPluginName() + "." + name); + } + + public void set(String valueName, Object value) { + this.config.set(this.getPluginName() + "." + valueName, value); + this.saveConfig(); + } + + public boolean is(String valueName) { + return this.config.contains(this.getPluginName() + "." + valueName); + } + + public void setBoolean(String valueName, boolean value) { + this.config.set(this.getPluginName() + "." + valueName, value); + this.saveConfig(); + } + + public boolean getBoolean(String valueName) { + return this.config.getBoolean(this.getPluginName() + "." + valueName); + } + + public void setString(String valueName, String value) { + this.config.set(this.getPluginName() + "." + valueName, value); + this.saveConfig(); + } + + public String getString(String valueName) { + return this.config.getString(this.getPluginName() + "." + valueName); + } + + public void setInt(String valueName, int value) { + this.config.set(this.getPluginName() + "." + valueName, value); + this.saveConfig(); + } + + public int getInt(String valueName) { + return this.config.getInt(this.getPluginName() + "." + valueName); + } + + public void setDouble(String valueName, double value) { + this.config.set(this.getPluginName() + "." + valueName, value); + this.saveConfig(); + } + + public double getDouble(String valueName) { + return this.config.getDouble(this.getPluginName() + "." + valueName); + } + + public void setLocation(String locName, World w, double x, double y, double z, float yaw, float pitch) { + this.config.set(this.getPluginName() + "." + locName + ".w", w.getName()); + this.config.set(this.getPluginName() + "." + locName + ".x", x); + this.config.set(this.getPluginName() + "." + locName + ".y", y); + this.config.set(this.getPluginName() + "." + locName + ".z", z); + this.config.set(this.getPluginName() + "." + locName + ".yaw", yaw); + this.config.set(this.getPluginName() + "." + locName + ".pitch", pitch); + this.saveConfig(); + } + + public void deleteLocation(String locName) { + this.config.set(this.getPluginName() + "." + locName + ".w", null); + this.config.set(this.getPluginName() + "." + locName + ".x", null); + this.config.set(this.getPluginName() + "." + locName + ".y", null); + this.config.set(this.getPluginName() + "." + locName + ".z", null); + this.config.set(this.getPluginName() + "." + locName + ".yaw", null); + this.config.set(this.getPluginName() + "." + locName + ".pitch", null); + this.saveConfig(); + } + + public void setLocation(String locName, Player p) { + this.config.set(this.getPluginName() + "." + locName + ".w", p.getWorld().getName()); + this.config.set(this.getPluginName() + "." + locName + ".x", p.getLocation().getX()); + this.config.set(this.getPluginName() + "." + locName + ".y", p.getLocation().getY()); + this.config.set(this.getPluginName() + "." + locName + ".z", p.getLocation().getZ()); + this.config.set(this.getPluginName() + "." + locName + ".yaw", p.getLocation().getYaw()); + this.config.set(this.getPluginName() + "." + locName + ".pitch", p.getLocation().getPitch()); + this.saveConfig(); + } + + public void setBlockLocation(String locName, Location loc) { + this.config.set(this.getPluginName() + "." + locName + ".w", loc.getWorld().getName()); + this.config.set(this.getPluginName() + "." + locName + ".x", loc.getBlockX()); + this.config.set(this.getPluginName() + "." + locName + ".y", loc.getBlockY()); + this.config.set(this.getPluginName() + "." + locName + ".z", loc.getBlockZ()); + this.saveConfig(); + } + + public Location getBlockLocation(String locName) { + return new Location(Bukkit.getWorld(this.config.getString(this.getPluginName() + "." + locName + ".w")), + this.config.getInt(this.getPluginName() + "." + locName + ".x"), this.config.getInt(this.getPluginName() + "." + locName + ".y"), + this.config.getInt(this.getPluginName() + "." + locName + ".z")); + } + + public Location getLocation(String locName) { + return new Location(Bukkit.getWorld(this.config.getString(this.getPluginName() + "." + locName + ".w")), + this.config.getDouble(this.getPluginName() + "." + locName + ".x"), this.config.getDouble(this.getPluginName() + "." + locName + ".y"), + this.config.getDouble(this.getPluginName() + "." + locName + ".z"), Float.valueOf(this.config.getString(this.getPluginName() + "." + locName + ".yaw")), + Float.valueOf(this.config.getString(this.getPluginName() + "." + locName + ".pitch"))); + } + + public boolean isLocation(String locName) { + return this.config.contains(this.getPluginName() + "." + locName + ".x"); + } + + public void delete(String locName) { + this.config.set(this.getPluginName() + "." + locName, null); + this.saveConfig(); + } + +} diff --git a/src/main/java/me/d3sox/easymapreset/utils/WorldUtils.java b/src/main/java/me/d3sox/easymapreset/utils/WorldUtils.java new file mode 100644 index 0000000..d8e1b2b --- /dev/null +++ b/src/main/java/me/d3sox/easymapreset/utils/WorldUtils.java @@ -0,0 +1,52 @@ +package me.d3sox.easymapreset.utils; + +import lombok.AllArgsConstructor; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.WorldCreator; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +@AllArgsConstructor +public class WorldUtils { + + private boolean kickPlayers; + private String kickReason; + private String teleportMessage; + + public void unloadMap(String mapName) { + World w = Bukkit.getServer().getWorld(mapName); + for (Entity en : w.getEntities()) { + if (en instanceof Player) { + Player p = (Player) en; + if (this.kickPlayers) { + p.kickPlayer(this.kickReason); + } else { + p.teleport(Bukkit.getWorlds().get(0).getSpawnLocation()); + if (this.teleportMessage == null || this.teleportMessage.equals("")) { + continue; + } + if (!StringUtils.isEmpty(this.teleportMessage)) + p.sendMessage(this.teleportMessage); + } + } + } + Bukkit.getServer().unloadWorld(w, false); + } + + private void loadMap(String mapName) { + World w; + if (Bukkit.getWorld(mapName) == null) { + w = Bukkit.getServer().createWorld(new WorldCreator(mapName)); + } else { + w = Bukkit.getWorld(mapName); + } + w.setAutoSave(false); + } + + public void rollback(String mapName) { + this.unloadMap(mapName); + this.loadMap(mapName); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..52d8cb3 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,11 @@ +name: EasyMapReset +author: D3SOX +version: 1.0.5-SNAPSHOT +description: A map reset plugin +main: me.d3sox.easymapreset.EasyMapReset +load: STARTUP +loadbefore: [Multiverse-Core] +commands: + easymapreset: + description: An info command for EasyMapReset + usage: / \ No newline at end of file