From bd8d945c7b83a9758a56b38ab01bf7b6e736594f Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 24 Apr 2013 11:30:00 +0200 Subject: [PATCH] Decoupled LWC integration. This is a good example of what integrations should look like. We are using events only. For this reason the LWC integration could be placed in another plugin and this is good practice. Im not saying we should move the LWC integration to another plugin but its good practice that its possible. --- src/com/massivecraft/factions/Factions.java | 8 +- .../massivecraft/factions/entity/Board.java | 10 +- .../massivecraft/factions/entity/UConf.java | 8 +- .../massivecraft/factions/entity/UPlayer.java | 7 -- .../factions/integration/LWCFeatures.java | 88 -------------- .../factions/integration/lwc/LwcEngine.java | 115 ++++++++++++++++++ .../factions/integration/lwc/LwcFeatures.java | 31 +++++ 7 files changed, 158 insertions(+), 109 deletions(-) delete mode 100644 src/com/massivecraft/factions/integration/LWCFeatures.java create mode 100644 src/com/massivecraft/factions/integration/lwc/LwcEngine.java create mode 100644 src/com/massivecraft/factions/integration/lwc/LwcFeatures.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 54e0b162..8536c024 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -24,9 +24,9 @@ import com.massivecraft.factions.entity.BoardColls; import com.massivecraft.factions.entity.UPlayerColls; import com.massivecraft.factions.entity.FactionColls; import com.massivecraft.factions.entity.MConfColl; -import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.Worldguard; import com.massivecraft.factions.integration.herochat.HerochatFeatures; +import com.massivecraft.factions.integration.lwc.LwcFeatures; import com.massivecraft.factions.listeners.FactionsListenerChat; import com.massivecraft.factions.listeners.FactionsListenerEcon; import com.massivecraft.factions.listeners.FactionsListenerExploit; @@ -157,9 +157,11 @@ public class Factions extends MPlugin ChatTagTitle.get().register(); // Integrate - this.integrate(HerochatFeatures.get()); + this.integrate( + HerochatFeatures.get(), + LwcFeatures.get() + ); - LWCFeatures.setup(); Worldguard.init(this); postEnable(); diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index 15cb5472..91bc1001 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -16,7 +16,6 @@ import com.massivecraft.factions.Const; import com.massivecraft.factions.Factions; import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.TerritoryAccess; -import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.util.AsciiCompass; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; @@ -110,14 +109,7 @@ public class Board extends Entity implements BoardInterface ps = ps.getChunkCoords(true); if (territoryAccess == null || (territoryAccess.getHostFactionId().equals(Const.FACTIONID_NONE) && territoryAccess.isDefault())) - { - // TODO: Listen to an event instead! - // NOTE: And this is probably the place where the event should be triggered! - if (UConf.get(ps).lwcRemoveOnUnclaim && LWCFeatures.getEnabled()) - { - LWCFeatures.clearAllProtections(ps); - } - + { this.map.remove(ps); } else diff --git a/src/com/massivecraft/factions/entity/UConf.java b/src/com/massivecraft/factions/entity/UConf.java index 8342e44d..855bceea 100644 --- a/src/com/massivecraft/factions/entity/UConf.java +++ b/src/com/massivecraft/factions/entity/UConf.java @@ -125,8 +125,12 @@ public class UConf extends Entity // INTEGRATION: LWC // -------------------------------------------- // - public boolean lwcRemoveOnUnclaim = false; - public boolean lwcRemoveOnCapture = false; + public Map lwcRemoveOnChange = MUtil.map( + FactionsEventChunkChangeType.BUY, false, + FactionsEventChunkChangeType.SELL, false, + FactionsEventChunkChangeType.CONQUER, false, + FactionsEventChunkChangeType.PILLAGE, false + ); // -------------------------------------------- // // INTEGRATION: ECONOMY diff --git a/src/com/massivecraft/factions/entity/UPlayer.java b/src/com/massivecraft/factions/entity/UPlayer.java index 9c79f888..ba39ad5f 100644 --- a/src/com/massivecraft/factions/entity/UPlayer.java +++ b/src/com/massivecraft/factions/entity/UPlayer.java @@ -18,7 +18,6 @@ import com.massivecraft.factions.event.FactionsEventChunkChange; import com.massivecraft.factions.event.FactionsEventMembershipChange; import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.Worldguard; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.mcore.mixin.Mixin; @@ -649,12 +648,6 @@ public class UPlayer extends SenderEntity implements EconomyParticipato event.run(); if (event.isCancelled()) return false; - // TODO: The LWC integration should listen to Monitor for the claim event. - if (LWCFeatures.getEnabled() && forFaction.isNormal() && UConf.get(forFaction).lwcRemoveOnCapture) - { - LWCFeatures.clearOtherProtections(psChunk, this.getFaction()); - } - // announce success Set informTheseUPlayers = new HashSet(); informTheseUPlayers.add(this); diff --git a/src/com/massivecraft/factions/integration/LWCFeatures.java b/src/com/massivecraft/factions/integration/LWCFeatures.java deleted file mode 100644 index 3c67c560..00000000 --- a/src/com/massivecraft/factions/integration/LWCFeatures.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.massivecraft.factions.integration; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.plugin.Plugin; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; - -import com.griefcraft.lwc.LWC; -import com.griefcraft.lwc.LWCPlugin; -import com.griefcraft.model.Protection; -import com.massivecraft.factions.Factions; -import com.massivecraft.factions.entity.UPlayer; -import com.massivecraft.factions.entity.Faction; -import com.massivecraft.mcore.ps.PS; - -public class LWCFeatures -{ - private static LWC lwc; - - public static void setup() - { - Plugin test = Bukkit.getServer().getPluginManager().getPlugin("LWC"); - if(test == null || !test.isEnabled()) return; - - lwc = ((LWCPlugin)test).getLWC(); - Factions.get().log("Successfully hooked into LWC!"); - } - - public static boolean getEnabled() - { - return lwc != null; - } - - public static void clearAllProtections(PS chunkPs) - { - for (Protection protection : getProtectionsInChunk(chunkPs)) - { - protection.remove(); - } - } - - public static void clearOtherProtections(PS chunkPs, Faction faction) - { - for (Protection protection : getProtectionsInChunk(chunkPs)) - { - UPlayer owner = UPlayer.get(protection.getOwner()); - if (faction.getUPlayers().contains(owner)) continue; - protection.remove(); - } - } - - public static List getProtectionsInChunk(PS chunkPs) - { - List ret = new ArrayList(); - - // Get the chunk - Chunk chunk = null; - try - { - chunk = chunkPs.asBukkitChunk(true); - } - catch (Exception e) - { - return ret; - } - - for (BlockState blockState : chunk.getTileEntities()) - { - // TODO: Can something else be protected by LWC? Or is it really only chests? - // TODO: How about we run through each block in the chunk just to be on the safe side? - if (blockState.getType() != Material.CHEST) continue; - Block block = blockState.getBlock(); - - Protection protection = lwc.findProtection(block); - if (protection == null) continue; - - ret.add(protection); - } - - return ret; - } - -} diff --git a/src/com/massivecraft/factions/integration/lwc/LwcEngine.java b/src/com/massivecraft/factions/integration/lwc/LwcEngine.java new file mode 100644 index 00000000..cdcda183 --- /dev/null +++ b/src/com/massivecraft/factions/integration/lwc/LwcEngine.java @@ -0,0 +1,115 @@ +package com.massivecraft.factions.integration.lwc; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Chunk; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; + +import com.griefcraft.lwc.LWC; +import com.griefcraft.model.Protection; +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.UConf; +import com.massivecraft.factions.entity.UPlayer; +import com.massivecraft.factions.event.FactionsEventChunkChange; +import com.massivecraft.factions.event.FactionsEventChunkChangeType; +import com.massivecraft.mcore.ps.PS; + + +public class LwcEngine implements Listener +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static LwcEngine i = new LwcEngine(); + public static LwcEngine get() { return i; } + private LwcEngine() {} + + // -------------------------------------------- // + // ACTIVATE & DEACTIVATE + // -------------------------------------------- // + + public void activate() + { + Bukkit.getPluginManager().registerEvents(this, Factions.get()); + } + + public void deactivate() + { + HandlerList.unregisterAll(this); + } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void removeProtectionsOnChunkChange(FactionsEventChunkChange event) + { + // If we are supposed to clear at this chunk change type ... + Faction newFaction = event.getNewFaction(); + UConf uconf = UConf.get(newFaction); + FactionsEventChunkChangeType type = event.getType(); + Boolean remove = uconf.lwcRemoveOnChange.get(type); + if (remove == null) return; + if (remove == false) return; + + // ... then remove for all other factions than the new one. + removeAlienProtections(event.getChunk(), newFaction); + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public static void removeAlienProtections(PS chunkPs, Faction faction) + { + for (Protection protection : getProtectionsInChunk(chunkPs)) + { + UPlayer owner = UPlayer.get(protection.getOwner()); + if (faction.getUPlayers().contains(owner)) continue; + protection.remove(); + } + } + + public static List getProtectionsInChunk(PS chunkPs) + { + List ret = new ArrayList(); + + // Get the chunk + Chunk chunk = null; + try + { + chunk = chunkPs.asBukkitChunk(true); + } + catch (Exception e) + { + return ret; + } + + for (BlockState blockState : chunk.getTileEntities()) + { + // TODO: Can something else be protected by LWC? Or is it really only chests? + // TODO: How about we run through each block in the chunk just to be on the safe side? + if (blockState.getType() != Material.CHEST) continue; + Block block = blockState.getBlock(); + + Protection protection = LWC.getInstance().findProtection(block); + if (protection == null) continue; + + ret.add(protection); + } + + return ret; + } + +} diff --git a/src/com/massivecraft/factions/integration/lwc/LwcFeatures.java b/src/com/massivecraft/factions/integration/lwc/LwcFeatures.java new file mode 100644 index 00000000..1e1b524c --- /dev/null +++ b/src/com/massivecraft/factions/integration/lwc/LwcFeatures.java @@ -0,0 +1,31 @@ +package com.massivecraft.factions.integration.lwc; + +import com.massivecraft.mcore.integration.IntegrationFeaturesAbstract; + +public class LwcFeatures extends IntegrationFeaturesAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static LwcFeatures i = new LwcFeatures(); + public static LwcFeatures get() { return i; } + private LwcFeatures() { super("LWC"); } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void activate() + { + LwcEngine.get().activate(); + } + + @Override + public void deactivate() + { + LwcEngine.get().deactivate(); + } + +}