mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-19 07:07:55 +01:00
Change how messaging actually works, support broadcast messages
This commit is contained in:
parent
e0d8caa95b
commit
75e7a11d0d
@ -27,8 +27,8 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
|||||||
private boolean isEnabled;
|
private boolean isEnabled;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
BungeeReceiver(AuthMe plugin, BukkitService bukkitService, Management management, DataSource dataSource,
|
BungeeReceiver(final AuthMe plugin, final BukkitService bukkitService, final Management management,
|
||||||
Settings settings) {
|
final DataSource dataSource, final Settings settings) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.bukkitService = bukkitService;
|
this.bukkitService = bukkitService;
|
||||||
this.management = management;
|
this.management = management;
|
||||||
@ -37,43 +37,41 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reload(Settings settings) {
|
public void reload(final Settings settings) {
|
||||||
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
||||||
|
|
||||||
if (this.isEnabled) {
|
if (this.isEnabled) {
|
||||||
Messenger messenger = plugin.getServer().getMessenger();
|
final Messenger messenger = plugin.getServer().getMessenger();
|
||||||
if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) {
|
if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) {
|
||||||
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this);
|
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void handleBroadcast(final ByteArrayDataInput in) {
|
||||||
public void onPluginMessageReceived(String channel, Player player, byte[] data) {
|
// Read data byte array
|
||||||
if (!isEnabled) {
|
final short dataLength = in.readShort();
|
||||||
|
final byte[] dataBytes = new byte[dataLength];
|
||||||
|
in.readFully(dataBytes);
|
||||||
|
final ByteArrayDataInput dataIn = ByteStreams.newDataInput(dataBytes);
|
||||||
|
|
||||||
|
// Parse type
|
||||||
|
final Optional<MessageType> type = MessageType.fromId(dataIn.readUTF());
|
||||||
|
if (!type.isPresent()) {
|
||||||
|
ConsoleLogger.debug("Received unsupported forwarded bungeecord message type! ({0})", type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArrayDataInput in = ByteStreams.newDataInput(data);
|
// Parse argument
|
||||||
String subchannel = in.readUTF();
|
final String argument;
|
||||||
if (!"AuthMe".equals(subchannel)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<MessageType> type = MessageType.fromId(in.readUTF());
|
|
||||||
if(!type.isPresent()) {
|
|
||||||
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String argument;
|
|
||||||
try {
|
try {
|
||||||
argument = in.readUTF();
|
argument = dataIn.readUTF();
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
ConsoleLogger.warning("Received invalid plugin message of type " + type.get().name() + ": argument is missing!");
|
ConsoleLogger.warning("Received invalid forwarded plugin message of type " + type.get().name() + ": argument is missing!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle type
|
||||||
switch (type.get()) {
|
switch (type.get()) {
|
||||||
case UNREGISTER:
|
case UNREGISTER:
|
||||||
dataSource.invalidateCache(argument);
|
dataSource.invalidateCache(argument);
|
||||||
@ -84,6 +82,29 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
|||||||
case REFRESH:
|
case REFRESH:
|
||||||
dataSource.refreshCache(argument);
|
dataSource.refreshCache(argument);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handle(final ByteArrayDataInput in) {
|
||||||
|
// Parse type
|
||||||
|
final Optional<MessageType> type = MessageType.fromId(in.readUTF());
|
||||||
|
if (!type.isPresent()) {
|
||||||
|
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse argument
|
||||||
|
final String argument;
|
||||||
|
try {
|
||||||
|
argument = in.readUTF();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
ConsoleLogger.warning("Received invalid plugin message of type " + type.get().name() + ": argument is missing!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle type
|
||||||
|
switch (type.get()) {
|
||||||
case PERFORM_LOGIN:
|
case PERFORM_LOGIN:
|
||||||
performLogin(argument);
|
performLogin(argument);
|
||||||
break;
|
break;
|
||||||
@ -91,14 +112,30 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void performLogin(String name) {
|
@Override
|
||||||
|
public void onPluginMessageReceived(final String channel, final Player player, final byte[] data) {
|
||||||
|
if (!isEnabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ByteArrayDataInput in = ByteStreams.newDataInput(data);
|
||||||
|
|
||||||
|
// Check subchannel
|
||||||
|
final String subChannel = in.readUTF();
|
||||||
|
if ("AuthMe.v2.Broadcast".equals(subChannel)) {
|
||||||
|
handleBroadcast(in);
|
||||||
|
} else if ("AuthMe.v2".equals(subChannel)) {
|
||||||
|
handle(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void performLogin(final String name) {
|
||||||
Player player = bukkitService.getPlayerExact(name);
|
Player player = bukkitService.getPlayerExact(name);
|
||||||
if (player != null && player.isOnline()) {
|
if (player != null && player.isOnline()) {
|
||||||
management.forceLogin(player);
|
management.forceLogin(player);
|
||||||
ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, "
|
ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, "
|
||||||
+ "as requested via plugin messaging.");
|
+ "as requested via plugin messaging.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,8 @@ public class BungeeSender implements SettingsDependent {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
@Inject
|
@Inject
|
||||||
BungeeSender(AuthMe plugin, BukkitService bukkitService, DataSource dataSource, Settings settings) {
|
BungeeSender(final AuthMe plugin, final BukkitService bukkitService, final DataSource dataSource,
|
||||||
|
final Settings settings) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.bukkitService = bukkitService;
|
this.bukkitService = bukkitService;
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
@ -35,12 +36,12 @@ public class BungeeSender implements SettingsDependent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reload(Settings settings) {
|
public void reload(final Settings settings) {
|
||||||
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
||||||
this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER);
|
this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER);
|
||||||
|
|
||||||
if (this.isEnabled) {
|
if (this.isEnabled) {
|
||||||
Messenger messenger = plugin.getServer().getMessenger();
|
final Messenger messenger = plugin.getServer().getMessenger();
|
||||||
if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) {
|
if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) {
|
||||||
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
|
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
|
||||||
}
|
}
|
||||||
@ -51,21 +52,36 @@ public class BungeeSender implements SettingsDependent {
|
|||||||
return isEnabled;
|
return isEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendBungeecordMessage(String... data) {
|
private void sendBungeecordMessage(final String... data) {
|
||||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||||
for (String element : data) {
|
for (final String element : data) {
|
||||||
out.writeUTF(element);
|
out.writeUTF(element);
|
||||||
}
|
}
|
||||||
bukkitService.sendBungeeMessage(out.toByteArray());
|
bukkitService.sendBungeeMessage(out.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendForwardedBungeecordMessage(final String subChannel, final String... data) {
|
||||||
|
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||||
|
out.writeUTF("Forward");
|
||||||
|
out.writeUTF("ALL");
|
||||||
|
out.writeUTF(subChannel);
|
||||||
|
final ByteArrayDataOutput dataOut = ByteStreams.newDataOutput();
|
||||||
|
for (final String element : data) {
|
||||||
|
dataOut.writeUTF(element);
|
||||||
|
}
|
||||||
|
final byte[] dataBytes = dataOut.toByteArray();
|
||||||
|
out.writeShort(dataBytes.length);
|
||||||
|
out.write(dataBytes);
|
||||||
|
bukkitService.sendBungeeMessage(out.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a player to a specified server. If no server is configured, this will
|
* Send a player to a specified server. If no server is configured, this will
|
||||||
* do nothing.
|
* do nothing.
|
||||||
*
|
*
|
||||||
* @param player The player to send.
|
* @param player The player to send.
|
||||||
*/
|
*/
|
||||||
public void connectPlayerOnLogin(Player player) {
|
public void connectPlayerOnLogin(final Player player) {
|
||||||
if (isEnabled && !destinationServerOnLogin.isEmpty()) {
|
if (isEnabled && !destinationServerOnLogin.isEmpty()) {
|
||||||
bukkitService.scheduleSyncDelayedTask(() ->
|
bukkitService.scheduleSyncDelayedTask(() ->
|
||||||
sendBungeecordMessage("ConnectOther", player.getName(), destinationServerOnLogin), 5L);
|
sendBungeecordMessage("ConnectOther", player.getName(), destinationServerOnLogin), 5L);
|
||||||
@ -78,19 +94,19 @@ public class BungeeSender implements SettingsDependent {
|
|||||||
* @param type The message type, See {@link MessageType}
|
* @param type The message type, See {@link MessageType}
|
||||||
* @param playerName the player related to the message
|
* @param playerName the player related to the message
|
||||||
*/
|
*/
|
||||||
public void sendAuthMeBungeecordMessage(MessageType type, String playerName) {
|
public void sendAuthMeBungeecordMessage(final MessageType type, final String playerName) {
|
||||||
if (isEnabled) {
|
if (isEnabled) {
|
||||||
if (!plugin.isEnabled()) {
|
if (!plugin.isEnabled()) {
|
||||||
ConsoleLogger.debug("Tried to send a " + type + " bungeecord message but the plugin was disabled!");
|
ConsoleLogger.debug("Tried to send a " + type + " bungeecord message but the plugin was disabled!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(type.isRequiresCaching() && !dataSource.isCached()) {
|
if (type.isRequiresCaching() && !dataSource.isCached()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (type.isBroadcast()) {
|
if (type.isBroadcast()) {
|
||||||
sendBungeecordMessage("Forward", "ALL", "AuthMe", type.getId(), playerName.toLowerCase());
|
sendForwardedBungeecordMessage("AuthMe.v2.Broadcast", type.getId(), playerName.toLowerCase());
|
||||||
} else {
|
} else {
|
||||||
sendBungeecordMessage("AuthMe", type.getId(), playerName.toLowerCase());
|
sendBungeecordMessage("AuthMe.v2", type.getId(), playerName.toLowerCase());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,13 @@ public enum MessageType {
|
|||||||
private final boolean broadcast;
|
private final boolean broadcast;
|
||||||
private final boolean requiresCaching;
|
private final boolean requiresCaching;
|
||||||
|
|
||||||
MessageType(String id, boolean broadcast, boolean requiresCaching) {
|
MessageType(final String id, final boolean broadcast, final boolean requiresCaching) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.broadcast = broadcast;
|
this.broadcast = broadcast;
|
||||||
this.requiresCaching = requiresCaching;
|
this.requiresCaching = requiresCaching;
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageType(String id, boolean broadcast) {
|
MessageType(final String id, final boolean broadcast) {
|
||||||
this(id, broadcast, false);
|
this(id, broadcast, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,8 +47,8 @@ public enum MessageType {
|
|||||||
*
|
*
|
||||||
* @return the MessageType with the given id, empty if invalid.
|
* @return the MessageType with the given id, empty if invalid.
|
||||||
*/
|
*/
|
||||||
public static Optional<MessageType> fromId(String id) {
|
public static Optional<MessageType> fromId(final String id) {
|
||||||
for (MessageType current : values()) {
|
for (final MessageType current : values()) {
|
||||||
if (current.getId().equals(id)) {
|
if (current.getId().equals(id)) {
|
||||||
return Optional.of(current);
|
return Optional.of(current);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user