Skip to content

Commit

Permalink
Add Translator to geyser to handle Geyser specific stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bundabrg committed Sep 9, 2020
1 parent 23d74ab commit ff9c719
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import au.com.grieve.geyser.reversion.config.Configuration;
import au.com.grieve.geyser.reversion.editions.bedrock.BedrockEdition;
import au.com.grieve.geyser.reversion.editions.education.EducationEdition;
import au.com.grieve.geyser.reversion.translators.geyser.v408.Register_Geyser_v408;
import au.com.grieve.reversion.api.RegisteredTranslator;
import au.com.grieve.reversion.api.ReversionServer;
import au.com.grieve.reversion.translators.v390ee_to_v408be.Register_v390ee_to_v408be;
Expand Down Expand Up @@ -93,6 +94,9 @@ private void registerTranslators() {
registerTranslator(Register_v411be_to_v409be.TRANSLATOR);
registerTranslator(Register_v390ee_to_v408be.TRANSLATOR);
registerTranslator(Register_v412be_to_v411be.TRANSLATOR);

// Add Geyser Translators
registerTranslator(Register_Geyser_v408.TRANSLATOR);
}


Expand Down Expand Up @@ -137,7 +141,7 @@ private void loadConfig() {
*/
@GeyserEventHandler(priority = EventHandler.PRIORITY.HIGH)
public void onGeyserStart(GeyserStartEvent event) {
Edition edition = registeredEditions.get(config.getEdition());
Edition edition = registeredEditions.get("geyser-" + config.getEdition());

if (edition == null) {
getLogger().error(String.format("Invalid Edition '%s'. Extension disabled.", config.getEdition()));
Expand All @@ -151,10 +155,6 @@ public void onGeyserStart(GeyserStartEvent event) {
bedrockServer.setAccessible(true);

ReversionServer server = edition.createReversionServer(GeyserConnector.getInstance().getBedrockServer().getBindAddress());
for (RegisteredTranslator translator : getRegisteredTranslators()) {
server.registerTranslator(translator);
getLogger().debug("Registered Translator: " + translator.getName());
}

GeyserConnector.getInstance().getBedrockServer().close();
bedrockServer.set(GeyserConnector.getInstance(), server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@

package au.com.grieve.geyser.reversion.editions.bedrock;

import au.com.grieve.geyser.reversion.GeyserReversionExtension;
import au.com.grieve.geyser.reversion.api.Edition;
import au.com.grieve.geyser.reversion.editions.bedrock.handlers.BedrockServerEventHandler;
import au.com.grieve.reversion.api.RegisteredTranslator;
import au.com.grieve.reversion.api.ReversionServer;
import au.com.grieve.reversion.editions.bedrock.BedrockRegisteredTranslator;
import au.com.grieve.reversion.editions.bedrock.BedrockReversionServer;
import lombok.RequiredArgsConstructor;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.extension.GeyserExtension;
import org.geysermc.connector.network.BedrockProtocol;

import java.net.InetSocketAddress;

@RequiredArgsConstructor
public class BedrockEdition implements Edition {
private final GeyserExtension extension;
private final GeyserReversionExtension extension;

@Override
public ReversionServer createReversionServer(InetSocketAddress address) {
extension.getLogger().info("BedrockServer listening on " + address.toString());
ReversionServer server = new BedrockReversionServer("bedrock", BedrockProtocol.DEFAULT_BEDROCK_CODEC, address);
BedrockReversionServer server = new BedrockReversionServer("geyser-bedrock", BedrockProtocol.DEFAULT_BEDROCK_CODEC, address);
server.setHandler(new BedrockServerEventHandler(GeyserConnector.getInstance()));

for (RegisteredTranslator translator : extension.getRegisteredTranslators()) {
if (translator instanceof BedrockRegisteredTranslator) {
server.registerTranslator((BedrockRegisteredTranslator) translator);
}
extension.getLogger().debug("Registered Translator: " + translator.getName());
}

return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package au.com.grieve.geyser.reversion.editions.bedrock.handlers;

import au.com.grieve.reversion.api.ReversionSession;
import au.com.grieve.reversion.editions.bedrock.BedrockReversionSession;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import lombok.Getter;
import org.geysermc.connector.GeyserConnector;
Expand All @@ -39,8 +38,5 @@ public BedrockServerEventHandler(GeyserConnector connector) {
public void onSessionCreation(BedrockServerSession bedrockServerSession) {
bedrockServerSession.setLogging(true);
bedrockServerSession.setPacketHandler(new BedrockUpstreamPacketHandler((ReversionSession) bedrockServerSession, this.connector, new GeyserSession(this.connector, bedrockServerSession)));

// Add a Geyser specific handler
((BedrockReversionSession) bedrockServerSession).getFromServerHandlers().add(new FromGeyserPacketHandler());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@

package au.com.grieve.geyser.reversion.editions.education;

import au.com.grieve.geyser.reversion.GeyserReversionExtension;
import au.com.grieve.geyser.reversion.api.Edition;
import au.com.grieve.geyser.reversion.editions.bedrock.handlers.BedrockServerEventHandler;
import au.com.grieve.geyser.reversion.editions.education.commands.EducationCommand;
import au.com.grieve.reversion.api.RegisteredTranslator;
import au.com.grieve.reversion.api.ReversionServer;
import au.com.grieve.reversion.editions.bedrock.BedrockRegisteredTranslator;
import au.com.grieve.reversion.editions.education.EducationReversionServer;
import au.com.grieve.reversion.editions.education.utils.TokenManager;
import lombok.Getter;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.event.annotations.GeyserEventHandler;
import org.geysermc.connector.event.events.geyser.GeyserStartEvent;
import org.geysermc.connector.extension.GeyserExtension;
import org.geysermc.connector.network.BedrockProtocol;

import java.io.File;
import java.net.InetSocketAddress;

@Getter
public class EducationEdition implements Edition {
private final GeyserExtension extension;
private final GeyserReversionExtension extension;
private final TokenManager tokenManager;

public EducationEdition(GeyserExtension extension) {
public EducationEdition(GeyserReversionExtension extension) {
this.extension = extension;

this.tokenManager = new TokenManager(new File(extension.getDataFolder(), "tokens.yml"));
Expand All @@ -58,8 +60,16 @@ public void onGeyserStart(GeyserStartEvent event) {
@Override
public ReversionServer createReversionServer(InetSocketAddress address) {
extension.getLogger().info("EducationServer listening on " + address.toString());
ReversionServer server = new EducationReversionServer("bedrock", BedrockProtocol.DEFAULT_BEDROCK_CODEC, tokenManager, address);
EducationReversionServer server = new EducationReversionServer("geyser-bedrock", BedrockProtocol.DEFAULT_BEDROCK_CODEC, tokenManager, address);
server.setHandler(new BedrockServerEventHandler(GeyserConnector.getInstance()));

for (RegisteredTranslator translator : extension.getRegisteredTranslators()) {
if (translator instanceof BedrockRegisteredTranslator) {
server.registerTranslator((BedrockRegisteredTranslator) translator);
}
extension.getLogger().debug("Registered Translator: " + translator.getName());
}

return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* EduSupport - Minecraft Protocol Support for MultiVersion in Geyser
* Copyright (C) 2020 GeyserReversion Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package au.com.grieve.geyser.reversion.translators.geyser.v408;

import au.com.grieve.geyser.reversion.translators.geyser.v408.handlers.StartGameHandler_Geyser_v408;
import au.com.grieve.reversion.editions.bedrock.BedrockRegisteredTranslator;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import com.nukkitx.protocol.bedrock.v408.Bedrock_v408;

public class Register_Geyser_v408 {
public static BedrockRegisteredTranslator TRANSLATOR = BedrockRegisteredTranslator.builder()
.fromEdition("bedrock")
.fromProtocolVersion(408)
.toEdition("geyser-bedrock")
.toProtocolVersion(408)
.codec(Bedrock_v408.V408_CODEC)
.translator(Translator_Geyser_v408.class)
.registerPacketHandler(StartGamePacket.class, StartGameHandler_Geyser_v408.class)
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* EduSupport - Minecraft Protocol Support for MultiVersion in Geyser
* Copyright (C) 2020 GeyserReversion Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package au.com.grieve.geyser.reversion.translators.geyser.v408;

import au.com.grieve.reversion.api.ReversionSession;
import au.com.grieve.reversion.editions.bedrock.BedrockRegisteredTranslator;
import au.com.grieve.reversion.editions.bedrock.BedrockTranslator;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@Getter
public class Translator_Geyser_v408 extends BedrockTranslator {
@Getter
private static final Map<Integer, Integer> blockRuntimeIdMap = new HashMap<>();

public Translator_Geyser_v408(BedrockRegisteredTranslator registeredTranslator, ReversionSession reversionSession) {
super(registeredTranslator, reversionSession);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,36 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package au.com.grieve.geyser.reversion.editions.bedrock.handlers;
package au.com.grieve.geyser.reversion.translators.geyser.v408.handlers;

import au.com.grieve.geyser.reversion.GeyserReversionExtension;
import au.com.grieve.geyser.reversion.translators.geyser.v408.Translator_Geyser_v408;
import au.com.grieve.reversion.api.PacketHandler;
import com.nukkitx.nbt.NBTInputStream;
import com.nukkitx.nbt.NbtList;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtType;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import org.geysermc.connector.utils.FileUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class FromGeyserPacketHandler implements BedrockPacketHandler {
/*
Geyser re-arranged the block palette which will cause issues with the new baked in palette format.
What we do here is to re-arrange it back based upon the supported bedrock palette in Geyser and keep a record
of the mapping so we translate it in other packets
*/
public class StartGameHandler_Geyser_v408 extends PacketHandler<Translator_Geyser_v408, StartGamePacket> {

public static Map<Integer, Integer> blockRuntimeIdMap;
public StartGameHandler_Geyser_v408(Translator_Geyser_v408 translator) {
super(translator);
}

/*
Geyser re-arranged the block palette which will cause issues with the new baked in palette format.
What we do here is to re-arrange it back based upon the supported bedrock palette in Geyser and keep a record
of the mapping so we translate it in other packets
*/
@Override
public boolean handle(StartGamePacket packet) {
// Only do this once
if (blockRuntimeIdMap == null) {
blockRuntimeIdMap = new HashMap<>();
public boolean fromServer(StartGamePacket packet) {
// Only grab runtime ID's for the first player
if (Translator_Geyser_v408.getBlockRuntimeIdMap().size() == 0) {

/* Load block palette */
InputStream stream = FileUtils.getResource("bedrock/runtime_block_states.dat");
Expand All @@ -60,18 +62,30 @@ public boolean handle(StartGamePacket packet) {

for (int geyserId = 0; geyserId < geyserTags.size(); geyserId++) {
NbtMap geyserTag = geyserTags.get(geyserId);
boolean found = false;
for (int originalId = 0; originalId < originalTags.size(); originalId++) {
NbtMap originalTag = originalTags.get(originalId);
if (geyserTag.equals(originalTag)) {
blockRuntimeIdMap.put(geyserId, originalId);
Translator_Geyser_v408.getBlockRuntimeIdMap().put(geyserId, originalId);
found = true;
break;
}
}
if (!found) {
GeyserReversionExtension.getInstance().getLogger().error("Unable to find palette entry: " + geyserTag);
Translator_Geyser_v408.getBlockRuntimeIdMap().put(geyserId, 0); // Set to 0 for now
}
}
}


packet.getBlockPalette();

return false;
// Reorder the BlockPalette
NbtList<NbtMap> originalTags = packet.getBlockPalette();
NbtList<NbtMap> translatedTags = new NbtList<>(NbtType.COMPOUND);
for (int id : Translator_Geyser_v408.getBlockRuntimeIdMap().keySet()) {
translatedTags.add(originalTags.get(Translator_Geyser_v408.getBlockRuntimeIdMap().get(id)));
}
packet.setBlockPalette(translatedTags);

return false;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<reversion.version>1.0.9</reversion.version>
<reversion.version>1.1.0-SNAPSHOT</reversion.version>
</properties>

<build>
Expand Down

0 comments on commit ff9c719

Please sign in to comment.