PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/onarandombox/MultiversePortals/MultiversePortals.java

https://github.com/VibroAxe/Multiverse-Portals
Java | 361 lines | 275 code | 48 blank | 38 comment | 41 complexity | d6655e1e5de1342eb1e2f86ead94ffe3 MD5 | raw file
  1. /*
  2. * Multiverse 2 Copyright (c) the Multiverse Team 2011.
  3. * Multiverse 2 is licensed under the BSD License.
  4. * For more information please check the README.md file included
  5. * with this project
  6. */
  7. package com.onarandombox.MultiversePortals;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import org.bukkit.command.Command;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.event.Event.Priority;
  20. import org.bukkit.event.Event.Type;
  21. import org.bukkit.event.block.BlockListener;
  22. import org.bukkit.event.vehicle.VehicleListener;
  23. import org.bukkit.permissions.Permission;
  24. import org.bukkit.plugin.java.JavaPlugin;
  25. import org.bukkit.util.config.Configuration;
  26. import com.onarandombox.MultiverseCore.MVPlugin;
  27. import com.onarandombox.MultiverseCore.MultiverseCore;
  28. import com.onarandombox.MultiverseCore.commands.HelpCommand;
  29. import com.onarandombox.MultiversePortals.commands.CreateCommand;
  30. import com.onarandombox.MultiversePortals.commands.DebugCommand;
  31. import com.onarandombox.MultiversePortals.commands.InfoCommand;
  32. import com.onarandombox.MultiversePortals.commands.ListCommand;
  33. import com.onarandombox.MultiversePortals.commands.ModifyCommand;
  34. import com.onarandombox.MultiversePortals.commands.RemoveCommand;
  35. import com.onarandombox.MultiversePortals.commands.SelectCommand;
  36. import com.onarandombox.MultiversePortals.commands.WandCommand;
  37. import com.onarandombox.MultiversePortals.configuration.MVPDefaultConfiguration;
  38. import com.onarandombox.MultiversePortals.configuration.MVPortalsConfigMigrator;
  39. import com.onarandombox.MultiversePortals.listeners.MVPBlockListener;
  40. import com.onarandombox.MultiversePortals.listeners.MVPConfigReloadListener;
  41. import com.onarandombox.MultiversePortals.listeners.MVPPlayerListener;
  42. import com.onarandombox.MultiversePortals.listeners.MVPPluginListener;
  43. import com.onarandombox.MultiversePortals.listeners.MVPVehicleListener;
  44. import com.onarandombox.MultiversePortals.utils.PortalDestination;
  45. import com.onarandombox.MultiversePortals.utils.PortalManager;
  46. import com.onarandombox.utils.DebugLog;
  47. import com.pneumaticraft.commandhandler.CommandHandler;
  48. import com.sk89q.worldedit.bukkit.WorldEditAPI;
  49. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  50. public class MultiversePortals extends JavaPlugin implements MVPlugin {
  51. private static final Logger log = Logger.getLogger("Minecraft");
  52. private static final String logPrefix = "[Multiverse-Portals] ";
  53. protected static DebugLog debugLog;
  54. private MultiverseCore core;
  55. private Configuration MVPPortalConfig;
  56. private CommandHandler commandHandler;
  57. protected WorldEditAPI worldEditAPI = null;
  58. private MVPPluginListener pluginListener;
  59. private MVPPlayerListener playerListener;
  60. private PortalManager portalManager;
  61. private Map<Player, PortalPlayerSession> portalSessions;
  62. private BlockListener blockListener;
  63. private VehicleListener vehicleListener;
  64. private MVPConfigReloadListener customListener;
  65. private Configuration MVPConfig;
  66. protected MVPortalsConfigMigrator migrator = new MVPortalsConfigMigrator(this);
  67. public static final int DEFAULT_WAND = 271;
  68. private long portalCooldown = 0;
  69. public void onLoad() {
  70. getDataFolder().mkdirs();
  71. }
  72. public void onEnable() {
  73. this.core = (MultiverseCore) getServer().getPluginManager().getPlugin("Multiverse-Core");
  74. // Test if the Core was found, if not we'll disable this plugin.
  75. if (this.core == null) {
  76. log.info(logPrefix + "Multiverse-Core not found, will keep looking.");
  77. getServer().getPluginManager().disablePlugin(this);
  78. return;
  79. }
  80. // Turn on Logging and register ourselves with Core
  81. log.info(logPrefix + "- Version " + this.getDescription().getVersion() + " Enabled - By " + getAuthors());
  82. debugLog = new DebugLog("Multiverse-Portals", getDataFolder() + File.separator + "debug.log");
  83. this.core.incrementPluginCount();
  84. // Register our commands
  85. this.registerCommands();
  86. // Ensure permissions are created
  87. this.createDefaultPerms();
  88. this.portalManager = new PortalManager(this);
  89. this.portalSessions = new HashMap<Player, PortalPlayerSession>();
  90. this.getCore().getDestinationFactory().registerDestinationType(PortalDestination.class, "p");
  91. this.loadPortals();
  92. this.loadConfig();
  93. // Register our events AFTER the config.
  94. this.registerEvents();
  95. this.checkForWorldEdit();
  96. }
  97. private void registerEvents() {
  98. // Initialize our listeners
  99. this.pluginListener = new MVPPluginListener(this);
  100. this.playerListener = new MVPPlayerListener(this);
  101. this.blockListener = new MVPBlockListener(this);
  102. this.vehicleListener = new MVPVehicleListener(this);
  103. this.customListener = new MVPConfigReloadListener(this);
  104. // Register our listeners with the Bukkit Server
  105. this.getServer().getPluginManager().registerEvent(Type.PLUGIN_ENABLE, this.pluginListener, Priority.Normal, this);
  106. this.getServer().getPluginManager().registerEvent(Type.PLAYER_PORTAL, this.playerListener, Priority.Normal, this);
  107. // Only register this one if they want it
  108. if(this.MVPConfig.getBoolean("use_onmove", true)) {
  109. this.getServer().getPluginManager().registerEvent(Type.PLAYER_MOVE, this.playerListener, Priority.Low, this);
  110. }
  111. this.getServer().getPluginManager().registerEvent(Type.PLAYER_TELEPORT, this.playerListener, Priority.Monitor, this);
  112. this.getServer().getPluginManager().registerEvent(Type.PLAYER_QUIT, this.playerListener, Priority.Monitor, this);
  113. this.getServer().getPluginManager().registerEvent(Type.PLAYER_KICK, this.playerListener, Priority.Monitor, this);
  114. this.getServer().getPluginManager().registerEvent(Type.BLOCK_FROMTO, this.blockListener, Priority.Low, this);
  115. this.getServer().getPluginManager().registerEvent(Type.BLOCK_PHYSICS, this.blockListener, Priority.Normal, this);
  116. this.getServer().getPluginManager().registerEvent(Type.VEHICLE_MOVE, this.vehicleListener, Priority.Normal, this);
  117. this.getServer().getPluginManager().registerEvent(Type.CUSTOM_EVENT, this.customListener, Priority.Normal, this);
  118. // High priority so we override NetherPortals
  119. this.getServer().getPluginManager().registerEvent(Type.PLAYER_PORTAL, this.playerListener, Priority.High, this);
  120. // These will only get used if WE is not found. so they're monitor.
  121. this.getServer().getPluginManager().registerEvent(Type.PLAYER_INTERACT, this.playerListener, Priority.Low, this);
  122. this.getServer().getPluginManager().registerEvent(Type.PLAYER_BUCKET_EMPTY, this.playerListener, Priority.Low, this);
  123. this.getServer().getPluginManager().registerEvent(Type.PLAYER_BUCKET_FILL, this.playerListener, Priority.Low, this);
  124. }
  125. /**
  126. * Currently, WorldEdit is required for portals, we're listening for new plugins coming online, but we need to make sure
  127. */
  128. private void checkForWorldEdit() {
  129. if (this.getServer().getPluginManager().getPlugin("WorldEdit") != null) {
  130. this.worldEditAPI = new WorldEditAPI((WorldEditPlugin) this.getServer().getPluginManager().getPlugin("WorldEdit"));
  131. }
  132. }
  133. /**
  134. * Create the higher level permissions so we can add finer ones to them.
  135. */
  136. private void createDefaultPerms() {
  137. if (this.getServer().getPluginManager().getPermission("multiverse.portal.*") == null) {
  138. Permission perm = new Permission("multiverse.portal.*");
  139. this.getServer().getPluginManager().addPermission(perm);
  140. }
  141. if (this.getServer().getPluginManager().getPermission("multiverse.portal.access.*") == null) {
  142. Permission perm = new Permission("multiverse.portal.access.*");
  143. this.getServer().getPluginManager().addPermission(perm);
  144. }
  145. // Now add these to our parent one.
  146. Permission allPortals = this.getServer().getPluginManager().getPermission("multiverse.portal.*");
  147. allPortals.getChildren().put("multiverse.portal.access.*", true);
  148. this.getServer().getPluginManager().recalculatePermissionDefaults(allPortals);
  149. Permission all = this.getServer().getPluginManager().getPermission("multiverse.*");
  150. all.getChildren().put("multiverse.portal.*", true);
  151. this.getServer().getPluginManager().recalculatePermissionDefaults(all);
  152. }
  153. public PortalPlayerSession getPortalSession(Player p) {
  154. if (this.portalSessions.containsKey(p)) {
  155. return this.portalSessions.get(p);
  156. }
  157. PortalPlayerSession session = new PortalPlayerSession(this, p);
  158. this.portalSessions.put(p, session);
  159. return session;
  160. }
  161. private void loadPortals() {
  162. new MVPDefaultConfiguration(getDataFolder(), "portals.yml", this.migrator);
  163. this.MVPPortalConfig = new Configuration(new File(getDataFolder(), "portals.yml"));
  164. this.MVPPortalConfig.load();
  165. List<String> keys = this.MVPPortalConfig.getKeys("portals");
  166. if (keys != null) {
  167. for (String pname : keys) {
  168. this.portalManager.addPortal(MVPortal.loadMVPortalFromConfig(this, pname));
  169. }
  170. staticLog(Level.INFO, keys.size() + " - Portals(s) loaded");
  171. }
  172. // Now Resolve destinations
  173. for (MVPortal portal : this.portalManager.getAllPortals()) {
  174. String dest = this.MVPPortalConfig.getString("portals." + portal.getName() + ".destination", "");
  175. if (dest != "") {
  176. portal.setDestination(dest);
  177. }
  178. }
  179. }
  180. private void loadConfig() {
  181. new MVPDefaultConfiguration(getDataFolder(), "config.yml", this.migrator);
  182. this.MVPConfig = new Configuration(new File(getDataFolder(), "config.yml"));
  183. this.MVPConfig.load();
  184. this.MVPConfig.getBoolean("use_onmove", true);
  185. this.portalCooldown = this.MVPConfig.getInt("portal_cooldown", 1000);
  186. this.MVPConfig.getBoolean("mvportals_default_to_nether", false);
  187. this.MVPConfig.save();
  188. }
  189. public void onDisable() {
  190. }
  191. /**
  192. * Register commands to Multiverse's CommandHandler so we get a super sexy single menu
  193. */
  194. private void registerCommands() {
  195. this.commandHandler = this.core.getCommandHandler();
  196. this.commandHandler.registerCommand(new InfoCommand(this));
  197. this.commandHandler.registerCommand(new ListCommand(this));
  198. this.commandHandler.registerCommand(new CreateCommand(this));
  199. this.commandHandler.registerCommand(new DebugCommand(this));
  200. this.commandHandler.registerCommand(new RemoveCommand(this));
  201. this.commandHandler.registerCommand(new ModifyCommand(this));
  202. this.commandHandler.registerCommand(new SelectCommand(this));
  203. this.commandHandler.registerCommand(new WandCommand(this));
  204. for(com.pneumaticraft.commandhandler.Command c : this.commandHandler.getAllCommands()) {
  205. if(c instanceof HelpCommand) {
  206. c.addKey("mvp");
  207. }
  208. }
  209. }
  210. @Override
  211. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
  212. if (!this.isEnabled()) {
  213. sender.sendMessage("This plugin is Disabled!");
  214. return true;
  215. }
  216. ArrayList<String> allArgs = new ArrayList<String>(Arrays.asList(args));
  217. allArgs.add(0, command.getName());
  218. return this.commandHandler.locateAndRunCommand(sender, allArgs);
  219. }
  220. /**
  221. * Parse the Authors Array into a readable String with ',' and 'and'.
  222. *
  223. * @return String containing all the authors formatted correctly with ',' and 'and'.
  224. */
  225. private String getAuthors() {
  226. String authors = "";
  227. for (int i = 0; i < this.getDescription().getAuthors().size(); i++) {
  228. if (i == this.getDescription().getAuthors().size() - 1) {
  229. authors += " and " + this.getDescription().getAuthors().get(i);
  230. } else {
  231. authors += ", " + this.getDescription().getAuthors().get(i);
  232. }
  233. }
  234. return authors.substring(2);
  235. }
  236. public WorldEditAPI getWEAPI() {
  237. return this.worldEditAPI;
  238. }
  239. public MultiverseCore getCore() {
  240. return this.core;
  241. }
  242. public PortalManager getPortalManager() {
  243. return this.portalManager;
  244. }
  245. public Configuration getPortalsConfig() {
  246. return this.MVPPortalConfig;
  247. }
  248. public void setCore(MultiverseCore multiverseCore) {
  249. this.core = multiverseCore;
  250. }
  251. public Configuration getMainConfig() {
  252. return this.MVPConfig;
  253. }
  254. public void reloadConfigs() {
  255. this.portalManager.removeAll(false);
  256. this.loadPortals();
  257. this.loadConfig();
  258. }
  259. /**
  260. * Print messages to the server Log as well as to our DebugLog.
  261. *
  262. * @param level
  263. * @param msg
  264. */
  265. public static void staticLog(Level level, String msg) {
  266. log.log(level, logPrefix + " " + msg);
  267. debugLog.log(level, logPrefix + " " + msg);
  268. }
  269. public static void staticDebugLog(Level level, String msg) {
  270. log.log(level, "[MVPortals-Debug] " + msg);
  271. debugLog.log(level, "[MVPortals-Debug] " + msg);
  272. }
  273. public void setWorldEditAPI(WorldEditAPI api) {
  274. this.worldEditAPI = api;
  275. }
  276. @Override
  277. public void log(Level level, String msg) {
  278. if (level == Level.FINE && MultiverseCore.GlobalDebug >= 1) {
  279. staticDebugLog(Level.INFO, msg);
  280. return;
  281. } else if (level == Level.FINER && MultiverseCore.GlobalDebug >= 2) {
  282. staticDebugLog(Level.INFO, msg);
  283. return;
  284. } else if (level == Level.FINEST && MultiverseCore.GlobalDebug >= 3) {
  285. staticDebugLog(Level.INFO, msg);
  286. return;
  287. } else if (level != Level.FINE && level != Level.FINER && level != Level.FINEST) {
  288. staticLog(level, msg);
  289. }
  290. }
  291. @Override
  292. public String dumpVersionInfo(String buffer) {
  293. buffer += logAndAddToPasteBinBuffer("Multiverse-Portals Version: " + this.getDescription().getVersion());
  294. buffer += logAndAddToPasteBinBuffer("Bukkit Version: " + this.getServer().getVersion());
  295. buffer += logAndAddToPasteBinBuffer("Loaded Portals: " + this.getPortalManager().getAllPortals().size());
  296. buffer += logAndAddToPasteBinBuffer("Dumping Portal Values: (version " + this.getPortalsConfig().getString("version", "NOT SET") + ")");
  297. buffer += logAndAddToPasteBinBuffer(this.getPortalsConfig().getAll() + "");
  298. buffer += logAndAddToPasteBinBuffer("Dumping Config Values: (version " + this.getMainConfig().getString("version", "NOT SET") + ")");
  299. buffer += logAndAddToPasteBinBuffer("wand: " + this.getMainConfig().getString("wand", "NOT SET"));
  300. buffer += logAndAddToPasteBinBuffer("Special Code: FRN001");
  301. return buffer;
  302. }
  303. private String logAndAddToPasteBinBuffer(String string) {
  304. this.log(Level.INFO, string);
  305. return "[Multiverse-Portals] " + string + "\n";
  306. }
  307. public void removePortalPlayerSession(String name) {
  308. this.portalSessions.remove(name);
  309. }
  310. public long getCooldownTime() {
  311. return this.portalCooldown;
  312. }
  313. }