Clean up the Coll's

This commit is contained in:
TheComputerGeek2 2017-03-12 09:55:01 -07:00 committed by ulumulu1510
parent 0ba52eae14
commit b13c77b6c5
6 changed files with 110 additions and 44 deletions

View File

@ -131,29 +131,42 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override
public Set<PS> getChunks(Faction faction)
{
// Create
Set<PS> ret = new HashSet<PS>();
// Fill
for (Board board : this.getAll())
{
ret.addAll(board.getChunks(faction));
}
// Return
return ret;
}
@Override
public Set<PS> getChunks(String factionId)
{
// Create
Set<PS> ret = new HashSet<PS>();
// Fill
for (Board board : this.getAll())
{
ret.addAll(board.getChunks(factionId));
}
// Return
return ret;
}
@Override
public Map<Faction, Set<PS>> getFactionToChunks()
{
// Create
Map<Faction, Set<PS>> ret = null;
// Fill
for (Board board : this.getAll())
{
// Use the first board directly
@ -180,7 +193,10 @@ public class BoardColl extends Coll<Board> implements BoardInterface
}
}
// Enforce create
if (ret == null) ret = new MassiveMap<Faction, Set<PS>>();
// Return
return ret;
}
@ -250,8 +266,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
{
for (Board board : this.getAll())
{
if ( ! board.hasClaimed(factionId)) continue;
return true;
if (board.hasClaimed(factionId)) return true;
}
return false;
}
@ -318,14 +333,16 @@ public class BoardColl extends Coll<Board> implements BoardInterface
public Set<String> getClaimedWorlds(String factionId)
{
// Create
Set<String> ret = new MassiveSet<>();
// Fill
for (Board board : this.getAll())
{
if ( ! board.hasClaimed(factionId)) continue;
ret.add(board.getId());
if (board.hasClaimed(factionId)) ret.add(board.getId());
}
// Return
return ret;
}
@ -336,33 +353,35 @@ public class BoardColl extends Coll<Board> implements BoardInterface
// 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)
public static Set<PS> getNearbyChunks(PS psChunk, int distance)
{
// Fix Args
if (chunk == null) throw new NullPointerException("chunk");
chunk = chunk.getChunk(true);
if (psChunk == null) throw new NullPointerException("psChunk");
psChunk = psChunk.getChunk(true);
// Create Ret
// Create
Set<PS> ret = new LinkedHashSet<PS>();
if (distance < 0) return ret;
// Main
int xmin = chunk.getChunkX() - distance;
int xmax = chunk.getChunkX() + distance;
// Fill
int chunkX = psChunk.getChunkX();
int xmin = chunkX - distance;
int xmax = chunkX + distance;
int zmin = chunk.getChunkZ() - distance;
int zmax = chunk.getChunkZ() + distance;
int chunkZ = psChunk.getChunkZ();
int zmin = chunkZ - distance;
int zmax = chunkZ + distance;
for (int x = xmin; x <= xmax; x++)
{
PS psChunkX = psChunk.withChunkX(x);
for (int z = zmin; z <= zmax; z++)
{
ret.add(chunk.withChunkX(x).withChunkZ(z));
ret.add(psChunkX.withChunkZ(z));
}
}
// Return Ret
// Return
return ret;
}
@ -371,18 +390,18 @@ public class BoardColl extends Coll<Board> implements BoardInterface
// Fix Args
if (chunks == null) throw new NullPointerException("chunks");
// Create Ret
// Create
Set<PS> ret = new LinkedHashSet<PS>();
if (distance < 0) return ret;
// Main
// Fill
for (PS chunk : chunks)
{
ret.addAll(getNearbyChunks(chunk, distance));
}
// Return Ret
// Return
return ret;
}
@ -391,33 +410,37 @@ public class BoardColl extends Coll<Board> implements BoardInterface
// Fix Args
if (chunks == null) throw new NullPointerException("chunks");
// Create Ret
// Create
Set<Faction> ret = new LinkedHashSet<Faction>();
// Main
// Fill
for (PS chunk : chunks)
{
Faction faction = BoardColl.get().getFactionAt(chunk);
Faction faction = get().getFactionAt(chunk);
if (faction == null) continue;
ret.add(faction);
}
// Return Ret
// Return
return ret;
}
public static Map<PS, Faction> getChunkFaction(Collection<PS> chunks)
{
// Create
Map<PS, Faction> ret = new LinkedHashMap<PS, Faction>();
// Fill
Faction none = FactionColl.get().getNone();
for (PS chunk : chunks)
{
chunk = chunk.getChunk(true);
Faction faction = get().getFactionAt(chunk);
if (faction == null) faction = FactionColl.get().getNone();
if (faction == null) faction = none;
ret.put(chunk, faction);
}
// Return
return ret;
}

View File

@ -1,13 +1,18 @@
package com.massivecraft.factions.entity;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.util.MiscUtil;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.util.Txt;
public class FactionColl extends Coll<Faction>
{
@ -197,25 +202,40 @@ public class FactionColl extends Coll<Faction>
public void econLandRewardRoutine()
{
// If econ is enabled ...
if (!Econ.isEnabled()) return;
// ... and the land reward is non zero ...
double econLandReward = MConf.get().econLandReward;
if (econLandReward == 0.0) return;
// ... log initiation ...
Factions.get().log("Running econLandRewardRoutine...");
MFlag flagPeaceful = MFlag.getFlagPeaceful();
// ... and for each faction ...
for (Faction faction : this.getAll())
{
// ... get the land count ...
int landCount = faction.getLandCount();
if (!faction.getFlag(MFlag.getFlagPeaceful()) && landCount > 0)
{
// ... and if the faction isn't peaceful and has land ...
if (faction.getFlag(flagPeaceful) || landCount > 0) continue;
// ... get the faction's members ...
List<MPlayer> players = faction.getMPlayers();
// ... calculate the reward ...
int playerCount = players.size();
double reward = econLandReward * landCount / playerCount;
// ... and grant the reward.
String description = String.format("own %s faction land divided among %s members", landCount, playerCount);
for (MPlayer player : players)
{
Econ.modifyMoney(player, reward, "own " + landCount + " faction land divided among " + playerCount + " members");
}
Econ.modifyMoney(player, reward, description);
}
}
}
@ -225,18 +245,23 @@ public class FactionColl extends Coll<Faction>
public ArrayList<String> validateName(String str)
{
// Create
ArrayList<String> errors = new ArrayList<String>();
// Fill
// Check minimum length
if (MiscUtil.getComparisonString(str).length() < MConf.get().factionNameLengthMin)
{
errors.add(Txt.parse("<i>The faction name can't be shorter than <h>%s<i> chars.", MConf.get().factionNameLengthMin));
}
// Check maximum length
if (str.length() > MConf.get().factionNameLengthMax)
{
errors.add(Txt.parse("<i>The faction name can't be longer than <h>%s<i> chars.", MConf.get().factionNameLengthMax));
}
// Check characters used
for (char c : str.toCharArray())
{
if (!MiscUtil.substanceChars.contains(String.valueOf(c)))
@ -245,6 +270,7 @@ public class FactionColl extends Coll<Faction>
}
}
// Return
return errors;
}
@ -270,7 +296,8 @@ public class FactionColl extends Coll<Faction>
{
// Create
Map<Rel, List<String>> ret = new LinkedHashMap<Rel, List<String>>();
boolean peaceful = faction.getFlag(MFlag.getFlagPeaceful());
MFlag flagPeaceful = MFlag.getFlagPeaceful();
boolean peaceful = faction.getFlag(flagPeaceful);
for (Rel rel : rels)
{
ret.put(rel, new ArrayList<String>());
@ -279,7 +306,7 @@ public class FactionColl extends Coll<Faction>
// Fill
for (Faction fac : FactionColl.get().getAll())
{
if (fac.getFlag(MFlag.getFlagPeaceful())) continue;
if (fac.getFlag(flagPeaceful)) continue;
Rel rel = fac.getRelationTo(faction);
List<String> names = ret.get(rel);
@ -289,7 +316,7 @@ public class FactionColl extends Coll<Faction>
names.add(name);
}
// Replace TRUCE if peasceful
// Replace TRUCE if peaceful
if (!peaceful) return ret;
List<String> names = ret.get(Rel.TRUCE);

View File

@ -46,12 +46,17 @@ public class MFlagColl extends Coll<MFlag>
public List<MFlag> getAll(boolean registered)
{
// Create
List<MFlag> ret = new ArrayList<MFlag>();
// Fill
for (MFlag mflag : this.getAll())
{
if (mflag.isRegistered() != registered) continue;
ret.add(mflag);
}
// Return
return ret;
}

View File

@ -46,12 +46,17 @@ public class MPermColl extends Coll<MPerm>
public List<MPerm> getAll(boolean registered)
{
// Create
List<MPerm> ret = new ArrayList<MPerm>();
// Fill
for (MPerm mperm : this.getAll())
{
if (mperm.isRegistered() != registered) continue;
ret.add(mperm);
}
// Return
return ret;
}

View File

@ -85,13 +85,17 @@ public class MPlayerColl extends SenderColl<MPlayer>
public void clean()
{
// For each player ...
for (MPlayer mplayer : this.getAll())
{
// ... who doesn't have a valid faction ...
String factionId = mplayer.getFactionId();
if (FactionColl.get().containsId(factionId)) continue;
// ... reset their faction data ...
mplayer.resetFactionData();
// ... and log.
String message = Txt.parse("<i>Reset data for <h>%s <i>. Unknown factionId <h>%s", mplayer.getDisplayName(IdUtil.getConsole()), factionId);
Factions.get().log(message);
}
@ -112,8 +116,10 @@ public class MPlayerColl extends SenderColl<MPlayer>
@Override
public void run()
{
// For each offline player ...
for (MPlayer mplayer : mplayersOffline)
{
// ... see if they should be removed.
mplayer.considerRemovePlayerMillis(true);
}
}