Advanced-Portals/src/main/java/com/sekwah/advancedportals/AdvancedPortalsPlugin.java

162 lines
5.4 KiB
Java
Raw Normal View History

package com.sekwah.advancedportals;
import com.sekwah.advancedportals.compat.CraftBukkit;
2017-06-10 09:21:14 +02:00
import com.sekwah.advancedportals.destinations.Destination;
import com.sekwah.advancedportals.destinations.DestinationCommand;
2016-03-01 21:18:52 +01:00
import com.sekwah.advancedportals.effects.WarpEffects;
2016-07-31 05:35:24 +02:00
import com.sekwah.advancedportals.listeners.*;
2017-06-13 02:32:28 +02:00
import com.sekwah.advancedportals.metrics.Metrics;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
2017-06-13 02:32:28 +02:00
public class AdvancedPortalsPlugin extends JavaPlugin {
public CraftBukkit compat = null;
private Settings settings;
2016-03-29 13:38:03 +02:00
public void onEnable() {
String packageName = getServer().getClass().getPackage().getName();
String[] packageSplit = packageName.split("\\.");
String version = packageSplit[packageSplit.length - 1];
saveDefaultConfig();
2017-06-13 02:32:28 +02:00
try {
Metrics metrics = new Metrics(this);
2017-06-13 19:06:48 +02:00
2017-06-13 02:32:28 +02:00
metrics.start();
} catch (IOException e) {
// Failed to submit the stats :-(
}
2016-03-29 13:38:03 +02:00
try {
2016-05-11 12:42:54 +02:00
this.compat = new CraftBukkit(this, version);
2016-05-11 12:42:54 +02:00
// This is a fix for my stupidity with the last version
File dataFolder = this.getDataFolder();
File configFile = new File(dataFolder, "portals.yml");
InputStreamReader inr = null;
try {
inr = new InputStreamReader(new FileInputStream(configFile), "ASCII");
BufferedReader br = new BufferedReader(inr);
StringBuffer sb = new StringBuffer();
while (true) {
String line = br.readLine();
if (line == null)
break;
sb.append(line);
sb.append("\n");
}
br.close();
inr.close();
String fileContents = sb.toString();
fileContents = fileContents.replaceAll(" getPos1\\(\\):", " pos1:");
fileContents = fileContents.replaceAll(" getPos2\\(\\):", " pos2:");
fileContents = fileContents.replaceAll(" getBungee\\(\\):", " bungee:");
try {
FileWriter fileWriter = new FileWriter(configFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(fileContents);
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close();
}
catch(IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
this.getLogger().warning("Invalid Encoding");
} catch (FileNotFoundException e) {
this.getLogger().info("File not found");
} catch (IOException e) {
e.printStackTrace();
}
ConfigAccessor portalConfig = new ConfigAccessor(this, "portals.yml");
portalConfig.saveDefaultConfig();
2016-05-11 12:42:54 +02:00
ConfigAccessor destinationConfig = new ConfigAccessor(this, "destinations.yml");
destinationConfig.saveDefaultConfig();
2016-05-11 12:42:54 +02:00
this.settings = new Settings(this);
2016-05-11 12:42:54 +02:00
// Loads the portal and destination editors
new Portal(this);
new Destination(this);
2016-05-11 12:42:54 +02:00
this.registerCommands();
2016-05-11 12:42:54 +02:00
new WarpEffects(this);
2016-05-11 12:42:54 +02:00
this.addListeners();
this.setupDataCollector();
2016-05-11 12:42:54 +02:00
this.setupBungee();
this.getServer().getConsoleSender().sendMessage("\u00A7aAdvanced portals have been successfully enabled!");
2016-05-11 12:42:54 +02:00
2016-03-29 13:38:03 +02:00
} catch (ClassNotFoundException e) {
2017-05-13 19:19:08 +02:00
e.printStackTrace();
this.getLogger().warning("This version of craftbukkit is not yet supported, please notify sekwah and tell him about this version v:" + version);
2017-05-13 19:19:08 +02:00
this.getLogger().warning("Along with the above stacktrace");
2016-03-29 13:38:03 +02:00
this.setEnabled(false);
} catch (IllegalArgumentException |
NoSuchFieldException | SecurityException | NoSuchMethodException e) {
2016-03-29 13:38:03 +02:00
e.printStackTrace();
this.getLogger().warning("Something went wrong, please notify sekwah and tell him about this version v:" + version);
this.getLogger().warning("Along with the above stacktrace");
this.setEnabled(false);
2016-03-29 13:38:03 +02:00
}
// thanks to the new config accessor code the config.saveDefaultConfig(); will now
// only copy the file if it doesnt exist!
}
private void registerCommands() {
new PluginMessages(this);
new AdvancedPortalsCommand(this);
new DestinationCommand(this);
}
private void addListeners() {
new Listeners(this);
new FlowStopper(this);
new PortalProtect(this);
new PortalPlacer(this);
}
private void setupDataCollector() {
2017-07-20 16:28:00 +02:00
Selection.loadData(this);
}
private void setupBungee() {
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new BungeeListener(this));
}
2016-03-29 13:38:03 +02:00
public void onDisable() {
this.getServer().getConsoleSender().sendMessage("\u00A7cAdvanced portals are being disabled!");
}
public Settings getSettings() {
return settings;
}
}