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.

This commit is contained in:
Olof Larsson
2013-04-24 11:30:00 +02:00
parent 05da06594a
commit bd8d945c7b
7 changed files with 158 additions and 109 deletions

View File

@ -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<Protection> getProtectionsInChunk(PS chunkPs)
{
List<Protection> ret = new ArrayList<Protection>();
// 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;
}
}

View File

@ -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<Protection> getProtectionsInChunk(PS chunkPs)
{
List<Protection> ret = new ArrayList<Protection>();
// 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;
}
}

View File

@ -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();
}
}