From 3284a7750898e3849b45718cfdc52f65b61b8172 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 7 Jan 2020 07:10:34 +0100 Subject: [PATCH] Removing usage of Pair class --- .../bungeecore/listeners/mods/Forge.java | 7 ++- .../bungeecore/listeners/mods/LabyMod.java | 43 ++++++++++--------- .../bungeecore/listeners/mods/Utils.java | 15 +++++-- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/de/steamwar/bungeecore/listeners/mods/Forge.java b/src/de/steamwar/bungeecore/listeners/mods/Forge.java index c4f54f82..cda65284 100644 --- a/src/de/steamwar/bungeecore/listeners/mods/Forge.java +++ b/src/de/steamwar/bungeecore/listeners/mods/Forge.java @@ -5,7 +5,6 @@ import de.steamwar.bungeecore.listeners.BasicListener; import de.steamwar.bungeecore.sql.Mod; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import javafx.util.Pair; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.Connection; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -74,11 +73,11 @@ public class Forge extends BasicListener { ProxiedPlayer p = (ProxiedPlayer) sender; if (data[0] == 2) { - Pair numMods = Utils.readVarInt(data, 1); + Utils.VarInt numMods = Utils.readVarInt(data, 1); List mods = new LinkedList<>(); - int bytePos = 1 + numMods.getKey(); - for (int i = 0; i < numMods.getValue(); i++) { + int bytePos = 1 + numMods.length; + for (int i = 0; i < numMods.value; i++) { byte[] name = Arrays.copyOfRange(data, bytePos + 1, bytePos + data[bytePos] + 1); bytePos += 1 + data[bytePos]; //Version information is unused diff --git a/src/de/steamwar/bungeecore/listeners/mods/LabyMod.java b/src/de/steamwar/bungeecore/listeners/mods/LabyMod.java index d43cc6dd..ed9a82b2 100644 --- a/src/de/steamwar/bungeecore/listeners/mods/LabyMod.java +++ b/src/de/steamwar/bungeecore/listeners/mods/LabyMod.java @@ -5,23 +5,22 @@ import com.google.gson.stream.JsonReader; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.listeners.BasicListener; import de.steamwar.bungeecore.sql.Mod; -import javafx.util.Pair; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.Connection; import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.event.EventHandler; import java.io.IOException; import java.io.StringReader; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; public class LabyMod extends BasicListener { - private static Set labyModUsers = new HashSet<>(); - @EventHandler public void onPluginMessageEvent(PluginMessageEvent event){ if(!event.getTag().equals("LMC")) @@ -32,7 +31,6 @@ public class LabyMod extends BasicListener { return; event.setCancelled(true); - labyModUsers.add((ProxiedPlayer) sender); updateGameInfo((ProxiedPlayer) sender); byte[] data = event.getData(); @@ -40,15 +38,15 @@ public class LabyMod extends BasicListener { } private void runAsync(byte[] data, ProxiedPlayer player) { - Pair purpose = readString(data, 0); - if(!"INFO".equals(purpose.getValue())) + VarString purpose = readString(data, 0); + if(!"INFO".equals(purpose.value)) return; - Pair value = readString(data, purpose.getKey()); + VarString value = readString(data, purpose.length); List mods = new LinkedList<>(); try{ - InfoPacket info = new InfoPacket(value.getValue()); + InfoPacket info = new InfoPacket(value.value); for(InfoPacket.Addon addon : info.addons) { mods.add(Mod.get(addon.name, Mod.Platform.LABYMOD)); } @@ -59,10 +57,10 @@ public class LabyMod extends BasicListener { Utils.handleMods(player, mods); } - private Pair readString(byte[] array, int startPos){ - Pair varInt = Utils.readVarInt(array, startPos); - startPos += varInt.getKey(); - return new Pair<>(varInt.getValue()+varInt.getKey(), new String(Arrays.copyOfRange(array, startPos, startPos + varInt.getValue()), StandardCharsets.UTF_8)); + private VarString readString(byte[] array, int startPos){ + Utils.VarInt varInt = Utils.readVarInt(array, startPos); + startPos += varInt.length; + return new VarString(varInt.value+varInt.length, new String(Arrays.copyOfRange(array, startPos, startPos + varInt.value), StandardCharsets.UTF_8)); } private void updateGameInfo(ProxiedPlayer proxiedPlayer) { @@ -76,6 +74,16 @@ public class LabyMod extends BasicListener { proxiedPlayer.sendData("LMC", output.getBytes()); } + private static class VarString{ + private final int length; + private final String value; + + private VarString(int length, String value) { + this.length = length; + this.value = value; + } + } + private static class InfoPacket{ String version = null; Feature ccp = null; @@ -154,11 +162,4 @@ public class LabyMod extends BasicListener { } } } - - @EventHandler - public void onDisconnect(PlayerDisconnectEvent event){ - labyModUsers.remove(event.getPlayer()); - } - - //TODO: PlayerHandshakeEvent } diff --git a/src/de/steamwar/bungeecore/listeners/mods/Utils.java b/src/de/steamwar/bungeecore/listeners/mods/Utils.java index 548f4118..c770868c 100644 --- a/src/de/steamwar/bungeecore/listeners/mods/Utils.java +++ b/src/de/steamwar/bungeecore/listeners/mods/Utils.java @@ -4,7 +4,6 @@ import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.sql.Mod; import de.steamwar.bungeecore.sql.Mod.ModType; import de.steamwar.bungeecore.sql.SteamwarUser; -import javafx.util.Pair; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.sql.Timestamp; @@ -16,7 +15,7 @@ import java.util.List; class Utils { private Utils(){} - static Pair readVarInt(byte[] array, int startPos) { + static VarInt readVarInt(byte[] array, int startPos) { int numRead = 0; int result = 0; byte read; @@ -31,7 +30,7 @@ class Utils { } } while ((read & 0b10000000) != 0); - return new Pair<>(numRead, result); + return new VarInt(numRead, result); } static boolean handleMods(ProxiedPlayer player, List mods){ @@ -68,4 +67,14 @@ class Utils { return false; } + + static class VarInt{ + final int length; + final int value; + + VarInt(int length, int value){ + this.length = length; + this.value = value; + } + } }