Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
D3S0X authored Apr 10, 2019
1 parent 72189e9 commit 20dda71
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>EasyMapReset</groupId>
<artifactId>me.d3sox.easymapreset</artifactId>
<version>1.0.4-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>

</project>
123 changes: 123 additions & 0 deletions src/main/java/me/d3sox/easymapreset/EasyMapReset.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

}
151 changes: 151 additions & 0 deletions src/main/java/me/d3sox/easymapreset/utils/FileManager.java
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}

}
52 changes: 52 additions & 0 deletions src/main/java/me/d3sox/easymapreset/utils/WorldUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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: /<command>

0 comments on commit 20dda71

Please sign in to comment.