Support Denizen 1.1.0+ in addition to legacy versions, fixes #845

This commit is contained in:
PikaMug 2019-07-25 03:51:55 -04:00
parent f898f34e1f
commit e55a0d9290
6 changed files with 239 additions and 31 deletions

View File

@ -58,9 +58,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.aufdemrand</groupId>
<groupId>com.denizenscript</groupId>
<artifactId>denizen</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -12,20 +12,18 @@
package me.blackvein.quests;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.scripts.containers.core.TaskScriptContainer;
public class DenizenTrigger {
private Quests plugin;
public DenizenTrigger(Quests plugin) {
this.plugin = plugin;
}
protected boolean runDenizenScript(String scriptName, Quester quester) {
if (scriptName == null) {
return false;
}
if (ScriptRegistry.containsScript(scriptName)) {
TaskScriptContainer task_script = ScriptRegistry.getScriptContainerAs(scriptName, TaskScriptContainer.class);
BukkitScriptEntryData entryData = new BukkitScriptEntryData(dPlayer.mirrorBukkitPlayer(quester.getPlayer()), null);
task_script.runTaskScript(entryData, null);
if (plugin.getDependencies().getDenizenAPI().containsScript(scriptName)) {
plugin.getDependencies().getDenizenAPI().runTaskScript(scriptName, quester.getPlayer());
}
return true;
}

View File

@ -14,9 +14,9 @@ package me.blackvein.quests;
import org.bukkit.plugin.RegisteredServiceProvider;
import me.blackvein.quests.util.DenizenAPI;
import me.blackvein.quests.util.WorldGuardAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import net.aufdemrand.denizen.Denizen;
import net.citizensnpcs.api.CitizensPlugin;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
@ -42,7 +42,7 @@ public class Dependencies {
private static PhatLoots phatLoots = null;
private static PlaceholderAPIPlugin placeholder = null;
private static CitizensPlugin citizens = null;
private static Denizen denizen = null;
private static DenizenAPI denizenApi = null;
private static CitizensBooksAPI citizensBooks = null;
private static DungeonsXL dungeons = null;
private static PartiesAPI parties = null;
@ -87,8 +87,8 @@ public class Dependencies {
citizens = null;
}
public Denizen getDenizen() {
return denizen;
public DenizenAPI getDenizenAPI() {
return denizenApi;
}
public CitizensBooksAPI getCitizensBooksApi() {
@ -126,7 +126,7 @@ public class Dependencies {
worldGuardApi = new WorldGuardAPI(plugin.getServer().getPluginManager().getPlugin("WorldGuard"));
}
if (isPluginAvailable("Denizen")) {
denizen = (Denizen) plugin.getServer().getPluginManager().getPlugin("Denizen");
denizenApi = new DenizenAPI();
}
if (isPluginAvailable("mcMMO")) {
try {

View File

@ -96,7 +96,7 @@ import me.blackvein.quests.util.Lang;
import me.blackvein.quests.util.LocaleQuery;
import me.blackvein.quests.util.MiscUtil;
import me.clip.placeholderapi.PlaceholderAPI;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
@ -1984,8 +1984,8 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
List<Integer> npcAmountsToKill = new LinkedList<Integer>();
// Denizen script load
if (config.contains("quests." + questKey + ".stages.ordered." + s2 + ".script-to-run")) {
if (ScriptRegistry.containsScript(config.getString("quests." + questKey + ".stages.ordered." + s2 + ".script-to-run"))) {
trigger = new DenizenTrigger();
if (getDependencies().getDenizenAPI().containsScript(config.getString("quests." + questKey + ".stages.ordered." + s2 + ".script-to-run"))) {
trigger = new DenizenTrigger(this);
oStage.script = config.getString("quests." + questKey + ".stages.ordered." + s2 + ".script-to-run");
} else {
stageFailed("script-to-run: in Stage " + s2 + " of Quest " + quest.getName() + " is not a Denizen script!");

View File

@ -0,0 +1,210 @@
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 com.denizenscript.denizen.BukkitScriptEntryData;
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 net.citizensnpcs.api.npc.NPC;
public class DenizenAPI {
private Object denizen = null;
private Class<?> scriptRegistry = null;
private Method containsScriptGetMethod = null;
private Method _getScriptNamesGetMethod = null;
private Class<?> scriptContainer = null;
private Class<?> taskScriptContainer = null;
private Method getScriptContainerAsGetMethod = null;
private Class<?> dPlayer = null;
private Class<?> dNPC = null;
private Method mirrorBukkitPlayerGetMethod = null;
private Method mirrorCitizensNPCGetMethod = null;
private Class<?> scriptEntryData = null;
private Class<?> bukkitScriptEntryData = null;
public boolean isLegacyVersion = 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
}
}
}
private void initialize() {
if (!isLegacyVersion) {
isLegacyVersion = true;
if (denizen == null) {
try {
containsScriptGetMethod = scriptRegistry.getMethod("containsScript", String.class);
_getScriptNamesGetMethod = scriptRegistry.getMethod("_getScriptNames");
getScriptContainerAsGetMethod = scriptRegistry.getMethod("getScriptContainerAs", String.class, taskScriptContainer.getClass());
mirrorBukkitPlayerGetMethod = dPlayer.getMethod("mirrorBukkitPlayer", OfflinePlayer.class);
mirrorCitizensNPCGetMethod = 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;
}
}
}
}
@Nullable
public boolean containsScript(String input) {
if (denizen != null) {
// Denizen 1.1.0+
return ScriptRegistry.containsScript(input);
} else {
// Denizen <1.1.0
initialize();
if (scriptRegistry == null || containsScriptGetMethod == null) return false;
boolean script = false;
try {
script = (boolean)containsScriptGetMethod.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 != null) {
return ScriptRegistry.getScriptContainer(input).getName();
} 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 != null) {
return ScriptRegistry._getScriptNames();
} else {
initialize();
if (scriptRegistry == null || _getScriptNamesGetMethod == null) return null;
Set<String> names = null;
try {
names = (Set<String>)_getScriptNamesGetMethod.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 != null) {
return ScriptRegistry.getScriptContainerAs(scriptName, TaskScriptContainer.class);
} else {
initialize();
if (scriptRegistry == null || taskScriptContainer == null) return null;
Object container = null;
try {
container = getScriptContainerAsGetMethod.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 != null) {
return PlayerTag.mirrorBukkitPlayer(player);
} else {
initialize();
if (dPlayer == null || mirrorBukkitPlayerGetMethod == null) return null;
Object dp = null;
try {
dp = mirrorBukkitPlayerGetMethod.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 != null) {
return NPCTag.mirrorCitizensNPC(npc);
} else {
initialize();
if (dNPC == null || mirrorCitizensNPCGetMethod == null) return null;
Object dp = null;
try {
dp = mirrorCitizensNPCGetMethod.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 != null) {
TaskScriptContainer taskScript = ScriptRegistry.getScriptContainerAs(scriptName, TaskScriptContainer.class);
BukkitScriptEntryData entryData = new BukkitScriptEntryData(PlayerTag.mirrorBukkitPlayer(player), null);
taskScript.runTaskScript(entryData, null);
} 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);
}
}
}
}

View File

@ -17,8 +17,7 @@ import java.util.logging.Level;
import javax.annotation.Nullable;
import me.blackvein.quests.Quests;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -33,7 +32,6 @@ import com.sk89q.worldguard.protection.managers.RegionManager;
* @author NathanWolf
*/
public class WorldGuardAPI {
private Quests plugin;
private Object worldGuard = null;
private WorldGuardPlugin worldGuardPlugin = null;
private Object regionContainer = null;
@ -74,8 +72,8 @@ public class WorldGuardAPI {
private void initialize() {
if (!initialized) {
initialized = true;
// Super hacky reflection to deal with differences in WorldGuard 6 and 7+
if (worldGuard != null) {
// WorldGuard 7+
try {
Method getPlatFormMethod = worldGuard.getClass().getMethod("getPlatform");
Object platform = getPlatFormMethod.invoke(worldGuard);
@ -85,25 +83,25 @@ public class WorldGuardAPI {
Class<?> worldEditAdapterClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitAdapter");
worldAdaptMethod = worldEditAdapterClass.getMethod("adapt", World.class);
regionContainerGetMethod = regionContainer.getClass().getMethod("get", worldEditWorldClass);
} catch (Exception ex) {
plugin.getLogger().log(Level.WARNING, "Failed to bind to WorldGuard, integration will not work!", ex);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Quests failed to bind to WorldGuard, integration will not work!", e);
regionContainer = null;
return;
}
} else {
// WorldGuard <7
regionContainer = worldGuardPlugin.getRegionContainer();
try {
regionContainerGetMethod = regionContainer.getClass().getMethod("get", World.class);
} catch (Exception ex) {
plugin.getLogger().log(Level.WARNING, "Failed to bind to WorldGuard, integration will not work!", ex);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Quests failed to bind to WorldGuard, integration will not work!", e);
regionContainer = null;
return;
}
}
if (regionContainer == null) {
plugin.getLogger().warning("Failed to find RegionContainer, WorldGuard integration will not function!");
Bukkit.getLogger().warning("Quests failed to find RegionContainer, WorldGuard integration will not function!");
}
}
}
@ -115,13 +113,15 @@ public class WorldGuardAPI {
RegionManager regionManager = null;
try {
if (worldAdaptMethod != null) {
// WorldGuard 7+
Object worldEditWorld = worldAdaptMethod.invoke(null, world);
regionManager = (RegionManager)regionContainerGetMethod.invoke(regionContainer, worldEditWorld);
} else {
// WorldGuard <7
regionManager = (RegionManager)regionContainerGetMethod.invoke(regionContainer, world);
}
} catch (Exception ex) {
plugin.getLogger().log(Level.WARNING, "An error occurred looking up a WorldGuard RegionManager", ex);
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Quests encountered an error getting WorldGuard RegionManager", e);
}
return regionManager;
}