Support for Denizen 1.1.1+, fixes #1033

This commit is contained in:
PikaMug 2020-01-09 22:30:15 -05:00
parent a8fd1d02e6
commit bbfb9360b9
11 changed files with 417 additions and 252 deletions

View File

@ -65,7 +65,7 @@
<dependency>
<groupId>com.denizenscript</groupId>
<artifactId>denizen</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -18,9 +18,9 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import me.blackvein.quests.util.DenizenAPI;
import me.blackvein.quests.reflect.denizen.DenizenAPI;
import me.blackvein.quests.reflect.worldguard.WorldGuardAPI;
import me.blackvein.quests.util.Lang;
import me.blackvein.quests.util.WorldGuardAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import net.citizensnpcs.api.CitizensPlugin;
import net.milkbowl.vault.economy.Economy;

View File

@ -58,12 +58,12 @@ import me.blackvein.quests.prompts.RequirementsPrompt;
import me.blackvein.quests.prompts.RewardsPrompt;
import me.blackvein.quests.prompts.PlannerPrompt;
import me.blackvein.quests.prompts.StageMenuPrompt;
import me.blackvein.quests.reflect.worldguard.WorldGuardAPI;
import me.blackvein.quests.util.CK;
import me.blackvein.quests.util.ConfigUtil;
import me.blackvein.quests.util.ItemUtil;
import me.blackvein.quests.util.Lang;
import me.blackvein.quests.util.MiscUtil;
import me.blackvein.quests.util.WorldGuardAPI;
import net.citizensnpcs.api.CitizensAPI;
public class QuestFactory implements ConversationAbandonedListener {

View File

@ -2719,7 +2719,7 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override
public String getPromptText(ConversationContext context) {
String text = ChatColor.DARK_AQUA + "- " + Lang.get("stageEditorDenizenScript") + " -\n";
for (String s : plugin.getDependencies().getDenizenAPI()._getScriptNames()) {
for (String s : plugin.getDependencies().getDenizenAPI().getScriptNames()) {
text += ChatColor.AQUA + "- " + s + "\n";
}
return text + ChatColor.YELLOW + Lang.get("stageEditorScriptPrompt");

View File

@ -0,0 +1,190 @@
/*******************************************************************************************************
* Continued by PikaMug (formerly HappyPikachu) with permission from _Blackvein_. All rights reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.reflect.denizen;
import java.lang.reflect.Method;
import java.util.Set;
import javax.annotation.Nullable;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import net.citizensnpcs.api.npc.NPC;
public class DenizenAPI {
private Class<?> denizen_1_0_9 = null;
private Class<?> denizen_1_1_0 = null;
private Class<?> denizen_1_1_1 = null;
protected Class<?> scriptRegistry = null;
protected Method containsScriptMethod = null;
protected Method getScriptNamesMethod = null;
protected Class<?> scriptContainer = null;
protected Class<?> taskScriptContainer = null;
protected Method getScriptContainerAsMethod = null;
protected Class<?> dPlayer = null;
protected Class<?> dNPC = null;
protected Method mirrorBukkitPlayerMethod = null;
protected Method mirrorCitizensNPCMethod = null;
protected Class<?> scriptEntryData = null;
protected Class<?> bukkitScriptEntryData = null;
public DenizenAPI() {
try {
bukkitScriptEntryData
= Class.forName("com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData");
denizen_1_1_1 = Class.forName("com.denizenscript.denizen.Denizen");
return;
} catch (Exception e2) {
// Fail silently
}
try {
bukkitScriptEntryData = Class.forName("com.denizenscript.denizen.BukkitScriptEntryData");
scriptRegistry = Class.forName("com.denizenscript.denizencore.scripts.ScriptRegistry");
scriptContainer = Class.forName("com.denizenscript.denizencore.scripts.containers.ScriptContainer");
taskScriptContainer
= Class.forName("com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer");
dPlayer = Class.forName("com.denizenscript.denizen.objects.PlayerTag");
dNPC = Class.forName("com.denizenscript.denizen.objects.NPCTag");
scriptEntryData = Class.forName("com.denizenscript.denizencore.scripts.ScriptEntryData");
bukkitScriptEntryData = Class.forName("com.denizenscript.denizen.BukkitScriptEntryData");
denizen_1_1_0 = Class.forName("com.denizenscript.denizen.Denizen");
getScriptNamesMethod = scriptRegistry.getMethod("_getScriptNames");
return;
} catch (Exception e1) {
// Fail silently
}
try {
scriptRegistry = Class.forName("net.aufdemrand.denizencore.scripts.ScriptRegistry");
scriptContainer = Class.forName("net.aufdemrand.denizencore.scripts.containers.ScriptContainer");
taskScriptContainer
= Class.forName("net.aufdemrand.denizencore.scripts.containers.core.TaskScriptContainer");
dPlayer = Class.forName("net.aufdemrand.denizen.objects.dPlayer");
dNPC = Class.forName("net.aufdemrand.denizen.objects.dNPC");
scriptEntryData = Class.forName("net.aufdemrand.denizencore.scripts.ScriptEntryData");
bukkitScriptEntryData = Class.forName("net.aufdemrand.denizen.BukkitScriptEntryData");
denizen_1_0_9 = Class.forName("net.aufdemrand.denizen.Denizen");
containsScriptMethod = scriptRegistry.getMethod("containsScript", String.class);
getScriptNamesMethod = scriptRegistry.getMethod("_getScriptNames");
getScriptContainerAsMethod = scriptRegistry
.getMethod("getScriptContainerAs", String.class, taskScriptContainer.getClass());
mirrorBukkitPlayerMethod = dPlayer.getMethod("mirrorBukkitPlayer", OfflinePlayer.class);
mirrorCitizensNPCMethod = dNPC.getMethod("mirrorCitizensNPC", NPC.class);
} catch (Exception e) {
// Fail silently
}
}
public boolean isEnabled() {
if (denizen_1_1_1 != null || denizen_1_1_0 != null || denizen_1_0_9 != null) {
return true;
}
return false;
}
public Class<?> getDenizenClass() {
if (denizen_1_1_1 != null) {
return denizen_1_1_1;
} else if (denizen_1_1_0 != null) {
return denizen_1_1_0;
} else if (denizen_1_0_9 != null) {
return denizen_1_0_9;
}
return null;
}
@Nullable
public boolean containsScript(String input) {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.containsScript(input);
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.containsScript(input);
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.containsScript(input);
}
return false;
}
@Nullable
public String getScriptContainerName(String input) {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.getScriptContainerName(input);
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.getScriptContainerName(input);
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.getScriptContainerName(input);
}
return null;
}
@Nullable
public Set<String> getScriptNames() {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.getScriptNames();
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.getScriptNames();
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.getScriptNames();
}
return null;
}
@Nullable
public Object getScriptContainerAs(String scriptName) {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.getScriptContainerAs(scriptName);
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.getScriptContainerAs(scriptName);
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.getScriptContainerAs(scriptName);
}
return null;
}
@Nullable
public Object mirrorBukkitPlayer(Player player) {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.mirrorBukkitPlayer(player);
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.mirrorBukkitPlayer(player);
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.mirrorBukkitPlayer(player);
}
return null;
}
@Nullable
public Object mirrorCitizensNPC(NPC npc) {
if (denizen_1_1_1 != null) {
return DenizenAPI_1_1_1.mirrorCitizensNPC(npc);
} else if (denizen_1_1_0 != null) {
return DenizenAPI_1_1_0.mirrorCitizensNPC(npc);
} else if (denizen_1_0_9 != null) {
return DenizenAPI_1_0_9.mirrorCitizensNPC(npc);
}
return null;
}
@Nullable
public void runTaskScript(String scriptName, Player player) {
if (denizen_1_1_1 != null) {
DenizenAPI_1_1_1.runTaskScript(scriptName, player);
} else if (denizen_1_1_0 != null) {
DenizenAPI_1_1_0.runTaskScript(scriptName, player);
} else if (denizen_1_0_9 != null) {
DenizenAPI_1_0_9.runTaskScript(scriptName, player);
}
}
}

View File

@ -0,0 +1,123 @@
/*******************************************************************************************************
* Continued by PikaMug (formerly HappyPikachu) with permission from _Blackvein_. All rights reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.reflect.denizen;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import me.blackvein.quests.Quests;
import net.citizensnpcs.api.npc.NPC;
public class DenizenAPI_1_0_9 {
private static Quests quests = (Quests) Bukkit.getPluginManager().getPlugin("Quests");
private static DenizenAPI api = quests.getDependencies().getDenizenAPI();
@Nullable
public static boolean containsScript(String input) {
if (api.scriptRegistry == null || api.containsScriptMethod == null) return false;
boolean script = false;
try {
script = (boolean)api.containsScriptMethod.invoke(api.scriptRegistry, input);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen ScriptRegistry#containsScript", e);
}
return script;
}
@Nullable
public static String getScriptContainerName(String input) {
if (api.scriptRegistry == null || api.scriptContainer == null) return null;
String name = null;
Object instance;
try {
Constructor<?> constructor = api.scriptRegistry.getConstructor(YamlConfiguration.class, String.class);
instance = constructor.newInstance(null, input);
name = (String)instance.getClass().getMethod("getName").invoke(api.scriptContainer);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen ScriptContainer#getName", e);
}
return name;
}
@SuppressWarnings("unchecked")
@Nullable
public static Set<String> getScriptNames() {
if (api.scriptRegistry == null || api.getScriptNamesMethod == null) return null;
Set<String> names = null;
try {
names = (Set<String>)api.getScriptNamesMethod.invoke(api.scriptRegistry);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen ScriptRegistry#_getScriptNames", e);
}
return names;
}
@Nullable
public static Object getScriptContainerAs(String scriptName) {
if (api.scriptRegistry == null || api.taskScriptContainer == null) return null;
Object container = null;
try {
container = api.getScriptContainerAsMethod.invoke(api.scriptRegistry, scriptName, api.taskScriptContainer);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen #getScriptContainerAs", e);
}
return container;
}
@Nullable
public static Object mirrorBukkitPlayer(Player player) {
if (api.dPlayer == null || api.mirrorBukkitPlayerMethod == null) return null;
Object dp = null;
try {
dp = api.mirrorBukkitPlayerMethod.invoke(api.dPlayer, player);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen dPlayer#mirrorBukkitPlayer", e);
}
return dp;
}
@Nullable
public static Object mirrorCitizensNPC(NPC npc) {
if (api.dNPC == null || api.mirrorCitizensNPCMethod == null) return null;
Object dp = null;
try {
dp = api.mirrorCitizensNPCMethod.invoke(api.dNPC, npc);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen dNPC#mirrorCitizensNPC", e);
}
return dp;
}
@Nullable
public static void runTaskScript(String scriptName, Player player) {
if (api.scriptRegistry == null || api.bukkitScriptEntryData == null || api.scriptEntryData == null) return;
try {
Constructor<?> constructor = api.bukkitScriptEntryData.getConstructors()[0];
Object tsc = getScriptContainerAs(scriptName);
Method runTaskScript = tsc.getClass().getMethod("runTaskScript", api.scriptEntryData, Map.class);
runTaskScript.invoke(tsc, constructor.newInstance(mirrorBukkitPlayer(player), null), null);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen TaskScriptContainer#runTaskScript", e);
}
}
}

View File

@ -0,0 +1,88 @@
/*******************************************************************************************************
* Continued by PikaMug (formerly HappyPikachu) with permission from _Blackvein_. All rights reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.reflect.denizen;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer;
import me.blackvein.quests.Quests;
import net.citizensnpcs.api.npc.NPC;
public class DenizenAPI_1_1_0 {
private static Quests quests = (Quests) Bukkit.getPluginManager().getPlugin("Quests");
private static DenizenAPI api = quests.getDependencies().getDenizenAPI();
@Nullable
public static boolean containsScript(String input) {
return ScriptRegistry.containsScript(input);
}
@Nullable
public static String getScriptContainerName(String input) {
return ScriptRegistry.getScriptContainer(input).getName();
}
@SuppressWarnings("unchecked")
@Nullable
public static Set<String> getScriptNames() {
if (api.scriptRegistry == null || api.getScriptNamesMethod == null) return null;
Set<String> names = null;
try {
names = (Set<String>)api.getScriptNamesMethod.invoke(api.scriptRegistry);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen ScriptRegistry#_getScriptNames", e);
}
return names;
}
@Nullable
public static Object getScriptContainerAs(String scriptName) {
return ScriptRegistry.getScriptContainerAs(scriptName, TaskScriptContainer.class);
}
@Nullable
public static Object mirrorBukkitPlayer(Player player) {
return PlayerTag.mirrorBukkitPlayer(player);
}
@Nullable
public static Object mirrorCitizensNPC(NPC npc) {
return NPCTag.mirrorCitizensNPC(npc);
}
@Nullable
public static void runTaskScript(String scriptName, Player player) {
try {
Constructor<?> constructor = api.bukkitScriptEntryData.getConstructors()[0];
Object tsc = getScriptContainerAs(scriptName);
Method runTaskScript = tsc.getClass().getMethod("runTaskScript", api.scriptEntryData, Map.class);
runTaskScript.invoke(tsc, constructor.newInstance(mirrorBukkitPlayer(player), null), null);
} catch (Exception e) {
quests.getLogger().log(Level.WARNING, "Error invoking Denizen TaskScriptContainer#runTaskScript", e);
}
}
}

View File

@ -10,7 +10,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.util;
package me.blackvein.quests.reflect.denizen;
import java.util.Set;
@ -18,19 +18,15 @@ import javax.annotation.Nullable;
import org.bukkit.entity.Player;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer;
import net.citizensnpcs.api.npc.NPC;
/**
* This class's imports must exist separately from DenizenAPI.java in order to not crash from NoClassDefFoundError at
* runtime
*/
public class DenizenAPI_1_1_0 {
public class DenizenAPI_1_1_1 {
@Nullable
public static boolean containsScript(String input) {
@ -43,8 +39,8 @@ public class DenizenAPI_1_1_0 {
}
@Nullable
public static Set<String> _getScriptNames() {
return ScriptRegistry._getScriptNames();
public static Set<String> getScriptNames() {
return ScriptRegistry.scriptContainers.keySet();
}
@Nullable

View File

@ -10,7 +10,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.util;
package me.blackvein.quests.reflect.worldguard;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
@ -41,10 +41,6 @@ public class WorldGuardAPI {
private Constructor<?> vectorConstructor = null;
private boolean initialized = false;
public boolean isEnabled() {
return worldGuardPlugin != null;
}
public WorldGuardAPI(Plugin wg) {
if (wg instanceof WorldGuardPlugin) {
worldGuardPlugin = (WorldGuardPlugin)wg;
@ -58,6 +54,10 @@ public class WorldGuardAPI {
}
}
}
public boolean isEnabled() {
return worldGuardPlugin != null;
}
protected RegionAssociable getAssociable(Player player) {
RegionAssociable associable;

View File

@ -10,7 +10,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.util;
package me.blackvein.quests.reflect.worldguard;
import java.util.List;
@ -24,10 +24,6 @@ import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
/**
* This class's imports must exist separately from WorldGuardAPI.java in order to not crash from NoClassDefFoundError
* at runtime
*/
public class WorldGuardAPI_7_0_0 {
@Nullable

View File

@ -1,228 +0,0 @@
/*******************************************************************************************************
* Continued by PikaMug (formerly HappyPikachu) with permission from _Blackvein_. All rights reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
package me.blackvein.quests.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import net.citizensnpcs.api.npc.NPC;
public class DenizenAPI {
private Class<?> denizen = null;
private Class<?> scriptRegistry = null;
private Method containsScriptMethod = null;
private Method getScriptNamesMethod = null;
private Class<?> scriptContainer = null;
private Class<?> taskScriptContainer = null;
private Method getScriptContainerAsMethod = null;
private Class<?> dPlayer = null;
private Class<?> dNPC = null;
private Method mirrorBukkitPlayerMethod = null;
private Method mirrorCitizensNPCMethod = null;
private Class<?> scriptEntryData = null;
private Class<?> bukkitScriptEntryData = null;
private boolean initialized = false;
public boolean isEnabled() {
return denizen != null;
}
public DenizenAPI() {
try {
denizen = Class.forName("com.denizenscript.denizen.Denizen");
// Denizen 1.1.0+
} catch (Exception e) {
try {
denizen = Class.forName("net.aufdemrand.denizen.Denizen");
scriptRegistry = Class.forName("net.aufdemrand.denizencore.scripts.ScriptRegistry");
scriptContainer = Class.forName("net.aufdemrand.denizencore.scripts.containers.ScriptContainer");
taskScriptContainer
= Class.forName("net.aufdemrand.denizencore.scripts.containers.core.TaskScriptContainer");
dPlayer = Class.forName("net.aufdemrand.denizen.objects.dPlayer");
dNPC = Class.forName("net.aufdemrand.denizen.objects.dNPC");
scriptEntryData = Class.forName("net.aufdemrand.denizencore.scripts.ScriptEntryData");
bukkitScriptEntryData = Class.forName("net.aufdemrand.denizen.BukkitScriptEntryData");
// Denizen <1.1.0
} catch (Exception e2) {
// Fail silently
}
}
}
/**
* Initialize Denizen <1.1.0 methods
*/
private void initialize() {
if (!initialized) {
initialized = true;
try {
containsScriptMethod = scriptRegistry.getMethod("containsScript", String.class);
getScriptNamesMethod = scriptRegistry.getMethod("_getScriptNames");
getScriptContainerAsMethod = scriptRegistry
.getMethod("getScriptContainerAs", String.class, taskScriptContainer.getClass());
mirrorBukkitPlayerMethod = dPlayer.getMethod("mirrorBukkitPlayer", OfflinePlayer.class);
mirrorCitizensNPCMethod = dNPC.getMethod("mirrorCitizensNPC", NPC.class);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests failed to bind to Denizen, integration will not work!", e);
return;
}
}
}
public Class<?> getDenizenClass() {
return denizen;
}
@Nullable
public boolean containsScript(String input) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0.containsScript(input);
} else {
initialize();
if (scriptRegistry == null || containsScriptMethod == null) return false;
boolean script = false;
try {
script = (boolean)containsScriptMethod.invoke(scriptRegistry, input);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen ScriptRegistry#containsScript", e);
}
return script;
}
}
@Nullable
public String getScriptContainerName(String input) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0.getScriptContainerName(input);
} else {
initialize();
if (scriptRegistry == null || scriptContainer == null) return null;
String name = null;
Object instance;
try {
Constructor<?> constructor = scriptRegistry.getConstructor(YamlConfiguration.class, String.class);
instance = constructor.newInstance(null, input);
name = (String)instance.getClass().getMethod("getName").invoke(scriptContainer);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen ScriptContainer#getName", e);
}
return name;
}
}
@SuppressWarnings("unchecked")
@Nullable
public Set<String> _getScriptNames() {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0._getScriptNames();
} else {
initialize();
if (scriptRegistry == null || getScriptNamesMethod == null) return null;
Set<String> names = null;
try {
names = (Set<String>)getScriptNamesMethod.invoke(scriptRegistry);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen ScriptRegistry#_getScriptNames", e);
}
return names;
}
}
@Nullable
public Object getScriptContainerAs(String scriptName) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0.getScriptContainerAs(scriptName);
} else {
initialize();
if (scriptRegistry == null || taskScriptContainer == null) return null;
Object container = null;
try {
container = getScriptContainerAsMethod.invoke(scriptRegistry, scriptName, taskScriptContainer);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen #getScriptContainerAs", e);
}
return container;
}
}
@Nullable
public Object mirrorBukkitPlayer(Player player) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0.mirrorBukkitPlayer(player);
} else {
initialize();
if (dPlayer == null || mirrorBukkitPlayerMethod == null) return null;
Object dp = null;
try {
dp = mirrorBukkitPlayerMethod.invoke(dPlayer, player);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen dPlayer#mirrorBukkitPlayer", e);
}
return dp;
}
}
@Nullable
public Object mirrorCitizensNPC(NPC npc) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
return DenizenAPI_1_1_0.mirrorCitizensNPC(npc);
} else {
initialize();
if (dNPC == null || mirrorCitizensNPCMethod == null) return null;
Object dp = null;
try {
dp = mirrorCitizensNPCMethod.invoke(dNPC, npc);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen dNPC#mirrorCitizensNPC", e);
}
return dp;
}
}
@Nullable
public void runTaskScript(String scriptName, Player player) {
if (denizen.getName().startsWith("c")) { // com.denizenscript.denizen.*
DenizenAPI_1_1_0.runTaskScript(scriptName, player);
} else {
initialize();
if (scriptRegistry == null || bukkitScriptEntryData == null || scriptEntryData == null) return;
try {
Constructor<?> constructor = bukkitScriptEntryData.getConstructors()[0];
Object tsc = getScriptContainerAs(scriptName);
Method runTaskScript = tsc.getClass().getMethod("runTaskScript", scriptEntryData, Map.class);
runTaskScript.invoke(tsc, constructor.newInstance(mirrorBukkitPlayer(player), null), null);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING,
"Quests encountered an error invoking Denizen TaskScriptContainer#runTaskScript", e);
}
}
}
}