mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-18 06:38:28 +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;
|
||||
|
||||
@Inject
|
||||
BungeeReceiver(AuthMe plugin, BukkitService bukkitService, Management management, DataSource dataSource,
|
||||
Settings settings) {
|
||||
BungeeReceiver(final AuthMe plugin, final BukkitService bukkitService, final Management management,
|
||||
final DataSource dataSource, final Settings settings) {
|
||||
this.plugin = plugin;
|
||||
this.bukkitService = bukkitService;
|
||||
this.management = management;
|
||||
@ -37,43 +37,41 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
public void reload(final Settings settings) {
|
||||
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
||||
|
||||
if (this.isEnabled) {
|
||||
Messenger messenger = plugin.getServer().getMessenger();
|
||||
final Messenger messenger = plugin.getServer().getMessenger();
|
||||
if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) {
|
||||
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] data) {
|
||||
if (!isEnabled) {
|
||||
private void handleBroadcast(final ByteArrayDataInput in) {
|
||||
// Read data byte array
|
||||
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;
|
||||
}
|
||||
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(data);
|
||||
String subchannel = in.readUTF();
|
||||
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;
|
||||
// Parse argument
|
||||
final String argument;
|
||||
try {
|
||||
argument = in.readUTF();
|
||||
argument = dataIn.readUTF();
|
||||
} 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;
|
||||
}
|
||||
|
||||
// Handle type
|
||||
switch (type.get()) {
|
||||
case UNREGISTER:
|
||||
dataSource.invalidateCache(argument);
|
||||
@ -84,6 +82,29 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent
|
||||
case REFRESH:
|
||||
dataSource.refreshCache(argument);
|
||||
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:
|
||||
performLogin(argument);
|
||||
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);
|
||||
if (player != null && player.isOnline()) {
|
||||
management.forceLogin(player);
|
||||
ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, "
|
||||
+ "as requested via plugin messaging.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,8 @@ public class BungeeSender implements SettingsDependent {
|
||||
* Constructor.
|
||||
*/
|
||||
@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.bukkitService = bukkitService;
|
||||
this.dataSource = dataSource;
|
||||
@ -35,12 +36,12 @@ public class BungeeSender implements SettingsDependent {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
public void reload(final Settings settings) {
|
||||
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
|
||||
this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER);
|
||||
|
||||
if (this.isEnabled) {
|
||||
Messenger messenger = plugin.getServer().getMessenger();
|
||||
final Messenger messenger = plugin.getServer().getMessenger();
|
||||
if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) {
|
||||
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
|
||||
}
|
||||
@ -51,21 +52,36 @@ public class BungeeSender implements SettingsDependent {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
private void sendBungeecordMessage(String... data) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
for (String element : data) {
|
||||
private void sendBungeecordMessage(final String... data) {
|
||||
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
for (final String element : data) {
|
||||
out.writeUTF(element);
|
||||
}
|
||||
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
|
||||
* do nothing.
|
||||
*
|
||||
* @param player The player to send.
|
||||
*/
|
||||
public void connectPlayerOnLogin(Player player) {
|
||||
public void connectPlayerOnLogin(final Player player) {
|
||||
if (isEnabled && !destinationServerOnLogin.isEmpty()) {
|
||||
bukkitService.scheduleSyncDelayedTask(() ->
|
||||
sendBungeecordMessage("ConnectOther", player.getName(), destinationServerOnLogin), 5L);
|
||||
@ -78,19 +94,19 @@ public class BungeeSender implements SettingsDependent {
|
||||
* @param type The message type, See {@link MessageType}
|
||||
* @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 (!plugin.isEnabled()) {
|
||||
ConsoleLogger.debug("Tried to send a " + type + " bungeecord message but the plugin was disabled!");
|
||||
return;
|
||||
}
|
||||
if(type.isRequiresCaching() && !dataSource.isCached()) {
|
||||
if (type.isRequiresCaching() && !dataSource.isCached()) {
|
||||
return;
|
||||
}
|
||||
if (type.isBroadcast()) {
|
||||
sendBungeecordMessage("Forward", "ALL", "AuthMe", type.getId(), playerName.toLowerCase());
|
||||
sendForwardedBungeecordMessage("AuthMe.v2.Broadcast", type.getId(), playerName.toLowerCase());
|
||||
} 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 requiresCaching;
|
||||
|
||||
MessageType(String id, boolean broadcast, boolean requiresCaching) {
|
||||
MessageType(final String id, final boolean broadcast, final boolean requiresCaching) {
|
||||
this.id = id;
|
||||
this.broadcast = broadcast;
|
||||
this.requiresCaching = requiresCaching;
|
||||
}
|
||||
|
||||
MessageType(String id, boolean broadcast) {
|
||||
MessageType(final String id, final boolean broadcast) {
|
||||
this(id, broadcast, false);
|
||||
}
|
||||
|
||||
@ -47,8 +47,8 @@ public enum MessageType {
|
||||
*
|
||||
* @return the MessageType with the given id, empty if invalid.
|
||||
*/
|
||||
public static Optional<MessageType> fromId(String id) {
|
||||
for (MessageType current : values()) {
|
||||
public static Optional<MessageType> fromId(final String id) {
|
||||
for (final MessageType current : values()) {
|
||||
if (current.getId().equals(id)) {
|
||||
return Optional.of(current);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user