From 9b3617ecbac0907f6a690c03a4d15f271ed930ac Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Thu, 17 Oct 2013 20:29:21 +0200 Subject: [PATCH] AuthMe 3.0 Support --- pom.xml | 8 ++- .../minecraft/limitedcreative/Hooks.java | 40 ++++++++++- .../limitedcreative/ModInventories.java | 7 +- .../minecraft/limitedcreative/ModRegions.java | 4 ++ .../limitedcreative/hooks/AuthMe3Hooks.java | 69 +++++++++++++++++++ .../limitedcreative/hooks/AuthMeHooks.java | 1 + .../inventories/AuthMe3Inventories.java | 52 ++++++++++++++ .../inventories/AuthMeInventories.java | 1 + 8 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMe3Hooks.java create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMe3Inventories.java diff --git a/pom.xml b/pom.xml index 1cd3011..0880d09 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,12 @@ authme 2.9.5 + + + fr.xephi + authme + 3.0 + com.onarandombox.multiversecore @@ -96,7 +102,7 @@ multiworld multiworld - 4.6.6 + 4.7.2 diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java index 96f3775..8c4266f 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java @@ -4,6 +4,7 @@ import org.bukkit.Bukkit; import de.jaschastarke.hooking.BooleanHooker; import de.jaschastarke.hooking.GetHooker; +import de.jaschastarke.minecraft.limitedcreative.hooks.AuthMe3Hooks; import de.jaschastarke.minecraft.limitedcreative.hooks.AuthMeHooks; import de.jaschastarke.minecraft.limitedcreative.hooks.MultiVerseHooks; import de.jaschastarke.minecraft.limitedcreative.hooks.PlayerCheckHooker; @@ -26,10 +27,12 @@ public final class Hooks { IsMultiVerse.clearHooks(); InventoryIncompatible.clearHooks(); - if (isPluginEnabled("AuthMe")) { + if (isAuthMePresent()) { new AuthMeHooks(plugin); + } else if (isAuthMe3Present()) { + plugin.getModules().addSharedModule(new AuthMe3Hooks(plugin)); } - if (isPluginEnabled("xAuth")) { + if (isXAuth20Present()) { new xAuthHooks(plugin); } if (isPluginEnabled("Multiverse-Core")) { @@ -49,4 +52,37 @@ public final class Hooks { } }); } + + public static boolean isAuthMePresent() { + if (isPluginEnabled("AuthMe")) { + try { + return Class.forName("uk.org.whoami.authme.api.API") != null; + } catch (ClassNotFoundException e) { + return false; + } + } + return false; + } + + public static boolean isAuthMe3Present() { + if (isPluginEnabled("AuthMe")) { + try { + return Class.forName("fr.xephi.authme.api.API") != null; + } catch (ClassNotFoundException e) { + return false; + } + } + return false; + } + + public static boolean isXAuth20Present() { + if (isPluginEnabled("xAuth")) { + try { + return Class.forName("com.cypherx.xauth.xAuth") != null; + } catch (ClassNotFoundException e) { + return false; + } + } + return false; + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java index 1b968ef..3fcbc30 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java @@ -9,7 +9,9 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.minecraft.limitedcreative.hooks.AuthMe3Hooks; import de.jaschastarke.minecraft.limitedcreative.inventories.ArmoryConfig; +import de.jaschastarke.minecraft.limitedcreative.inventories.AuthMe3Inventories; import de.jaschastarke.minecraft.limitedcreative.inventories.AuthMeInventories; import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; import de.jaschastarke.minecraft.limitedcreative.inventories.InventoryConfig; @@ -41,8 +43,11 @@ public class ModInventories extends CoreModule { config = plugin.getPluginConfig().registerSection(new InventoryConfig(this, entry)); armor_config = config.registerSection(new ArmoryConfig(this)); - if (plugin.getServer().getPluginManager().isPluginEnabled("AuthMe")) { + if (Hooks.isAuthMePresent()) { addModule(new AuthMeInventories(plugin, this)); + } else if (Hooks.isAuthMe3Present()) { + modules.linkSharedModule(AuthMe3Hooks.class, plugin.getModules()); + addModule(new AuthMe3Inventories(plugin, this)); } String incomp = Hooks.InventoryIncompatible.test(); if (config.getEnabled() && incomp != null) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java index 4ebffc9..8f1ce93 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.bukkit.lib.commands.AliasHelpedCommand; +import de.jaschastarke.minecraft.limitedcreative.hooks.AuthMe3Hooks; import de.jaschastarke.minecraft.limitedcreative.regions.BlockListener; import de.jaschastarke.minecraft.limitedcreative.regions.IWorldGuardIntegration; import de.jaschastarke.minecraft.limitedcreative.regions.PlayerData; @@ -49,6 +50,9 @@ public class ModRegions extends CoreModule { entry.deactivateUsage(); return; } + if (Hooks.isAuthMe3Present()) { + modules.linkSharedModule(AuthMe3Hooks.class, plugin.getModules()); + } command = new RegionsCommand(this); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMe3Hooks.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMe3Hooks.java new file mode 100644 index 0000000..9a894bf --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMe3Hooks.java @@ -0,0 +1,69 @@ +package de.jaschastarke.minecraft.limitedcreative.hooks; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.plugin.IllegalPluginAccessException; + +import de.jaschastarke.bukkit.lib.SimpleModule; +import de.jaschastarke.minecraft.limitedcreative.Hooks; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; +import fr.xephi.authme.api.API; +import fr.xephi.authme.events.LoginEvent; + +public class AuthMe3Hooks extends SimpleModule implements Listener { + private Map users = new HashMap(); + private PlayerCheckHooker.Check check; + public AuthMe3Hooks(final LimitedCreative plugin) { + super(plugin); + check = new PlayerCheckHooker.Check() { + @Override + public boolean test(Player player) { + if (users.containsKey(player.getName())) { + if (plugin.isDebug()) // not nessesary, but so no string concation without debug needed + plugin.getLog().debug("AuthMe: "+player.getName()+": Event logged in: "+users.get(player.getName())); + return users.get(player.getName()); + } else { + boolean li = API.isAuthenticated(player); + if (plugin.isDebug()) // not nessesary, but so no string concation without debug needed + plugin.getLog().debug("AuthMe: "+player.getName()+": logged in: "+li); + return li; + } + } + }; + } + + @Override + public void onEnable() { + try { + super.onEnable(); + } catch (IllegalPluginAccessException e) { + enabled = true; + plugin.getLog().warn("AuthMe 3.0 Bug detected. Update AuthMe to a newer version as soon as avilable."); + } + Hooks.IsLoggedIn.register(check); + } + + @Override + public void onDisable() { + super.onDisable(); + Hooks.IsLoggedIn.unregister(check); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLogin(PlayerLoginEvent event) { + if (event.getPlayer() == null) + return; + users.put(event.getPlayer().getName(), API.isAuthenticated(event.getPlayer())); + } + + @EventHandler + public void onAuthMeLogin(LoginEvent event) { + users.put(event.getPlayer().getName(), true); + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMeHooks.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMeHooks.java index a0f511c..d665b72 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMeHooks.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/hooks/AuthMeHooks.java @@ -6,6 +6,7 @@ import uk.org.whoami.authme.api.API; import de.jaschastarke.minecraft.limitedcreative.Hooks; import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; +@Deprecated // AuthMe 3.0 released. Compatibility for older versions will be removed sometime public class AuthMeHooks { public AuthMeHooks(final LimitedCreative plugin) { Hooks.IsLoggedIn.register(new PlayerCheckHooker.Check() { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMe3Inventories.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMe3Inventories.java new file mode 100644 index 0000000..3707bdd --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMe3Inventories.java @@ -0,0 +1,52 @@ +package de.jaschastarke.minecraft.limitedcreative.inventories; + +import org.bukkit.GameMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import de.jaschastarke.bukkit.lib.SimpleModule; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; +import de.jaschastarke.minecraft.limitedcreative.ModInventories; +import fr.xephi.authme.events.RestoreInventoryEvent; +import fr.xephi.authme.events.StoreInventoryEvent; + +public class AuthMe3Inventories extends SimpleModule implements Listener { + ModInventories invmod; + public AuthMe3Inventories(LimitedCreative plugin, ModInventories modInventories) { + super(plugin); + invmod = modInventories; + } + + @EventHandler + public void onStoreInventory(StoreInventoryEvent event) { + if (isDebug()) + getLog().debug("AuthMe Store Event: "+event.getPlayer().getName()); + + event.getPlayer().closeInventory(); + GameMode cgm = event.getPlayer().getGameMode(); + + if (cgm == GameMode.ADVENTURE && !invmod.getConfig().getSeparateAdventure()) + cgm = GameMode.SURVIVAL; + + if (cgm != GameMode.CREATIVE || invmod.getConfig().getStoreCreative()) { + invmod.getInventory(event.getPlayer()).save(cgm); + } + } + @EventHandler + public void onRestoreInventory(RestoreInventoryEvent event) { + if (isDebug()) + getLog().debug("AuthMe Restore Event: "+event.getPlayer().getName()); + + event.getPlayer().closeInventory(); + GameMode cgm = event.getPlayer().getGameMode(); + + if (cgm == GameMode.ADVENTURE && !invmod.getConfig().getSeparateAdventure()) + cgm = GameMode.SURVIVAL; + + Inventory inv = invmod.getInventory(event.getPlayer()); + if (inv.isStored(cgm)) { + inv.load(cgm); + event.setCancelled(true); + } + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java index d8067de..b0d109b 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java @@ -10,6 +10,7 @@ import de.jaschastarke.bukkit.lib.SimpleModule; import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; import de.jaschastarke.minecraft.limitedcreative.ModInventories; +@Deprecated // AuthMe 3.0 released. Compatibility for older versions will be removed sometime public class AuthMeInventories extends SimpleModule implements Listener { ModInventories invmod; public AuthMeInventories(LimitedCreative plugin, ModInventories modInventories) {