2011-07-18 22:06:02 +02:00
|
|
|
package com.massivecraft.factions.listeners;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.*;
|
|
|
|
import com.massivecraft.factions.integration.Worldguard;
|
|
|
|
import com.massivecraft.factions.struct.Permission;
|
|
|
|
import com.massivecraft.factions.struct.Relation;
|
2018-01-04 10:36:51 +01:00
|
|
|
import com.massivecraft.factions.zcore.fperms.Access;
|
|
|
|
import com.massivecraft.factions.zcore.fperms.Action;
|
2011-07-20 19:22:03 +02:00
|
|
|
import org.bukkit.Location;
|
2011-06-10 21:16:14 +02:00
|
|
|
import org.bukkit.Material;
|
2011-02-06 13:36:11 +01:00
|
|
|
import org.bukkit.block.Block;
|
2016-06-11 01:43:48 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
2011-02-06 13:36:11 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2012-01-28 11:37:55 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
2014-04-04 20:55:21 +02:00
|
|
|
import org.bukkit.event.block.*;
|
|
|
|
|
|
|
|
|
|
|
|
public class FactionsBlockListener implements Listener {
|
2014-08-05 17:17:27 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public P p;
|
|
|
|
|
|
|
|
public FactionsBlockListener(P p) {
|
|
|
|
this.p = p;
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:33:23 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2014-04-04 20:55:21 +02:00
|
|
|
public void onBlockPlace(BlockPlaceEvent event) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!event.canBuild()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// special case for flint&steel, which should only be prevented by DenyUsage list
|
|
|
|
if (event.getBlockPlaced().getType() == Material.FIRE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock().getLocation(), "build", false)) {
|
2014-09-25 20:44:35 +02:00
|
|
|
event.setCancelled(true);
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 18:08:42 +02:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onBlockFromTo(BlockFromToEvent event) {
|
|
|
|
if (!Conf.handleExploitLiquidFlow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.getBlock().isLiquid()) {
|
|
|
|
if (event.getToBlock().isEmpty()) {
|
|
|
|
Faction from = Board.getInstance().getFactionAt(new FLocation(event.getBlock()));
|
|
|
|
Faction to = Board.getInstance().getFactionAt(new FLocation(event.getToBlock()));
|
|
|
|
if (from == to) {
|
|
|
|
// not concerned with inter-faction events
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// from faction != to faction
|
|
|
|
if (to.isNormal()) {
|
|
|
|
if (from.isNormal() && from.getRelationTo(to).isAlly()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:33:23 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2014-04-04 20:55:21 +02:00
|
|
|
public void onBlockBreak(BlockBreakEvent event) {
|
|
|
|
if (!playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock().getLocation(), "destroy", false)) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2015-02-23 20:33:23 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2014-04-04 20:55:21 +02:00
|
|
|
public void onBlockDamage(BlockDamageEvent event) {
|
|
|
|
if (event.getInstaBreak() && !playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock().getLocation(), "destroy", false)) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:33:23 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2014-04-04 20:55:21 +02:00
|
|
|
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!Conf.pistonProtectionThroughDenyBuild) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
Faction pistonFaction = Board.getInstance().getFactionAt(new FLocation(event.getBlock()));
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// target end-of-the-line empty (air) block which is being pushed into, including if piston itself would extend into air
|
|
|
|
Block targetBlock = event.getBlock().getRelative(event.getDirection(), event.getLength() + 1);
|
|
|
|
|
|
|
|
// if potentially pushing into air/water/lava in another territory, we need to check it out
|
|
|
|
if ((targetBlock.isEmpty() || targetBlock.isLiquid()) && !canPistonMoveBlock(pistonFaction, targetBlock.getLocation())) {
|
2014-07-01 22:10:18 +02:00
|
|
|
event.setCancelled(true);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
2011-07-20 19:22:03 +02:00
|
|
|
|
2018-01-05 02:40:27 +01:00
|
|
|
/*
|
2014-04-04 20:55:21 +02:00
|
|
|
* note that I originally was testing the territory of each affected block, but since I found that pistons can only push
|
2018-01-05 02:40:27 +01:00
|
|
|
* 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
|
|
|
|
*/
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 20:33:23 +01:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
2014-04-04 20:55:21 +02:00
|
|
|
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
|
|
|
// if not a sticky piston, retraction should be fine
|
2015-02-23 20:33:23 +01:00
|
|
|
if (!event.isSticky() || !Conf.pistonProtectionThroughDenyBuild) {
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Location targetLoc = event.getRetractLocation();
|
2014-11-09 02:07:42 +01:00
|
|
|
Faction otherFaction = Board.getInstance().getFactionAt(new FLocation(targetLoc));
|
2014-12-07 21:26:13 +01:00
|
|
|
|
2014-11-09 02:07:42 +01:00
|
|
|
// Check if the piston is moving in a faction's territory. This disables pistons entirely in faction territory.
|
2014-12-07 21:26:13 +01:00
|
|
|
if (otherFaction.isNormal() && P.p.getConfig().getBoolean("disable-pistons-in-territory", false)) {
|
2014-11-09 02:07:42 +01:00
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// if potentially retracted block is just air/water/lava, no worries
|
|
|
|
if (targetLoc.getBlock().isEmpty() || targetLoc.getBlock().isLiquid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
Faction pistonFaction = Board.getInstance().getFactionAt(new FLocation(event.getBlock()));
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
if (!canPistonMoveBlock(pistonFaction, targetLoc)) {
|
2014-07-01 22:10:18 +02:00
|
|
|
event.setCancelled(true);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 01:43:48 +02:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onFrostWalker(EntityBlockFormEvent event) {
|
|
|
|
if (event.getEntity() == null || event.getEntity().getType() != EntityType.PLAYER || event.getBlock() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Player player = (Player) event.getEntity();
|
|
|
|
Location location = event.getBlock().getLocation();
|
|
|
|
|
2016-06-12 01:49:33 +02:00
|
|
|
// only notify every 10 seconds
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player);
|
|
|
|
boolean justCheck = fPlayer.getLastFrostwalkerMessage() + 10000 > System.currentTimeMillis();
|
|
|
|
if (!justCheck) {
|
|
|
|
fPlayer.setLastFrostwalkerMessage();
|
|
|
|
}
|
|
|
|
|
2016-06-11 01:43:48 +02:00
|
|
|
// Check if they have build permissions here. If not, block this from happening.
|
2018-01-04 10:36:51 +01:00
|
|
|
if (!playerCanBuildDestroyBlock(player, location, "frostwalk", justCheck)) {
|
2016-06-11 01:43:48 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
private boolean canPistonMoveBlock(Faction pistonFaction, Location target) {
|
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
Faction otherFaction = Board.getInstance().getFactionAt(new FLocation(target));
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (pistonFaction == otherFaction) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2015-09-08 18:46:48 +02:00
|
|
|
if (otherFaction.isWilderness()) {
|
2017-12-19 11:18:13 +01:00
|
|
|
return !Conf.wildernessDenyBuild || Conf.worldsNoWildernessProtection.contains(target.getWorld().getName());
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
} else if (otherFaction.isSafeZone()) {
|
2017-12-19 11:18:13 +01:00
|
|
|
return !Conf.safeZoneDenyBuild;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
} else if (otherFaction.isWarZone()) {
|
2017-12-19 11:18:13 +01:00
|
|
|
return !Conf.warZoneDenyBuild;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Relation rel = pistonFaction.getRelationTo(otherFaction);
|
|
|
|
|
2017-12-19 11:18:13 +01:00
|
|
|
return !rel.confDenyBuild(otherFaction.hasPlayersOnline());
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean playerCanBuildDestroyBlock(Player player, Location location, String action, boolean justCheck) {
|
2014-07-01 22:10:18 +02:00
|
|
|
String name = player.getName();
|
|
|
|
if (Conf.playersWhoBypassAllProtection.contains(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
FPlayer me = FPlayers.getInstance().getById(player.getUniqueId().toString());
|
2014-07-01 22:10:18 +02:00
|
|
|
if (me.isAdminBypassing()) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
FLocation loc = new FLocation(location);
|
2014-10-19 07:37:25 +02:00
|
|
|
Faction otherFaction = Board.getInstance().getFactionAt(loc);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2015-09-08 18:46:48 +02:00
|
|
|
if (otherFaction.isWilderness()) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!Conf.wildernessDenyBuild || Conf.worldsNoWildernessProtection.contains(location.getWorld().getName())) {
|
2014-04-04 20:55:21 +02:00
|
|
|
return true; // This is not faction territory. Use whatever you like here.
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!justCheck) {
|
|
|
|
me.msg("<b>You can't " + action + " in the wilderness.");
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
return false;
|
2014-07-01 21:52:40 +02:00
|
|
|
} else if (otherFaction.isSafeZone()) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!Conf.safeZoneDenyBuild || Permission.MANAGE_SAFE_ZONE.has(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!justCheck) {
|
|
|
|
me.msg("<b>You can't " + action + " in a safe zone.");
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
return false;
|
2014-07-01 21:52:40 +02:00
|
|
|
} else if (otherFaction.isWarZone()) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!Conf.warZoneDenyBuild || Permission.MANAGE_WAR_ZONE.has(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!justCheck) {
|
|
|
|
me.msg("<b>You can't " + action + " in a war zone.");
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-27 18:02:16 +01:00
|
|
|
if (P.p.getConfig().getBoolean("hcf.raidable", false) && otherFaction.getLandRounded() >= otherFaction.getPowerRounded()) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
Faction myFaction = me.getFaction();
|
|
|
|
Relation rel = myFaction.getRelationTo(otherFaction);
|
|
|
|
boolean online = otherFaction.hasPlayersOnline();
|
|
|
|
boolean pain = !justCheck && rel.confPainBuild(online);
|
2014-04-04 20:55:21 +02:00
|
|
|
boolean deny = rel.confDenyBuild(online);
|
|
|
|
|
|
|
|
// hurt the player for building/destroying in other territory?
|
|
|
|
if (pain) {
|
|
|
|
player.damage(Conf.actionDeniedPainAmount);
|
|
|
|
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!deny) {
|
2014-04-04 20:55:21 +02:00
|
|
|
me.msg("<b>It is painful to try to " + action + " in the territory of " + otherFaction.getTag(myFaction));
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:17:26 +01:00
|
|
|
Access access = otherFaction.getAccess(me, Action.valueOf(action));
|
2018-01-04 10:36:51 +01:00
|
|
|
if (access != null && access != Access.UNDEFINED) {
|
|
|
|
// TODO: Update this once new access values are added other than just allow / deny.
|
|
|
|
return access == Access.ALLOW;
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
// cancel building/destroying in other territory?
|
|
|
|
if (deny) {
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!justCheck) {
|
2014-04-04 20:55:21 +02:00
|
|
|
me.msg("<b>You can't " + action + " in the territory of " + otherFaction.getTag(myFaction));
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also cancel and/or cause pain if player doesn't have ownership rights for this claim
|
|
|
|
if (Conf.ownedAreasEnabled && (Conf.ownedAreaDenyBuild || Conf.ownedAreaPainBuild) && !otherFaction.playerHasOwnershipRights(me, loc)) {
|
|
|
|
if (!pain && Conf.ownedAreaPainBuild && !justCheck) {
|
|
|
|
player.damage(Conf.actionDeniedPainAmount);
|
2011-07-20 19:22:03 +02:00
|
|
|
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!Conf.ownedAreaDenyBuild) {
|
2014-04-04 20:55:21 +02:00
|
|
|
me.msg("<b>It is painful to try to " + action + " in this territory, it is owned by: " + otherFaction.getOwnerListString(loc));
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (Conf.ownedAreaDenyBuild) {
|
2014-07-01 21:49:42 +02:00
|
|
|
if (!justCheck) {
|
2014-04-04 20:55:21 +02:00
|
|
|
me.msg("<b>You can't " + action + " in this territory, it is owned by: " + otherFaction.getOwnerListString(loc));
|
2014-07-01 21:49:42 +02:00
|
|
|
}
|
2011-07-20 19:22:03 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-07-20 19:22:03 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|