PageRenderTime 3850ms CodeModel.GetById 19ms RepoModel.GetById 3ms app.codeStats 0ms

/common/src/main/java/com/sk89q/craftbook/bukkit/BaseBukkitPlugin.java

http://github.com/sk89q/craftbook
Java | 325 lines | 181 code | 62 blank | 82 comment | 40 complexity | ef67fc2425be4877b9771f5ff039e5db MD5 | raw file
Possible License(s): GPL-3.0
  1. // $Id$
  2. /*
  3. * Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.sk89q.craftbook.bukkit;
  19. import com.sk89q.bukkit.util.CommandsManagerRegistration;
  20. import com.sk89q.craftbook.BaseConfiguration;
  21. import com.sk89q.craftbook.LanguageManager;
  22. import com.sk89q.craftbook.LocalPlayer;
  23. import com.sk89q.craftbook.util.LocationUtil;
  24. import com.sk89q.minecraft.util.commands.*;
  25. import com.sk89q.wepif.PermissionsResolverManager;
  26. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  27. import com.sk89q.worldguard.protection.ApplicableRegionSet;
  28. import com.sk89q.worldguard.protection.flags.StateFlag;
  29. import org.bukkit.ChatColor;
  30. import org.bukkit.Location;
  31. import org.bukkit.World;
  32. import org.bukkit.command.CommandSender;
  33. import org.bukkit.command.ConsoleCommandSender;
  34. import org.bukkit.entity.Player;
  35. import org.bukkit.event.Listener;
  36. import org.bukkit.plugin.java.JavaPlugin;
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40. import java.io.InputStream;
  41. import java.util.Random;
  42. import java.util.logging.Logger;
  43. /**
  44. * Base plugin class for CraftBook for child CraftBook plugins.
  45. *
  46. * @author sk89q
  47. */
  48. public abstract class BaseBukkitPlugin extends JavaPlugin {
  49. public BaseConfiguration config;
  50. /**
  51. * The permissions resolver in use.
  52. */
  53. private PermissionsResolverManager perms;
  54. /**
  55. * The Language Manager
  56. */
  57. protected LanguageManager languageManager;
  58. protected final CommandsManager<CommandSender> commands;
  59. private final CommandsManagerRegistration commandManager;
  60. protected WorldGuardPlugin worldguard = null;
  61. protected boolean useWorldGuard = false;
  62. protected StateFlag useFlag = null;
  63. public static final Random random = new Random(); //Good random, allowing for more random numbers.
  64. /**
  65. * Logger for messages.
  66. */
  67. protected static final Logger logger = Logger.getLogger("Minecraft.CraftBook");
  68. public BaseBukkitPlugin() {
  69. commands = new CommandsManager<CommandSender>() {
  70. @Override
  71. public boolean hasPermission(CommandSender player, String perm) {
  72. return player.hasPermission(perm);
  73. }
  74. };
  75. // create the command manager
  76. commandManager = new CommandsManagerRegistration(this, commands);
  77. // Set the proper command injector
  78. commands.setInjector(new SimpleInjector(this));
  79. }
  80. public WorldGuardPlugin getWorldGuard() {
  81. if (!useWorldGuard) return null;
  82. if (worldguard == null) {
  83. worldguard = (WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard");
  84. }
  85. return worldguard;
  86. }
  87. public boolean canUseInArea(Location loc, Player p) {
  88. if (!useWorldGuard) return true;
  89. if (!CraftBookPlugin.getInstance().getLocalConfiguration().checkWGRegions || getWorldGuard() == null)
  90. return true;
  91. if (useFlag == null) {
  92. useFlag = new StateFlag("use", true);
  93. }
  94. if (loc == null || p == null) return true;
  95. ApplicableRegionSet rset = getWorldGuard().getRegionManager(loc.getWorld()).getApplicableRegions(loc);
  96. return rset == null || rset.allows(useFlag, getWorldGuard().wrapPlayer(p));
  97. }
  98. public boolean canBuildInArea(Location loc, Player p) {
  99. return !useWorldGuard || loc == null || p == null || getWorldGuard() == null || !CraftBookPlugin.getInstance().getLocalConfiguration()
  100. .checkWGRegions || getWorldGuard().canBuild(p, loc);
  101. }
  102. /**
  103. * Called on load.
  104. */
  105. @Override
  106. public void onLoad() {
  107. }
  108. /**
  109. * Called when the plugin is enabled. This is where configuration is loaded,
  110. * and the plugin is setup.
  111. */
  112. @Override
  113. public void onEnable() {
  114. // Make the data folder for the plugin where configuration files
  115. // and other data files will be stored
  116. getDataFolder().mkdirs();
  117. createDefaultConfiguration("en_US.txt", true);
  118. createDefaultConfiguration("config.yml", false);
  119. //config = new BaseConfiguration(getConfig(), getDataFolder());
  120. //saveConfig();
  121. // init the util classes that need a plugin reference
  122. LocationUtil.init();
  123. logger.info(getDescription().getName() + " "
  124. + getDescription().getVersion() + " enabled.");
  125. // Prepare permissions
  126. PermissionsResolverManager.initialize(this);
  127. perms = PermissionsResolverManager.getInstance();
  128. if (getServer().getPluginManager().isPluginEnabled("WorldGuard")) {
  129. useWorldGuard = true;
  130. }
  131. }
  132. /**
  133. * Called when the plugin is disabled. Shutdown and clearing of any
  134. * temporary data occurs here.
  135. */
  136. @Override
  137. public void onDisable() {
  138. }
  139. /**
  140. * Register the events that are used.
  141. */
  142. protected abstract void registerEvents();
  143. /**
  144. * Register an event.
  145. *
  146. * @param listener
  147. */
  148. protected void registerEvents(Listener listener) {
  149. getServer().getPluginManager().registerEvents(listener, this);
  150. }
  151. protected void registerCommand(Class<?> clazz) {
  152. commandManager.register(clazz);
  153. }
  154. /**
  155. * Create a default configuration file from the .jar.
  156. *
  157. * @param name
  158. */
  159. protected void createDefaultConfiguration(String name, boolean force) {
  160. File actual = new File(getDataFolder(), name);
  161. if (!actual.exists() || force) {
  162. InputStream input =
  163. this.getClass().getResourceAsStream("/defaults/" + name);
  164. if (input != null) {
  165. FileOutputStream output = null;
  166. try {
  167. output = new FileOutputStream(actual);
  168. byte[] buf = new byte[8192];
  169. int length;
  170. while ((length = input.read(buf)) > 0) {
  171. output.write(buf, 0, length);
  172. }
  173. logger.info(getDescription().getName()
  174. + ": Default configuration file written: " + name);
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. } finally {
  178. try {
  179. input.close();
  180. } catch (IOException ignored) {
  181. }
  182. try {
  183. if (output != null) {
  184. output.close();
  185. }
  186. } catch (IOException ignored) {
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * Get a player.
  194. *
  195. * @param player Bukkit Player object
  196. *
  197. * @return a (new!) object wrapping Bukkit's player type with our own.
  198. */
  199. public LocalPlayer wrap(Player player) {
  200. return new BukkitPlayer(this, player);
  201. }
  202. /**
  203. * Checks permissions.
  204. *
  205. * @param sender
  206. * @param perm
  207. *
  208. * @return true if the sender has the requested permission, false otherwise
  209. */
  210. public boolean hasPermission(CommandSender sender, String perm) {
  211. if (!(sender instanceof Player)) return sender.isOp() && sender instanceof ConsoleCommandSender
  212. || perms.hasPermission(sender.getName(), perm);
  213. return hasPermission(sender, ((Player) sender).getWorld(), perm);
  214. }
  215. public boolean hasPermission(CommandSender sender, World world, String perm) {
  216. if (sender.isOp() && CraftBookPlugin.getInstance().getLocalConfiguration().opPerms || sender instanceof
  217. ConsoleCommandSender)
  218. return true;
  219. // Invoke the permissions resolver
  220. if (sender instanceof Player) {
  221. Player player = (Player) sender;
  222. return perms.hasPermission(world.getName(), player.getName(), perm);
  223. }
  224. return false;
  225. }
  226. public boolean isInGroup(String player, String group) {
  227. return perms.inGroup(player, group);
  228. }
  229. public LanguageManager getLanguageManager() {
  230. return languageManager;
  231. }
  232. public BaseConfiguration getLocalConfiguration() {
  233. return config;
  234. }
  235. /**
  236. * Handle a command.
  237. */
  238. @Override
  239. public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label,
  240. String[] args) {
  241. try {
  242. commands.execute(cmd.getName(), args, sender, sender);
  243. } catch (CommandPermissionsException e) {
  244. sender.sendMessage(ChatColor.RED + "You don't have permission.");
  245. } catch (MissingNestedCommandException e) {
  246. sender.sendMessage(ChatColor.RED + e.getUsage());
  247. } catch (CommandUsageException e) {
  248. sender.sendMessage(ChatColor.RED + e.getMessage());
  249. sender.sendMessage(ChatColor.RED + e.getUsage());
  250. } catch (WrappedCommandException e) {
  251. if (e.getCause() instanceof NumberFormatException) {
  252. sender.sendMessage(ChatColor.RED + "Number expected, string received instead.");
  253. } else {
  254. sender.sendMessage(ChatColor.RED + "An error has occurred. See console.");
  255. e.printStackTrace();
  256. }
  257. } catch (CommandException e) {
  258. sender.sendMessage(ChatColor.RED + e.getMessage());
  259. }
  260. return true;
  261. }
  262. public abstract void reloadConfiguration();
  263. }