Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.plytki</groupId>
<artifactId>VirtualRealty</artifactId>
<version>1.4.1</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>VirtualRealty</name>
Expand All @@ -20,7 +20,6 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
Expand Down Expand Up @@ -108,6 +107,10 @@
<id>okaeri-repo</id>
<url>https://storehouse.okaeri.eu/repository/maven-public/</url>
</repository>
<repository>
<id>dynmap-repo</id>
<url>http://repo.mikeprimm.com/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -168,6 +171,12 @@
<version>3.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.dynmap</groupId>
<artifactId>dynmap-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
145 changes: 115 additions & 30 deletions src/main/java/me/plytki/virtualrealty/VirtualRealty.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import me.plytki.virtualrealty.sql.SQL;
import me.plytki.virtualrealty.tasks.PlotExpireTask;
import me.plytki.virtualrealty.utils.ConfigurationFactory;
import me.plytki.virtualrealty.utils.SchematicUtil;
import me.plytki.virtualrealty.utils.UpdateChecker;
import me.plytki.virtualrealty.utils.multiversion.VMaterial;
import org.apache.commons.io.FileUtils;
Expand All @@ -23,44 +24,51 @@
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;

import java.io.*;
import java.util.*;
import java.util.concurrent.Callable;

public final class VirtualRealty extends JavaPlugin {

//CORE
private static VirtualRealty instance;
public static final String PREFIX = "§a§lVR §8§l» §7";
public static ArrayList<BukkitTask> tasks = new ArrayList<>();
private static final ArrayList<String> postVersions = new ArrayList<>();
public static boolean isLegacy = false;

//FILES
public static File plotsFolder;
public static File plotsSchemaFolder;
public PluginConfiguration pluginConfiguration;
public SizesConfiguration sizesConfiguration;
private final File pluginConfigurationFile = new File(this.getDataFolder(), "config.yml");
private final File sizesConfigurationFile = new File(this.getDataFolder(), "sizes.yml");

public static PluginConfiguration pluginConfiguration;
public static SizesConfiguration sizesConfiguration;
private final File pluginConfigurationFile = new File(this.getDataFolder(), "config.yml");
private final File sizesConfigurationFile = new File(this.getDataFolder(), "sizes.yml");

private static final ArrayList<String> postVersions = new ArrayList<>();
private boolean configError = false;

public static boolean isLegacy = false;
//DYNMAP API
public static DynmapAPI dapi = null;
public static MarkerSet markerset = null;
public static MarkerIcon markerIcon = null;

@Override
public void onEnable() {
// Plugin startup logic
instance = this;
if (!checkLegacyVersions()) {
isLegacy = true;
}
String[] updateCheck = UpdateChecker.getUpdate();
if (updateCheck != null) {
if (!updateCheck[0].equals(this.getDescription().getVersion())) {
this.getLogger().info("A new version is available!");
this.getLogger().info("Current version you're using: " + this.getDescription().getVersion());
this.getLogger().info("A newer version is available!");
this.getLogger().info("The current version you use: " + this.getDescription().getVersion());
this.getLogger().info("Latest version available: " + updateCheck[0]);
this.getLogger().info("Download link: https://www.spigotmc.org/resources/virtual-realty.95599/");
} else {
Expand All @@ -73,7 +81,6 @@ public void onEnable() {
} catch (IOException e) {
e.printStackTrace();
}
registerMetrics();
plotsFolder = new File(getInstance().getDataFolder().getAbsolutePath(), "plots");
plotsFolder.mkdirs();
plotsSchemaFolder = new File(plotsFolder.getAbsolutePath(), "primary-terrain");
Expand All @@ -82,31 +89,89 @@ public void onEnable() {
ConfigurationFactory configFactory = new ConfigurationFactory();
pluginConfiguration = configFactory.createPluginConfiguration(pluginConfigurationFile);
sizesConfiguration = configFactory.createSizesConfiguration(sizesConfigurationFile);
}
catch (Exception exception) {
} catch (Exception exception) {
exception.printStackTrace();
//shutdown("Critical error has been encountered!");
return;
}

//createSizesConfig();
registerMetrics();
loadSizesConfiguration();
connectToDatabase();
PlotManager.loadPlots();
if (pluginConfiguration.dynmapMarkers) {
registerDynmap();
}
registerCommands();
registerListeners();
registerTasks();
checkForOldSchemas();
debug("Server Version: " + this.getServer().getBukkitVersion() + " | " + this.getServer().getVersion());
}

@Override
public void onDisable() {
// Plugin shutdown logic
if (configError) {
return;
}
PlotManager.plots.forEach(Plot::update);
tasks.forEach(BukkitTask::cancel);
SQL.closeConnection();
}

public static void debug(String debugMessage) {
if (VirtualRealty.getPluginConfiguration().debugMode)
VirtualRealty.getInstance().getLogger().warning("DEBUG-MODE > " + debugMessage);
}

public static void checkForOldSchemas() {
for (Plot plot : PlotManager.plots) {
File f = new File(VirtualRealty.plotsSchemaFolder, "plot" + plot.getID() + ".schem");
if (f.exists()) {
List<String> data = SchematicUtil.oldLoad(plot.getID());
FileUtils.deleteQuietly(f);
SchematicUtil.save(plot.getID(), data.toArray(new String[0]));
debug("Converted Plot #" + plot.getID() + " | File: " + f.getName());
}
}
}

public void registerDynmap() {
new BukkitRunnable() {
@Override
public void run() {
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("dynmap");
if (plugin != null && plugin.isEnabled()) {
dapi = (DynmapAPI) plugin;
if (dapi.markerAPIInitialized()) {
markerset = dapi.getMarkerAPI().getMarkerSet("virtualrealty.plots");
if (markerset == null)
markerset = dapi.getMarkerAPI().createMarkerSet("virutalrealty.plots", "Plots", dapi.getMarkerAPI().getMarkerIcons(), false);
for (MarkerSet markerSet : dapi.getMarkerAPI().getMarkerSets()) {
if (markerSet.getMarkerSetLabel().equalsIgnoreCase("Plots")) {
markerset = markerSet;
}
}
try {
if (dapi.getMarkerAPI().getMarkerIcon("virtualrealty_main_icon") == null) {
InputStream in = this.getClass().getResourceAsStream("/ploticon.png");
if (in.available() > 0) {
markerIcon = dapi.getMarkerAPI().createMarkerIcon("virtualrealty_main_icon", "Plots", in);
}
}
else {
markerIcon = dapi.getMarkerAPI().getMarkerIcon("virtualrealty_main_icon");
}
}
catch (IOException ex) {}
VirtualRealty.debug("Registering plots markers..");
for (Plot plot : PlotManager.plots) {
PlotManager.resetPlotMarker(plot);
}
VirtualRealty.debug("Registered plots markers");
this.cancel();
}
}
}
}.runTaskTimer(this, 20, 20*5);
}


private void registerCommands() {
this.getCommand("plot").setExecutor(new PlotCommand());
this.getCommand("virtualrealty").setExecutor(new VirtualRealtyCommand());
Expand All @@ -116,10 +181,12 @@ private void registerListeners() {
getServer().getPluginManager().registerEvents(new PlotListener(), this);
getServer().getPluginManager().registerEvents(new PlotProtectionListener(), this);
getServer().getPluginManager().registerEvents(new WorldListener(), this);
debug("Registered listeners");
}

private void registerTasks() {
tasks.add(new PlotExpireTask().runTaskTimer(this, 20 * 30, 20 * 30));
debug("Registered tasks");
}

private void registerMetrics() {
Expand Down Expand Up @@ -162,15 +229,16 @@ public Map<String, Integer> call() throws Exception {
return valueMap;
}
}));
debug("Registered metrics");
}

private void connectToDatabase() {
SQL.connect();
SQL.createTables();
PlotManager.loadPlots();
debug("Connected to database");
}

public static void loadSizesConfiguration() {
public void loadSizesConfiguration() {
for (PlotSize plotSize : PlotSize.values()) {
if (plotSize == PlotSize.CUSTOM) return;
SizesConfiguration.PlotSizes.Size classSize = null;
Expand Down Expand Up @@ -212,14 +280,27 @@ public static void loadSizesConfiguration() {
plotSize.setWidth(classSize.width);
plotSize.setHeight(classSize.height);
}
debug("Loaded sizes config");
}

public static VirtualRealty getInstance() {
return instance;
}

public static PluginConfiguration getPluginConfiguration() {
return pluginConfiguration;
return VirtualRealty.getInstance().pluginConfiguration;
}

public static File getPluginConfigurationFile() {
return VirtualRealty.getInstance().pluginConfigurationFile;
}

public static SizesConfiguration getSizesConfiguration() {
return VirtualRealty.getInstance().sizesConfiguration;
}

public static File getSizesConfigurationFile() {
return VirtualRealty.getInstance().sizesConfigurationFile;
}

public boolean checkLegacyVersions() {
Expand All @@ -241,19 +322,22 @@ public void setPostVersions() {
}

public void checkConfig() throws IOException {
File oldConfigFile = new File(this.getDataFolder().getAbsolutePath(), "config.yml");
File oldConfigFile = new File(this.getDataFolder(), "config.yml");
if (!oldConfigFile.exists()) return;
String version = null;
boolean isOldVersion = true;
boolean updateConfigVersion = false;
BufferedReader reader = new BufferedReader(new FileReader(this.pluginConfigurationFile));
FileReader fileReader = new FileReader(this.pluginConfigurationFile);
BufferedReader reader = new BufferedReader(fileReader);
String latestLine;
while((latestLine = reader.readLine()) != null) {
if (latestLine.contains("config-version")) {
version = latestLine.replaceAll("config-version: ", "");
isOldVersion = false;
}
}
fileReader.close();
reader.close();
if (version == null) {
System.err.println(" ");
this.getLogger().warning("Config has been reset due to major config changes!");
Expand All @@ -275,7 +359,7 @@ public void checkConfig() throws IOException {
oldConfigFile.delete();
}

// update config version
// update config version
if (updateConfigVersion) {
List<String> lines = new ArrayList<>();
LineIterator iterator = FileUtils.lineIterator(oldConfigFile);
Expand All @@ -297,19 +381,20 @@ public void checkConfig() throws IOException {
}

public void checkSizesConfig() throws IOException {
File oldConfigFile = new File(this.getDataFolder().getAbsolutePath(), "sizes.yml");
File oldConfigFile = new File(this.getDataFolder(), "sizes.yml");
if (!oldConfigFile.exists()) return;
String version = null;
boolean isOldVersion = true;
boolean updateConfigVersion = false;
BufferedReader reader = new BufferedReader(new FileReader(this.pluginConfigurationFile));
BufferedReader reader = new BufferedReader(new FileReader(this.sizesConfigurationFile));
String latestLine;
while((latestLine = reader.readLine()) != null) {
if (latestLine.contains("config-version")) {
version = latestLine.replaceAll("config-version: ", "");
isOldVersion = false;
}
}
reader.close();
if (version == null) {
System.err.println(" ");
this.getLogger().warning("Config has been reset due to major config changes!");
Expand Down
Loading