From c19f3405bf1ba406b517f14da7c3236fb04834ba Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 15 Jan 2012 11:41:14 -0600 Subject: [PATCH] when redstone torches or TNT were attempted to be placed in protected territory but prevented, it could previously still trigger a TNT explosion due to a longstanding unfixed CraftBukkit exploit. A workaround is now in place to prevent this, and any such attempts are logged and announced to everyone on the server. For reference: https://bukkit.atlassian.net/browse/BUKKIT-89 --- .../listeners/FactionsBlockListener.java | 8 ++ .../listeners/FactionsChatEarlyListener.java | 4 +- .../listeners/FactionsEntityListener.java | 78 ++++++++++++++++++- 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/com/massivecraft/factions/listeners/FactionsBlockListener.java b/src/com/massivecraft/factions/listeners/FactionsBlockListener.java index 8c1df823..a26a4d3c 100644 --- a/src/com/massivecraft/factions/listeners/FactionsBlockListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsBlockListener.java @@ -1,6 +1,7 @@ package com.massivecraft.factions.listeners; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; @@ -93,6 +94,13 @@ public class FactionsBlockListener extends BlockListener if ( ! playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "build", false)) { event.setCancelled(true); + + Material handItem = event.getPlayer().getItemInHand().getType(); + if (handItem == Material.TNT || handItem == Material.REDSTONE_TORCH_ON) + { + Faction targetFaction = Board.getFactionAt(new FLocation(event.getBlock())); + FactionsEntityListener.trackPotentialExplosionExploit(event.getPlayer().getName(), targetFaction, handItem, event.getBlock().getLocation()); + } } } diff --git a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java index dbef7ac2..a5209f0a 100644 --- a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java @@ -50,13 +50,13 @@ public class FactionsChatEarlyListener extends PlayerListener String message = String.format(Conf.factionChatFormat, me.describeTo(myFaction), msg); myFaction.sendMessage(message); - P.p.log(Level.INFO, ChatColor.stripColor("FactionChat "+me.getFaction().getTag()+": "+message)); + P.p.log(Level.INFO, ChatColor.stripColor("FactionChat "+myFaction.getTag()+": "+message)); //Send to any players who are spying chat for (FPlayer fplayer : FPlayers.i.getOnline()) { if(fplayer.isSpyingChat() && fplayer.getFaction() != myFaction) - fplayer.sendMessage("[FCspy] "+me.getFaction().getTag()+": "+message); + fplayer.sendMessage("[FCspy] "+myFaction.getTag()+": "+message); } event.setCancelled(true); diff --git a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java index a3002ff4..d744ed2c 100644 --- a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -1,12 +1,16 @@ package com.massivecraft.factions.listeners; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.logging.Level; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; +import org.bukkit.entity.TNTPrimed; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EndermanPickupEvent; import org.bukkit.event.entity.EndermanPlaceEvent; @@ -39,7 +43,9 @@ public class FactionsEntityListener extends EntityListener { this.p = p; } - + + private static ArrayList exploitExplosions = new ArrayList(); + @Override public void onEntityDeath(EntityDeathEvent event) { @@ -91,7 +97,41 @@ public class FactionsEntityListener extends EntityListener public void onEntityExplode(EntityExplodeEvent event) { if ( event.isCancelled()) return; - + + if (event.getEntity() instanceof TNTPrimed && exploitExplosions.size() > 0) + { // make sure this isn't a TNT explosion exploit attempt + int locX = event.getLocation().getBlockX(); + int locZ = event.getLocation().getBlockZ(); + + for (int i = exploitExplosions.size() - 1; i >= 0; i--) + { + PotentialExplosionExploit ex = exploitExplosions.get(i); + + // remove anything from the list older than 10 seconds (TNT takes 4 seconds to trigger; provide some leeway) + if (ex.timeMillis + 10000 < System.currentTimeMillis()) + { + exploitExplosions.remove(i); + continue; + } + + int absX = Math.abs(ex.X - locX); + int absZ = Math.abs(ex.Z - locZ); + if (absX < 5 && absZ < 5) + { // it sure looks like an exploit attempt + // let's tattle on him to everyone + String msg = "NOTICE: Player \""+ex.playerName+"\" attempted to exploit a TNT bug in the territory of \""+ex.faction.getTag()+"\" at "+ex.X+","+ex.Z+" (X,Z) using "+ex.item.name(); + P.p.log(Level.WARNING, msg); + for (FPlayer fplayer : FPlayers.i.getOnline()) + { + fplayer.sendMessage(msg); + } + event.setCancelled(true); + exploitExplosions.remove(i); + return; + } + } + } + for (Block block : event.blockList()) { Faction faction = Board.getFactionAt(new FLocation(block)); @@ -321,4 +361,38 @@ public class FactionsEntityListener extends EntityListener event.setCancelled(true); } + + + /** + * Since the Bukkit team still don't seem to be in any hurry to fix the problem after at least half a year, + * we'll track potential explosion exploits ourselves and try to prevent them + * For reference, canceled TNT placement next to redstone power is bugged and triggers a free explosion + * Same thing happens for canceled redstone torch placement next to existing TNT + * https://bukkit.atlassian.net/browse/BUKKIT-89 + */ + + public static void trackPotentialExplosionExploit(String playerName, Faction faction, Material item, Location location) + { + exploitExplosions.add(new PotentialExplosionExploit(playerName, faction, item, location)); + } + + public static class PotentialExplosionExploit + { + public String playerName; + public Faction faction; + public Material item; + public long timeMillis; + public int X; + public int Z; + + public PotentialExplosionExploit(String playerName, Faction faction, Material item, Location location) + { + this.playerName = playerName; + this.faction = faction; + this.item = item; + this.timeMillis = System.currentTimeMillis(); + this.X = location.getBlockX(); + this.Z = location.getBlockZ(); + } + } }