diff --git a/pom.xml b/pom.xml index bf35017..7435a01 100644 --- a/pom.xml +++ b/pom.xml @@ -16,14 +16,18 @@ - - essentials-repo - http://ess.ementalo.com/nexus/content/groups/public/ - repobo-snap http://repo.bukkit.org/content/groups/public + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + cindyscats + http://basket.cindyscats.com/content/repositories/thirdparty/ + @@ -71,10 +75,21 @@ AnnotatedConfig 0.1 + + org.apache.commons + commons-io + 1.3.2 + + + com.palmergames + Towny + 0.89.0.0 + org.projectlombok lombok 0.10.8 + diff --git a/src/main/java/TownyListener.java b/src/main/java/TownyListener.java new file mode 100644 index 0000000..499ac8c --- /dev/null +++ b/src/main/java/TownyListener.java @@ -0,0 +1,263 @@ +import com.daemitus.deadbolt.DeadboltPlugin; +import com.daemitus.deadbolt.listener.DeadboltListener; +import com.daemitus.deadbolt.Config; +import com.daemitus.deadbolt.Deadbolt; +import com.daemitus.deadbolt.Deadbolted; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.TownBlock; +import com.palmergames.bukkit.towny.object.TownyPermission; +import com.palmergames.bukkit.towny.object.TownyUniverse; +import com.palmergames.bukkit.towny.object.TownyWorld; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.block.Block; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +public final class TownyListener extends DeadboltListener { + + private TownyUniverse towny; + private static final String patternBracketTooLong = "\\[.{14,}\\]"; + private static boolean denyWilderness = false; + private static boolean wildernessOverride = false; + private static boolean mayorOverride = false; + private static boolean assistantOverride = false; + DeadboltPlugin plugin; + + @Override + public List getDependencies() { + return Arrays.asList("Towny"); + } + + @Override + public void load(final DeadboltPlugin plugin) { + try { + towny = ((Towny) Bukkit.getServer().getPluginManager().getPlugin("Towny")).getTownyUniverse(); + this.plugin = plugin; + + File configFile = new File(plugin.getDataFolder() + "/listeners/TownyListener.yml"); + checkFile(configFile); + YamlConfiguration config = new YamlConfiguration(); + config.load(configFile); + denyWilderness = config.getBoolean("deny_wilderness", denyWilderness); + wildernessOverride = config.getBoolean("wilderness_override", wildernessOverride); + mayorOverride = config.getBoolean("mayor_override", mayorOverride); + assistantOverride = config.getBoolean("assistant_override", assistantOverride); + } catch (FileNotFoundException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } catch (IOException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } catch (InvalidConfigurationException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } + } + + private String truncate(String text) { + if (text.matches(patternBracketTooLong)) + return "[" + text.substring(1, 14) + "]"; + return text; + } + + private void checkFile(File file) { + try { + if (!file.exists()) { + file.createNewFile(); + FileWriter fw = new FileWriter(file); + fw.write("deny_wilderness: false #Denies protecting of blocks in the wild\n"); + fw.write("wilderness_override: false #Allows ANYONE to break open locks in the wilderness\n"); + fw.write("mayor_override: false #Allows Mayors to break open locks in their town\n"); + fw.write("assistant_override: false #Allows Assistants to break open locks in their town\n"); + fw.close(); + plugin.getLogger().log(Level.INFO, "[Deadbolt][" + this.getClass().getSimpleName() + "] Retrieved file " + file.getName()); + } + } catch (IOException ex) { + plugin.getLogger().log(Level.SEVERE, "[Deadbolt][" + this.getClass().getSimpleName() + "] Error retrieving " + file.getName()); + } + } + + @Override + public boolean canPlayerInteract(Deadbolted db, PlayerInteractEvent event) { + Player player = event.getPlayer(); + Block block = event.getClickedBlock(); + try { + for (TownyWorld tw : towny.getWorlds()) { + if (tw.getName().equalsIgnoreCase(player.getWorld().getName()) && tw.isUsingTowny()) { + Resident resident = towny.getResident(player.getName()); + + Town town = resident.getTown(); + if (db.getUsers().contains(truncate("[" + town.getName().toLowerCase() + "]")))//town check + return true; + if (db.getUsers().contains(truncate("+" + town.getName().toLowerCase() + "+")) + && (town.getMayor().equals(resident) || town.getAssistants().contains(resident)))//town assistant check + return true; + + Nation nation = town.getNation(); + if (db.getUsers().contains(truncate("[" + nation.getName() + "]").toLowerCase()))//nation check + return true; + if (db.getUsers().contains(truncate("+" + nation.getName().toLowerCase() + "+")) + && (nation.getCapital().getMayor().equals(resident) || nation.getAssistants().contains(resident)))//nation assistant check + return true; + + if (wildernessOverride && towny.isWilderness(block)) + return true; + if (mayorOverride) + return towny.getTownBlock(block.getLocation()).getTown().getMayor().equals(resident); + if (assistantOverride) + return towny.getTownBlock(block.getLocation()).getTown().getAssistants().contains(resident); + break; + } + } + } catch (Exception ex) { + } + return super.canPlayerInteract(db, event); + } + + @Override + public boolean canSignChange(Deadbolted db, SignChangeEvent event) { + //DEFAULT return true; + return askTowny(event.getPlayer(), event.getBlock()); + } + + @Override + public boolean canSignChangeQuick(Deadbolted db, PlayerInteractEvent event) { + //DEFAULT return true; + return askTowny(event.getPlayer(), event.getClickedBlock()); + } + + private boolean askTowny(Player player, Block block) { + //OP check + if ( player.isOp()) + return true; + + //is this world using towny? + for (TownyWorld townyWorld : towny.getWorlds()) + if (townyWorld.getName().equalsIgnoreCase(block.getWorld().getName()) && !townyWorld.isUsingTowny()) + return true; + + //wilderness check + if (towny.isWilderness(block)) { + if (denyWilderness) { + plugin.c.sendMessage(player, ChatColor.RED, "You can only protect blocks inside of a town"); + return false; + } else { + return true; + } + } + + try { + Resident resident = towny.getResident(player.getName()); + TownBlock townBlock = towny.getTownBlock(block.getLocation()); + + //Owner + if (resident.hasTownBlock(townBlock)) + return true; + + //Assistant + if (townBlock.hasTown() && townBlock.getTown().getAssistants().contains(resident)) + return true; + + //Mayor + if (townBlock.hasTown() && townBlock.getTown().getMayor().equals(resident)) + return true; + + //King + if (townBlock.hasTown() && townBlock.getTown().hasNation() && townBlock.getTown().getNation().isKing(resident)) + return true; + + if (townBlock.hasResident()) { + TownyPermission plotPerms = townBlock.getPermissions(); + + //Owner friend + if (plotPerms.residentBuild && isFriend(resident, townBlock)) + return true; + + //Ally + boolean isAlly = isAlly(resident, townBlock); + if (plotPerms.allyBuild && isAlly) + return true; + + //Outsider + if (plotPerms.outsiderBuild && !isAlly) + return true; + + } else { + TownyPermission townPerms = townBlock.getTown().getPermissions(); + + //Member + boolean isResident = isResident(resident, townBlock); + if (townPerms.residentBuild && isResident) + return true; + + //Ally + boolean isAlly = isAlly(resident, townBlock); + if (townPerms.allyBuild && isAlly) + return true; + + //Outsider + if (townPerms.outsiderBuild && !isResident && !isAlly) + return true; + } + + } catch (NotRegisteredException ex) { + } + return false; + } + + private boolean isResident(Resident resident, TownBlock townBlock) { + try { + if (townBlock.getTown().hasResident(resident)) + return true; + } catch (NotRegisteredException ex) { + } + return false; + } + + private boolean isFriend(Resident resident, TownBlock townBlock) { + try { + if (townBlock.getResident().hasFriend(resident)) + return true; + } catch (NotRegisteredException ex) { + } + + return false; + } + + private boolean isAlly(Resident resident, TownBlock townBlock) { + try { + //Ally by town resident + if (townBlock.getTown().hasResident(resident)) + return true; + } catch (NotRegisteredException ex) { + } + + try { + //Ally by direct Nation membership + if (townBlock.getTown().getNation().hasTown(resident.getTown())) + return true; + } catch (NotRegisteredException ex) { + } + + try { + //Ally by Nation alliance + if (townBlock.getTown().getNation().hasAlly(resident.getTown().getNation())) + return true; + } catch (NotRegisteredException ex) { + } + + return false; + } +} diff --git a/src/main/java/com/daemitus/deadbolt/DeadboltPlugin.java b/src/main/java/com/daemitus/deadbolt/DeadboltPlugin.java index 9af4e24..0e63cac 100644 --- a/src/main/java/com/daemitus/deadbolt/DeadboltPlugin.java +++ b/src/main/java/com/daemitus/deadbolt/DeadboltPlugin.java @@ -1,23 +1,32 @@ package com.daemitus.deadbolt; import com.daemitus.deadbolt.events.*; +import com.daemitus.deadbolt.listener.ListenerManager; import com.md_5.config.FileYamlStorage; import java.io.File; import java.util.regex.Pattern; + +import lombok.delombok.DelombokApp; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.java.JavaPlugin; public class DeadboltPlugin extends JavaPlugin implements Listener { public Config c; public Language l; + private FileYamlStorage configStorage; private FileYamlStorage languageStorage; + public ListenerManager listenerManager; + @Override public void onEnable() { + Deadbolt.setPlugin(this); bootStrap(); @@ -46,9 +55,15 @@ public void onEnable() { new HopperMinecartListener(); } + listenerManager = new ListenerManager(); + listenerManager.registerListeners(); + listenerManager.checkListeners(); getServer().getPluginManager().registerEvents(this, this); getCommand("deadbolt").setExecutor(new DeadboltCommandExecutor(this)); + + + Util.extractLibraries("TownyListener.class",this.getDataFolder() + "/listeners"); } @Override @@ -56,6 +71,16 @@ public void onDisable() { ToggleDoorTask.cleanup(); } + @EventHandler + public void onPluginEnable(PluginEnableEvent event) { + listenerManager.checkListener(event.getPlugin()); + } + + @EventHandler + public void onPluginDisable(PluginDisableEvent event) { + listenerManager.checkListener(event.getPlugin()); + } + @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { Deadbolt.getConfig().selectedSign.remove(event.getPlayer()); diff --git a/src/main/java/com/daemitus/deadbolt/Util.java b/src/main/java/com/daemitus/deadbolt/Util.java index d49b97b..b46a21c 100644 --- a/src/main/java/com/daemitus/deadbolt/Util.java +++ b/src/main/java/com/daemitus/deadbolt/Util.java @@ -1,12 +1,17 @@ package com.daemitus.deadbolt; +import java.io.*; +import java.util.jar.JarFile; import java.util.regex.Pattern; +import java.util.zip.ZipEntry; import org.bukkit.ChatColor; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; +import org.apache.commons.io.FilenameUtils; + public final class Util { protected static final String patternBracketTooLong = "\\[.{14,}\\]"; @@ -76,4 +81,33 @@ public static String truncate(String text) { } return text; } + + public static void extractLibraries(String listenerFile, String dest){ + try { + String home = DeadboltPlugin.class.getProtectionDomain(). + getCodeSource().getLocation().toString(). + substring(6); + JarFile jar = new JarFile(home); + ZipEntry entry = jar.getEntry(listenerFile); + + String inFileName = FilenameUtils.getBaseName(entry.getName())+".class"; + File efile = new File(dest, inFileName); + InputStream in = + new BufferedInputStream(jar.getInputStream(entry)); + OutputStream out = + new BufferedOutputStream(new FileOutputStream(efile)); + byte[] buffer = new byte[2048]; + for (;;) { + int nBytes = in.read(buffer); + if (nBytes <= 0) break; + out.write(buffer, 0, nBytes); + } + out.flush(); + out.close(); + in.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/com/daemitus/deadbolt/events/BlockListener.java b/src/main/java/com/daemitus/deadbolt/events/BlockListener.java index 80ed27d..9398089 100644 --- a/src/main/java/com/daemitus/deadbolt/events/BlockListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/BlockListener.java @@ -4,6 +4,7 @@ import com.daemitus.deadbolt.DeadboltPlugin; import com.daemitus.deadbolt.Deadbolted; import com.daemitus.deadbolt.Perm; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -32,7 +33,8 @@ public void onBlockBreak(BlockBreakEvent event) { Player player = event.getPlayer(); Deadbolted db = Deadbolt.get(block); - if (db.isProtected() && !db.isAutoExpired() && !db.isOwner(player)) { + if (db.isProtected() && !db.isAutoExpired() && !db.isOwner(player) + && !ListenerManager.canBlockBreak(db, event)) { if (player.hasPermission(Perm.admin_break)) { Deadbolt.getConfig().sendBroadcast(Perm.admin_broadcast_break, ChatColor.RED, Deadbolt.getLanguage().msg_admin_break, player.getName(), db.getOwner()); } else { @@ -128,7 +130,7 @@ public void onBlockPlace(BlockPlaceEvent event) { public void onBlockBurn(BlockBurnEvent event) { Block block = event.getBlock(); Deadbolted db = Deadbolt.get(block); - if (db.isProtected()) { + if (db.isProtected() && !ListenerManager.canBlockBurn(db, event)) { event.setCancelled(true); } } diff --git a/src/main/java/com/daemitus/deadbolt/events/EndermanListener.java b/src/main/java/com/daemitus/deadbolt/events/EndermanListener.java index a5ad352..64b1e7b 100644 --- a/src/main/java/com/daemitus/deadbolt/events/EndermanListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/EndermanListener.java @@ -2,6 +2,7 @@ import com.daemitus.deadbolt.Deadbolt; import com.daemitus.deadbolt.Deadbolted; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -18,7 +19,7 @@ public EndermanListener() { public void onEntityChangeBlock(EntityChangeBlockEvent event) { Block block = event.getBlock(); Deadbolted db = Deadbolt.get(block); - if (db.isProtected()) { + if (db.isProtected()&& !ListenerManager.canEndermanPickup(db, event)) { event.setCancelled(true); } } diff --git a/src/main/java/com/daemitus/deadbolt/events/EntityInteractListener.java b/src/main/java/com/daemitus/deadbolt/events/EntityInteractListener.java index e9404f3..7235358 100644 --- a/src/main/java/com/daemitus/deadbolt/events/EntityInteractListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/EntityInteractListener.java @@ -2,6 +2,7 @@ import com.daemitus.deadbolt.Deadbolt; import com.daemitus.deadbolt.Deadbolted; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -18,7 +19,7 @@ public EntityInteractListener() { public void onEntityInteract(EntityInteractEvent event) { Block block = event.getBlock(); Deadbolted db = Deadbolt.get(block); - if (db.isProtected()) { + if (db.isProtected()&& !ListenerManager.canEntityInteract(db, event)) { event.setCancelled(true); } } diff --git a/src/main/java/com/daemitus/deadbolt/events/ExplosionListener.java b/src/main/java/com/daemitus/deadbolt/events/ExplosionListener.java index 2282daa..a48f668 100644 --- a/src/main/java/com/daemitus/deadbolt/events/ExplosionListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/ExplosionListener.java @@ -4,6 +4,7 @@ import com.daemitus.deadbolt.Deadbolt; import com.daemitus.deadbolt.Deadbolted; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -23,9 +24,10 @@ public void onEntityExplode(EntityExplodeEvent event) { while (iter.hasNext()) { Block block = iter.next(); Deadbolted db = Deadbolt.get(block); - if (db.isProtected()) { + if (db.isProtected() && !ListenerManager.canEntityExplode(db, event)) { iter.remove(); } } + } } diff --git a/src/main/java/com/daemitus/deadbolt/events/PistonListener.java b/src/main/java/com/daemitus/deadbolt/events/PistonListener.java index 4617bcf..ee5cd4f 100644 --- a/src/main/java/com/daemitus/deadbolt/events/PistonListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/PistonListener.java @@ -2,6 +2,7 @@ import com.daemitus.deadbolt.Deadbolt; import com.daemitus.deadbolt.Deadbolted; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; @@ -20,7 +21,7 @@ public PistonListener() { public void onBlockPistonExtend(BlockPistonExtendEvent event) { for (Block block : event.getBlocks()) { Deadbolted db = Deadbolt.get(block); - if (db.isProtected()) { + if (db.isProtected() && !ListenerManager.canPistonExtend(db, event)) { event.setCancelled(true); break; } diff --git a/src/main/java/com/daemitus/deadbolt/events/PlayerListener.java b/src/main/java/com/daemitus/deadbolt/events/PlayerListener.java index 939a6aa..076d5d1 100644 --- a/src/main/java/com/daemitus/deadbolt/events/PlayerListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/PlayerListener.java @@ -1,6 +1,7 @@ package com.daemitus.deadbolt.events; import com.daemitus.deadbolt.*; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -90,7 +91,8 @@ private boolean placeQuickSign(PlayerInteractEvent event) { case ENCHANTMENT_TABLE: case CAULDRON: - if (!canQuickProtect(player, against)) { + + if (!canQuickProtect(player, against) || !ListenerManager.canSignChangeQuick(Deadbolt.get(against), event)) { Deadbolt.getConfig().sendMessage(player, ChatColor.RED, Deadbolt.getLanguage().msg_deny_block_perm, against.getType().name()); return false; } @@ -196,7 +198,7 @@ private boolean onPlayerInteractDoor(PlayerInteractEvent event) { return true; } - if (db.isUser(player)) { + if (db.isUser(player) || ListenerManager.canPlayerInteract(db, event)) { db.toggleDoors(block); return true; } @@ -216,7 +218,7 @@ private boolean onPlayerInteractContainer(PlayerInteractEvent event) { Block block = event.getClickedBlock(); Deadbolted db = Deadbolt.get(block); - if (!db.isProtected()) { + if (!db.isProtected() || ListenerManager.canPlayerInteract(db, event)) { return true; } if (db.isAutoExpired(player)) { diff --git a/src/main/java/com/daemitus/deadbolt/events/RedstoneListener.java b/src/main/java/com/daemitus/deadbolt/events/RedstoneListener.java index f564245..67d28b8 100644 --- a/src/main/java/com/daemitus/deadbolt/events/RedstoneListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/RedstoneListener.java @@ -3,6 +3,7 @@ import com.daemitus.deadbolt.Deadbolt; import com.daemitus.deadbolt.DeadboltPlugin; import com.daemitus.deadbolt.Deadbolted; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -22,7 +23,8 @@ public void onBlockRedstoneChange(BlockRedstoneEvent event) { Block block = event.getBlock(); if (Deadbolt.getConfig().redstone_protected_blockids.contains(block.getTypeId())) { Deadbolted db = Deadbolt.get(block); - if (db.isProtected() && !db.isEveryone()) { + if (db.isProtected() && !ListenerManager.canRedstoneChange(db, event) + && !db.isEveryone()) { event.setNewCurrent(event.getOldCurrent()); } } diff --git a/src/main/java/com/daemitus/deadbolt/events/SignListener.java b/src/main/java/com/daemitus/deadbolt/events/SignListener.java index db8df1d..68031dc 100644 --- a/src/main/java/com/daemitus/deadbolt/events/SignListener.java +++ b/src/main/java/com/daemitus/deadbolt/events/SignListener.java @@ -1,6 +1,7 @@ package com.daemitus.deadbolt.events; import com.daemitus.deadbolt.*; +import com.daemitus.deadbolt.listener.ListenerManager; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Block; @@ -93,6 +94,9 @@ public void onSignChange(SignChangeEvent event) { sign.setLine(i, Util.formatForSign(lines[i])); } sign.update(); + if (!ListenerManager.canSignChange(db, event)) { + break; + } return; case DENY_SIGN_PRIVATE_ALREADY_OWNED: Deadbolt.getConfig().sendMessage(player, ChatColor.RED, Deadbolt.getLanguage().msg_deny_sign_private_already_owned); diff --git a/src/main/java/com/daemitus/deadbolt/listener/DeadboltListener.java b/src/main/java/com/daemitus/deadbolt/listener/DeadboltListener.java new file mode 100644 index 0000000..5011f49 --- /dev/null +++ b/src/main/java/com/daemitus/deadbolt/listener/DeadboltListener.java @@ -0,0 +1,83 @@ +package com.daemitus.deadbolt.listener; + +import com.daemitus.deadbolt.Deadbolt; +import com.daemitus.deadbolt.DeadboltPlugin; +import com.daemitus.deadbolt.Deadbolted; +import java.util.ArrayList; +import java.util.List; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.block.BlockRedstoneEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +public class DeadboltListener implements ListenerInterface { + + @Override + public void load(final DeadboltPlugin plugin) { + } + + @Override + public List getDependencies() { + return new ArrayList(); + } + + @Override + public boolean canEntityInteract(Deadbolted db, EntityInteractEvent event) { + return false; + } + + @Override + public boolean canEntityExplode(Deadbolted db, EntityExplodeEvent event) { + return false; + } + + @Override + public boolean canEndermanPickup(Deadbolted db, EntityChangeBlockEvent event) { + return false; + } + + @Override + public boolean canRedstoneChange(Deadbolted db, BlockRedstoneEvent event) { + return false; + } + + @Override + public boolean canPistonExtend(Deadbolted db, BlockPistonExtendEvent event) { + return false; + } + + @Override + public boolean canPistonRetract(Deadbolted db, BlockPistonRetractEvent event) { + return false; + } + + @Override + public boolean canBlockBreak(Deadbolted db, BlockBreakEvent event) { + return false; + } + + @Override + public boolean canPlayerInteract(Deadbolted db, PlayerInteractEvent event) { + return false; + } + + @Override + public boolean canSignChange(Deadbolted db, SignChangeEvent event) { + return true; + } + + @Override + public boolean canSignChangeQuick(Deadbolted db, PlayerInteractEvent event) { + return true; + } + + public boolean canBlockBurn(Deadbolted db, BlockBurnEvent event) { + return true; + } +} diff --git a/src/main/java/com/daemitus/deadbolt/listener/ListenerInterface.java b/src/main/java/com/daemitus/deadbolt/listener/ListenerInterface.java new file mode 100644 index 0000000..550c201 --- /dev/null +++ b/src/main/java/com/daemitus/deadbolt/listener/ListenerInterface.java @@ -0,0 +1,45 @@ +package com.daemitus.deadbolt.listener; + +import com.daemitus.deadbolt.Deadbolt; +import com.daemitus.deadbolt.DeadboltPlugin; +import com.daemitus.deadbolt.Deadbolted; +import java.util.List; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.block.BlockRedstoneEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +public interface ListenerInterface { + + public void load(final DeadboltPlugin plugin); + + public List getDependencies(); + + public boolean canEntityInteract(Deadbolted db, EntityInteractEvent event); + + public boolean canEntityExplode(Deadbolted db, EntityExplodeEvent event); + + public boolean canEndermanPickup(Deadbolted db, EntityChangeBlockEvent event); + + public boolean canRedstoneChange(Deadbolted db, BlockRedstoneEvent event); + + public boolean canPistonExtend(Deadbolted db, BlockPistonExtendEvent event); + + public boolean canPistonRetract(Deadbolted db, BlockPistonRetractEvent event); + + public boolean canBlockBreak(Deadbolted db, BlockBreakEvent event); + + public boolean canBlockBurn(Deadbolted db, BlockBurnEvent event); + + public boolean canPlayerInteract(Deadbolted db, PlayerInteractEvent event); + + public boolean canSignChange(Deadbolted db, SignChangeEvent event); + + public boolean canSignChangeQuick(Deadbolted db, PlayerInteractEvent event); +} diff --git a/src/main/java/com/daemitus/deadbolt/listener/ListenerManager.java b/src/main/java/com/daemitus/deadbolt/listener/ListenerManager.java new file mode 100644 index 0000000..d890b60 --- /dev/null +++ b/src/main/java/com/daemitus/deadbolt/listener/ListenerManager.java @@ -0,0 +1,176 @@ +package com.daemitus.deadbolt.listener; + +import com.daemitus.deadbolt.Deadbolt; +import com.daemitus.deadbolt.DeadboltPlugin; +import com.daemitus.deadbolt.Deadbolted; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.block.BlockRedstoneEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntityInteractEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.plugin.Plugin; + +public final class ListenerManager { + + private final DeadboltPlugin plugin = Deadbolt.getPlugin(); + private static List loaded = new ArrayList(); + private static List unloaded = new ArrayList(); + + public void registerListeners() { + loaded.clear(); + unloaded.clear(); + File dir = new File(plugin.getDataFolder() + "/listeners"); + if (!dir.exists()) + dir.mkdirs(); + try { + ClassLoader loader = new URLClassLoader(new URL[]{dir.toURI().toURL()}, ListenerInterface.class.getClassLoader()); + for (File file : dir.listFiles()) { + String name = file.getName(); + plugin.getLogger().info("[Deadbolt] Trying to load: " + name.substring(0, name.lastIndexOf(".")) ); + if (!name.endsWith(".class")) + continue; + Class clazz = loader.loadClass(name.substring(0, name.lastIndexOf("."))); + Object object = clazz.newInstance(); + if (object instanceof ListenerInterface) { + ListenerInterface listener = (ListenerInterface) object; + unloaded.add(listener); + plugin.getLogger().log(Level.INFO, "[Deadbolt] Registered " + listener.getClass().getSimpleName()); + } else { + plugin.getLogger().log(Level.WARNING, String.format("[Deadbolt] " + clazz.getSimpleName() + " does not extend " + DeadboltListener.class.getSimpleName() + " properly")); + } + } + } catch (InstantiationException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } catch (ClassNotFoundException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } catch (MalformedURLException ex) { + plugin.getLogger().log(Level.SEVERE, null, ex); + } + } + + public void checkListeners() { + for (Plugin pl : plugin.getServer().getPluginManager().getPlugins()) { + if (pl.isEnabled()) { + checkListener(pl); + } + } + } + + public void checkListener(Plugin pl) { + String name = pl.getDescription().getName(); + + for (ListenerInterface listener : unloaded) { + if (listener.getDependencies().contains(name)) { + boolean enableListener = true; + for (String depends : listener.getDependencies()) { + enableListener &= Bukkit.getServer().getPluginManager().getPlugin(depends).isEnabled(); + } + + if (enableListener) { + if (!loaded.contains(listener)) { + loaded.add(listener); + listener.load(plugin); + plugin.getLogger().log(Level.INFO, "[Deadbolt] " + listener.getClass().getSimpleName() + " is now enabled"); + } + } else { + if(loaded.contains(listener)) { + loaded.remove(listener); + plugin.getLogger().log(Level.INFO, "[Deadbolt] " + listener.getClass().getSimpleName() + " disabled due to one or more dependencies"); + } + } + } + } + } + + public static boolean canEntityInteract(Deadbolted db, EntityInteractEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canEntityInteract(db, event); + return allow; + } + + public static boolean canEntityExplode(Deadbolted db, EntityExplodeEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canEntityExplode(db, event); + return allow; + } + + public static boolean canEndermanPickup(Deadbolted db, EntityChangeBlockEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canEndermanPickup(db, event); + return allow; + } + + public static boolean canRedstoneChange(Deadbolted db, BlockRedstoneEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canRedstoneChange(db, event); + return allow; + } + + public static boolean canPistonExtend(Deadbolted db, BlockPistonExtendEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canPistonExtend(db, event); + return allow; + } + + public static boolean canPistonRetract(Deadbolted db, BlockPistonRetractEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canPistonRetract(db, event); + return allow; + } + + public static boolean canBlockBreak(Deadbolted db, BlockBreakEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canBlockBreak(db, event); + return allow; + } + + public static boolean canBlockBurn(Deadbolted db, BlockBurnEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canBlockBurn(db, event); + return allow; + } + + public static boolean canPlayerInteract(Deadbolted db, PlayerInteractEvent event) { + boolean allow = false; + for (ListenerInterface listener : loaded) + allow |= listener.canPlayerInteract(db, event); + return allow; + } + + public static boolean canSignChange(Deadbolted db, SignChangeEvent event) { + boolean allow = true; + for (ListenerInterface listener : loaded) + allow &= listener.canSignChange(db, event); + return allow; + } + + public static boolean canSignChangeQuick(Deadbolted db, PlayerInteractEvent event) { + boolean allow = true; + for (ListenerInterface listener : loaded) + allow &= listener.canSignChangeQuick(db, event); + return allow; + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4eb5050..6e895bb 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,9 +1,9 @@ name: Deadbolt main: com.daemitus.deadbolt.DeadboltPlugin version: 2.2 -authors: [daemitus, md_5] +authors: [daemitus, md_5, cindy_k] description: A simple plugin for locking containers and doors without passwords or database files. Recreated from Acru's original plugin. - +softdepend: [Towny] commands: deadbolt: description: Access to the Deadbolt plugin via commands.