From 21ddc83da8119bdeeaa2b70bb071d770cc9da004 Mon Sep 17 00:00:00 2001 From: Redned Date: Sat, 14 Sep 2019 23:23:14 -0500 Subject: [PATCH] Implement multi-block chunk updates --- .../network/translators/TranslatorsInit.java | 1 + .../java/world/JavaBlockChangeTranslator.java | 23 ++++---- .../world/JavaMultiBlockChangeTranslator.java | 56 +++++++++++++++++++ 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMultiBlockChangeTranslator.java diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java index d068a8291..9f791d272 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java @@ -176,6 +176,7 @@ public class TranslatorsInit { Registry.registerJava(ServerDisplayScoreboardPacket.class, new JavaDisplayScoreboardTranslator()); Registry.registerJava(ServerUpdateScorePacket.class, new JavaUpdateScoreTranslator()); Registry.registerJava(ServerBlockChangePacket.class, new JavaBlockChangeTranslator()); + Registry.registerJava(ServerMultiBlockChangePacket.class, new JavaMultiBlockChangeTranslator()); Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator()); Registry.registerBedrock(CommandRequestPacket.class, new BedrockCommandRequestTranslator()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java index 5037269ca..417ac9ee3 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java @@ -4,29 +4,26 @@ import com.flowpowered.math.vector.Vector3i; import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord; import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerBlockChangePacket; import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket; -import org.geysermc.api.Geyser; 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.item.BedrockItem; -import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.world.GlobalBlockPalette; public class JavaBlockChangeTranslator extends PacketTranslator { @Override public void translate(ServerBlockChangePacket packet, GeyserSession session) { - UpdateBlockPacket pack = new UpdateBlockPacket(); - + UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket(); BlockChangeRecord record = packet.getRecord(); + updateBlockPacket.setDataLayer(0); + updateBlockPacket.setBlockPosition(new Vector3i( + record.getPosition().getX(), + record.getPosition().getY(), + record.getPosition().getZ())); - BedrockItem i = new ItemTranslator().getBedrockBlock(record.getBlock()); + BedrockItem bedrockItem = TranslatorsInit.getItemTranslator().getBedrockBlock(record.getBlock()); + updateBlockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(bedrockItem.hashCode())); - pack.setDataLayer(0); - pack.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(i.hashCode())); - pack.setBlockPosition(new Vector3i( - record.getPosition().getX() - , record.getPosition().getY() - , record.getPosition().getZ())); - - session.getUpstream().sendPacket(pack); + session.getUpstream().sendPacket(updateBlockPacket); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMultiBlockChangeTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMultiBlockChangeTranslator.java new file mode 100644 index 000000000..ec8748bd4 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMultiBlockChangeTranslator.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 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.java.world; + +import com.flowpowered.math.vector.Vector3i; +import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord; +import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerMultiBlockChangePacket; +import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket; +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.item.BedrockItem; +import org.geysermc.connector.world.GlobalBlockPalette; + +public class JavaMultiBlockChangeTranslator extends PacketTranslator { + + @Override + public void translate(ServerMultiBlockChangePacket packet, GeyserSession session) { + for (BlockChangeRecord record : packet.getRecords()) { + UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket(); + updateBlockPacket.setDataLayer(0); + updateBlockPacket.setBlockPosition(new Vector3i( + record.getPosition().getX(), + record.getPosition().getY(), + record.getPosition().getZ())); + + BedrockItem bedrockItem = TranslatorsInit.getItemTranslator().getBedrockBlock(record.getBlock()); + updateBlockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(bedrockItem.hashCode())); + + session.getUpstream().sendPacket(updateBlockPacket); + } + } +}