PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/cn/nukkit/plugin/PluginBase.java

https://gitlab.com/Skull3x/Nukkit
Java | 294 lines | 175 code | 43 blank | 76 comment | 29 complexity | 81254beda46ad4ae371e9e4aadcfa0d2 MD5 | raw file
  1. package cn.nukkit.plugin;
  2. import cn.nukkit.Server;
  3. import cn.nukkit.command.Command;
  4. import cn.nukkit.command.CommandSender;
  5. import cn.nukkit.command.PluginIdentifiableCommand;
  6. import cn.nukkit.utils.Config;
  7. import cn.nukkit.utils.Utils;
  8. import com.google.common.base.Preconditions;
  9. import org.yaml.snakeyaml.DumperOptions;
  10. import org.yaml.snakeyaml.Yaml;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.LinkedHashMap;
  15. /**
  16. * 一般的Nukkit插件需要继承的类。<br>
  17. * A class to be extended by a normal Nukkit plugin.
  18. *
  19. * @author MagicDroidX(code) @ Nukkit Project
  20. * @author 粉鞋大妈(javadoc) @ Nukkit Project
  21. * @see cn.nukkit.plugin.PluginDescription
  22. * @since Nukkit 1.0 | Nukkit API 1.0.0
  23. */
  24. abstract public class PluginBase implements Plugin {
  25. private PluginLoader loader;
  26. private Server server;
  27. private boolean isEnabled = false;
  28. private boolean initialized = false;
  29. private PluginDescription description;
  30. private File dataFolder;
  31. private Config config;
  32. private File configFile;
  33. private File file;
  34. private PluginLogger logger;
  35. public void onLoad() {
  36. }
  37. public void onEnable() {
  38. }
  39. public void onDisable() {
  40. }
  41. public final boolean isEnabled() {
  42. return isEnabled;
  43. }
  44. /**
  45. * 加载这个插件。<br>
  46. * Enables this plugin.
  47. * <p>
  48. * <p>如果你需要卸载这个插件,建议使用{@link #setEnabled(boolean)}<br>
  49. * If you need to disable this plugin, it's recommended to use {@link #setEnabled(boolean)}</p>
  50. *
  51. * @since Nukkit 1.0 | Nukkit API 1.0.0
  52. */
  53. public final void setEnabled() {
  54. this.setEnabled(true);
  55. }
  56. /**
  57. * 加载或卸载这个插件。<br>
  58. * Enables or disables this plugin.
  59. * <p>
  60. * <p>插件管理器插件常常使用这个方法。<br>
  61. * It's normally used by a plugin manager plugin to manage plugins.</p>
  62. *
  63. * @param value {@code true}为加载,{@code false}为卸载。<br>{@code true} for enable, {@code false} for disable.
  64. * @since Nukkit 1.0 | Nukkit API 1.0.0
  65. */
  66. public final void setEnabled(boolean value) {
  67. if (isEnabled != value) {
  68. isEnabled = value;
  69. if (isEnabled) {
  70. onEnable();
  71. } else {
  72. onDisable();
  73. }
  74. }
  75. }
  76. public final boolean isDisabled() {
  77. return !isEnabled;
  78. }
  79. public final File getDataFolder() {
  80. return dataFolder;
  81. }
  82. public final PluginDescription getDescription() {
  83. return description;
  84. }
  85. /**
  86. * 初始化这个插件。<br>
  87. * Initialize the plugin.
  88. * <p>
  89. * <p>这个方法会在加载(load)之前被插件加载器调用,初始化关于插件的一些事项,不能被重写。<br>
  90. * Called by plugin loader before load, and initialize the plugin. Can't be overridden.</p>
  91. *
  92. * @param loader 加载这个插件的插件加载器的{@code PluginLoader}对象。<br>
  93. * The plugin loader ,which loads this plugin, as a {@code PluginLoader} object.
  94. * @param server 运行这个插件的服务器的{@code Server}对象。<br>
  95. * The server running this plugin, as a {@code Server} object.
  96. * @param description 描述这个插件的{@code PluginDescription}对象。<br>
  97. * A {@code PluginDescription} object that describes this plugin.
  98. * @param dataFolder 这个插件的数据的文件夹。<br>
  99. * The data folder of this plugin.
  100. * @param file 这个插件的文件{@code File}对象。对于jar格式的插件,就是jar文件本身。<br>
  101. * The {@code File} object of this plugin itself. For jar-packed plugins, it is the jar file itself.
  102. * @since Nukkit 1.0 | Nukkit API 1.0.0
  103. */
  104. public final void init(PluginLoader loader, Server server, PluginDescription description, File dataFolder, File file) {
  105. if (!initialized) {
  106. initialized = true;
  107. this.loader = loader;
  108. this.server = server;
  109. this.description = description;
  110. this.dataFolder = dataFolder;
  111. this.file = file;
  112. this.configFile = new File(this.dataFolder, "config.yml");
  113. this.logger = new PluginLogger(this);
  114. }
  115. }
  116. public PluginLogger getLogger() {
  117. return logger;
  118. }
  119. /**
  120. * 返回这个插件是否已经初始化。<br>
  121. * Returns if this plugin is initialized.
  122. *
  123. * @return 这个插件是否已初始化。<br>if this plugin is initialized.
  124. * @since Nukkit 1.0 | Nukkit API 1.0.0
  125. */
  126. public final boolean isInitialized() {
  127. return initialized;
  128. }
  129. /**
  130. * TODO: FINISH JAVADOC
  131. */
  132. public PluginIdentifiableCommand getCommand(String name) {
  133. PluginIdentifiableCommand command = this.getServer().getPluginCommand(name);
  134. if (command == null || !command.getPlugin().equals(this)) {
  135. command = this.getServer().getPluginCommand(this.description.getName().toLowerCase() + ":" + name);
  136. }
  137. if (command != null && command.getPlugin().equals(this)) {
  138. return command;
  139. } else {
  140. return null;
  141. }
  142. }
  143. @Override
  144. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  145. return false;
  146. }
  147. @Override
  148. public InputStream getResource(String filename) {
  149. return this.getClass().getClassLoader().getResourceAsStream(filename);
  150. }
  151. @Override
  152. public boolean saveResource(String filename) {
  153. return saveResource(filename, false);
  154. }
  155. @Override
  156. public boolean saveResource(String filename, boolean replace) {
  157. return saveResource(filename, filename, replace);
  158. }
  159. @Override
  160. public boolean saveResource(String filename, String outputName, boolean replace) {
  161. Preconditions.checkArgument(filename != null && outputName != null, "Filename can not be null!");
  162. Preconditions.checkArgument(filename.trim().length() != 0 && outputName.trim().length() != 0, "Filename can not be empty!");
  163. File out = new File(dataFolder, outputName);
  164. if (!out.exists() || replace) {
  165. try (InputStream resource = getResource(filename)) {
  166. if (resource != null) {
  167. File outFolder = out.getParentFile();
  168. if (!outFolder.exists()) {
  169. outFolder.mkdirs();
  170. }
  171. Utils.writeFile(out, resource);
  172. return true;
  173. }
  174. } catch (IOException e) {
  175. Server.getInstance().getLogger().logException(e);
  176. }
  177. }
  178. return false;
  179. }
  180. @Override
  181. public Config getConfig() {
  182. if (this.config == null) {
  183. this.reloadConfig();
  184. }
  185. return this.config;
  186. }
  187. @Override
  188. public void saveConfig() {
  189. if (!this.getConfig().save()) {
  190. this.getLogger().critical("Could not save config to " + this.configFile.toString());
  191. }
  192. }
  193. @Override
  194. public void saveDefaultConfig() {
  195. if (!this.configFile.exists()) {
  196. this.saveResource("config.yml", false);
  197. }
  198. }
  199. @Override
  200. public void reloadConfig() {
  201. this.config = new Config(this.configFile);
  202. InputStream configStream = this.getResource("config.yml");
  203. if (configStream != null) {
  204. DumperOptions dumperOptions = new DumperOptions();
  205. dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  206. Yaml yaml = new Yaml(dumperOptions);
  207. try {
  208. this.config.setDefault(yaml.loadAs(Utils.readFile(this.configFile), LinkedHashMap.class));
  209. } catch (IOException e) {
  210. Server.getInstance().getLogger().logException(e);
  211. }
  212. }
  213. }
  214. @Override
  215. public Server getServer() {
  216. return server;
  217. }
  218. @Override
  219. public String getName() {
  220. return this.description.getName();
  221. }
  222. /**
  223. * 返回这个插件完整的名字。<br>
  224. * Returns the full name of this plugin.
  225. * <p>
  226. * <p>一个插件完整的名字由{@code 名字+" v"+版本号}组成。比如:<br>
  227. * A full name of a plugin is composed by {@code name+" v"+version}.for example:</p>
  228. * <p>{@code HelloWorld v1.0.0}</p>
  229. *
  230. * @return 这个插件完整的名字。<br>The full name of this plugin.
  231. * @see cn.nukkit.plugin.PluginDescription#getFullName
  232. * @since Nukkit 1.0 | Nukkit API 1.0.0
  233. */
  234. public final String getFullName() {
  235. return this.description.getFullName();
  236. }
  237. /**
  238. * 返回这个插件的文件{@code File}对象。对于jar格式的插件,就是jar文件本身。<br>
  239. * Returns the {@code File} object of this plugin itself. For jar-packed plugins, it is the jar file itself.
  240. *
  241. * @return 这个插件的文件 {@code File}对象。<br>The {@code File} object of this plugin itself.
  242. * @since Nukkit 1.0 | Nukkit API 1.0.0
  243. */
  244. protected File getFile() {
  245. return file;
  246. }
  247. @Override
  248. public PluginLoader getPluginLoader() {
  249. return this.loader;
  250. }
  251. }