From 39a02f0fd4e2c25032747ef6de539d8470cff876 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 22 Mar 2011 20:36:33 +0100 Subject: [PATCH] New auto leave on anactivity feature. --- src/com/bukkit/mcteam/factions/Conf.java | 2 + src/com/bukkit/mcteam/factions/FPlayer.java | 49 ++++++++++++++++++- src/com/bukkit/mcteam/factions/Factions.java | 21 ++++---- .../factions/commands/FCommandLeave.java | 24 +-------- .../listeners/FactionsPlayerListener.java | 10 +++- 5 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/Conf.java b/src/com/bukkit/mcteam/factions/Conf.java index b9238cff..fc1a2e20 100644 --- a/src/com/bukkit/mcteam/factions/Conf.java +++ b/src/com/bukkit/mcteam/factions/Conf.java @@ -53,6 +53,8 @@ public class Conf { public static boolean allowNoSlashCommand = true; + public static double autoLeaveFactionAfterDaysOfInactivity = 14; + static { territoryProtectedMaterials.add(Material.WOODEN_DOOR); territoryProtectedMaterials.add(Material.DISPENSER); diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index 98efd557..e713d74c 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -43,6 +43,7 @@ public class FPlayer { private String title; private double power; private long lastPowerUpdateTime; + private long lastLoginTime; private transient boolean mapAutoUpdating; private boolean factionChatting; @@ -55,6 +56,7 @@ public class FPlayer { this.resetFactionData(); this.power = this.getPowerMax(); this.lastPowerUpdateTime = System.currentTimeMillis(); + this.lastLoginTime = System.currentTimeMillis(); this.mapAutoUpdating = false; } @@ -126,6 +128,14 @@ public class FPlayer { this.factionChatting = factionChatting; } + public long getLastLoginTime() { + return lastLoginTime; + } + + public void setLastLoginTime(long lastLoginTime) { + this.lastLoginTime = lastLoginTime; + } + public boolean isMapAutoUpdating() { return mapAutoUpdating; } @@ -354,6 +364,32 @@ public class FPlayer { this.sendMessage(msg); } + // ------------------------------- + // Actions + // ------------------------------- + + public void leave() { + Faction myFaction = this.getFaction(); + + if (this.getRole() == Role.ADMIN && myFaction.getFPlayers().size() > 1) { + sendMessage("You must give the admin role to someone else first."); + return; + } + + myFaction.sendMessage(this.getNameAndRelevant(myFaction) + Conf.colorSystem + " left your faction."); + this.resetFactionData(); + + if (myFaction.getFPlayers().size() == 0) { + // Remove this faction + for (FPlayer fplayer : FPlayer.getAllOnline()) { + fplayer.sendMessage("The faction "+myFaction.getTag(fplayer)+Conf.colorSystem+" was disbanded."); + } + Faction.delete(myFaction.getId()); + } + FPlayer.save(); + FPlayer.save(); + } + // -------------------------------------------- // // Messages // -------------------------------------------- // @@ -476,6 +512,17 @@ public class FPlayer { fplayer.resetFactionData(); } } - } + } + + public static void autoLeaveOnInactivityRoutine() { + long now = System.currentTimeMillis(); + double toleranceMillis = Conf.autoLeaveFactionAfterDaysOfInactivity * 24 * 60 * 60 * 1000; + + for (FPlayer fplayer : FPlayer.getAll()) { + if (now - fplayer.getLastLoginTime() > toleranceMillis) { + fplayer.leave(); + } + } + } } \ No newline at end of file diff --git a/src/com/bukkit/mcteam/factions/Factions.java b/src/com/bukkit/mcteam/factions/Factions.java index 6976e93a..2674781a 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -77,6 +77,13 @@ public class Factions extends JavaPlugin { public Factions() { Factions.instance = this; + } + + + @Override + public void onEnable() { + log("=== INIT START ==="); + long timeInitStart = System.currentTimeMillis(); // Add the commands commands.add(new FCommandHelp()); @@ -102,24 +109,18 @@ public class Factions extends JavaPlugin { commands.add(new FCommandTitle()); commands.add(new FCommandUnclaim()); commands.add(new FCommandVersion()); - } - - - @Override - public void onEnable() { - log("=== INIT START ==="); - long timeInitStart = System.currentTimeMillis(); - - setupHelp(); - setupPermissions(); // Ensure basefolder exists! this.getDataFolder().mkdirs(); + Conf.load(); FPlayer.load(); Faction.load(); Board.load(); + setupHelp(); + setupPermissions(); + // Register events PluginManager pm = this.getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java index 18947ba2..213f97c1 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandLeave.java @@ -2,11 +2,6 @@ package com.bukkit.mcteam.factions.commands; import java.util.ArrayList; -import com.bukkit.mcteam.factions.Conf; -import com.bukkit.mcteam.factions.FPlayer; -import com.bukkit.mcteam.factions.Faction; -import com.bukkit.mcteam.factions.struct.Role; - public class FCommandLeave extends FBaseCommand { public FCommandLeave() { @@ -28,24 +23,7 @@ public class FCommandLeave extends FBaseCommand { return; } - Faction faction = me.getFaction(); - - if (me.getRole() == Role.ADMIN && faction.getFPlayers().size() > 1) { - sendMessage("You must give the admin role to someone else first."); - return; - } - - faction.sendMessage(me.getNameAndRelevant(faction) + Conf.colorSystem + " left your faction."); - me.resetFactionData(); - FPlayer.save(); - - if (faction.getFPlayers().size() == 0) { - // Remove this faction - for (FPlayer fplayer : FPlayer.getAllOnline()) { - fplayer.sendMessage("The faction "+faction.getTag(fplayer)+Conf.colorSystem+" was disbanded."); - } - Faction.delete(faction.getId()); - } + me.leave(); } } diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 5ba13634..7f7ada68 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -91,9 +91,17 @@ public class FactionsPlayerListener extends PlayerListener{ @Override public void onPlayerJoin(PlayerEvent event) { // Make sure that all online players do have a fplayer. - FPlayer.get(event.getPlayer()); + FPlayer me = FPlayer.get(event.getPlayer()); + + // Update the lastLoginTime for this fplayer + me.setLastLoginTime(System.currentTimeMillis()); + + // Run the member auto kick routine. Twice to getToTheAdmins... + FPlayer.autoLeaveOnInactivityRoutine(); + FPlayer.autoLeaveOnInactivityRoutine(); } + @Override public void onPlayerQuit(PlayerEvent event) { // Save all players on player quit.