From 48bda6a2df8c2de2492601bc99e8ce1e51c7e590 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 7 Oct 2014 17:08:40 +0200 Subject: [PATCH] Claimnear perm and optional minimum distance between factions. --- .../factions/entity/BoardColl.java | 84 +++++++++++-------- .../massivecraft/factions/entity/MConf.java | 8 +- .../massivecraft/factions/entity/MPerm.java | 12 ++- .../massivecraft/factions/entity/MPlayer.java | 24 +++++- .../factions/integration/Econ.java | 4 +- 5 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/entity/BoardColl.java b/src/main/java/com/massivecraft/factions/entity/BoardColl.java index 1e4938e0..4a1001b4 100644 --- a/src/main/java/com/massivecraft/factions/entity/BoardColl.java +++ b/src/main/java/com/massivecraft/factions/entity/BoardColl.java @@ -1,7 +1,9 @@ package com.massivecraft.factions.entity; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import com.massivecraft.factions.Const; @@ -168,51 +170,63 @@ public class BoardColl extends Coll implements BoardInterface return board.getMap(observer, centerPs, inDegrees); } - /* - @Override - public void init() - { - super.init(); - - this.migrate(); - } + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // - // This method is for the 1.8.X --> 2.0.0 migration - public void migrate() + // Distance -1 returns 0 chunks always. + // Distance 0 returns 1 chunk only (the one supplied). + // Distance 1 returns 3x3 = 9 chunks. + public static Set getNearbyChunks(PS chunk, int distance, boolean includeSelf) { - // Create file objects - File oldFile = new File(Factions.get().getDataFolder(), "board.json"); - File newFile = new File(Factions.get().getDataFolder(), "board.json.migrated"); + // Fix Args + if (chunk == null) throw new NullPointerException("chunk"); + chunk = chunk.getChunk(true); - // Already migrated? - if ( ! oldFile.exists()) return; + // Create Ret + Set ret = new LinkedHashSet(); - // Read the file content through GSON. - Type type = new TypeToken>>(){}.getType(); - Map> worldCoordIds = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type); + if (distance < 0) return ret; + // if (distance == 0 && ! includeSelf) return ret; // This will be done by the code below. - // Set the data - for (Entry> entry : worldCoordIds.entrySet()) + // Main + int xmin = chunk.getChunkX() - distance; + int xmax = chunk.getChunkX() + distance; + + int zmin = chunk.getChunkZ() - distance; + int zmax = chunk.getChunkZ() + distance; + + for (int x = xmin; x <= xmax; x++) { - String worldName = entry.getKey(); - BoardColl boardColl = this.getForWorld(worldName); - Board board = boardColl.get(worldName); - for (Entry entry2 : entry.getValue().entrySet()) + for (int z = zmin; z <= zmax; z++) { - String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+"); - int chunkX = Integer.parseInt(ChunkCoordParts[0]); - int chunkZ = Integer.parseInt(ChunkCoordParts[1]); - PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build(); - - TerritoryAccess territoryAccess = entry2.getValue(); - - board.setTerritoryAccessAt(ps, territoryAccess); + if ( ! includeSelf && x == chunk.getChunkX() && z == chunk.getChunkZ()) continue; + ret.add(chunk.withChunkX(x).withChunkZ(z)); } } - // Mark as migrated - oldFile.renameTo(newFile); + // Return Ret + return ret; + } + + public static Set getDistinctFactions(Collection chunks) + { + // Fix Args + if (chunks == null) throw new NullPointerException("chunks"); + + // Create Ret + Set ret = new LinkedHashSet(); + + // Main + for (PS chunk : chunks) + { + Faction faction = BoardColl.get().getFactionAt(chunk); + if (faction == null) continue; + ret.add(faction); + } + + // Return Ret + return ret; } - */ } diff --git a/src/main/java/com/massivecraft/factions/entity/MConf.java b/src/main/java/com/massivecraft/factions/entity/MConf.java index 4074a882..9e456439 100644 --- a/src/main/java/com/massivecraft/factions/entity/MConf.java +++ b/src/main/java/com/massivecraft/factions/entity/MConf.java @@ -161,8 +161,14 @@ public class MConf extends Entity // -------------------------------------------- // public boolean claimsMustBeConnected = true; - public boolean claimingFromOthersAllowed = true; public boolean claimsCanBeUnconnectedIfOwnedByOtherFaction = false; + + public boolean claimingFromOthersAllowed = true; + + // 0 means you can claim just next to others + // 1 means you must have a single chunk of padding in between. + public int claimMinimumChunksDistanceToOthers = 0; + public int claimsRequireMinFactionMembers = 1; public int claimedLandsMax = 0; diff --git a/src/main/java/com/massivecraft/factions/entity/MPerm.java b/src/main/java/com/massivecraft/factions/entity/MPerm.java index 17778bc7..9660cdc2 100644 --- a/src/main/java/com/massivecraft/factions/entity/MPerm.java +++ b/src/main/java/com/massivecraft/factions/entity/MPerm.java @@ -43,6 +43,7 @@ public class MPerm extends Entity implements Prioritized, Registerable public final static transient String ID_WITHDRAW = "withdraw"; public final static transient String ID_TERRITORY = "territory"; public final static transient String ID_ACCESS = "access"; + public final static transient String ID_CLAIMNEAR = "claimnear"; public final static transient String ID_REL = "rel"; public final static transient String ID_DISBAND = "disband"; public final static transient String ID_FLAGS = "flags"; @@ -67,10 +68,11 @@ public class MPerm extends Entity implements Prioritized, Registerable public final static transient int PRIORITY_WITHDRAW = 16000; public final static transient int PRIORITY_TERRITORY = 17000; public final static transient int PRIORITY_ACCESS = 18000; - public final static transient int PRIORITY_REL = 19000; - public final static transient int PRIORITY_DISBAND = 20000; - public final static transient int PRIORITY_FLAGS = 21000; - public final static transient int PRIORITY_PERMS = 22000; + public final static transient int PRIORITY_CLAIMNEAR = 19000; + public final static transient int PRIORITY_REL = 20000; + public final static transient int PRIORITY_DISBAND = 21000; + public final static transient int PRIORITY_FLAGS = 22000; + public final static transient int PRIORITY_PERMS = 23000; // -------------------------------------------- // // META: CORE @@ -107,6 +109,7 @@ public class MPerm extends Entity implements Prioritized, Registerable getPermWithdraw(); getPermTerritory(); getPermAccess(); + getPermClaimnear(); getPermRel(); getPermDisband(); getPermFlags(); @@ -132,6 +135,7 @@ public class MPerm extends Entity implements Prioritized, Registerable public static MPerm getPermWithdraw() { return getCreative(PRIORITY_WITHDRAW, ID_WITHDRAW, ID_WITHDRAW, "withdraw money", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermTerritory() { return getCreative(PRIORITY_TERRITORY, ID_TERRITORY, ID_TERRITORY, "claim or unclaim", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermAccess() { return getCreative(PRIORITY_ACCESS, ID_ACCESS, ID_ACCESS, "grant territory", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } + public static MPerm getPermClaimnear() { return getCreative(PRIORITY_CLAIMNEAR, ID_CLAIMNEAR, ID_CLAIMNEAR, "claim nearby", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), false, false, false); } // non editable, non visible. public static MPerm getPermRel() { return getCreative(PRIORITY_REL, ID_REL, ID_REL, "change relations", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermDisband() { return getCreative(PRIORITY_DISBAND, ID_DISBAND, ID_DISBAND, "disband the faction", MUtil.set(Rel.LEADER), false, true, true); } public static MPerm getPermFlags() { return getCreative(PRIORITY_FLAGS, ID_FLAGS, ID_FLAGS, "manage flags", MUtil.set(Rel.LEADER), false, true, true); } diff --git a/src/main/java/com/massivecraft/factions/entity/MPlayer.java b/src/main/java/com/massivecraft/factions/entity/MPlayer.java index ba3858da..38d3aa28 100644 --- a/src/main/java/com/massivecraft/factions/entity/MPlayer.java +++ b/src/main/java/com/massivecraft/factions/entity/MPlayer.java @@ -794,7 +794,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato MConf mconf = MConf.get(); - // Validate + // NoChange if (newFaction == oldFaction) { msg("%s already owns this land.", newFaction.describeTo(this, true)); @@ -836,6 +836,21 @@ public class MPlayer extends SenderEntity implements EconomyParticipato return false; } + // Calculate the factions nearby, excluding the chunk itself, the faction itself and the wilderness faction. + // The chunk itself is handled in the "if (oldFaction.isNormal())" section below. + Set nearbyChunks = BoardColl.getNearbyChunks(chunk, MConf.get().claimMinimumChunksDistanceToOthers, false); + Set nearbyFactions = BoardColl.getDistinctFactions(nearbyChunks); + nearbyFactions.remove(FactionColl.get().getNone()); + nearbyFactions.remove(newFaction); + // Next we check if the new faction has permission to claim nearby the nearby factions. + MPerm claimnear = MPerm.getPermClaimnear(); + for (Faction nearbyFaction : nearbyFactions) + { + if (claimnear.has(newFaction, nearbyFaction)) continue; + sendMessage(claimnear.createDeniedMessage(this, nearbyFaction)); + return false; + } + if ( mconf.claimsMustBeConnected @@ -863,6 +878,12 @@ public class MPlayer extends SenderEntity implements EconomyParticipato { if (!MPerm.getPermTerritory().has(this, oldFaction, false)) { + if (this.hasFaction() && this.getFaction() == oldFaction) + { + sendMessage(MPerm.getPermTerritory().createDeniedMessage(this, oldFaction)); + return false; + } + if (!mconf.claimingFromOthersAllowed) { msg("You may not claim land from others."); @@ -888,6 +909,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato } } } + } // Event diff --git a/src/main/java/com/massivecraft/factions/integration/Econ.java b/src/main/java/com/massivecraft/factions/integration/Econ.java index 8ff95b01..cc1194eb 100644 --- a/src/main/java/com/massivecraft/factions/integration/Econ.java +++ b/src/main/java/com/massivecraft/factions/integration/Econ.java @@ -117,13 +117,13 @@ public class Econ // Check Permissions if ( ! isMePermittedYou(by, from, MPerm.getPermWithdraw())) { - by.msg("%s lack permission to withdraw money from %s's.", by.describeTo(by, true), from.describeTo(by)); + by.msg("%s lack permission to withdraw money from %s.", by.describeTo(by, true), from.describeTo(by)); return false; } if ( ! isMePermittedYou(by, to, MPerm.getPermDeposit())) { - by.msg("%s lack permission to deposit money to %s's.", by.describeTo(by, true), to.describeTo(by)); + by.msg("%s lack permission to deposit money to %s.", by.describeTo(by, true), to.describeTo(by)); return false; }