Safer piston build protection

This commit is contained in:
ulumulu1510 2015-01-24 04:43:28 +01:00 committed by Olof Larsson
parent 262d1f2efa
commit 22bf4bc9bb
3 changed files with 100 additions and 14 deletions

View File

@ -84,6 +84,7 @@ import com.massivecraft.factions.event.EventFactionsPvpDisallowed;
import com.massivecraft.factions.event.EventFactionsPowerChange;
import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.spigot.SpigotFeatures;
import com.massivecraft.factions.util.VisualizeUtil;
import com.massivecraft.massivecore.EngineAbstract;
import com.massivecraft.massivecore.PriorityLines;
@ -1383,15 +1384,19 @@ public class EngineMain extends EngineAbstract
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockBuild(BlockPistonExtendEvent event)
{
// Is using Spigot or is checking deactivated by MConf?
if (SpigotFeatures.isActive() || ! MConf.get().handlePistonProtectionThroughDenyBuild) return;
Block block = event.getBlock();
Faction pistonFaction = BoardColl.get().getFactionAt(PS.valueOf(block));
// target end-of-the-line empty (air) block which is being pushed into, including if piston itself would extend into air
// Targets end-of-the-line empty (air) block which is being pushed into, including if piston itself would extend into air
Block targetBlock = block.getRelative(event.getDirection(), event.getLength() + 1);
// members of faction might not have build rights in their own territory, but pistons should still work regardless; so, address that corner case
// Factions involved
Faction pistonFaction = BoardColl.get().getFactionAt(PS.valueOf(block));
Faction targetFaction = BoardColl.get().getFactionAt(PS.valueOf(targetBlock));
// Members of a faction might not have build rights in their own territory, but pistons should still work regardless
if (targetFaction == pistonFaction) return;
// if potentially pushing into air/water/lava in another territory, we need to check it out
@ -1405,12 +1410,16 @@ public class EngineMain extends EngineAbstract
* up to 12 blocks and the width of any territory is 16 blocks, it should be safe (and much more lightweight) to test
* only the final target block as done above
*/
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockBuild(BlockPistonRetractEvent event)
{
// if not a sticky piston, retraction should be fine
// Is using Spigot or is checking deactivated by MConf?
if (SpigotFeatures.isActive() || ! MConf.get().handlePistonProtectionThroughDenyBuild) return;
// If not a sticky piston, retraction should be fine
if ( ! event.isSticky()) return;
Block retractBlock = event.getRetractLocation().getBlock();
@ -1419,17 +1428,17 @@ public class EngineMain extends EngineAbstract
// if potentially retracted block is just air/water/lava, no worries
if (retractBlock.isEmpty() || retractBlock.isLiquid()) return;
// Factions involved
Faction pistonFaction = BoardColl.get().getFactionAt(PS.valueOf(event.getBlock()));
// members of faction might not have build rights in their own territory, but pistons should still work regardless; so, address that corner case
Faction targetFaction = BoardColl.get().getFactionAt(retractPs);
// Members of a faction might not have build rights in their own territory, but pistons should still work regardless
if (targetFaction == pistonFaction) return;
if (!MPerm.getPermBuild().has(pistonFaction, targetFaction))
{
if (MPerm.getPermBuild().has(pistonFaction, targetFaction)) return;
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockBuild(HangingPlaceEvent event)

View File

@ -305,6 +305,10 @@ public class MConf extends Entity<MConf>
// 0.1 means that you take 10% less damage at home.
public double territoryShieldFactor = 0.1D;
// Protects the faction land from piston extending/retracting
// through the denying of MPerm build
public boolean handlePistonProtectionThroughDenyBuild = true;
// -------------------------------------------- //
// DENY COMMANDS
// -------------------------------------------- //

View File

@ -1,16 +1,26 @@
package com.massivecraft.factions.spigot;
import java.util.List;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.plugin.Plugin;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.engine.EngineMain;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.massivecore.EngineAbstract;
import com.massivecraft.massivecore.ps.PS;
public class EngineSpigot extends EngineAbstract
@ -73,4 +83,67 @@ public class EngineSpigot extends EngineAbstract
event.setCancelled(true);
}
/*
* Note: With 1.8 and the slime blocks, retracting and extending pistons
* became more of a problem. Blocks located on the border of a chunk
* could have easily been stolen. That is the reason why every block
* needs to be checked now, whether he moved into a territory which
* he actually may not move into.
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockBuild(BlockPistonExtendEvent event)
{
// Is checking deactivated by MConf?
if ( ! MConf.get().handlePistonProtectionThroughDenyBuild) return;
Faction pistonFaction = BoardColl.get().getFactionAt(PS.valueOf(event.getBlock()));
List<Block> blocks = event.getBlocks();
// Check for all extended blocks
for (Block block : blocks)
{
// Block which is being pushed into
Block targetBlock = block.getRelative(event.getDirection());
// Members of a faction might not have build rights in their own territory, but pistons should still work regardless
Faction targetFaction = BoardColl.get().getFactionAt(PS.valueOf(targetBlock));
if (targetFaction == pistonFaction) continue;
// Perm check
if (MPerm.getPermBuild().has(pistonFaction, targetFaction)) continue;
event.setCancelled(true);
return;
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockBuild(BlockPistonRetractEvent event)
{
// Is checking deactivated by MConf?
if ( ! MConf.get().handlePistonProtectionThroughDenyBuild) return;
Faction pistonFaction = BoardColl.get().getFactionAt(PS.valueOf(event.getBlock()));
List<Block> blocks = event.getBlocks();
// Check for all retracted blocks
for (Block block : blocks)
{
// Is the retracted block air/water/lava? Don't worry about it
if (block.isEmpty() || block.isLiquid()) return;
// Members of a faction might not have build rights in their own territory, but pistons should still work regardless
Faction targetFaction = BoardColl.get().getFactionAt(PS.valueOf(block));
if (targetFaction == pistonFaction) continue;
// Perm check
if (MPerm.getPermBuild().has(pistonFaction, targetFaction)) continue;
event.setCancelled(true);
return;
}
}
}