From 59112577b89d352876c066df9bbfbb62848a5c63 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 24 Mar 2017 20:19:04 +0100 Subject: [PATCH] Add clean command --- plugin.yml | 3 ++ src/com/massivecraft/factions/Perm.java | 1 + .../factions/cmd/CmdFactions.java | 2 ++ .../factions/cmd/CmdFactionsClean.java | 36 +++++++++++++++++++ .../massivecraft/factions/entity/Board.java | 10 +++++- .../factions/entity/BoardColl.java | 8 +++-- .../factions/entity/BoardInterface.java | 2 +- .../factions/entity/MPlayerColl.java | 9 ++++- 8 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsClean.java diff --git a/plugin.yml b/plugin.yml index d0deca9a..57ae179a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -89,6 +89,7 @@ permissions: factions.unsethome: {description: unset faction home, default: false} factions.unstuck: {description: teleport to nearest wilderness, default: false} factions.config: {description: edit the factions config, default: false} + factions.clean: {description: clean the factions database, default: false} factions.version: {description: see plugin version, default: false} # -------------------------------------------- # # STAR NOTATION @@ -177,6 +178,7 @@ permissions: factions.unsethome: true factions.unstuck: true factions.config: true + factions.clean: true factions.version: true # -------------------------------------------- # # KITS @@ -190,6 +192,7 @@ permissions: children: factions.kit.rank2: true factions.config: true + factions.clean: true factions.kit.rank2: default: false children: diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 7bd45e67..aa764bc4 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -89,6 +89,7 @@ public enum Perm implements Identified UNSETHOME, UNSTUCK, CONFIG, + CLEAN, VERSION, // END OF LIST diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index ce5d7c51..e23afc4f 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -69,6 +69,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsPowerBoost cmdFactionsPowerBoost = new CmdFactionsPowerBoost(); public CmdFactionsSetpower cmdFactionsSetpower = new CmdFactionsSetpower(); public CmdFactionsConfig cmdFactionsConfig = new CmdFactionsConfig(); + public CmdFactionsClean cmdFactionsClean = new CmdFactionsClean(); public MassiveCommandVersion cmdFactionsVersion = new MassiveCommandVersion(Factions.get()).setAliases("v", "version").addRequirements(RequirementHasPerm.get(Perm.VERSION)); // -------------------------------------------- // @@ -125,6 +126,7 @@ public class CmdFactions extends FactionsCommand this.addChild(this.cmdFactionsPowerBoost); this.addChild(this.cmdFactionsSetpower); this.addChild(this.cmdFactionsConfig); + this.addChild(this.cmdFactionsClean); this.addChild(this.cmdFactionsVersion); // Deprecated Commands diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsClean.java b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java new file mode 100644 index 00000000..dda1fd45 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java @@ -0,0 +1,36 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.entity.BoardColl; +import com.massivecraft.factions.entity.MPlayerColl; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsClean extends FactionsCommand +{ + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + Object message; + + // Apply + int chunks = BoardColl.get().clean(); + int players = MPlayerColl.get().clean(); + + // Title + message = Txt.titleize("Factions Cleaner Results"); + message(message); + + // Chunks + message = Txt.parse("%d chunks were cleaned.", chunks); + message(message); + + // Players + message = Txt.parse("%d players were cleaned.", players); + message(message); + } + +} diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index 727527c2..479fff36 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -159,8 +159,12 @@ public class Board extends Entity implements BoardInterface // Removes orphaned foreign keys @Override - public void clean() + public int clean() { + int ret = 0; + + if (!FactionColl.get().isActive()) return ret; + for (Entry entry : this.map.entrySet()) { TerritoryAccess territoryAccess = entry.getValue(); @@ -171,8 +175,12 @@ public class Board extends Entity implements BoardInterface PS ps = entry.getKey(); this.removeAt(ps); + ret += 0; + Factions.get().log("Board cleaner removed "+factionId+" from "+ps); } + + return ret; } // CHUNKS diff --git a/src/com/massivecraft/factions/entity/BoardColl.java b/src/com/massivecraft/factions/entity/BoardColl.java index 8e1f3e20..23c74db3 100644 --- a/src/com/massivecraft/factions/entity/BoardColl.java +++ b/src/com/massivecraft/factions/entity/BoardColl.java @@ -118,12 +118,16 @@ public class BoardColl extends Coll implements BoardInterface } @Override - public void clean() + public int clean() { + int ret = 0; + for (Board board : this.getAll()) { - board.clean(); + ret += board.clean(); } + + return ret; } // CHUNKS diff --git a/src/com/massivecraft/factions/entity/BoardInterface.java b/src/com/massivecraft/factions/entity/BoardInterface.java index 1a158089..b31993f7 100644 --- a/src/com/massivecraft/factions/entity/BoardInterface.java +++ b/src/com/massivecraft/factions/entity/BoardInterface.java @@ -21,7 +21,7 @@ public interface BoardInterface // REMOVE void removeAt(PS ps); void removeAll(Faction faction); - void clean(); + int clean(); // CHUNKS Set getChunks(Faction faction); diff --git a/src/com/massivecraft/factions/entity/MPlayerColl.java b/src/com/massivecraft/factions/entity/MPlayerColl.java index effacea9..5091f8df 100644 --- a/src/com/massivecraft/factions/entity/MPlayerColl.java +++ b/src/com/massivecraft/factions/entity/MPlayerColl.java @@ -82,8 +82,12 @@ public class MPlayerColl extends SenderColl // EXTRAS // -------------------------------------------- // - public void clean() + public int clean() { + int ret = 0; + + if (!FactionColl.get().isActive()) return ret; + // For each player ... for (MPlayer mplayer : this.getAll()) { @@ -93,11 +97,14 @@ public class MPlayerColl extends SenderColl // ... reset their faction data ... mplayer.resetFactionData(); + ret += 1; // ... and log. String message = Txt.parse("Reset data for %s . Unknown factionId %s", mplayer.getDisplayName(IdUtil.getConsole()), factionId); Factions.get().log(message); } + + return ret; } public void considerRemovePlayerMillis()