Claimnear perm and optional minimum distance between factions.

This commit is contained in:
Olof Larsson 2014-10-07 17:08:40 +02:00
parent 4286a0c1a0
commit 48bda6a2df
5 changed files with 89 additions and 43 deletions

View File

@ -1,7 +1,9 @@
package com.massivecraft.factions.entity; package com.massivecraft.factions.entity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import com.massivecraft.factions.Const; import com.massivecraft.factions.Const;
@ -168,51 +170,63 @@ public class BoardColl extends Coll<Board> implements BoardInterface
return board.getMap(observer, centerPs, inDegrees); return board.getMap(observer, centerPs, inDegrees);
} }
/* // -------------------------------------------- //
@Override // UTIL
public void init() // -------------------------------------------- //
// Distance -1 returns 0 chunks always.
// Distance 0 returns 1 chunk only (the one supplied).
// Distance 1 returns 3x3 = 9 chunks.
public static Set<PS> getNearbyChunks(PS chunk, int distance, boolean includeSelf)
{ {
super.init(); // Fix Args
if (chunk == null) throw new NullPointerException("chunk");
chunk = chunk.getChunk(true);
this.migrate(); // Create Ret
} Set<PS> ret = new LinkedHashSet<PS>();
// This method is for the 1.8.X --> 2.0.0 migration if (distance < 0) return ret;
public void migrate() // if (distance == 0 && ! includeSelf) return ret; // This will be done by the code below.
// 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++)
{ {
// Create file objects for (int z = zmin; z <= zmax; z++)
File oldFile = new File(Factions.get().getDataFolder(), "board.json");
File newFile = new File(Factions.get().getDataFolder(), "board.json.migrated");
// Already migrated?
if ( ! oldFile.exists()) return;
// Read the file content through GSON.
Type type = new TypeToken<Map<String,Map<String,TerritoryAccess>>>(){}.getType();
Map<String,Map<String,TerritoryAccess>> worldCoordIds = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
// Set the data
for (Entry<String,Map<String,TerritoryAccess>> entry : worldCoordIds.entrySet())
{ {
String worldName = entry.getKey(); if ( ! includeSelf && x == chunk.getChunkX() && z == chunk.getChunkZ()) continue;
BoardColl boardColl = this.getForWorld(worldName); ret.add(chunk.withChunkX(x).withChunkZ(z));
Board board = boardColl.get(worldName); }
for (Entry<String,TerritoryAccess> entry2 : entry.getValue().entrySet()) }
// Return Ret
return ret;
}
public static Set<Faction> getDistinctFactions(Collection<PS> chunks)
{ {
String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+"); // Fix Args
int chunkX = Integer.parseInt(ChunkCoordParts[0]); if (chunks == null) throw new NullPointerException("chunks");
int chunkZ = Integer.parseInt(ChunkCoordParts[1]);
PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build();
TerritoryAccess territoryAccess = entry2.getValue(); // Create Ret
Set<Faction> ret = new LinkedHashSet<Faction>();
board.setTerritoryAccessAt(ps, territoryAccess); // Main
} for (PS chunk : chunks)
{
Faction faction = BoardColl.get().getFactionAt(chunk);
if (faction == null) continue;
ret.add(faction);
} }
// Mark as migrated // Return Ret
oldFile.renameTo(newFile); return ret;
} }
*/
} }

View File

@ -161,8 +161,14 @@ public class MConf extends Entity<MConf>
// -------------------------------------------- // // -------------------------------------------- //
public boolean claimsMustBeConnected = true; public boolean claimsMustBeConnected = true;
public boolean claimingFromOthersAllowed = true;
public boolean claimsCanBeUnconnectedIfOwnedByOtherFaction = false; 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 claimsRequireMinFactionMembers = 1;
public int claimedLandsMax = 0; public int claimedLandsMax = 0;

View File

@ -43,6 +43,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable
public final static transient String ID_WITHDRAW = "withdraw"; public final static transient String ID_WITHDRAW = "withdraw";
public final static transient String ID_TERRITORY = "territory"; public final static transient String ID_TERRITORY = "territory";
public final static transient String ID_ACCESS = "access"; 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_REL = "rel";
public final static transient String ID_DISBAND = "disband"; public final static transient String ID_DISBAND = "disband";
public final static transient String ID_FLAGS = "flags"; public final static transient String ID_FLAGS = "flags";
@ -67,10 +68,11 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable
public final static transient int PRIORITY_WITHDRAW = 16000; public final static transient int PRIORITY_WITHDRAW = 16000;
public final static transient int PRIORITY_TERRITORY = 17000; public final static transient int PRIORITY_TERRITORY = 17000;
public final static transient int PRIORITY_ACCESS = 18000; public final static transient int PRIORITY_ACCESS = 18000;
public final static transient int PRIORITY_REL = 19000; public final static transient int PRIORITY_CLAIMNEAR = 19000;
public final static transient int PRIORITY_DISBAND = 20000; public final static transient int PRIORITY_REL = 20000;
public final static transient int PRIORITY_FLAGS = 21000; public final static transient int PRIORITY_DISBAND = 21000;
public final static transient int PRIORITY_PERMS = 22000; public final static transient int PRIORITY_FLAGS = 22000;
public final static transient int PRIORITY_PERMS = 23000;
// -------------------------------------------- // // -------------------------------------------- //
// META: CORE // META: CORE
@ -107,6 +109,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable
getPermWithdraw(); getPermWithdraw();
getPermTerritory(); getPermTerritory();
getPermAccess(); getPermAccess();
getPermClaimnear();
getPermRel(); getPermRel();
getPermDisband(); getPermDisband();
getPermFlags(); getPermFlags();
@ -132,6 +135,7 @@ public class MPerm extends Entity<MPerm> 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 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 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 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 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 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); } public static MPerm getPermFlags() { return getCreative(PRIORITY_FLAGS, ID_FLAGS, ID_FLAGS, "manage flags", MUtil.set(Rel.LEADER), false, true, true); }

View File

@ -794,7 +794,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements EconomyParticipato
MConf mconf = MConf.get(); MConf mconf = MConf.get();
// Validate // NoChange
if (newFaction == oldFaction) if (newFaction == oldFaction)
{ {
msg("%s<i> already owns this land.", newFaction.describeTo(this, true)); msg("%s<i> already owns this land.", newFaction.describeTo(this, true));
@ -836,6 +836,21 @@ public class MPlayer extends SenderEntity<MPlayer> implements EconomyParticipato
return false; 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<PS> nearbyChunks = BoardColl.getNearbyChunks(chunk, MConf.get().claimMinimumChunksDistanceToOthers, false);
Set<Faction> 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 if
( (
mconf.claimsMustBeConnected mconf.claimsMustBeConnected
@ -863,6 +878,12 @@ public class MPlayer extends SenderEntity<MPlayer> implements EconomyParticipato
{ {
if (!MPerm.getPermTerritory().has(this, oldFaction, false)) if (!MPerm.getPermTerritory().has(this, oldFaction, false))
{ {
if (this.hasFaction() && this.getFaction() == oldFaction)
{
sendMessage(MPerm.getPermTerritory().createDeniedMessage(this, oldFaction));
return false;
}
if (!mconf.claimingFromOthersAllowed) if (!mconf.claimingFromOthersAllowed)
{ {
msg("<b>You may not claim land from others."); msg("<b>You may not claim land from others.");
@ -888,6 +909,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements EconomyParticipato
} }
} }
} }
} }
// Event // Event

View File

@ -117,13 +117,13 @@ public class Econ
// Check Permissions // Check Permissions
if ( ! isMePermittedYou(by, from, MPerm.getPermWithdraw())) if ( ! isMePermittedYou(by, from, MPerm.getPermWithdraw()))
{ {
by.msg("<h>%s<i> lack permission to withdraw money from <h>%s's<i>.", by.describeTo(by, true), from.describeTo(by)); by.msg("<h>%s<i> lack permission to withdraw money from <h>%s<i>.", by.describeTo(by, true), from.describeTo(by));
return false; return false;
} }
if ( ! isMePermittedYou(by, to, MPerm.getPermDeposit())) if ( ! isMePermittedYou(by, to, MPerm.getPermDeposit()))
{ {
by.msg("<h>%s<i> lack permission to deposit money to <h>%s's<i>.", by.describeTo(by, true), to.describeTo(by)); by.msg("<h>%s<i> lack permission to deposit money to <h>%s<i>.", by.describeTo(by, true), to.describeTo(by));
return false; return false;
} }