From 3a11ffc77a75688d8e22faee43c558519154a0c1 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 11 Jul 2020 19:03:55 +0100 Subject: [PATCH 01/21] Clean up bukkit listeners by splitting them up --- .../com/plotsquared/bukkit/BukkitMain.java | 13 +- .../bukkit/listener/BlockEventListener.java | 1050 ++++++ .../bukkit/listener/EntityEventListener.java | 317 ++ .../bukkit/listener/EntitySpawnListener.java | 3 +- .../bukkit/listener/PlayerEventListener.java | 1518 ++++++++ .../bukkit/listener/PlayerEvents.java | 3221 ----------------- .../listener/ProjectileEventListener.java | 156 + .../bukkit/util/BukkitEntityUtil.java | 363 ++ .../java/com/plotsquared/core/IPlotMain.java | 2 +- .../com/plotsquared/core/util/EntityUtil.java | 2 +- 10 files changed, 3417 insertions(+), 3228 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java delete mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 39b2a3c9d..b40b6a904 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -27,10 +27,13 @@ package com.plotsquared.bukkit; import com.plotsquared.bukkit.generator.BukkitHybridUtils; import com.plotsquared.bukkit.generator.BukkitPlotGenerator; +import com.plotsquared.bukkit.listener.BlockEventListener; import com.plotsquared.bukkit.listener.ChunkListener; +import com.plotsquared.bukkit.listener.EntityEventListener; import com.plotsquared.bukkit.listener.EntitySpawnListener; import com.plotsquared.bukkit.listener.PaperListener; -import com.plotsquared.bukkit.listener.PlayerEvents; +import com.plotsquared.bukkit.listener.PlayerEventListener; +import com.plotsquared.bukkit.listener.ProjectileEventListener; import com.plotsquared.bukkit.listener.SingleWorldListener; import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.managers.BukkitWorldManager; @@ -891,9 +894,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< return (ChunkGenerator) result.specify(worldName); } - @Override public void registerPlayerEvents() { - final PlayerEvents main = new PlayerEvents(); - getServer().getPluginManager().registerEvents(main, this); + @Override public void registerEvents() { + getServer().getPluginManager().registerEvents(new PlayerEventListener(), this); + getServer().getPluginManager().registerEvents(new BlockEventListener(), this); + getServer().getPluginManager().registerEvents(new EntityEventListener(), this); + getServer().getPluginManager().registerEvents(new ProjectileEventListener(), this); getServer().getPluginManager().registerEvents(new EntitySpawnListener(), this); if (PaperLib.isPaper() && Settings.Paper_Components.PAPER_LISTENERS) { getServer().getPluginManager().registerEvents(new PaperListener(), this); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java new file mode 100644 index 000000000..220e8d073 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -0,0 +1,1050 @@ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.database.DBFunc; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.flag.implementations.BlockBurnFlag; +import com.plotsquared.core.plot.flag.implementations.BlockIgnitionFlag; +import com.plotsquared.core.plot.flag.implementations.BreakFlag; +import com.plotsquared.core.plot.flag.implementations.CoralDryFlag; +import com.plotsquared.core.plot.flag.implementations.DisablePhysicsFlag; +import com.plotsquared.core.plot.flag.implementations.DoneFlag; +import com.plotsquared.core.plot.flag.implementations.ExplosionFlag; +import com.plotsquared.core.plot.flag.implementations.GrassGrowFlag; +import com.plotsquared.core.plot.flag.implementations.IceFormFlag; +import com.plotsquared.core.plot.flag.implementations.IceMeltFlag; +import com.plotsquared.core.plot.flag.implementations.InstabreakFlag; +import com.plotsquared.core.plot.flag.implementations.KelpGrowFlag; +import com.plotsquared.core.plot.flag.implementations.LiquidFlowFlag; +import com.plotsquared.core.plot.flag.implementations.MycelGrowFlag; +import com.plotsquared.core.plot.flag.implementations.PlaceFlag; +import com.plotsquared.core.plot.flag.implementations.RedstoneFlag; +import com.plotsquared.core.plot.flag.implementations.SnowFormFlag; +import com.plotsquared.core.plot.flag.implementations.SnowMeltFlag; +import com.plotsquared.core.plot.flag.implementations.SoilDryFlag; +import com.plotsquared.core.plot.flag.implementations.VineGrowFlag; +import com.plotsquared.core.plot.flag.types.BlockTypeWrapper; +import com.plotsquared.core.util.MainUtil; +import com.plotsquared.core.util.Permissions; +import com.plotsquared.core.util.task.TaskManager; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.world.block.BlockType; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Fireball; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockDamageEvent; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.event.block.BlockFadeEvent; +import org.bukkit.event.block.BlockFormEvent; +import org.bukkit.event.block.BlockFromToEvent; +import org.bukkit.event.block.BlockGrowEvent; +import org.bukkit.event.block.BlockIgniteEvent; +import org.bukkit.event.block.BlockPhysicsEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.block.BlockRedstoneEvent; +import org.bukkit.event.block.BlockSpreadEvent; +import org.bukkit.event.block.EntityBlockFormEvent; +import org.bukkit.event.world.StructureGrowEvent; +import org.bukkit.material.Directional; +import org.bukkit.projectiles.BlockProjectileSource; +import org.bukkit.util.Vector; + +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +@SuppressWarnings("unused") +public class BlockEventListener implements Listener { + + public static void sendBlockChange(final org.bukkit.Location bloc, final BlockData data) { + TaskManager.runTaskLater(() -> { + String world = bloc.getWorld().getName(); + int x = bloc.getBlockX(); + int z = bloc.getBlockZ(); + int distance = Bukkit.getViewDistance() * 16; + + for (final PlotPlayer player : PlotSquared.imp().getPlayerManager().getPlayers()) { + Location location = player.getLocation(); + if (location.getWorld().equals(world)) { + if (16 * Math.abs(location.getX() - x) / 16 > distance + || 16 * Math.abs(location.getZ() - z) / 16 > distance) { + continue; + } + ((BukkitPlayer) player).player.sendBlockChange(bloc, data); + } + } + }, 3); + } + + @EventHandler public void onRedstoneEvent(BlockRedstoneEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && area.getRoadFlag(RedstoneFlag.class)) { + event.setNewCurrent(0); + } + return; + } + if (!plot.getFlag(RedstoneFlag.class)) { + event.setNewCurrent(0); + plot.debug("Redstone event was cancelled because redstone = false"); + return; + } + if (Settings.Redstone.DISABLE_OFFLINE) { + boolean disable = false; + if (!plot.getOwner().equals(DBFunc.SERVER)) { + if (plot.isMerged()) { + disable = true; + for (UUID owner : plot.getOwners()) { + if (PlotSquared.imp().getPlayerManager().getPlayerIfExists(owner) != null) { + disable = false; + break; + } + } + } else { + disable = + PlotSquared.imp().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs()) + == null; + } + } + if (disable) { + for (UUID trusted : plot.getTrusted()) { + if (PlotSquared.imp().getPlayerManager().getPlayerIfExists(trusted) != null) { + disable = false; + break; + } + } + if (disable) { + event.setNewCurrent(0); + plot.debug( + "Redstone event was cancelled because no trusted player was in the plot"); + return; + } + } + } + if (Settings.Redstone.DISABLE_UNOCCUPIED) { + for (final PlotPlayer player : PlotSquared.imp().getPlayerManager().getPlayers()) { + if (plot.equals(player.getCurrentPlot())) { + return; + } + } + event.setNewCurrent(0); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void onPhysicsEvent(BlockPhysicsEvent event) { + switch (event.getChangedType()) { + case COMPARATOR: { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + if (location.isPlotArea()) { + return; + } + Plot plot = location.getOwnedPlotAbs(); + if (plot == null) { + return; + } + if (!plot.getFlag(RedstoneFlag.class)) { + event.setCancelled(true); + plot.debug("Prevented comparator update because redstone = false"); + } + return; + } + case ANVIL: + case DRAGON_EGG: + case GRAVEL: + case SAND: + case TURTLE_EGG: + case TURTLE_HELMET: + case TURTLE_SPAWN_EGG: { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlotAbs(location); + if (plot == null) { + return; + } + if (plot.getFlag(DisablePhysicsFlag.class)) { + event.setCancelled(true); + plot.debug("Prevented block physics because disable-physics = true"); + } + return; + } + default: + if (Settings.Redstone.DETECT_INVALID_EDGE_PISTONS) { + Block block = event.getBlock(); + switch (block.getType()) { + case PISTON: + case STICKY_PISTON: + org.bukkit.block.data.Directional piston = + (org.bukkit.block.data.Directional) block.getBlockData(); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlotAbs(location); + if (plot == null) { + return; + } + switch (piston.getFacing()) { + case EAST: + location.setX(location.getX() + 1); + break; + case SOUTH: + location.setX(location.getX() - 1); + break; + case WEST: + location.setZ(location.getZ() + 1); + break; + case NORTH: + location.setZ(location.getZ() - 1); + break; + } + Plot newPlot = area.getOwnedPlotAbs(location); + if (!plot.equals(newPlot)) { + event.setCancelled(true); + plot.debug( + "Prevented piston update because of invalid edge piston detection"); + return; + } + } + } + break; + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void blockCreate(BlockPlaceEvent event) { + Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + Plot plot = area.getPlot(location); + if (plot != null) { + if ((location.getY() > area.getMaxBuildHeight() || location.getY() < area + .getMinBuildHeight()) && !Permissions + .hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { + event.setCancelled(true); + MainUtil.sendMessage(pp, Captions.HEIGHT_LIMIT.getTranslated() + .replace("{limit}", String.valueOf(area.getMaxBuildHeight()))); + } + if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_UNOWNED); + event.setCancelled(true); + return; + } + } else if (!plot.isAdded(pp.getUUID())) { + List place = plot.getFlag(PlaceFlag.class); + if (place != null) { + Block block = event.getBlock(); + if (place.contains( + BlockTypeWrapper.get(BukkitAdapter.asBlockType(block.getType())))) { + return; + } + } + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + plot.debug(player.getName() + " could not place " + event.getBlock().getType() + + " because of the place flag"); + return; + } + } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + return; + } + } + if (plot.getFlag(DisablePhysicsFlag.class)) { + Block block = event.getBlockPlaced(); + if (block.getType().hasGravity()) { + sendBlockChange(block.getLocation(), block.getBlockData()); + plot.debug(event.getBlock().getType() + + " did not fall because of disable-physics = true"); + } + } + } else if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_ROAD); + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.LOWEST) public void blockDestroy(BlockBreakEvent event) { + Player player = event.getPlayer(); + Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getPlot(location); + if (plot != null) { + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (event.getBlock().getY() == 0) { + if (!Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_GROUNDLEVEL)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_GROUNDLEVEL); + event.setCancelled(true); + return; + } + } else if ((location.getY() > area.getMaxBuildHeight() || location.getY() < area + .getMinBuildHeight()) && !Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { + event.setCancelled(true); + MainUtil.sendMessage(plotPlayer, Captions.HEIGHT_LIMIT.getTranslated() + .replace("{limit}", String.valueOf(area.getMaxBuildHeight()))); + } + if (!plot.hasOwner()) { + if (!Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED, true)) { + event.setCancelled(true); + } + return; + } + if (!plot.isAdded(plotPlayer.getUUID())) { + List destroy = plot.getFlag(BreakFlag.class); + Block block = event.getBlock(); + final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); + for (final BlockTypeWrapper blockTypeWrapper : destroy) { + if (blockTypeWrapper.accepts(blockType)) { + return; + } + } + if (Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { + return; + } + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_OTHER); + event.setCancelled(true); + } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + return; + } + } + return; + } + BukkitPlayer pp = BukkitUtil.getPlayer(player); + if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { + return; + } + if (PlotSquared.get().worldedit != null && pp.getAttribute("worldedit")) { + if (player.getInventory().getItemInMainHand().getType() == Material + .getMaterial(PlotSquared.get().worldedit.getConfiguration().wandItem)) { + return; + } + } + MainUtil + .sendMessage(pp, Captions.NO_PERMISSION_EVENT, Captions.PERMISSION_ADMIN_DESTROY_ROAD); + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockSpread(BlockSpreadEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + if (location.isPlotRoad()) { + event.setCancelled(true); + return; + } + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + return; + } + switch (event.getSource().getType()) { + case GRASS_BLOCK: + if (!plot.getFlag(GrassGrowFlag.class)) { + plot.debug("Grass could not grow because grass-grow = false"); + event.setCancelled(true); + } + break; + case MYCELIUM: + if (!plot.getFlag(MycelGrowFlag.class)) { + plot.debug("Mycelium could not grow because mycel-grow = false"); + event.setCancelled(true); + } + break; + case VINE: + if (!plot.getFlag(VineGrowFlag.class)) { + plot.debug("Vine could not grow because vine-grow = false"); + event.setCancelled(true); + } + break; + case KELP: + if (!plot.getFlag(KelpGrowFlag.class)) { + plot.debug("Kelp could not grow because kelp-grow = false"); + event.setCancelled(true); + } + break; + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockForm(BlockFormEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + if (location.isPlotRoad()) { + event.setCancelled(true); + return; + } + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + return; + } + switch (event.getNewState().getType()) { + case SNOW: + case SNOW_BLOCK: + if (!plot.getFlag(SnowFormFlag.class)) { + plot.debug("Snow could not form because snow-form = false"); + event.setCancelled(true); + } + return; + case ICE: + case FROSTED_ICE: + case PACKED_ICE: + if (!plot.getFlag(IceFormFlag.class)) { + plot.debug("Ice could not form because ice-form = false"); + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityBlockForm(EntityBlockFormEvent event) { + String world = event.getBlock().getWorld().getName(); + if (!PlotSquared.get().hasPlotArea(world)) { + return; + } + Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + event.setCancelled(true); + return; + } + Entity entity = event.getEntity(); + if (entity instanceof Player) { + Player player = (Player) entity; + if (!plot.hasOwner()) { + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (plot.getFlag(IceFormFlag.class)) { + plot.debug("Ice could not be formed because ice-form = false"); + return; + } + event.setCancelled(true); + return; + } + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (!plot.isAdded(plotPlayer.getUUID())) { + if (plot.getFlag(IceFormFlag.class)) { + plot.debug("Ice could not be formed because ice-form = false"); + return; + } + event.setCancelled(true); + return; + } + return; + } + if (!plot.getFlag(IceFormFlag.class)) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockDamage(BlockDamageEvent event) { + Player player = event.getPlayer(); + Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + if (player.getGameMode() != GameMode.SURVIVAL) { + return; + } + Plot plot = area.getPlot(location); + if (plot != null) { + if (plot.getFlag(InstabreakFlag.class)) { + Block block = event.getBlock(); + BlockBreakEvent call = new BlockBreakEvent(block, player); + Bukkit.getServer().getPluginManager().callEvent(call); + if (!call.isCancelled()) { + event.getBlock().breakNaturally(); + } + } + if (location.getY() == 0) { + event.setCancelled(true); + return; + } + if (!plot.hasOwner()) { + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { + return; + } + event.setCancelled(true); + return; + } + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (!plot.isAdded(plotPlayer.getUUID())) { + List destroy = plot.getFlag(BreakFlag.class); + Block block = event.getBlock(); + if (destroy + .contains(BlockTypeWrapper.get(BukkitAdapter.asBlockType(block.getType()))) + || Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { + return; + } + plot.debug(player.getName() + " could not break " + block.getType() + + " because it was not in the break flag"); + event.setCancelled(true); + return; + } + return; + } + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { + return; + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onFade(BlockFadeEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + event.setCancelled(true); + return; + } + switch (block.getType()) { + case ICE: + if (!plot.getFlag(IceMeltFlag.class)) { + plot.debug("Ice could not melt because ice-melt = false"); + event.setCancelled(true); + } + break; + case SNOW: + if (!plot.getFlag(SnowMeltFlag.class)) { + plot.debug("Snow could not melt because snow-melt = false"); + event.setCancelled(true); + } + break; + case FARMLAND: + if (!plot.getFlag(SoilDryFlag.class)) { + plot.debug("Soil could not dry because soil-dry = false"); + event.setCancelled(true); + } + break; + case TUBE_CORAL_BLOCK: + case BRAIN_CORAL_BLOCK: + case BUBBLE_CORAL_BLOCK: + case FIRE_CORAL_BLOCK: + case HORN_CORAL_BLOCK: + case TUBE_CORAL: + case BRAIN_CORAL: + case BUBBLE_CORAL: + case FIRE_CORAL: + case HORN_CORAL: + case TUBE_CORAL_FAN: + case BRAIN_CORAL_FAN: + case BUBBLE_CORAL_FAN: + case FIRE_CORAL_FAN: + case HORN_CORAL_FAN: + case BRAIN_CORAL_WALL_FAN: + case BUBBLE_CORAL_WALL_FAN: + case FIRE_CORAL_WALL_FAN: + case HORN_CORAL_WALL_FAN: + case TUBE_CORAL_WALL_FAN: + if (!plot.getFlag(CoralDryFlag.class)) { + plot.debug("Coral could not dry because coral-dry = false"); + event.setCancelled(true); + } + break; + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onChange(BlockFromToEvent event) { + Block from = event.getBlock(); + + // Check liquid flow flag inside of origin plot too + final Location fLocation = BukkitUtil.getLocation(from.getLocation()); + final PlotArea fromArea = fLocation.getPlotArea(); + if (fromArea != null) { + final Plot plot = fromArea.getOwnedPlot(fLocation); + if (plot != null + && plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event + .getBlock().isLiquid()) { + plot.debug("Liquid could now flow because liquid-flow = disabled"); + event.setCancelled(true); + return; + } + } + + Block to = event.getToBlock(); + Location tLocation = BukkitUtil.getLocation(to.getLocation()); + PlotArea area = tLocation.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlot(tLocation); + if (plot != null) { + if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects + .equals(plot, area.getOwnedPlot(fLocation))) { + event.setCancelled(true); + return; + } + if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.ENABLED && event + .getBlock().isLiquid()) { + return; + } + if (plot.getFlag(DisablePhysicsFlag.class)) { + plot.debug(event.getBlock().getType() + + " could not update because disable-physics = true"); + event.setCancelled(true); + return; + } + if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event + .getBlock().isLiquid()) { + plot.debug("Liquid could not flow because liquid-flow = disabled"); + event.setCancelled(true); + } + } else if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects + .equals(null, area.getOwnedPlot(fLocation))) { + event.setCancelled(true); + } else if (event.getBlock().isLiquid()) { + final org.bukkit.Location location = event.getBlock().getLocation(); + + /* + X = block location + A-H = potential plot locations + + Z + ^ + | A B C + o D X E + | F G H + v + <-----O-----> x + */ + if (BukkitUtil.getPlot(location.clone().add(-1, 0, 1) /* A */) != null + || BukkitUtil.getPlot(location.clone().add(1, 0, 0) /* B */) != null + || BukkitUtil.getPlot(location.clone().add(1, 0, 1) /* C */) != null + || BukkitUtil.getPlot(location.clone().add(-1, 0, 0) /* D */) != null + || BukkitUtil.getPlot(location.clone().add(1, 0, 0) /* E */) != null + || BukkitUtil.getPlot(location.clone().add(-1, 0, -1) /* F */) != null + || BukkitUtil.getPlot(location.clone().add(0, 0, -1) /* G */) != null + || BukkitUtil.getPlot(location.clone().add(1, 0, 1) /* H */) != null) { + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onGrow(BlockGrowEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + if (location.isUnownedPlotArea()) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPistonExtend(BlockPistonExtendEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + BlockFace face = event.getDirection(); + Vector relative = new Vector(face.getModX(), face.getModY(), face.getModZ()); + PlotArea area = location.getPlotArea(); + if (area == null) { + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + for (Block block1 : event.getBlocks()) { + Location bloc = BukkitUtil.getLocation(block1.getLocation()); + if (bloc.isPlotArea() || bloc + .add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) + .isPlotArea()) { + event.setCancelled(true); + return; + } + } + if (location.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) + .isPlotArea()) { + // Prevent pistons from extending if they are: bordering a plot + // area, facing inside plot area, and not pushing any blocks + event.setCancelled(true); + } + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + event.setCancelled(true); + return; + } + for (Block block1 : event.getBlocks()) { + Location bloc = BukkitUtil.getLocation(block1.getLocation()); + if (!area.contains(bloc.getX(), bloc.getZ()) || !area + .contains(bloc.getX() + relative.getBlockX(), bloc.getZ() + relative.getBlockZ())) { + event.setCancelled(true); + return; + } + if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot( + bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { + event.setCancelled(true); + return; + } + } + if (!plot.equals(area.getOwnedPlot( + location.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { + // This branch is only necessary to prevent pistons from extending + // if they are: on a plot edge, facing outside the plot, and not + // pushing any blocks + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockPistonRetract(BlockPistonRetractEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + BlockFace face = event.getDirection(); + Vector relative = new Vector(face.getModX(), face.getModY(), face.getModZ()); + PlotArea area = location.getPlotArea(); + if (area == null) { + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + for (Block block1 : event.getBlocks()) { + Location bloc = BukkitUtil.getLocation(block1.getLocation()); + if (bloc.isPlotArea() || bloc + .add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) + .isPlotArea()) { + event.setCancelled(true); + return; + } + } + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null) { + event.setCancelled(true); + return; + } + for (Block block1 : event.getBlocks()) { + Location bloc = BukkitUtil.getLocation(block1.getLocation()); + if (!area.contains(bloc.getX(), bloc.getZ()) || !area + .contains(bloc.getX() + relative.getBlockX(), bloc.getZ() + relative.getBlockZ())) { + event.setCancelled(true); + return; + } + if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot( + bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { + event.setCancelled(true); + return; + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockDispense(BlockDispenseEvent event) { + Material type = event.getItem().getType(); + switch (type) { + case SHULKER_BOX: + case WHITE_SHULKER_BOX: + case ORANGE_SHULKER_BOX: + case MAGENTA_SHULKER_BOX: + case LIGHT_BLUE_SHULKER_BOX: + case YELLOW_SHULKER_BOX: + case LIME_SHULKER_BOX: + case PINK_SHULKER_BOX: + case GRAY_SHULKER_BOX: + case LIGHT_GRAY_SHULKER_BOX: + case CYAN_SHULKER_BOX: + case PURPLE_SHULKER_BOX: + case BLUE_SHULKER_BOX: + case BROWN_SHULKER_BOX: + case GREEN_SHULKER_BOX: + case RED_SHULKER_BOX: + case BLACK_SHULKER_BOX: + case CARVED_PUMPKIN: + case WITHER_SKELETON_SKULL: + case FLINT_AND_STEEL: + case BONE_MEAL: + case SHEARS: + case GLASS_BOTTLE: + case GLOWSTONE: + case COD_BUCKET: + case PUFFERFISH_BUCKET: + case SALMON_BUCKET: + case TROPICAL_FISH_BUCKET: + case BUCKET: + case WATER_BUCKET: + case LAVA_BUCKET: { + if (event.getBlock().getType() == Material.DROPPER) { + return; + } + BlockFace targetFace = + ((Directional) event.getBlock().getState().getData()).getFacing(); + Location location = + BukkitUtil.getLocation(event.getBlock().getRelative(targetFace).getLocation()); + if (location.isPlotRoad()) { + event.setCancelled(true); + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onStructureGrow(StructureGrowEvent event) { + if (!PlotSquared.get().hasPlotArea(event.getWorld().getName())) { + return; + } + List blocks = event.getBlocks(); + if (blocks.isEmpty()) { + return; + } + Location location = BukkitUtil.getLocation(blocks.get(0).getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + for (int i = blocks.size() - 1; i >= 0; i--) { + location = BukkitUtil.getLocation(blocks.get(i).getLocation()); + if (location.isPlotArea()) { + blocks.remove(i); + } + } + return; + } else { + Plot origin = area.getOwnedPlot(location); + if (origin == null) { + event.setCancelled(true); + return; + } + for (int i = blocks.size() - 1; i >= 0; i--) { + location = BukkitUtil.getLocation(blocks.get(i).getLocation()); + if (!area.contains(location.getX(), location.getZ())) { + blocks.remove(i); + continue; + } + Plot plot = area.getOwnedPlot(location); + if (!Objects.equals(plot, origin)) { + event.getBlocks().remove(i); + } + } + } + Plot origin = area.getPlot(location); + if (origin == null) { + event.setCancelled(true); + return; + } + for (int i = blocks.size() - 1; i >= 0; i--) { + location = BukkitUtil.getLocation(blocks.get(i).getLocation()); + Plot plot = area.getOwnedPlot(location); + /* + * plot → the base plot of the merged area + * origin → the plot where the event gets called + */ + + // Are plot and origin different AND are both plots merged + if (plot != null && !Objects.equals(plot, origin) && (!plot.isMerged() && !origin + .isMerged())) { + event.getBlocks().remove(i); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBigBoom(BlockExplodeEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + String world = location.getWorld(); + if (!PlotSquared.get().hasPlotArea(world)) { + return; + } + PlotArea area = location.getPlotArea(); + if (area == null) { + Iterator iterator = event.blockList().iterator(); + while (iterator.hasNext()) { + location = BukkitUtil.getLocation(iterator.next().getLocation()); + if (location.isPlotArea()) { + iterator.remove(); + } + } + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot == null || !plot.getFlag(ExplosionFlag.class)) { + event.setCancelled(true); + if (plot != null) { + plot.debug("Explosion was cancelled because explosion = false"); + } + } + event.blockList().removeIf(blox -> plot != null && !plot + .equals(area.getOwnedPlot(BukkitUtil.getLocation(blox.getLocation())))); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockBurn(BlockBurnEvent event) { + Block block = event.getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + + Plot plot = location.getOwnedPlot(); + if (plot == null || !plot.getFlag(BlockBurnFlag.class)) { + if (plot != null) { + plot.debug("Block burning was cancelled because block-burn = false"); + } + event.setCancelled(true); + } + + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockIgnite(BlockIgniteEvent event) { + Player player = event.getPlayer(); + Entity ignitingEntity = event.getIgnitingEntity(); + Block block = event.getBlock(); + BlockIgniteEvent.IgniteCause igniteCause = event.getCause(); + Location location1 = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location1.getPlotArea(); + if (area == null) { + return; + } + if (igniteCause == BlockIgniteEvent.IgniteCause.LIGHTNING) { + event.setCancelled(true); + return; + } + + Plot plot = area.getOwnedPlot(location1); + if (player != null) { + BukkitPlayer pp = BukkitUtil.getPlayer(player); + if (plot == null) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_ROAD); + event.setCancelled(true); + } + } else if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_UNOWNED); + event.setCancelled(true); + } + } else if (!plot.isAdded(pp.getUUID())) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } + } else if (!plot.getFlag(BlockIgnitionFlag.class)) { + event.setCancelled(true); + } + } else { + if (plot == null) { + event.setCancelled(true); + return; + } + if (ignitingEntity != null) { + if (!plot.getFlag(BlockIgnitionFlag.class)) { + event.setCancelled(true); + plot.debug("Block ignition was cancelled because block-ignition = false"); + return; + } + if (igniteCause == BlockIgniteEvent.IgniteCause.FIREBALL) { + if (ignitingEntity instanceof Fireball) { + Projectile fireball = (Projectile) ignitingEntity; + Location location = null; + if (fireball.getShooter() instanceof Entity) { + Entity shooter = (Entity) fireball.getShooter(); + location = BukkitUtil.getLocation(shooter.getLocation()); + } else if (fireball.getShooter() instanceof BlockProjectileSource) { + Block shooter = + ((BlockProjectileSource) fireball.getShooter()).getBlock(); + location = BukkitUtil.getLocation(shooter.getLocation()); + } + if (location != null && !plot.equals(location.getPlot())) { + event.setCancelled(true); + } + } + } + + } else if (event.getIgnitingBlock() != null) { + Block ignitingBlock = event.getIgnitingBlock(); + Plot plotIgnited = BukkitUtil.getLocation(ignitingBlock.getLocation()).getPlot(); + if (igniteCause == BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL && ( + !plot.getFlag(BlockIgnitionFlag.class) || plotIgnited == null || !plotIgnited + .equals(plot)) || (igniteCause == BlockIgniteEvent.IgniteCause.SPREAD + || igniteCause == BlockIgniteEvent.IgniteCause.LAVA) && ( + !plot.getFlag(BlockIgnitionFlag.class) || plotIgnited == null || !plotIgnited + .equals(plot))) { + event.setCancelled(true); + } + } + } + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java new file mode 100644 index 000000000..8de57cdaf --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java @@ -0,0 +1,317 @@ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.util.BukkitEntityUtil; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.flag.implementations.DisablePhysicsFlag; +import com.plotsquared.core.plot.flag.implementations.ExplosionFlag; +import com.plotsquared.core.plot.flag.implementations.InvincibleFlag; +import com.plotsquared.core.plot.flag.implementations.MobPlaceFlag; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Ageable; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.FallingBlock; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.entity.Vehicle; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityCombustByEntityEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.ExplosionPrimeEvent; +import org.bukkit.event.vehicle.VehicleCreateEvent; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; +import org.bukkit.plugin.Plugin; + +import java.util.Iterator; +import java.util.List; + +@SuppressWarnings("unused") +public class EntityEventListener implements Listener { + + private float lastRadius; + + @EventHandler(priority = EventPriority.HIGHEST) + public void onEntityCombustByEntity(EntityCombustByEntityEvent event) { + EntityDamageByEntityEvent eventChange = + new EntityDamageByEntityEvent(event.getCombuster(), event.getEntity(), + EntityDamageEvent.DamageCause.FIRE_TICK, event.getDuration()); + onEntityDamageByEntityEvent(eventChange); + if (eventChange.isCancelled()) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) { + Entity damager = event.getDamager(); + Location location = BukkitUtil.getLocation(damager); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + Entity victim = event.getEntity(); +/* + if (victim.getType().equals(EntityType.ITEM_FRAME)) { + Plot plot = BukkitUtil.getLocation(victim).getPlot(); + if (plot != null && !plot.isAdded(damager.getUniqueId())) { + event.setCancelled(true); + return; + } + } +*/ + if (!BukkitEntityUtil.entityDamage(damager, victim, event.getCause())) { + if (event.isCancelled()) { + if (victim instanceof Ageable) { + Ageable ageable = (Ageable) victim; + if (ageable.getAge() == -24000) { + ageable.setAge(0); + ageable.setAdult(); + } + } + } + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void creatureSpawnEvent(CreatureSpawnEvent event) { + Entity entity = event.getEntity(); + Location location = BukkitUtil.getLocation(entity.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + CreatureSpawnEvent.SpawnReason reason = event.getSpawnReason(); + switch (reason.toString()) { + case "DISPENSE_EGG": + case "EGG": + case "OCELOT_BABY": + case "SPAWNER_EGG": + if (!area.isSpawnEggs()) { + event.setCancelled(true); + return; + } + break; + case "REINFORCEMENTS": + case "NATURAL": + case "MOUNT": + case "PATROL": + case "RAID": + case "SHEARED": + case "SHOULDER_ENTITY": + case "SILVERFISH_BLOCK": + case "TRAP": + case "VILLAGE_DEFENSE": + case "VILLAGE_INVASION": + case "BEEHIVE": + case "CHUNK_GEN": + if (!area.isMobSpawning()) { + event.setCancelled(true); + return; + } + case "BREEDING": + if (!area.isSpawnBreeding()) { + event.setCancelled(true); + return; + } + break; + case "BUILD_IRONGOLEM": + case "BUILD_SNOWMAN": + case "BUILD_WITHER": + case "CUSTOM": + if (!area.isSpawnCustom() && entity.getType() != EntityType.ARMOR_STAND) { + event.setCancelled(true); + return; + } + break; + case "SPAWNER": + if (!area.isMobSpawnerSpawning()) { + event.setCancelled(true); + return; + } + break; + } + Plot plot = area.getOwnedPlotAbs(location); + if (plot == null) { + if (!area.isMobSpawning()) { + event.setCancelled(true); + } + return; + } + if (BukkitEntityUtil.checkEntity(entity, plot)) { + event.setCancelled(true); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void onEntityFall(EntityChangeBlockEvent event) { + if (event.getEntityType() != EntityType.FALLING_BLOCK) { + return; + } + Block block = event.getBlock(); + World world = block.getWorld(); + String worldName = world.getName(); + if (!PlotSquared.get().hasPlotArea(worldName)) { + return; + } + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlotAbs(location); + if (plot == null || plot.getFlag(DisablePhysicsFlag.class)) { + event.setCancelled(true); + if (plot != null) { + plot.debug("Fallin block event was cancelled because disable-physics = true"); + } + return; + } + if (event.getTo().hasGravity()) { + Entity entity = event.getEntity(); + List meta = entity.getMetadata("plot"); + if (meta.isEmpty()) { + return; + } + Plot origin = (Plot) meta.get(0).value(); + if (origin != null && !origin.equals(plot)) { + event.setCancelled(true); + entity.remove(); + } + } else if (event.getTo() == Material.AIR) { + event.getEntity() + .setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); + } + } + + @EventHandler(priority = EventPriority.HIGH) public void onDamage(EntityDamageEvent event) { + if (event.getEntityType() != EntityType.PLAYER) { + return; + } + Location location = BukkitUtil.getLocation(event.getEntity()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && area.getRoadFlag(InvincibleFlag.class)) { + event.setCancelled(true); + } + return; + } + if (plot.getFlag(InvincibleFlag.class)) { + plot.debug( + event.getEntity().getName() + " could not take damage because invincible = true"); + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBigBoom(EntityExplodeEvent event) { + Location location = BukkitUtil.getLocation(event.getLocation()); + PlotArea area = location.getPlotArea(); + boolean plotArea = location.isPlotArea(); + if (!plotArea) { + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + return; + } + Plot plot = area.getOwnedPlot(location); + if (plot != null) { + if (plot.getFlag(ExplosionFlag.class)) { + List meta = event.getEntity().getMetadata("plot"); + Plot origin; + if (meta.isEmpty()) { + origin = plot; + } else { + origin = (Plot) meta.get(0).value(); + } + if (this.lastRadius != 0) { + List nearby = event.getEntity() + .getNearbyEntities(this.lastRadius, this.lastRadius, this.lastRadius); + for (Entity near : nearby) { + if (near instanceof TNTPrimed || near.getType() + .equals(EntityType.MINECART_TNT)) { + if (!near.hasMetadata("plot")) { + near.setMetadata("plot", + new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); + } + } + } + this.lastRadius = 0; + } + Iterator iterator = event.blockList().iterator(); + while (iterator.hasNext()) { + Block block = iterator.next(); + location = BukkitUtil.getLocation(block.getLocation()); + if (!area.contains(location.getX(), location.getZ()) || (origin != null + && !origin.equals(area.getOwnedPlot(location)))) { + iterator.remove(); + } + } + return; + } else { + plot.debug("Explosion was cancelled because explosion = false"); + } + } + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPeskyMobsChangeTheWorldLikeWTFEvent(EntityChangeBlockEvent event) { + Entity e = event.getEntity(); + if (!(e instanceof FallingBlock)) { + Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + PlotArea area = location.getPlotArea(); + if (area != null) { + Plot plot = area.getOwnedPlot(location); + if (plot != null && plot.getFlag(MobPlaceFlag.class)) { + System.out.println("bb"); + return; + } + if (plot != null) { + plot.debug(e.getType() + " could not change block because mob-place = false"); + } + System.out.println("aa"); + event.setCancelled(true); + } + } + } + + @EventHandler public void onPrime(ExplosionPrimeEvent event) { + this.lastRadius = event.getRadius() + 1; + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onVehicleCreate(VehicleCreateEvent event) { + Vehicle entity = event.getVehicle(); + Location location = BukkitUtil.getLocation(entity); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getOwnedPlotAbs(location); + if (plot == null || BukkitEntityUtil.checkEntity(entity, plot)) { + entity.remove(); + return; + } + if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { + entity + .setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); + } + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java index 0bf5e75e8..4b80909e7 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java @@ -25,6 +25,7 @@ */ package com.plotsquared.bukkit.listener; +import com.plotsquared.bukkit.util.BukkitEntityUtil; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; @@ -155,7 +156,7 @@ public class EntitySpawnListener implements Listener { } switch (entity.getType()) { case ENDER_CRYSTAL: - if (PlayerEvents.checkEntity(entity, plot)) { + if (BukkitEntityUtil.checkEntity(entity, plot)) { event.setCancelled(true); } case SHULKER: diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java new file mode 100644 index 000000000..e2bb3cb36 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -0,0 +1,1518 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.listener; + +import com.destroystokyo.paper.MaterialTags; +import com.google.common.base.Charsets; +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.bukkit.util.BukkitEntityUtil; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.bukkit.util.UpdateUtility; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.listener.PlayerBlockEventType; +import com.plotsquared.core.listener.PlotListener; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.PlotId; +import com.plotsquared.core.plot.PlotInventory; +import com.plotsquared.core.plot.flag.FlagContainer; +import com.plotsquared.core.plot.flag.implementations.AnimalInteractFlag; +import com.plotsquared.core.plot.flag.implementations.BlockedCmdsFlag; +import com.plotsquared.core.plot.flag.implementations.ChatFlag; +import com.plotsquared.core.plot.flag.implementations.DenyTeleportFlag; +import com.plotsquared.core.plot.flag.implementations.DoneFlag; +import com.plotsquared.core.plot.flag.implementations.DropProtectionFlag; +import com.plotsquared.core.plot.flag.implementations.HangingBreakFlag; +import com.plotsquared.core.plot.flag.implementations.HangingPlaceFlag; +import com.plotsquared.core.plot.flag.implementations.HostileInteractFlag; +import com.plotsquared.core.plot.flag.implementations.ItemDropFlag; +import com.plotsquared.core.plot.flag.implementations.KeepInventoryFlag; +import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; +import com.plotsquared.core.plot.flag.implementations.PlayerInteractFlag; +import com.plotsquared.core.plot.flag.implementations.PreventCreativeCopyFlag; +import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; +import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; +import com.plotsquared.core.plot.flag.implementations.UseFlag; +import com.plotsquared.core.plot.flag.implementations.VehicleBreakFlag; +import com.plotsquared.core.plot.flag.implementations.VehicleUseFlag; +import com.plotsquared.core.plot.flag.implementations.VillagerInteractFlag; +import com.plotsquared.core.plot.flag.types.BlockTypeWrapper; +import com.plotsquared.core.plot.message.PlotMessage; +import com.plotsquared.core.util.MainUtil; +import com.plotsquared.core.util.MathMan; +import com.plotsquared.core.util.Permissions; +import com.plotsquared.core.util.PremiumVerification; +import com.plotsquared.core.util.RegExUtil; +import com.plotsquared.core.util.entity.EntityCategories; +import com.plotsquared.core.util.task.TaskManager; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.world.block.BlockType; +import io.papermc.lib.PaperLib; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.FluidCollisionMode; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.command.PluginCommand; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.ItemFrame; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.Tameable; +import org.bukkit.entity.Vehicle; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.EntityPickupItemEvent; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.hanging.HangingBreakByEntityEvent; +import org.bukkit.event.hanging.HangingPlaceEvent; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.AsyncPlayerPreLoginEvent; +import org.bukkit.event.player.PlayerBucketEmptyEvent; +import org.bukkit.event.player.PlayerBucketFillEvent; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerEggThrowEvent; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.vehicle.VehicleDestroyEvent; +import org.bukkit.event.vehicle.VehicleEntityCollisionEvent; +import org.bukkit.event.vehicle.VehicleMoveEvent; +import org.bukkit.help.HelpTopic; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; +import org.bukkit.plugin.Plugin; +import org.bukkit.util.Vector; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.regex.Pattern; + +/** + * Player Events involving plots. + */ +@SuppressWarnings("unused") +public class PlayerEventListener extends PlotListener implements Listener { + + // To prevent recursion + private boolean tmpTeleport = true; + private Field fieldPlayer; + private PlayerMoveEvent moveTmp; + private String internalVersion; + + { + try { + fieldPlayer = PlayerEvent.class.getDeclaredField("player"); + fieldPlayer.setAccessible(true); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + + @EventHandler public void onVehicleEntityCollision(VehicleEntityCollisionEvent e) { + if (e.getVehicle().getType() == EntityType.BOAT) { + Location location = BukkitUtil.getLocation(e.getEntity()); + if (location.isPlotArea()) { + if (e.getEntity() instanceof Player) { + PlotPlayer player = BukkitUtil.getPlayer((Player) e.getEntity()); + Plot plot = player.getCurrentPlot(); + if (plot != null) { + if (!plot.isAdded(player.getUUID())) { + //Here the event is only canceled if the player is not the owner + //of the property on which he is located. + e.setCancelled(true); + } + } else { + e.setCancelled(true); + } + } else { + //Here the event is cancelled too, otherwise you can move the + //boat with EchoPets or other mobs running around on the plot. + e.setCancelled(true); + } + } + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void playerCommand(PlayerCommandPreprocessEvent event) { + String msg = event.getMessage().toLowerCase().replaceAll("/", "").trim(); + if (msg.isEmpty()) { + return; + } + Player player = event.getPlayer(); + PlotPlayer plotPlayer = BukkitUtil.getPlayer(player); + Location location = plotPlayer.getLocation(); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + String[] parts = msg.split(" "); + Plot plot = plotPlayer.getCurrentPlot(); + // Check WorldEdit + switch (parts[0].toLowerCase()) { + case "up": + case "/up": + case "worldedit:up": + case "worldedit:/up": + if (plot == null || (!plot.isAdded(plotPlayer.getUUID()) && !Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER, true))) { + event.setCancelled(true); + return; + } + } + if (plot == null && !area.isRoadFlags()) { + return; + } + + List blockedCommands = plot != null ? + plot.getFlag(BlockedCmdsFlag.class) : + area.getFlag(BlockedCmdsFlag.class); + if (!blockedCommands.isEmpty() && !Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) { + String part = parts[0]; + if (parts[0].contains(":")) { + part = parts[0].split(":")[1]; + msg = msg.replace(parts[0].split(":")[0] + ':', ""); + } + String s1 = part; + List aliases = new ArrayList<>(); + for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { + if (part.equals(cmdLabel.getName())) { + break; + } + String label = cmdLabel.getName().replaceFirst("/", ""); + if (aliases.contains(label)) { + continue; + } + PluginCommand p; + if ((p = Bukkit.getPluginCommand(label)) != null) { + for (String a : p.getAliases()) { + if (aliases.contains(a)) { + continue; + } + aliases.add(a); + a = a.replaceFirst("/", ""); + if (!a.equals(label) && a.equals(part)) { + part = label; + break; + } + } + } + } + if (!s1.equals(part)) { + msg = msg.replace(s1, part); + } + for (String s : blockedCommands) { + Pattern pattern; + if (!RegExUtil.compiledPatterns.containsKey(s)) { + RegExUtil.compiledPatterns.put(s, pattern = Pattern.compile(s)); + } else { + pattern = RegExUtil.compiledPatterns.get(s); + } + if (pattern.matcher(msg).matches()) { + String perm; + if (plot != null && plot.isAdded(plotPlayer.getUUID())) { + perm = "plots.admin.command.blocked-cmds.shared"; + } else { + perm = "plots.admin.command.blocked-cmds.road"; + } + if (!Permissions.hasPermission(plotPlayer, perm)) { + MainUtil.sendMessage(plotPlayer, Captions.COMMAND_BLOCKED); + event.setCancelled(true); + } + return; + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPreLoin(final AsyncPlayerPreLoginEvent event) { + final UUID uuid; + if (Settings.UUID.OFFLINE) { + if (Settings.UUID.FORCE_LOWERCASE) { + uuid = UUID.nameUUIDFromBytes( + ("OfflinePlayer:" + event.getName().toLowerCase()).getBytes(Charsets.UTF_8)); + } else { + uuid = UUID.nameUUIDFromBytes( + ("OfflinePlayer:" + event.getName()).getBytes(Charsets.UTF_8)); + } + } else { + uuid = event.getUniqueId(); + } + PlotSquared.get().getImpromptuUUIDPipeline().storeImmediately(event.getName(), uuid); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onConnect(PlayerJoinEvent event) { + final Player player = event.getPlayer(); + BukkitUtil.removePlayer(player.getUniqueId()); + final PlotPlayer pp = BukkitUtil.getPlayer(player); + + Location location = pp.getLocation(); + PlotArea area = location.getPlotArea(); + if (area != null) { + Plot plot = area.getPlot(location); + if (plot != null) { + plotEntry(pp, plot); + } + } + // Delayed + + // Async + TaskManager.runTaskLaterAsync(() -> { + if (!player.hasPlayedBefore() && player.isOnline()) { + player.saveData(); + } + PlotSquared.get().getEventDispatcher().doJoinTask(pp); + }, 20); + + if (pp.hasPermission(Captions.PERMISSION_ADMIN_UPDATE_NOTIFICATION.getTranslated()) + && Settings.Enabled_Components.UPDATE_NOTIFICATIONS && PremiumVerification.isPremium() + && UpdateUtility.hasUpdate) { + new PlotMessage("-----------------------------------").send(pp); + new PlotMessage(Captions.PREFIX + "There appears to be a PlotSquared update available!") + .color("$1").send(pp); + new PlotMessage( + Captions.PREFIX + "&6You are running version " + UpdateUtility.internalVersion + .versionString() + ", &6latest version is " + UpdateUtility.spigotVersion) + .color("$1").send(pp); + new PlotMessage(Captions.PREFIX + "Download at:").color("$1").send(pp); + player.sendMessage(" https://www.spigotmc.org/resources/77506/updates"); + new PlotMessage("-----------------------------------").send(pp); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void playerRespawn(PlayerRespawnEvent event) { + Player player = event.getPlayer(); + PlotPlayer pp = BukkitUtil.getPlayer(player); + PlotSquared.get().getEventDispatcher().doRespawnTask(pp); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onTeleport(PlayerTeleportEvent event) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); + org.bukkit.Location to = event.getTo(); + //noinspection ConstantConditions + if (to != null) { + Location location = BukkitUtil.getLocation(to); + PlotArea area = location.getPlotArea(); + if (area == null) { + if (lastPlot != null) { + plotExit(pp, lastPlot); + pp.deleteMeta(PlotPlayer.META_LAST_PLOT); + } + pp.deleteMeta(PlotPlayer.META_LOCATION); + return; + } + Plot plot = area.getPlot(location); + if (plot != null) { + final boolean result = DenyTeleportFlag.allowsTeleport(pp, plot); + // there is one possibility to still allow teleportation: + // to is identical to the plot's home location, and untrusted-visit is true + // i.e. untrusted-visit can override deny-teleport + // this is acceptable, because otherwise it wouldn't make sense to have both flags set + if (!result && !(plot.getFlag(UntrustedVisitFlag.class) && plot.getHomeSynchronous() + .equals(BukkitUtil.getLocationFull(to)))) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_ENTRY_DENIED); + event.setCancelled(true); + } + } + } + playerMove(event); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void vehicleMove(VehicleMoveEvent event) throws IllegalAccessException { + final org.bukkit.Location from = event.getFrom(); + final org.bukkit.Location to = event.getTo(); + + int toX, toZ; + if ((toX = MathMan.roundInt(to.getX())) != MathMan.roundInt(from.getX()) + | (toZ = MathMan.roundInt(to.getZ())) != MathMan.roundInt(from.getZ())) { + Vehicle vehicle = event.getVehicle(); + + // Check allowed + if (!vehicle.getPassengers().isEmpty()) { + Entity passenger = vehicle.getPassengers().get(0); + + if (passenger instanceof Player) { + final Player player = (Player) passenger; + // reset + if (moveTmp == null) { + moveTmp = new PlayerMoveEvent(null, from, to); + } + moveTmp.setFrom(from); + moveTmp.setTo(to); + moveTmp.setCancelled(false); + fieldPlayer.set(moveTmp, player); + + List passengers = vehicle.getPassengers(); + + this.playerMove(moveTmp); + org.bukkit.Location dest; + if (moveTmp.isCancelled()) { + dest = from; + } else if (MathMan.roundInt(moveTmp.getTo().getX()) != toX + || MathMan.roundInt(moveTmp.getTo().getZ()) != toZ) { + dest = to; + } else { + dest = null; + } + if (dest != null) { + vehicle.eject(); + vehicle.setVelocity(new Vector(0d, 0d, 0d)); + PaperLib.teleportAsync(vehicle, dest); + passengers.forEach(vehicle::addPassenger); + return; + } + } + if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { + final com.sk89q.worldedit.world.entity.EntityType entityType = + BukkitAdapter.adapt(vehicle.getType()); + // Horses etc are vehicles, but they're also animals + // so this filters out all living entities + if (EntityCategories.VEHICLE.contains(entityType) && !EntityCategories.ANIMAL + .contains(entityType)) { + List meta = vehicle.getMetadata("plot"); + Plot toPlot = BukkitUtil.getLocation(to).getPlot(); + if (!meta.isEmpty()) { + Plot origin = (Plot) meta.get(0).value(); + if (origin != null && !origin.getBasePlot(false).equals(toPlot)) { + vehicle.remove(); + } + } else if (toPlot != null) { + vehicle.setMetadata("plot", + new FixedMetadataValue((Plugin) PlotSquared.get().IMP, toPlot)); + } + } + } + } + + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void playerMove(PlayerMoveEvent event) { + org.bukkit.Location from = event.getFrom(); + org.bukkit.Location to = event.getTo(); + int x2; + if (MathMan.roundInt(from.getX()) != (x2 = MathMan.roundInt(to.getX()))) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + // Cancel teleport + if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { + MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); + } + // Set last location + Location location = BukkitUtil.getLocation(to); + pp.setMeta(PlotPlayer.META_LOCATION, location); + PlotArea area = location.getPlotArea(); + if (area == null) { + pp.deleteMeta(PlotPlayer.META_LAST_PLOT); + return; + } + Plot now = area.getPlot(location); + Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); + if (now == null) { + if (lastPlot != null && !plotExit(pp, lastPlot) && this.tmpTeleport && !pp + .getMeta("kick", false)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_EXIT_DENIED); + this.tmpTeleport = false; + if (lastPlot.equals(BukkitUtil.getLocation(from).getPlot())) { + player.teleport(from); + } else { + player.teleport(player.getWorld().getSpawnLocation()); + } + this.tmpTeleport = true; + event.setCancelled(true); + return; + } + } else if (now.equals(lastPlot)) { + ForceFieldListener.handleForcefield(player, pp, now); + } else if (!plotEntry(pp, now) && this.tmpTeleport) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_ENTRY_DENIED); + this.tmpTeleport = false; + to.setX(from.getBlockX()); + to.setY(from.getBlockY()); + to.setZ(from.getBlockZ()); + player.teleport(event.getTo()); + this.tmpTeleport = true; + return; + } + int border = area.getBorder(); + if (x2 > border && this.tmpTeleport) { + to.setX(border - 1); + this.tmpTeleport = false; + player.teleport(event.getTo()); + this.tmpTeleport = true; + MainUtil.sendMessage(pp, Captions.BORDER); + } + if (x2 < -border && this.tmpTeleport) { + to.setX(-border + 1); + this.tmpTeleport = false; + player.teleport(event.getTo()); + this.tmpTeleport = true; + MainUtil.sendMessage(pp, Captions.BORDER); + } + } + int z2; + if (MathMan.roundInt(from.getZ()) != (z2 = MathMan.roundInt(to.getZ()))) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + // Cancel teleport + if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { + MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); + } + // Set last location + Location location = BukkitUtil.getLocation(to); + pp.setMeta(PlotPlayer.META_LOCATION, location); + PlotArea area = location.getPlotArea(); + if (area == null) { + pp.deleteMeta(PlotPlayer.META_LAST_PLOT); + return; + } + Plot now = area.getPlot(location); + Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); + if (now == null) { + if (lastPlot != null && !plotExit(pp, lastPlot) && this.tmpTeleport && !pp + .getMeta("kick", false)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_EXIT_DENIED); + this.tmpTeleport = false; + if (lastPlot.equals(BukkitUtil.getLocation(from).getPlot())) { + player.teleport(from); + } else { + player.teleport(player.getWorld().getSpawnLocation()); + } + this.tmpTeleport = true; + event.setCancelled(true); + return; + } + } else if (now.equals(lastPlot)) { + ForceFieldListener.handleForcefield(player, pp, now); + } else if (!plotEntry(pp, now) && this.tmpTeleport) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_ENTRY_DENIED); + this.tmpTeleport = false; + player.teleport(from); + to.setX(from.getBlockX()); + to.setY(from.getBlockY()); + to.setZ(from.getBlockZ()); + player.teleport(event.getTo()); + this.tmpTeleport = true; + return; + } + int border = area.getBorder(); + if (z2 > border && this.tmpTeleport) { + to.setZ(border - 1); + this.tmpTeleport = false; + player.teleport(event.getTo()); + this.tmpTeleport = true; + MainUtil.sendMessage(pp, Captions.BORDER); + } else if (z2 < -border && this.tmpTeleport) { + to.setZ(-border + 1); + this.tmpTeleport = false; + player.teleport(event.getTo()); + this.tmpTeleport = true; + MainUtil.sendMessage(pp, Captions.BORDER); + } + } + } + + @EventHandler(priority = EventPriority.LOW) public void onChat(AsyncPlayerChatEvent event) { + if (event.isCancelled()) { + return; + } + + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(event.getPlayer()); + Location location = plotPlayer.getLocation(); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getPlot(location); + if (plot == null) { + return; + } + if (!((plot.getFlag(ChatFlag.class) && area.isPlotChat() && plotPlayer.getAttribute("chat")) + || area.isForcingPlotChat())) { + return; + } + if (plot.isDenied(plotPlayer.getUUID()) && !Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_CHAT_BYPASS)) { + return; + } + event.setCancelled(true); + String message = event.getMessage(); + String format = Captions.PLOT_CHAT_FORMAT.getTranslated(); + String sender = event.getPlayer().getDisplayName(); + PlotId id = plot.getId(); + Set recipients = event.getRecipients(); + recipients.clear(); + Set spies = new HashSet<>(); + for (final PlotPlayer pp : PlotSquared.imp().getPlayerManager().getPlayers()) { + if (pp.getAttribute("chatspy")) { + spies.add(((BukkitPlayer) pp).player); + } else { + Plot current = pp.getCurrentPlot(); + if (current != null && current.getBasePlot(false).equals(plot)) { + recipients.add(((BukkitPlayer) pp).player); + } + } + } + String partial = ChatColor.translateAlternateColorCodes('&', + format.replace("%plot_id%", id.getX() + ";" + id.getY()).replace("%sender%", sender)); + if (plotPlayer.hasPermission("plots.chat.color")) { + message = Captions.color(message); + } + String full = partial.replace("%msg%", message); + for (Player receiver : recipients) { + receiver.sendMessage(full); + } + if (!spies.isEmpty()) { + String spyMessage = Captions.PLOT_CHAT_SPY_FORMAT.getTranslated() + .replace("%plot_id%", id.getX() + ";" + id.getY()).replace("%sender%", sender) + .replace("%msg%", message); + for (Player player : spies) { + player.sendMessage(spyMessage); + } + } + PlotSquared.debug(full); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onWorldChanged(PlayerChangedWorldEvent event) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + // Delete last location + Plot plot = (Plot) pp.deleteMeta(PlotPlayer.META_LAST_PLOT); + pp.deleteMeta(PlotPlayer.META_LOCATION); + if (plot != null) { + plotExit(pp, plot); + } + if (PlotSquared.get().worldedit != null) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_WORLDEDIT_BYPASS)) { + if (pp.getAttribute("worldedit")) { + pp.removeAttribute("worldedit"); + } + } + } + if (Settings.Enabled_Components.PERMISSION_CACHE) { + pp.deleteMeta("perm"); + } + Location location = pp.getLocation(); + PlotArea area = location.getPlotArea(); + if (location.isPlotArea()) { + plot = location.getPlot(); + if (plot != null) { + plotEntry(pp, plot); + } + } + } + + @SuppressWarnings("deprecation") + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onInventoryClick(InventoryClickEvent event) { + /*if (!event.isLeftClick() || (event.getAction() != InventoryAction.PLACE_ALL) || event + .isShiftClick()) { + return; + }*/ + HumanEntity entity = event.getWhoClicked(); + if (!(entity instanceof Player) || !PlotSquared.get() + .hasPlotArea(entity.getWorld().getName())) { + return; + } + + HumanEntity clicker = event.getWhoClicked(); + if (!(clicker instanceof Player)) { + return; + } + Player player = (Player) clicker; + BukkitPlayer pp = BukkitUtil.getPlayer(player); + final PlotInventory inventory = PlotInventory.getOpenPlotInventory(pp); + if (inventory != null && event.getRawSlot() == event.getSlot()) { + if (!inventory.onClick(event.getSlot())) { + event.setResult(Event.Result.DENY); + event.setCancelled(true); + inventory.close(); + } + } + PlayerInventory inv = player.getInventory(); + int slot = inv.getHeldItemSlot(); + if ((slot > 8) || !event.getEventName().equals("InventoryCreativeEvent")) { + return; + } + ItemStack current = inv.getItemInHand(); + ItemStack newItem = event.getCursor(); + if (newItem == null) { + return; + } + ItemMeta newMeta = newItem.getItemMeta(); + ItemMeta oldMeta = newItem.getItemMeta(); + + if (event.getClick() == ClickType.CREATIVE) { + final Plot plot = pp.getCurrentPlot(); + if (plot != null) { + if (plot.getFlag(PreventCreativeCopyFlag.class) && !plot + .isAdded(player.getUniqueId()) && !Permissions + .hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_OTHER)) { + final ItemStack newStack = + new ItemStack(newItem.getType(), newItem.getAmount()); + event.setCursor(newStack); + plot.debug(player.getName() + + " could not creative-copy an item because prevent-creative-copy = true"); + } + } else { + PlotArea area = pp.getPlotAreaAbs(); + if (area != null && area.isRoadFlags() && area + .getRoadFlag(PreventCreativeCopyFlag.class)) { + final ItemStack newStack = + new ItemStack(newItem.getType(), newItem.getAmount()); + event.setCursor(newStack); + } + } + return; + } + + String newLore = ""; + if (newMeta != null) { + List lore = newMeta.getLore(); + if (lore != null) { + newLore = lore.toString(); + } + } + String oldLore = ""; + if (oldMeta != null) { + List lore = oldMeta.getLore(); + if (lore != null) { + oldLore = lore.toString(); + } + } + if (!"[(+NBT)]".equals(newLore) || (current.equals(newItem) && newLore.equals(oldLore))) { + switch (newItem.getType()) { + case LEGACY_BANNER: + case PLAYER_HEAD: + if (newMeta != null) { + break; + } + default: + return; + } + } + Block block = player.getTargetBlock(null, 7); + org.bukkit.block.BlockState state = block.getState(); + Material stateType = state.getType(); + Material itemType = newItem.getType(); + if (stateType != itemType) { + switch (stateType) { + case LEGACY_STANDING_BANNER: + case LEGACY_WALL_BANNER: + if (itemType == Material.LEGACY_BANNER) { + break; + } + case LEGACY_SKULL: + if (itemType == Material.LEGACY_SKULL_ITEM) { + break; + } + default: + return; + } + } + Location location = BukkitUtil.getLocation(state.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = area.getPlotAbs(location); + boolean cancelled = false; + if (plot == null) { + if (!Permissions.hasPermission(pp, "plots.admin.interact.road")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.road"); + cancelled = true; + } + } else if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, "plots.admin.interact.unowned")) { + MainUtil + .sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.unowned"); + cancelled = true; + } + } else { + UUID uuid = pp.getUUID(); + if (!plot.isAdded(uuid)) { + if (!Permissions.hasPermission(pp, "plots.admin.interact.other")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.interact.other"); + cancelled = true; + } + } + } + if (cancelled) { + if ((current.getType() == newItem.getType()) && (current.getDurability() == newItem + .getDurability())) { + event.setCursor( + new ItemStack(newItem.getType(), newItem.getAmount(), newItem.getDurability())); + event.setCancelled(true); + return; + } + event.setCursor( + new ItemStack(newItem.getType(), newItem.getAmount(), newItem.getDurability())); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onInteract(PlayerInteractAtEntityEvent e) { + Entity entity = e.getRightClicked(); + if (!(entity instanceof ArmorStand) && !(entity instanceof ItemFrame)) { + return; + } + Location location = BukkitUtil.getLocation(e.getRightClicked().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + EntitySpawnListener.testNether(entity); + Plot plot = location.getPlotAbs(); + BukkitPlayer pp = BukkitUtil.getPlayer(e.getPlayer()); + if (plot == null) { + if (!area.isRoadFlags() && !area.getRoadFlag(MiscInteractFlag.class) && !Permissions + .hasPermission(pp, "plots.admin.interact.road")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.road"); + e.setCancelled(true); + } + } else { + if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + e.setCancelled(true); + return; + } + } + if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, "plots.admin.interact.unowned")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.interact.unowned"); + e.setCancelled(true); + } + } else { + UUID uuid = pp.getUUID(); + if (plot.isAdded(uuid)) { + return; + } + if (plot.getFlag(MiscInteractFlag.class)) { + return; + } + if (!Permissions.hasPermission(pp, "plots.admin.interact.other")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.interact.other"); + e.setCancelled(true); + plot.debug(pp.getName() + " could not interact with " + entity.getType() + + " bcause misc-interact = false"); + } + } + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onCancelledInteract(PlayerInteractEvent event) { + if (event.isCancelled() && event.getAction() == Action.RIGHT_CLICK_AIR) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + PlotArea area = pp.getPlotAreaAbs(); + if (area == null) { + return; + } + if (event.getAction() == Action.RIGHT_CLICK_AIR) { + Material item = event.getMaterial(); + if (item.toString().toLowerCase().endsWith("_egg")) { + event.setCancelled(true); + event.setUseItemInHand(Event.Result.DENY); + } + } + ItemStack hand = player.getInventory().getItemInMainHand(); + ItemStack offHand = player.getInventory().getItemInOffHand(); + Material type = hand.getType(); + Material offType = offHand.getType(); + if (type == Material.AIR) { + type = offType; + } + if (type.toString().toLowerCase().endsWith("_egg")) { + Block block = player.getTargetBlockExact(5, FluidCollisionMode.SOURCE_ONLY); + if (block != null && block.getType() != Material.AIR) { + Location location = BukkitUtil.getLocation(block.getLocation()); + if (!PlotSquared.get().getEventDispatcher() + .checkPlayerBlockEvent(pp, PlayerBlockEventType.SPAWN_MOB, location, null, + true)) { + event.setCancelled(true); + event.setUseItemInHand(Event.Result.DENY); + } + } + } + } + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + PlotArea area = pp.getPlotAreaAbs(); + if (area == null) { + return; + } + PlayerBlockEventType eventType = null; + BlockType blocktype1; + Block block = event.getClickedBlock(); + if (block == null) { + return; + } + Location location = BukkitUtil.getLocation(block.getLocation()); + Action action = event.getAction(); + outer: + switch (action) { + case PHYSICAL: { + eventType = PlayerBlockEventType.TRIGGER_PHYSICAL; + blocktype1 = BukkitAdapter.asBlockType(block.getType()); + System.out.println("cc"); + break; + } + //todo rearrange the right click code. it is all over the place. + case RIGHT_CLICK_BLOCK: { + Material blockType = block.getType(); + eventType = PlayerBlockEventType.INTERACT_BLOCK; + blocktype1 = BukkitAdapter.asBlockType(block.getType()); + + if (blockType.isInteractable()) { + if (!player.isSneaking()) { + break; + } + ItemStack hand = player.getInventory().getItemInMainHand(); + ItemStack offHand = player.getInventory().getItemInOffHand(); + + // sneaking players interact with blocks if both hands are empty + if (hand.getType() == Material.AIR && offHand.getType() == Material.AIR) { + break; + } + } + + Material type = event.getMaterial(); + + // in the following, lb needs to have the material of the item in hand i.e. type + switch (type) { + case REDSTONE: + case STRING: + case PUMPKIN_SEEDS: + case MELON_SEEDS: + case COCOA_BEANS: + case WHEAT_SEEDS: + case BEETROOT_SEEDS: + case SWEET_BERRIES: + return; + default: + //eventType = PlayerBlockEventType.PLACE_BLOCK; + if (type.isBlock()) { + return; + } + } + if (PaperLib.isPaper()) { + if (MaterialTags.SPAWN_EGGS.isTagged(type) || Material.EGG.equals(type)) { + eventType = PlayerBlockEventType.SPAWN_MOB; + break; + } + } else { + if (type.toString().toLowerCase().endsWith("egg")) { + eventType = PlayerBlockEventType.SPAWN_MOB; + break; + } + } + if (type.isEdible()) { + //Allow all players to eat while also allowing the block place event ot be fired + return; + } + switch (type) { + case ACACIA_BOAT: + case BIRCH_BOAT: + case CHEST_MINECART: + case COMMAND_BLOCK_MINECART: + case DARK_OAK_BOAT: + case FURNACE_MINECART: + case HOPPER_MINECART: + case JUNGLE_BOAT: + case MINECART: + case OAK_BOAT: + case SPRUCE_BOAT: + case TNT_MINECART: + eventType = PlayerBlockEventType.PLACE_VEHICLE; + break outer; + case FIREWORK_ROCKET: + case FIREWORK_STAR: + eventType = PlayerBlockEventType.SPAWN_MOB; + break outer; + case BOOK: + case KNOWLEDGE_BOOK: + case WRITABLE_BOOK: + case WRITTEN_BOOK: + eventType = PlayerBlockEventType.READ; + break outer; + case ARMOR_STAND: + location = BukkitUtil + .getLocation(block.getRelative(event.getBlockFace()).getLocation()); + eventType = PlayerBlockEventType.PLACE_MISC; + break outer; + } + break; + } + case LEFT_CLICK_BLOCK: { + location = BukkitUtil.getLocation(block.getLocation()); + //eventType = PlayerBlockEventType.BREAK_BLOCK; + blocktype1 = BukkitAdapter.asBlockType(block.getType()); + if (block.getType() == Material.DRAGON_EGG) { + eventType = PlayerBlockEventType.TELEPORT_OBJECT; + break; + } + + return; + } + default: + return; + } + if (PlotSquared.get().worldedit != null && pp.getAttribute("worldedit")) { + if (event.getMaterial() == Material + .getMaterial(PlotSquared.get().worldedit.getConfiguration().wandItem)) { + return; + } + } + if (!PlotSquared.get().getEventDispatcher() + .checkPlayerBlockEvent(pp, eventType, location, blocktype1, true)) { + System.out.println("dd"); + event.setCancelled(true); + event.setUseInteractedBlock(Event.Result.DENY); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBucketEmpty(PlayerBucketEmptyEvent event) { + BlockFace bf = event.getBlockFace(); + Block block = + event.getBlockClicked().getLocation().add(bf.getModX(), bf.getModY(), bf.getModZ()) + .getBlock(); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + BukkitPlayer pp = BukkitUtil.getPlayer(event.getPlayer()); + Plot plot = area.getPlot(location); + if (plot == null) { + if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { + return; + } + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_ROAD); + event.setCancelled(true); + } else if (!plot.hasOwner()) { + if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { + return; + } + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_UNOWNED); + event.setCancelled(true); + } else if (!plot.isAdded(pp.getUUID())) { + List use = plot.getFlag(UseFlag.class); + final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); + for (final BlockTypeWrapper blockTypeWrapper : use) { + if (blockTypeWrapper.accepts(blockType)) { + return; + } + } + if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + return; + } + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onInventoryClose(InventoryCloseEvent event) { + HumanEntity closer = event.getPlayer(); + if (!(closer instanceof Player)) { + return; + } + Player player = (Player) closer; + PlotInventory.removePlotInventoryOpen(BukkitUtil.getPlayer(player)); + } + + @EventHandler(priority = EventPriority.MONITOR) public void onLeave(PlayerQuitEvent event) { + TaskManager.TELEPORT_QUEUE.remove(event.getPlayer().getName()); + BukkitPlayer pp = BukkitUtil.getPlayer(event.getPlayer()); + pp.unregister(); + PlotListener.logout(pp.getUUID()); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBucketFill(PlayerBucketFillEvent event) { + Block blockClicked = event.getBlockClicked(); + Location location = BukkitUtil.getLocation(blockClicked.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Player player = event.getPlayer(); + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + Plot plot = area.getPlot(location); + if (plot == null) { + if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { + return; + } + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_ROAD); + event.setCancelled(true); + } else if (!plot.hasOwner()) { + if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { + return; + } + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_UNOWNED); + event.setCancelled(true); + } else if (!plot.isAdded(plotPlayer.getUUID())) { + List use = plot.getFlag(UseFlag.class); + Block block = event.getBlockClicked(); + final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); + for (final BlockTypeWrapper blockTypeWrapper : use) { + if (blockTypeWrapper.accepts(blockType)) { + return; + } + } + if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + return; + } + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onHangingPlace(HangingPlaceEvent event) { + Block block = event.getBlock().getRelative(event.getBlockFace()); + Location location = BukkitUtil.getLocation(block.getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Player p = event.getPlayer(); + if (p == null) { + PlotSquared.debug("PlotSquared does not support HangingPlaceEvent for non-players."); + event.setCancelled(true); + return; + } + BukkitPlayer pp = BukkitUtil.getPlayer(p); + Plot plot = area.getPlot(location); + if (plot == null) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_ROAD); + event.setCancelled(true); + } + } else { + if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_UNOWNED); + event.setCancelled(true); + } + return; + } + if (!plot.isAdded(pp.getUUID())) { + if (!plot.getFlag(HangingPlaceFlag.class)) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + event.setCancelled(true); + } + return; + } + } + if (BukkitEntityUtil.checkEntity(event.getEntity(), plot)) { + event.setCancelled(true); + } + + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onHangingBreakByEntity(HangingBreakByEntityEvent event) { + Entity remover = event.getRemover(); + if (remover instanceof Player) { + Player p = (Player) remover; + Location location = BukkitUtil.getLocation(event.getEntity()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + BukkitPlayer pp = BukkitUtil.getPlayer(p); + Plot plot = area.getPlot(location); + if (plot == null) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_ROAD); + event.setCancelled(true); + } + } else if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_UNOWNED); + event.setCancelled(true); + } + } else if (!plot.isAdded(pp.getUUID())) { + if (plot.getFlag(HangingBreakFlag.class)) { + return; + } + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_OTHER); + event.setCancelled(true); + plot.debug(p.getName() + + " could not break hanging entity because hanging-break = false"); + } + } + } else if (remover instanceof Projectile) { + Projectile p = (Projectile) remover; + if (p.getShooter() instanceof Player) { + Player shooter = (Player) p.getShooter(); + Location location = BukkitUtil.getLocation(event.getEntity()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + BukkitPlayer player = BukkitUtil.getPlayer(shooter); + Plot plot = area.getPlot(BukkitUtil.getLocation(event.getEntity())); + if (plot != null) { + if (!plot.hasOwner()) { + if (!Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { + MainUtil.sendMessage(player, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_UNOWNED); + event.setCancelled(true); + } + } else if (!plot.isAdded(player.getUUID())) { + if (!plot.getFlag(HangingBreakFlag.class)) { + if (!Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { + MainUtil.sendMessage(player, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_DESTROY_OTHER); + event.setCancelled(true); + plot.debug(player.getName() + + " could not break hanging entity because hanging-break = false"); + } + } + } + } + } + } else { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + Location location = BukkitUtil.getLocation(event.getRightClicked().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Player p = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(p); + Plot plot = area.getPlot(location); + if (plot == null && !area.isRoadFlags()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_ROAD)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_INTERACT_ROAD); + event.setCancelled(true); + } + } else if (plot != null && !plot.hasOwner()) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_UNOWNED)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_INTERACT_UNOWNED); + event.setCancelled(true); + } + } else if ((plot != null && !plot.isAdded(pp.getUUID())) || (plot == null && area + .isRoadFlags())) { + final Entity entity = event.getRightClicked(); + final com.sk89q.worldedit.world.entity.EntityType entityType = + BukkitAdapter.adapt(entity.getType()); + + FlagContainer flagContainer; + if (plot == null) { + flagContainer = area.getRoadFlagContainer(); + } else { + flagContainer = plot.getFlagContainer(); + } + + if (EntityCategories.HOSTILE.contains(entityType) && flagContainer + .getFlag(HostileInteractFlag.class).getValue()) { + return; + } + + if (EntityCategories.ANIMAL.contains(entityType) && flagContainer + .getFlag(AnimalInteractFlag.class).getValue()) { + return; + } + + // This actually makes use of the interface, so we don't use the + // category + if (entity instanceof Tameable && ((Tameable) entity).isTamed() && flagContainer + .getFlag(TamedInteractFlag.class).getValue()) { + return; + } + + if (EntityCategories.VEHICLE.contains(entityType) && flagContainer + .getFlag(VehicleUseFlag.class).getValue()) { + return; + } + + if (EntityCategories.PLAYER.contains(entityType) && flagContainer + .getFlag(PlayerInteractFlag.class).getValue()) { + return; + } + + if (EntityCategories.VILLAGER.contains(entityType) && flagContainer + .getFlag(VillagerInteractFlag.class).getValue()) { + return; + } + + if ((EntityCategories.HANGING.contains(entityType) || EntityCategories.OTHER + .contains(entityType)) && flagContainer.getFlag(MiscInteractFlag.class) + .getValue()) { + return; + } + + if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_OTHER)) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_INTERACT_OTHER); + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onVehicleDestroy(VehicleDestroyEvent event) { + Location location = BukkitUtil.getLocation(event.getVehicle()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Entity attacker = event.getAttacker(); + if (attacker instanceof Player) { + Player p = (Player) attacker; + BukkitPlayer pp = BukkitUtil.getPlayer(p); + Plot plot = area.getPlot(location); + if (plot == null) { + if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.road")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.vehicle.break.road"); + event.setCancelled(true); + } + } else { + if (!plot.hasOwner()) { + if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.unowned")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.vehicle.break.unowned"); + event.setCancelled(true); + return; + } + return; + } + if (!plot.isAdded(pp.getUUID())) { + if (plot.getFlag(VehicleBreakFlag.class)) { + return; + } + if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.other")) { + MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, + "plots.admin.vehicle.break.other"); + event.setCancelled(true); + plot.debug(pp.getName() + + " could not break vehicle because vehicle-break = false"); + } + } + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerEggThrow(PlayerEggThrowEvent event) { + Location location = BukkitUtil.getLocation(event.getEgg().getLocation()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Player player = event.getPlayer(); + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + Plot plot = area.getPlot(location); + if (plot == null) { + if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.road")) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.projectile.road"); + event.setHatching(false); + } + } else if (!plot.hasOwner()) { + if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.unowned")) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.projectile.unowned"); + event.setHatching(false); + } + } else if (!plot.isAdded(plotPlayer.getUUID())) { + if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.other")) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.projectile.other"); + event.setHatching(false); + } + } + } + + @EventHandler public void onItemDrop(PlayerDropItemEvent event) { + Player player = event.getPlayer(); + BukkitPlayer pp = BukkitUtil.getPlayer(player); + Location location = pp.getLocation(); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && !area.getRoadFlag(ItemDropFlag.class)) { + event.setCancelled(true); + } + return; + } + UUID uuid = pp.getUUID(); + if (!plot.isAdded(uuid)) { + if (!plot.getFlag(ItemDropFlag.class)) { + plot.debug(player.getName() + " could not drop item because of item-drop = false"); + event.setCancelled(true); + } + } + } + + @EventHandler public void onItemPickup(EntityPickupItemEvent event) { + LivingEntity ent = event.getEntity(); + if (ent instanceof Player) { + Player player = (Player) ent; + BukkitPlayer pp = BukkitUtil.getPlayer(player); + Location location = pp.getLocation(); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && area.getRoadFlag(DropProtectionFlag.class)) { + event.setCancelled(true); + } + return; + } + UUID uuid = pp.getUUID(); + if (!plot.isAdded(uuid) && plot.getFlag(DropProtectionFlag.class)) { + plot.debug( + player.getName() + " could not pick up item because of drop-protection = true"); + event.setCancelled(true); + } + } + } + + @EventHandler public void onDeath(final PlayerDeathEvent event) { + Location location = BukkitUtil.getLocation(event.getEntity()); + PlotArea area = location.getPlotArea(); + if (area == null) { + return; + } + Plot plot = location.getOwnedPlot(); + if (plot == null) { + if (area.isRoadFlags() && area.getRoadFlag(KeepInventoryFlag.class)) { + event.setCancelled(true); + } + return; + } + if (plot.getFlag(KeepInventoryFlag.class)) { + if (plot.getFlag(KeepInventoryFlag.class)) { + plot.debug(event.getEntity().getName() + + " kept their inventory because of keep-inventory = true"); + event.setKeepInventory(true); + } + } + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java deleted file mode 100644 index a9cb82336..000000000 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java +++ /dev/null @@ -1,3221 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.bukkit.listener; - -import com.destroystokyo.paper.MaterialTags; -import com.google.common.base.Charsets; -import com.plotsquared.bukkit.player.BukkitPlayer; -import com.plotsquared.bukkit.util.BukkitUtil; -import com.plotsquared.bukkit.util.UpdateUtility; -import com.plotsquared.core.PlotSquared; -import com.plotsquared.core.configuration.Captions; -import com.plotsquared.core.configuration.Settings; -import com.plotsquared.core.database.DBFunc; -import com.plotsquared.core.listener.PlayerBlockEventType; -import com.plotsquared.core.listener.PlotListener; -import com.plotsquared.core.location.Location; -import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.plot.PlotArea; -import com.plotsquared.core.plot.PlotHandler; -import com.plotsquared.core.plot.PlotId; -import com.plotsquared.core.plot.PlotInventory; -import com.plotsquared.core.plot.flag.FlagContainer; -import com.plotsquared.core.plot.flag.implementations.AnimalAttackFlag; -import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag; -import com.plotsquared.core.plot.flag.implementations.AnimalInteractFlag; -import com.plotsquared.core.plot.flag.implementations.BlockBurnFlag; -import com.plotsquared.core.plot.flag.implementations.BlockIgnitionFlag; -import com.plotsquared.core.plot.flag.implementations.BlockedCmdsFlag; -import com.plotsquared.core.plot.flag.implementations.BreakFlag; -import com.plotsquared.core.plot.flag.implementations.ChatFlag; -import com.plotsquared.core.plot.flag.implementations.CoralDryFlag; -import com.plotsquared.core.plot.flag.implementations.DenyTeleportFlag; -import com.plotsquared.core.plot.flag.implementations.DisablePhysicsFlag; -import com.plotsquared.core.plot.flag.implementations.DoneFlag; -import com.plotsquared.core.plot.flag.implementations.DropProtectionFlag; -import com.plotsquared.core.plot.flag.implementations.EntityCapFlag; -import com.plotsquared.core.plot.flag.implementations.ExplosionFlag; -import com.plotsquared.core.plot.flag.implementations.GrassGrowFlag; -import com.plotsquared.core.plot.flag.implementations.HangingBreakFlag; -import com.plotsquared.core.plot.flag.implementations.HangingPlaceFlag; -import com.plotsquared.core.plot.flag.implementations.HostileAttackFlag; -import com.plotsquared.core.plot.flag.implementations.HostileCapFlag; -import com.plotsquared.core.plot.flag.implementations.HostileInteractFlag; -import com.plotsquared.core.plot.flag.implementations.IceFormFlag; -import com.plotsquared.core.plot.flag.implementations.IceMeltFlag; -import com.plotsquared.core.plot.flag.implementations.InstabreakFlag; -import com.plotsquared.core.plot.flag.implementations.InvincibleFlag; -import com.plotsquared.core.plot.flag.implementations.ItemDropFlag; -import com.plotsquared.core.plot.flag.implementations.KeepInventoryFlag; -import com.plotsquared.core.plot.flag.implementations.KelpGrowFlag; -import com.plotsquared.core.plot.flag.implementations.LiquidFlowFlag; -import com.plotsquared.core.plot.flag.implementations.MiscBreakFlag; -import com.plotsquared.core.plot.flag.implementations.MiscCapFlag; -import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag; -import com.plotsquared.core.plot.flag.implementations.MobCapFlag; -import com.plotsquared.core.plot.flag.implementations.MobPlaceFlag; -import com.plotsquared.core.plot.flag.implementations.MycelGrowFlag; -import com.plotsquared.core.plot.flag.implementations.PlaceFlag; -import com.plotsquared.core.plot.flag.implementations.PlayerInteractFlag; -import com.plotsquared.core.plot.flag.implementations.PreventCreativeCopyFlag; -import com.plotsquared.core.plot.flag.implementations.PveFlag; -import com.plotsquared.core.plot.flag.implementations.PvpFlag; -import com.plotsquared.core.plot.flag.implementations.RedstoneFlag; -import com.plotsquared.core.plot.flag.implementations.SnowFormFlag; -import com.plotsquared.core.plot.flag.implementations.SnowMeltFlag; -import com.plotsquared.core.plot.flag.implementations.SoilDryFlag; -import com.plotsquared.core.plot.flag.implementations.TamedAttackFlag; -import com.plotsquared.core.plot.flag.implementations.TamedInteractFlag; -import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; -import com.plotsquared.core.plot.flag.implementations.UseFlag; -import com.plotsquared.core.plot.flag.implementations.VehicleBreakFlag; -import com.plotsquared.core.plot.flag.implementations.VehicleCapFlag; -import com.plotsquared.core.plot.flag.implementations.VehicleUseFlag; -import com.plotsquared.core.plot.flag.implementations.VillagerInteractFlag; -import com.plotsquared.core.plot.flag.implementations.VineGrowFlag; -import com.plotsquared.core.plot.flag.types.BlockTypeWrapper; -import com.plotsquared.core.plot.message.PlotMessage; -import com.plotsquared.core.util.EntityUtil; -import com.plotsquared.core.util.MainUtil; -import com.plotsquared.core.util.MathMan; -import com.plotsquared.core.util.Permissions; -import com.plotsquared.core.util.PremiumVerification; -import com.plotsquared.core.util.RegExUtil; -import com.plotsquared.core.util.entity.EntityCategories; -import com.plotsquared.core.util.task.TaskManager; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldedit.world.block.BlockType; -import io.papermc.lib.PaperLib; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.FluidCollisionMode; -import org.bukkit.GameMode; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.data.BlockData; -import org.bukkit.command.PluginCommand; -import org.bukkit.entity.Ageable; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.Creature; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.FallingBlock; -import org.bukkit.entity.Fireball; -import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.ItemFrame; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.entity.Tameable; -import org.bukkit.entity.ThrownPotion; -import org.bukkit.entity.Vehicle; -import org.bukkit.event.Event; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockBurnEvent; -import org.bukkit.event.block.BlockDamageEvent; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.block.BlockExplodeEvent; -import org.bukkit.event.block.BlockFadeEvent; -import org.bukkit.event.block.BlockFormEvent; -import org.bukkit.event.block.BlockFromToEvent; -import org.bukkit.event.block.BlockGrowEvent; -import org.bukkit.event.block.BlockIgniteEvent; -import org.bukkit.event.block.BlockPhysicsEvent; -import org.bukkit.event.block.BlockPistonExtendEvent; -import org.bukkit.event.block.BlockPistonRetractEvent; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.block.BlockRedstoneEvent; -import org.bukkit.event.block.BlockSpreadEvent; -import org.bukkit.event.block.EntityBlockFormEvent; -import org.bukkit.event.entity.CreatureSpawnEvent; -import org.bukkit.event.entity.EntityChangeBlockEvent; -import org.bukkit.event.entity.EntityCombustByEntityEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.entity.EntityPickupItemEvent; -import org.bukkit.event.entity.ExplosionPrimeEvent; -import org.bukkit.event.entity.LingeringPotionSplashEvent; -import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.entity.PotionSplashEvent; -import org.bukkit.event.entity.ProjectileHitEvent; -import org.bukkit.event.entity.ProjectileLaunchEvent; -import org.bukkit.event.hanging.HangingBreakByEntityEvent; -import org.bukkit.event.hanging.HangingPlaceEvent; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.event.player.AsyncPlayerPreLoginEvent; -import org.bukkit.event.player.PlayerBucketEmptyEvent; -import org.bukkit.event.player.PlayerBucketFillEvent; -import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerEggThrowEvent; -import org.bukkit.event.player.PlayerEvent; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerRespawnEvent; -import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.event.vehicle.VehicleCreateEvent; -import org.bukkit.event.vehicle.VehicleDestroyEvent; -import org.bukkit.event.vehicle.VehicleEntityCollisionEvent; -import org.bukkit.event.vehicle.VehicleMoveEvent; -import org.bukkit.event.world.StructureGrowEvent; -import org.bukkit.help.HelpTopic; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.material.Directional; -import org.bukkit.metadata.FixedMetadataValue; -import org.bukkit.metadata.MetadataValue; -import org.bukkit.plugin.Plugin; -import org.bukkit.projectiles.BlockProjectileSource; -import org.bukkit.projectiles.ProjectileSource; -import org.bukkit.util.Vector; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import java.util.regex.Pattern; - -/** - * Player Events involving plots. - */ -@SuppressWarnings("unused") -public class PlayerEvents extends PlotListener implements Listener { - - public static final com.sk89q.worldedit.world.entity.EntityType FAKE_ENTITY_TYPE = - new com.sk89q.worldedit.world.entity.EntityType("plotsquared:fake"); - - private float lastRadius; - // To prevent recursion - private boolean tmpTeleport = true; - private Field fieldPlayer; - private PlayerMoveEvent moveTmp; - private String internalVersion; - - { - try { - fieldPlayer = PlayerEvent.class.getDeclaredField("player"); - fieldPlayer.setAccessible(true); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - } - - public static void sendBlockChange(final org.bukkit.Location bloc, final BlockData data) { - TaskManager.runTaskLater(() -> { - String world = bloc.getWorld().getName(); - int x = bloc.getBlockX(); - int z = bloc.getBlockZ(); - int distance = Bukkit.getViewDistance() * 16; - - for (final PlotPlayer player : PlotSquared.imp().getPlayerManager().getPlayers()) { - Location location = player.getLocation(); - if (location.getWorld().equals(world)) { - if (16 * Math.abs(location.getX() - x) / 16 > distance - || 16 * Math.abs(location.getZ() - z) / 16 > distance) { - continue; - } - ((BukkitPlayer) player).player.sendBlockChange(bloc, data); - } - } - }, 3); - } - - public static boolean checkEntity(Entity entity, Plot plot) { - if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot.getArea() - .getFlagContainer().getFlagMap().isEmpty()) { - return false; - } - - final com.sk89q.worldedit.world.entity.EntityType entityType = - BukkitAdapter.adapt(entity.getType()); - - if (EntityCategories.PLAYER.contains(entityType)) { - return false; - } - - if (EntityCategories.PROJECTILE.contains(entityType) || EntityCategories.OTHER - .contains(entityType) || EntityCategories.HANGING.contains(entityType)) { - return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, - MiscCapFlag.MISC_CAP_UNLIMITED); - } - - // Has to go go before vehicle as horses are both - // animals and vehicles - if (EntityCategories.ANIMAL.contains(entityType) || EntityCategories.VILLAGER - .contains(entityType) || EntityCategories.TAMEABLE.contains(entityType)) { - return EntityUtil - .checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, MobCapFlag.MOB_CAP_UNLIMITED, - AnimalCapFlag.ANIMAL_CAP_UNLIMITED); - } - - if (EntityCategories.HOSTILE.contains(entityType)) { - return EntityUtil - .checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, MobCapFlag.MOB_CAP_UNLIMITED, - HostileCapFlag.HOSTILE_CAP_UNLIMITED); - } - - if (EntityCategories.VEHICLE.contains(entityType)) { - return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, - VehicleCapFlag.VEHICLE_CAP_UNLIMITED); - } - - return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED); - } - - @EventHandler public void onVehicleEntityCollision(VehicleEntityCollisionEvent e) { - if (e.getVehicle().getType() == EntityType.BOAT) { - Location location = BukkitUtil.getLocation(e.getEntity()); - if (location.isPlotArea()) { - if (e.getEntity() instanceof Player) { - PlotPlayer player = BukkitUtil.getPlayer((Player) e.getEntity()); - Plot plot = player.getCurrentPlot(); - if (plot != null) { - if (!plot.isAdded(player.getUUID())) { - //Here the event is only canceled if the player is not the owner - //of the property on which he is located. - e.setCancelled(true); - } - } else { - e.setCancelled(true); - } - } else { - //Here the event is cancelled too, otherwise you can move the - //boat with EchoPets or other mobs running around on the plot. - e.setCancelled(true); - } - } - } - } - - @EventHandler public void onRedstoneEvent(BlockRedstoneEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(RedstoneFlag.class)) { - event.setNewCurrent(0); - } - return; - } - if (!plot.getFlag(RedstoneFlag.class)) { - event.setNewCurrent(0); - plot.debug("Redstone event was cancelled because redstone = false"); - return; - } - if (Settings.Redstone.DISABLE_OFFLINE) { - boolean disable = false; - if (!plot.getOwner().equals(DBFunc.SERVER)) { - if (plot.isMerged()) { - disable = true; - for (UUID owner : plot.getOwners()) { - if (PlotSquared.imp().getPlayerManager().getPlayerIfExists(owner) != null) { - disable = false; - break; - } - } - } else { - disable = - PlotSquared.imp().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs()) - == null; - } - } - if (disable) { - for (UUID trusted : plot.getTrusted()) { - if (PlotSquared.imp().getPlayerManager().getPlayerIfExists(trusted) != null) { - disable = false; - break; - } - } - if (disable) { - event.setNewCurrent(0); - plot.debug( - "Redstone event was cancelled because no trusted player was in the plot"); - return; - } - } - } - if (Settings.Redstone.DISABLE_UNOCCUPIED) { - for (final PlotPlayer player : PlotSquared.imp().getPlayerManager().getPlayers()) { - if (plot.equals(player.getCurrentPlot())) { - return; - } - } - event.setNewCurrent(0); - } - } - - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) - public void onPhysicsEvent(BlockPhysicsEvent event) { - switch (event.getChangedType()) { - case COMPARATOR: { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - if (location.isPlotArea()) { - return; - } - Plot plot = location.getOwnedPlotAbs(); - if (plot == null) { - return; - } - if (!plot.getFlag(RedstoneFlag.class)) { - event.setCancelled(true); - plot.debug("Prevented comparator update because redstone = false"); - } - return; - } - case ANVIL: - case DRAGON_EGG: - case GRAVEL: - case SAND: - case TURTLE_EGG: - case TURTLE_HELMET: - case TURTLE_SPAWN_EGG: { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlotAbs(location); - if (plot == null) { - return; - } - if (plot.getFlag(DisablePhysicsFlag.class)) { - event.setCancelled(true); - plot.debug("Prevented block physics because disable-physics = true"); - } - return; - } - default: - if (Settings.Redstone.DETECT_INVALID_EDGE_PISTONS) { - Block block = event.getBlock(); - switch (block.getType()) { - case PISTON: - case STICKY_PISTON: - org.bukkit.block.data.Directional piston = - (org.bukkit.block.data.Directional) block.getBlockData(); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlotAbs(location); - if (plot == null) { - return; - } - switch (piston.getFacing()) { - case EAST: - location.setX(location.getX() + 1); - break; - case SOUTH: - location.setX(location.getX() - 1); - break; - case WEST: - location.setZ(location.getZ() + 1); - break; - case NORTH: - location.setZ(location.getZ() - 1); - break; - } - Plot newPlot = area.getOwnedPlotAbs(location); - if (!plot.equals(newPlot)) { - event.setCancelled(true); - plot.debug( - "Prevented piston update because of invalid edge piston detection"); - return; - } - } - } - break; - } - } - - @EventHandler public void onProjectileLaunch(ProjectileLaunchEvent event) { - Projectile entity = event.getEntity(); - if (!(entity instanceof ThrownPotion)) { - return; - } - ProjectileSource shooter = entity.getShooter(); - if (!(shooter instanceof Player)) { - return; - } - Location location = BukkitUtil.getLocation(entity); - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - PlotPlayer pp = BukkitUtil.getPlayer((Player) shooter); - Plot plot = location.getOwnedPlot(); - if (plot != null && !plot.isAdded(pp.getUUID())) { - entity.remove(); - event.setCancelled(true); - } - } - - @EventHandler public boolean onProjectileHit(ProjectileHitEvent event) { - Projectile entity = event.getEntity(); - Location location = BukkitUtil.getLocation(entity); - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return true; - } - PlotArea area = location.getPlotArea(); - if (area == null) { - return true; - } - Plot plot = area.getPlot(location); - ProjectileSource shooter = entity.getShooter(); - if (shooter instanceof Player) { - PlotPlayer pp = BukkitUtil.getPlayer((Player) shooter); - if (plot == null) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_PROJECTILE_UNOWNED)) { - entity.remove(); - return false; - } - return true; - } - if (plot.isAdded(pp.getUUID()) || Permissions - .hasPermission(pp, Captions.PERMISSION_PROJECTILE_OTHER)) { - return true; - } - entity.remove(); - return false; - } - if (!(shooter instanceof Entity) && shooter != null) { - if (plot == null) { - entity.remove(); - return false; - } - Location sLoc = - BukkitUtil.getLocation(((BlockProjectileSource) shooter).getBlock().getLocation()); - if (!area.contains(sLoc.getX(), sLoc.getZ())) { - entity.remove(); - return false; - } - Plot sPlot = area.getOwnedPlotAbs(sLoc); - if (sPlot == null || !PlotHandler.sameOwners(plot, sPlot)) { - entity.remove(); - return false; - } - } - return true; - } - - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) - public void playerCommand(PlayerCommandPreprocessEvent event) { - String msg = event.getMessage().toLowerCase().replaceAll("/", "").trim(); - if (msg.isEmpty()) { - return; - } - Player player = event.getPlayer(); - PlotPlayer plotPlayer = BukkitUtil.getPlayer(player); - Location location = plotPlayer.getLocation(); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - String[] parts = msg.split(" "); - Plot plot = plotPlayer.getCurrentPlot(); - // Check WorldEdit - switch (parts[0].toLowerCase()) { - case "up": - case "/up": - case "worldedit:up": - case "worldedit:/up": - if (plot == null || (!plot.isAdded(plotPlayer.getUUID()) && !Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER, true))) { - event.setCancelled(true); - return; - } - } - if (plot == null && !area.isRoadFlags()) { - return; - } - - List blockedCommands = plot != null ? - plot.getFlag(BlockedCmdsFlag.class) : - area.getFlag(BlockedCmdsFlag.class); - if (!blockedCommands.isEmpty() && !Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_INTERACT_BLOCKED_CMDS)) { - String part = parts[0]; - if (parts[0].contains(":")) { - part = parts[0].split(":")[1]; - msg = msg.replace(parts[0].split(":")[0] + ':', ""); - } - String s1 = part; - List aliases = new ArrayList<>(); - for (HelpTopic cmdLabel : Bukkit.getServer().getHelpMap().getHelpTopics()) { - if (part.equals(cmdLabel.getName())) { - break; - } - String label = cmdLabel.getName().replaceFirst("/", ""); - if (aliases.contains(label)) { - continue; - } - PluginCommand p; - if ((p = Bukkit.getPluginCommand(label)) != null) { - for (String a : p.getAliases()) { - if (aliases.contains(a)) { - continue; - } - aliases.add(a); - a = a.replaceFirst("/", ""); - if (!a.equals(label) && a.equals(part)) { - part = label; - break; - } - } - } - } - if (!s1.equals(part)) { - msg = msg.replace(s1, part); - } - for (String s : blockedCommands) { - Pattern pattern; - if (!RegExUtil.compiledPatterns.containsKey(s)) { - RegExUtil.compiledPatterns.put(s, pattern = Pattern.compile(s)); - } else { - pattern = RegExUtil.compiledPatterns.get(s); - } - if (pattern.matcher(msg).matches()) { - String perm; - if (plot != null && plot.isAdded(plotPlayer.getUUID())) { - perm = "plots.admin.command.blocked-cmds.shared"; - } else { - perm = "plots.admin.command.blocked-cmds.road"; - } - if (!Permissions.hasPermission(plotPlayer, perm)) { - MainUtil.sendMessage(plotPlayer, Captions.COMMAND_BLOCKED); - event.setCancelled(true); - } - return; - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPreLoin(final AsyncPlayerPreLoginEvent event) { - final UUID uuid; - if (Settings.UUID.OFFLINE) { - if (Settings.UUID.FORCE_LOWERCASE) { - uuid = UUID.nameUUIDFromBytes( - ("OfflinePlayer:" + event.getName().toLowerCase()).getBytes(Charsets.UTF_8)); - } else { - uuid = UUID.nameUUIDFromBytes( - ("OfflinePlayer:" + event.getName()).getBytes(Charsets.UTF_8)); - } - } else { - uuid = event.getUniqueId(); - } - PlotSquared.get().getImpromptuUUIDPipeline().storeImmediately(event.getName(), uuid); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onConnect(PlayerJoinEvent event) { - final Player player = event.getPlayer(); - BukkitUtil.removePlayer(player.getUniqueId()); - final PlotPlayer pp = BukkitUtil.getPlayer(player); - - Location location = pp.getLocation(); - PlotArea area = location.getPlotArea(); - if (area != null) { - Plot plot = area.getPlot(location); - if (plot != null) { - plotEntry(pp, plot); - } - } - // Delayed - - // Async - TaskManager.runTaskLaterAsync(() -> { - if (!player.hasPlayedBefore() && player.isOnline()) { - player.saveData(); - } - PlotSquared.get().getEventDispatcher().doJoinTask(pp); - }, 20); - - if (pp.hasPermission(Captions.PERMISSION_ADMIN_UPDATE_NOTIFICATION.getTranslated()) - && Settings.Enabled_Components.UPDATE_NOTIFICATIONS && PremiumVerification.isPremium() - && UpdateUtility.hasUpdate) { - new PlotMessage("-----------------------------------").send(pp); - new PlotMessage(Captions.PREFIX + "There appears to be a PlotSquared update available!") - .color("$1").send(pp); - new PlotMessage( - Captions.PREFIX + "&6You are running version " + UpdateUtility.internalVersion - .versionString() + ", &6latest version is " + UpdateUtility.spigotVersion) - .color("$1").send(pp); - new PlotMessage(Captions.PREFIX + "Download at:").color("$1").send(pp); - player.sendMessage(" https://www.spigotmc.org/resources/77506/updates"); - new PlotMessage("-----------------------------------").send(pp); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void playerRespawn(PlayerRespawnEvent event) { - Player player = event.getPlayer(); - PlotPlayer pp = BukkitUtil.getPlayer(player); - PlotSquared.get().getEventDispatcher().doRespawnTask(pp); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onTeleport(PlayerTeleportEvent event) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); - org.bukkit.Location to = event.getTo(); - //noinspection ConstantConditions - if (to != null) { - Location location = BukkitUtil.getLocation(to); - PlotArea area = location.getPlotArea(); - if (area == null) { - if (lastPlot != null) { - plotExit(pp, lastPlot); - pp.deleteMeta(PlotPlayer.META_LAST_PLOT); - } - pp.deleteMeta(PlotPlayer.META_LOCATION); - return; - } - Plot plot = area.getPlot(location); - if (plot != null) { - final boolean result = DenyTeleportFlag.allowsTeleport(pp, plot); - // there is one possibility to still allow teleportation: - // to is identical to the plot's home location, and untrusted-visit is true - // i.e. untrusted-visit can override deny-teleport - // this is acceptable, because otherwise it wouldn't make sense to have both flags set - if (!result && !(plot.getFlag(UntrustedVisitFlag.class) && plot.getHomeSynchronous() - .equals(BukkitUtil.getLocationFull(to)))) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_ENTRY_DENIED); - event.setCancelled(true); - } - } - } - playerMove(event); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void vehicleMove(VehicleMoveEvent event) throws IllegalAccessException { - final org.bukkit.Location from = event.getFrom(); - final org.bukkit.Location to = event.getTo(); - - int toX, toZ; - if ((toX = MathMan.roundInt(to.getX())) != MathMan.roundInt(from.getX()) - | (toZ = MathMan.roundInt(to.getZ())) != MathMan.roundInt(from.getZ())) { - Vehicle vehicle = event.getVehicle(); - - // Check allowed - if (!vehicle.getPassengers().isEmpty()) { - Entity passenger = vehicle.getPassengers().get(0); - - if (passenger instanceof Player) { - final Player player = (Player) passenger; - // reset - if (moveTmp == null) { - moveTmp = new PlayerMoveEvent(null, from, to); - } - moveTmp.setFrom(from); - moveTmp.setTo(to); - moveTmp.setCancelled(false); - fieldPlayer.set(moveTmp, player); - - List passengers = vehicle.getPassengers(); - - this.playerMove(moveTmp); - org.bukkit.Location dest; - if (moveTmp.isCancelled()) { - dest = from; - } else if (MathMan.roundInt(moveTmp.getTo().getX()) != toX - || MathMan.roundInt(moveTmp.getTo().getZ()) != toZ) { - dest = to; - } else { - dest = null; - } - if (dest != null) { - vehicle.eject(); - vehicle.setVelocity(new Vector(0d, 0d, 0d)); - PaperLib.teleportAsync(vehicle, dest); - passengers.forEach(vehicle::addPassenger); - return; - } - } - if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { - final com.sk89q.worldedit.world.entity.EntityType entityType = - BukkitAdapter.adapt(vehicle.getType()); - // Horses etc are vehicles, but they're also animals - // so this filters out all living entities - if (EntityCategories.VEHICLE.contains(entityType) && !EntityCategories.ANIMAL - .contains(entityType)) { - List meta = vehicle.getMetadata("plot"); - Plot toPlot = BukkitUtil.getLocation(to).getPlot(); - if (!meta.isEmpty()) { - Plot origin = (Plot) meta.get(0).value(); - if (origin != null && !origin.getBasePlot(false).equals(toPlot)) { - vehicle.remove(); - } - } else if (toPlot != null) { - vehicle.setMetadata("plot", - new FixedMetadataValue((Plugin) PlotSquared.get().IMP, toPlot)); - } - } - } - } - - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void playerMove(PlayerMoveEvent event) { - org.bukkit.Location from = event.getFrom(); - org.bukkit.Location to = event.getTo(); - int x2; - if (MathMan.roundInt(from.getX()) != (x2 = MathMan.roundInt(to.getX()))) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - // Cancel teleport - if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { - MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); - } - // Set last location - Location location = BukkitUtil.getLocation(to); - pp.setMeta(PlotPlayer.META_LOCATION, location); - PlotArea area = location.getPlotArea(); - if (area == null) { - pp.deleteMeta(PlotPlayer.META_LAST_PLOT); - return; - } - Plot now = area.getPlot(location); - Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); - if (now == null) { - if (lastPlot != null && !plotExit(pp, lastPlot) && this.tmpTeleport && !pp - .getMeta("kick", false)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_EXIT_DENIED); - this.tmpTeleport = false; - if (lastPlot.equals(BukkitUtil.getLocation(from).getPlot())) { - player.teleport(from); - } else { - player.teleport(player.getWorld().getSpawnLocation()); - } - this.tmpTeleport = true; - event.setCancelled(true); - return; - } - } else if (now.equals(lastPlot)) { - ForceFieldListener.handleForcefield(player, pp, now); - } else if (!plotEntry(pp, now) && this.tmpTeleport) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_ENTRY_DENIED); - this.tmpTeleport = false; - to.setX(from.getBlockX()); - to.setY(from.getBlockY()); - to.setZ(from.getBlockZ()); - player.teleport(event.getTo()); - this.tmpTeleport = true; - return; - } - int border = area.getBorder(); - if (x2 > border && this.tmpTeleport) { - to.setX(border - 1); - this.tmpTeleport = false; - player.teleport(event.getTo()); - this.tmpTeleport = true; - MainUtil.sendMessage(pp, Captions.BORDER); - } - if (x2 < -border && this.tmpTeleport) { - to.setX(-border + 1); - this.tmpTeleport = false; - player.teleport(event.getTo()); - this.tmpTeleport = true; - MainUtil.sendMessage(pp, Captions.BORDER); - } - } - int z2; - if (MathMan.roundInt(from.getZ()) != (z2 = MathMan.roundInt(to.getZ()))) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - // Cancel teleport - if (TaskManager.TELEPORT_QUEUE.remove(pp.getName())) { - MainUtil.sendMessage(pp, Captions.TELEPORT_FAILED); - } - // Set last location - Location location = BukkitUtil.getLocation(to); - pp.setMeta(PlotPlayer.META_LOCATION, location); - PlotArea area = location.getPlotArea(); - if (area == null) { - pp.deleteMeta(PlotPlayer.META_LAST_PLOT); - return; - } - Plot now = area.getPlot(location); - Plot lastPlot = pp.getMeta(PlotPlayer.META_LAST_PLOT); - if (now == null) { - if (lastPlot != null && !plotExit(pp, lastPlot) && this.tmpTeleport && !pp - .getMeta("kick", false)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_EXIT_DENIED); - this.tmpTeleport = false; - if (lastPlot.equals(BukkitUtil.getLocation(from).getPlot())) { - player.teleport(from); - } else { - player.teleport(player.getWorld().getSpawnLocation()); - } - this.tmpTeleport = true; - event.setCancelled(true); - return; - } - } else if (now.equals(lastPlot)) { - ForceFieldListener.handleForcefield(player, pp, now); - } else if (!plotEntry(pp, now) && this.tmpTeleport) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_ENTRY_DENIED); - this.tmpTeleport = false; - player.teleport(from); - to.setX(from.getBlockX()); - to.setY(from.getBlockY()); - to.setZ(from.getBlockZ()); - player.teleport(event.getTo()); - this.tmpTeleport = true; - return; - } - int border = area.getBorder(); - if (z2 > border && this.tmpTeleport) { - to.setZ(border - 1); - this.tmpTeleport = false; - player.teleport(event.getTo()); - this.tmpTeleport = true; - MainUtil.sendMessage(pp, Captions.BORDER); - } else if (z2 < -border && this.tmpTeleport) { - to.setZ(-border + 1); - this.tmpTeleport = false; - player.teleport(event.getTo()); - this.tmpTeleport = true; - MainUtil.sendMessage(pp, Captions.BORDER); - } - } - } - - @EventHandler(priority = EventPriority.LOW) public void onChat(AsyncPlayerChatEvent event) { - if (event.isCancelled()) { - return; - } - - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(event.getPlayer()); - Location location = plotPlayer.getLocation(); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getPlot(location); - if (plot == null) { - return; - } - if (!((plot.getFlag(ChatFlag.class) && area.isPlotChat() && plotPlayer.getAttribute("chat")) - || area.isForcingPlotChat())) { - return; - } - if (plot.isDenied(plotPlayer.getUUID()) && !Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_CHAT_BYPASS)) { - return; - } - event.setCancelled(true); - String message = event.getMessage(); - String format = Captions.PLOT_CHAT_FORMAT.getTranslated(); - String sender = event.getPlayer().getDisplayName(); - PlotId id = plot.getId(); - Set recipients = event.getRecipients(); - recipients.clear(); - Set spies = new HashSet<>(); - for (final PlotPlayer pp : PlotSquared.imp().getPlayerManager().getPlayers()) { - if (pp.getAttribute("chatspy")) { - spies.add(((BukkitPlayer) pp).player); - } else { - Plot current = pp.getCurrentPlot(); - if (current != null && current.getBasePlot(false).equals(plot)) { - recipients.add(((BukkitPlayer) pp).player); - } - } - } - String partial = ChatColor.translateAlternateColorCodes('&', - format.replace("%plot_id%", id.x + ";" + id.y).replace("%sender%", sender)); - if (plotPlayer.hasPermission("plots.chat.color")) { - message = Captions.color(message); - } - String full = partial.replace("%msg%", message); - for (Player receiver : recipients) { - receiver.sendMessage(full); - } - if (!spies.isEmpty()) { - String spyMessage = Captions.PLOT_CHAT_SPY_FORMAT.getTranslated() - .replace("%plot_id%", id.x + ";" + id.y).replace("%sender%", sender) - .replace("%msg%", message); - for (Player player : spies) { - player.sendMessage(spyMessage); - } - } - PlotSquared.debug(full); - } - - @EventHandler(priority = EventPriority.LOWEST) public void blockDestroy(BlockBreakEvent event) { - Player player = event.getPlayer(); - Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getPlot(location); - if (plot != null) { - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (event.getBlock().getY() == 0) { - if (!Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_GROUNDLEVEL)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_GROUNDLEVEL); - event.setCancelled(true); - return; - } - } else if ((location.getY() > area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { - event.setCancelled(true); - MainUtil.sendMessage(plotPlayer, Captions.HEIGHT_LIMIT.getTranslated() - .replace("{limit}", String.valueOf(area.getMaxBuildHeight()))); - } - if (!plot.hasOwner()) { - if (!Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED, true)) { - event.setCancelled(true); - } - return; - } - if (!plot.isAdded(plotPlayer.getUUID())) { - List destroy = plot.getFlag(BreakFlag.class); - Block block = event.getBlock(); - final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); - for (final BlockTypeWrapper blockTypeWrapper : destroy) { - if (blockTypeWrapper.accepts(blockType)) { - return; - } - } - if (Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { - return; - } - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_OTHER); - event.setCancelled(true); - } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - return; - } - } - return; - } - BukkitPlayer pp = BukkitUtil.getPlayer(player); - if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { - return; - } - if (PlotSquared.get().worldedit != null && pp.getAttribute("worldedit")) { - if (player.getInventory().getItemInMainHand().getType() == Material - .getMaterial(PlotSquared.get().worldedit.getConfiguration().wandItem)) { - return; - } - } - MainUtil - .sendMessage(pp, Captions.NO_PERMISSION_EVENT, Captions.PERMISSION_ADMIN_DESTROY_ROAD); - event.setCancelled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBigBoom(EntityExplodeEvent event) { - Location location = BukkitUtil.getLocation(event.getLocation()); - PlotArea area = location.getPlotArea(); - boolean plotArea = location.isPlotArea(); - if (!plotArea) { - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot != null) { - if (plot.getFlag(ExplosionFlag.class)) { - List meta = event.getEntity().getMetadata("plot"); - Plot origin; - if (meta.isEmpty()) { - origin = plot; - } else { - origin = (Plot) meta.get(0).value(); - } - if (this.lastRadius != 0) { - List nearby = event.getEntity() - .getNearbyEntities(this.lastRadius, this.lastRadius, this.lastRadius); - for (Entity near : nearby) { - if (near instanceof TNTPrimed || near.getType() - .equals(EntityType.MINECART_TNT)) { - if (!near.hasMetadata("plot")) { - near.setMetadata("plot", - new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); - } - } - } - this.lastRadius = 0; - } - Iterator iterator = event.blockList().iterator(); - while (iterator.hasNext()) { - Block block = iterator.next(); - location = BukkitUtil.getLocation(block.getLocation()); - if (!area.contains(location.getX(), location.getZ()) || !origin - .equals(area.getOwnedPlot(location))) { - iterator.remove(); - } - } - return; - } else { - plot.debug("Explosion was cancelled because explosion = false"); - } - } - event.setCancelled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onWorldChanged(PlayerChangedWorldEvent event) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - // Delete last location - Plot plot = (Plot) pp.deleteMeta(PlotPlayer.META_LAST_PLOT); - pp.deleteMeta(PlotPlayer.META_LOCATION); - if (plot != null) { - plotExit(pp, plot); - } - if (PlotSquared.get().worldedit != null) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_WORLDEDIT_BYPASS)) { - if (pp.getAttribute("worldedit")) { - pp.removeAttribute("worldedit"); - } - } - } - if (Settings.Enabled_Components.PERMISSION_CACHE) { - pp.deleteMeta("perm"); - } - Location location = pp.getLocation(); - PlotArea area = location.getPlotArea(); - if (location.isPlotArea()) { - plot = location.getPlot(); - if (plot != null) { - plotEntry(pp, plot); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPeskyMobsChangeTheWorldLikeWTFEvent(EntityChangeBlockEvent event) { - Entity e = event.getEntity(); - if (!(e instanceof FallingBlock)) { - Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); - PlotArea area = location.getPlotArea(); - if (area != null) { - Plot plot = area.getOwnedPlot(location); - if (plot != null && plot.getFlag(MobPlaceFlag.class)) { - plot.debug(e.getType() + " could not change block because mob-place = false"); - return; - } - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityBlockForm(EntityBlockFormEvent event) { - String world = event.getBlock().getWorld().getName(); - if (!PlotSquared.get().hasPlotArea(world)) { - return; - } - Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - event.setCancelled(true); - return; - } - Entity entity = event.getEntity(); - if (entity instanceof Player) { - Player player = (Player) entity; - if (!plot.hasOwner()) { - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (plot.getFlag(IceFormFlag.class)) { - plot.debug("Ice could not be formed because ice-form = false"); - return; - } - event.setCancelled(true); - return; - } - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (!plot.isAdded(plotPlayer.getUUID())) { - if (plot.getFlag(IceFormFlag.class)) { - plot.debug("Ice could not be formed because ice-form = false"); - return; - } - event.setCancelled(true); - return; - } - return; - } - if (!plot.getFlag(IceFormFlag.class)) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockSpread(BlockSpreadEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - if (location.isPlotRoad()) { - event.setCancelled(true); - return; - } - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - return; - } - switch (event.getSource().getType()) { - case GRASS_BLOCK: - if (!plot.getFlag(GrassGrowFlag.class)) { - plot.debug("Grass could not grow because grass-grow = false"); - event.setCancelled(true); - } - break; - case MYCELIUM: - if (!plot.getFlag(MycelGrowFlag.class)) { - plot.debug("Mycelium could not grow because mycel-grow = false"); - event.setCancelled(true); - } - break; - case VINE: - if (!plot.getFlag(VineGrowFlag.class)) { - plot.debug("Vine could not grow because vine-grow = false"); - event.setCancelled(true); - } - break; - case KELP: - if (!plot.getFlag(KelpGrowFlag.class)) { - plot.debug("Kelp could not grow because kelp-grow = false"); - event.setCancelled(true); - } - break; - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockForm(BlockFormEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - if (location.isPlotRoad()) { - event.setCancelled(true); - return; - } - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - return; - } - switch (event.getNewState().getType()) { - case SNOW: - case SNOW_BLOCK: - if (!plot.getFlag(SnowFormFlag.class)) { - plot.debug("Snow could not form because snow-form = false"); - event.setCancelled(true); - } - return; - case ICE: - case FROSTED_ICE: - case PACKED_ICE: - if (!plot.getFlag(IceFormFlag.class)) { - plot.debug("Ice could not form because ice-form = false"); - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockDamage(BlockDamageEvent event) { - Player player = event.getPlayer(); - Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - if (player.getGameMode() != GameMode.SURVIVAL) { - return; - } - Plot plot = area.getPlot(location); - if (plot != null) { - if (plot.getFlag(InstabreakFlag.class)) { - Block block = event.getBlock(); - BlockBreakEvent call = new BlockBreakEvent(block, player); - Bukkit.getServer().getPluginManager().callEvent(call); - if (!call.isCancelled()) { - event.getBlock().breakNaturally(); - } - } - if (location.getY() == 0) { - event.setCancelled(true); - return; - } - if (!plot.hasOwner()) { - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { - return; - } - event.setCancelled(true); - return; - } - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (!plot.isAdded(plotPlayer.getUUID())) { - List destroy = plot.getFlag(BreakFlag.class); - Block block = event.getBlock(); - if (destroy - .contains(BlockTypeWrapper.get(BukkitAdapter.asBlockType(block.getType()))) - || Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { - return; - } - plot.debug(player.getName() + " could not break " + block.getType() - + " because it was not in the break flag"); - event.setCancelled(true); - return; - } - return; - } - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { - return; - } - event.setCancelled(true); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onFade(BlockFadeEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - event.setCancelled(true); - return; - } - switch (block.getType()) { - case ICE: - if (!plot.getFlag(IceMeltFlag.class)) { - plot.debug("Ice could not melt because ice-melt = false"); - event.setCancelled(true); - } - break; - case SNOW: - if (!plot.getFlag(SnowMeltFlag.class)) { - plot.debug("Snow could not melt because snow-melt = false"); - event.setCancelled(true); - } - break; - case FARMLAND: - if (!plot.getFlag(SoilDryFlag.class)) { - plot.debug("Soil could not dry because soil-dry = false"); - event.setCancelled(true); - } - break; - case TUBE_CORAL_BLOCK: - case BRAIN_CORAL_BLOCK: - case BUBBLE_CORAL_BLOCK: - case FIRE_CORAL_BLOCK: - case HORN_CORAL_BLOCK: - case TUBE_CORAL: - case BRAIN_CORAL: - case BUBBLE_CORAL: - case FIRE_CORAL: - case HORN_CORAL: - case TUBE_CORAL_FAN: - case BRAIN_CORAL_FAN: - case BUBBLE_CORAL_FAN: - case FIRE_CORAL_FAN: - case HORN_CORAL_FAN: - case BRAIN_CORAL_WALL_FAN: - case BUBBLE_CORAL_WALL_FAN: - case FIRE_CORAL_WALL_FAN: - case HORN_CORAL_WALL_FAN: - case TUBE_CORAL_WALL_FAN: - if (!plot.getFlag(CoralDryFlag.class)) { - plot.debug("Coral could not dry because coral-dry = false"); - event.setCancelled(true); - } - break; - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onChange(BlockFromToEvent event) { - Block from = event.getBlock(); - - // Check liquid flow flag inside of origin plot too - final Location fLocation = BukkitUtil.getLocation(from.getLocation()); - final PlotArea fromArea = fLocation.getPlotArea(); - if (fromArea != null) { - final Plot plot = fromArea.getOwnedPlot(fLocation); - if (plot != null - && plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event - .getBlock().isLiquid()) { - plot.debug("Liquid could now flow because liquid-flow = disabled"); - event.setCancelled(true); - return; - } - } - - Block to = event.getToBlock(); - Location tLocation = BukkitUtil.getLocation(to.getLocation()); - PlotArea area = tLocation.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlot(tLocation); - if (plot != null) { - if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects - .equals(plot, area.getOwnedPlot(fLocation))) { - event.setCancelled(true); - return; - } - if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.ENABLED && event - .getBlock().isLiquid()) { - return; - } - if (plot.getFlag(DisablePhysicsFlag.class)) { - plot.debug(event.getBlock().getType() - + " could not update because disable-physics = true"); - event.setCancelled(true); - return; - } - if (plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event - .getBlock().isLiquid()) { - plot.debug("Liquid could not flow because liquid-flow = disabled"); - event.setCancelled(true); - } - } else if (!area.contains(fLocation.getX(), fLocation.getZ()) || !Objects - .equals(null, area.getOwnedPlot(fLocation))) { - event.setCancelled(true); - } else if (event.getBlock().isLiquid()) { - final org.bukkit.Location location = event.getBlock().getLocation(); - - /* - X = block location - A-H = potential plot locations - - Z - ^ - | A B C - o D X E - | F G H - v - <-----O-----> x - */ - if (BukkitUtil.getPlot(location.clone().add(-1, 0, 1) /* A */) != null - || BukkitUtil.getPlot(location.clone().add(1, 0, 0) /* B */) != null - || BukkitUtil.getPlot(location.clone().add(1, 0, 1) /* C */) != null - || BukkitUtil.getPlot(location.clone().add(-1, 0, 0) /* D */) != null - || BukkitUtil.getPlot(location.clone().add(1, 0, 0) /* E */) != null - || BukkitUtil.getPlot(location.clone().add(-1, 0, -1) /* F */) != null - || BukkitUtil.getPlot(location.clone().add(0, 0, -1) /* G */) != null - || BukkitUtil.getPlot(location.clone().add(1, 0, 1) /* H */) != null) { - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onGrow(BlockGrowEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - if (location.isUnownedPlotArea()) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPistonExtend(BlockPistonExtendEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - BlockFace face = event.getDirection(); - Vector relative = new Vector(face.getModX(), face.getModY(), face.getModZ()); - PlotArea area = location.getPlotArea(); - if (area == null) { - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - for (Block block1 : event.getBlocks()) { - Location bloc = BukkitUtil.getLocation(block1.getLocation()); - if (bloc.isPlotArea() || bloc - .add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) - .isPlotArea()) { - event.setCancelled(true); - return; - } - } - if (location.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) - .isPlotArea()) { - // Prevent pistons from extending if they are: bordering a plot - // area, facing inside plot area, and not pushing any blocks - event.setCancelled(true); - } - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - event.setCancelled(true); - return; - } - for (Block block1 : event.getBlocks()) { - Location bloc = BukkitUtil.getLocation(block1.getLocation()); - if (!area.contains(bloc.getX(), bloc.getZ()) || !area - .contains(bloc.getX() + relative.getBlockX(), bloc.getZ() + relative.getBlockZ())) { - event.setCancelled(true); - return; - } - if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot( - bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { - event.setCancelled(true); - return; - } - } - if (!plot.equals(area.getOwnedPlot( - location.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { - // This branch is only necessary to prevent pistons from extending - // if they are: on a plot edge, facing outside the plot, and not - // pushing any blocks - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockPistonRetract(BlockPistonRetractEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - BlockFace face = event.getDirection(); - Vector relative = new Vector(face.getModX(), face.getModY(), face.getModZ()); - PlotArea area = location.getPlotArea(); - if (area == null) { - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - for (Block block1 : event.getBlocks()) { - Location bloc = BukkitUtil.getLocation(block1.getLocation()); - if (bloc.isPlotArea() || bloc - .add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ()) - .isPlotArea()) { - event.setCancelled(true); - return; - } - } - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null) { - event.setCancelled(true); - return; - } - for (Block block1 : event.getBlocks()) { - Location bloc = BukkitUtil.getLocation(block1.getLocation()); - if (!area.contains(bloc.getX(), bloc.getZ()) || !area - .contains(bloc.getX() + relative.getBlockX(), bloc.getZ() + relative.getBlockZ())) { - event.setCancelled(true); - return; - } - if (!plot.equals(area.getOwnedPlot(bloc)) || !plot.equals(area.getOwnedPlot( - bloc.add(relative.getBlockX(), relative.getBlockY(), relative.getBlockZ())))) { - event.setCancelled(true); - return; - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockDispense(BlockDispenseEvent event) { - Material type = event.getItem().getType(); - switch (type) { - case SHULKER_BOX: - case WHITE_SHULKER_BOX: - case ORANGE_SHULKER_BOX: - case MAGENTA_SHULKER_BOX: - case LIGHT_BLUE_SHULKER_BOX: - case YELLOW_SHULKER_BOX: - case LIME_SHULKER_BOX: - case PINK_SHULKER_BOX: - case GRAY_SHULKER_BOX: - case LIGHT_GRAY_SHULKER_BOX: - case CYAN_SHULKER_BOX: - case PURPLE_SHULKER_BOX: - case BLUE_SHULKER_BOX: - case BROWN_SHULKER_BOX: - case GREEN_SHULKER_BOX: - case RED_SHULKER_BOX: - case BLACK_SHULKER_BOX: - case CARVED_PUMPKIN: - case WITHER_SKELETON_SKULL: - case FLINT_AND_STEEL: - case BONE_MEAL: - case SHEARS: - case GLASS_BOTTLE: - case GLOWSTONE: - case COD_BUCKET: - case PUFFERFISH_BUCKET: - case SALMON_BUCKET: - case TROPICAL_FISH_BUCKET: - case BUCKET: - case WATER_BUCKET: - case LAVA_BUCKET: { - if (event.getBlock().getType() == Material.DROPPER) { - return; - } - BlockFace targetFace = - ((Directional) event.getBlock().getState().getData()).getFacing(); - Location location = - BukkitUtil.getLocation(event.getBlock().getRelative(targetFace).getLocation()); - if (location.isPlotRoad()) { - event.setCancelled(true); - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onStructureGrow(StructureGrowEvent event) { - if (!PlotSquared.get().hasPlotArea(event.getWorld().getName())) { - return; - } - List blocks = event.getBlocks(); - if (blocks.isEmpty()) { - return; - } - Location location = BukkitUtil.getLocation(blocks.get(0).getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.getLocation(blocks.get(i).getLocation()); - if (location.isPlotArea()) { - blocks.remove(i); - } - } - return; - } else { - Plot origin = area.getOwnedPlot(location); - if (origin == null) { - event.setCancelled(true); - return; - } - for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.getLocation(blocks.get(i).getLocation()); - if (!area.contains(location.getX(), location.getZ())) { - blocks.remove(i); - continue; - } - Plot plot = area.getOwnedPlot(location); - if (!Objects.equals(plot, origin)) { - event.getBlocks().remove(i); - } - } - } - Plot origin = area.getPlot(location); - if (origin == null) { - event.setCancelled(true); - return; - } - for (int i = blocks.size() - 1; i >= 0; i--) { - location = BukkitUtil.getLocation(blocks.get(i).getLocation()); - Plot plot = area.getOwnedPlot(location); - /* - * plot → the base plot of the merged area - * origin → the plot where the event gets called - */ - - // Are plot and origin different AND are both plots merged - if (!Objects.equals(plot, origin) && (!plot.isMerged() && !origin.isMerged())) { - event.getBlocks().remove(i); - } - } - } - - @SuppressWarnings("deprecation") - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onInventoryClick(InventoryClickEvent event) { - /*if (!event.isLeftClick() || (event.getAction() != InventoryAction.PLACE_ALL) || event - .isShiftClick()) { - return; - }*/ - HumanEntity entity = event.getWhoClicked(); - if (!(entity instanceof Player) || !PlotSquared.get() - .hasPlotArea(entity.getWorld().getName())) { - return; - } - - HumanEntity clicker = event.getWhoClicked(); - if (!(clicker instanceof Player)) { - return; - } - Player player = (Player) clicker; - BukkitPlayer pp = BukkitUtil.getPlayer(player); - final PlotInventory inventory = PlotInventory.getOpenPlotInventory(pp); - if (inventory != null && event.getRawSlot() == event.getSlot()) { - if (!inventory.onClick(event.getSlot())) { - event.setResult(Event.Result.DENY); - event.setCancelled(true); - inventory.close(); - } - } - PlayerInventory inv = player.getInventory(); - int slot = inv.getHeldItemSlot(); - if ((slot > 8) || !event.getEventName().equals("InventoryCreativeEvent")) { - return; - } - ItemStack current = inv.getItemInHand(); - ItemStack newItem = event.getCursor(); - ItemMeta newMeta = newItem.getItemMeta(); - ItemMeta oldMeta = newItem.getItemMeta(); - - if (event.getClick() == ClickType.CREATIVE) { - final Plot plot = pp.getCurrentPlot(); - if (plot != null) { - if (plot.getFlag(PreventCreativeCopyFlag.class) && !plot - .isAdded(player.getUniqueId()) && !Permissions - .hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_OTHER)) { - final ItemStack newStack = - new ItemStack(newItem.getType(), newItem.getAmount()); - event.setCursor(newStack); - plot.debug(player.getName() - + " could not creative-copy an item because prevent-creative-copy = true"); - } - } else { - PlotArea area = pp.getPlotAreaAbs(); - if (area != null && area.isRoadFlags() && area - .getRoadFlag(PreventCreativeCopyFlag.class)) { - final ItemStack newStack = - new ItemStack(newItem.getType(), newItem.getAmount()); - event.setCursor(newStack); - } - } - return; - } - - String newLore = ""; - if (newMeta != null) { - List lore = newMeta.getLore(); - if (lore != null) { - newLore = lore.toString(); - } - } - String oldLore = ""; - if (oldMeta != null) { - List lore = oldMeta.getLore(); - if (lore != null) { - oldLore = lore.toString(); - } - } - if (!"[(+NBT)]".equals(newLore) || (current.equals(newItem) && newLore.equals(oldLore))) { - switch (newItem.getType()) { - case LEGACY_BANNER: - case PLAYER_HEAD: - if (newMeta != null) { - break; - } - default: - return; - } - } - Block block = player.getTargetBlock(null, 7); - org.bukkit.block.BlockState state = block.getState(); - Material stateType = state.getType(); - Material itemType = newItem.getType(); - if (stateType != itemType) { - switch (stateType) { - case LEGACY_STANDING_BANNER: - case LEGACY_WALL_BANNER: - if (itemType == Material.LEGACY_BANNER) { - break; - } - case LEGACY_SKULL: - if (itemType == Material.LEGACY_SKULL_ITEM) { - break; - } - default: - return; - } - } - Location location = BukkitUtil.getLocation(state.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getPlotAbs(location); - boolean cancelled = false; - if (plot == null) { - if (!Permissions.hasPermission(pp, "plots.admin.interact.road")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.road"); - cancelled = true; - } - } else if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, "plots.admin.interact.unowned")) { - MainUtil - .sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.unowned"); - cancelled = true; - } - } else { - UUID uuid = pp.getUUID(); - if (!plot.isAdded(uuid)) { - if (!Permissions.hasPermission(pp, "plots.admin.interact.other")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.interact.other"); - cancelled = true; - } - } - } - if (cancelled) { - if ((current.getType() == newItem.getType()) && (current.getDurability() == newItem - .getDurability())) { - event.setCursor( - new ItemStack(newItem.getType(), newItem.getAmount(), newItem.getDurability())); - event.setCancelled(true); - return; - } - event.setCursor( - new ItemStack(newItem.getType(), newItem.getAmount(), newItem.getDurability())); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPotionSplash(LingeringPotionSplashEvent event) { - Projectile entity = event.getEntity(); - Location location = BukkitUtil.getLocation(entity); - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - if (!this.onProjectileHit(event)) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onInteract(PlayerInteractAtEntityEvent e) { - Entity entity = e.getRightClicked(); - if (!(entity instanceof ArmorStand) && !(entity instanceof ItemFrame)) { - return; - } - Location location = BukkitUtil.getLocation(e.getRightClicked().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - EntitySpawnListener.testNether(entity); - Plot plot = location.getPlotAbs(); - BukkitPlayer pp = BukkitUtil.getPlayer(e.getPlayer()); - if (plot == null) { - if (!area.isRoadFlags() && !area.getRoadFlag(MiscInteractFlag.class) && !Permissions - .hasPermission(pp, "plots.admin.interact.road")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, "plots.admin.interact.road"); - e.setCancelled(true); - } - } else { - if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - e.setCancelled(true); - return; - } - } - if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, "plots.admin.interact.unowned")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.interact.unowned"); - e.setCancelled(true); - } - } else { - UUID uuid = pp.getUUID(); - if (plot.isAdded(uuid)) { - return; - } - if (plot.getFlag(MiscInteractFlag.class)) { - return; - } - if (!Permissions.hasPermission(pp, "plots.admin.interact.other")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.interact.other"); - e.setCancelled(true); - plot.debug(pp.getName() + " could not interact with " + entity.getType() - + " bcause misc-interact = false"); - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBigBoom(BlockExplodeEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - String world = location.getWorld(); - if (!PlotSquared.get().hasPlotArea(world)) { - return; - } - PlotArea area = location.getPlotArea(); - if (area == null) { - Iterator iterator = event.blockList().iterator(); - while (iterator.hasNext()) { - location = BukkitUtil.getLocation(iterator.next().getLocation()); - if (location.isPlotArea()) { - iterator.remove(); - } - } - return; - } - Plot plot = area.getOwnedPlot(location); - if (plot == null || !plot.getFlag(ExplosionFlag.class)) { - event.setCancelled(true); - if (plot != null) { - plot.debug("Explosion was cancelled because explosion = false"); - } - } - event.blockList().removeIf( - blox -> !plot.equals(area.getOwnedPlot(BukkitUtil.getLocation(blox.getLocation())))); - } - - @EventHandler(priority = EventPriority.LOW) - public void onCancelledInteract(PlayerInteractEvent event) { - if (event.isCancelled() && event.getAction() == Action.RIGHT_CLICK_AIR) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - PlotArea area = pp.getPlotAreaAbs(); - if (area == null) { - return; - } - if (event.getAction() == Action.RIGHT_CLICK_AIR) { - Material item = event.getMaterial(); - if (item.toString().toLowerCase().endsWith("_egg")) { - event.setCancelled(true); - event.setUseItemInHand(Event.Result.DENY); - } - } - ItemStack hand = player.getInventory().getItemInMainHand(); - ItemStack offHand = player.getInventory().getItemInOffHand(); - Material type = hand.getType(); - Material offType = offHand.getType(); - if (type == Material.AIR) { - type = offType; - } - if (type.toString().toLowerCase().endsWith("_egg")) { - Block block = player.getTargetBlockExact(5, FluidCollisionMode.SOURCE_ONLY); - if (block != null && block.getType() != Material.AIR) { - Location location = BukkitUtil.getLocation(block.getLocation()); - if (!PlotSquared.get().getEventDispatcher() - .checkPlayerBlockEvent(pp, PlayerBlockEventType.SPAWN_MOB, location, null, - true)) { - event.setCancelled(true); - event.setUseItemInHand(Event.Result.DENY); - } - } - } - } - } - - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onInteract(PlayerInteractEvent event) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - PlotArea area = pp.getPlotAreaAbs(); - if (area == null) { - return; - } - PlayerBlockEventType eventType = null; - BlockType blocktype1; - Block block = event.getClickedBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - Action action = event.getAction(); - outer: - switch (action) { - case PHYSICAL: { - eventType = PlayerBlockEventType.TRIGGER_PHYSICAL; - blocktype1 = BukkitAdapter.asBlockType(block.getType()); - break; - } - //todo rearrange the right click code. it is all over the place. - case RIGHT_CLICK_BLOCK: { - Material blockType = block.getType(); - eventType = PlayerBlockEventType.INTERACT_BLOCK; - blocktype1 = BukkitAdapter.asBlockType(block.getType()); - - if (blockType.isInteractable()) { - if (!player.isSneaking()) { - break; - } - ItemStack hand = player.getInventory().getItemInMainHand(); - ItemStack offHand = player.getInventory().getItemInOffHand(); - - // sneaking players interact with blocks if both hands are empty - if (hand.getType() == Material.AIR && offHand.getType() == Material.AIR) { - break; - } - } - - Material type = event.getMaterial(); - - // in the following, lb needs to have the material of the item in hand i.e. type - switch (type) { - case REDSTONE: - case STRING: - case PUMPKIN_SEEDS: - case MELON_SEEDS: - case COCOA_BEANS: - case WHEAT_SEEDS: - case BEETROOT_SEEDS: - case SWEET_BERRIES: - return; - default: - //eventType = PlayerBlockEventType.PLACE_BLOCK; - if (type.isBlock()) { - return; - } - } - if (PaperLib.isPaper()) { - if (MaterialTags.SPAWN_EGGS.isTagged(type) || Material.EGG.equals(type)) { - eventType = PlayerBlockEventType.SPAWN_MOB; - break outer; - } - } else { - if (type.toString().toLowerCase().endsWith("egg")) { - eventType = PlayerBlockEventType.SPAWN_MOB; - break outer; - } - } - if (type.isEdible()) { - //Allow all players to eat while also allowing the block place event ot be fired - return; - } - switch (type) { - case ACACIA_BOAT: - case BIRCH_BOAT: - case CHEST_MINECART: - case COMMAND_BLOCK_MINECART: - case DARK_OAK_BOAT: - case FURNACE_MINECART: - case HOPPER_MINECART: - case JUNGLE_BOAT: - case MINECART: - case OAK_BOAT: - case SPRUCE_BOAT: - case TNT_MINECART: - eventType = PlayerBlockEventType.PLACE_VEHICLE; - break outer; - case FIREWORK_ROCKET: - case FIREWORK_STAR: - eventType = PlayerBlockEventType.SPAWN_MOB; - break outer; - case BOOK: - case KNOWLEDGE_BOOK: - case WRITABLE_BOOK: - case WRITTEN_BOOK: - eventType = PlayerBlockEventType.READ; - break outer; - case ARMOR_STAND: - location = BukkitUtil - .getLocation(block.getRelative(event.getBlockFace()).getLocation()); - eventType = PlayerBlockEventType.PLACE_MISC; - break outer; - } - break; - } - case LEFT_CLICK_BLOCK: { - location = BukkitUtil.getLocation(block.getLocation()); - //eventType = PlayerBlockEventType.BREAK_BLOCK; - blocktype1 = BukkitAdapter.asBlockType(block.getType()); - if (block.getType() == Material.DRAGON_EGG) { - eventType = PlayerBlockEventType.TELEPORT_OBJECT; - break; - } - - return; - } - default: - return; - } - if (PlotSquared.get().worldedit != null && pp.getAttribute("worldedit")) { - if (event.getMaterial() == Material - .getMaterial(PlotSquared.get().worldedit.getConfiguration().wandItem)) { - return; - } - } - if (!PlotSquared.get().getEventDispatcher() - .checkPlayerBlockEvent(pp, eventType, location, blocktype1, true)) { - event.setCancelled(true); - event.setUseInteractedBlock(Event.Result.DENY); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void creatureSpawnEvent(CreatureSpawnEvent event) { - Entity entity = event.getEntity(); - Location location = BukkitUtil.getLocation(entity.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - CreatureSpawnEvent.SpawnReason reason = event.getSpawnReason(); - switch (reason.toString()) { - case "DISPENSE_EGG": - case "EGG": - case "OCELOT_BABY": - case "SPAWNER_EGG": - if (!area.isSpawnEggs()) { - event.setCancelled(true); - return; - } - break; - case "REINFORCEMENTS": - case "NATURAL": - case "MOUNT": - case "PATROL": - case "RAID": - case "SHEARED": - case "SHOULDER_ENTITY": - case "SILVERFISH_BLOCK": - case "TRAP": - case "VILLAGE_DEFENSE": - case "VILLAGE_INVASION": - case "BEEHIVE": - case "CHUNK_GEN": - if (!area.isMobSpawning()) { - event.setCancelled(true); - return; - } - case "BREEDING": - if (!area.isSpawnBreeding()) { - event.setCancelled(true); - return; - } - break; - case "BUILD_IRONGOLEM": - case "BUILD_SNOWMAN": - case "BUILD_WITHER": - case "CUSTOM": - if (!area.isSpawnCustom() && entity.getType() != EntityType.ARMOR_STAND) { - event.setCancelled(true); - return; - } - break; - case "SPAWNER": - if (!area.isMobSpawnerSpawning()) { - event.setCancelled(true); - return; - } - break; - } - Plot plot = area.getOwnedPlotAbs(location); - if (plot == null) { - if (!area.isMobSpawning()) { - event.setCancelled(true); - } - return; - } - if (checkEntity(entity, plot)) { - event.setCancelled(true); - } - } - - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) - public void onEntityFall(EntityChangeBlockEvent event) { - if (event.getEntityType() != EntityType.FALLING_BLOCK) { - return; - } - Block block = event.getBlock(); - World world = block.getWorld(); - String worldName = world.getName(); - if (!PlotSquared.get().hasPlotArea(worldName)) { - return; - } - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlotAbs(location); - if (plot == null || plot.getFlag(DisablePhysicsFlag.class)) { - event.setCancelled(true); - plot.debug("Falling block event was cancelled because disable-physics = true"); - return; - } - if (event.getTo().hasGravity()) { - Entity entity = event.getEntity(); - List meta = entity.getMetadata("plot"); - if (meta.isEmpty()) { - return; - } - Plot origin = (Plot) meta.get(0).value(); - if (origin != null && !origin.equals(plot)) { - event.setCancelled(true); - entity.remove(); - } - } else if (event.getTo() == Material.AIR) { - event.getEntity() - .setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); - } - } - - @EventHandler public void onPrime(ExplosionPrimeEvent event) { - this.lastRadius = event.getRadius() + 1; - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockBurn(BlockBurnEvent event) { - Block block = event.getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - - Plot plot = location.getOwnedPlot(); - if (plot == null || !plot.getFlag(BlockBurnFlag.class)) { - if (plot != null) { - plot.debug("Block burning was cancelled because block-burn = false"); - } - event.setCancelled(true); - } - - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBlockIgnite(BlockIgniteEvent event) { - Player player = event.getPlayer(); - Entity ignitingEntity = event.getIgnitingEntity(); - Block block = event.getBlock(); - BlockIgniteEvent.IgniteCause igniteCause = event.getCause(); - Location location1 = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location1.getPlotArea(); - if (area == null) { - return; - } - if (igniteCause == BlockIgniteEvent.IgniteCause.LIGHTNING) { - event.setCancelled(true); - return; - } - - Plot plot = area.getOwnedPlot(location1); - if (player != null) { - BukkitPlayer pp = BukkitUtil.getPlayer(player); - if (plot == null) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_ROAD); - event.setCancelled(true); - } - } else if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_UNOWNED); - event.setCancelled(true); - } - } else if (!plot.isAdded(pp.getUUID())) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } - } else if (!plot.getFlag(BlockIgnitionFlag.class)) { - event.setCancelled(true); - } - } else { - if (plot == null) { - event.setCancelled(true); - return; - } - if (ignitingEntity != null) { - if (!plot.getFlag(BlockIgnitionFlag.class)) { - event.setCancelled(true); - plot.debug("Block ignition was cancelled because block-ignition = false"); - return; - } - if (igniteCause == BlockIgniteEvent.IgniteCause.FIREBALL) { - if (ignitingEntity instanceof Fireball) { - Projectile fireball = (Projectile) ignitingEntity; - Location location = null; - if (fireball.getShooter() instanceof Entity) { - Entity shooter = (Entity) fireball.getShooter(); - location = BukkitUtil.getLocation(shooter.getLocation()); - } else if (fireball.getShooter() instanceof BlockProjectileSource) { - Block shooter = - ((BlockProjectileSource) fireball.getShooter()).getBlock(); - location = BukkitUtil.getLocation(shooter.getLocation()); - } - if (location != null && !plot.equals(location.getPlot())) { - event.setCancelled(true); - } - } - } - - } else if (event.getIgnitingBlock() != null) { - Block ignitingBlock = event.getIgnitingBlock(); - Plot plotIgnited = BukkitUtil.getLocation(ignitingBlock.getLocation()).getPlot(); - if (igniteCause == BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL && ( - !plot.getFlag(BlockIgnitionFlag.class) || plotIgnited == null || !plotIgnited - .equals(plot)) || (igniteCause == BlockIgniteEvent.IgniteCause.SPREAD - || igniteCause == BlockIgniteEvent.IgniteCause.LAVA) && ( - !plot.getFlag(BlockIgnitionFlag.class) || plotIgnited == null || !plotIgnited - .equals(plot))) { - event.setCancelled(true); - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBucketEmpty(PlayerBucketEmptyEvent event) { - BlockFace bf = event.getBlockFace(); - Block block = - event.getBlockClicked().getLocation().add(bf.getModX(), bf.getModY(), bf.getModZ()) - .getBlock(); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - BukkitPlayer pp = BukkitUtil.getPlayer(event.getPlayer()); - Plot plot = area.getPlot(location); - if (plot == null) { - if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { - return; - } - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_ROAD); - event.setCancelled(true); - } else if (!plot.hasOwner()) { - if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { - return; - } - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_UNOWNED); - event.setCancelled(true); - } else if (!plot.isAdded(pp.getUUID())) { - List use = plot.getFlag(UseFlag.class); - final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); - for (final BlockTypeWrapper blockTypeWrapper : use) { - if (blockTypeWrapper.accepts(blockType)) { - return; - } - } - if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - return; - } - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onInventoryClose(InventoryCloseEvent event) { - HumanEntity closer = event.getPlayer(); - if (!(closer instanceof Player)) { - return; - } - Player player = (Player) closer; - PlotInventory.removePlotInventoryOpen(BukkitUtil.getPlayer(player)); - } - - @EventHandler(priority = EventPriority.MONITOR) public void onLeave(PlayerQuitEvent event) { - TaskManager.TELEPORT_QUEUE.remove(event.getPlayer().getName()); - BukkitPlayer pp = BukkitUtil.getPlayer(event.getPlayer()); - pp.unregister(); - PlotListener.logout(pp.getUUID()); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onBucketFill(PlayerBucketFillEvent event) { - Block blockClicked = event.getBlockClicked(); - Location location = BukkitUtil.getLocation(blockClicked.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Player player = event.getPlayer(); - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - Plot plot = area.getPlot(location); - if (plot == null) { - if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { - return; - } - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_ROAD); - event.setCancelled(true); - } else if (!plot.hasOwner()) { - if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { - return; - } - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_UNOWNED); - event.setCancelled(true); - } else if (!plot.isAdded(plotPlayer.getUUID())) { - List use = plot.getFlag(UseFlag.class); - Block block = event.getBlockClicked(); - final BlockType blockType = BukkitAdapter.asBlockType(block.getType()); - for (final BlockTypeWrapper blockTypeWrapper : use) { - if (blockTypeWrapper.accepts(blockType)) { - return; - } - } - if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - return; - } - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onVehicleCreate(VehicleCreateEvent event) { - Vehicle entity = event.getVehicle(); - Location location = BukkitUtil.getLocation(entity); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = area.getOwnedPlotAbs(location); - if (plot == null || checkEntity(entity, plot)) { - entity.remove(); - return; - } - if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { - entity - .setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.get().IMP, plot)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onHangingPlace(HangingPlaceEvent event) { - Block block = event.getBlock().getRelative(event.getBlockFace()); - Location location = BukkitUtil.getLocation(block.getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Player p = event.getPlayer(); - if (p == null) { - PlotSquared.debug("PlotSquared does not support HangingPlaceEvent for non-players."); - event.setCancelled(true); - return; - } - BukkitPlayer pp = BukkitUtil.getPlayer(p); - Plot plot = area.getPlot(location); - if (plot == null) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_ROAD); - event.setCancelled(true); - } - } else { - if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_UNOWNED); - event.setCancelled(true); - } - return; - } - if (!plot.isAdded(pp.getUUID())) { - if (!plot.getFlag(HangingPlaceFlag.class)) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - } - return; - } - } - if (checkEntity(event.getEntity(), plot)) { - event.setCancelled(true); - } - - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onHangingBreakByEntity(HangingBreakByEntityEvent event) { - Entity remover = event.getRemover(); - if (remover instanceof Player) { - Player p = (Player) remover; - Location location = BukkitUtil.getLocation(event.getEntity()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - BukkitPlayer pp = BukkitUtil.getPlayer(p); - Plot plot = area.getPlot(location); - if (plot == null) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_ROAD)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_ROAD); - event.setCancelled(true); - } - } else if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_UNOWNED); - event.setCancelled(true); - } - } else if (!plot.isAdded(pp.getUUID())) { - if (plot.getFlag(HangingBreakFlag.class)) { - return; - } - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_OTHER); - event.setCancelled(true); - plot.debug(p.getName() - + " could not break hanging entity because hanging-break = false"); - } - } - } else if (remover instanceof Projectile) { - Projectile p = (Projectile) remover; - if (p.getShooter() instanceof Player) { - Player shooter = (Player) p.getShooter(); - Location location = BukkitUtil.getLocation(event.getEntity()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - BukkitPlayer player = BukkitUtil.getPlayer(shooter); - Plot plot = area.getPlot(BukkitUtil.getLocation(event.getEntity())); - if (plot != null) { - if (!plot.hasOwner()) { - if (!Permissions - .hasPermission(player, Captions.PERMISSION_ADMIN_DESTROY_UNOWNED)) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_UNOWNED); - event.setCancelled(true); - } - } else if (!plot.isAdded(player.getUUID())) { - if (!plot.getFlag(HangingBreakFlag.class)) { - if (!Permissions - .hasPermission(player, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_DESTROY_OTHER); - event.setCancelled(true); - plot.debug(player.getName() - + " could not break hanging entity because hanging-break = false"); - } - } - } - } - } - } else { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { - Location location = BukkitUtil.getLocation(event.getRightClicked().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Player p = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(p); - Plot plot = area.getPlot(location); - if (plot == null && !area.isRoadFlags()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_ROAD)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_INTERACT_ROAD); - event.setCancelled(true); - } - } else if (plot != null && !plot.hasOwner()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_UNOWNED)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_INTERACT_UNOWNED); - event.setCancelled(true); - } - } else if ((plot != null && !plot.isAdded(pp.getUUID())) || (plot == null && area - .isRoadFlags())) { - final Entity entity = event.getRightClicked(); - final com.sk89q.worldedit.world.entity.EntityType entityType = - BukkitAdapter.adapt(entity.getType()); - - FlagContainer flagContainer; - if (plot == null) { - flagContainer = area.getRoadFlagContainer(); - } else { - flagContainer = plot.getFlagContainer(); - } - - if (EntityCategories.HOSTILE.contains(entityType) && flagContainer - .getFlag(HostileInteractFlag.class).getValue()) { - return; - } - - if (EntityCategories.ANIMAL.contains(entityType) && flagContainer - .getFlag(AnimalInteractFlag.class).getValue()) { - return; - } - - // This actually makes use of the interface, so we don't use the - // category - if (entity instanceof Tameable && ((Tameable) entity).isTamed() && flagContainer - .getFlag(TamedInteractFlag.class).getValue()) { - return; - } - - if (EntityCategories.VEHICLE.contains(entityType) && flagContainer - .getFlag(VehicleUseFlag.class).getValue()) { - return; - } - - if (EntityCategories.PLAYER.contains(entityType) && flagContainer - .getFlag(PlayerInteractFlag.class).getValue()) { - return; - } - - if (EntityCategories.VILLAGER.contains(entityType) && flagContainer - .getFlag(VillagerInteractFlag.class).getValue()) { - return; - } - - if ((EntityCategories.HANGING.contains(entityType) || EntityCategories.OTHER - .contains(entityType)) && flagContainer.getFlag(MiscInteractFlag.class) - .getValue()) { - return; - } - - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_INTERACT_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_INTERACT_OTHER); - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onVehicleDestroy(VehicleDestroyEvent event) { - Location location = BukkitUtil.getLocation(event.getVehicle()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Entity attacker = event.getAttacker(); - if (attacker instanceof Player) { - Player p = (Player) attacker; - BukkitPlayer pp = BukkitUtil.getPlayer(p); - Plot plot = area.getPlot(location); - if (plot == null) { - if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.road")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.vehicle.break.road"); - event.setCancelled(true); - } - } else { - if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.unowned")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.vehicle.break.unowned"); - event.setCancelled(true); - return; - } - return; - } - if (!plot.isAdded(pp.getUUID())) { - if (plot.getFlag(VehicleBreakFlag.class)) { - return; - } - if (!Permissions.hasPermission(pp, "plots.admin.vehicle.break.other")) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - "plots.admin.vehicle.break.other"); - event.setCancelled(true); - plot.debug(pp.getName() - + " could not break vehicle because vehicle-break = false"); - } - } - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPotionSplash(PotionSplashEvent event) { - ThrownPotion damager = event.getPotion(); - Location location = BukkitUtil.getLocation(damager); - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - int count = 0; - for (LivingEntity victim : event.getAffectedEntities()) { - if (!entityDamage(damager, victim)) { - event.setIntensity(victim, 0); - count++; - } - } - if ((count > 0 && count == event.getAffectedEntities().size()) || !onProjectileHit(event)) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onEntityCombustByEntity(EntityCombustByEntityEvent event) { - EntityDamageByEntityEvent eventChange = - new EntityDamageByEntityEvent(event.getCombuster(), event.getEntity(), - EntityDamageEvent.DamageCause.FIRE_TICK, event.getDuration()); - onEntityDamageByEntityEvent(eventChange); - if (eventChange.isCancelled()) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) { - Entity damager = event.getDamager(); - Location location = BukkitUtil.getLocation(damager); - if (!PlotSquared.get().hasPlotArea(location.getWorld())) { - return; - } - Entity victim = event.getEntity(); -/* - if (victim.getType().equals(EntityType.ITEM_FRAME)) { - Plot plot = BukkitUtil.getLocation(victim).getPlot(); - if (plot != null && !plot.isAdded(damager.getUniqueId())) { - event.setCancelled(true); - return; - } - } -*/ - if (!entityDamage(damager, victim, event.getCause())) { - if (event.isCancelled()) { - if (victim instanceof Ageable) { - Ageable ageable = (Ageable) victim; - if (ageable.getAge() == -24000) { - ageable.setAge(0); - ageable.setAdult(); - } - } - } - event.setCancelled(true); - } - } - - private boolean entityDamage(Entity damager, Entity victim) { - return entityDamage(damager, victim, null); - } - - private boolean entityDamage(Entity damager, Entity victim, - EntityDamageEvent.DamageCause cause) { - Location dloc = BukkitUtil.getLocation(damager); - Location vloc = BukkitUtil.getLocation(victim); - PlotArea dArea = dloc.getPlotArea(); - PlotArea vArea; - if (dArea != null && dArea.contains(vloc.getX(), vloc.getZ())) { - vArea = dArea; - } else { - vArea = vloc.getPlotArea(); - } - if (dArea == null && vArea == null) { - return true; - } - - Plot dplot; - if (dArea != null) { - dplot = dArea.getPlot(dloc); - } else { - dplot = null; - } - Plot vplot; - if (vArea != null) { - vplot = vArea.getPlot(vloc); - } else { - vplot = null; - } - - Plot plot; - String stub; - boolean isPlot = true; - if (dplot == null && vplot == null) { - if (dArea == null) { - return true; - } - plot = null; - stub = "road"; - isPlot = false; - } else { - // Prioritize plots for close to seamless pvp zones - if (victim.getTicksLived() > damager.getTicksLived()) { - if (dplot == null || !(victim instanceof Player)) { - if (vplot == null) { - plot = dplot; - } else { - plot = vplot; - } - } else { - plot = dplot; - } - } else if (dplot == null || !(victim instanceof Player)) { - if (vplot == null) { - plot = dplot; - } else { - plot = vplot; - } - } else if (vplot == null) { - plot = dplot; - } else { - plot = vplot; - } - if (plot.hasOwner()) { - stub = "other"; - } else { - stub = "unowned"; - } - } - boolean roadFlags = vArea != null ? vArea.isRoadFlags() : dArea.isRoadFlags(); - PlotArea area = vArea != null ? vArea : dArea; - - Player player; - if (damager instanceof Player) { // attacker is player - player = (Player) damager; - } else if (damager instanceof Projectile) { - Projectile projectile = (Projectile) damager; - ProjectileSource shooter = projectile.getShooter(); - if (shooter instanceof Player) { // shooter is player - player = (Player) shooter; - } else { // shooter is not player - if (shooter instanceof BlockProjectileSource) { - Location sLoc = BukkitUtil - .getLocation(((BlockProjectileSource) shooter).getBlock().getLocation()); - dplot = dArea.getPlot(sLoc); - } - player = null; - } - } else { // Attacker is not player - player = null; - } - if (player != null) { - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - - final com.sk89q.worldedit.world.entity.EntityType entityType; - - // Create a fake entity type if the type does not have a name - if (victim.getType().getName() == null) { - entityType = FAKE_ENTITY_TYPE; - } else { - entityType = BukkitAdapter.adapt(victim.getType()); - } - - if (EntityCategories.HANGING.contains(entityType)) { // hanging - if (plot != null && (plot.getFlag(HangingBreakFlag.class) || plot - .isAdded(plotPlayer.getUUID()))) { - if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions - .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - return false; - } - } - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.destroy." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.destroy." + stub); - return false; - } - } else if (victim.getType() == EntityType.ARMOR_STAND) { - if (plot != null && (plot.getFlag(MiscBreakFlag.class) || plot - .isAdded(plotPlayer.getUUID()))) { - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.destroy." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.destroy." + stub); - if (plot != null) { - plot.debug(player.getName() - + " could not break armor stand because misc-break = false"); - } - return false; - } - } else if (EntityCategories.HOSTILE.contains(entityType)) { - if (isPlot) { - if (plot.getFlag(HostileAttackFlag.class) || plot.getFlag(PveFlag.class) || plot - .isAdded(plotPlayer.getUUID())) { - return true; - } - } else if (roadFlags && (area.getRoadFlag(HostileAttackFlag.class) || area - .getFlag(PveFlag.class))) { - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pve." + stub); - if (plot != null) { - plot.debug(player.getName() + " could not attack " + entityType - + " because pve = false OR hostile-attack = false"); - } - return false; - } - } else if (EntityCategories.TAMEABLE.contains(entityType)) { // victim is tameable - if (isPlot) { - if (plot.getFlag(TamedAttackFlag.class) || plot.getFlag(PveFlag.class) || plot - .isAdded(plotPlayer.getUUID())) { - return true; - } - } else if (roadFlags && (area.getRoadFlag(TamedAttackFlag.class) || area - .getFlag(PveFlag.class))) { - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pve." + stub); - if (plot != null) { - plot.debug(player.getName() + " could not attack " + entityType - + " because pve = false OR tamned-attack = false"); - } - return false; - } - } else if (EntityCategories.PLAYER.contains(entityType)) { - if (isPlot) { - if (!plot.getFlag(PvpFlag.class) && !Permissions - .hasPermission(plotPlayer, "plots.admin.pvp." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pvp." + stub); - plot.debug(player.getName() + " could not attack " + entityType - + " because pve = false"); - return false; - } else { - return true; - } - } else if (roadFlags && area.getRoadFlag(PvpFlag.class)) { - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.pvp." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pvp." + stub); - return false; - } - } else if (EntityCategories.ANIMAL.contains(entityType)) { // victim is animal - if (isPlot) { - if (plot.getFlag(AnimalAttackFlag.class) || plot.getFlag(PveFlag.class) || plot - .isAdded(plotPlayer.getUUID())) { - plot.debug(player.getName() + " could not attack " + entityType - + " because pve = false OR animal-attack = false"); - return true; - } - } else if (roadFlags && (area.getRoadFlag(AnimalAttackFlag.class) || area - .getFlag(PveFlag.class))) { - if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pve." + stub); - return false; - } - } - } else if (EntityCategories.VEHICLE - .contains(entityType)) { // Vehicles are managed in vehicle destroy event - return true; - } else { // victim is something else - if (isPlot) { - if (plot.getFlag(PveFlag.class) || plot.isAdded(plotPlayer.getUUID())) { - return true; - } - } else if (roadFlags && area.getRoadFlag(PveFlag.class)) { - return true; - } - if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.pve." + stub); - if (plot != null) { - plot.debug(player.getName() + " could not attack " + entityType - + " because pve = false"); - } - return false; - } - } - return true; - } else if (dplot != null && (!dplot.equals(vplot) || Objects - .equals(dplot.getOwnerAbs(), vplot.getOwnerAbs()))) { - return vplot != null && vplot.getFlag(PveFlag.class); - } - //disable the firework damage. too much of a headache to support at the moment. - if (vplot != null) { - if (EntityDamageEvent.DamageCause.ENTITY_EXPLOSION == cause - && damager.getType() == EntityType.FIREWORK) { - return false; - } - } - if (vplot == null && roadFlags && area.getRoadFlag(PveFlag.class)) { - return true; - } - return ((vplot != null && vplot.getFlag(PveFlag.class)) || !(damager instanceof Arrow - && !(victim instanceof Creature))); - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPlayerEggThrow(PlayerEggThrowEvent event) { - Location location = BukkitUtil.getLocation(event.getEgg().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Player player = event.getPlayer(); - BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); - Plot plot = area.getPlot(location); - if (plot == null) { - if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.road")) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.projectile.road"); - event.setHatching(false); - } - } else if (!plot.hasOwner()) { - if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.unowned")) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.projectile.unowned"); - event.setHatching(false); - } - } else if (!plot.isAdded(plotPlayer.getUUID())) { - if (!Permissions.hasPermission(plotPlayer, "plots.admin.projectile.other")) { - MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, - "plots.admin.projectile.other"); - event.setHatching(false); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void blockCreate(BlockPlaceEvent event) { - Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - Plot plot = area.getPlot(location); - if (plot != null) { - if ((location.getY() > area.getMaxBuildHeight() || location.getY() < area - .getMinBuildHeight()) && !Permissions - .hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) { - event.setCancelled(true); - MainUtil.sendMessage(pp, Captions.HEIGHT_LIMIT.getTranslated() - .replace("{limit}", String.valueOf(area.getMaxBuildHeight()))); - } - if (!plot.hasOwner()) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_UNOWNED); - event.setCancelled(true); - return; - } - } else if (!plot.isAdded(pp.getUUID())) { - List place = plot.getFlag(PlaceFlag.class); - if (place != null) { - Block block = event.getBlock(); - if (place.contains( - BlockTypeWrapper.get(BukkitAdapter.asBlockType(block.getType())))) { - return; - } - } - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - plot.debug(player.getName() + " could not place " + event.getBlock().getType() - + " because of the place flag"); - return; - } - } else if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { - if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_OTHER); - event.setCancelled(true); - return; - } - } - if (plot.getFlag(DisablePhysicsFlag.class)) { - Block block = event.getBlockPlaced(); - if (block.getType().hasGravity()) { - sendBlockChange(block.getLocation(), block.getBlockData()); - plot.debug(event.getBlock().getType() - + " did not fall because of disable-physics = true"); - } - } - } else if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_ROAD)) { - MainUtil.sendMessage(pp, Captions.NO_PERMISSION_EVENT, - Captions.PERMISSION_ADMIN_BUILD_ROAD); - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGH) public void onDamage(EntityDamageEvent event) { - if (event.getEntityType() != EntityType.PLAYER) { - return; - } - Location location = BukkitUtil.getLocation(event.getEntity()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(InvincibleFlag.class)) { - event.setCancelled(true); - } - return; - } - if (plot.getFlag(InvincibleFlag.class)) { - plot.debug( - event.getEntity().getName() + " could not take damage because invincible = true"); - event.setCancelled(true); - } - } - - @EventHandler public void onItemDrop(PlayerDropItemEvent event) { - Player player = event.getPlayer(); - BukkitPlayer pp = BukkitUtil.getPlayer(player); - Location location = pp.getLocation(); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && !area.getRoadFlag(ItemDropFlag.class)) { - event.setCancelled(true); - } - return; - } - UUID uuid = pp.getUUID(); - if (!plot.isAdded(uuid)) { - if (!plot.getFlag(ItemDropFlag.class)) { - plot.debug(player.getName() + " could not drop item because of item-drop = false"); - event.setCancelled(true); - } - } - } - - @EventHandler public void onItemPickup(EntityPickupItemEvent event) { - LivingEntity ent = event.getEntity(); - if (ent instanceof Player) { - Player player = (Player) ent; - BukkitPlayer pp = BukkitUtil.getPlayer(player); - Location location = pp.getLocation(); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(DropProtectionFlag.class)) { - event.setCancelled(true); - } - return; - } - UUID uuid = pp.getUUID(); - if (!plot.isAdded(uuid) && plot.getFlag(DropProtectionFlag.class)) { - plot.debug( - player.getName() + " could not pick up item because of drop-protection = true"); - event.setCancelled(true); - } - } - } - - @EventHandler public void onDeath(final PlayerDeathEvent event) { - Location location = BukkitUtil.getLocation(event.getEntity()); - PlotArea area = location.getPlotArea(); - if (area == null) { - return; - } - Plot plot = location.getOwnedPlot(); - if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(KeepInventoryFlag.class)) { - event.setCancelled(true); - } - return; - } - if (plot.getFlag(KeepInventoryFlag.class)) { - if (plot.getFlag(KeepInventoryFlag.class)) { - plot.debug(event.getEntity().getName() - + " kept their inventory because of keep-inventory = true"); - event.setKeepInventory(true); - } - } - } -} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java new file mode 100644 index 000000000..a4cfd6171 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java @@ -0,0 +1,156 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.util.BukkitEntityUtil; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.PlotHandler; +import com.plotsquared.core.util.Permissions; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.ThrownPotion; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.LingeringPotionSplashEvent; +import org.bukkit.event.entity.PotionSplashEvent; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.entity.ProjectileLaunchEvent; +import org.bukkit.projectiles.BlockProjectileSource; +import org.bukkit.projectiles.ProjectileSource; + +@SuppressWarnings("unused") +public class ProjectileEventListener implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPotionSplash(LingeringPotionSplashEvent event) { + Projectile entity = event.getEntity(); + Location location = BukkitUtil.getLocation(entity); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + if (!this.onProjectileHit(event)) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPotionSplash(PotionSplashEvent event) { + ThrownPotion damager = event.getPotion(); + Location location = BukkitUtil.getLocation(damager); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + int count = 0; + for (LivingEntity victim : event.getAffectedEntities()) { + if (!BukkitEntityUtil.entityDamage(damager, victim)) { + event.setIntensity(victim, 0); + count++; + } + } + if ((count > 0 && count == event.getAffectedEntities().size()) || !onProjectileHit(event)) { + event.setCancelled(true); + } + } + + @EventHandler public void onProjectileLaunch(ProjectileLaunchEvent event) { + Projectile entity = event.getEntity(); + if (!(entity instanceof ThrownPotion)) { + return; + } + ProjectileSource shooter = entity.getShooter(); + if (!(shooter instanceof Player)) { + return; + } + Location location = BukkitUtil.getLocation(entity); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + PlotPlayer pp = BukkitUtil.getPlayer((Player) shooter); + Plot plot = location.getOwnedPlot(); + if (plot != null && !plot.isAdded(pp.getUUID())) { + entity.remove(); + event.setCancelled(true); + } + } + + @SuppressWarnings({"BooleanMethodIsAlwaysInverted", "cos it's not... dum IntelliJ"}) @EventHandler + public boolean onProjectileHit(ProjectileHitEvent event) { + Projectile entity = event.getEntity(); + Location location = BukkitUtil.getLocation(entity); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return true; + } + PlotArea area = location.getPlotArea(); + if (area == null) { + return true; + } + Plot plot = area.getPlot(location); + ProjectileSource shooter = entity.getShooter(); + if (shooter instanceof Player) { + PlotPlayer pp = BukkitUtil.getPlayer((Player) shooter); + if (plot == null) { + if (!Permissions.hasPermission(pp, Captions.PERMISSION_PROJECTILE_UNOWNED)) { + entity.remove(); + return false; + } + return true; + } + if (plot.isAdded(pp.getUUID()) || Permissions + .hasPermission(pp, Captions.PERMISSION_PROJECTILE_OTHER)) { + return true; + } + entity.remove(); + return false; + } + if (!(shooter instanceof Entity) && shooter != null) { + if (plot == null) { + entity.remove(); + return false; + } + Location sLoc = + BukkitUtil.getLocation(((BlockProjectileSource) shooter).getBlock().getLocation()); + if (!area.contains(sLoc.getX(), sLoc.getZ())) { + entity.remove(); + return false; + } + Plot sPlot = area.getOwnedPlotAbs(sLoc); + if (sPlot == null || !PlotHandler.sameOwners(plot, sPlot)) { + entity.remove(); + return false; + } + } + return true; + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java new file mode 100644 index 000000000..d5ef32ba3 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java @@ -0,0 +1,363 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.util; + +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.flag.implementations.AnimalAttackFlag; +import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag; +import com.plotsquared.core.plot.flag.implementations.DoneFlag; +import com.plotsquared.core.plot.flag.implementations.EntityCapFlag; +import com.plotsquared.core.plot.flag.implementations.HangingBreakFlag; +import com.plotsquared.core.plot.flag.implementations.HostileAttackFlag; +import com.plotsquared.core.plot.flag.implementations.HostileCapFlag; +import com.plotsquared.core.plot.flag.implementations.MiscBreakFlag; +import com.plotsquared.core.plot.flag.implementations.MiscCapFlag; +import com.plotsquared.core.plot.flag.implementations.MobCapFlag; +import com.plotsquared.core.plot.flag.implementations.PveFlag; +import com.plotsquared.core.plot.flag.implementations.PvpFlag; +import com.plotsquared.core.plot.flag.implementations.TamedAttackFlag; +import com.plotsquared.core.plot.flag.implementations.VehicleCapFlag; +import com.plotsquared.core.util.EntityUtil; +import com.plotsquared.core.util.MainUtil; +import com.plotsquared.core.util.Permissions; +import com.plotsquared.core.util.entity.EntityCategories; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Creature; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.projectiles.BlockProjectileSource; +import org.bukkit.projectiles.ProjectileSource; + +import java.util.Objects; + +public class BukkitEntityUtil { + + public static final com.sk89q.worldedit.world.entity.EntityType FAKE_ENTITY_TYPE = + new com.sk89q.worldedit.world.entity.EntityType("plotsquared:fake"); + + public static boolean entityDamage(Entity damager, Entity victim) { + return entityDamage(damager, victim, null); + } + + public static boolean entityDamage(Entity damager, Entity victim, + EntityDamageEvent.DamageCause cause) { + Location dloc = BukkitUtil.getLocation(damager); + Location vloc = BukkitUtil.getLocation(victim); + PlotArea dArea = dloc.getPlotArea(); + PlotArea vArea; + if (dArea != null && dArea.contains(vloc.getX(), vloc.getZ())) { + vArea = dArea; + } else { + vArea = vloc.getPlotArea(); + } + if (dArea == null && vArea == null) { + return true; + } + + Plot dplot; + if (dArea != null) { + dplot = dArea.getPlot(dloc); + } else { + dplot = null; + } + Plot vplot; + if (vArea != null) { + vplot = vArea.getPlot(vloc); + } else { + vplot = null; + } + + Plot plot; + String stub; + boolean isPlot = true; + if (dplot == null && vplot == null) { + if (dArea == null) { + return true; + } + plot = null; + stub = "road"; + isPlot = false; + } else { + // Prioritize plots for close to seamless pvp zones + if (victim.getTicksLived() > damager.getTicksLived()) { + if (dplot == null || !(victim instanceof Player)) { + if (vplot == null) { + plot = dplot; + } else { + plot = vplot; + } + } else { + plot = dplot; + } + } else if (dplot == null || !(victim instanceof Player)) { + if (vplot == null) { + plot = dplot; + } else { + plot = vplot; + } + } else if (vplot == null) { + plot = dplot; + } else { + plot = vplot; + } + if (plot.hasOwner()) { + stub = "other"; + } else { + stub = "unowned"; + } + } + boolean roadFlags = vArea != null ? vArea.isRoadFlags() : dArea.isRoadFlags(); + PlotArea area = vArea != null ? vArea : dArea; + + Player player; + if (damager instanceof Player) { // attacker is player + player = (Player) damager; + } else if (damager instanceof Projectile) { + Projectile projectile = (Projectile) damager; + ProjectileSource shooter = projectile.getShooter(); + if (shooter instanceof Player) { // shooter is player + player = (Player) shooter; + } else { // shooter is not player + if (shooter instanceof BlockProjectileSource) { + Location sLoc = BukkitUtil + .getLocation(((BlockProjectileSource) shooter).getBlock().getLocation()); + dplot = dArea.getPlot(sLoc); + } + player = null; + } + } else { // Attacker is not player + player = null; + } + if (player != null) { + BukkitPlayer plotPlayer = BukkitUtil.getPlayer(player); + + final com.sk89q.worldedit.world.entity.EntityType entityType; + + // Create a fake entity type if the type does not have a name + if (victim.getType().getName() == null) { + entityType = FAKE_ENTITY_TYPE; + } else { + entityType = BukkitAdapter.adapt(victim.getType()); + } + + if (EntityCategories.HANGING.contains(entityType)) { // hanging + if (plot != null && (plot.getFlag(HangingBreakFlag.class) || plot + .isAdded(plotPlayer.getUUID()))) { + if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + if (!Permissions + .hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + Captions.PERMISSION_ADMIN_BUILD_OTHER); + return false; + } + } + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.destroy." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.destroy." + stub); + return false; + } + } else if (victim.getType() == EntityType.ARMOR_STAND) { + if (plot != null && (plot.getFlag(MiscBreakFlag.class) || plot + .isAdded(plotPlayer.getUUID()))) { + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.destroy." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.destroy." + stub); + if (plot != null) { + plot.debug(player.getName() + + " could not break armor stand because misc-break = false"); + } + return false; + } + } else if (EntityCategories.HOSTILE.contains(entityType)) { + if (isPlot) { + if (plot.getFlag(HostileAttackFlag.class) || plot.getFlag(PveFlag.class) || plot + .isAdded(plotPlayer.getUUID())) { + return true; + } + } else if (roadFlags && (area.getRoadFlag(HostileAttackFlag.class) || area + .getFlag(PveFlag.class))) { + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pve." + stub); + if (plot != null) { + plot.debug(player.getName() + " could not attack " + entityType + + " because pve = false OR hostile-attack = false"); + } + return false; + } + } else if (EntityCategories.TAMEABLE.contains(entityType)) { // victim is tameable + if (isPlot) { + if (plot.getFlag(TamedAttackFlag.class) || plot.getFlag(PveFlag.class) || plot + .isAdded(plotPlayer.getUUID())) { + return true; + } + } else if (roadFlags && (area.getRoadFlag(TamedAttackFlag.class) || area + .getFlag(PveFlag.class))) { + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pve." + stub); + if (plot != null) { + plot.debug(player.getName() + " could not attack " + entityType + + " because pve = false OR tamned-attack = false"); + } + return false; + } + } else if (EntityCategories.PLAYER.contains(entityType)) { + if (isPlot) { + if (!plot.getFlag(PvpFlag.class) && !Permissions + .hasPermission(plotPlayer, "plots.admin.pvp." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pvp." + stub); + plot.debug(player.getName() + " could not attack " + entityType + + " because pve = false"); + return false; + } else { + return true; + } + } else if (roadFlags && area.getRoadFlag(PvpFlag.class)) { + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.pvp." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pvp." + stub); + return false; + } + } else if (EntityCategories.ANIMAL.contains(entityType)) { // victim is animal + if (isPlot) { + if (plot.getFlag(AnimalAttackFlag.class) || plot.getFlag(PveFlag.class) || plot + .isAdded(plotPlayer.getUUID())) { + plot.debug(player.getName() + " could not attack " + entityType + + " because pve = false OR animal-attack = false"); + return true; + } + } else if (roadFlags && (area.getRoadFlag(AnimalAttackFlag.class) || area + .getFlag(PveFlag.class))) { + if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pve." + stub); + return false; + } + } + } else if (EntityCategories.VEHICLE + .contains(entityType)) { // Vehicles are managed in vehicle destroy event + return true; + } else { // victim is something else + if (isPlot) { + if (plot.getFlag(PveFlag.class) || plot.isAdded(plotPlayer.getUUID())) { + return true; + } + } else if (roadFlags && area.getRoadFlag(PveFlag.class)) { + return true; + } + if (!Permissions.hasPermission(plotPlayer, "plots.admin.pve." + stub)) { + MainUtil.sendMessage(plotPlayer, Captions.NO_PERMISSION_EVENT, + "plots.admin.pve." + stub); + if (plot != null) { + plot.debug(player.getName() + " could not attack " + entityType + + " because pve = false"); + } + return false; + } + } + return true; + } else if (dplot != null && (!dplot.equals(vplot) || Objects + .equals(dplot.getOwnerAbs(), vplot.getOwnerAbs()))) { + return vplot != null && vplot.getFlag(PveFlag.class); + } + //disable the firework damage. too much of a headache to support at the moment. + if (vplot != null) { + if (EntityDamageEvent.DamageCause.ENTITY_EXPLOSION == cause + && damager.getType() == EntityType.FIREWORK) { + return false; + } + } + if (vplot == null && roadFlags && area.getRoadFlag(PveFlag.class)) { + return true; + } + return ((vplot != null && vplot.getFlag(PveFlag.class)) || !(damager instanceof Arrow + && !(victim instanceof Creature))); + } + + public static boolean checkEntity(Entity entity, Plot plot) { + if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot.getArea() + .getFlagContainer().getFlagMap().isEmpty()) { + return false; + } + + final com.sk89q.worldedit.world.entity.EntityType entityType = + BukkitAdapter.adapt(entity.getType()); + + if (EntityCategories.PLAYER.contains(entityType)) { + return false; + } + + if (EntityCategories.PROJECTILE.contains(entityType) || EntityCategories.OTHER + .contains(entityType) || EntityCategories.HANGING.contains(entityType)) { + return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, + MiscCapFlag.MISC_CAP_UNLIMITED); + } + + // Has to go go before vehicle as horses are both + // animals and vehicles + if (EntityCategories.ANIMAL.contains(entityType) || EntityCategories.VILLAGER + .contains(entityType) || EntityCategories.TAMEABLE.contains(entityType)) { + return EntityUtil + .checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, MobCapFlag.MOB_CAP_UNLIMITED, + AnimalCapFlag.ANIMAL_CAP_UNLIMITED); + } + + if (EntityCategories.HOSTILE.contains(entityType)) { + return EntityUtil + .checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, MobCapFlag.MOB_CAP_UNLIMITED, + HostileCapFlag.HOSTILE_CAP_UNLIMITED); + } + + if (EntityCategories.VEHICLE.contains(entityType)) { + return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED, + VehicleCapFlag.VEHICLE_CAP_UNLIMITED); + } + + return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/IPlotMain.java b/Core/src/main/java/com/plotsquared/core/IPlotMain.java index a38045acb..d88d7ccb9 100644 --- a/Core/src/main/java/com/plotsquared/core/IPlotMain.java +++ b/Core/src/main/java/com/plotsquared/core/IPlotMain.java @@ -162,7 +162,7 @@ public interface IPlotMain

extends ILogger { /** * Register the protection system. */ - void registerPlayerEvents(); + void registerEvents(); /** * Register force field events. diff --git a/Core/src/main/java/com/plotsquared/core/util/EntityUtil.java b/Core/src/main/java/com/plotsquared/core/util/EntityUtil.java index 9bbc4e1aa..de451de83 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EntityUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/EntityUtil.java @@ -43,7 +43,7 @@ import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE; * Entity related general utility methods */ @UtilityClass -public final class EntityUtil { +public class EntityUtil { private static int capNumeral(@NonNull final String flagName) { int i; From c045ef698cae5724697d2d034afa074a0e48bbed Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 12 Jul 2020 11:42:45 +0100 Subject: [PATCH 02/21] Add license headers and fix method change in PlotSquared.java --- .../bukkit/listener/BlockEventListener.java | 25 +++++++++++++++++++ .../bukkit/listener/EntityEventListener.java | 25 +++++++++++++++++++ .../com/plotsquared/core/PlotSquared.java | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java index 220e8d073..99bce75fe 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.bukkit.listener; import com.plotsquared.bukkit.player.BukkitPlayer; diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java index 8de57cdaf..b052cc70b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.bukkit.listener; import com.plotsquared.bukkit.util.BukkitEntityUtil; diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index e9048f726..3e0e0443b 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -252,7 +252,7 @@ public class PlotSquared { this.IMP.runEntityTask(); } if (Settings.Enabled_Components.EVENTS) { - this.IMP.registerPlayerEvents(); + this.IMP.registerEvents(); } // Required this.IMP.registerWorldEvents(); From 14b6f84816869e3ac94ecfd42f30bbb084b199ca Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 12 Jul 2020 18:50:04 +0100 Subject: [PATCH 03/21] fix typos that were c/p over --- .../com/plotsquared/bukkit/listener/BlockEventListener.java | 2 +- .../com/plotsquared/bukkit/listener/EntityEventListener.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java index 99bce75fe..899a03a04 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -661,7 +661,7 @@ public class BlockEventListener implements Listener { if (plot != null && plot.getFlag(LiquidFlowFlag.class) == LiquidFlowFlag.FlowStatus.DISABLED && event .getBlock().isLiquid()) { - plot.debug("Liquid could now flow because liquid-flow = disabled"); + plot.debug("Liquid could not flow because liquid-flow = disabled"); event.setCancelled(true); return; } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java index b052cc70b..0363d42cf 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java @@ -200,7 +200,7 @@ public class EntityEventListener implements Listener { if (plot == null || plot.getFlag(DisablePhysicsFlag.class)) { event.setCancelled(true); if (plot != null) { - plot.debug("Fallin block event was cancelled because disable-physics = true"); + plot.debug("Falling block event was cancelled because disable-physics = true"); } return; } From af7db08036c4769544004888070e832a8d4a7f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 13:14:02 +0200 Subject: [PATCH 04/21] Add new placeholder system --- .../bukkit/placeholder/Placeholders.java | 130 +--------- .../com/plotsquared/core/PlotSquared.java | 9 + .../java/com/plotsquared/core/plot/Plot.java | 2 +- .../core/plot/flag/GlobalFlagContainer.java | 10 +- .../core/util/EventDispatcher.java | 8 +- .../core/util/placeholders/Placeholder.java | 63 +++++ .../placeholders/PlaceholderRegistry.java | 230 ++++++++++++++++++ .../placeholders/PlotFlagPlaceholder.java | 75 ++++++ .../placeholders/PlotSpecificPlaceholder.java | 59 +++++ 9 files changed, 455 insertions(+), 131 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java index c61b29f37..783d80257 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -27,17 +27,10 @@ package com.plotsquared.bukkit.placeholder; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.plot.flag.GlobalFlagContainer; -import com.plotsquared.core.plot.flag.PlotFlag; -import com.plotsquared.core.util.MainUtil; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import java.util.UUID; - public class Placeholders extends PlaceholderExpansion { public Placeholders() { @@ -70,6 +63,7 @@ public class Placeholders extends PlaceholderExpansion { return ""; } + // PAPI specific ones that don't translate well over into other placeholder APIs if (identifier.startsWith("has_plot_")) { identifier = identifier.substring("has_plot_".length()); if (identifier.isEmpty()) @@ -88,126 +82,8 @@ public class Placeholders extends PlaceholderExpansion { return String.valueOf(pl.getPlotCount(identifier)); } - switch (identifier) { - case "currentplot_world": { - return p.getWorld().getName(); - } - case "has_plot": { - return (pl.getPlotCount() > 0) ? - PlaceholderAPIPlugin.booleanTrue() : - PlaceholderAPIPlugin.booleanFalse(); - } - case "allowed_plot_count": { - return String.valueOf(pl.getAllowedPlots()); - } - case "plot_count": { - return String.valueOf(pl.getPlotCount()); - } - } - - Plot plot = pl.getCurrentPlot(); - - if (plot == null) { - return ""; - } - - switch (identifier) { - case "currentplot_alias": { - return plot.getAlias(); - } - case "currentplot_owner": { - final UUID plotOwner = plot.getOwnerAbs(); - if (plotOwner == null) { - return ""; - } - - try { - return MainUtil.getName(plotOwner, false); - } catch (final Exception ignored) {} - - final String name = Bukkit.getOfflinePlayer(plotOwner).getName(); - return name != null ? name : "unknown"; - } - case "currentplot_members": { - if (plot.getMembers() == null && plot.getTrusted() == null) { - return "0"; - } - return String.valueOf(plot.getMembers().size() + plot.getTrusted().size()); - } - case "currentplot_members_added": { - if (plot.getMembers() == null) { - return "0"; - } - return String.valueOf(plot.getMembers().size()); - } - case "currentplot_members_trusted": { - if (plot.getTrusted() == null) { - return "0"; - } - return String.valueOf(plot.getTrusted().size()); - } - case "currentplot_members_denied": { - if (plot.getDenied() == null) { - return "0"; - } - return String.valueOf(plot.getDenied().size()); - } - case "has_build_rights": { - return plot.isAdded(pl.getUUID()) ? - PlaceholderAPIPlugin.booleanTrue() : - PlaceholderAPIPlugin.booleanFalse(); - } - case "currentplot_x": { - return String.valueOf(plot.getId().getX()); - } - case "currentplot_y": { - return String.valueOf(plot.getId().getY()); - } - case "currentplot_xy": { - return plot.getId().getX() + ";" + plot.getId().getY(); - } - case "currentplot_rating": { - return String.valueOf(plot.getAverageRating()); - } - case "currentplot_biome": { - return plot.getBiomeSynchronous() + ""; - } - default: - break; - } - if (identifier.startsWith("currentplot_localflag_")) { - return getFlagValue(plot, identifier.substring("currentplot_localflag_".length()), - false); - } - if (identifier.startsWith("currentplot_flag_")) { - return getFlagValue(plot, identifier.substring("currentplot_flag_".length()), true); - } - return ""; + // PlotSquared placeholders + return PlotSquared.get().getPlaceholderRegistry().getPlaceholderValue(identifier, pl); } - /** - * Return the flag value from its name on the current plot. - * If the flag doesn't exist it returns an empty string. - * If the flag exists but it is not set on current plot and the parameter inherit is set to true, - * it returns the default value. - * - * @param plot Current plot where the player is - * @param flagName Name of flag to get from current plot - * @param inherit Define if it returns only the flag set on currentplot or also inherited flag - * @return The value of flag serialized in string - */ - private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { - if (flagName.isEmpty()) - return ""; - final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); - if (flag == null) - return ""; - - if (inherit) { - return plot.getFlag(flag).toString(); - } else { - final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); - return (plotFlag != null) ? plotFlag.getValue().toString() : ""; - } - } } diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index e9048f726..3451b0a1c 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -62,6 +62,7 @@ import com.plotsquared.core.plot.PlotManager; import com.plotsquared.core.plot.comment.CommentManager; import com.plotsquared.core.plot.expiration.ExpireManager; import com.plotsquared.core.plot.expiration.ExpiryTask; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; import com.plotsquared.core.plot.world.DefaultPlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotArea; @@ -82,6 +83,7 @@ import com.plotsquared.core.util.SetupUtils; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.WorldUtil; import com.plotsquared.core.util.logger.ILogger; +import com.plotsquared.core.util.placeholders.PlaceholderRegistry; import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.uuid.UUIDPipeline; @@ -171,6 +173,7 @@ public class PlotSquared { private File storageFile; @Getter private PlotAreaManager plotAreaManager; @Getter private EventDispatcher eventDispatcher; + @Getter private PlaceholderRegistry placeholderRegistry; /** * Initialize PlotSquared with the desired Implementation class. @@ -194,6 +197,9 @@ public class PlotSquared { // ConfigurationSerialization.registerClass(BlockBucket.class, "BlockBucket"); + // Setup the global flag container + GlobalFlagContainer.setup(); + try { new ReflectionUtils(this.IMP.getNMSPackage()); try { @@ -209,6 +215,7 @@ public class PlotSquared { "PlotSquared-" + platform + ".jar"); } } + TaskManager.IMP = this.IMP.getTaskManager(); // World Util. Has to be done before config files are loaded @@ -262,6 +269,8 @@ public class PlotSquared { startExpiryTasks(); // Create Event utility class eventDispatcher = new EventDispatcher(); + // Create placeholder registry + placeholderRegistry = new PlaceholderRegistry(eventDispatcher); // create Hybrid utility class HybridUtils.manager = this.IMP.initHybridUtils(); // Inventory utility class diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 2adca5334..ac1766e93 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -2256,7 +2256,7 @@ public class Plot { * * @return The plot alias */ - public String getAlias() { + @NotNull public String getAlias() { if (this.settings == null) { return ""; } diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java index 4d7fdd396..3c44a8b5a 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.plot.flag; +import com.google.common.base.Preconditions; import com.plotsquared.core.plot.flag.implementations.AnalysisFlag; import com.plotsquared.core.plot.flag.implementations.AnimalAttackFlag; import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag; @@ -114,7 +115,13 @@ import java.util.Map; public final class GlobalFlagContainer extends FlagContainer { - @Getter private static final GlobalFlagContainer instance = new GlobalFlagContainer(); + @Getter private static GlobalFlagContainer instance; + + public static void setup() { + Preconditions.checkState(instance == null, "Cannot setup the container twice"); + instance = new GlobalFlagContainer(); + } + private static Map> stringClassMap; private GlobalFlagContainer() { @@ -124,6 +131,7 @@ public final class GlobalFlagContainer extends FlagContainer { } }); stringClassMap = new HashMap<>(); + // Register all default flags here // Boolean flags this.addFlag(ExplosionFlag.EXPLOSION_FALSE); diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index cb554b754..23d5dd15f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -80,9 +80,9 @@ import java.util.UUID; public class EventDispatcher { - private EventBus eventBus = new EventBus("PlotSquaredEvents"); + private final EventBus eventBus = new EventBus("PlotSquaredEvents"); - private List listeners = new ArrayList<>(); + private final List listeners = new ArrayList<>(); public void registerListener(Object listener) { eventBus.register(listener); @@ -100,6 +100,10 @@ public class EventDispatcher { } } + public void callGenericEvent(@NotNull final Object event) { + eventBus.post(event); + } + public void callEvent(@NotNull final PlotEvent event) { eventBus.post(event); } diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java new file mode 100644 index 000000000..38e28a586 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java @@ -0,0 +1,63 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.util.placeholders; + +import com.google.common.base.Preconditions; +import com.plotsquared.core.player.PlotPlayer; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.jetbrains.annotations.NotNull; + +/** + * A placeholder is a keyed value that gets replaced by a {@link PlotPlayer player}-specific value at runtime + */ +@EqualsAndHashCode(of = "key") @ToString(of = "key") +public abstract class Placeholder { + + private final String key; + + public Placeholder(@NotNull final String key) { + this.key = Preconditions.checkNotNull(key, "Key may not be null"); + } + + /** + * Get the value of the placeholder for a particular player + * + * @param player Player + * @return Placeholder value. Return {@code ""} if no placeholder value can be returned + */ + @NotNull public abstract String getValue(@NotNull final PlotPlayer player); + + /** + * Get the placeholder key + * + * @return Placeholder key + */ + @NotNull public final String getKey() { + return this.key; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java new file mode 100644 index 000000000..243ca0909 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java @@ -0,0 +1,230 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.util.placeholders; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; +import com.plotsquared.core.util.EventDispatcher; +import com.plotsquared.core.util.MainUtil; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.Collections; +import java.util.Locale; +import java.util.Map; +import java.util.UUID; +import java.util.function.BiFunction; + +/** + * Registry that contains {@link Placeholder placeholders} + */ +public final class PlaceholderRegistry { + + private final Map placeholders; + private final EventDispatcher eventDispatcher; + + public PlaceholderRegistry(@NotNull final EventDispatcher eventDispatcher) { + this.placeholders = Maps.newHashMap(); + this.eventDispatcher = eventDispatcher; + this.registerDefault(); + } + + private void registerDefault() { + final GlobalFlagContainer globalFlagContainer = GlobalFlagContainer.getInstance(); + for (final PlotFlag flag : globalFlagContainer.getRecognizedPlotFlags()) { + this.registerPlaceholder(new PlotFlagPlaceholder(flag, true)); + this.registerPlaceholder(new PlotFlagPlaceholder(flag, false)); + } + GlobalFlagContainer.getInstance().subscribe((flag, type) -> { + this.registerPlaceholder(new PlotFlagPlaceholder(flag, true)); + this.registerPlaceholder(new PlotFlagPlaceholder(flag, false)); + }); + this.createPlaceholder("currentplot_world", player -> player.getLocation().getWorld()); + this.createPlaceholder("has_plot", player -> player.getPlotCount() > 0 ? "true" : "false"); + this.createPlaceholder("allowed_plot_count", player -> Integer.toString(player.getAllowedPlots())); + this.createPlaceholder("plot_count", player -> Integer.toString(player.getPlotCount())); + this.createPlaceholder("currentplot_alias", (player, plot) -> plot.getAlias()); + this.createPlaceholder("currentplot_owner", (player, plot) -> { + final UUID plotOwner = plot.getOwnerAbs(); + if (plotOwner == null) { + return ""; + } + + try { + return MainUtil.getName(plotOwner, false); + } catch (final Exception ignored) {} + + return "unknown"; + }); + this.createPlaceholder("currentplot_members", (player, plot) -> { + if (plot.getMembers() == null && plot.getTrusted() == null) { + return "0"; + } + return String.valueOf(plot.getMembers().size() + plot.getTrusted().size()); + }); + this.createPlaceholder("currentplot_members_added", (player, plot) -> { + if (plot.getMembers() == null) { + return "0"; + } + return String.valueOf(plot.getMembers().size()); + }); + this.createPlaceholder("currentplot_members_trusted", (player, plot) -> { + if (plot.getTrusted() == null) { + return "0"; + } + return String.valueOf(plot.getTrusted().size()); + }); + this.createPlaceholder("currentplot_members_denied", (player, plot) -> { + if (plot.getDenied() == null) { + return "0"; + } + return String.valueOf(plot.getDenied().size()); + }); + this.createPlaceholder("has_build_rights", (player, plot) -> + plot.isAdded(player.getUUID()) ? "true" : "false"); + this.createPlaceholder("currentplot_x", (player, plot) -> Integer.toString(plot.getId().getX())); + this.createPlaceholder("currentplot_y", (player, plot) -> Integer.toString(plot.getId().getY())); + this.createPlaceholder("currentplot_xy", (player, plot) -> plot.getId().toString()); + this.createPlaceholder("currentplot_rating", (player, plot) -> Double.toString(plot.getAverageRating())); + this.createPlaceholder("currentplot_biome", (player, plot) -> plot.getBiomeSynchronous().toString()); + } + + /** + * Create a functional placeholder + * + * @param key Placeholder key + * @param placeholderFunction Placeholder generator. Cannot return null + */ + @SuppressWarnings("ALL") public void createPlaceholder(@NotNull final String key, + @NotNull final Function, String> placeholderFunction) { + this.registerPlaceholder(new Placeholder(key) { + @Override @NotNull public String getValue(@NotNull final PlotPlayer player) { + return placeholderFunction.apply(player); + } + }); + } + + /** + * Create a functional placeholder + * + * @param key Placeholder key + * @param placeholderFunction Placeholder generator. Cannot return null + */ + public void createPlaceholder(@NotNull final String key, + @NotNull final BiFunction, Plot, String> placeholderFunction) { + this.registerPlaceholder(new PlotSpecificPlaceholder(key) { + @Override @NotNull public String getValue(@NotNull final PlotPlayer player, @NotNull final Plot plot) { + return placeholderFunction.apply(player, plot); + } + }); + } + + /** + * Register a placeholder + * + * @param placeholder Placeholder instance + */ + public void registerPlaceholder(@NotNull final Placeholder placeholder) { + final Placeholder previous = this.placeholders + .put(placeholder.getKey().toLowerCase(Locale.ENGLISH), + Preconditions.checkNotNull(placeholder, "Placeholder may not be null")); + if (previous != null) { + this.eventDispatcher.callGenericEvent(new PlaceholderAddedEvent(placeholder)); + } + } + + /** + * Get a placeholder instance from its key + * + * @param key Placeholder key + * @return Placeholder value + */ + @Nullable public Placeholder getPlaceholder(@NotNull final String key) { + return this.placeholders.get( + Preconditions.checkNotNull(key, "Key may not be null").toLowerCase(Locale.ENGLISH)); + } + + /** + * Get the placeholder value evaluated for a player, and catch and deal with any problems + * occurring while doing so + * + * @param key Placeholder key + * @param player Player to evaluate for + * @return Replacement value + */ + @NotNull public String getPlaceholderValue(@NotNull final String key, + @NotNull final PlotPlayer player) { + final Placeholder placeholder = getPlaceholder(key); + if (placeholder == null) { + return ""; + } + String placeholderValue = ""; + try { + placeholderValue = placeholder.getValue(player); + // If a placeholder for some reason decides to be disobedient, we catch it here + if (placeholderValue == null) { + new RuntimeException(String + .format("Placeholder '%s' returned null for player '%s'", placeholder.getKey(), + player.getName())).printStackTrace(); + } + } catch (final Exception exception) { + new RuntimeException(String + .format("Placeholder '%s' failed to evalulate for player '%s'", + placeholder.getKey(), player.getName()), exception).printStackTrace(); + } + return placeholderValue; + } + + /** + * Get all placeholders + * + * @return Unmodifiable collection of placeholders + */ + @NotNull public Collection getPlaceholders() { + return Collections.unmodifiableCollection(this.placeholders.values()); + } + + + /** + * Event called when a new {@link Placeholder} has been added + */ + @RequiredArgsConstructor + @Getter + public static class PlaceholderAddedEvent { + + private final Placeholder placeholder; + + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java new file mode 100644 index 000000000..40b963d06 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -0,0 +1,75 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.util.placeholders; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; +import org.jetbrains.annotations.NotNull; + +public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { + + private final PlotFlag flag; + private final boolean local; + + public PlotFlagPlaceholder(@NotNull final PlotFlag flag, final boolean local) { + super(String.format("currentplot_%sflag_%s", local ? "local": "", flag.getName())); + this.flag = flag; + this.local = local; + } + + @Override @NotNull public String getValue(@NotNull final PlotPlayer player, @NotNull final Plot plot) { + return this.getFlagValue(plot, this.flag.getName(), !this.local); + } + + /** + * Return the flag value from its name on the current plot. + * If the flag doesn't exist it returns an empty string. + * If the flag exists but it is not set on current plot and the parameter inherit is set to true, + * it returns the default value. + * + * @param plot Current plot where the player is + * @param flagName Name of flag to get from current plot + * @param inherit Define if it returns only the flag set on currentplot or also inherited flag + * @return The value of flag serialized in string + */ + private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { + if (flagName.isEmpty()) + return ""; + final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); + if (flag == null) + return ""; + + if (inherit) { + return plot.getFlag(flag).toString(); + } else { + final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); + return (plotFlag != null) ? plotFlag.getValue().toString() : ""; + } + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java new file mode 100644 index 000000000..92285238d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java @@ -0,0 +1,59 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.util.placeholders; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import org.jetbrains.annotations.NotNull; + +/** + * A {@link Placeholder placeholder} that requires a {@link com.plotsquared.core.plot.Plot plot} + */ +public abstract class PlotSpecificPlaceholder extends Placeholder { + + public PlotSpecificPlaceholder(@NotNull final String key) { + super(key); + } + + @Override @NotNull public final String getValue(@NotNull final PlotPlayer player) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + return ""; + } + return this.getValue(player, plot); + } + + /** + * Get the value of the placeholder for the {@link PlotPlayer player} in a specific {@link Plot plot} + * + * @param player Player that the placeholder is evaluated for + * @param plot Plot that the player is in + * @return Placeholder value, or {@code ""} if the placeholder does not apply + */ + @NotNull public abstract String getValue(@NotNull final PlotPlayer player, + @NotNull final Plot plot); + +} From 1908588fcbd4c35ad16b1f17f6870a0e34db0cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 13:17:21 +0200 Subject: [PATCH 05/21] Change expansion author and version --- .../com/plotsquared/bukkit/placeholder/Placeholders.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java index 783d80257..0c3b8bfad 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -45,7 +45,7 @@ public class Placeholders extends PlaceholderExpansion { } @Override public String getAuthor() { - return "NotMyFault"; + return "IntellectualSites"; } @Override public String getIdentifier() { @@ -53,11 +53,11 @@ public class Placeholders extends PlaceholderExpansion { } @Override public String getVersion() { - return "2.5"; + return "3"; } @Override public String onPlaceholderRequest(Player p, String identifier) { - final PlotPlayer pl = PlotSquared.imp().getPlayerManager().getPlayerIfExists(p.getUniqueId()); + final PlotPlayer pl = PlotSquared.imp().getPlayerManager().getPlayerIfExists(p.getUniqueId()); if (pl == null) { return ""; From 2e4c43c251c3011ae13bf0670850cae1670ca41d Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 14 Jul 2020 19:10:53 +0100 Subject: [PATCH 06/21] Add config option to disable placing top wall blocks --- .../core/generator/ClassicPlotManager.java | 67 +++++++++++-------- .../core/generator/ClassicPlotWorld.java | 4 ++ .../plotsquared/core/generator/HybridGen.java | 12 ++-- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotManager.java b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotManager.java index 46333a84c..be60498bd 100644 --- a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotManager.java @@ -84,8 +84,8 @@ public class ClassicPlotManager extends SquarePlotManager { @Override public boolean unClaimPlot(Plot plot, Runnable whenDone) { setWallFilling(plot.getId(), classicPlotWorld.WALL_FILLING.toPattern()); - if (!classicPlotWorld.WALL_BLOCK.isAir() || !classicPlotWorld.WALL_BLOCK - .equals(classicPlotWorld.CLAIMED_WALL_BLOCK)) { + if (classicPlotWorld.PLACE_TOP_BLOCK && (!classicPlotWorld.WALL_BLOCK.isAir() + || !classicPlotWorld.WALL_BLOCK.equals(classicPlotWorld.CLAIMED_WALL_BLOCK))) { setWall(plot.getId(), classicPlotWorld.WALL_BLOCK.toPattern()); } return GlobalBlockQueue.IMP.addEmptyTask(whenDone); @@ -324,19 +324,23 @@ public class ClassicPlotManager extends SquarePlotManager { queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx, 1, sz + 1), new Location(classicPlotWorld.getWorldName(), sx, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING.toPattern()); - queue.setCuboid( - new Location(classicPlotWorld.getWorldName(), sx, classicPlotWorld.WALL_HEIGHT + 1, - sz + 1), - new Location(classicPlotWorld.getWorldName(), sx, classicPlotWorld.WALL_HEIGHT + 1, - ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); + if (classicPlotWorld.PLACE_TOP_BLOCK) { + queue.setCuboid( + new Location(classicPlotWorld.getWorldName(), sx, classicPlotWorld.WALL_HEIGHT + 1, + sz + 1), + new Location(classicPlotWorld.getWorldName(), sx, classicPlotWorld.WALL_HEIGHT + 1, + ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); + } queue.setCuboid(new Location(classicPlotWorld.getWorldName(), ex, 1, sz + 1), new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING.toPattern()); - queue.setCuboid( - new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.WALL_HEIGHT + 1, - sz + 1), - new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.WALL_HEIGHT + 1, - ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); + if (classicPlotWorld.PLACE_TOP_BLOCK) { + queue.setCuboid( + new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.WALL_HEIGHT + 1, + sz + 1), + new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.WALL_HEIGHT + 1, + ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); + } queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, 1, sz + 1), new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK.toPattern()); @@ -362,19 +366,21 @@ public class ClassicPlotManager extends SquarePlotManager { queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, 1, sz), new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.WALL_HEIGHT, sz), classicPlotWorld.WALL_FILLING.toPattern()); - queue.setCuboid( - new Location(classicPlotWorld.getWorldName(), sx + 1, classicPlotWorld.WALL_HEIGHT + 1, - sz), - new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.WALL_HEIGHT + 1, - sz), classicPlotWorld.WALL_BLOCK.toPattern()); + if (classicPlotWorld.PLACE_TOP_BLOCK) { + queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, + classicPlotWorld.WALL_HEIGHT + 1, sz), + new Location(classicPlotWorld.getWorldName(), ex - 1, + classicPlotWorld.WALL_HEIGHT + 1, sz), classicPlotWorld.WALL_BLOCK.toPattern()); + } queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, 1, ez), new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.WALL_HEIGHT, ez), classicPlotWorld.WALL_FILLING.toPattern()); - queue.setCuboid( - new Location(classicPlotWorld.getWorldName(), sx + 1, classicPlotWorld.WALL_HEIGHT + 1, - ez), - new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.WALL_HEIGHT + 1, - ez), classicPlotWorld.WALL_BLOCK.toPattern()); + if (classicPlotWorld.PLACE_TOP_BLOCK) { + queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, + classicPlotWorld.WALL_HEIGHT + 1, ez), + new Location(classicPlotWorld.getWorldName(), ex - 1, + classicPlotWorld.WALL_HEIGHT + 1, ez), classicPlotWorld.WALL_BLOCK.toPattern()); + } queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx + 1, 1, sz + 1), new Location(classicPlotWorld.getWorldName(), ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK.toPattern()); @@ -456,10 +462,10 @@ public class ClassicPlotManager extends SquarePlotManager { int ez = sz + classicPlotWorld.ROAD_WIDTH - 1; LocalBlockQueue queue = classicPlotWorld.getQueue(false); queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx, - Math.min(classicPlotWorld.PLOT_HEIGHT, classicPlotWorld.ROAD_HEIGHT) + 1, sz), - new Location(classicPlotWorld.getWorldName(), ex, - classicPlotWorld.getPlotManager().getWorldHeight(), ez), - BlockTypes.AIR.getDefaultState()); + Math.min(classicPlotWorld.PLOT_HEIGHT, classicPlotWorld.ROAD_HEIGHT) + 1, sz), + new Location(classicPlotWorld.getWorldName(), ex, + classicPlotWorld.getPlotManager().getWorldHeight(), ez), + BlockTypes.AIR.getDefaultState()); queue.setCuboid(new Location(classicPlotWorld.getWorldName(), sx, 1, sz), new Location(classicPlotWorld.getWorldName(), ex, classicPlotWorld.PLOT_HEIGHT - 1, ez), classicPlotWorld.MAIN_BLOCK.toPattern()); @@ -477,7 +483,8 @@ public class ClassicPlotManager extends SquarePlotManager { */ @Override public boolean finishPlotMerge(List plotIds) { final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + if (classicPlotWorld.PLACE_TOP_BLOCK && (!claim.isAir() || !claim + .equals(classicPlotWorld.WALL_BLOCK))) { for (PlotId plotId : plotIds) { setWall(plotId, claim.toPattern()); } @@ -493,7 +500,8 @@ public class ClassicPlotManager extends SquarePlotManager { @Override public boolean finishPlotUnlink(List plotIds) { final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + if (classicPlotWorld.PLACE_TOP_BLOCK && (!claim.isAir() || !claim + .equals(classicPlotWorld.WALL_BLOCK))) { for (PlotId id : plotIds) { setWall(id, claim.toPattern()); } @@ -511,7 +519,8 @@ public class ClassicPlotManager extends SquarePlotManager { @Override public boolean claimPlot(Plot plot) { final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + if (classicPlotWorld.PLACE_TOP_BLOCK && (!claim.isAir() || !claim + .equals(classicPlotWorld.WALL_BLOCK))) { return setWall(plot.getId(), claim.toPattern()); } return true; diff --git a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java index 13b2fb352..8a0e41c02 100644 --- a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java +++ b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java @@ -52,6 +52,7 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { public BlockBucket ROAD_BLOCK = new BlockBucket(BlockTypes.QUARTZ_BLOCK); // BlockUtil.get((short) 155, (byte) 0); public boolean PLOT_BEDROCK = true; + public boolean PLACE_TOP_BLOCK = true; public ClassicPlotWorld(String worldName, String id, @NotNull IndependentPlotGenerator generator, PlotId min, PlotId max) { @@ -79,6 +80,8 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { ConfigurationUtil.BLOCK_BUCKET), new ConfigurationNode("wall.block_claimed", this.CLAIMED_WALL_BLOCK, "Wall block (claimed)", ConfigurationUtil.BLOCK_BUCKET), + new ConfigurationNode("wall.place_top_block", this.PLACE_TOP_BLOCK, + "Place or not the top block", ConfigurationUtil.BOOLEAN), new ConfigurationNode("road.width", this.ROAD_WIDTH, "Road width", ConfigurationUtil.INTEGER), new ConfigurationNode("road.height", this.ROAD_HEIGHT, "Road height", @@ -109,5 +112,6 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { this.WALL_FILLING = new BlockBucket(config.getString("wall.filling")); this.WALL_HEIGHT = Math.min(254, config.getInt("wall.height")); this.CLAIMED_WALL_BLOCK = new BlockBucket(config.getString("wall.block_claimed")); + this.PLACE_TOP_BLOCK = config.getBoolean("wall.place_top_block"); } } diff --git a/Core/src/main/java/com/plotsquared/core/generator/HybridGen.java b/Core/src/main/java/com/plotsquared/core/generator/HybridGen.java index a4f955682..c45931d81 100644 --- a/Core/src/main/java/com/plotsquared/core/generator/HybridGen.java +++ b/Core/src/main/java/com/plotsquared/core/generator/HybridGen.java @@ -172,8 +172,10 @@ public class HybridGen extends IndependentPlotGenerator { result.setBlock(x, y, z, hybridPlotWorld.WALL_FILLING.toPattern()); } if (!hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, - hybridPlotWorld.WALL_BLOCK.toPattern()); + if (hybridPlotWorld.PLACE_TOP_BLOCK) { + result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, + hybridPlotWorld.WALL_BLOCK.toPattern()); + } } else { placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true); @@ -197,8 +199,10 @@ public class HybridGen extends IndependentPlotGenerator { result.setBlock(x, y, z, hybridPlotWorld.WALL_FILLING.toPattern()); } if (!hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, - hybridPlotWorld.WALL_BLOCK.toPattern()); + if (hybridPlotWorld.PLACE_TOP_BLOCK) { + result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, + hybridPlotWorld.WALL_BLOCK.toPattern()); + } } else { placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true); From 30327d2fa3a288c19f97724a75f73f15d7b2331c Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 14 Jul 2020 19:11:46 +0100 Subject: [PATCH 07/21] update bukkit pom --- Bukkit/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index cde998b9a..0e7adcda4 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -45,7 +45,7 @@ io.papermc paperlib - 1.0.2 + 1.0.4 compile From 34655b8d4114ce6190d2974758becaebcc41308b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:41:08 +0200 Subject: [PATCH 08/21] Add (untested) MVdWPlaceholderAPI support --- Bukkit/build.gradle | 2 + .../com/plotsquared/bukkit/BukkitMain.java | 10 ++- .../bukkit/placeholder/MVdWPlaceholders.java | 76 +++++++++++++++++++ ...laceholders.java => PAPIPlaceholders.java} | 4 +- Bukkit/src/main/resources/plugin.yml | 2 +- 5 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java rename Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/{Placeholders.java => PAPIPlaceholders.java} (97%) diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index e206fbe5b..fa23da2ff 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -14,6 +14,7 @@ repositories { maven { url = "https://ci.ender.zone/plugin/repository/everything/" } maven { url = "https://mvn.intellectualsites.com/content/repositories/snapshots" } maven { url = "https://repo.wea-ondara.net/repository/public/" } + maven { url = "http://repo.mvdw-software.be/content/groups/public/" } mavenLocal() } @@ -41,6 +42,7 @@ dependencies { implementation("net.alpenblock:BungeePerms:4.0-dev-106") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile('com.sk89q:squirrelid:1.0.0-SNAPSHOT'){ transitive = false } + compile('be.maximvdw:MVdWPlaceholderAPI:2.1.1-SNAPSHOT'){ transitive = false } } sourceCompatibility = 1.8 diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index c03cdd694..66bc5ed3c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -36,8 +36,9 @@ import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.managers.BukkitWorldManager; import com.plotsquared.bukkit.managers.HyperverseWorldManager; import com.plotsquared.bukkit.managers.MultiverseWorldManager; +import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; import com.plotsquared.bukkit.placeholder.PlaceholderFormatter; -import com.plotsquared.bukkit.placeholder.Placeholders; +import com.plotsquared.bukkit.placeholder.PAPIPlaceholders; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.queue.BukkitLocalQueue; import com.plotsquared.bukkit.schematic.BukkitSchematicHandler; @@ -363,7 +364,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< } if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { - new Placeholders().register(); + new PAPIPlaceholders().register(); if (Settings.Enabled_Components.EXTERNAL_PLACEHOLDERS) { ChatFormatter.formatters.add(new PlaceholderFormatter()); } @@ -373,6 +374,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< .debug(Captions.PREFIX + "&6PlaceholderAPI is not in use. Hook deactivated."); } + if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { + new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); + PlotSquared.log(Captions.PREFIX + "&cPlotSquared hooked into MVdWPlaceholderAPI"); + } + this.startMetrics(); if (Settings.Enabled_Components.WORLDS) { TaskManager.IMP.taskRepeat(this::unload, 20); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java new file mode 100644 index 000000000..5bd835de0 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java @@ -0,0 +1,76 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.placeholder; + +import be.maximvdw.placeholderapi.PlaceholderAPI; +import com.google.common.eventbus.Subscribe; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.util.placeholders.Placeholder; +import com.plotsquared.core.util.placeholders.PlaceholderRegistry; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +/** + * Placeholder support for MVdWPlaceholderAPI + */ +public class MVdWPlaceholders { + + private final Plugin plugin; + private final PlaceholderRegistry registry; + + public MVdWPlaceholders(@NotNull final Plugin plugin, + @NotNull final PlaceholderRegistry registry) { + this.plugin = plugin; + this.registry = registry; + for (final Placeholder placeholder : registry.getPlaceholders()) { + this.addPlaceholder(placeholder); + } + PlotSquared.get().getEventDispatcher().registerListener(this); + } + + @Subscribe public void onNewPlaceholder(@NotNull final + PlaceholderRegistry.PlaceholderAddedEvent event) { + this.addPlaceholder(event.getPlaceholder()); + } + + private void addPlaceholder(@NotNull final Placeholder placeholder) { + PlaceholderAPI.registerPlaceholder(plugin, String.format("plotsquared_%s", placeholder.getKey()), + placeholderReplaceEvent -> { + if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) { + return ""; + } + final PlotPlayer player = BukkitUtil.getPlayer(placeholderReplaceEvent.getPlayer()); + if (player == null) { + return ""; + } + return registry.getPlaceholderValue(placeholderReplaceEvent.getPlaceholder(), player); + }); + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java similarity index 97% rename from Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java rename to Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java index 0c3b8bfad..52d30d5ed 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java @@ -31,9 +31,9 @@ import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.entity.Player; -public class Placeholders extends PlaceholderExpansion { +public class PAPIPlaceholders extends PlaceholderExpansion { - public Placeholders() { + public PAPIPlaceholders() { } @Override public boolean persist() { diff --git a/Bukkit/src/main/resources/plugin.yml b/Bukkit/src/main/resources/plugin.yml index 41967220c..2dd7edbac 100644 --- a/Bukkit/src/main/resources/plugin.yml +++ b/Bukkit/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ load: STARTUP description: "Easy, yet powerful Plot World generation and management." authors: [Citymonstret, Empire92, MattBDev, dordsor21, NotMyFault, SirYwell] website: https://www.spigotmc.org/resources/77506/ -softdepend: [Vault, PlaceholderAPI, Essentials, LuckPerms, BungeePerms] +softdepend: [Vault, PlaceholderAPI, Essentials, LuckPerms, BungeePerms, MVdWPlaceholderAPI] loadbefore: [MultiWorld, Multiverse-Core] depend: [WorldEdit] database: false From 64f5580eddbdcf1e0e10e06bed8d373f4cc43fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:46:52 +0200 Subject: [PATCH 09/21] Fix code styling issue --- .../core/util/placeholders/PlotFlagPlaceholder.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java index 40b963d06..51d075d26 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -57,13 +57,14 @@ public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { * @param inherit Define if it returns only the flag set on currentplot or also inherited flag * @return The value of flag serialized in string */ - private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { - if (flagName.isEmpty()) + @NotNull private String getFlagValue(@NotNull final Plot plot, @NotNull final String flagName, final boolean inherit) { + if (flagName.isEmpty()) { return ""; + } final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); - if (flag == null) + if (flag == null) { return ""; - + } if (inherit) { return plot.getFlag(flag).toString(); } else { From 5d2d4ac12b1a243890373da8a8386d805e962f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:47:15 +0200 Subject: [PATCH 10/21] fix language issue --- .../plotsquared/core/util/placeholders/PlotFlagPlaceholder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java index 51d075d26..4a3d1876a 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -54,7 +54,7 @@ public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { * * @param plot Current plot where the player is * @param flagName Name of flag to get from current plot - * @param inherit Define if it returns only the flag set on currentplot or also inherited flag + * @param inherit Define if it returns only the flag set on the current plot or also inherited flags * @return The value of flag serialized in string */ @NotNull private String getFlagValue(@NotNull final Plot plot, @NotNull final String flagName, final boolean inherit) { From a52249513e17dc65e31d9493e3afac735800ae23 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Wed, 15 Jul 2020 12:05:42 +0200 Subject: [PATCH 11/21] Update MVdW version --- Bukkit/build.gradle | 2 +- Bukkit/pom.xml | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index fa23da2ff..61842d3ce 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -42,7 +42,7 @@ dependencies { implementation("net.alpenblock:BungeePerms:4.0-dev-106") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile('com.sk89q:squirrelid:1.0.0-SNAPSHOT'){ transitive = false } - compile('be.maximvdw:MVdWPlaceholderAPI:2.1.1-SNAPSHOT'){ transitive = false } + compile('be.maximvdw:MVdWPlaceholderAPI:3.1.1-SNAPSHOT'){ transitive = false } } sourceCompatibility = 1.8 diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index cde998b9a..c70676567 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -45,7 +45,7 @@ io.papermc paperlib - 1.0.2 + 1.0.4 compile @@ -84,6 +84,18 @@ + + be.maximvdw + MVdWPlaceholderAPI + 3.1.1-SNAPSHOT + compile + + + * + * + + + com.sk89q.worldedit worldedit-core From e8c155763be355172f28b54c5ff3545d5182406f Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Wed, 15 Jul 2020 12:44:07 +0200 Subject: [PATCH 12/21] Register placeholders --- Bukkit/pom.xml | 2 +- .../src/main/java/com/plotsquared/bukkit/BukkitMain.java | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index c70676567..07e1a9ca7 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -87,7 +87,7 @@ be.maximvdw MVdWPlaceholderAPI - 3.1.1-SNAPSHOT + 2.1.1-SNAPSHOT compile diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 66bc5ed3c..5ee38078c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -369,14 +369,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< ChatFormatter.formatters.add(new PlaceholderFormatter()); } PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into PlaceholderAPI"); - } else { - PlotSquared - .debug(Captions.PREFIX + "&6PlaceholderAPI is not in use. Hook deactivated."); } - if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { + if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); - PlotSquared.log(Captions.PREFIX + "&cPlotSquared hooked into MVdWPlaceholderAPI"); + PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); } this.startMetrics(); From 4996d8bcd1cfa1982513d626c3ba2761d873fa65 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Wed, 15 Jul 2020 14:05:15 +0200 Subject: [PATCH 13/21] Remove left over debugs --- .../com/plotsquared/bukkit/listener/EntityEventListener.java | 2 -- .../com/plotsquared/bukkit/listener/PlayerEventListener.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java index 0363d42cf..5bfb26a56 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java @@ -305,13 +305,11 @@ public class EntityEventListener implements Listener { if (area != null) { Plot plot = area.getOwnedPlot(location); if (plot != null && plot.getFlag(MobPlaceFlag.class)) { - System.out.println("bb"); return; } if (plot != null) { plot.debug(e.getType() + " could not change block because mob-place = false"); } - System.out.println("aa"); event.setCancelled(true); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index e2bb3cb36..6d11cd75c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -933,7 +933,6 @@ public class PlayerEventListener extends PlotListener implements Listener { case PHYSICAL: { eventType = PlayerBlockEventType.TRIGGER_PHYSICAL; blocktype1 = BukkitAdapter.asBlockType(block.getType()); - System.out.println("cc"); break; } //todo rearrange the right click code. it is all over the place. @@ -1044,7 +1043,6 @@ public class PlayerEventListener extends PlotListener implements Listener { } if (!PlotSquared.get().getEventDispatcher() .checkPlayerBlockEvent(pp, eventType, location, blocktype1, true)) { - System.out.println("dd"); event.setCancelled(true); event.setUseInteractedBlock(Event.Result.DENY); } From 32b28a4ca5679607580fb2e3f2677f1464d77bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 15 Jul 2020 22:42:58 +0200 Subject: [PATCH 14/21] Turn all UUID messages into debug messages --- .../com/plotsquared/core/uuid/UUIDPipeline.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java index 997cdfdc1..9ca6e783a 100644 --- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java +++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java @@ -164,8 +164,9 @@ public class UUIDPipeline { } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } catch (TimeoutException ignored) { - PlotSquared.log(Captions.PREFIX + " (UUID) Request for " + username + " timed out"); - // This is completely valid, we just don't care anymore + if (Settings.DEBUG) { + PlotSquared.debug(Captions.PREFIX + " (UUID) Request for " + username + " timed out"); + } } return null; } @@ -187,8 +188,9 @@ public class UUIDPipeline { } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } catch (TimeoutException ignored) { - PlotSquared.log(Captions.PREFIX + " (UUID) Request for " + uuid + " timed out"); - // This is completely valid, we just don't care anymore + if (Settings.DEBUG) { + PlotSquared.debug(Captions.PREFIX + " (UUID) Request for " + uuid + " timed out"); + } } return null; } @@ -321,7 +323,7 @@ public class UUIDPipeline { this.consume(mappings); return mappings; } else if (Settings.DEBUG) { - PlotSquared.log("Failed to find all usernames"); + PlotSquared.debug("Failed to find all usernames"); } if (Settings.UUID.UNKNOWN_AS_DEFAULT) { @@ -384,7 +386,7 @@ public class UUIDPipeline { this.consume(mappings); return mappings; } else if (Settings.DEBUG) { - PlotSquared.log("Failed to find all UUIDs"); + PlotSquared.debug("Failed to find all UUIDs"); } throw new ServiceError("End of pipeline"); From 2d9cf8b7597bb7b8684fce35263813b7194874de Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:04:17 +0200 Subject: [PATCH 15/21] Get placeholders working --- .../com/plotsquared/bukkit/BukkitMain.java | 12 ++++---- .../bukkit/listener/ServerListener.java | 29 +++++++++++++++++++ .../bukkit/placeholder/MVdWPlaceholders.java | 6 ++-- .../java/com/plotsquared/core/IPlotMain.java | 5 ++++ .../com/plotsquared/core/PlotSquared.java | 1 + .../placeholders/PlaceholderRegistry.java | 2 +- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 5ee38078c..fc3689dcd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -31,12 +31,12 @@ import com.plotsquared.bukkit.listener.ChunkListener; import com.plotsquared.bukkit.listener.EntitySpawnListener; import com.plotsquared.bukkit.listener.PaperListener; import com.plotsquared.bukkit.listener.PlayerEvents; +import com.plotsquared.bukkit.listener.ServerListener; import com.plotsquared.bukkit.listener.SingleWorldListener; import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.managers.BukkitWorldManager; import com.plotsquared.bukkit.managers.HyperverseWorldManager; import com.plotsquared.bukkit.managers.MultiverseWorldManager; -import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; import com.plotsquared.bukkit.placeholder.PlaceholderFormatter; import com.plotsquared.bukkit.placeholder.PAPIPlaceholders; import com.plotsquared.bukkit.player.BukkitPlayerManager; @@ -371,11 +371,6 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into PlaceholderAPI"); } - if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { - new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); - PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); - } - this.startMetrics(); if (Settings.Enabled_Components.WORLDS) { TaskManager.IMP.taskRepeat(this::unload, 20); @@ -1052,6 +1047,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< getServer().getPluginManager().registerEvents(new WorldEvents(), this); } + @Override + public void registerServerEvents() { + getServer().getPluginManager().registerEvents(new ServerListener(this), this); + } + @NotNull @Override public IndependentPlotGenerator getDefaultGenerator() { return new HybridGen(); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java new file mode 100644 index 000000000..c6f8b072e --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -0,0 +1,29 @@ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.BukkitMain; +import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerLoadEvent; + +public class ServerListener implements Listener { + + private final BukkitMain plugin; + + public ServerListener(BukkitMain plugin) { + this.plugin = plugin; + } + + @EventHandler public void onServerLoad(ServerLoadEvent event) { + if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { + new MVdWPlaceholders(this.plugin, PlotSquared.get().getPlaceholderRegistry()); + PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); + } else { + System.out.println("Nope"); + } + + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java index 5bd835de0..599a1122b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java @@ -41,6 +41,7 @@ import org.jetbrains.annotations.NotNull; */ public class MVdWPlaceholders { + private static final String PREFIX = "plotsquared_"; private final Plugin plugin; private final PlaceholderRegistry registry; @@ -60,7 +61,7 @@ public class MVdWPlaceholders { } private void addPlaceholder(@NotNull final Placeholder placeholder) { - PlaceholderAPI.registerPlaceholder(plugin, String.format("plotsquared_%s", placeholder.getKey()), + PlaceholderAPI.registerPlaceholder(plugin, PREFIX + String.format("%s", placeholder.getKey()), placeholderReplaceEvent -> { if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) { return ""; @@ -69,7 +70,8 @@ public class MVdWPlaceholders { if (player == null) { return ""; } - return registry.getPlaceholderValue(placeholderReplaceEvent.getPlaceholder(), player); + String key = placeholderReplaceEvent.getPlaceholder().substring(PREFIX.length()); + return registry.getPlaceholderValue(key, player); }); } diff --git a/Core/src/main/java/com/plotsquared/core/IPlotMain.java b/Core/src/main/java/com/plotsquared/core/IPlotMain.java index a38045acb..5dcc6d5a1 100644 --- a/Core/src/main/java/com/plotsquared/core/IPlotMain.java +++ b/Core/src/main/java/com/plotsquared/core/IPlotMain.java @@ -270,6 +270,11 @@ public interface IPlotMain

extends ILogger { */ void registerWorldEvents(); + /** + * Register events related to the server + */ + void registerServerEvents(); + /** * Usually HybridGen * diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 3451b0a1c..f1287dbcb 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -262,6 +262,7 @@ public class PlotSquared { this.IMP.registerPlayerEvents(); } // Required + this.IMP.registerServerEvents(); this.IMP.registerWorldEvents(); if (Settings.Enabled_Components.CHUNK_PROCESSOR) { this.IMP.registerChunkProcessor(); diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java index 243ca0909..d18859b2e 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java @@ -159,7 +159,7 @@ public final class PlaceholderRegistry { final Placeholder previous = this.placeholders .put(placeholder.getKey().toLowerCase(Locale.ENGLISH), Preconditions.checkNotNull(placeholder, "Placeholder may not be null")); - if (previous != null) { + if (previous == null) { this.eventDispatcher.callGenericEvent(new PlaceholderAddedEvent(placeholder)); } } From 10ab28c1ec3a44b9b947349c39e3feaec338b359 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:08:00 +0200 Subject: [PATCH 16/21] Add missing license headers --- .../bukkit/listener/ServerListener.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java index c6f8b072e..245393ba0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.bukkit.listener; import com.plotsquared.bukkit.BukkitMain; From 97a356c4a072ee1b96ed76162b218f8309746962 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:13:02 +0200 Subject: [PATCH 17/21] Very attentive reviewers, can recommend --- .../java/com/plotsquared/bukkit/listener/ServerListener.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java index 245393ba0..c74db87d4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -46,9 +46,6 @@ public class ServerListener implements Listener { if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { new MVdWPlaceholders(this.plugin, PlotSquared.get().getPlaceholderRegistry()); PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); - } else { - System.out.println("Nope"); } - } } From ff3a94933aada14eb7b495e5c58ad4011a4c7e43 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Thu, 16 Jul 2020 18:43:52 +0200 Subject: [PATCH 18/21] Bump version --- Bukkit/pom.xml | 4 ++-- build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index 07e1a9ca7..bba553386 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -21,7 +21,7 @@ com.plotsquared PlotSquared-Core - 5.12.5 + 5.13.0 compile @@ -87,7 +87,7 @@ be.maximvdw MVdWPlaceholderAPI - 2.1.1-SNAPSHOT + 3.1.1-SNAPSHOT compile diff --git a/build.gradle b/build.gradle index a0bae9c0c..bfd4ef86f 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ ext { git = Grgit.open(dir: new File(rootDir.toString() + "/.git")) } -def ver = "5.12.5" +def ver = "5.13.0" def versuffix = "" ext { if (project.hasProperty("versionsuffix")) { From 57435fdf349a9af989b13f9ddd70b0466370f25a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 16 Jul 2020 23:24:11 +0100 Subject: [PATCH 19/21] verbosely ensure items cannot be duplicated when keeping inventory on death Seemingly there's duplication issues on some servers (and apparently even when only PlotSquared is installed). Likely a Spigot issue, but there's nothing to be done for Spigot on 1.13/14/15 --- .../com/plotsquared/bukkit/listener/PlayerEventListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index 6d11cd75c..c4d7c6d90 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -1509,6 +1509,7 @@ public class PlayerEventListener extends PlotListener implements Listener { if (plot.getFlag(KeepInventoryFlag.class)) { plot.debug(event.getEntity().getName() + " kept their inventory because of keep-inventory = true"); + event.getDrops().clear(); event.setKeepInventory(true); } } From 87a8ff742fe5c542fac80b8ce20c93563ec70bd2 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Sun, 19 Jul 2020 14:16:13 +0200 Subject: [PATCH 20/21] 5.13.1 --- Bukkit/pom.xml | 2 +- build.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index bba553386..26f8a6607 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -21,7 +21,7 @@ com.plotsquared PlotSquared-Core - 5.13.0 + 5.13.1 compile diff --git a/build.gradle b/build.gradle index bfd4ef86f..40639372c 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ ext { git = Grgit.open(dir: new File(rootDir.toString() + "/.git")) } -def ver = "5.13.0" +def ver = "5.13.1" def versuffix = "" ext { if (project.hasProperty("versionsuffix")) { From 73e3572c726849a884842b2f83b891b9a5b14a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 21 Jul 2020 13:11:28 +0200 Subject: [PATCH 21/21] Fix the redstone flag on roads --- .../com/plotsquared/bukkit/listener/BlockEventListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java index 899a03a04..32408f41e 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/BlockEventListener.java @@ -133,7 +133,7 @@ public class BlockEventListener implements Listener { } Plot plot = location.getOwnedPlot(); if (plot == null) { - if (area.isRoadFlags() && area.getRoadFlag(RedstoneFlag.class)) { + if (area.isRoadFlags() && !area.getRoadFlag(RedstoneFlag.class)) { event.setNewCurrent(0); } return;