From 13a58dc8252d2124c71be8eb87a3b0134b540705 Mon Sep 17 00:00:00 2001 From: Ender <33337309+e3ndr@users.noreply.github.com> Date: Mon, 23 Mar 2020 23:03:33 -0500 Subject: [PATCH] Begin making translators annotation based Also, rename TranslatorsInit to Translators; makes alot more sense. --- connector/pom.xml | 5 ++ .../geysermc/connector/GeyserConnector.java | 4 +- .../geysermc/connector/entity/ItemEntity.java | 4 +- .../network/session/cache/ChunkCache.java | 4 +- .../network/translators/Registry.java | 9 +- .../network/translators/Translator.java | 34 ++++++++ ...{TranslatorsInit.java => Translators.java} | 85 ++++++++++--------- .../inventory/GenericInventoryTranslator.java | 6 +- .../entity/JavaEntityEquipmentTranslator.java | 4 +- .../inventory/OpenWindowPacketTranslator.java | 4 +- .../geysermc/connector/utils/ChunkUtils.java | 4 +- .../connector/utils/InventoryUtils.java | 14 +-- 12 files changed, 112 insertions(+), 65 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/Translator.java rename connector/src/main/java/org/geysermc/connector/network/translators/{TranslatorsInit.java => Translators.java} (79%) diff --git a/connector/pom.xml b/connector/pom.xml index a523d2633..25ef50730 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -116,5 +116,10 @@ + + org.reflections + reflections + 0.9.12 + diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 401e3eb93..fb93a27de 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -40,7 +40,7 @@ import org.geysermc.connector.metrics.Metrics; import org.geysermc.connector.network.ConnectorServerEventHandler; import org.geysermc.connector.network.remote.RemoteServer; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.thread.PingPassthroughThread; import org.geysermc.connector.utils.Toolbox; import org.geysermc.common.IGeyserConfiguration; @@ -104,7 +104,7 @@ public class GeyserConnector { logger.setDebug(config.isDebugMode()); Toolbox.init(); - TranslatorsInit.start(); + Translators.start(); commandMap = new GeyserCommandMap(this); remoteServer = new RemoteServer(config.getRemote().getAddress(), config.getRemote().getPort()); diff --git a/connector/src/main/java/org/geysermc/connector/entity/ItemEntity.java b/connector/src/main/java/org/geysermc/connector/entity/ItemEntity.java index 4c227cadd..a80323076 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/ItemEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/ItemEntity.java @@ -31,7 +31,7 @@ import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.packet.AddItemEntityPacket; import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; public class ItemEntity extends Entity { @@ -49,7 +49,7 @@ public class ItemEntity extends Entity { itemPacket.setUniqueEntityId(geyserId); itemPacket.setFromFishing(false); itemPacket.getMetadata().putAll(metadata); - itemPacket.setItemInHand(TranslatorsInit.getItemTranslator().translateToBedrock((ItemStack) entityMetadata.getValue())); + itemPacket.setItemInHand(Translators.getItemTranslator().translateToBedrock((ItemStack) entityMetadata.getValue())); session.getUpstream().sendPacket(itemPacket); } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/ChunkCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/ChunkCache.java index 5e94f3d9c..bc88694d8 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/ChunkCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/ChunkCache.java @@ -32,7 +32,7 @@ import com.github.steveice10.mc.protocol.data.game.world.block.BlockState; import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket; import lombok.Getter; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.block.BlockTranslator; import org.geysermc.connector.world.chunk.ChunkPosition; @@ -102,7 +102,7 @@ public class ChunkCache { levelChunkPacket.setChunkZ(position.getZ()); levelChunkPacket.setCachingEnabled(false); levelChunkPacket.setSubChunksLength(0); - levelChunkPacket.setData(TranslatorsInit.EMPTY_LEVEL_CHUNK_DATA); + levelChunkPacket.setData(Translators.EMPTY_LEVEL_CHUNK_DATA); session.getUpstream().sendPacket(levelChunkPacket); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/Registry.java b/connector/src/main/java/org/geysermc/connector/network/translators/Registry.java index f32fe4b6d..ecdd35812 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/Registry.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/Registry.java @@ -39,14 +39,15 @@ public class Registry { public static final Registry JAVA = new Registry<>(); public static final Registry BEDROCK = new Registry<>(); - public static void registerJava(Class clazz, PacketTranslator translator) { - JAVA.MAP.put(clazz, translator); + public static void registerJava(Class targetPacket, PacketTranslator translator) { + JAVA.MAP.put(targetPacket, translator); } - public static void registerBedrock(Class clazz, PacketTranslator translator) { - BEDROCK.MAP.put(clazz, translator); + public static void registerBedrock(Class targetPacket, PacketTranslator translator) { + BEDROCK.MAP.put(targetPacket, translator); } + @SuppressWarnings("unchecked") public

boolean translate(Class clazz, P packet, GeyserSession session) { if (!session.getUpstream().isClosed() && !session.isClosed()) { try { diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/Translator.java b/connector/src/main/java/org/geysermc/connector/network/translators/Translator.java new file mode 100644 index 000000000..8e097ba4d --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/Translator.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ +package org.geysermc.connector.network.translators; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(value = RetentionPolicy.RUNTIME) +public @interface Translator { + Class packet(); + +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java b/connector/src/main/java/org/geysermc/connector/network/translators/Translators.java similarity index 79% rename from connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java rename to connector/src/main/java/org/geysermc/connector/network/translators/Translators.java index a1ca1faf0..c87cab3ce 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/Translators.java @@ -25,48 +25,26 @@ package org.geysermc.connector.network.translators; -import com.github.steveice10.mc.protocol.packet.ingame.server.*; -import com.github.steveice10.mc.protocol.packet.ingame.server.entity.*; -import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.*; -import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.*; -import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerDisplayScoreboardPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerScoreboardObjectivePacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerTeamPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerUpdateScorePacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.world.*; -import com.github.steveice10.mc.protocol.packet.login.server.LoginPluginRequestPacket; -import com.nukkitx.nbt.CompoundTagBuilder; -import com.nukkitx.nbt.NbtUtils; -import com.nukkitx.nbt.stream.NBTOutputStream; -import com.nukkitx.nbt.tag.CompoundTag; -import com.nukkitx.protocol.bedrock.packet.*; -import lombok.Getter; -import org.geysermc.connector.network.translators.bedrock.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.translators.block.BlockTranslator; import org.geysermc.connector.network.translators.inventory.GenericInventoryTranslator; import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.network.translators.item.ItemTranslator; -import org.geysermc.connector.network.translators.java.*; -import org.geysermc.connector.network.translators.java.entity.*; -import org.geysermc.connector.network.translators.java.entity.player.*; -import org.geysermc.connector.network.translators.java.entity.spawn.*; -import org.geysermc.connector.network.translators.java.inventory.OpenWindowPacketTranslator; -import org.geysermc.connector.network.translators.java.scoreboard.JavaDisplayScoreboardTranslator; -import org.geysermc.connector.network.translators.java.scoreboard.JavaScoreboardObjectiveTranslator; -import org.geysermc.connector.network.translators.java.scoreboard.JavaTeamTranslator; -import org.geysermc.connector.network.translators.java.scoreboard.JavaUpdateScoreTranslator; -import org.geysermc.connector.network.translators.java.window.JavaOpenWindowTranslator; -import org.geysermc.connector.network.translators.java.window.JavaSetSlotTranslator; -import org.geysermc.connector.network.translators.java.window.JavaWindowItemsTranslator; -import org.geysermc.connector.network.translators.java.world.*; +import org.reflections.Reflections; -import java.io.ByteArrayOutputStream; -import java.io.IOException; +import com.github.steveice10.packetlib.packet.Packet; +import com.nukkitx.nbt.CompoundTagBuilder; +import com.nukkitx.nbt.NbtUtils; +import com.nukkitx.nbt.stream.NBTOutputStream; +import com.nukkitx.nbt.tag.CompoundTag; +import com.nukkitx.protocol.bedrock.BedrockPacket; -public class TranslatorsInit { +import lombok.Getter; + +public class Translators { @Getter private static ItemTranslator itemTranslator; @@ -91,8 +69,9 @@ public class TranslatorsInit { } } + @SuppressWarnings("unchecked") public static void start() { - Registry.registerJava(LoginPluginRequestPacket.class, new JavaLoginPluginMessageTranslator()); + /*Registry.registerJava(LoginPluginRequestPacket.class, new JavaLoginPluginMessageTranslator()); Registry.registerJava(ServerJoinGamePacket.class, new JavaJoinGameTranslator()); Registry.registerJava(ServerPluginMessagePacket.class, new JavaPluginMessageTranslator()); @@ -162,8 +141,36 @@ public class TranslatorsInit { Registry.registerBedrock(InteractPacket.class, new BedrockInteractTranslator()); Registry.registerBedrock(TextPacket.class, new BedrockTextTranslator()); Registry.registerBedrock(RespawnPacket.class, new BedrockRespawnTranslator()); - Registry.registerBedrock(ShowCreditsPacket.class, new BedrockShowCreditsTranslator()); - + Registry.registerBedrock(ShowCreditsPacket.class, new BedrockShowCreditsTranslator());*/ + + Reflections ref = new Reflections("org.geysermc.connector.network.translators"); + + for (Class clazz : ref.getTypesAnnotatedWith(Translator.class)) { + Class packet = clazz.getAnnotation(Translator.class).packet(); + + GeyserConnector.getInstance().getLogger().debug("Found annotated translator: " + clazz.getCanonicalName() + " : " + packet.getSimpleName()); + + try { + if (Packet.class.isAssignableFrom(packet)) { + Class targetPacket = (Class) packet; + PacketTranslator translator = (PacketTranslator) clazz.newInstance(); + + Registry.registerJava(targetPacket, translator); + + } else if (BedrockPacket.class.isAssignableFrom(packet)) { + Class targetPacket = (Class) packet; + PacketTranslator translator = (PacketTranslator) clazz.newInstance(); + + Registry.registerBedrock(targetPacket, translator); + + } else { + GeyserConnector.getInstance().getLogger().error("Class " + clazz.getCanonicalName() + " is annotated as a translator but has an invalid target packet."); + } + } catch (InstantiationException | IllegalAccessException e) { + GeyserConnector.getInstance().getLogger().error("Could not instantiate annotated translator " + clazz.getCanonicalName() + "."); + } + } + itemTranslator = new ItemTranslator(); BlockTranslator.init(); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/GenericInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/GenericInventoryTranslator.java index 754d05c67..f7be13c39 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/GenericInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/GenericInventoryTranslator.java @@ -32,7 +32,7 @@ import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket; import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; public class GenericInventoryTranslator extends InventoryTranslator { @@ -54,7 +54,7 @@ public class GenericInventoryTranslator extends InventoryTranslator { public void updateInventory(GeyserSession session, Inventory inventory) { ItemData[] bedrockItems = new ItemData[inventory.getItems().length]; for (int i = 0; i < bedrockItems.length; i++) { - bedrockItems[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]); + bedrockItems[i] = Translators.getItemTranslator().translateToBedrock(inventory.getItems()[i]); } InventoryContentPacket contentPacket = new InventoryContentPacket(); @@ -67,7 +67,7 @@ public class GenericInventoryTranslator extends InventoryTranslator { public void updateSlot(GeyserSession session, Inventory inventory, int slot) { InventorySlotPacket slotPacket = new InventorySlotPacket(); slotPacket.setContainerId(inventory.getId()); - slotPacket.setItem(TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[slot])); + slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(inventory.getItems()[slot])); slotPacket.setSlot(slot); session.getUpstream().sendPacket(slotPacket); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityEquipmentTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityEquipmentTranslator.java index 5efe8e4e3..c5f5ad3f6 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityEquipmentTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityEquipmentTranslator.java @@ -31,7 +31,7 @@ import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.LivingEntity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; public class JavaEntityEquipmentTranslator extends PacketTranslator { @@ -52,7 +52,7 @@ public class JavaEntityEquipmentTranslator extends PacketTranslator { @Override public void translate(ServerOpenWindowPacket packet, GeyserSession session) { System.out.println("debug: " + packet.getType()); - InventoryTranslator translator = TranslatorsInit.getInventoryTranslator(); + InventoryTranslator translator = Translators.getInventoryTranslator(); translator.openInventory(session, new Inventory(packet.getName(), packet.getWindowId(), packet.getType(), 54)); diff --git a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java index 1a6296c02..2acda2c2b 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java @@ -33,7 +33,7 @@ import com.nukkitx.math.vector.Vector3i; import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket; import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.block.BlockTranslator; import org.geysermc.connector.world.chunk.ChunkSection; @@ -109,7 +109,7 @@ public class ChunkUtils { data.setChunkX(chunkX + x); data.setChunkZ(chunkZ + z); data.setSubChunksLength(0); - data.setData(TranslatorsInit.EMPTY_LEVEL_CHUNK_DATA); + data.setData(Translators.EMPTY_LEVEL_CHUNK_DATA); data.setCachingEnabled(false); session.getUpstream().sendPacket(data); diff --git a/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java b/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java index 56b59584d..045b81d16 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java @@ -37,7 +37,7 @@ import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.TranslatorsInit; +import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import java.util.List; @@ -52,17 +52,17 @@ public class InventoryUtils { ItemData[] contents = new ItemData[40]; // Inventory for (int i = 9; i < 36; i++) { - contents[i] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]); + contents[i] = Translators.getItemTranslator().translateToBedrock(inventory.getItems()[i]); } // Hotbar for (int i = 36; i < 45; i++) { - contents[i - 36] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]); + contents[i - 36] = Translators.getItemTranslator().translateToBedrock(inventory.getItems()[i]); } // Armor for (int i = 5; i < 9; i++) { - contents[i + 31] = TranslatorsInit.getItemTranslator().translateToBedrock(inventory.getItems()[i]); + contents[i + 31] = Translators.getItemTranslator().translateToBedrock(inventory.getItems()[i]); } inventoryContentPacket.setContents(contents); @@ -74,7 +74,7 @@ public class InventoryUtils { session.getInventoryCache().getInventories().put(packet.getWindowId(), inventory); session.getInventoryCache().setOpenInventory(inventory); - InventoryTranslator translator = TranslatorsInit.getInventoryTranslator(); + InventoryTranslator translator = Translators.getInventoryTranslator(); translator.prepareInventory(session, inventory); GeyserConnector.getInstance().getGeneralThreadPool().schedule(() -> { List packets = session.getInventoryCache().getCachedPackets().get(inventory.getId()); @@ -99,7 +99,7 @@ public class InventoryUtils { if (packet.getWindowId() != openInventory.getId()) return; - InventoryTranslator translator = TranslatorsInit.getInventoryTranslator(); + InventoryTranslator translator = Translators.getInventoryTranslator(); if (translator == null) { session.getDownstream().getSession().send(new ClientCloseWindowPacket(packet.getWindowId())); return; @@ -120,7 +120,7 @@ public class InventoryUtils { if (packet.getWindowId() != openInventory.getId()) return; - InventoryTranslator translator = TranslatorsInit.getInventoryTranslator(); + InventoryTranslator translator = Translators.getInventoryTranslator(); if (translator == null) { session.getDownstream().getSession().send(new ClientCloseWindowPacket(packet.getWindowId())); return;