PageRenderTime 2489ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/sk89q/craftbook
Java | 185 lines | 84 code | 23 blank | 78 comment | 10 complexity | 413395cc719ff3c6b9c209eb5633a6fd 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 java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.logging.Logger;
  24. import org.bukkit.command.CommandSender;
  25. import org.bukkit.entity.Player;
  26. import org.bukkit.event.Event;
  27. import org.bukkit.event.Listener;
  28. import org.bukkit.event.Event.Priority;
  29. import org.bukkit.plugin.java.JavaPlugin;
  30. import com.sk89q.bukkit.migration.PermissionsResolverManager;
  31. import com.sk89q.craftbook.LocalPlayer;
  32. /**
  33. * Base plugin class for CraftBook for child CraftBook plugins.
  34. *
  35. * @author sk89q
  36. */
  37. public abstract class BaseBukkitPlugin extends JavaPlugin {
  38. /**
  39. * The permissions resolver in use.
  40. */
  41. private PermissionsResolverManager perms;
  42. /**
  43. * Logger for messages.
  44. */
  45. protected static final Logger logger = Logger.getLogger("Minecraft.CraftBook");
  46. /**
  47. * Called on load.
  48. */
  49. @Override
  50. public void onLoad() {
  51. }
  52. /**
  53. * Called when the plugin is enabled. This is where configuration is loaded,
  54. * and the plugin is setup.
  55. */
  56. public void onEnable() {
  57. logger.info(getDescription().getName() + " "
  58. + getDescription().getVersion() + " enabled.");
  59. // Make the data folder for the plugin where configuration files
  60. // and other data files will be stored
  61. getDataFolder().mkdirs();
  62. // Prepare permissions
  63. perms = new PermissionsResolverManager(
  64. getConfiguration(), getServer(), getDescription().getName(), logger);
  65. perms.load();
  66. // Register events
  67. registerEvents();
  68. }
  69. /**
  70. * Called when the plugin is disabled. Shutdown and clearing of any
  71. * temporary data occurs here.
  72. */
  73. public void onDisable() {
  74. }
  75. /**
  76. * Register the events that are used.
  77. */
  78. protected abstract void registerEvents();
  79. /**
  80. * Register an event.
  81. *
  82. * @param type
  83. * @param listener
  84. * @param priority
  85. */
  86. protected void registerEvent(Event.Type type, Listener listener, Priority priority) {
  87. getServer().getPluginManager()
  88. .registerEvent(type, listener, priority, this);
  89. }
  90. /**
  91. * Register an event at normal priority.
  92. *
  93. * @param type
  94. * @param listener
  95. */
  96. protected void registerEvent(Event.Type type, Listener listener) {
  97. getServer().getPluginManager()
  98. .registerEvent(type, listener, Priority.Normal, this);
  99. }
  100. /**
  101. * Create a default configuration file from the .jar.
  102. *
  103. * @param name
  104. */
  105. protected void createDefaultConfiguration(String name) {
  106. File actual = new File(getDataFolder(), name);
  107. if (!actual.exists()) {
  108. InputStream input =
  109. this.getClass().getResourceAsStream("/defaults/" + name);
  110. if (input != null) {
  111. FileOutputStream output = null;
  112. try {
  113. output = new FileOutputStream(actual);
  114. byte[] buf = new byte[8192];
  115. int length = 0;
  116. while ((length = input.read(buf)) > 0) {
  117. output.write(buf, 0, length);
  118. }
  119. logger.info(getDescription().getName()
  120. + ": Default configuration file written: " + name);
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. } finally {
  124. try {
  125. if (input != null)
  126. input.close();
  127. } catch (IOException e) {}
  128. try {
  129. if (output != null)
  130. output.close();
  131. } catch (IOException e) {}
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Get a player.
  138. *
  139. * @param player Bukkit Player object
  140. * @return
  141. */
  142. public LocalPlayer wrap(Player player) {
  143. return new BukkitPlayer(this, player);
  144. }
  145. /**
  146. * Checks permissions.
  147. *
  148. * @param sender
  149. * @param perm
  150. * @return
  151. */
  152. public boolean hasPermission(CommandSender sender, String perm) {
  153. if (sender.isOp()) {
  154. return true;
  155. }
  156. // Invoke the permissions resolver
  157. if (sender instanceof Player) {
  158. return perms.hasPermission(((Player) sender).getName(), perm);
  159. }
  160. return false;
  161. }
  162. }