From a7fede9c138c162f25d9dcbc4b56f5d8765f74ea Mon Sep 17 00:00:00 2001 From: Brettflan Date: Tue, 8 Mar 2011 06:06:52 -0600 Subject: [PATCH] Now preventing placement/use of these items inside another faction's territory: redstone wire, signs, flint&steel, beds (not detected properly by Bukkit yet), buckets (empty, water, and lava), repeaters --- src/com/bukkit/mcteam/factions/Factions.java | 1 + .../listeners/FactionsPlayerListener.java | 49 +++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/Factions.java b/src/com/bukkit/mcteam/factions/Factions.java index 98597c63..ee20ee9a 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -37,6 +37,7 @@ public class Factions extends JavaPlugin { PluginManager pm = this.getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.playerListener, Event.Priority.Normal, this); + pm.registerEvent(Event.Type.PLAYER_ITEM, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_QUIT, this.playerListener, Event.Priority.Normal, this); diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 0dcff113..0efe2b66 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -3,9 +3,11 @@ package com.bukkit.mcteam.factions.listeners; import java.util.*; import java.util.logging.Logger; +import org.bukkit.block.Block; import org.bukkit.entity.*; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerItemEvent; import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerMoveEvent; @@ -21,7 +23,7 @@ public class FactionsPlayerListener extends PlayerListener{ public FactionsPlayerListener(Factions plugin) { this.plugin = plugin; } - + /** * If someone says something that starts with the factions base command * we handle that command. @@ -145,6 +147,47 @@ public class FactionsPlayerListener extends PlayerListener{ } } } - - + + @Override + public void onPlayerItem(PlayerItemEvent event) { + if (event.isCancelled()) + return; + + if (!this.playerCanUseItemHere(event.getPlayer(), event.getBlockClicked(), event.getItem().getTypeId())) + { + event.setCancelled(true); + return; + } + + } + + //currently checking placement/use of: redstone, sign, flint&steel, beds (not detected properly by Bukkit yet), buckets (empty, water, lava), repeater + private static Set badItems = new HashSet(Arrays.asList( + new Integer[] {331, 323, 259, 355, 325, 326, 327, 356} + )); + + public boolean playerCanUseItemHere(Player player, Block block, int itemId) { + + if (!badItems.contains(new Integer(itemId))) { + return true; // Item isn't one we're preventing. + } + + Coord coord = Coord.parseCoord(block); + Faction otherFaction = Board.get(player.getWorld()).getFactionAt(coord); + + if (otherFaction == null || otherFaction.id == 0) { + return true; // This is not faction territory. Use whatever you like here. + } + + Follower me = Follower.get(player); + Faction myFaction = me.getFaction(); + + // Cancel if we are not in our own territory + if (myFaction != otherFaction) { + me.sendMessage(Conf.colorSystem+"You can't use that in the territory of "+otherFaction.getTag(myFaction)); + return false; + } + + return true; + } }