IMPORTANT : Change how API works !

This commit is contained in:
Xephi59 2015-07-07 00:21:27 +02:00
parent 627a57615a
commit d22607e8bd
6 changed files with 203 additions and 188 deletions

View File

@ -1,174 +1,188 @@
package fr.xephi.authme.api; package fr.xephi.authme.api;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.Location;
import org.bukkit.inventory.ItemStack; import org.bukkit.Server;
import org.bukkit.plugin.Plugin; import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe; import org.bukkit.inventory.ItemStack;
import fr.xephi.authme.Utils; import org.bukkit.plugin.Plugin;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.Utils;
import fr.xephi.authme.plugin.manager.CombatTagComunicator; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.plugin.manager.CombatTagComunicator;
public class API { import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Settings;
public static final String newline = System.getProperty("line.separator");
public static AuthMe instance; public class API {
public static DataSource database;
public static final String newline = System.getProperty("line.separator");
public API(AuthMe instance, DataSource database) { public static API singleton;
API.instance = instance; public AuthMe plugin;
API.database = database; public DataSource database;
}
public API(AuthMe plugin, DataSource database) {
/** this.plugin = plugin;
* Hook into AuthMe this.database = database;
* }
* @return AuthMe instance
*/ public API(Server serv) {
public static AuthMe hookAuthMe() { this.plugin = (AuthMe) serv.getPluginManager().getPlugin("AuthMe");
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("AuthMe"); this.database = this.plugin.database;
if (plugin == null || !(plugin instanceof AuthMe)) { }
return null;
} /**
return (AuthMe) plugin; * Hook into AuthMe
} *
* @return
public AuthMe getPlugin() { *
return instance; * @return AuthMe plugin
} */
public static API getInstance() {
/** if (singleton != null)
* return singleton;
* @param player Plugin p = Bukkit.getServer().getPluginManager().getPlugin("AuthMe");
* @return true if player is authenticate if (p == null || !(p instanceof AuthMe)) {
*/ return null;
public static boolean isAuthenticated(Player player) { }
return PlayerCache.getInstance().isAuthenticated(player.getName()); AuthMe authme = (AuthMe) p;
} singleton = (new API(authme, authme.database));
return singleton;
/** }
*
* @param player public AuthMe getPlugin() {
* @return true if player is a npc return plugin;
*/ }
@Deprecated
public boolean isaNPC(Player player) { /**
if (instance.getCitizensCommunicator().isNPC(player, instance)) *
return true; * @param player
return CombatTagComunicator.isNPC(player); * @return true if player is authenticate
} */
public boolean isAuthenticated(Player player) {
/** return PlayerCache.getInstance().isAuthenticated(player.getName());
* }
* @param player
* @return true if player is a npc /**
*/ *
public boolean isNPC(Player player) { * @param player
if (instance.getCitizensCommunicator().isNPC(player, instance)) * @return true if player is a npc
return true; */
return CombatTagComunicator.isNPC(player); @Deprecated
} public boolean isaNPC(Player player) {
if (plugin.getCitizensCommunicator().isNPC(player, plugin))
/** return true;
* return CombatTagComunicator.isNPC(player);
* @param player }
* @return true if the player is unrestricted
*/ /**
public static boolean isUnrestricted(Player player) { *
return Utils.getInstance().isUnrestricted(player); * @param player
} * @return true if player is a npc
*/
public static Location getLastLocation(Player player) { public boolean isNPC(Player player) {
try { if (plugin.getCitizensCommunicator().isNPC(player, plugin))
PlayerAuth auth = PlayerCache.getInstance().getAuth(player.getName().toLowerCase()); return true;
return CombatTagComunicator.isNPC(player);
if (auth != null) { }
Location loc = new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ());
return loc; /**
} else { *
return null; * @param player
} * @return true if the player is unrestricted
*/
} catch (NullPointerException ex) { public boolean isUnrestricted(Player player) {
return null; return Utils.getInstance().isUnrestricted(player);
} }
}
public Location getLastLocation(Player player) {
public static void setPlayerInventory(Player player, ItemStack[] content, try {
ItemStack[] armor) { PlayerAuth auth = PlayerCache.getInstance().getAuth(player.getName().toLowerCase());
try {
player.getInventory().setContents(content); if (auth != null) {
player.getInventory().setArmorContents(armor); Location loc = new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ());
} catch (NullPointerException npe) { return loc;
} } else {
} return null;
}
/**
* } catch (NullPointerException ex) {
* @param playerName return null;
* @return true if player is registered }
*/ }
public static boolean isRegistered(String playerName) {
String player = playerName.toLowerCase(); public void setPlayerInventory(Player player, ItemStack[] content,
return database.isAuthAvailable(player); ItemStack[] armor) {
} try {
player.getInventory().setContents(content);
/** player.getInventory().setArmorContents(armor);
* @param String } catch (NullPointerException npe) {
* playerName, String passwordToCheck }
* @return true if the password is correct , false else }
*/
public static boolean checkPassword(String playerName, /**
String passwordToCheck) { *
if (!isRegistered(playerName)) * @param playerName
return false; * @return true if player is registered
String player = playerName.toLowerCase(); */
PlayerAuth auth = database.getAuth(player); public boolean isRegistered(String playerName) {
try { String player = playerName.toLowerCase();
return PasswordSecurity.comparePasswordWithHash(passwordToCheck, auth.getHash(), playerName); return database.isAuthAvailable(player);
} catch (NoSuchAlgorithmException e) { }
return false;
} /**
} * @param String
* playerName, String passwordToCheck
/** * @return true if the password is correct , false else
* Register a player */
* public boolean checkPassword(String playerName, String passwordToCheck) {
* @param String if (!isRegistered(playerName))
* playerName, String password return false;
* @return true if the player is register correctly String player = playerName.toLowerCase();
*/ PlayerAuth auth = database.getAuth(player);
public static boolean registerPlayer(String playerName, String password) { try {
try { return PasswordSecurity.comparePasswordWithHash(passwordToCheck, auth.getHash(), playerName);
String name = playerName.toLowerCase(); } catch (NoSuchAlgorithmException e) {
String hash = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); return false;
if (isRegistered(name)) { }
return false; }
}
PlayerAuth auth = new PlayerAuth(name, hash, "198.18.0.1", 0, "your@email.com"); /**
if (!database.saveAuth(auth)) { * Register a player
return false; *
} * @param String
return true; * playerName, String password
} catch (NoSuchAlgorithmException ex) { * @return true if the player is register correctly
return false; */
} public boolean registerPlayer(String playerName, String password) {
} try {
String name = playerName.toLowerCase();
/** String hash = PasswordSecurity.getHash(Settings.getPasswordHash, password, name);
* Force a player to login if (isRegistered(name)) {
* return false;
* @param Player }
* player PlayerAuth auth = new PlayerAuth(name, hash, "198.18.0.1", 0, "your@email.com");
*/ if (!database.saveAuth(auth)) {
public static void forceLogin(Player player) { return false;
instance.management.performLogin(player, "dontneed", true); }
} return true;
} catch (NoSuchAlgorithmException ex) {
} return false;
}
}
/**
* Force a player to login
*
* @param Player
* player
*/
public void forceLogin(Player player) {
plugin.management.performLogin(player, "dontneed", true);
}
}

View File

@ -39,7 +39,6 @@ import org.bukkit.event.player.PlayerRespawnEvent;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.Utils; import fr.xephi.authme.Utils;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
@ -405,6 +404,7 @@ public class AuthMePlayerListener implements Listener {
if (player == null) if (player == null)
return; return;
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
boolean isAuthAvailable = data.isAuthAvailable(name);
if (plugin.getCitizensCommunicator().isNPC(player, plugin) || Utils.getInstance().isUnrestricted(player) || CombatTagComunicator.isNPC(player)) { if (plugin.getCitizensCommunicator().isNPC(player, plugin) || Utils.getInstance().isUnrestricted(player) || CombatTagComunicator.isNPC(player)) {
return; return;
@ -412,7 +412,7 @@ public class AuthMePlayerListener implements Listener {
if (Settings.enablePasspartu && !Settings.countriesBlacklist.isEmpty()) { if (Settings.enablePasspartu && !Settings.countriesBlacklist.isEmpty()) {
String code = plugin.getCountryCode(event.getAddress().getHostAddress()); String code = plugin.getCountryCode(event.getAddress().getHostAddress());
if (((code == null) || (Settings.countriesBlacklist.contains(code) && !API.isRegistered(name))) && !plugin.authmePermissible(player, "authme.bypassantibot")) { if (((code == null) || (Settings.countriesBlacklist.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) {
event.setKickMessage(m.send("country_banned")[0]); event.setKickMessage(m.send("country_banned")[0]);
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
return; return;
@ -420,7 +420,7 @@ public class AuthMePlayerListener implements Listener {
} }
if (Settings.enableProtection && !Settings.countries.isEmpty()) { if (Settings.enableProtection && !Settings.countries.isEmpty()) {
String code = plugin.getCountryCode(event.getAddress().getHostAddress()); String code = plugin.getCountryCode(event.getAddress().getHostAddress());
if (((code == null) || (!Settings.countries.contains(code) && !API.isRegistered(name))) && !plugin.authmePermissible(player, "authme.bypassantibot")) { if (((code == null) || (!Settings.countries.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) {
event.setKickMessage(m.send("country_banned")[0]); event.setKickMessage(m.send("country_banned")[0]);
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
return; return;
@ -441,7 +441,7 @@ public class AuthMePlayerListener implements Listener {
return; return;
} }
if (data.isAuthAvailable(name) && LimboCache.getInstance().hasLimboPlayer(name)) if (isAuthAvailable && LimboCache.getInstance().hasLimboPlayer(name))
if (Settings.isSessionsEnabled) if (Settings.isSessionsEnabled)
if (PlayerCache.getInstance().isAuthenticated(name)) if (PlayerCache.getInstance().isAuthenticated(name))
if (!Settings.sessionExpireOnIpChange) if (!Settings.sessionExpireOnIpChange)

View File

@ -17,7 +17,6 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.Utils; import fr.xephi.authme.Utils;
import fr.xephi.authme.Utils.groupType; import fr.xephi.authme.Utils.groupType;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.backup.DataFileCache; import fr.xephi.authme.cache.backup.DataFileCache;
@ -255,7 +254,7 @@ public class AsyncronousJoin {
if (!Settings.noConsoleSpam) if (!Settings.noConsoleSpam)
ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + " ..."); ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + " ...");
} else { } else {
API.setPlayerInventory(player, ev.getEmptyInventory(), ev.getEmptyArmor()); plugin.api.setPlayerInventory(player, ev.getEmptyInventory(), ev.getEmptyArmor());
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
} }

View File

@ -10,7 +10,6 @@ import org.bukkit.potion.PotionEffectType;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.Utils; import fr.xephi.authme.Utils;
import fr.xephi.authme.Utils.groupType; import fr.xephi.authme.Utils.groupType;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.backup.FileCache; import fr.xephi.authme.cache.backup.FileCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
@ -91,7 +90,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable {
RestoreInventoryEvent event = new RestoreInventoryEvent(player, limbo.getInventory(), limbo.getArmour()); RestoreInventoryEvent event = new RestoreInventoryEvent(player, limbo.getInventory(), limbo.getArmour());
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) { if (!event.isCancelled()) {
API.setPlayerInventory(player, event.getInventory(), event.getArmor()); plugin.api.setPlayerInventory(player, event.getInventory(), event.getArmor());
} }
} }
@ -146,7 +145,8 @@ public class ProcessSyncronousPlayerLogin implements Runnable {
} else { } else {
teleportBackFromSpawn(); teleportBackFromSpawn();
} }
} else if (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) { } else
if (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) {
teleportToSpawn(); teleportToSpawn();
} else if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) { } else if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) {
packQuitLocation(); packQuitLocation();

View File

@ -82,6 +82,6 @@ public class AsyncronousQuit {
PlayerCache.getInstance().removePlayer(name); PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name); database.setUnlogged(name);
AuthMePlayerListener.gameMode.remove(name); AuthMePlayerListener.gameMode.remove(name);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerQuit(player, inv, armor, isOp, isFlying, needToChange)); Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerQuit(plugin, player, inv, armor, isOp, isFlying, needToChange));
} }
} }

View File

@ -4,12 +4,13 @@ import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import fr.xephi.authme.api.API; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.events.RestoreInventoryEvent; import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
public class ProcessSyncronousPlayerQuit implements Runnable { public class ProcessSyncronousPlayerQuit implements Runnable {
protected AuthMe plugin;
protected Player player; protected Player player;
protected boolean isOp; protected boolean isOp;
protected boolean isFlying; protected boolean isFlying;
@ -17,9 +18,10 @@ public class ProcessSyncronousPlayerQuit implements Runnable {
protected ItemStack[] armor; protected ItemStack[] armor;
protected boolean needToChange; protected boolean needToChange;
public ProcessSyncronousPlayerQuit(Player player, ItemStack[] inv, public ProcessSyncronousPlayerQuit(AuthMe plugin, Player player,
ItemStack[] armor, boolean isOp, boolean isFlying, ItemStack[] inv, ItemStack[] armor, boolean isOp, boolean isFlying,
boolean needToChange) { boolean needToChange) {
this.plugin = plugin;
this.player = player; this.player = player;
this.isOp = isOp; this.isOp = isOp;
this.isFlying = isFlying; this.isFlying = isFlying;
@ -34,7 +36,7 @@ public class ProcessSyncronousPlayerQuit implements Runnable {
RestoreInventoryEvent ev = new RestoreInventoryEvent(player, inv, armor); RestoreInventoryEvent ev = new RestoreInventoryEvent(player, inv, armor);
player.getServer().getPluginManager().callEvent(ev); player.getServer().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) { if (!ev.isCancelled()) {
API.setPlayerInventory(player, ev.getInventory(), ev.getArmor()); plugin.api.setPlayerInventory(player, ev.getInventory(), ev.getArmor());
} }
} }
if (needToChange) { if (needToChange) {