diff --git a/BlocksHub/nbproject/project.properties b/BlocksHub/nbproject/project.properties index 2c97bda..d8c228a 100644 --- a/BlocksHub/nbproject/project.properties +++ b/BlocksHub/nbproject/project.properties @@ -28,7 +28,8 @@ endorsed.classpath= excludes= file.reference.CoreProtect.jar=lib/CoreProtect.jar file.reference.craftbukkit.jar=lib/craftbukkit.jar -file.reference.GriefPrevention.jar=lib\\GriefPrevention.jar +file.reference.GriefPrevention-7x.jar=lib/GriefPrevention-7x.jar +file.reference.GriefPrevention-8x.jar=lib/GriefPrevention-8x.jar file.reference.HawkEye.jar=lib/HawkEye.jar file.reference.LogBlock.jar=lib/LogBlock.jar file.reference.Prism.jar=lib/Prism.jar @@ -44,7 +45,8 @@ javac.classpath=\ ${file.reference.Residence.jar}:\ ${file.reference.WorldGuard.jar}:\ ${file.reference.craftbukkit.jar}:\ - ${file.reference.GriefPrevention.jar} + ${file.reference.GriefPrevention-8x.jar}:\ + ${file.reference.GriefPrevention-7x.jar} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/BaseAccessController.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/BaseAccessController.java index c128d30..88d75ba 100644 --- a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/BaseAccessController.java +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/BaseAccessController.java @@ -23,7 +23,6 @@ */ package org.PrimeSoft.blocksHub.accessControl; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import org.bukkit.Server; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -71,6 +70,10 @@ public String getName() { return m_name; } + protected void disable() { + + } + protected BaseAccessController(JavaPlugin plugin, final String pluginName) { m_isEnabled = false; PluginDescriptionFile pd = null; @@ -87,10 +90,18 @@ protected BaseAccessController(JavaPlugin plugin, final String pluginName) { } } catch (NoClassDefFoundError ex) { hook = null; + m_isEnabled = false; } + m_hook = hook; + if (m_isEnabled) { + m_isEnabled = postInit(pd); + } + m_name = pd != null && m_isEnabled ? pd.getFullName() : "Disabled - " + pluginName; + } - m_name = pd != null ? pd.getFullName() : "Disabled - " + pluginName; + protected boolean postInit(PluginDescriptionFile pd) { + return true; } protected abstract boolean instanceOfT(Class aClass); diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention7x.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention7x.java new file mode 100644 index 0000000..6da514d --- /dev/null +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention7x.java @@ -0,0 +1,65 @@ +package org.PrimeSoft.blocksHub.accessControl.GriefPrevention; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.logging.Level; +import java.util.logging.Logger; +import me.ryanhamshire.GriefPrevention.BlockEventHandler; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import org.PrimeSoft.blocksHub.SilentPlayer; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; + +/* + * The MIT License + * + * Copyright 2014 SBPrime. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +/** + * + * @author SBPrime + */ +public class GriefPrevention7x extends GriefPreventionBase { + @Override + public boolean Initialize(GriefPrevention hook) { + try { + m_listener = hook != null ? + (BlockEventHandler)BlockEventHandler.class.getConstructor().newInstance() : null; + return m_listener != null; + } catch (NoSuchMethodException ex) { + return false; + } catch (SecurityException ex) { + return false; + } catch (InstantiationException ex) { + return false; + } catch (IllegalAccessException ex) { + return false; + } catch (IllegalArgumentException ex) { + return false; + } catch (InvocationTargetException ex) { + return false; + } + } +} diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention8x.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention8x.java new file mode 100644 index 0000000..ca1c6c7 --- /dev/null +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPrevention8x.java @@ -0,0 +1,87 @@ +package org.PrimeSoft.blocksHub.accessControl.GriefPrevention; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import me.ryanhamshire.GriefPrevention.BlockEventHandler; +import me.ryanhamshire.GriefPrevention.DataStore; +import me.ryanhamshire.GriefPrevention.GriefPrevention; + +/* + * The MIT License + * + * Copyright 2014 SBPrime. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +/** + * + * @author SBPrime + */ +public class GriefPrevention8x extends GriefPreventionBase { + + @Override + public boolean Initialize(GriefPrevention hook) { + try { + if (hook == null) { + return false; + } + + Class sourceClass = hook.getClass(); + Field field = sourceClass.getDeclaredField("dataStore"); + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + + boolean accessible = modifiersField.isAccessible(); + if (!accessible) { + modifiersField.setAccessible(true); + } + try { + m_listener = (BlockEventHandler) BlockEventHandler.class.getConstructor(DataStore.class) + .newInstance(field.get(hook)); + } finally { + if (!accessible) { + modifiersField.setAccessible(false); + } + } + + return m_listener != null; + } catch (NoSuchMethodException ex) { + ex.printStackTrace(); + return false; + } catch (SecurityException ex) { + ex.printStackTrace(); + return false; + } catch (InstantiationException ex) { + ex.printStackTrace(); + return false; + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + return false; + } catch (IllegalArgumentException ex) { + ex.printStackTrace(); + return false; + } catch (InvocationTargetException ex) { + ex.printStackTrace(); + return false; + } catch (NoSuchFieldException ex) { + ex.printStackTrace(); + return false; + } + } +} diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPreventionBase.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPreventionBase.java new file mode 100644 index 0000000..0812be8 --- /dev/null +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPrevention/GriefPreventionBase.java @@ -0,0 +1,85 @@ +/* + * The MIT License + * + * Copyright 2014 SBPrime. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.PrimeSoft.blocksHub.accessControl.GriefPrevention; + +import me.ryanhamshire.GriefPrevention.BlockEventHandler; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import org.PrimeSoft.blocksHub.SilentPlayer; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; + +/** + * + * @author SBPrime + */ +public abstract class GriefPreventionBase { + protected BlockEventHandler m_listener; + + /** + * Initialize the GP hook + * @param hook + * @return + */ + public abstract boolean Initialize(GriefPrevention hook); + + + /** + * Test if the block can by placed + * @param player + * @param world + * @param location + * @return + */ + public boolean canPlace(Player player, World world, Location location) { + if (player == null) { + return true; + } + player = new SilentPlayer(player); + Block block = location.getBlock(); + + if (!block.isEmpty()) { + BlockBreakEvent event = new BlockBreakEvent(block, player); + m_listener.onBlockBreak(event); + + if (event.isCancelled()) { + return false; + } + } else { + BlockPlaceEvent event = new BlockPlaceEvent(block, block.getState(), block, + player.getItemInHand(), player, true); + m_listener.onBlockPlace(event); + + if (event.isCancelled()) { + return false; + } + } + + //We do not support white/black lists + return true; + } +} diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPreventionAc.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPreventionAc.java index 25a7790..9636bf6 100644 --- a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPreventionAc.java +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/GriefPreventionAc.java @@ -23,17 +23,13 @@ */ package org.PrimeSoft.blocksHub.accessControl; -import com.bekvon.bukkit.residence.Residence; -import com.bekvon.bukkit.residence.listeners.ResidenceBlockListener; -import me.ryanhamshire.GriefPrevention.BlockEventHandler; import me.ryanhamshire.GriefPrevention.GriefPrevention; -import org.PrimeSoft.blocksHub.SilentPlayer; +import org.PrimeSoft.blocksHub.accessControl.GriefPrevention.GriefPrevention7x; +import org.PrimeSoft.blocksHub.accessControl.GriefPrevention.GriefPrevention8x; +import org.PrimeSoft.blocksHub.accessControl.GriefPrevention.GriefPreventionBase; import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.block.Block; import org.bukkit.entity.Player; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; @@ -44,15 +40,39 @@ */ public class GriefPreventionAc extends BaseAccessController { - private BlockEventHandler m_listener; + private GriefPreventionBase m_interface; public GriefPreventionAc(JavaPlugin plugin) { super(plugin, "GriefPrevention"); + } + @Override + protected boolean postInit(PluginDescriptionFile pd) { + try { + m_interface = new GriefPrevention7x(); + if (m_interface.Initialize(m_hook)) + { + return true; + } + } + catch (Exception ex) + { + //v7.x not supported try v8.x + } + + try { - m_listener = m_hook != null ? new BlockEventHandler() : null; - } catch (NoClassDefFoundError ex) { + m_interface = new GriefPrevention8x(); + if (m_interface.Initialize(m_hook)) + { + return true; + } + } + catch (Exception ex) + { + //v8.x not supported } + return false; } @Override @@ -60,33 +80,14 @@ public boolean canPlace(String player, World world, Location location) { if (!m_isEnabled || player == null || world == null || location == null) { return true; } - Player bPlayer = m_server.getPlayer(player); - if (bPlayer == null) { + + if (m_interface == null || bPlayer == null) + { return true; } - bPlayer = new SilentPlayer(bPlayer); - Block block = location.getBlock(); - - if (!block.isEmpty()) { - BlockBreakEvent event = new BlockBreakEvent(block, bPlayer); - m_listener.onBlockBreak(event); - - if (event.isCancelled()) { - return false; - } - } else { - BlockPlaceEvent event = new BlockPlaceEvent(block, block.getState(), block, - bPlayer.getItemInHand(), bPlayer, true); - m_listener.onBlockPlace(event); - - if (event.isCancelled()) { - return false; - } - } - - //We do not support white/black lists - return true; + + return m_interface.canPlace(bPlayer, world, location); } @Override diff --git a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/ResidenceAc.java b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/ResidenceAc.java index e6b6c13..c8f845c 100644 --- a/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/ResidenceAc.java +++ b/BlocksHub/src/org/PrimeSoft/blocksHub/accessControl/ResidenceAc.java @@ -33,6 +33,7 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; /** @@ -48,10 +49,15 @@ public class ResidenceAc extends BaseAccessController { public ResidenceAc(JavaPlugin plugin) { super(plugin, "Residence"); + } + @Override + protected boolean postInit(PluginDescriptionFile pd) { try { m_listener = m_hook != null ? new ResidenceBlockListener() : null; + return true; } catch (NoClassDefFoundError ex) { + return false; } } @@ -88,7 +94,7 @@ public boolean canPlace(String player, World world, Location location) { //We do not support white/black lists return true; } - + @Override protected boolean instanceOfT(Class aClass) { return Residence.class.isAssignableFrom(aClass); diff --git a/BlocksHub/src/plugin.yml b/BlocksHub/src/plugin.yml index 2fde11b..9a62666 100644 --- a/BlocksHub/src/plugin.yml +++ b/BlocksHub/src/plugin.yml @@ -1,6 +1,6 @@ name: BlocksHub main: org.PrimeSoft.blocksHub.BlocksHub -version: 0.5 +version: 0.6 website: http://dev.bukkit.org/bukkit-plugins/blockshub/ description: Logger & Block access API. authors: [SBPrime]