PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java

https://github.com/talmor/worldguard
Java | 1099 lines | 721 code | 130 blank | 248 comment | 282 complexity | 5c15703e86a5ef1bd6603b6101898b8e MD5 | raw file
  1. // $Id$
  2. /*
  3. * WorldGuard
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the 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,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.bukkit;
  20. import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
  21. import java.util.Iterator;
  22. import java.util.Set;
  23. import java.util.logging.Logger;
  24. import org.bukkit.ChatColor;
  25. import org.bukkit.Location;
  26. import org.bukkit.Material;
  27. import org.bukkit.World;
  28. import org.bukkit.block.Block;
  29. import org.bukkit.entity.Entity;
  30. import org.bukkit.entity.Item;
  31. import org.bukkit.entity.Player;
  32. import org.bukkit.event.Event;
  33. import org.bukkit.event.Event.Priority;
  34. import org.bukkit.event.Event.Result;
  35. import org.bukkit.event.block.Action;
  36. import org.bukkit.event.player.PlayerBedEnterEvent;
  37. import org.bukkit.event.player.PlayerBucketEmptyEvent;
  38. import org.bukkit.event.player.PlayerBucketFillEvent;
  39. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  40. import org.bukkit.event.player.PlayerDropItemEvent;
  41. import org.bukkit.event.player.PlayerInteractEvent;
  42. import org.bukkit.event.player.PlayerItemHeldEvent;
  43. import org.bukkit.event.player.PlayerJoinEvent;
  44. import org.bukkit.event.player.PlayerListener;
  45. import org.bukkit.event.player.PlayerMoveEvent;
  46. import org.bukkit.event.player.PlayerPickupItemEvent;
  47. import org.bukkit.event.player.PlayerQuitEvent;
  48. import org.bukkit.event.player.PlayerRespawnEvent;
  49. import org.bukkit.inventory.ItemStack;
  50. import org.bukkit.plugin.PluginManager;
  51. import com.sk89q.worldedit.Vector;
  52. import com.sk89q.worldedit.blocks.BlockType;
  53. import com.sk89q.worldguard.LocalPlayer;
  54. import com.sk89q.worldguard.blacklist.events.BlockBreakBlacklistEvent;
  55. import com.sk89q.worldguard.blacklist.events.BlockInteractBlacklistEvent;
  56. import com.sk89q.worldguard.blacklist.events.BlockPlaceBlacklistEvent;
  57. import com.sk89q.worldguard.blacklist.events.ItemAcquireBlacklistEvent;
  58. import com.sk89q.worldguard.blacklist.events.ItemDropBlacklistEvent;
  59. import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
  60. import com.sk89q.worldguard.bukkit.FlagStateManager.PlayerFlagState;
  61. import com.sk89q.worldguard.protection.ApplicableRegionSet;
  62. import com.sk89q.worldguard.protection.flags.DefaultFlag;
  63. import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
  64. import com.sk89q.worldguard.protection.flags.RegionGroupFlag.RegionGroup;
  65. import com.sk89q.worldguard.protection.managers.RegionManager;
  66. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
  67. /**
  68. * Handles all events thrown in relation to a player.
  69. */
  70. public class WorldGuardPlayerListener extends PlayerListener {
  71. /**
  72. * Logger for messages.
  73. */
  74. private static final Logger logger = Logger.getLogger("Minecraft.WorldGuard");
  75. private WorldGuardPlugin plugin;
  76. /**
  77. * Construct the object;
  78. *
  79. * @param plugin
  80. */
  81. public WorldGuardPlayerListener(WorldGuardPlugin plugin) {
  82. this.plugin = plugin;
  83. }
  84. /**
  85. * Register events.
  86. */
  87. public void registerEvents() {
  88. // PluginManager pm = plugin.getServer().getPluginManager();
  89. registerEvent("PLAYER_INTERACT", Priority.High);
  90. registerEvent("PLAYER_DROP_ITEM", Priority.High);
  91. registerEvent("PLAYER_PICKUP_ITEM", Priority.High);
  92. registerEvent("PLAYER_JOIN", Priority.Normal);
  93. registerEvent("PLAYER_LOGIN", Priority.Normal);
  94. registerEvent("PLAYER_QUIT", Priority.Normal);
  95. registerEvent("PLAYER_BUCKET_FILL", Priority.High);
  96. registerEvent("PLAYER_BUCKET_EMPTY", Priority.High);
  97. registerEvent("PLAYER_RESPAWN", Priority.High);
  98. registerEvent("PLAYER_ITEM_HELD", Priority.High);
  99. registerEvent("PLAYER_BED_ENTER", Priority.High);
  100. registerEvent("PLAYER_COMMAND_PREPROCESS", Priority.Lowest);
  101. if (plugin.getGlobalStateManager().usePlayerMove) {
  102. registerEvent("PLAYER_MOVE", Priority.High);
  103. }
  104. }
  105. /**
  106. * Register an event, but not failing if the event is not implemented.
  107. *
  108. * @param typeName
  109. * @param priority
  110. */
  111. private void registerEvent(String typeName, Priority priority) {
  112. try {
  113. Event.Type type = Event.Type.valueOf(typeName);
  114. PluginManager pm = plugin.getServer().getPluginManager();
  115. pm.registerEvent(type, this, priority, plugin);
  116. } catch (IllegalArgumentException e) {
  117. logger.info("WorldGuard: Unable to register missing event type " + typeName);
  118. }
  119. }
  120. /**
  121. * Called when a player attempts to log in to the server.
  122. */
  123. /*
  124. @Override
  125. public void onPlayerLogin(PlayerLoginEvent event) {
  126. Player player = event.getPlayer();
  127. ConfigurationManager cfg = plugin.getGlobalStateManager();
  128. WorldConfiguration wcfg = cfg.get(player.getWorld());
  129. if (wcfg.enforceOneSession) {
  130. String name = player.getName();
  131. for (Player pl : plugin.getServer().getOnlinePlayers()) {
  132. if (pl.getName().equalsIgnoreCase(name)) {
  133. pl.kickPlayer("Logged in from another location.");
  134. }
  135. }
  136. }
  137. }
  138. */
  139. /**
  140. * Called when a player joins a server.
  141. */
  142. @Override
  143. public void onPlayerJoin(PlayerJoinEvent event) {
  144. Player player = event.getPlayer();
  145. World world = player.getWorld();
  146. ConfigurationManager cfg = plugin.getGlobalStateManager();
  147. WorldConfiguration wcfg = cfg.get(world);
  148. if (cfg.activityHaltToggle) {
  149. player.sendMessage(ChatColor.YELLOW
  150. + "Intensive server activity has been HALTED.");
  151. int removed = 0;
  152. for (Entity entity : world.getEntities()) {
  153. if (BukkitUtil.isIntensiveEntity(entity)) {
  154. entity.remove();
  155. removed++;
  156. }
  157. }
  158. if (removed > 10) {
  159. logger.info("WG Halt-Act: " + removed + " entities (>10) auto-removed from "
  160. + player.getWorld().toString());
  161. }
  162. }
  163. if (wcfg.fireSpreadDisableToggle) {
  164. player.sendMessage(ChatColor.YELLOW
  165. + "Fire spread is currently globally disabled for this world.");
  166. }
  167. if (plugin.inGroup(player, "wg-invincible")
  168. || (cfg.autoGodMode && plugin.hasPermission(player, "worldguard.auto-invincible"))) {
  169. cfg.enableGodMode(player);
  170. }
  171. if (plugin.inGroup(player, "wg-amphibious")) {
  172. cfg.enableAmphibiousMode(player);
  173. }
  174. if (wcfg.useRegions) {
  175. PlayerFlagState state = plugin.getFlagStateManager().getState(player);
  176. Location loc = player.getLocation();
  177. state.lastWorld = loc.getWorld();
  178. state.lastBlockX = loc.getBlockX();
  179. state.lastBlockY = loc.getBlockY();
  180. state.lastBlockZ = loc.getBlockZ();
  181. }
  182. }
  183. /**
  184. * Called when a player leaves a server.
  185. */
  186. @Override
  187. public void onPlayerQuit(PlayerQuitEvent event) {
  188. Player player = event.getPlayer();
  189. World world = player.getWorld();
  190. ConfigurationManager cfg = plugin.getGlobalStateManager();
  191. WorldConfiguration wcfg = cfg.get(world);
  192. // This is to make the enter/exit flags accurate -- move events are not
  193. // sent constantly, so it is possible to move just a little enough to
  194. // not trigger the event and then rejoin so that you are then considered
  195. // outside the border. This should work around that.
  196. if (wcfg.useRegions) {
  197. boolean hasBypass = plugin.getGlobalRegionManager().hasBypass(player, world);
  198. PlayerFlagState state = plugin.getFlagStateManager().getState(player);
  199. if (state.lastWorld != null && !hasBypass) {
  200. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  201. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  202. Location loc = player.getLocation();
  203. Vector pt = new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
  204. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  205. if (state.lastExitAllowed == null) {
  206. state.lastExitAllowed = set.allows(DefaultFlag.EXIT, localPlayer);
  207. }
  208. if (!state.lastExitAllowed || !set.allows(DefaultFlag.ENTRY, localPlayer)) {
  209. // Only if we have the last location cached
  210. if (state.lastWorld.equals(world)) {
  211. Location newLoc = new Location(world, state.lastBlockX + 0.5,
  212. state.lastBlockY, state.lastBlockZ + 0.5);
  213. player.teleport(newLoc);
  214. }
  215. }
  216. }
  217. }
  218. cfg.forgetPlayer(plugin.wrapPlayer(player));
  219. plugin.forgetPlayer(player);
  220. }
  221. /**
  222. * Called when a player interacts with an item.
  223. */
  224. @Override
  225. public void onPlayerInteract(PlayerInteractEvent event) {
  226. Player player = event.getPlayer();
  227. World world = player.getWorld();
  228. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
  229. handleBlockRightClick(event);
  230. } else if (event.getAction() == Action.RIGHT_CLICK_AIR) {
  231. handleAirRightClick(event);
  232. } else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
  233. handleBlockLeftClick(event);
  234. } else if (event.getAction() == Action.LEFT_CLICK_AIR) {
  235. handleAirLeftClick(event);
  236. } else if (event.getAction() == Action.PHYSICAL) {
  237. handlePhysicalInteract(event);
  238. }
  239. ConfigurationManager cfg = plugin.getGlobalStateManager();
  240. WorldConfiguration wcfg = cfg.get(world);
  241. if (wcfg.removeInfiniteStacks
  242. && !plugin.hasPermission(player, "worldguard.override.infinite-stack")) {
  243. int slot = player.getInventory().getHeldItemSlot();
  244. ItemStack heldItem = player.getInventory().getItem(slot);
  245. if (heldItem.getAmount() < 0) {
  246. player.getInventory().setItem(slot, null);
  247. player.sendMessage(ChatColor.RED + "Infinite stack removed.");
  248. }
  249. }
  250. }
  251. /**
  252. * Called when a player attempts to move.
  253. *
  254. * @param event
  255. */
  256. @Override
  257. public void onPlayerMove(PlayerMoveEvent event) {
  258. Player player = event.getPlayer();
  259. World world = player.getWorld();
  260. ConfigurationManager cfg = plugin.getGlobalStateManager();
  261. WorldConfiguration wcfg = cfg.get(world);
  262. if (player.getVehicle() != null) return; // handled in vehicle listener
  263. if (wcfg.useRegions) {
  264. // Did we move a block?
  265. if (event.getFrom().getBlockX() != event.getTo().getBlockX()
  266. || event.getFrom().getBlockY() != event.getTo().getBlockY()
  267. || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
  268. PlayerFlagState state = plugin.getFlagStateManager().getState(player);
  269. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  270. boolean hasBypass = plugin.getGlobalRegionManager().hasBypass(player, world);
  271. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  272. Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ());
  273. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  274. boolean entryAllowed = set.allows(DefaultFlag.ENTRY, localPlayer);
  275. if (!hasBypass && !entryAllowed) {
  276. player.sendMessage(ChatColor.DARK_RED + "You are not permitted to enter this area.");
  277. Location newLoc = event.getFrom();
  278. newLoc.setX(newLoc.getBlockX() + 0.5);
  279. newLoc.setY(newLoc.getBlockY());
  280. newLoc.setZ(newLoc.getBlockZ() + 0.5);
  281. event.setTo(newLoc);
  282. return;
  283. }
  284. // Have to set this state
  285. if (state.lastExitAllowed == null) {
  286. state.lastExitAllowed = mgr.getApplicableRegions(toVector(event.getFrom()))
  287. .allows(DefaultFlag.EXIT, localPlayer);
  288. }
  289. boolean exitAllowed = set.allows(DefaultFlag.EXIT, localPlayer);
  290. if (!hasBypass && exitAllowed && !state.lastExitAllowed) {
  291. player.sendMessage(ChatColor.DARK_RED + "You are not permitted to leave this area.");
  292. Location newLoc = event.getFrom();
  293. newLoc.setX(newLoc.getBlockX() + 0.5);
  294. newLoc.setY(newLoc.getBlockY());
  295. newLoc.setZ(newLoc.getBlockZ() + 0.5);
  296. event.setTo(newLoc);
  297. return;
  298. }
  299. String greeting = set.getFlag(DefaultFlag.GREET_MESSAGE);
  300. String farewell = set.getFlag(DefaultFlag.FAREWELL_MESSAGE);
  301. Boolean notifyEnter = set.getFlag(DefaultFlag.NOTIFY_ENTER);
  302. Boolean notifyLeave = set.getFlag(DefaultFlag.NOTIFY_LEAVE);
  303. if (state.lastFarewell != null && (farewell == null
  304. || !state.lastFarewell.equals(farewell))) {
  305. String replacedFarewell = plugin.replaceMacros(
  306. player, BukkitUtil.replaceColorMacros(state.lastFarewell));
  307. player.sendMessage(ChatColor.AQUA + " ** " + replacedFarewell);
  308. }
  309. if (greeting != null && (state.lastGreeting == null
  310. || !state.lastGreeting.equals(greeting))) {
  311. String replacedGreeting = plugin.replaceMacros(
  312. player, BukkitUtil.replaceColorMacros(greeting));
  313. player.sendMessage(ChatColor.AQUA + " ** " + replacedGreeting);
  314. }
  315. if ((notifyLeave == null || !notifyLeave)
  316. && state.notifiedForLeave != null && state.notifiedForLeave) {
  317. plugin.broadcastNotification(ChatColor.GRAY + "WG: "
  318. + ChatColor.LIGHT_PURPLE + player.getName()
  319. + ChatColor.GOLD + " left NOTIFY region");
  320. }
  321. if (notifyEnter != null && notifyEnter && (state.notifiedForEnter == null
  322. || !state.notifiedForEnter)) {
  323. StringBuilder regionList = new StringBuilder();
  324. for (ProtectedRegion region : set) {
  325. if (regionList.length() != 0) {
  326. regionList.append(", ");
  327. }
  328. regionList.append(region.getId());
  329. }
  330. plugin.broadcastNotification(ChatColor.GRAY + "WG: "
  331. + ChatColor.LIGHT_PURPLE + player.getName()
  332. + ChatColor.GOLD + " entered NOTIFY region: "
  333. + ChatColor.WHITE
  334. + regionList);
  335. }
  336. state.lastGreeting = greeting;
  337. state.lastFarewell = farewell;
  338. state.notifiedForEnter = notifyEnter;
  339. state.notifiedForLeave = notifyLeave;
  340. state.lastExitAllowed = exitAllowed;
  341. state.lastWorld = event.getTo().getWorld();
  342. state.lastBlockX = event.getTo().getBlockX();
  343. state.lastBlockY = event.getTo().getBlockY();
  344. state.lastBlockZ = event.getTo().getBlockZ();
  345. }
  346. }
  347. }
  348. /**
  349. * Called when a player left clicks air.
  350. *
  351. * @param event
  352. */
  353. private void handleAirLeftClick(PlayerInteractEvent event) {
  354. // I don't think we have to do anything here yet.
  355. return;
  356. }
  357. /**
  358. * Called when a player left clicks a block.
  359. *
  360. * @param event
  361. */
  362. private void handleBlockLeftClick(PlayerInteractEvent event) {
  363. if (event.isCancelled()) return;
  364. Player player = event.getPlayer();
  365. Block block = event.getClickedBlock();
  366. Material type = block.getType();
  367. World world = player.getWorld();
  368. ConfigurationManager cfg = plugin.getGlobalStateManager();
  369. WorldConfiguration wcfg = cfg.get(world);
  370. if (wcfg.useRegions) {
  371. Vector pt = toVector(block);
  372. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  373. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  374. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  375. if (type == Material.STONE_BUTTON
  376. || type == Material.LEVER
  377. || type == Material.WOODEN_DOOR
  378. || type == Material.TRAP_DOOR
  379. || type == Material.NOTE_BLOCK) {
  380. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  381. && !set.allows(DefaultFlag.USE)
  382. && !set.canBuild(localPlayer)) {
  383. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that in this area.");
  384. event.setUseInteractedBlock(Result.DENY);
  385. event.setCancelled(true);
  386. return;
  387. }
  388. }
  389. if (block.getRelative(event.getBlockFace()).getType() == Material.FIRE) {
  390. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  391. && !set.canBuild(localPlayer)) {
  392. event.setUseInteractedBlock(Result.DENY);
  393. event.setCancelled(true);
  394. return;
  395. }
  396. }
  397. }
  398. if (type == Material.TNT && player.getItemInHand().getType() == Material.FLINT_AND_STEEL) {
  399. if (wcfg.getBlacklist() != null) {
  400. if (!wcfg.getBlacklist().check(
  401. new BlockBreakBlacklistEvent(plugin.wrapPlayer(player),
  402. toVector(event.getClickedBlock()),
  403. event.getClickedBlock().getTypeId()), false, false)) {
  404. event.setUseInteractedBlock(Result.DENY);
  405. event.setUseItemInHand(Result.DENY);
  406. event.setCancelled(true);
  407. return;
  408. }
  409. }
  410. }
  411. }
  412. /**
  413. * Called when a player right clicks air.
  414. *
  415. * @param event
  416. */
  417. private void handleAirRightClick(PlayerInteractEvent event) {
  418. if (event.isCancelled()) {
  419. return;
  420. }
  421. Player player = event.getPlayer();
  422. World world = player.getWorld();
  423. ItemStack item = player.getItemInHand();
  424. ConfigurationManager cfg = plugin.getGlobalStateManager();
  425. WorldConfiguration wcfg = cfg.get(world);
  426. if (wcfg.getBlacklist() != null) {
  427. if (!wcfg.getBlacklist().check(
  428. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  429. toVector(player.getLocation()),
  430. item.getTypeId()), false, false)) {
  431. event.setCancelled(true);
  432. event.setUseItemInHand(Result.DENY);
  433. return;
  434. }
  435. }
  436. }
  437. /**
  438. * Called when a player right clicks a block.
  439. *
  440. * @param event
  441. */
  442. private void handleBlockRightClick(PlayerInteractEvent event) {
  443. if (event.isCancelled()) {
  444. return;
  445. }
  446. Block block = event.getClickedBlock();
  447. World world = block.getWorld();
  448. Material type = block.getType();
  449. Player player = event.getPlayer();
  450. ItemStack item = player.getItemInHand();
  451. ConfigurationManager cfg = plugin.getGlobalStateManager();
  452. WorldConfiguration wcfg = cfg.get(world);
  453. // Infinite stack removal
  454. if ((type == Material.CHEST
  455. || type == Material.JUKEBOX
  456. || type == Material.DISPENSER
  457. || type == Material.FURNACE
  458. || type == Material.BURNING_FURNACE)
  459. && wcfg.removeInfiniteStacks
  460. && !plugin.hasPermission(player, "worldguard.override.infinite-stack")) {
  461. for (int slot = 0; slot < 40; slot++) {
  462. ItemStack heldItem = player.getInventory().getItem(slot);
  463. if (heldItem != null && heldItem.getAmount() < 0) {
  464. player.getInventory().setItem(slot, null);
  465. player.sendMessage(ChatColor.RED + "Infinite stack in slot #" + slot + " removed.");
  466. }
  467. }
  468. }
  469. if (wcfg.useRegions) {
  470. Vector pt = toVector(block);
  471. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  472. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  473. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  474. if (item.getTypeId() == wcfg.regionWand) {
  475. if (set.size() > 0) {
  476. player.sendMessage(ChatColor.YELLOW + "Can you build? "
  477. + (set.canBuild(localPlayer) ? "Yes" : "No"));
  478. StringBuilder str = new StringBuilder();
  479. for (Iterator<ProtectedRegion> it = set.iterator(); it.hasNext();) {
  480. str.append(it.next().getId());
  481. if (it.hasNext()) {
  482. str.append(", ");
  483. }
  484. }
  485. player.sendMessage(ChatColor.YELLOW + "Applicable regions: " + str.toString());
  486. } else {
  487. player.sendMessage(ChatColor.YELLOW + "WorldGuard: No defined regions here!");
  488. }
  489. event.setCancelled(true);
  490. return;
  491. }
  492. if (item.getType() == Material.TNT) {
  493. Block placedOn = block.getRelative(event.getBlockFace());
  494. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  495. && !plugin.getGlobalRegionManager().allows(
  496. DefaultFlag.TNT, placedOn.getLocation())) {
  497. event.setUseItemInHand(Result.DENY);
  498. event.setCancelled(true);
  499. }
  500. }
  501. if (item.getType() == Material.INK_SACK
  502. && item.getData() != null
  503. && item.getData().getData() == 15 // bonemeal
  504. && type == Material.GRASS) {
  505. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  506. && !set.canBuild(localPlayer)) {
  507. event.setCancelled(true);
  508. event.setUseItemInHand(Result.DENY);
  509. }
  510. }
  511. if (type == Material.BED_BLOCK) {
  512. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  513. && !set.allows(DefaultFlag.SLEEP)) {
  514. player.sendMessage(ChatColor.DARK_RED + "You're not allowed to use that bed.");
  515. event.setUseInteractedBlock(Result.DENY);
  516. event.setCancelled(true);
  517. return;
  518. }
  519. }
  520. if (type == Material.CHEST
  521. || type == Material.JUKEBOX //stores the (arguably) most valuable item
  522. || type == Material.DISPENSER
  523. || type == Material.FURNACE
  524. || type == Material.BURNING_FURNACE) {
  525. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  526. && !set.allows(DefaultFlag.CHEST_ACCESS)
  527. && !set.canBuild(localPlayer)) {
  528. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to open that in this area.");
  529. event.setUseInteractedBlock(Result.DENY);
  530. event.setCancelled(true);
  531. return;
  532. }
  533. }
  534. if (type == Material.LEVER
  535. || type == Material.STONE_BUTTON
  536. || type == Material.NOTE_BLOCK
  537. || type == Material.DIODE_BLOCK_OFF
  538. || type == Material.DIODE_BLOCK_ON
  539. || type == Material.WOODEN_DOOR
  540. || type == Material.TRAP_DOOR
  541. || type == Material.FENCE_GATE
  542. || type == Material.WORKBENCH) {
  543. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  544. && !set.allows(DefaultFlag.USE)
  545. && !set.canBuild(localPlayer)) {
  546. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that in this area.");
  547. event.setUseInteractedBlock(Result.DENY);
  548. event.setCancelled(true);
  549. return;
  550. }
  551. }
  552. if (type == Material.CAKE_BLOCK) {
  553. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  554. && !set.canBuild(localPlayer)) {
  555. player.sendMessage(ChatColor.DARK_RED + "You're not invited to this tea party!");
  556. event.setUseInteractedBlock(Result.DENY);
  557. event.setCancelled(true);
  558. return;
  559. }
  560. }
  561. if (BlockType.isRailBlock(type.getId()) && item.getType() == Material.MINECART) {
  562. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  563. && !set.canBuild(localPlayer)
  564. && !set.allows(DefaultFlag.PLACE_VEHICLE)) {
  565. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to place vehicles here.");
  566. event.setUseItemInHand(Result.DENY);
  567. event.setCancelled(true);
  568. return;
  569. }
  570. }
  571. if (item.getType() == Material.BOAT) {
  572. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  573. && !set.canBuild(localPlayer)
  574. && !set.allows(DefaultFlag.PLACE_VEHICLE)) {
  575. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to place vehicles here.");
  576. event.setUseItemInHand(Result.DENY);
  577. event.setCancelled(true);
  578. return;
  579. }
  580. }
  581. }
  582. if (wcfg.getBlacklist() != null) {
  583. if((block.getType() != Material.CHEST
  584. && block.getType() != Material.DISPENSER
  585. && block.getType() != Material.FURNACE
  586. && block.getType() != Material.BURNING_FURNACE)) {
  587. if (!wcfg.getBlacklist().check(
  588. new ItemUseBlacklistEvent(plugin.wrapPlayer(player), toVector(block),
  589. item.getTypeId()), false, false)) {
  590. event.setUseItemInHand(Result.DENY);
  591. event.setCancelled(true);
  592. return;
  593. }
  594. }
  595. if (!wcfg.getBlacklist().check(
  596. new BlockInteractBlacklistEvent(plugin.wrapPlayer(player), toVector(block),
  597. block.getTypeId()), false, false)) {
  598. event.setUseInteractedBlock(Result.DENY);
  599. event.setCancelled(true);
  600. return;
  601. }
  602. // Workaround for http://leaky.bukkit.org/issues/1034
  603. if (item.getType() == Material.TNT) {
  604. Block placedOn = block.getRelative(event.getBlockFace());
  605. if (!wcfg.getBlacklist().check(
  606. new BlockPlaceBlacklistEvent(plugin.wrapPlayer(player), toVector(placedOn),
  607. item.getTypeId()), false, false)) {
  608. event.setUseItemInHand(Result.DENY);
  609. event.setCancelled(true);
  610. return;
  611. }
  612. }
  613. }
  614. if ((block.getType() == Material.CHEST
  615. || block.getType() == Material.DISPENSER
  616. || block.getType() == Material.FURNACE
  617. || block.getType() == Material.BURNING_FURNACE)) {
  618. if (wcfg.isChestProtected(block, player)) {
  619. player.sendMessage(ChatColor.DARK_RED + "The chest is protected.");
  620. event.setUseInteractedBlock(Result.DENY);
  621. event.setCancelled(true);
  622. return;
  623. }
  624. }
  625. /*if (wcfg.useRegions && wcfg.useiConomy && cfg.getiConomy() != null
  626. && (type == Material.SIGN_POST || type == Material.SIGN || type == Material.WALL_SIGN)) {
  627. BlockState block = blockClicked.getState();
  628. if (((Sign)block).getLine(0).equalsIgnoreCase("[WorldGuard]")
  629. && ((Sign)block).getLine(1).equalsIgnoreCase("For sale")) {
  630. String regionId = ((Sign)block).getLine(2);
  631. //String regionComment = ((Sign)block).getLine(3);
  632. if (regionId != null && regionId != "") {
  633. RegionManager mgr = cfg.getWorldGuardPlugin().getGlobalRegionManager().get(player.getWorld().getName());
  634. ProtectedRegion region = mgr.getRegion(regionId);
  635. if (region != null) {
  636. RegionFlags flags = region.getFlags();
  637. if (flags.getBooleanFlag(DefaultFlag.BUYABLE).getValue(false)) {
  638. if (iConomy.getBank().hasAccount(player.getName())) {
  639. Account account = iConomy.getBank().getAccount(player.getName());
  640. double balance = account.getBalance();
  641. double regionPrice = flags.getDoubleFlag(DefaultFlag.PRICE).getValue();
  642. if (balance >= regionPrice) {
  643. account.subtract(regionPrice);
  644. player.sendMessage(ChatColor.YELLOW + "You have bought the region " + regionId + " for " +
  645. iConomy.getBank().format(regionPrice));
  646. DefaultDomain owners = region.getOwners();
  647. owners.addPlayer(player.getName());
  648. region.setOwners(owners);
  649. flags.getBooleanFlag(DefaultFlag.BUYABLE).setValue(false);
  650. account.save();
  651. } else {
  652. player.sendMessage(ChatColor.YELLOW + "You have not enough money.");
  653. }
  654. } else {
  655. player.sendMessage(ChatColor.YELLOW + "You have not enough money.");
  656. }
  657. } else {
  658. player.sendMessage(ChatColor.RED + "Region: " + regionId + " is not buyable");
  659. }
  660. } else {
  661. player.sendMessage(ChatColor.DARK_RED + "The region " + regionId + " does not exist.");
  662. }
  663. } else {
  664. player.sendMessage(ChatColor.DARK_RED + "No region specified.");
  665. }
  666. }
  667. }*/
  668. }
  669. /**
  670. * Called when a player steps on a pressure plate or tramples crops.
  671. *
  672. * @param event
  673. */
  674. private void handlePhysicalInteract(PlayerInteractEvent event) {
  675. if (event.isCancelled() == true) return;
  676. Player player = event.getPlayer();
  677. Block block = event.getClickedBlock(); //not actually clicked but whatever
  678. Material type = block.getType();
  679. World world = player.getWorld();
  680. ConfigurationManager cfg = plugin.getGlobalStateManager();
  681. WorldConfiguration wcfg = cfg.get(world);
  682. if (block.getType() == Material.SOIL && wcfg.disablePlayerCropTrampling) {
  683. event.setCancelled(true);
  684. return;
  685. }
  686. if (wcfg.useRegions) {
  687. Vector pt = toVector(block);
  688. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  689. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  690. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  691. if (type == Material.STONE_PLATE || type == Material.WOOD_PLATE) {
  692. if (!plugin.getGlobalRegionManager().hasBypass(player, world)
  693. && !set.allows(DefaultFlag.USE)
  694. && !set.canBuild(localPlayer)) {
  695. event.setUseInteractedBlock(Result.DENY);
  696. event.setCancelled(true);
  697. return;
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * Called when a player uses an item.
  704. *//*
  705. @Override
  706. public void onPlayerItem(PlayerItemEvent event) {
  707. if (event.isCancelled()) {
  708. return;
  709. }
  710. Player player = event.getPlayer();
  711. Block block = event.getBlockClicked();
  712. ItemStack item = event.getItem();
  713. int itemId = item.getTypeId();
  714. GlobalConfiguration cfg = plugin.getGlobalConfiguration();
  715. WorldConfiguration wcfg = cfg.getWorldConfig(player.getWorld().getName());
  716. if (wcfg.useRegions
  717. && (itemId == 322 || itemId == 320 || itemId == 319 || itemId == 297 || itemId == 260
  718. || itemId == 350 || itemId == 349 || itemId == 354) ) {
  719. return;
  720. }
  721. if (!wcfg.itemDurability) {
  722. // Hoes
  723. if (item.getTypeId() >= 290 && item.getTypeId() <= 294) {
  724. item.setDurability((byte) -1);
  725. player.setItemInHand(item);
  726. }
  727. }
  728. if (wcfg.useRegions && !event.isBlock() && block != null) {
  729. Vector pt = toVector(block.getRelative(event.getBlockFace()));
  730. if (block.getType() == Material.WALL_SIGN) {
  731. pt = pt.subtract(0, 1, 0);
  732. }
  733. if (!cfg.canBuild(player, pt)) {
  734. player.sendMessage(ChatColor.DARK_RED
  735. + "You don't have permission for this area.");
  736. event.setCancelled(true);
  737. return;
  738. }
  739. }
  740. if (wcfg.getBlacklist() != null && item != null && block != null) {
  741. if (!wcfg.getBlacklist().check(
  742. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  743. toVector(block.getRelative(event.getBlockFace())),
  744. item.getTypeId()), false, false)) {
  745. event.setCancelled(true);
  746. return;
  747. }
  748. }
  749. if (wcfg.useRegions && item != null && block != null && item.getTypeId() == 259) {
  750. Vector pt = toVector(block.getRelative(event.getBlockFace()));
  751. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld().getName());
  752. if (!mgr.getApplicableRegions(pt).isStateFlagAllowed(DefaultFlag.LIGHTER)) {
  753. event.setCancelled(true);
  754. return;
  755. }
  756. }
  757. }*/
  758. /**
  759. * Called when a player attempts to drop an item.
  760. */
  761. @Override
  762. public void onPlayerDropItem(PlayerDropItemEvent event) {
  763. if (event.isCancelled()) {
  764. return;
  765. }
  766. ConfigurationManager cfg = plugin.getGlobalStateManager();
  767. WorldConfiguration wcfg = cfg.get(event.getPlayer().getWorld());
  768. if (wcfg.getBlacklist() != null) {
  769. Item ci = event.getItemDrop();
  770. if (!wcfg.getBlacklist().check(
  771. new ItemDropBlacklistEvent(plugin.wrapPlayer(event.getPlayer()),
  772. toVector(ci.getLocation()), ci.getItemStack().getTypeId()), false, false)) {
  773. event.setCancelled(true);
  774. return;
  775. }
  776. }
  777. }
  778. /**
  779. * Called when a player attempts to pickup an item.
  780. */
  781. @Override
  782. public void onPlayerPickupItem(PlayerPickupItemEvent event) {
  783. if (event.isCancelled()) {
  784. return;
  785. }
  786. ConfigurationManager cfg = plugin.getGlobalStateManager();
  787. WorldConfiguration wcfg = cfg.get(event.getPlayer().getWorld());
  788. if (wcfg.getBlacklist() != null) {
  789. Item ci = event.getItem();
  790. if (!wcfg.getBlacklist().check(
  791. new ItemAcquireBlacklistEvent(plugin.wrapPlayer(event.getPlayer()),
  792. toVector(ci.getLocation()), ci.getItemStack().getTypeId()), false, true)) {
  793. event.setCancelled(true);
  794. return;
  795. }
  796. }
  797. }
  798. /**
  799. * Called when a bucket is filled.
  800. */
  801. @Override
  802. public void onPlayerBucketFill(PlayerBucketFillEvent event) {
  803. Player player = event.getPlayer();
  804. World world = player.getWorld();
  805. ConfigurationManager cfg = plugin.getGlobalStateManager();
  806. WorldConfiguration wcfg = cfg.get(world);
  807. if (!plugin.getGlobalRegionManager().canBuild(
  808. player, event.getBlockClicked().getRelative(event.getBlockFace()))) {
  809. player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
  810. event.setCancelled(true);
  811. return;
  812. }
  813. if (wcfg.getBlacklist() != null) {
  814. if (!wcfg.getBlacklist().check(
  815. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  816. toVector(player.getLocation()), event.getBucket().getId()), false, false)) {
  817. event.setCancelled(true);
  818. return;
  819. }
  820. }
  821. }
  822. /**
  823. * Called when a bucket is empty.
  824. */
  825. @Override
  826. public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
  827. Player player = event.getPlayer();
  828. World world = player.getWorld();
  829. ConfigurationManager cfg = plugin.getGlobalStateManager();
  830. WorldConfiguration wcfg = cfg.get(world);
  831. if (!plugin.getGlobalRegionManager().canBuild(
  832. player, event.getBlockClicked().getRelative(event.getBlockFace()))) {
  833. player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this area.");
  834. event.setCancelled(true);
  835. return;
  836. }
  837. if (wcfg.getBlacklist() != null) {
  838. if (!wcfg.getBlacklist().check(
  839. new ItemUseBlacklistEvent(plugin.wrapPlayer(player),
  840. toVector(player.getLocation()), event.getBucket().getId()), false, false)) {
  841. event.setCancelled(true);
  842. return;
  843. }
  844. }
  845. }
  846. /**
  847. * Called when a player is respawned.
  848. */
  849. @Override
  850. public void onPlayerRespawn(PlayerRespawnEvent event) {
  851. Player player = event.getPlayer();
  852. Location location = player.getLocation();
  853. ConfigurationManager cfg = plugin.getGlobalStateManager();
  854. WorldConfiguration wcfg = cfg.get(player.getWorld());
  855. if (wcfg.useRegions) {
  856. Vector pt = toVector(location);
  857. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  858. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  859. Vector spawn = set.getFlag(DefaultFlag.SPAWN_LOC);
  860. if (spawn != null) {
  861. RegionGroup group = set.getFlag(DefaultFlag.SPAWN_PERM);
  862. Location spawnLoc = BukkitUtil.toLocation(player.getWorld(), spawn);
  863. if (group != null) {
  864. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  865. if (RegionGroupFlag.isMember(set, group, localPlayer)) {
  866. event.setRespawnLocation(spawnLoc);
  867. }
  868. } else {
  869. event.setRespawnLocation(spawnLoc);
  870. }
  871. }
  872. }
  873. }
  874. /**
  875. * Called when a player changes their held item.
  876. */
  877. @Override
  878. public void onItemHeldChange(PlayerItemHeldEvent event) {
  879. Player player = event.getPlayer();
  880. ConfigurationManager cfg = plugin.getGlobalStateManager();
  881. WorldConfiguration wcfg = cfg.get(player.getWorld());
  882. if (wcfg.removeInfiniteStacks
  883. && !plugin.hasPermission(player, "worldguard.override.infinite-stack")) {
  884. int newSlot = event.getNewSlot();
  885. ItemStack heldItem = player.getInventory().getItem(newSlot);
  886. if (heldItem.getAmount() < 0) {
  887. player.getInventory().setItem(newSlot, null);
  888. player.sendMessage(ChatColor.RED + "Infinite stack removed.");
  889. }
  890. }
  891. }
  892. /**
  893. * Called when a player enters a bed.
  894. */
  895. @Override
  896. public void onPlayerBedEnter(PlayerBedEnterEvent event) {
  897. if (event.isCancelled()) {
  898. return;
  899. }
  900. Player player = event.getPlayer();
  901. Location location = player.getLocation();
  902. ConfigurationManager cfg = plugin.getGlobalStateManager();
  903. WorldConfiguration wcfg = cfg.get(player.getWorld());
  904. if (wcfg.useRegions) {
  905. Vector pt = toVector(location);
  906. RegionManager mgr = plugin.getGlobalRegionManager().get(player.getWorld());
  907. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  908. if (!plugin.getGlobalRegionManager().hasBypass(player, player.getWorld())
  909. && !set.allows(DefaultFlag.SLEEP)) {
  910. event.setCancelled(true);
  911. player.sendMessage("This bed doesn't belong to you!");
  912. return;
  913. }
  914. }
  915. }
  916. /**
  917. * Called on command run.
  918. */
  919. @Override
  920. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
  921. Player player = event.getPlayer();
  922. World world = player.getWorld();
  923. ConfigurationManager cfg = plugin.getGlobalStateManager();
  924. WorldConfiguration wcfg = cfg.get(world);
  925. if (wcfg.useRegions && !plugin.getGlobalRegionManager().hasBypass(player, world)) {
  926. Vector pt = toVector(player.getLocation());
  927. RegionManager mgr = plugin.getGlobalRegionManager().get(world);
  928. ApplicableRegionSet set = mgr.getApplicableRegions(pt);
  929. ProtectedRegion globalRegion = mgr.getRegion("__global__");
  930. String[] parts = event.getMessage().split(" ");
  931. Set<String> allowedCommands = set.getFlag(DefaultFlag.ALLOWED_CMDS);
  932. if (allowedCommands == null && globalRegion != null) {
  933. allowedCommands = globalRegion.getFlag(DefaultFlag.ALLOWED_CMDS);
  934. }
  935. if (allowedCommands != null && !allowedCommands.contains(parts[0].toLowerCase())) {
  936. player.sendMessage(ChatColor.RED + parts[0].toLowerCase() + " is not allowed in this area.");
  937. event.setCancelled(true);
  938. return;
  939. }
  940. Set<String> blockedCommands = set.getFlag(DefaultFlag.BLOCKED_CMDS);
  941. if (blockedCommands == null && globalRegion != null) {
  942. blockedCommands = globalRegion.getFlag(DefaultFlag.BLOCKED_CMDS);
  943. }
  944. if (blockedCommands != null && blockedCommands.contains(parts[0].toLowerCase())) {
  945. player.sendMessage(ChatColor.RED + parts[0].toLowerCase() + " is blocked in this area.");
  946. event.setCancelled(true);
  947. return;
  948. }
  949. }
  950. }
  951. }