MrNorbertPL24

Untitled

Jul 15th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.16 KB | None | 0 0
  1. /*
  2. * WorldGuard, a suite of tools for Minecraft
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldGuard team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20. package com.sk89q.worldguard.bukkit;
  21.  
  22. import com.sk89q.bukkit.util.CommandsManagerRegistration;
  23. import com.sk89q.minecraft.util.commands.CommandException;
  24. import com.sk89q.minecraft.util.commands.CommandPermissionsException;
  25. import com.sk89q.minecraft.util.commands.CommandUsageException;
  26. import com.sk89q.minecraft.util.commands.CommandsManager;
  27. import com.sk89q.minecraft.util.commands.MissingNestedCommandException;
  28. import com.sk89q.minecraft.util.commands.SimpleInjector;
  29. import com.sk89q.minecraft.util.commands.WrappedCommandException;
  30. import com.sk89q.wepif.PermissionsResolverManager;
  31. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  32. import com.sk89q.worldguard.LocalPlayer;
  33. import com.sk89q.worldguard.bukkit.commands.GeneralCommands;
  34. import com.sk89q.worldguard.bukkit.commands.ProtectionCommands;
  35. import com.sk89q.worldguard.bukkit.commands.ToggleCommands;
  36. import com.sk89q.worldguard.internal.listener.BlacklistListener;
  37. import com.sk89q.worldguard.internal.listener.ChestProtectionListener;
  38. import com.sk89q.worldguard.internal.listener.RegionProtectionListener;
  39. import com.sk89q.worldguard.protection.GlobalRegionManager;
  40. import com.sk89q.worldguard.protection.managers.RegionManager;
  41. import com.sk89q.worldguard.util.FatalConfigurationLoadingException;
  42. import org.bukkit.ChatColor;
  43. import org.bukkit.Location;
  44. import org.bukkit.World;
  45. import org.bukkit.World.Environment;
  46. import org.bukkit.block.Block;
  47. import org.bukkit.command.Command;
  48. import org.bukkit.command.CommandSender;
  49. import org.bukkit.command.ConsoleCommandSender;
  50. import org.bukkit.entity.Player;
  51. import org.bukkit.permissions.Permissible;
  52. import org.bukkit.plugin.Plugin;
  53. import org.bukkit.plugin.java.JavaPlugin;
  54.  
  55. import java.io.File;
  56. import java.io.FileNotFoundException;
  57. import java.io.FileOutputStream;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.util.ArrayList;
  61. import java.util.Arrays;
  62. import java.util.Iterator;
  63. import java.util.List;
  64. import java.util.Set;
  65. import java.util.jar.JarFile;
  66. import java.util.zip.ZipEntry;
  67.  
  68. /**
  69. * The main class for WorldGuard as a Bukkit plugin.
  70. *
  71. * @author sk89q
  72. */
  73. public class WorldGuardPlugin extends JavaPlugin {
  74.  
  75. /**
  76. * Current instance of this plugin.
  77. */
  78. private static WorldGuardPlugin inst;
  79.  
  80. /**
  81. * Manager for commands. This automatically handles nested commands,
  82. * permissions checking, and a number of other fancy command things.
  83. * We just set it up and register commands against it.
  84. */
  85. private final CommandsManager<CommandSender> commands;
  86.  
  87. /**
  88. * Handles the region databases for all worlds.
  89. */
  90. private final GlobalRegionManager globalRegionManager;
  91.  
  92. /**
  93. * Handles all configuration.
  94. */
  95. private final ConfigurationManager configuration;
  96.  
  97. /**
  98. * Used for scheduling flags.
  99. */
  100. private FlagStateManager flagStateManager;
  101.  
  102. /**
  103. * Construct objects. Actual loading occurs when the plugin is enabled, so
  104. * this merely instantiates the objects.
  105. */
  106. public WorldGuardPlugin() {
  107. configuration = new ConfigurationManager(this);
  108. globalRegionManager = new GlobalRegionManager(this);
  109.  
  110. final WorldGuardPlugin plugin = inst = this;
  111. commands = new CommandsManager<CommandSender>() {
  112. @Override
  113. public boolean hasPermission(CommandSender player, String perm) {
  114. return plugin.hasPermission(player, perm);
  115. }
  116. };
  117. }
  118.  
  119. /**
  120. * Get the current instance of WorldGuard
  121. * @return WorldGuardPlugin instance
  122. */
  123. public static WorldGuardPlugin inst() {
  124. return inst;
  125. }
  126.  
  127. /**
  128. * Called on plugin enable.
  129. */
  130. @Override
  131. @SuppressWarnings("deprecation")
  132. public void onEnable() {
  133.  
  134. // Set the proper command injector
  135. commands.setInjector(new SimpleInjector(this));
  136.  
  137. // Register command classes
  138. final CommandsManagerRegistration reg = new CommandsManagerRegistration(this, commands);
  139. reg.register(ToggleCommands.class);
  140. reg.register(ProtectionCommands.class);
  141.  
  142. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
  143. @Override
  144. public void run() {
  145. if (!getGlobalStateManager().hasCommandBookGodMode()) {
  146. reg.register(GeneralCommands.class);
  147. }
  148. }
  149. }, 0L);
  150.  
  151. // Need to create the plugins/WorldGuard folder
  152. getDataFolder().mkdirs();
  153.  
  154. PermissionsResolverManager.initialize(this);
  155.  
  156. // This must be done before configuration is loaded
  157. LegacyWorldGuardMigration.migrateBlacklist(this);
  158.  
  159. try {
  160. // Load the configuration
  161. configuration.load();
  162. globalRegionManager.preload();
  163. } catch (FatalConfigurationLoadingException e) {
  164. e.printStackTrace();
  165. getServer().shutdown();
  166. }
  167.  
  168. // Migrate regions after the regions were loaded because
  169. // the migration code reuses the loaded region managers
  170. LegacyWorldGuardMigration.migrateRegions(this);
  171.  
  172. flagStateManager = new FlagStateManager(this);
  173.  
  174. if (configuration.useRegionsScheduler) {
  175. getServer().getScheduler().scheduleSyncRepeatingTask(this, flagStateManager,
  176. FlagStateManager.RUN_DELAY, FlagStateManager.RUN_DELAY);
  177. }
  178.  
  179. // Register events
  180. (new WorldGuardPlayerListener(this)).registerEvents();
  181. (new WorldGuardBlockListener(this)).registerEvents();
  182. (new WorldGuardEntityListener(this)).registerEvents();
  183. (new WorldGuardWeatherListener(this)).registerEvents();
  184. (new WorldGuardVehicleListener(this)).registerEvents();
  185. (new WorldGuardServerListener(this)).registerEvents();
  186. (new WorldGuardHangingListener(this)).registerEvents();
  187.  
  188. // Modules
  189. (new BlacklistListener(this)).registerEvents();
  190. (new ChestProtectionListener(this)).registerEvents();
  191. (new RegionProtectionListener(this)).registerEvents();
  192.  
  193. configuration.updateCommandBookGodMode();
  194.  
  195. if (getServer().getPluginManager().isPluginEnabled("CommandBook")) {
  196. getServer().getPluginManager().registerEvents(new WorldGuardCommandBookListener(this), this);
  197. }
  198.  
  199. // handle worlds separately to initialize already loaded worlds
  200. WorldGuardWorldListener worldListener = (new WorldGuardWorldListener(this));
  201. for (World world : getServer().getWorlds()) {
  202. worldListener.initWorld(world);
  203. }
  204. worldListener.registerEvents();
  205.  
  206. if (!configuration.hasCommandBookGodMode()) {
  207. // Check god mode for existing players, if any
  208. for (Player player : getServer().getOnlinePlayers()) {
  209. if (inGroup(player, "wg-invincible") ||
  210. (configuration.autoGodMode && hasPermission(player, "worldguard.auto-invincible"))) {
  211. configuration.enableGodMode(player);
  212. }
  213. }
  214. }
  215. }
  216.  
  217. /**
  218. * Called on plugin disable.
  219. */
  220. @Override
  221. public void onDisable() {
  222. globalRegionManager.unload();
  223. configuration.unload();
  224. this.getServer().getScheduler().cancelTasks(this);
  225. }
  226.  
  227. /**
  228. * Handle a command.
  229. */
  230. @Override
  231. public boolean onCommand(CommandSender sender, Command cmd, String label,
  232. String[] args) {
  233. try {
  234. commands.execute(cmd.getName(), args, sender, sender);
  235. } catch (CommandPermissionsException e) {
  236. sender.sendMessage(ChatColor.RED + "You don't have permission.");
  237. } catch (MissingNestedCommandException e) {
  238. sender.sendMessage(ChatColor.RED + e.getUsage());
  239. } catch (CommandUsageException e) {
  240. sender.sendMessage(ChatColor.RED + e.getMessage());
  241. sender.sendMessage(ChatColor.RED + e.getUsage());
  242. } catch (WrappedCommandException e) {
  243. if (e.getCause() instanceof NumberFormatException) {
  244. sender.sendMessage(ChatColor.RED + "Number expected, string received instead.");
  245. } else {
  246. sender.sendMessage(ChatColor.RED + "An error has occurred. See console.");
  247. e.printStackTrace();
  248. }
  249. } catch (CommandException e) {
  250. sender.sendMessage(ChatColor.RED + e.getMessage());
  251. }
  252.  
  253. return true;
  254. }
  255.  
  256. /**
  257. * Get the GlobalRegionManager.
  258. *
  259. * @return The plugin's global region manager
  260. */
  261. public GlobalRegionManager getGlobalRegionManager() {
  262. return globalRegionManager;
  263. }
  264.  
  265. /**
  266. * Get the WorldGuard Configuration.
  267. *
  268. * @return ConfigurationManager
  269. * @deprecated Use {@link #getGlobalStateManager()} instead
  270. */
  271. @Deprecated
  272. public ConfigurationManager getGlobalConfiguration() {
  273. return getGlobalStateManager();
  274. }
  275.  
  276. /**
  277. * Gets the flag state manager.
  278. *
  279. * @return The flag state manager
  280. */
  281. public FlagStateManager getFlagStateManager() {
  282. return flagStateManager;
  283. }
  284.  
  285. /**
  286. * Get the global ConfigurationManager.
  287. * USe this to access global configuration values and per-world configuration values.
  288. * @return The global ConfigurationManager
  289. */
  290. public ConfigurationManager getGlobalStateManager() {
  291. return configuration;
  292. }
  293.  
  294. /**
  295. * Check whether a player is in a group.
  296. * This calls the corresponding method in PermissionsResolverManager
  297. *
  298. * @param player The player to check
  299. * @param group The group
  300. * @return whether {@code player} is in {@code group}
  301. */
  302. public boolean inGroup(Player player, String group) {
  303. try {
  304. return PermissionsResolverManager.getInstance().inGroup(player, group);
  305. } catch (Throwable t) {
  306. t.printStackTrace();
  307. return false;
  308. }
  309. }
  310.  
  311. /**
  312. * Get the groups of a player.
  313. * This calls the corresponding method in PermissionsResolverManager.
  314. * @param player The player to check
  315. * @return The names of each group the playe is in.
  316. */
  317. public String[] getGroups(Player player) {
  318. try {
  319. return PermissionsResolverManager.getInstance().getGroups(player);
  320. } catch (Throwable t) {
  321. t.printStackTrace();
  322. return new String[0];
  323. }
  324. }
  325.  
  326. /**
  327. * Gets the name of a command sender. This is a unique name and this
  328. * method should never return a "display name".
  329. *
  330. * @param sender The sender to get the name of
  331. * @return The unique name of the sender.
  332. */
  333. public String toUniqueName(CommandSender sender) {
  334. if (sender instanceof ConsoleCommandSender) {
  335. return "*Console*";
  336. } else {
  337. return sender.getName();
  338. }
  339. }
  340.  
  341. /**
  342. * Gets the name of a command sender. This play be a display name.
  343. *
  344. * @param sender The CommandSender to get the name of.
  345. * @return The name of the given sender
  346. */
  347. public String toName(CommandSender sender) {
  348. if (sender instanceof ConsoleCommandSender) {
  349. return "*Console*";
  350. } else if (sender instanceof Player) {
  351. return ((Player) sender).getDisplayName();
  352. } else {
  353. return sender.getName();
  354. }
  355. }
  356.  
  357. /**
  358. * Checks permissions.
  359. *
  360. * @param sender The sender to check the permission on.
  361. * @param perm The permission to check the permission on.
  362. * @return whether {@code sender} has {@code perm}
  363. */
  364. public boolean hasPermission(CommandSender sender, String perm) {
  365. if (sender.isOp()) {
  366. if (sender instanceof Player) {
  367. if (this.getGlobalStateManager().get(((Player) sender).
  368. getWorld()).opPermissions) {
  369. return true;
  370. }
  371. } else {
  372. return true;
  373. }
  374. }
  375.  
  376. // Invoke the permissions resolver
  377. if (sender instanceof Player) {
  378. Player player = (Player) sender;
  379. return PermissionsResolverManager.getInstance().hasPermission(player.getWorld().getName(), player.getName(), perm);
  380. }
  381.  
  382. return false;
  383. }
  384.  
  385. /**
  386. * Checks permissions and throws an exception if permission is not met.
  387. *
  388. * @param sender The sender to check the permission on.
  389. * @param perm The permission to check the permission on.
  390. * @throws CommandPermissionsException if {@code sender} doesn't have {@code perm}
  391. */
  392. public void checkPermission(CommandSender sender, String perm)
  393. throws CommandPermissionsException {
  394. if (!hasPermission(sender, perm)) {
  395. throw new CommandPermissionsException();
  396. }
  397. }
  398.  
  399. /**
  400. * Checks to see if the sender is a player, otherwise throw an exception.
  401. *
  402. * @param sender The {@link CommandSender} to check
  403. * @return {@code sender} casted to a player
  404. * @throws CommandException if {@code sender} isn't a {@link Player}
  405. */
  406. public Player checkPlayer(CommandSender sender)
  407. throws CommandException {
  408. if (sender instanceof Player) {
  409. return (Player) sender;
  410. } else {
  411. throw new CommandException("A player is expected.");
  412. }
  413. }
  414.  
  415. /**
  416. * Match player names.
  417. *
  418. * The filter string uses the following format:
  419. * @[name] looks up all players with the exact {@code name}
  420. * *[name] matches any player whose name contains {@code name}
  421. * [name] matches any player whose name starts with {@code name}
  422. *
  423. * @param filter The filter string to check.
  424. * @return A {@link List} of players who match {@code filter}
  425. */
  426. public List<Player> matchPlayerNames(String filter) {
  427. Player[] players = getServer().getOnlinePlayers();
  428.  
  429. filter = filter.toLowerCase();
  430.  
  431. // Allow exact name matching
  432. if (filter.charAt(0) == '@' && filter.length() >= 2) {
  433. filter = filter.substring(1);
  434.  
  435. for (Player player : players) {
  436. if (player.getName().equalsIgnoreCase(filter)) {
  437. List<Player> list = new ArrayList<Player>();
  438. list.add(player);
  439. return list;
  440. }
  441. }
  442.  
  443. return new ArrayList<Player>();
  444. // Allow partial name matching
  445. } else if (filter.charAt(0) == '*' && filter.length() >= 2) {
  446. filter = filter.substring(1);
  447.  
  448. List<Player> list = new ArrayList<Player>();
  449.  
  450. for (Player player : players) {
  451. if (player.getName().toLowerCase().contains(filter)) {
  452. list.add(player);
  453. }
  454. }
  455.  
  456. return list;
  457.  
  458. // Start with name matching
  459. } else {
  460. List<Player> list = new ArrayList<Player>();
  461.  
  462. for (Player player : players) {
  463. if (player.getName().toLowerCase().startsWith(filter)) {
  464. list.add(player);
  465. }
  466. }
  467.  
  468. return list;
  469. }
  470. }
  471.  
  472. /**
  473. * Checks if the given list of players is greater than size 0, otherwise
  474. * throw an exception.
  475. *
  476. * @param players The {@link List} to check
  477. * @return {@code players} as an {@link Iterable}
  478. * @throws CommandException If {@code players} is empty
  479. */
  480. protected Iterable<Player> checkPlayerMatch(List<Player> players)
  481. throws CommandException {
  482. // Check to see if there were any matches
  483. if (players.size() == 0) {
  484. throw new CommandException("No players matched query.");
  485. }
  486.  
  487. return players;
  488. }
  489.  
  490. /**
  491. * Matches players based on the specified filter string
  492. *
  493. * The filter string format is as follows:
  494. * * returns all the players currently online
  495. * If {@code sender} is a {@link Player}:
  496. * #world returns all players in the world that {@code sender} is in
  497. * #near reaturns all players within 30 blocks of {@code sender}'s location
  498. * Otherwise, the format is as specified in {@link #matchPlayerNames(String)}
  499. *
  500. * @param source The CommandSender who is trying to find a player
  501. * @param filter The filter string for players
  502. * @return iterator for players
  503. * @throws CommandException if no matches are found
  504. */
  505. public Iterable<Player> matchPlayers(CommandSender source, String filter)
  506. throws CommandException {
  507.  
  508. if (getServer().getOnlinePlayers().length == 0) {
  509. throw new CommandException("No players matched query.");
  510. }
  511.  
  512. if (filter.equals("*")) {
  513. return checkPlayerMatch(Arrays.asList(getServer().getOnlinePlayers()));
  514. }
  515.  
  516. // Handle special hash tag groups
  517. if (filter.charAt(0) == '#') {
  518. // Handle #world, which matches player of the same world as the
  519. // calling source
  520. if (filter.equalsIgnoreCase("#world")) {
  521. List<Player> players = new ArrayList<Player>();
  522. Player sourcePlayer = checkPlayer(source);
  523. World sourceWorld = sourcePlayer.getWorld();
  524.  
  525. for (Player player : getServer().getOnlinePlayers()) {
  526. if (player.getWorld().equals(sourceWorld)) {
  527. players.add(player);
  528. }
  529. }
  530.  
  531. return checkPlayerMatch(players);
  532.  
  533. // Handle #near, which is for nearby players.
  534. } else if (filter.equalsIgnoreCase("#near")) {
  535. List<Player> players = new ArrayList<Player>();
  536. Player sourcePlayer = checkPlayer(source);
  537. World sourceWorld = sourcePlayer.getWorld();
  538. org.bukkit.util.Vector sourceVector
  539. = sourcePlayer.getLocation().toVector();
  540.  
  541. for (Player player : getServer().getOnlinePlayers()) {
  542. if (player.getWorld().equals(sourceWorld)
  543. && player.getLocation().toVector().distanceSquared(
  544. sourceVector) < 900) {
  545. players.add(player);
  546. }
  547. }
  548.  
  549. return checkPlayerMatch(players);
  550.  
  551. } else {
  552. throw new CommandException("Invalid group '" + filter + "'.");
  553. }
  554. }
  555.  
  556. List<Player> players = matchPlayerNames(filter);
  557.  
  558. return checkPlayerMatch(players);
  559. }
  560.  
  561. /**
  562. * Match only a single player.
  563. *
  564. * @param sender The {@link CommandSender} who is requesting a player match
  565. * @param filter The filter string.
  566. * @see #matchPlayers(org.bukkit.entity.Player) for filter string syntax
  567. * @return The single player
  568. * @throws CommandException If more than one player match was found
  569. */
  570. public Player matchSinglePlayer(CommandSender sender, String filter)
  571. throws CommandException {
  572. // This will throw an exception if there are no matches
  573. Iterator<Player> players = matchPlayers(sender, filter).iterator();
  574.  
  575. Player match = players.next();
  576.  
  577. // We don't want to match the wrong person, so fail if if multiple
  578. // players were found (we don't want to just pick off the first one,
  579. // as that may be the wrong player)
  580. if (players.hasNext()) {
  581. throw new CommandException("More than one player found! " +
  582. "Use @<name> for exact matching.");
  583. }
  584.  
  585. return match;
  586. }
  587.  
  588. /**
  589. * Match only a single player or console.
  590. *
  591. * The filter string syntax is as follows:
  592. * #console, *console, or ! return the server console
  593. * All syntax from {@link #matchSinglePlayer(org.bukkit.command.CommandSender, String)}
  594. * @param sender The sender trying to match a CommandSender
  595. * @param filter The filter string
  596. * @return The resulting CommandSender
  597. * @throws CommandException if either zero or more than one player matched.
  598. */
  599. public CommandSender matchPlayerOrConsole(CommandSender sender, String filter)
  600. throws CommandException {
  601.  
  602. // Let's see if console is wanted
  603. if (filter.equalsIgnoreCase("#console")
  604. || filter.equalsIgnoreCase("*console*")
  605. || filter.equalsIgnoreCase("!")) {
  606. return getServer().getConsoleSender();
  607. }
  608.  
  609. return matchSinglePlayer(sender, filter);
  610. }
  611.  
  612. /**
  613. * Get a single player as an iterator for players.
  614. *
  615. * @param player The player to return in an Iterable
  616. * @return iterator for player
  617. */
  618. public Iterable<Player> matchPlayers(Player player) {
  619. return Arrays.asList(player);
  620. }
  621.  
  622. /**
  623. * Match a world.
  624. *
  625. * The filter string syntax is as follows:
  626. * #main returns the main world
  627. * #normal returns the first world with a normal environment
  628. * #nether return the first world with a nether environment
  629. * #player:[name] returns the world that a player named {@code name} is located in, if the player is online.
  630. * [name] A world with the name {@code name}
  631. *
  632. * @param sender The sender requesting a match
  633. * @param filter The filter string
  634. * @return The resulting world
  635. * @throws CommandException if no world matches
  636. */
  637. public World matchWorld(CommandSender sender, String filter) throws CommandException {
  638. List<World> worlds = getServer().getWorlds();
  639.  
  640. // Handle special hash tag groups
  641. if (filter.charAt(0) == '#') {
  642. // #main for the main world
  643. if (filter.equalsIgnoreCase("#main")) {
  644. return worlds.get(0);
  645.  
  646. // #normal for the first normal world
  647. } else if (filter.equalsIgnoreCase("#normal")) {
  648. for (World world : worlds) {
  649. if (world.getEnvironment() == Environment.NORMAL) {
  650. return world;
  651. }
  652. }
  653.  
  654. throw new CommandException("No normal world found.");
  655.  
  656. // #nether for the first nether world
  657. } else if (filter.equalsIgnoreCase("#nether")) {
  658. for (World world : worlds) {
  659. if (world.getEnvironment() == Environment.NETHER) {
  660. return world;
  661. }
  662. }
  663.  
  664. throw new CommandException("No nether world found.");
  665.  
  666. // Handle getting a world from a player
  667. } else if (filter.matches("^#player$")) {
  668. String parts[] = filter.split(":", 2);
  669.  
  670. // They didn't specify an argument for the player!
  671. if (parts.length == 1) {
  672. throw new CommandException("Argument expected for #player.");
  673. }
  674.  
  675. return matchPlayers(sender, parts[1]).iterator().next().getWorld();
  676. } else {
  677. throw new CommandException("Invalid identifier '" + filter + "'.");
  678. }
  679. }
  680.  
  681. for (World world : worlds) {
  682. if (world.getName().equals(filter)) {
  683. return world;
  684. }
  685. }
  686.  
  687. throw new CommandException("No world by that exact name found.");
  688. }
  689.  
  690. /**
  691. * Gets a copy of the WorldEdit plugin.
  692. *
  693. * @return The WorldEditPlugin instance
  694. * @throws CommandException If there is no WorldEditPlugin available
  695. */
  696. public WorldEditPlugin getWorldEdit() throws CommandException {
  697. Plugin worldEdit = getServer().getPluginManager().getPlugin("WorldEdit");
  698. if (worldEdit == null) {
  699. throw new CommandException("WorldEdit does not appear to be installed.");
  700. }
  701.  
  702. if (worldEdit instanceof WorldEditPlugin) {
  703. return (WorldEditPlugin) worldEdit;
  704. } else {
  705. throw new CommandException("WorldEdit detection failed (report error).");
  706. }
  707. }
  708.  
  709. /**
  710. * Wrap a player as a LocalPlayer.
  711. *
  712. * @param player The player to wrap
  713. * @return The wrapped player
  714. */
  715. public LocalPlayer wrapPlayer(Player player) {
  716. return new BukkitPlayer(this, player);
  717. }
  718.  
  719. /**
  720. * Create a default configuration file from the .jar.
  721. *
  722. * @param actual The destination file
  723. * @param defaultName The name of the file inside the jar's defaults folder
  724. */
  725. public void createDefaultConfiguration(File actual,
  726. String defaultName) {
  727.  
  728. // Make parent directories
  729. File parent = actual.getParentFile();
  730. if (!parent.exists()) {
  731. parent.mkdirs();
  732. }
  733.  
  734. if (actual.exists()) {
  735. return;
  736. }
  737.  
  738. InputStream input =
  739. null;
  740. try {
  741. JarFile file = new JarFile(getFile());
  742. ZipEntry copy = file.getEntry("defaults/" + defaultName);
  743. if (copy == null) throw new FileNotFoundException();
  744. input = file.getInputStream(copy);
  745. } catch (IOException e) {
  746. getLogger().severe("Unable to read default configuration: " + defaultName);
  747. }
  748.  
  749. if (input != null) {
  750. FileOutputStream output = null;
  751.  
  752. try {
  753. output = new FileOutputStream(actual);
  754. byte[] buf = new byte[8192];
  755. int length = 0;
  756. while ((length = input.read(buf)) > 0) {
  757. output.write(buf, 0, length);
  758. }
  759.  
  760. getLogger().info("Default configuration file written: "
  761. + actual.getAbsolutePath());
  762. } catch (IOException e) {
  763. e.printStackTrace();
  764. } finally {
  765. try {
  766. if (input != null) {
  767. input.close();
  768. }
  769. } catch (IOException ignore) {
  770. }
  771.  
  772. try {
  773. if (output != null) {
  774. output.close();
  775. }
  776. } catch (IOException ignore) {
  777. }
  778. }
  779. }
  780. }
  781.  
  782. /**
  783. * Notifies all with the worldguard.notify permission.
  784. * This will check both superperms and WEPIF,
  785. * but makes sure WEPIF checks don't result in duplicate notifications
  786. *
  787. * @param msg The notification to broadcast
  788. */
  789. public void broadcastNotification(String msg) {
  790. getServer().broadcast(msg, "worldguard.notify");
  791. Set<Permissible> subs = getServer().getPluginManager().getPermissionSubscriptions("worldguard.notify");
  792. for (Player player : getServer().getOnlinePlayers()) {
  793. if (!(subs.contains(player) && player.hasPermission("worldguard.notify")) &&
  794. hasPermission(player, "worldguard.notify")) { // Make sure the player wasn't already broadcasted to.
  795. player.sendMessage(msg);
  796. }
  797. }
  798. getLogger().info(msg);
  799. }
  800.  
  801. /**
  802. * Forgets a player.
  803. *
  804. * @param player The player to remove state information for
  805. */
  806. public void forgetPlayer(Player player) {
  807. flagStateManager.forget(player);
  808. }
  809.  
  810. /**
  811. * Checks to see if a player can build at a location. This will return
  812. * true if region protection is disabled.
  813. *
  814. * @param player The player to check.
  815. * @param loc The location to check at.
  816. * @see GlobalRegionManager#canBuild(org.bukkit.entity.Player, org.bukkit.Location)
  817. * @return whether {@code player} can build at {@code loc}
  818. */
  819. public boolean canBuild(Player player, Location loc) {
  820. return getGlobalRegionManager().canBuild(player, loc);
  821. }
  822.  
  823. /**
  824. * Checks to see if a player can build at a location. This will return
  825. * true if region protection is disabled.
  826. *
  827. * @param player The player to check
  828. * @param block The block to check at.
  829. * @see GlobalRegionManager#canBuild(org.bukkit.entity.Player, org.bukkit.block.Block)
  830. * @return whether {@code player} can build at {@code block}'s location
  831. */
  832. public boolean canBuild(Player player, Block block) {
  833. return getGlobalRegionManager().canBuild(player, block);
  834. }
  835.  
  836. /**
  837. * Gets the region manager for a world.
  838. *
  839. * @param world world to get the region manager for
  840. * @return the region manager or null if regions are not enabled
  841. */
  842. public RegionManager getRegionManager(World world) {
  843. if (!getGlobalStateManager().get(world).useRegions) {
  844. return null;
  845. }
  846.  
  847. return getGlobalRegionManager().get(world);
  848. }
  849.  
  850. /**
  851. * Replace macros in the text.
  852. *
  853. * The macros replaced are as follows:
  854. * %name%: The name of {@code sender}. See {@link #toName(org.bukkit.command.CommandSender)}
  855. * %id%: The unique name of the sender. See {@link #toUniqueName(org.bukkit.command.CommandSender)}
  856. * %online%: The number of players currently online on the server
  857. * If {@code sender} is a Player:
  858. * %world%: The name of the world {@code sender} is located in
  859. * %health%: The health of {@code sender}. See {@link org.bukkit.entity.Player#getHealth()}
  860. *
  861. * @param sender The sender to check
  862. * @param message The message to replace macros in
  863. * @return The message with macros replaced
  864. */
  865. public String replaceMacros(CommandSender sender, String message) {
  866. Player[] online = getServer().getOnlinePlayers();
  867.  
  868. message = message.replace("%name%", toName(sender));
  869. message = message.replace("%id%", toUniqueName(sender));
  870. message = message.replace("%online%", String.valueOf(online.length));
  871.  
  872. if (sender instanceof Player) {
  873. Player player = (Player) sender;
  874. World world = player.getWorld();
  875.  
  876. message = message.replace("%world%", world.getName());
  877. message = message.replace("%health%", String.valueOf(player.getHealth()));
  878. }
  879.  
  880. return message;
  881. }
  882. }
Advertisement
Add Comment
Please, Sign In to add comment