boolean translate(Class extends P> 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 extends Packet> targetPacket = (Class extends Packet>) packet;
+ PacketTranslator extends Packet> translator = (PacketTranslator extends Packet>) clazz.newInstance();
+
+ Registry.registerJava(targetPacket, translator);
+
+ } else if (BedrockPacket.class.isAssignableFrom(packet)) {
+ Class extends BedrockPacket> targetPacket = (Class extends BedrockPacket>) packet;
+ PacketTranslator extends BedrockPacket> translator = (PacketTranslator extends BedrockPacket>) 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