From 9770cb89834785e67fcc689b41e0caf4856534f8 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 19 Apr 2013 14:24:35 +0200 Subject: [PATCH] Minor messaround with land claiming. --- src/com/massivecraft/factions/FPlayer.java | 58 +++++++------------ .../factions/cmd/CmdFactionsAutoClaim.java | 3 +- .../factions/cmd/CmdFactionsClaim.java | 4 +- .../factions/integration/Econ.java | 4 +- .../factions/integration/Worldguard.java | 21 +++++-- .../listeners/FactionsListenerEcon.java | 2 +- .../listeners/TodoFactionsPlayerListener.java | 2 +- 7 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index f2a25c44..4648f238 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -4,7 +4,6 @@ import java.util.HashSet; import java.util.Set; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.entity.Player; import com.massivecraft.factions.event.FactionsEventLandClaim; @@ -730,16 +729,15 @@ public class FPlayer extends SenderEntity implements EconomyParticipato } } - public boolean canClaimForFactionAtLocation(Faction forFaction, Location location, boolean notifyFailure) + public boolean canClaimForFactionAtLocation(Faction forFaction, PS ps, boolean notifyFailure) { String error = null; - PS ps = PS.valueOf(location); Faction myFaction = this.getFaction(); Faction currentFaction = BoardColl.get().getFactionAt(ps); int ownedLand = forFaction.getLandCount(); - if (ConfServer.worldGuardChecking && Worldguard.checkForRegionsInChunk(location)) + if (ConfServer.worldGuardChecking && Worldguard.checkForRegionsInChunk(ps)) { // Checks for WorldGuard regions in the chunk attempting to be claimed error = Txt.parse("This land is protected"); @@ -814,51 +812,35 @@ public class FPlayer extends SenderEntity implements EconomyParticipato return error == null; } - public boolean attemptClaim(Faction forFaction, Location location, boolean notifyFailure) + // notifyFailure is false if called by auto-claim; no need to notify on every failure for it + // return value is false on failure, true on success + public boolean attemptClaim(Faction forFaction, PS psChunk, boolean notifyFailure) { - // notifyFailure is false if called by auto-claim; no need to notify on every failure for it - // return value is false on failure, true on success - - PS flocation = PS.valueOf(location).getChunk(true); - Faction currentFaction = BoardColl.get().getFactionAt(flocation); - + psChunk = psChunk.getChunk(true); + Faction currentFaction = BoardColl.get().getFactionAt(psChunk); int ownedLand = forFaction.getLandCount(); - if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false; - - // TODO: Add flag no costs?? - // if economy is enabled and they're not on the bypass list, make sure they can pay - boolean mustPay = Econ.isEnabled() && ! this.isUsingAdminMode(); - double cost = 0.0; - EconomyParticipator payee = null; - if (mustPay) - { - cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal()); - - if (ConfServer.econClaimUnconnectedFee != 0.0 && forFaction.getLandCountInWorld(flocation.getWorld()) > 0 && !BoardColl.get().isConnectedPs(flocation, forFaction)) - cost += ConfServer.econClaimUnconnectedFee; - - if(ConfServer.bankEnabled && ConfServer.bankFactionPaysLandCosts && this.hasFaction()) - payee = this.getFaction(); - else - payee = this; - - if ( ! Econ.hasAtLeast(payee, cost, "to claim this land")) return false; - } + if ( ! this.canClaimForFactionAtLocation(forFaction, psChunk, notifyFailure)) return false; // Event - FactionsEventLandClaim event = new FactionsEventLandClaim(sender, forFaction, flocation); + FactionsEventLandClaim event = new FactionsEventLandClaim(sender, forFaction, psChunk); event.run(); if (event.isCancelled()) return false; // then make 'em pay (if applicable) // TODO: The economy integration should cancel the event above! - if (mustPay && ! Econ.modifyMoney(payee, -cost, "claim this land")) return false; + // Calculate the cost to claim the area + double cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal()); + if (ConfServer.econClaimUnconnectedFee != 0.0 && forFaction.getLandCountInWorld(psChunk.getWorld()) > 0 && !BoardColl.get().isConnectedPs(psChunk, forFaction)) + { + cost += ConfServer.econClaimUnconnectedFee; + } + if (Econ.payForAction(cost, this, "claim this land")) return false; // TODO: The LWC integration should listen to Monitor for the claim event. if (LWCFeatures.getEnabled() && forFaction.isNormal() && ConfServer.onCaptureResetLwcLocks) { - LWCFeatures.clearOtherProtections(flocation, this.getFaction()); + LWCFeatures.clearOtherProtections(psChunk, this.getFaction()); } // announce success @@ -870,11 +852,11 @@ public class FPlayer extends SenderEntity implements EconomyParticipato fp.msg("%s claimed land for %s from %s.", this.describeTo(fp, true), forFaction.describeTo(fp), currentFaction.describeTo(fp)); } - BoardColl.get().setFactionAt(flocation, forFaction); - SpoutFeatures.updateTerritoryDisplayLoc(flocation); + BoardColl.get().setFactionAt(psChunk, forFaction); + SpoutFeatures.updateTerritoryDisplayLoc(psChunk); if (ConfServer.logLandClaims) - Factions.get().log(this.getName()+" claimed land at ("+flocation.getChunkX()+","+flocation.getChunkZ()+") for the faction: "+forFaction.getTag()); + Factions.get().log(this.getName()+" claimed land at ("+psChunk.getChunkX()+","+psChunk.getChunkZ()+") for the faction: "+forFaction.getTag()); return true; } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAutoClaim.java b/src/com/massivecraft/factions/cmd/CmdFactionsAutoClaim.java index a4129924..3b21586b 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAutoClaim.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAutoClaim.java @@ -6,6 +6,7 @@ import com.massivecraft.factions.Perm; import com.massivecraft.factions.cmd.arg.ARFaction; import com.massivecraft.mcore.cmd.req.ReqHasPerm; import com.massivecraft.mcore.cmd.req.ReqIsPlayer; +import com.massivecraft.mcore.ps.PS; public class CmdFactionsAutoClaim extends FCommand { @@ -35,7 +36,7 @@ public class CmdFactionsAutoClaim extends FCommand fme.setAutoClaimFor(forFaction); msg("Now auto-claiming land for %s.", forFaction.describeTo(fme)); - fme.attemptClaim(forFaction, me.getLocation(), true); + fme.attemptClaim(forFaction, PS.valueOf(me), true); } } \ No newline at end of file diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsClaim.java b/src/com/massivecraft/factions/cmd/CmdFactionsClaim.java index 716dd59e..3e24bbd0 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsClaim.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsClaim.java @@ -44,7 +44,7 @@ public class CmdFactionsClaim extends FCommand if (radius < 2) { // single chunk - fme.attemptClaim(forFaction, me.getLocation(), true); + fme.attemptClaim(forFaction, PS.valueOf(me), true); return; } @@ -64,7 +64,7 @@ public class CmdFactionsClaim extends FCommand @Override public boolean work() { - boolean success = fme.attemptClaim(forFaction, this.currentLocation(), true); + boolean success = fme.attemptClaim(forFaction, PS.valueOf(this.currentLocation()), true); if (success) failCount = 0; else if ( ! success && failCount++ >= limit) diff --git a/src/com/massivecraft/factions/integration/Econ.java b/src/com/massivecraft/factions/integration/Econ.java index 0331b1c9..271ed301 100644 --- a/src/com/massivecraft/factions/integration/Econ.java +++ b/src/com/massivecraft/factions/integration/Econ.java @@ -5,7 +5,6 @@ import java.util.Set; import java.util.logging.Level; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import org.bukkit.plugin.RegisteredServiceProvider; import com.massivecraft.factions.ConfServer; @@ -71,12 +70,11 @@ public class Econ // UTIL // -------------------------------------------- // - public static boolean payForAction(double cost, CommandSender sender, String actionDescription) + public static boolean payForAction(double cost, FPlayer fsender, String actionDescription) { if (!isEnabled()) return true; if (cost == 0D) return true; - FPlayer fsender = FPlayer.get(sender); if (fsender.isUsingAdminMode()) return true; Faction fsenderFaction = fsender.getFaction(); diff --git a/src/com/massivecraft/factions/integration/Worldguard.java b/src/com/massivecraft/factions/integration/Worldguard.java index f1dbb5bf..5f36a5f4 100644 --- a/src/com/massivecraft/factions/integration/Worldguard.java +++ b/src/com/massivecraft/factions/integration/Worldguard.java @@ -1,6 +1,8 @@ package com.massivecraft.factions.integration; import com.massivecraft.factions.Factions; +import com.massivecraft.mcore.ps.PS; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -79,16 +81,23 @@ public class Worldguard // Returns: // True: Regions found within chunk // False: No regions found within chunk - public static boolean checkForRegionsInChunk(Location loc) + public static boolean checkForRegionsInChunk(PS psChunk) { - if ( ! enabled) + // No WG hooks so we'll always bypass this check. + if (!enabled) return false; + + World world = null; + Chunk chunk = null; + try + { + world = psChunk.asBukkitWorld(true); + chunk = psChunk.asBukkitChunk(true); + } + catch (Exception e) { - // No WG hooks so we'll always bypass this check. return false; } - - World world = loc.getWorld(); - Chunk chunk = world.getChunkAt(loc); + int minChunkX = chunk.getX() << 4; int minChunkZ = chunk.getZ() << 4; int maxChunkX = minChunkX + 15; diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java b/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java index b8f49265..02157879 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java @@ -51,7 +51,7 @@ public class FactionsListenerEcon implements Listener if (event.getSender() == null) return; // ... and the sender can't afford ... - if (Econ.payForAction(cost, event.getSender(), command.getDesc())) return; + if (Econ.payForAction(cost, event.getFSender(), command.getDesc())) return; // ... then cancel. event.setCancelled(true); diff --git a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java index bf881aac..d95732c4 100644 --- a/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/TodoFactionsPlayerListener.java @@ -108,7 +108,7 @@ public class TodoFactionsPlayerListener implements Listener if (fplayer.getAutoClaimFor() != null) { - fplayer.attemptClaim(fplayer.getAutoClaimFor(), event.getTo(), true); + fplayer.attemptClaim(fplayer.getAutoClaimFor(), PS.valueOf(event.getTo()), true); } }