Main.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. package de.Linus122.TimeIsMoney;
  2. import com.earth2me.essentials.Essentials;
  3. import de.Linus122.TimeIsMoney.tools.ActionBarUtils;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Location;
  6. import org.bukkit.Server;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.ConsoleCommandSender;
  9. import org.bukkit.command.PluginCommand;
  10. import org.bukkit.command.SimpleCommandMap;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.Plugin;
  14. import org.bukkit.plugin.PluginManager;
  15. import org.bukkit.plugin.RegisteredServiceProvider;
  16. import org.bukkit.plugin.SimplePluginManager;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. import org.bukkit.scheduler.BukkitWorker;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.FileReader;
  23. import java.io.IOException;
  24. import java.io.ObjectInputStream;
  25. import java.io.ObjectOutputStream;
  26. import java.io.PrintWriter;
  27. import java.lang.reflect.Field;
  28. import java.sql.Timestamp;
  29. import java.util.ArrayList;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.Iterator;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Random;
  37. import java.util.UUID;
  38. import static de.Linus122.TimeIsMoney.tools.Utils.CC;
  39. /**
  40. * The main class for TimeIsMoney
  41. *
  42. * @author Linus122
  43. * @since 1.9.6.1
  44. */
  45. public class Main extends JavaPlugin {
  46. /**
  47. * The economy being used by vault.
  48. */
  49. static net.milkbowl.vault.economy.Economy economy = null;
  50. /**
  51. * The actionbar utils class, null if not using a supported server version.
  52. */
  53. private static ActionBarUtils actionBarUtils = null;
  54. /**
  55. * The config version number.
  56. */
  57. private static final int CFG_VERSION = 12;
  58. /**
  59. * The TimeIsMoney version.
  60. */
  61. static String PL_VERSION;
  62. /**
  63. * The TimeIsMoney config.
  64. */
  65. static YamlConfiguration finalconfig;
  66. /**
  67. * The list of worlds where the payout feature will be disabled.
  68. */
  69. private static List<String> disabledWorlds;
  70. /**
  71. * The IPs of each UUID.
  72. */
  73. private static final HashMap<String, UUID> boundIPs = new HashMap<>();
  74. /**
  75. * The payouts listed in the config.
  76. */
  77. private final List<Payout> payouts = new ArrayList<>();
  78. /**
  79. * The payouts for the day.
  80. */
  81. private HashMap<String, Double> payedMoney = new HashMap<>();
  82. /**
  83. * The time online in seconds of each player by UUID.
  84. */
  85. private final HashMap<UUID, Integer> onlineSeconds = new HashMap<>();
  86. /**
  87. * The last location of each player by UUID.
  88. */
  89. private final HashMap<UUID, Location> lastLocation = new HashMap<>();
  90. /**
  91. * The chat message.
  92. */
  93. private String message;
  94. /**
  95. * The actionbar message.
  96. */
  97. private String messageActionbar;
  98. /**
  99. * The console logger.
  100. */
  101. private final ConsoleCommandSender clogger = this.getServer().getConsoleSender();
  102. /**
  103. * The current day.
  104. */
  105. private int currentDay = 0;
  106. /**
  107. * If actionbars are supported for the server's version.
  108. */
  109. private boolean useActionbars = true;
  110. /**
  111. * {@inheritDoc}
  112. */
  113. @SuppressWarnings({"deprecation", "unchecked"})
  114. @Override
  115. public void onEnable() {
  116. this.getCommand("timeismoney").setExecutor(new Cmd(this));
  117. PL_VERSION = this.getDescription().getVersion();
  118. currentDay = (new Date()).getDay();
  119. this.reloadConfig();
  120. File config = new File("plugins/TimeIsMoney/config.yml");
  121. if (config.exists()) {
  122. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(config);
  123. String old_config = "config_old " + cfg.getInt("configuration-version") + ".yml";
  124. if (cfg.contains("configuration-version")) {
  125. if (cfg.getInt("configuration-version") < CFG_VERSION) {
  126. clogger.sendMessage(CC("[TimeIsMoney] &cYOU ARE USING AN OLD CONFIG-VERSION. The plugin CANT work with this."));
  127. clogger.sendMessage(CC("[TimeIsMoney] &cI have created an new config for you. The old one is saved as config_old.yml."));
  128. config.renameTo(new File("plugins/TimeIsMoney/" + old_config));
  129. }
  130. }
  131. this.saveDefaultConfig();
  132. for (String key : cfg.getConfigurationSection("").getKeys(true)) {
  133. if (!this.getConfig().contains(key)) {
  134. this.getConfig().set(key, cfg.get(key));
  135. }
  136. }
  137. } else {
  138. this.saveDefaultConfig();
  139. }
  140. finalconfig = YamlConfiguration.loadConfiguration(config);
  141. disabledWorlds = getConfig().getStringList("disabled_in_worlds");
  142. if (getConfig().getBoolean("enable_atm")) new ATM(this);
  143. final int seconds = getConfig().getInt("give_money_every_second");
  144. Bukkit.getScheduler().runTaskTimer(this, () -> {
  145. try {
  146. for (Player p : Bukkit.getOnlinePlayers()) {
  147. if (disabledWorlds.contains(p.getWorld().getName())) continue;
  148. if (!boundIPs.containsKey(p.getAddress().getHostString())) {
  149. boundIPs.put(p.getAddress().getHostString(), p.getUniqueId());
  150. }
  151. if (onlineSeconds.containsKey(p.getUniqueId())) {
  152. onlineSeconds.put(p.getUniqueId(), onlineSeconds.get(p.getUniqueId()) + 1);
  153. } else {
  154. onlineSeconds.put(p.getUniqueId(), 1);
  155. }
  156. if (onlineSeconds.get(p.getUniqueId()) >= seconds) {
  157. pay(p);
  158. onlineSeconds.remove(p.getUniqueId());
  159. }
  160. }
  161. } catch (NullPointerException ignored) {
  162. }
  163. }, 20L, 20L);
  164. // Placeholder API
  165. if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
  166. new NamePlaceholder(this).register();
  167. }
  168. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
  169. if (currentDay != new Date().getDay()) { //Next day, clear payouts!
  170. log("Cleared all payouts");
  171. payedMoney.clear();
  172. currentDay = new Date().getDay();
  173. }
  174. }, 20L * 60, 20L * 60 * 15);
  175. setupEconomy();
  176. message = finalconfig.getString("message");
  177. message = CC(message);
  178. messageActionbar = finalconfig.getString("message_actionbar");
  179. messageActionbar = CC(messageActionbar);
  180. try {
  181. FileInputStream fis = new FileInputStream(new File("plugins/TimeIsMoney/payed_today.data"));
  182. ObjectInputStream ois = new ObjectInputStream(fis);
  183. payedMoney = (HashMap<String, Double>) ((HashMap<String, Double>) ois.readObject()).clone();
  184. ois.close();
  185. } catch (Exception ignored) {
  186. }
  187. loadPayouts();
  188. String packageName = this.getServer().getClass().getPackage().getName();
  189. // Get full package string of CraftServer.
  190. // org.bukkit.craftbukkit.version
  191. String Bukkitversion = packageName.substring(packageName.lastIndexOf('.') + 1);
  192. // Get the last element of the package
  193. try {
  194. final Class<?> clazz = Class.forName("de.Linus122.TimeIsMoney.version." + Bukkitversion + ".NBTUtils");
  195. // Check if we have a NMSHandler class at that location.
  196. if (ActionBarUtils.class.isAssignableFrom(clazz)) { // Make sure it actually implements NMS
  197. actionBarUtils = (ActionBarUtils) clazz.getConstructor().newInstance(); // Set our handler
  198. }
  199. } catch (final Exception e) {
  200. this.getLogger().severe("Actionbars are not supported on your spigot version, sorry.");
  201. useActionbars = false;
  202. return;
  203. }
  204. if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) {
  205. clogger.sendMessage("Time is Money: Essentials found. Hook in it -> Will use Essentials's AFK feature if afk is enabled.");
  206. }
  207. new Metrics(this);
  208. clogger.sendMessage(CC("&aTime is Money &2v" + PL_VERSION + " &astarted."));
  209. }
  210. /**
  211. * {@inheritDoc}
  212. */
  213. @Override
  214. public void onDisable() {
  215. FileOutputStream fos;
  216. try {
  217. fos = new FileOutputStream(new File("plugins/TimeIsMoney/payed_today.data"));
  218. ObjectOutputStream oos = new ObjectOutputStream(fos);
  219. oos.writeObject(payedMoney);
  220. oos.close();
  221. } catch (Exception ignored) {
  222. }
  223. }
  224. /**
  225. * Reloads TimeIsMoney.
  226. */
  227. void reload() {
  228. // cancelling current tasks
  229. for (BukkitWorker bw: Bukkit.getScheduler().getActiveWorkers()) {
  230. if (bw.getOwner() == this) {
  231. Bukkit.getScheduler().cancelTask(bw.getTaskId());
  232. }
  233. }
  234. Bukkit.getPluginManager().disablePlugin(this);
  235. Bukkit.getPluginManager().enablePlugin(this);
  236. }
  237. /**
  238. * Loads the payouts.
  239. */
  240. private void loadPayouts() {
  241. try {
  242. payouts.clear();
  243. for (String key : finalconfig.getConfigurationSection("payouts").getKeys(false)) {
  244. Payout payout = new Payout();
  245. payout.max_payout_per_day = finalconfig.getDouble("payouts." + key + ".max_payout_per_day");
  246. payout.payout_amount = finalconfig.getDouble("payouts." + key + ".payout_amount");
  247. if (finalconfig.getString("payouts." + key + ".permission") != null) {
  248. payout.permission = finalconfig.getString("payouts." + key + ".permission");
  249. }
  250. if (finalconfig.getString("payouts." + key + ".commands") != null) {
  251. payout.commands = finalconfig.getStringList("payouts." + key + ".commands");
  252. }
  253. if (finalconfig.isSet("payouts." + key + ".commands_if_afk")) {
  254. payout.commands_if_afk = finalconfig.getStringList("payouts." + key + ".commands_if_afk");
  255. }
  256. if (finalconfig.getString("payouts." + key + ".chance") != null) {
  257. payout.chance = finalconfig.getInt("payouts." + key + ".chance");
  258. }
  259. payouts.add(payout);
  260. }
  261. clogger.sendMessage(CC("[TimeIsMoney] &aLoaded " + finalconfig.getConfigurationSection("payouts").getKeys(false).size() + " Payouts!"));
  262. } catch (Exception e) {
  263. clogger.sendMessage(CC("[TimeIsMoney] &aFailed to load Payouts! (May made a mistake in config.yml?)"));
  264. }
  265. }
  266. /**
  267. * Sets up the vault economy.
  268. *
  269. * @return True if the economy was set up, false otherwise.
  270. */
  271. private boolean setupEconomy() {
  272. RegisteredServiceProvider<net.milkbowl.vault.economy.Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
  273. if (economyProvider != null) {
  274. economy = economyProvider.getProvider();
  275. }
  276. return economy != null;
  277. }
  278. /**
  279. * Gets the payout for the specified player.
  280. *
  281. * @param p The player to get the payout of.
  282. * @return The payout of the player.
  283. */
  284. private Payout getPayOutForPlayer(Player p) {
  285. Payout finalpayout = null;
  286. if (!this.getConfig().getBoolean("choose-payout-by-chance")) {
  287. //by Permission
  288. for (Payout payout : payouts) {
  289. if (payout.permission.equalsIgnoreCase("")) finalpayout = payout;
  290. if (p.hasPermission(payout.permission)) {
  291. finalpayout = payout;
  292. }
  293. }
  294. } else {
  295. //by Chance
  296. Random rnd = new Random();
  297. List<Payout> list = new ArrayList<>();
  298. for (Payout payout : payouts) {
  299. for (int i = 0; i < payout.chance; i++) list.add(payout);
  300. }
  301. finalpayout = list.get(rnd.nextInt(list.size() - 1));
  302. }
  303. return finalpayout;
  304. }
  305. /**
  306. * Pays the specified player.
  307. *
  308. * @param p The player to pay.
  309. */
  310. @SuppressWarnings("deprecation")
  311. private void pay(Player p) {
  312. if (p == null) return;
  313. //REACHED MAX PAYOUT CHECK
  314. double payed = 0;
  315. if (payedMoney.containsKey(p.getName())) {
  316. payed = payedMoney.get(p.getName());
  317. }
  318. Payout payout = getPayOutForPlayer(p);
  319. if (payout == null) return;
  320. if (payout.max_payout_per_day != -1) {
  321. if (payed >= payout.max_payout_per_day) { //Reached max payout
  322. if (finalconfig.getBoolean("display-messages-in-chat")) {
  323. sendMessage(p, finalconfig.getString("message_payoutlimit_reached"));
  324. }
  325. if (finalconfig.getBoolean("display-messages-in-actionbar") && useActionbars) {
  326. sendActionbar(p, finalconfig.getString("message_payoutlimit_reached_actionbar"));
  327. }
  328. return;
  329. }
  330. }
  331. if (!finalconfig.getBoolean("allow-multiple-accounts")) {
  332. if (boundIPs.containsKey(p.getAddress().getHostString())) {
  333. if (!boundIPs.get(p.getAddress().getHostString()).equals(p.getUniqueId())) {
  334. sendMessage(p, finalconfig.getString("message_multiple_ips"));
  335. return;
  336. }
  337. }
  338. }
  339. //AFK CHECK
  340. boolean afk = false;
  341. double afkPercent = 0.0D;
  342. if (!p.hasPermission("tim.afkbypass")) {
  343. //ESENTIALS_AFK_FEATURE
  344. if (Bukkit.getServer().getPluginManager().isPluginEnabled("Essentials")) {
  345. Essentials essentials = (com.earth2me.essentials.Essentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials");
  346. if (essentials.getUser(p).isAfk()) {
  347. afk = true;
  348. }
  349. } else {
  350. //PLUGIN_AFK_FEATURE
  351. if (lastLocation.containsKey(p.getUniqueId())) { //AntiAFK
  352. if (lastLocation.get(p.getUniqueId()).getX() == p.getLocation().getX() && lastLocation.get(p.getUniqueId()).getY() == p.getLocation().getY() && lastLocation.get(p.getUniqueId()).getZ() == p.getLocation().getZ() || lastLocation.get(p.getUniqueId()).getYaw() == p.getLocation().getYaw()) {
  353. afk = true;
  354. }
  355. }
  356. }
  357. if (afk) {
  358. if (!finalconfig.getBoolean("afk_payout")) { // Payout is disabled
  359. if (finalconfig.getBoolean("display-messages-in-chat")) {
  360. sendMessage(p, finalconfig.getString("message_afk"));
  361. }
  362. if (finalconfig.getBoolean("display-messages-in-actionbar") && useActionbars) {
  363. sendActionbar(p, finalconfig.getString("message_afk_actionbar"));
  364. }
  365. return;
  366. } else { // Payout is enabled
  367. if (!finalconfig.isSet("afk_payout_percent")) {
  368. afkPercent = 100; // Payout % isn't set (older config), so assume 100% as before
  369. } else {
  370. afkPercent = finalconfig.getDouble("afk_payout_percent");
  371. }
  372. }
  373. }
  374. }
  375. //DEPOSIT
  376. double payout_amt = afk ? payout.payout_amount * (afkPercent / 100) : payout.payout_amount;
  377. if (finalconfig.getBoolean("store-money-in-bank")) {
  378. ATM.depositBank(p, payout_amt);
  379. } else {
  380. double before = 0;
  381. if (economy.hasAccount(p)) {
  382. before = economy.getBalance(p);
  383. }
  384. economy.depositPlayer(p, payout_amt);
  385. log(p.getName() + ": Deposited: " + payout_amt + " Balance-before: " + before + " Balance-now: " + economy.getBalance(p));
  386. }
  387. if (!afk) {
  388. if (finalconfig.getBoolean("display-messages-in-chat")) {
  389. sendMessage(p, message.replace("%money%", economy.format(payout_amt)));
  390. }
  391. if (finalconfig.getBoolean("display-messages-in-actionbar") && useActionbars) {
  392. sendActionbar(p, messageActionbar.replace("%money%", economy.format(payout_amt)));
  393. }
  394. for (String cmd : payout.commands) {
  395. dispatchCommandSync(cmd.replace("/", "").replaceAll("%player%", p.getName()));
  396. }
  397. } else {
  398. if (finalconfig.getBoolean("display-messages-in-chat") && finalconfig.isSet("message_afk_payout")) {
  399. sendMessage(p, CC(finalconfig.getString("message_afk_payout").replace("%money%", economy.format(payout_amt)).replace("%percent%", "" + afkPercent)));
  400. }
  401. if (finalconfig.getBoolean("display-messages-in-actionbar") && finalconfig.isSet("message_afk_actionbar_payout") && useActionbars) {
  402. sendActionbar(p, CC(finalconfig.getString("message_afk_actionbar_payout").replace("%money%", economy.format(payout_amt)).replace("%percent%", "" + afkPercent)));
  403. }
  404. for (String cmd : payout.commands_if_afk) {
  405. dispatchCommandSync(cmd.replace("/", "").replaceAll("%player%", p.getName()));
  406. }
  407. }
  408. //ADD PAYED MONEY
  409. if (payedMoney.containsKey(p.getName())) {
  410. payedMoney.put(p.getName(), payedMoney.get(p.getName()) + payout_amt);
  411. } else {
  412. payedMoney.put(p.getName(), payout_amt);
  413. }
  414. lastLocation.put(p.getUniqueId(), p.getLocation());
  415. }
  416. /**
  417. * Dispatches a command as sync.
  418. *
  419. * @param cmd The command to dispatch sync.
  420. */
  421. private void dispatchCommandSync(final String cmd) {
  422. final Server server = this.getServer();
  423. this.getServer().getScheduler().runTask(this, () -> server.dispatchCommand(server.getConsoleSender(), cmd));
  424. }
  425. /**
  426. * Logs debug information if enabled in the config.
  427. *
  428. * @param msg The debug message to log.
  429. */
  430. @SuppressWarnings("deprecation")
  431. private void log(String msg) {
  432. if (!this.getConfig().getBoolean("debug-log")) {
  433. return;
  434. }
  435. Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
  436. File file = new File("plugins/TimeIsMoney/log.txt");
  437. try {
  438. if (!file.exists()) {
  439. file.createNewFile();
  440. }
  441. FileReader pr = new FileReader(file);
  442. int number;
  443. StringBuffer text = new StringBuffer();
  444. while ((number = pr.read()) != -1) {
  445. text.append((char) number);
  446. }
  447. text.append(currentTimestamp.toGMTString()).append(":").append(msg).append("\n");
  448. PrintWriter pw = new PrintWriter(file);
  449. pw.print(text);
  450. pw.close();
  451. } catch (IOException e) {
  452. e.printStackTrace();
  453. }
  454. }
  455. /**
  456. * Sends a chat message that supports color codes to the specified player.
  457. *
  458. * @param p The player to send the chat message to.
  459. * @param msg The message the chat should give the player.
  460. */
  461. private void sendMessage(Player p, String msg) {
  462. if (msg == null) return;
  463. if (msg.length() == 0) return;
  464. p.sendMessage(CC(msg));
  465. }
  466. /**
  467. * Sends an actionbar message to the specified player.
  468. *
  469. * @param p The player to send the actionbar message to.
  470. * @param msg The message the actionbar should give to the player.
  471. */
  472. private void sendActionbar(final Player p, final String msg) {
  473. if (msg.length() == 0) return;
  474. int times = finalconfig.getInt("display-messages-in-actionbar-time");
  475. if (times == 1) {
  476. if (actionBarUtils != null) {
  477. actionBarUtils.sendActionBarMessage(p, msg);
  478. }
  479. } else if (times > 1) {
  480. if (actionBarUtils != null) {
  481. actionBarUtils.sendActionBarMessage(p, msg);
  482. }
  483. times--;
  484. for (int i = 0; i < times; i++) {
  485. Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> actionBarUtils.sendActionBarMessage(p, CC(msg)), 20L * i);
  486. }
  487. }
  488. }
  489. /**
  490. * Unloads the specified plugin.
  491. *
  492. * @param pluginName The name of the plugin to unload.
  493. * @return True if the specified plugin was unloaded, false otherwise.
  494. */
  495. private boolean unloadPlugin(String pluginName)
  496. throws Exception {
  497. PluginManager manager = getServer().getPluginManager();
  498. SimplePluginManager spmanager = (SimplePluginManager) manager;
  499. if (spmanager != null) {
  500. Field pluginsField = spmanager.getClass().getDeclaredField("plugins");
  501. pluginsField.setAccessible(true);
  502. List<Plugin> plugins = (List) pluginsField.get(spmanager);
  503. Field lookupNamesField = spmanager.getClass().getDeclaredField("lookupNames");
  504. lookupNamesField.setAccessible(true);
  505. Map<String, Plugin> lookupNames = (Map) lookupNamesField.get(spmanager);
  506. Field commandMapField = spmanager.getClass().getDeclaredField("commandMap");
  507. commandMapField.setAccessible(true);
  508. SimpleCommandMap commandMap = (SimpleCommandMap) commandMapField.get(spmanager);
  509. Field knownCommandsField;
  510. Map<String, Command> knownCommands = null;
  511. if (commandMap != null) {
  512. knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
  513. knownCommandsField.setAccessible(true);
  514. knownCommands = (Map) knownCommandsField.get(commandMap);
  515. }
  516. Plugin plugin;
  517. Iterator<Map.Entry<String, Command>> it;
  518. for (Plugin plugin1 : manager.getPlugins()) {
  519. if (plugin1.getDescription().getName().equalsIgnoreCase(pluginName)) {
  520. manager.disablePlugin(plugin1);
  521. if ((plugins != null) && (plugins.contains(plugin1))) {
  522. plugins.remove(plugin1);
  523. }
  524. if ((lookupNames != null) && (lookupNames.containsKey(pluginName))) {
  525. lookupNames.remove(pluginName);
  526. }
  527. if (commandMap != null) {
  528. for (it = knownCommands.entrySet().iterator(); it.hasNext(); ) {
  529. Map.Entry<String, Command> entry = it.next();
  530. if ((entry.getValue() instanceof PluginCommand)) {
  531. PluginCommand command = (PluginCommand) entry.getValue();
  532. if (command.getPlugin() == plugin1) {
  533. command.unregister(commandMap);
  534. it.remove();
  535. }
  536. }
  537. }
  538. }
  539. }
  540. }
  541. } else {
  542. return true;
  543. }
  544. return true;
  545. }
  546. /**
  547. * Loads the specified plugin.
  548. *
  549. * @param pluginName The name of the plugin to load.
  550. * @return True if the specified plugin was loaded, false otherwise.
  551. */
  552. private boolean loadPlugin(String pluginName) {
  553. try {
  554. PluginManager manager = getServer().getPluginManager();
  555. Plugin plugin = manager.loadPlugin(new File("plugins", pluginName + ".jar"));
  556. if (plugin == null) {
  557. return false;
  558. }
  559. plugin.onLoad();
  560. manager.enablePlugin(plugin);
  561. } catch (Exception e) {
  562. return false;
  563. }
  564. return true;
  565. }
  566. /**
  567. * Reloads the specified plugin.
  568. *
  569. * @param pluginName The name of the plugin to reload.
  570. * @return True if the plugin was reloaded, false otherwise.
  571. * @throws Exception If an error occurred while loading or unloading the specified plugin.
  572. */
  573. private boolean reloadPlugin(String pluginName)
  574. throws Exception {
  575. boolean unload = unloadPlugin(pluginName);
  576. boolean load = loadPlugin(pluginName);
  577. return unload && load;
  578. }
  579. }