From da2cc0f052c32092f8fb360964c9a42fa038dbdb Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 24 Apr 2013 15:35:46 +0200 Subject: [PATCH] Whatever, lets just move the last listener stuff over --- src/com/massivecraft/factions/Factions.java | 8 - .../listeners/FactionsListenerMain.java | 157 ++++++++++++++---- .../listeners/TodoFactionsPlayerListener.java | 112 ------------- 3 files changed, 125 insertions(+), 152 deletions(-) delete mode 100644 src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 12c1eba5..1b695e26 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -30,7 +30,6 @@ import com.massivecraft.factions.listeners.FactionsListenerChat; import com.massivecraft.factions.listeners.FactionsListenerEcon; import com.massivecraft.factions.listeners.FactionsListenerExploit; import com.massivecraft.factions.listeners.FactionsListenerMain; -import com.massivecraft.factions.listeners.TodoFactionsPlayerListener; import com.massivecraft.factions.mixin.PowerMixin; import com.massivecraft.factions.mixin.PowerMixinDefault; import com.massivecraft.factions.task.TaskPlayerDataRemove; @@ -63,9 +62,6 @@ public class Factions extends MPlugin private CmdFactions outerCmdFactions; public CmdFactions getOuterCmdFactions() { return this.outerCmdFactions; } - // Listeners - public TodoFactionsPlayerListener playerListener; - // Aspects private Aspect aspect; public Aspect getAspect() { return this.aspect; } @@ -127,10 +123,6 @@ public class Factions extends MPlugin // Right now only a few situations are handled through this listener. FactionsListenerEcon.get().setup(); - // TODO: Get rid of this one - this.playerListener = new TodoFactionsPlayerListener(); - getServer().getPluginManager().registerEvents(this.playerListener, this); - // Schedule recurring non-tps-dependent tasks TaskPlayerPowerUpdate.get().schedule(this); TaskPlayerDataRemove.get().schedule(this); diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index 399812c3..23b02128 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -6,6 +6,7 @@ import java.util.Iterator; import java.util.List; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Enderman; import org.bukkit.entity.Entity; @@ -18,6 +19,7 @@ import org.bukkit.event.Cancellable; 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; @@ -40,7 +42,10 @@ import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause; +import org.bukkit.event.player.PlayerBucketEmptyEvent; +import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerMoveEvent; @@ -572,6 +577,51 @@ public class FactionsListenerMain implements Listener // ... stop the block alteration. event.setCancelled(true); } + + // -------------------------------------------- // + // FLAG: FIRE SPREAD + // -------------------------------------------- // + + public void blockFireSpread(Block block, Cancellable cancellable) + { + // If the faction at the block has firespread disabled ... + PS ps = PS.valueOf(block); + Faction faction = BoardColls.get().getFactionAt(ps); + if (faction.getFlag(FFlag.FIRESPREAD)) return; + + // then cancel the event. + cancellable.setCancelled(true); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockFireSpread(BlockIgniteEvent event) + { + // If fire is spreading ... + if (event.getCause() != IgniteCause.SPREAD && event.getCause() != IgniteCause.LAVA) return; + + // ... consider blocking it. + blockFireSpread(event.getBlock(), event); + } + + // TODO: Is use of this event deprecated? + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockFireSpread(BlockSpreadEvent event) + { + // If fire is spreading ... + if (event.getNewState().getTypeId() != 51) return; + + // ... consider blocking it. + blockFireSpread(event.getBlock(), event); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void blockFireSpread(BlockBurnEvent event) + { + // If a block is burning ... + + // ... consider blocking it. + blockFireSpread(event.getBlock(), event); + } // -------------------------------------------- // // FLAG: BUILD @@ -704,48 +754,91 @@ public class FactionsListenerMain implements Listener } // -------------------------------------------- // - // FLAG: FIRE SPREAD + // ASSORTED BUILD AND INTERACT // -------------------------------------------- // - public void blockFireSpread(Block block, Cancellable cancellable) + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerInteract(PlayerInteractEvent event) { - // If the faction at the block has firespread disabled ... + // only need to check right-clicks and physical as of MC 1.4+; good performance boost + if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.PHYSICAL) return; + + Block block = event.getClickedBlock(); + Player player = event.getPlayer(); + + if (block == null) return; // clicked in air, apparently + + if ( ! canPlayerUseBlock(player, block, false)) + { + event.setCancelled(true); + return; + } + + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // only interested on right-clicks for below + + if ( ! playerCanUseItemHere(player, PS.valueOf(block), event.getMaterial(), false)) + { + event.setCancelled(true); + return; + } + } + + // TODO: Refactor ! justCheck -> to informIfNot + // TODO: Possibly incorporate pain build... + public static boolean playerCanUseItemHere(Player player, PS ps, Material material, boolean justCheck) + { + if (!Const.MATERIALS_EDIT_TOOLS.contains(material)) return true; + + String name = player.getName(); + if (MConf.get().playersWhoBypassAllProtection.contains(name)) return true; + + UPlayer uplayer = UPlayer.get(player); + if (uplayer.isUsingAdminMode()) return true; + + return FPerm.BUILD.has(uplayer, ps, !justCheck); + } + + public static boolean canPlayerUseBlock(Player player, Block block, boolean justCheck) + { + String name = player.getName(); + if (MConf.get().playersWhoBypassAllProtection.contains(name)) return true; + + UPlayer me = UPlayer.get(player); + if (me.isUsingAdminMode()) return true; + PS ps = PS.valueOf(block); - Faction faction = BoardColls.get().getFactionAt(ps); - if (faction.getFlag(FFlag.FIRESPREAD)) return; + Material material = block.getType(); - // then cancel the event. - cancellable.setCancelled(true); + if (Const.MATERIALS_EDIT_ON_INTERACT.contains(material) && ! FPerm.BUILD.has(me, ps, ! justCheck)) return false; + if (Const.MATERIALS_CONTAINER.contains(material) && ! FPerm.CONTAINER.has(me, ps, ! justCheck)) return false; + if (Const.MATERIALS_DOOR.contains(material) && ! FPerm.DOOR.has(me, ps, ! justCheck)) return false; + if (material == Material.STONE_BUTTON && ! FPerm.BUTTON.has(me, ps, ! justCheck)) return false; + if (material == Material.LEVER && ! FPerm.LEVER.has(me, ps, ! justCheck)) return false; + return true; + } + + // For some reason onPlayerInteract() sometimes misses bucket events depending on distance (something like 2-3 blocks away isn't detected), + // but these separate bucket events below always fire without fail + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) + { + Block block = event.getBlockClicked(); + Player player = event.getPlayer(); + + if (playerCanUseItemHere(player, PS.valueOf(block), event.getBucket(), false)) return; + + event.setCancelled(true); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void blockFireSpread(BlockIgniteEvent event) + public void onPlayerBucketFill(PlayerBucketFillEvent event) { - // If fire is spreading ... - if (event.getCause() != IgniteCause.SPREAD && event.getCause() != IgniteCause.LAVA) return; + Block block = event.getBlockClicked(); + Player player = event.getPlayer(); + + if (playerCanUseItemHere(player, PS.valueOf(block), event.getBucket(), false)) return; - // ... consider blocking it. - blockFireSpread(event.getBlock(), event); - } - - // TODO: Is use of this event deprecated? - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void blockFireSpread(BlockSpreadEvent event) - { - // If fire is spreading ... - if (event.getNewState().getTypeId() != 51) return; - - // ... consider blocking it. - blockFireSpread(event.getBlock(), event); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void blockFireSpread(BlockBurnEvent event) - { - // If a block is burning ... - - // ... consider blocking it. - blockFireSpread(event.getBlock(), event); + event.setCancelled(true); } } diff --git a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java deleted file mode 100644 index 89e62d67..00000000 --- a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.massivecraft.factions.listeners; - -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -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.player.PlayerBucketEmptyEvent; -import org.bukkit.event.player.PlayerBucketFillEvent; -import org.bukkit.event.player.PlayerInteractEvent; - -import com.massivecraft.factions.Const; -import com.massivecraft.factions.FPerm; -import com.massivecraft.factions.entity.UPlayer; -import com.massivecraft.factions.entity.MConf; -import com.massivecraft.mcore.ps.PS; - - -public class TodoFactionsPlayerListener implements Listener -{ - // -------------------------------------------- // - // ASSORTED BUILD AND INTERACT - // -------------------------------------------- // - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onPlayerInteract(PlayerInteractEvent event) - { - // only need to check right-clicks and physical as of MC 1.4+; good performance boost - if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.PHYSICAL) return; - - Block block = event.getClickedBlock(); - Player player = event.getPlayer(); - - if (block == null) return; // clicked in air, apparently - - if ( ! canPlayerUseBlock(player, block, false)) - { - event.setCancelled(true); - return; - } - - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // only interested on right-clicks for below - - if ( ! playerCanUseItemHere(player, PS.valueOf(block), event.getMaterial(), false)) - { - event.setCancelled(true); - return; - } - } - - // TODO: Refactor ! justCheck -> to informIfNot - // TODO: Possibly incorporate pain build... - public static boolean playerCanUseItemHere(Player player, PS ps, Material material, boolean justCheck) - { - if (!Const.MATERIALS_EDIT_TOOLS.contains(material)) return true; - - String name = player.getName(); - if (MConf.get().playersWhoBypassAllProtection.contains(name)) return true; - - UPlayer uplayer = UPlayer.get(player); - if (uplayer.isUsingAdminMode()) return true; - - return FPerm.BUILD.has(uplayer, ps, !justCheck); - } - - public static boolean canPlayerUseBlock(Player player, Block block, boolean justCheck) - { - String name = player.getName(); - if (MConf.get().playersWhoBypassAllProtection.contains(name)) return true; - - UPlayer me = UPlayer.get(player); - if (me.isUsingAdminMode()) return true; - - PS ps = PS.valueOf(block); - Material material = block.getType(); - - if (Const.MATERIALS_EDIT_ON_INTERACT.contains(material) && ! FPerm.BUILD.has(me, ps, ! justCheck)) return false; - if (Const.MATERIALS_CONTAINER.contains(material) && ! FPerm.CONTAINER.has(me, ps, ! justCheck)) return false; - if (Const.MATERIALS_DOOR.contains(material) && ! FPerm.DOOR.has(me, ps, ! justCheck)) return false; - if (material == Material.STONE_BUTTON && ! FPerm.BUTTON.has(me, ps, ! justCheck)) return false; - if (material == Material.LEVER && ! FPerm.LEVER.has(me, ps, ! justCheck)) return false; - return true; - } - - // For some reason onPlayerInteract() sometimes misses bucket events depending on distance (something like 2-3 blocks away isn't detected), - // but these separate bucket events below always fire without fail - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) - { - Block block = event.getBlockClicked(); - Player player = event.getPlayer(); - - if (playerCanUseItemHere(player, PS.valueOf(block), event.getBucket(), false)) return; - - event.setCancelled(true); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onPlayerBucketFill(PlayerBucketFillEvent event) - { - Block block = event.getBlockClicked(); - Player player = event.getPlayer(); - - if (playerCanUseItemHere(player, PS.valueOf(block), event.getBucket(), false)) return; - - event.setCancelled(true); - } - - -}