Skip to content

Commit

Permalink
Queue packets if all players are unencrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Apr 16, 2022
1 parent c9b44b7 commit ada03c7
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.azisaba.azipluginmessaging.api.entity.Player;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.protocol.PacketQueue;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -48,6 +49,13 @@ public interface AziPluginMessaging {
*/
<T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz);

/**
* Returns the packet queue.
* @return the packet queue
*/
@NotNull
PacketQueue getPacketQueue();

interface Proxy {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.azisaba.azipluginmessaging.api.protocol;

import net.azisaba.azipluginmessaging.api.protocol.message.Message;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import org.jetbrains.annotations.NotNull;

public abstract class PacketQueue {
public static final PacketQueue EMPTY = new Empty();

public abstract void add(@NotNull Protocol<?, ?> protocol, @NotNull Message message);
public abstract void flush(@NotNull PacketSender sender);

private static class Empty extends PacketQueue {
private Empty() {
}

@Override
public void add(@NotNull Protocol<?, ?> protocol, @NotNull Message message) {
}

@Override
public void flush(@NotNull PacketSender sender) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.azisaba.azipluginmessaging.api.protocol;

import net.azisaba.azipluginmessaging.api.AziPluginMessagingProvider;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.protocol.handler.MessageHandler;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyMessageHandler;
Expand Down Expand Up @@ -100,13 +101,22 @@ public T getHandler() {

/**
* Attempt to send a packet.
* @param sender the packet sender to send the packet from.
* @param sender the packet sender to send the packet from. if null, the packet will always be queued.
* @param msg the message to send
* @return true if the message was sent successfully, false otherwise.
*/
public boolean sendPacket(@NotNull PacketSender sender, @NotNull M msg) {
if (id != 0 && !sender.isEncrypted()) {
throw new IllegalStateException("Cannot send packet " + id + " without encryption");
public boolean sendPacket(@Nullable PacketSender sender, @NotNull M msg) {
if (id != 0 && (sender == null || !sender.isEncrypted())) {
if (this.getPacketFlow() == PacketFlow.TO_SERVER) {
throw new IllegalStateException("Cannot send packet " + id + " without encryption");
}
// usually it should get executed soon after the encryption is done
AziPluginMessagingProvider.get().getPacketQueue().add(this, msg);
Logger.getCurrentLogger().info("Queued packet " + id + " to send after encryption (sender: {})", sender);
return true;
}
if (sender == null) {
throw new IllegalArgumentException("Cannot send packet " + id + " without sender");
}
try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.azisaba.azipluginmessaging.api.protocol.handler;

import net.azisaba.azipluginmessaging.api.AziPluginMessagingProvider;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.entity.Player;
import net.azisaba.azipluginmessaging.api.protocol.message.EncryptionMessage;
Expand Down Expand Up @@ -38,5 +39,7 @@ public void handle(@NotNull PacketSender sender, @NotNull EncryptionMessage msg)
if (Constants.DEBUG) {
Logger.getCurrentLogger().info("Encryption enabled for " + sender);
}

AziPluginMessagingProvider.get().getPacketQueue().flush(sender);
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "net.azisaba.azipluginmessaging"
version = "2.0.3"
version = "2.1.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
import net.azisaba.azipluginmessaging.api.AziPluginMessaging;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.protocol.PacketQueue;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.spigot.entity.PlayerImpl;
import net.azisaba.azipluginmessaging.spigot.protocol.SimplePacketQueue;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

public class AziPluginMessagingSpigot implements AziPluginMessaging {
private final PacketQueue packetQueue = new SimplePacketQueue();
private final Logger logger;
private final Server server;
private final ServerImpl server;

public AziPluginMessagingSpigot(@NotNull SpigotPlugin plugin) {
this.logger = Logger.createFromJavaLogger(plugin.getLogger());
Expand All @@ -34,7 +38,7 @@ public AziPluginMessagingSpigot(@NotNull SpigotPlugin plugin) {
}

@Override
public @NotNull Server getServer() {
public @NotNull ServerImpl getServer() {
return server;
}

Expand All @@ -52,6 +56,11 @@ public <T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz) {
return (PlayerAdapter<T>) (PlayerAdapter<Player>) PlayerImpl::of;
}

@Override
public @NotNull PacketQueue getPacketQueue() {
return packetQueue;
}

public static class ServerImpl implements Server {
@Override
public @NotNull PacketSender getPacketSender() {
Expand All @@ -64,5 +73,15 @@ public static class ServerImpl implements Server {
Optional<PlayerImpl> encryptedPlayer = players.stream().filter(PlayerImpl::isEncrypted).findAny();
return encryptedPlayer.orElseGet(() -> players.get(0));
}

public @Nullable PacketSender getPacketSenderOrNull() {
try {
PacketSender sender = getPacketSender();
if (sender.isEncrypted()) {
return sender;
}
} catch (RuntimeException ignored) {}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.security.KeyPair;
import java.util.Objects;
Expand Down Expand Up @@ -50,6 +51,14 @@ public static PacketSender getAnyPacketSender() {
return AziPluginMessagingProvider.get().getServer().getPacketSender();
}

@Nullable
public static PacketSender getAnyPacketSenderOrNull() {
if (Bukkit.getOnlinePlayers().isEmpty()) {
return null;
}
return ((AziPluginMessagingSpigot.ServerImpl) AziPluginMessagingProvider.get().getServer()).getPacketSenderOrNull();
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String[] args) {
return;
}
Player target = PlayerUtil.getOfflinePlayer(args[0]);
boolean res = Protocol.P_GIVE_GAMING_SARA.sendPacket(SpigotPlugin.getAnyPacketSender(), new PlayerMessage(target));
boolean res = Protocol.P_GIVE_GAMING_SARA.sendPacket(SpigotPlugin.getAnyPacketSenderOrNull(), new PlayerMessage(target));
if (res) {
sender.sendMessage(ChatColor.GREEN + "Sent a request to give " + target.getUsername() + " the gaming sara");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String[] args) {
}
int amount = Integer.parseInt(args[0]);
Player target = PlayerUtil.getOfflinePlayer(args[1]);
boolean res = Protocol.P_GIVE_SARA.sendPacket(SpigotPlugin.getAnyPacketSender(), new ProxyboundGiveSaraMessage(amount, target));
boolean res = Protocol.P_GIVE_SARA.sendPacket(SpigotPlugin.getAnyPacketSenderOrNull(), new ProxyboundGiveSaraMessage(amount, target));
if (res) {
sender.sendMessage(ChatColor.GREEN + "Sent a request to give " + target.getUsername() + " the " + amount + "yen sara");
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.azisaba.azipluginmessaging.spigot.protocol;

import net.azisaba.azipluginmessaging.api.protocol.PacketQueue;
import net.azisaba.azipluginmessaging.api.protocol.Protocol;
import net.azisaba.azipluginmessaging.api.protocol.message.Message;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import org.jetbrains.annotations.NotNull;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class SimplePacketQueue extends PacketQueue {
private final List<Map.Entry<Protocol<?, ?>, Message>> list = new ArrayList<>();

public void add(@NotNull Protocol<?, ?> protocol, @NotNull Message message) {
Objects.requireNonNull(protocol, "protocol must not be null");
Objects.requireNonNull(message, "message must not be null");
list.add(new AbstractMap.SimpleImmutableEntry<>(protocol, message));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public void flush(@NotNull PacketSender sender) {
for (Map.Entry<Protocol<?, ? extends Message>, ? extends Message> entry : list) {
((Protocol) entry.getKey()).sendPacket(sender, entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import net.azisaba.azipluginmessaging.api.AziPluginMessaging;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.protocol.Protocol;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.api.protocol.PacketQueue;
import net.azisaba.azipluginmessaging.velocity.entity.PlayerImpl;
import net.azisaba.azipluginmessaging.velocity.server.ServerConnectionImpl;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand Down Expand Up @@ -53,6 +50,11 @@ public <T> PlayerAdapter<T> getPlayerAdapter(@NotNull Class<T> clazz) {
return (PlayerAdapter<T>) (PlayerAdapter<Player>) PlayerImpl::new;
}

@Override
public @NotNull PacketQueue getPacketQueue() {
return PacketQueue.EMPTY;
}

public static class ProxyImpl implements Proxy {
}
}

0 comments on commit ada03c7

Please sign in to comment.