Fix messaging [WIP] (#1614)

* Fix messaging [WIP]

* Make codeclimate happy

* Codeclimate, pls

* got it, i hope

* Improvements

* Remove duplicated checks, other improvements, make login/logout broadcasts

* Optimize project imports

* Make codeclimate happy again
This commit is contained in:
Gabriele C
2018-08-26 15:29:47 +02:00
committed by GitHub
parent adb3c06f51
commit da7e7edc37
25 changed files with 130 additions and 59 deletions
@@ -13,12 +13,12 @@ import com.maxmind.db.Reader.FileMode;
import com.maxmind.db.cache.CHMCache;
import com.maxmind.db.model.Country;
import com.maxmind.db.model.CountryResponse;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.InternetProtocolUtils;
import javax.inject.Inject;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
@@ -41,8 +41,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.zip.GZIPInputStream;
import javax.inject.Inject;
public class GeoIpService {
private static final String LICENSE =
@@ -15,6 +15,7 @@ import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.PluginMessageListener;
import javax.inject.Inject;
import java.util.Optional;
public class BungeeReceiver implements PluginMessageListener, SettingsDependent {
@@ -59,32 +60,36 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
return;
}
String type = in.readUTF();
String name = in.readUTF();
switch (type) {
case MessageType.UNREGISTER:
dataSource.invalidateCache(name);
Optional<MessageType> type = MessageType.fromId(in.readUTF());
if(!type.isPresent()) {
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
return;
}
String argument = in.readUTF();
switch (type.get()) {
case UNREGISTER:
dataSource.invalidateCache(argument);
break;
case MessageType.REFRESH_PASSWORD:
case MessageType.REFRESH_QUITLOC:
case MessageType.REFRESH_EMAIL:
case MessageType.REFRESH:
dataSource.refreshCache(name);
case REFRESH_PASSWORD:
case REFRESH_QUITLOC:
case REFRESH_EMAIL:
case REFRESH:
dataSource.refreshCache(argument);
break;
case MessageType.BUNGEE_LOGIN:
handleBungeeLogin(name);
case PERFORM_LOGIN:
performLogin(argument);
break;
default:
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
}
}
private void handleBungeeLogin(String name) {
private void performLogin(String name) {
Player player = bukkitService.getPlayerExact(name);
if (player != null && player.isOnline()) {
management.forceLogin(player);
ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, "
+ "as requested by the AuthMeBungee integration.");
+ "as requested via plugin messaging.");
}
}
@@ -4,6 +4,7 @@ import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
@@ -17,6 +18,7 @@ public class BungeeSender implements SettingsDependent {
private final AuthMe plugin;
private final BukkitService bukkitService;
private final DataSource dataSource;
private boolean isEnabled;
private String destinationServerOnLogin;
@@ -25,9 +27,10 @@ public class BungeeSender implements SettingsDependent {
* Constructor.
*/
@Inject
BungeeSender(AuthMe plugin, BukkitService bukkitService, Settings settings) {
BungeeSender(AuthMe plugin, BukkitService bukkitService, DataSource dataSource, Settings settings) {
this.plugin = plugin;
this.bukkitService = bukkitService;
this.dataSource = dataSource;
reload(settings);
}
@@ -75,13 +78,20 @@ public class BungeeSender implements SettingsDependent {
* @param type The message type, See {@link MessageType}
* @param playerName the player related to the message
*/
public void sendAuthMeBungeecordMessage(String type, String playerName) {
public void sendAuthMeBungeecordMessage(MessageType type, String playerName) {
if (isEnabled) {
if(!plugin.isEnabled()) {
if (!plugin.isEnabled()) {
ConsoleLogger.debug("Tried to send a " + type + " bungeecord message but the plugin was disabled!");
return;
}
sendBungeecordMessage("AuthMe", type, playerName.toLowerCase());
if(type.isRequiresCaching() && !dataSource.isCached()) {
return;
}
if (type.isBroadcast()) {
sendBungeecordMessage("Forward", "ALL", "AuthMe", type.getId(), playerName.toLowerCase());
} else {
sendBungeecordMessage("AuthMe", type.getId(), playerName.toLowerCase());
}
}
}
@@ -1,19 +1,59 @@
package fr.xephi.authme.service.bungeecord;
public final class MessageType {
import java.util.Optional;
public static final String LOGIN = "login";
public static final String LOGOUT = "logout";
public static final String REGISTER = "register";
public static final String UNREGISTER = "unregister";
public static final String REFRESH_PASSWORD = "refresh.password";
public static final String REFRESH_SESSION = "refresh.session";
public static final String REFRESH_QUITLOC = "refresh.quitloc";
public static final String REFRESH_EMAIL = "refresh.email";
public static final String REFRESH = "refresh";
public static final String BUNGEE_LOGIN = "bungeelogin";
public enum MessageType {
REFRESH_PASSWORD("refresh.password", true),
REFRESH_SESSION("refresh.session", true),
REFRESH_QUITLOC("refresh.quitloc", true),
REFRESH_EMAIL("refresh.email", true),
REFRESH("refresh", true),
REGISTER("register", true),
UNREGISTER("unregister", true),
LOGIN("login", true),
LOGOUT("logout", true),
PERFORM_LOGIN("perform.login", false);
private MessageType() {
private final String id;
private final boolean broadcast;
private final boolean requiresCaching;
MessageType(String id, boolean broadcast, boolean requiresCaching) {
this.id = id;
this.broadcast = broadcast;
this.requiresCaching = requiresCaching;
}
MessageType(String id, boolean broadcast) {
this(id, broadcast, false);
}
public String getId() {
return id;
}
public boolean isBroadcast() {
return broadcast;
}
public boolean isRequiresCaching() {
return requiresCaching;
}
/**
* Returns the MessageType with the given ID.
*
* @param id the message type id.
*
* @return the MessageType with the given id, empty if invalid.
*/
public static Optional<MessageType> fromId(String id) {
for (MessageType current : values()) {
if (current.getId().equals(id)) {
return Optional.of(current);
}
}
return Optional.empty();
}
}