From b13c77b6c527f040c1bf0f5425b99d8033aa25b1 Mon Sep 17 00:00:00 2001 From: TheComputerGeek2 Date: Sun, 12 Mar 2017 09:55:01 -0700 Subject: [PATCH] Clean up the Coll's --- .../factions/entity/BoardColl.java | 71 ++++++++++++------- .../factions/entity/FactionColl.java | 61 +++++++++++----- .../factions/entity/MConfColl.java | 2 +- .../factions/entity/MFlagColl.java | 7 +- .../factions/entity/MPermColl.java | 7 +- .../factions/entity/MPlayerColl.java | 6 ++ 6 files changed, 110 insertions(+), 44 deletions(-) diff --git a/src/com/massivecraft/factions/entity/BoardColl.java b/src/com/massivecraft/factions/entity/BoardColl.java index 51682f5f..1193d526 100644 --- a/src/com/massivecraft/factions/entity/BoardColl.java +++ b/src/com/massivecraft/factions/entity/BoardColl.java @@ -131,29 +131,42 @@ public class BoardColl extends Coll implements BoardInterface @Override public Set getChunks(Faction faction) { + // Create Set ret = new HashSet(); + + // Fill for (Board board : this.getAll()) { ret.addAll(board.getChunks(faction)); } + + // Return return ret; } @Override public Set getChunks(String factionId) { + // Create Set ret = new HashSet(); + + // Fill for (Board board : this.getAll()) { ret.addAll(board.getChunks(factionId)); } + + // Return return ret; } @Override public Map> getFactionToChunks() { + // Create Map> ret = null; + + // Fill for (Board board : this.getAll()) { // Use the first board directly @@ -180,7 +193,10 @@ public class BoardColl extends Coll implements BoardInterface } } + // Enforce create if (ret == null) ret = new MassiveMap>(); + + // Return return ret; } @@ -250,8 +266,7 @@ public class BoardColl extends Coll 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 implements BoardInterface public Set getClaimedWorlds(String factionId) { + // Create Set 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 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 getNearbyChunks(PS chunk, int distance) + public static Set 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 ret = new LinkedHashSet(); - 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 implements BoardInterface // Fix Args if (chunks == null) throw new NullPointerException("chunks"); - // Create Ret + // Create Set ret = new LinkedHashSet(); 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 implements BoardInterface // Fix Args if (chunks == null) throw new NullPointerException("chunks"); - // Create Ret + // Create Set ret = new LinkedHashSet(); - // 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 getChunkFaction(Collection chunks) { + // Create Map ret = new LinkedHashMap(); + // 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; } diff --git a/src/com/massivecraft/factions/entity/FactionColl.java b/src/com/massivecraft/factions/entity/FactionColl.java index f61f87b5..66f4140d 100644 --- a/src/com/massivecraft/factions/entity/FactionColl.java +++ b/src/com/massivecraft/factions/entity/FactionColl.java @@ -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 { @@ -37,7 +42,7 @@ public class FactionColl extends Coll { super.setActive(active); - if ( ! active) return; + if (!active) return; this.createSpecialFactions(); } @@ -197,25 +202,40 @@ public class FactionColl extends Coll 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 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) { - List players = faction.getMPlayers(); - int playerCount = players.size(); - double reward = econLandReward * landCount / playerCount; - for (MPlayer player : players) - { - Econ.modifyMoney(player, reward, "own " + landCount + " faction land divided among " + playerCount + " members"); - } + Econ.modifyMoney(player, reward, description); } + } } @@ -225,26 +245,32 @@ public class FactionColl extends Coll public ArrayList validateName(String str) { + // Create ArrayList errors = new ArrayList(); + // Fill + // Check minimum length if (MiscUtil.getComparisonString(str).length() < MConf.get().factionNameLengthMin) { errors.add(Txt.parse("The faction name can't be shorter than %s chars.", MConf.get().factionNameLengthMin)); } + // Check maximum length if (str.length() > MConf.get().factionNameLengthMax) { errors.add(Txt.parse("The faction name can't be longer than %s chars.", MConf.get().factionNameLengthMax)); } + // Check characters used for (char c : str.toCharArray()) { - if ( ! MiscUtil.substanceChars.contains(String.valueOf(c))) + if (!MiscUtil.substanceChars.contains(String.valueOf(c))) { errors.add(Txt.parse("Faction name must be alphanumeric. \"%s\" is not allowed.", c)); } } + // Return return errors; } @@ -270,7 +296,8 @@ public class FactionColl extends Coll { // Create Map> ret = new LinkedHashMap>(); - boolean peaceful = faction.getFlag(MFlag.getFlagPeaceful()); + MFlag flagPeaceful = MFlag.getFlagPeaceful(); + boolean peaceful = faction.getFlag(flagPeaceful); for (Rel rel : rels) { ret.put(rel, new ArrayList()); @@ -279,7 +306,7 @@ public class FactionColl extends Coll // Fill for (Faction fac : FactionColl.get().getAll()) { - if (fac.getFlag(MFlag.getFlagPeaceful())) continue; + if (fac.getFlag(flagPeaceful)) continue; Rel rel = fac.getRelationTo(faction); List names = ret.get(rel); @@ -289,8 +316,8 @@ public class FactionColl extends Coll names.add(name); } - // Replace TRUCE if peasceful - if ( ! peaceful) return ret; + // Replace TRUCE if peaceful + if (!peaceful) return ret; List names = ret.get(Rel.TRUCE); if (names == null) return ret; diff --git a/src/com/massivecraft/factions/entity/MConfColl.java b/src/com/massivecraft/factions/entity/MConfColl.java index ee4b683c..3ae6ce9d 100644 --- a/src/com/massivecraft/factions/entity/MConfColl.java +++ b/src/com/massivecraft/factions/entity/MConfColl.java @@ -30,7 +30,7 @@ public class MConfColl extends Coll public void setActive(boolean active) { super.setActive(active); - if ( ! active) return; + if (!active) return; MConf.i = this.get(MassiveCore.INSTANCE, true); } diff --git a/src/com/massivecraft/factions/entity/MFlagColl.java b/src/com/massivecraft/factions/entity/MFlagColl.java index 80f37d0c..9f8e030b 100644 --- a/src/com/massivecraft/factions/entity/MFlagColl.java +++ b/src/com/massivecraft/factions/entity/MFlagColl.java @@ -36,7 +36,7 @@ public class MFlagColl extends Coll public void setActive(boolean active) { super.setActive(active); - if ( ! active) return; + if (!active) return; MFlag.setupStandardFlags(); } @@ -46,12 +46,17 @@ public class MFlagColl extends Coll public List getAll(boolean registered) { + // Create List ret = new ArrayList(); + + // Fill for (MFlag mflag : this.getAll()) { if (mflag.isRegistered() != registered) continue; ret.add(mflag); } + + // Return return ret; } diff --git a/src/com/massivecraft/factions/entity/MPermColl.java b/src/com/massivecraft/factions/entity/MPermColl.java index 16bcc5be..5c2c2c73 100644 --- a/src/com/massivecraft/factions/entity/MPermColl.java +++ b/src/com/massivecraft/factions/entity/MPermColl.java @@ -36,7 +36,7 @@ public class MPermColl extends Coll public void setActive(boolean active) { super.setActive(active); - if ( ! active) return; + if (!active) return; MPerm.setupStandardPerms(); } @@ -46,12 +46,17 @@ public class MPermColl extends Coll public List getAll(boolean registered) { + // Create List ret = new ArrayList(); + + // Fill for (MPerm mperm : this.getAll()) { if (mperm.isRegistered() != registered) continue; ret.add(mperm); } + + // Return return ret; } diff --git a/src/com/massivecraft/factions/entity/MPlayerColl.java b/src/com/massivecraft/factions/entity/MPlayerColl.java index de2b4b0a..656a7051 100644 --- a/src/com/massivecraft/factions/entity/MPlayerColl.java +++ b/src/com/massivecraft/factions/entity/MPlayerColl.java @@ -85,13 +85,17 @@ public class MPlayerColl extends SenderColl 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("Reset data for %s . Unknown factionId %s", mplayer.getDisplayName(IdUtil.getConsole()), factionId); Factions.get().log(message); } @@ -112,8 +116,10 @@ public class MPlayerColl extends SenderColl @Override public void run() { + // For each offline player ... for (MPlayer mplayer : mplayersOffline) { + // ... see if they should be removed. mplayer.considerRemovePlayerMillis(true); } }