From f8d7e50828d5f2719530366a9df98731324d979a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 19 Nov 2014 11:24:36 +0100 Subject: [PATCH] Ensure recreation of faction flags and perms on getAll and every 3 seconds to allow for runtime database deletion without server crash. --- .../com/massivecraft/factions/Factions.java | 2 + .../massivecraft/factions/entity/MFlag.java | 8 ++++ .../massivecraft/factions/entity/MPerm.java | 9 +++++ .../factions/event/EventFactionsAbstract.java | 15 ++++++- .../event/EventFactionsCreateFlags.java | 32 +++++++++++++++ .../event/EventFactionsCreatePerms.java | 18 +++++++++ .../factions/task/TaskFlagPermCreate.java | 40 +++++++++++++++++++ 7 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/massivecraft/factions/event/EventFactionsCreateFlags.java create mode 100644 src/main/java/com/massivecraft/factions/event/EventFactionsCreatePerms.java create mode 100644 src/main/java/com/massivecraft/factions/task/TaskFlagPermCreate.java diff --git a/src/main/java/com/massivecraft/factions/Factions.java b/src/main/java/com/massivecraft/factions/Factions.java index 10ded88c..8b9e7bcc 100644 --- a/src/main/java/com/massivecraft/factions/Factions.java +++ b/src/main/java/com/massivecraft/factions/Factions.java @@ -39,6 +39,7 @@ import com.massivecraft.factions.integration.herochat.IntegrationHerochat; import com.massivecraft.factions.integration.lwc.IntegrationLwc; import com.massivecraft.factions.mixin.PowerMixin; import com.massivecraft.factions.mixin.PowerMixinDefault; +import com.massivecraft.factions.task.TaskFlagPermCreate; import com.massivecraft.factions.task.TaskPlayerDataRemove; import com.massivecraft.factions.task.TaskEconLandReward; import com.massivecraft.factions.task.TaskPlayerPowerUpdate; @@ -161,6 +162,7 @@ public class Factions extends MassivePlugin TaskPlayerPowerUpdate.get().activate(this); TaskPlayerDataRemove.get().activate(this); TaskEconLandReward.get().activate(this); + TaskFlagPermCreate.get().activate(this); // Register built in chat modifiers ChatModifierLc.get().register(); diff --git a/src/main/java/com/massivecraft/factions/entity/MFlag.java b/src/main/java/com/massivecraft/factions/entity/MFlag.java index 768bd25e..eb5d06d8 100644 --- a/src/main/java/com/massivecraft/factions/entity/MFlag.java +++ b/src/main/java/com/massivecraft/factions/entity/MFlag.java @@ -2,6 +2,7 @@ package com.massivecraft.factions.entity; import java.util.List; +import com.massivecraft.factions.event.EventFactionsCreateFlags; import com.massivecraft.massivecore.PredictateIsRegistered; import com.massivecraft.massivecore.Prioritized; import com.massivecraft.massivecore.Registerable; @@ -52,6 +53,13 @@ public class MFlag extends Entity implements Prioritized, Registerable public static List getAll() { + return getAll(false); + } + + public static List getAll(boolean isAsync) + { + setupStandardFlags(); + new EventFactionsCreateFlags(isAsync).run(); return MFlagColl.get().getAll(PredictateIsRegistered.get()); } diff --git a/src/main/java/com/massivecraft/factions/entity/MPerm.java b/src/main/java/com/massivecraft/factions/entity/MPerm.java index c9e61f59..ec4aa373 100644 --- a/src/main/java/com/massivecraft/factions/entity/MPerm.java +++ b/src/main/java/com/massivecraft/factions/entity/MPerm.java @@ -10,6 +10,7 @@ import com.massivecraft.factions.Factions; import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; import com.massivecraft.factions.TerritoryAccess; +import com.massivecraft.factions.event.EventFactionsCreatePerms; import com.massivecraft.massivecore.PredictateIsRegistered; import com.massivecraft.massivecore.Prioritized; import com.massivecraft.massivecore.Registerable; @@ -85,6 +86,14 @@ public class MPerm extends Entity implements Prioritized, Registerable public static List getAll() { + return getAll(false); + } + + public static List getAll(boolean isAsync) + { + setupStandardPerms(); + new EventFactionsCreatePerms().run(); + return MPermColl.get().getAll(PredictateIsRegistered.get()); } diff --git a/src/main/java/com/massivecraft/factions/event/EventFactionsAbstract.java b/src/main/java/com/massivecraft/factions/event/EventFactionsAbstract.java index 1f309537..5e20b0f9 100644 --- a/src/main/java/com/massivecraft/factions/event/EventFactionsAbstract.java +++ b/src/main/java/com/massivecraft/factions/event/EventFactionsAbstract.java @@ -4,5 +4,18 @@ import com.massivecraft.massivecore.event.EventMassiveCore; public abstract class EventFactionsAbstract extends EventMassiveCore { - + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public EventFactionsAbstract() + { + + } + + public EventFactionsAbstract(boolean isAsync) + { + super(isAsync); + } + } diff --git a/src/main/java/com/massivecraft/factions/event/EventFactionsCreateFlags.java b/src/main/java/com/massivecraft/factions/event/EventFactionsCreateFlags.java new file mode 100644 index 00000000..b1ded42a --- /dev/null +++ b/src/main/java/com/massivecraft/factions/event/EventFactionsCreateFlags.java @@ -0,0 +1,32 @@ +package com.massivecraft.factions.event; + +import org.bukkit.event.HandlerList; + +/** + * External plugins that add Faction flags should make sure they exist when this event is called. + */ +public class EventFactionsCreateFlags extends EventFactionsAbstract +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public EventFactionsCreateFlags() + { + + } + + public EventFactionsCreateFlags(boolean isAsync) + { + super(isAsync); + } + +} diff --git a/src/main/java/com/massivecraft/factions/event/EventFactionsCreatePerms.java b/src/main/java/com/massivecraft/factions/event/EventFactionsCreatePerms.java new file mode 100644 index 00000000..16034785 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/event/EventFactionsCreatePerms.java @@ -0,0 +1,18 @@ +package com.massivecraft.factions.event; + +import org.bukkit.event.HandlerList; + +/** + * External plugins that add Faction perms should make sure they exist when this event is called. + */ +public class EventFactionsCreatePerms extends EventFactionsAbstract +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + +} diff --git a/src/main/java/com/massivecraft/factions/task/TaskFlagPermCreate.java b/src/main/java/com/massivecraft/factions/task/TaskFlagPermCreate.java new file mode 100644 index 00000000..3db342ba --- /dev/null +++ b/src/main/java/com/massivecraft/factions/task/TaskFlagPermCreate.java @@ -0,0 +1,40 @@ +package com.massivecraft.factions.task; + +import com.massivecraft.factions.entity.MFlag; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.massivecore.ModuloRepeatTask; +import com.massivecraft.massivecore.util.TimeUnit; + +public class TaskFlagPermCreate extends ModuloRepeatTask +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TaskFlagPermCreate i = new TaskFlagPermCreate(); + public static TaskFlagPermCreate get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: MODULO REPEAT TASK + // -------------------------------------------- // + + @Override + public long getDelayMillis() + { + return TimeUnit.MILLIS_PER_SECOND * 3; + } + + @Override + public void setDelayMillis(long delayMillis) + { + + } + + @Override + public void invoke(long now) + { + MPerm.getAll(); + MFlag.getAll(); + } + +}