Begin making translators annotation based

Also, rename TranslatorsInit to Translators; makes alot more sense.
This commit is contained in:
Ender 2020-03-23 23:03:33 -05:00
parent 8ce0916827
commit 13a58dc825
12 changed files with 112 additions and 65 deletions

View file

@ -116,5 +116,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
</dependencies>
</project>

View file

@ -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());

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -39,14 +39,15 @@ public class Registry<T> {
public static final Registry<Packet> JAVA = new Registry<>();
public static final Registry<BedrockPacket> BEDROCK = new Registry<>();
public static <T extends Packet> void registerJava(Class<T> clazz, PacketTranslator<T> translator) {
JAVA.MAP.put(clazz, translator);
public static void registerJava(Class<? extends Packet> targetPacket, PacketTranslator<? extends Packet> translator) {
JAVA.MAP.put(targetPacket, translator);
}
public static <T extends BedrockPacket> void registerBedrock(Class<T> clazz, PacketTranslator<T> translator) {
BEDROCK.MAP.put(clazz, translator);
public static void registerBedrock(Class<? extends BedrockPacket> targetPacket, PacketTranslator<? extends BedrockPacket> translator) {
BEDROCK.MAP.put(targetPacket, translator);
}
@SuppressWarnings("unchecked")
public <P extends T> boolean translate(Class<? extends P> clazz, P packet, GeyserSession session) {
if (!session.getUpstream().isClosed() && !session.isClosed()) {
try {

View file

@ -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();
}

View file

@ -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();

View file

@ -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);
}

View file

@ -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<ServerEntityEquipmentPacket> {
@ -52,7 +52,7 @@ public class JavaEntityEquipmentTranslator extends PacketTranslator<ServerEntity
}
LivingEntity livingEntity = (LivingEntity) entity;
ItemData item = TranslatorsInit.getItemTranslator().translateToBedrock(packet.getItem());
ItemData item = Translators.getItemTranslator().translateToBedrock(packet.getItem());
switch (packet.getSlot()) {
case HELMET:
livingEntity.setHelmet(item);

View file

@ -7,14 +7,14 @@ import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import org.geysermc.connector.inventory.Inventory;
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;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
public class OpenWindowPacketTranslator extends PacketTranslator<ServerOpenWindowPacket> {
@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));

View file

@ -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);

View file

@ -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<Packet> 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;