From 6f7ad4b569f0f8259c94e6d5f971310e4c337328 Mon Sep 17 00:00:00 2001 From: ForceUpdate1 Date: Thu, 23 Apr 2020 08:26:54 +0200 Subject: [PATCH 01/14] Implement Book pages (#408) * add text component library implement books * add licence header --- connector/pom.xml | 18 ++++ .../translators/nbt/BookPagesTranslator.java | 84 +++++++++++++++++++ .../connector/utils/MessageUtils.java | 27 +++++- 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/BookPagesTranslator.java diff --git a/connector/pom.xml b/connector/pom.xml index 9e223d4ea..4a488271d 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -139,5 +139,23 @@ reflections 0.9.12 + + net.kyori + text-api + 3.0.3 + compile + + + net.kyori + text-serializer-gson + 3.0.3 + compile + + + net.kyori + text-serializer-legacy + 3.0.3 + compile + diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/BookPagesTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/BookPagesTranslator.java new file mode 100644 index 000000000..f29f16fea --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/BookPagesTranslator.java @@ -0,0 +1,84 @@ +/* + * 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.item.translators.nbt; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; +import org.geysermc.connector.network.translators.ItemRemapper; +import org.geysermc.connector.network.translators.NbtItemStackTranslator; +import org.geysermc.connector.network.translators.item.ItemEntry; +import org.geysermc.connector.utils.MessageUtils; + +import java.util.ArrayList; +import java.util.List; + +@ItemRemapper +public class BookPagesTranslator extends NbtItemStackTranslator { + + @Override + public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { + if (itemTag.contains("pages")) { + List pages = new ArrayList<>(); + ListTag pagesTag = itemTag.get("pages"); + for (Tag tag : pagesTag.getValue()) { + if (!(tag instanceof StringTag)) + continue; + + StringTag textTag = (StringTag) tag; + + CompoundTag pageTag = new CompoundTag(""); + pageTag.put(new StringTag("photoname", "")); + pageTag.put(new StringTag("text", MessageUtils.getBedrockMessage(textTag.getValue()))); + pages.add(pageTag); + } + + itemTag.remove("pages"); + itemTag.put(new ListTag("pages", pages)); + } + } + + @Override + public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { + if (itemTag.contains("pages")) { + List pages = new ArrayList<>(); + ListTag pagesTag = itemTag.get("pages"); + for (Tag tag : pagesTag.getValue()) { + if (!(tag instanceof CompoundTag)) + continue; + + CompoundTag pageTag = (CompoundTag) tag; + + StringTag textTag = pageTag.get("text"); + pages.add(new StringTag(MessageUtils.getJavaMessage(textTag.getValue()))); + } + + itemTag.remove("pages"); + itemTag.put(new ListTag("pages", pages)); + } + } +} diff --git a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java index c00d4bd5b..ac111c71e 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -32,6 +32,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; +import net.kyori.text.Component; +import net.kyori.text.serializer.gson.GsonComponentSerializer; +import net.kyori.text.serializer.legacy.LegacyComponentSerializer; import org.geysermc.connector.network.session.GeyserSession; import java.util.*; @@ -62,7 +65,7 @@ public class MessageUtils { List furtherParams = getTranslationParams(translation.getTranslationParams(), locale); if (locale != null) { strings.add(insertParams(LocaleUtils.getLocaleString(translation.getTranslationKey(), locale), furtherParams)); - }else{ + } else { strings.addAll(furtherParams); } } else { @@ -118,14 +121,30 @@ public class MessageUtils { } public static String getBedrockMessage(Message message) { - return getTranslatedBedrockMessage(message, null, false); + Component component; + if (isMessage(message.getText())) { + component = GsonComponentSerializer.INSTANCE.deserialize(message.getText()); + } else { + component = GsonComponentSerializer.INSTANCE.deserialize(message.toJsonString()); + } + return LegacyComponentSerializer.legacy().serialize(component); + } + + public static String getBedrockMessage(String message) { + Component component = GsonComponentSerializer.INSTANCE.deserialize(message); + return LegacyComponentSerializer.legacy().serialize(component); + } + + public static String getJavaMessage(String message) { + Component component = LegacyComponentSerializer.legacy().deserialize(message); + return GsonComponentSerializer.INSTANCE.serialize(component); } /** * Inserts the given parameters into the given message both in sequence and as requested * * @param message Message containing possible parameter replacement strings - * @param params A list of parameter strings + * @param params A list of parameter strings * @return Parsed message with all params inserted as needed */ public static String insertParams(String message, List params) { @@ -135,7 +154,7 @@ public class MessageUtils { Matcher m = p.matcher(message); while (m.find()) { try { - newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s" , params.get(Integer.parseInt(m.group(1)) - 1)); + newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s", params.get(Integer.parseInt(m.group(1)) - 1)); } catch (Exception e) { // Couldn't find the param to replace } From 59b2805a4a4b87290f25402c144df1f47e4e2eef Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Thu, 23 Apr 2020 02:09:40 -0500 Subject: [PATCH 02/14] Fix NPE for unknown enchantments/enchantments not in bedrock --- .../item/translators/nbt/EnchantmentTranslator.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/EnchantmentTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/EnchantmentTranslator.java index fba206668..41e1ae36a 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/EnchantmentTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/translators/nbt/EnchantmentTranslator.java @@ -58,8 +58,10 @@ public class EnchantmentTranslator extends NbtItemStackTranslator { if (!(tag instanceof CompoundTag)) continue; CompoundTag bedrockTag = remapEnchantment((CompoundTag) tag); - bedrockTag.put(new ShortTag("GeyserStoredEnchantment", (short) 0)); - newTags.add(bedrockTag); + if (bedrockTag != null) { + bedrockTag.put(new ShortTag("GeyserStoredEnchantment", (short) 0)); + newTags.add(bedrockTag); + } } itemTag.remove("StoredEnchantments"); } @@ -117,11 +119,11 @@ public class EnchantmentTranslator extends NbtItemStackTranslator { private CompoundTag remapEnchantment(CompoundTag tag) { - Tag javaEnchLvl = ((CompoundTag) tag).get("lvl"); + Tag javaEnchLvl = tag.get("lvl"); if (!(javaEnchLvl instanceof ShortTag)) return null; - Tag javaEnchId = ((CompoundTag) tag).get("id"); + Tag javaEnchId = tag.get("id"); if (!(javaEnchId instanceof StringTag)) return null; From 298558078c0303cdc640d3c22788667621e82159 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Fri, 24 Apr 2020 18:57:15 -0800 Subject: [PATCH 03/14] Bump MCAuthLib to 1.3-SNAPSHOT (#416) --- connector/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connector/pom.xml b/connector/pom.xml index 4a488271d..dadffb080 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -111,7 +111,7 @@ com.github.steveice10 mcauthlib - 1.1-SNAPSHOT + 1.3-SNAPSHOT compile From e82adf2cf905ade54962435c58cd63f68e4abb65 Mon Sep 17 00:00:00 2001 From: OnlyBMan <27742182+OnlyBMan@users.noreply.github.com> Date: Fri, 24 Apr 2020 23:11:28 -0400 Subject: [PATCH 04/14] Implement shield blocking (#395) * basic shield blocking * Offhand works * Add Shield as id in BlockTranslator * Change Block Translator to Item Translator * Fix formatting Co-authored-by: DoctorMacc Co-authored-by: Redned --- .../org/geysermc/connector/entity/Entity.java | 40 +++++++++++++++++-- .../bedrock/BedrockInteractTranslator.java | 4 ++ ...BedrockInventoryTransactionTranslator.java | 4 ++ .../translators/item/ItemTranslator.java | 3 ++ connector/src/main/resources/mappings | 2 +- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/entity/Entity.java b/connector/src/main/java/org/geysermc/connector/entity/Entity.java index dcd2475a7..5596ca7ae 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -27,12 +27,15 @@ package org.geysermc.connector.entity; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; +import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction; +import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace; import com.github.steveice10.mc.protocol.data.message.TextMessage; +import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket; +import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket; import com.nukkitx.math.vector.Vector3f; -import com.nukkitx.protocol.bedrock.data.EntityData; -import com.nukkitx.protocol.bedrock.data.EntityDataMap; -import com.nukkitx.protocol.bedrock.data.EntityFlag; -import com.nukkitx.protocol.bedrock.data.EntityFlags; +import com.nukkitx.protocol.bedrock.data.*; import com.nukkitx.protocol.bedrock.packet.*; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; @@ -45,6 +48,7 @@ import org.geysermc.connector.entity.attribute.Attribute; import org.geysermc.connector.entity.attribute.AttributeType; import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.utils.AttributeUtils; import org.geysermc.connector.utils.MessageUtils; @@ -199,6 +203,28 @@ public class Entity { metadata.getFlags().setFlag(EntityFlag.SPRINTING, (xd & 0x08) == 0x08); metadata.getFlags().setFlag(EntityFlag.SWIMMING, (xd & 0x10) == 0x10); metadata.getFlags().setFlag(EntityFlag.GLIDING, (xd & 0x80) == 0x80); + + // Shield code + if (session.getPlayerEntity().getEntityId() == entityId && metadata.getFlags().getFlag(EntityFlag.SNEAKING)) { + if ((session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemTranslator.SHIELD) || + (session.getInventoryCache().getPlayerInventory().getItem(45) != null && session.getInventoryCache().getPlayerInventory().getItem(45).getId() == ItemTranslator.SHIELD)) { + ClientPlayerUseItemPacket useItemPacket; + metadata.getFlags().setFlag(EntityFlag.BLOCKING, true); + if (session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemTranslator.SHIELD) { + useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND); + } + // Else we just assume it's the offhand, to simplify logic and to assure the packet gets sent + else { + useItemPacket = new ClientPlayerUseItemPacket(Hand.OFF_HAND); + } + session.getDownstream().getSession().send(useItemPacket); + } + } else if (session.getPlayerEntity().getEntityId() == entityId && !metadata.getFlags().getFlag(EntityFlag.SNEAKING) && metadata.getFlags().getFlag(EntityFlag.BLOCKING)) { + metadata.getFlags().setFlag(EntityFlag.BLOCKING, false); + metadata.getFlags().setFlag(EntityFlag.DISABLE_BLOCKING, true); + ClientPlayerActionPacket releaseItemPacket = new ClientPlayerActionPacket(PlayerAction.RELEASE_USE_ITEM, new Position(0,0,0), BlockFace.DOWN); + session.getDownstream().getSession().send(releaseItemPacket); + } // metadata.getFlags().setFlag(EntityFlag.INVISIBLE, (xd & 0x20) == 0x20); if ((xd & 0x20) == 0x20) metadata.put(EntityData.SCALE, 0.0f); @@ -221,6 +247,12 @@ public class Entity { case 5: // no gravity metadata.getFlags().setFlag(EntityFlag.HAS_GRAVITY, !(boolean) entityMetadata.getValue()); break; + case 7: // blocking + if (entityMetadata.getType() == MetadataType.BYTE) { + byte xd = (byte) entityMetadata.getValue(); + metadata.getFlags().setFlag(EntityFlag.BLOCKING, (xd & 0x01) == 0x01); + } + break; } updateBedrockMetadata(session); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java index 012ef90ea..0d1c08b22 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java @@ -34,6 +34,7 @@ import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket; import com.nukkitx.protocol.bedrock.packet.InteractPacket; +import org.geysermc.connector.network.translators.item.ItemTranslator; @Translator(packet = InteractPacket.class) public class BedrockInteractTranslator extends PacketTranslator { @@ -46,6 +47,9 @@ public class BedrockInteractTranslator extends PacketTranslator switch (packet.getAction()) { case INTERACT: + if (session.getInventory().getItem(session.getInventory().getHeldItemSlot() + 36).getId() == ItemTranslator.SHIELD) { + break; + } ClientPlayerInteractEntityPacket interactPacket = new ClientPlayerInteractEntityPacket((int) entity.getEntityId(), InteractAction.INTERACT, Hand.MAIN_HAND); session.getDownstream().getSession().send(interactPacket); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java index fd23cd3ab..3f6eba55d 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java @@ -32,6 +32,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translators; +import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.utils.InventoryUtils; import com.nukkitx.math.vector.Vector3f; @@ -75,6 +76,9 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator itemTranslators = new Int2ObjectOpenHashMap(); private List nbtItemTranslators; private Map javaIdentifierMap = new HashMap<>(); + + // Shield ID, used in Entity.java + public static final int SHIELD = 829; public void init() { Reflections ref = new Reflections("org.geysermc.connector.network.translators.item"); diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index a6a790d94..efc9db6b7 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit a6a790d944eb153c93d0c414813d8a0880fd5d26 +Subproject commit efc9db6b7d51bdf145230933ac23b321ac1c132d From 357b681841bddec1f32d2791500a78df8e137319 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Fri, 24 Apr 2020 19:56:56 -0800 Subject: [PATCH 05/14] Fix movement when far away from the center of the world (#419) * Fix movement when far away from the center of the world * Add comment on why we parse a float as string Co-authored-by: Redned --- .../translators/bedrock/BedrockMovePlayerTranslator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java index 0b5ab02bb..0ae3d88ae 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMovePlayerTranslator.java @@ -67,8 +67,10 @@ public class BedrockMovePlayerTranslator extends PacketTranslator Date: Fri, 24 Apr 2020 23:59:31 -0400 Subject: [PATCH 06/14] Request additional information for bug reports (#413) * Add additional requests to bug report * Add request for plugins --- .github/ISSUE_TEMPLATE/bug_report.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 8001187ca..7a1fdba58 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -31,10 +31,10 @@ assignees: '' **Geyser Version** - + **Minecraft: Bedrock Edition Version** **Additional Context** - + From 602166b25da6d78e6fce2c8f38e3c876198b7a34 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 25 Apr 2020 05:03:34 +0100 Subject: [PATCH 07/14] Added offhand command (#412) --- .../connector/command/CommandManager.java | 6 +- .../command/defaults/OffhandCommand.java | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java diff --git a/connector/src/main/java/org/geysermc/connector/command/CommandManager.java b/connector/src/main/java/org/geysermc/connector/command/CommandManager.java index 24ce37a66..7b1b4d69b 100644 --- a/connector/src/main/java/org/geysermc/connector/command/CommandManager.java +++ b/connector/src/main/java/org/geysermc/connector/command/CommandManager.java @@ -28,10 +28,7 @@ package org.geysermc.connector.command; import lombok.Getter; import org.geysermc.common.command.ICommandManager; import org.geysermc.connector.GeyserConnector; -import org.geysermc.connector.command.defaults.HelpCommand; -import org.geysermc.connector.command.defaults.ListCommand; -import org.geysermc.connector.command.defaults.ReloadCommand; -import org.geysermc.connector.command.defaults.StopCommand; +import org.geysermc.connector.command.defaults.*; import java.util.Collections; import java.util.HashMap; @@ -51,6 +48,7 @@ public abstract class CommandManager implements ICommandManager { registerCommand(new ListCommand(connector, "list", "List all players connected through Geyser.", "geyser.command.list")); registerCommand(new ReloadCommand(connector, "reload", "Reloads the Geyser configurations. Kicks all players when used!", "geyser.command.reload")); registerCommand(new StopCommand(connector, "stop", "Shuts down Geyser.", "geyser.command.stop")); + registerCommand(new OffhandCommand(connector, "offhand", "Puts an items in your offhand.", "geyser.command.offhand")); } public void registerCommand(GeyserCommand command) { diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java new file mode 100644 index 000000000..d181d07e2 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/OffhandCommand.java @@ -0,0 +1,61 @@ +/* + * 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.command.defaults; + +import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction; +import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace; +import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket; +import org.geysermc.connector.GeyserConnector; +import org.geysermc.connector.command.CommandSender; +import org.geysermc.connector.command.GeyserCommand; +import org.geysermc.connector.network.session.GeyserSession; + +public class OffhandCommand extends GeyserCommand { + + private GeyserConnector connector; + + public OffhandCommand(GeyserConnector connector, String name, String description, String permission) { + super(name, description, permission); + + this.connector = connector; + } + + @Override + public void execute(CommandSender sender, String[] args) { + if (sender.isConsole()) { + return; + } + + // Make sure the sender is a Bedrock edition client + if (sender instanceof GeyserSession) { + GeyserSession session = (GeyserSession) sender; + ClientPlayerActionPacket releaseItemPacket = new ClientPlayerActionPacket(PlayerAction.SWAP_HANDS, new Position(0,0,0), + BlockFace.DOWN); + session.getDownstream().getSession().send(releaseItemPacket); + } + } +} From 6cfcab88d3643a864af63de5ff6b8adf7a489bdf Mon Sep 17 00:00:00 2001 From: LegacyGamerHD Date: Sat, 25 Apr 2020 06:05:06 +0200 Subject: [PATCH 08/14] Go to stable NukkitX Protocol Lib (#411) --- connector/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connector/pom.xml b/connector/pom.xml index dadffb080..84948efdb 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -33,7 +33,7 @@ com.nukkitx.protocol bedrock-v390 - 2.5.5-SNAPSHOT + 2.5.5 compile From 74f869beb572a4b6e9bf456b3e04fc6876abb7c2 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Sat, 25 Apr 2020 22:46:24 +0200 Subject: [PATCH 09/14] Fix map colors, remove unnecessary array stream (#423) --- connector/pom.xml | 2 +- .../java/world/JavaMapDataTranslator.java | 5 ++--- .../geysermc/connector/utils/MapColor.java | 21 +++++++------------ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/connector/pom.xml b/connector/pom.xml index 84948efdb..9de533b6a 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -33,7 +33,7 @@ com.nukkitx.protocol bedrock-v390 - 2.5.5 + 2.5.6-SNAPSHOT compile diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMapDataTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMapDataTranslator.java index 28022c16d..78681f8fb 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMapDataTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaMapDataTranslator.java @@ -51,13 +51,12 @@ public class JavaMapDataTranslator extends PacketTranslator mapItemDataPacket.setWidth(data.getColumns()); mapItemDataPacket.setHeight(data.getRows()); - // Every int entry is an ARGB color + // Every int entry is an ABGR color int[] colors = new int[data.getData().length]; int idx = 0; for (byte colorId : data.getData()) { - colors[idx] = MapColor.fromId(colorId).toARGB(); - idx++; + colors[idx++] = MapColor.fromId(colorId & 0xFF).toABGR(); } mapItemDataPacket.setColors(colors); diff --git a/connector/src/main/java/org/geysermc/connector/utils/MapColor.java b/connector/src/main/java/org/geysermc/connector/utils/MapColor.java index 2db144648..b011edc71 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MapColor.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MapColor.java @@ -1,7 +1,5 @@ package org.geysermc.connector.utils; -import java.util.Arrays; - public enum MapColor { COLOR_0(-1, -1, -1), COLOR_1(-1, -1, -1), @@ -212,6 +210,8 @@ public enum MapColor { COLOR_206(37, 22, 16), COLOR_207(19, 11, 8); + private static final MapColor[] VALUES = values(); + private final int red; private final int green; private final int blue; @@ -222,23 +222,18 @@ public enum MapColor { this.blue = blue; } - int getId() { - return ordinal(); - } - public static MapColor fromId(int id) { - return Arrays.stream(values()).filter(color -> color.getId() == id).findFirst().orElse(COLOR_0); + return id >= 0 && id < VALUES.length ? VALUES[id] : COLOR_0; } - public int toARGB() { + public int toABGR() { int alpha = 255; if (red == -1 && green == -1 && blue == -1) alpha = 0; // transparent - long result = red & 0xff; - result |= (green & 0xff) << 8; - result |= (blue & 0xff) << 16; - result |= (alpha & 0xff) << 24; - return (int) (result & 0xFFFFFFFFL); + return ((alpha & 0xFF) << 24) | + ((blue & 0xFF) << 16) | + ((green & 0xFF) << 8) | + ((red & 0xFF) << 0); } } \ No newline at end of file From 36708da1d635054ac7bb060243f2d15e7b801cb4 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+DoctorMacc@users.noreply.github.com> Date: Sat, 25 Apr 2020 18:29:26 -0400 Subject: [PATCH 10/14] Add message when you try to use creative crafting table (#422) * Add message when you try to use creative crafting table * Update message; set access level --- .../translators/inventory/PlayerInventoryTranslator.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java index c1e00d2a0..4173b8070 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java @@ -31,6 +31,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreat import com.nukkitx.protocol.bedrock.data.*; import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket; import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket; +import it.unimi.dsi.fastutil.longs.LongArraySet; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.Translators; @@ -40,6 +41,9 @@ import org.geysermc.connector.utils.InventoryUtils; import java.util.List; public class PlayerInventoryTranslator extends InventoryTranslator { + + private static final LongArraySet HAS_RECEIVED_MESSAGE = new LongArraySet(); + public PlayerInventoryTranslator() { super(46); } @@ -164,6 +168,11 @@ public class PlayerInventoryTranslator extends InventoryTranslator { //crafting grid is not visible in creative mode in java edition for (InventoryActionData action : actions) { if (action.getSource().getContainerId() == ContainerId.CURSOR && (action.getSlot() >= 28 && 31 >= action.getSlot())) { + if (!HAS_RECEIVED_MESSAGE.contains(session.getPlayerEntity().getEntityId())) { + // TODO: Allow the crafting table to be used with non-standalone versions + session.sendMessage("The creative crafting table cannot be used as it's incompatible with Minecraft: Java Edition."); + HAS_RECEIVED_MESSAGE.add(session.getPlayerEntity().getEntityId()); + } updateInventory(session, inventory); InventoryUtils.updateCursor(session); return; From 1a53f53d7849675f7fe7b1a66456d3a8e8c03c02 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sat, 25 Apr 2020 23:33:52 +0100 Subject: [PATCH 11/14] Added barriers to creative inventory crafting grid (#426) --- .../inventory/PlayerInventoryTranslator.java | 37 ++++++++++++++----- .../world/JavaNotifyClientTranslator.java | 27 +++++++------- .../org/geysermc/connector/utils/Toolbox.java | 6 ++- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java index 4173b8070..f4560787f 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java @@ -28,7 +28,10 @@ package org.geysermc.connector.network.translators.inventory; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket; -import com.nukkitx.protocol.bedrock.data.*; +import com.nukkitx.protocol.bedrock.data.ContainerId; +import com.nukkitx.protocol.bedrock.data.InventoryActionData; +import com.nukkitx.protocol.bedrock.data.InventorySource; +import com.nukkitx.protocol.bedrock.data.ItemData; import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket; import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket; import it.unimi.dsi.fastutil.longs.LongArraySet; @@ -37,6 +40,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.inventory.action.InventoryActionDataTranslator; import org.geysermc.connector.utils.InventoryUtils; +import org.geysermc.connector.utils.Toolbox; import java.util.List; @@ -50,14 +54,7 @@ public class PlayerInventoryTranslator extends InventoryTranslator { @Override public void updateInventory(GeyserSession session, Inventory inventory) { - // Crafting grid - for (int i = 1; i < 5; i++) { - InventorySlotPacket slotPacket = new InventorySlotPacket(); - slotPacket.setContainerId(ContainerId.CURSOR); - slotPacket.setSlot(i + 27); - slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i))); - session.getUpstream().sendPacket(slotPacket); - } + updateCraftingGrid(session, inventory); InventoryContentPacket inventoryContentPacket = new InventoryContentPacket(); inventoryContentPacket.setContainerId(ContainerId.INVENTORY); @@ -90,6 +87,28 @@ public class PlayerInventoryTranslator extends InventoryTranslator { session.getUpstream().sendPacket(offhandPacket); } + /** + * Update the crafting grid for the player to hide/show the barriers in the creative inventory + * @param session Session of the player + * @param inventory Inventory of the player + */ + public static void updateCraftingGrid(GeyserSession session, Inventory inventory) { + // Crafting grid + for (int i = 1; i < 5; i++) { + InventorySlotPacket slotPacket = new InventorySlotPacket(); + slotPacket.setContainerId(ContainerId.CURSOR); + slotPacket.setSlot(i + 27); + + if (session.getGameMode() == GameMode.CREATIVE) { + slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, new ItemStack(Toolbox.BARRIER_INDEX))); + }else{ + slotPacket.setItem(Translators.getItemTranslator().translateToBedrock(session, inventory.getItem(i))); + } + + session.getUpstream().sendPacket(slotPacket); + } + } + @Override public void updateSlot(GeyserSession session, Inventory inventory, int slot) { if (slot >= 1 && slot <= 44) { diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaNotifyClientTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaNotifyClientTranslator.java index 6c7eeaf92..3c11d87b6 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaNotifyClientTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaNotifyClientTranslator.java @@ -25,14 +25,6 @@ package org.geysermc.connector.network.translators.java.world; -import java.util.Set; -import java.util.concurrent.ThreadLocalRandom; - -import org.geysermc.connector.entity.Entity; -import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.PacketTranslator; -import org.geysermc.connector.network.translators.Translator; - import com.github.steveice10.mc.protocol.data.game.ClientRequest; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.world.notify.EnterCreditsValue; @@ -43,13 +35,16 @@ import com.nukkitx.protocol.bedrock.data.EntityDataMap; import com.nukkitx.protocol.bedrock.data.EntityFlag; import com.nukkitx.protocol.bedrock.data.LevelEventType; import com.nukkitx.protocol.bedrock.data.PlayerPermission; -import com.nukkitx.protocol.bedrock.packet.AdventureSettingsPacket; -import com.nukkitx.protocol.bedrock.packet.LevelEventPacket; -import com.nukkitx.protocol.bedrock.packet.SetEntityDataPacket; -import com.nukkitx.protocol.bedrock.packet.SetPlayerGameTypePacket; -import com.nukkitx.protocol.bedrock.packet.ShowCreditsPacket; - +import com.nukkitx.protocol.bedrock.packet.*; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; +import org.geysermc.connector.network.translators.Translator; +import org.geysermc.connector.network.translators.inventory.PlayerInventoryTranslator; + +import java.util.Set; +import java.util.concurrent.ThreadLocalRandom; @Translator(packet = ServerNotifyClientPacket.class) public class JavaNotifyClientTranslator extends PacketTranslator { @@ -110,6 +105,10 @@ public class JavaNotifyClientTranslator extends PacketTranslator ITEM_ENTRIES = new Int2ObjectOpenHashMap<>(); - public static final Map> LOCALE_MAPPINGS = new HashMap<>(); + public static int BARRIER_INDEX = 0; static { /* Load biomes */ @@ -126,6 +126,10 @@ public class Toolbox { entry.getValue().get("bedrock_id").intValue(), entry.getValue().get("bedrock_data").intValue())); } + if (entry.getKey().equals("minecraft:barrier")) { + BARRIER_INDEX = itemIndex; + } + itemIndex++; } From 8b487df6987f5b374182c49148ba1962d4abb7ff Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 25 Apr 2020 17:53:35 -0500 Subject: [PATCH 12/14] Properly remove players from Geyser playerlist (Fixes #421 --- .../geysermc/connector/command/defaults/ReloadCommand.java | 2 +- .../geysermc/connector/network/UpstreamPacketHandler.java | 6 +++--- .../geysermc/connector/network/session/GeyserSession.java | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java index bd8444c79..c38a0c23d 100644 --- a/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/ReloadCommand.java @@ -48,7 +48,7 @@ public class ReloadCommand extends GeyserCommand { } sender.sendMessage(ChatColor.YELLOW + "Reloading Geyser configurations... all connected bedrock clients will be kicked."); for (GeyserSession session : connector.getPlayers().values()) { - session.getUpstream().disconnect("Geyser has been reloaded... sorry for the inconvenience!"); + session.disconnect("Geyser has been reloaded... sorry for the inconvenience!"); } connector.reload(); } diff --git a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java index 4ad4deffa..7e41fca8b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java @@ -47,10 +47,10 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { @Override public boolean handle(LoginPacket loginPacket) { if (loginPacket.getProtocolVersion() > GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()) { - session.getUpstream().disconnect("Outdated Geyser proxy! I'm still on " + GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion()); + session.disconnect("Outdated Geyser proxy! I'm still on " + GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion()); return true; } else if (loginPacket.getProtocolVersion() < GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()) { - session.getUpstream().disconnect("Outdated Bedrock client! Please use " + GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion()); + session.disconnect("Outdated Bedrock client! Please use " + GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion()); return true; } @@ -80,7 +80,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { session.getUpstream().sendPacket(stack); break; default: - session.getUpstream().disconnect("disconnectionScreen.resourcePack"); + session.disconnect("disconnectionScreen.resourcePack"); break; } diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 12ae39bfc..fdfc14714 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -330,10 +330,16 @@ public class GeyserSession implements CommandSender { downstream.getSession().disconnect(reason); } if (upstream != null && !upstream.isClosed()) { + connector.getPlayers().remove(this.upstream.getAddress()); upstream.disconnect(reason); } } + this.entityCache.getEntities().clear(); + this.scoreboardCache.removeScoreboard(); + this.inventoryCache.getInventories().clear(); + this.windowCache.getWindows().clear(); + closed = true; } From 61587ec320089278c69d6146b6e35f05cc0dfe84 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 25 Apr 2020 17:56:19 -0500 Subject: [PATCH 13/14] Use Int2ObjectMap in InventoryCache and JavaServerDeclareCommandsTranslator --- .../connector/network/session/cache/InventoryCache.java | 7 +++---- .../java/JavaServerDeclareCommandsTranslator.java | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java index 8734c710a..032f64024 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java @@ -25,14 +25,13 @@ package org.geysermc.connector.network.session.cache; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import lombok.Getter; import lombok.Setter; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; -import java.util.HashMap; -import java.util.Map; - public class InventoryCache { private GeyserSession session; @@ -42,7 +41,7 @@ public class InventoryCache { private Inventory openInventory; @Getter - private Map inventories = new HashMap(); + private Int2ObjectMap inventories = new Int2ObjectOpenHashMap<>(); public InventoryCache(GeyserSession session) { this.session = session; diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaServerDeclareCommandsTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaServerDeclareCommandsTranslator.java index edc20a648..d57b89487 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaServerDeclareCommandsTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaServerDeclareCommandsTranslator.java @@ -32,6 +32,8 @@ import com.nukkitx.protocol.bedrock.data.CommandData; import com.nukkitx.protocol.bedrock.data.CommandEnumData; import com.nukkitx.protocol.bedrock.data.CommandParamData; import com.nukkitx.protocol.bedrock.packet.AvailableCommandsPacket; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import lombok.Getter; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.session.GeyserSession; @@ -45,8 +47,8 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator commandData = new ArrayList<>(); - Map commands = new HashMap<>(); - Map> commandArgs = new HashMap<>(); + Int2ObjectMap commands = new Int2ObjectOpenHashMap<>(); + Int2ObjectMap> commandArgs = new Int2ObjectOpenHashMap<>(); // Get the first node, it should be a root node CommandNode rootNode = packet.getNodes()[packet.getFirstNodeIndex()]; From 7320e38a0823b2d328b5bd5226a4f8aeebbbf1b9 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 25 Apr 2020 17:56:39 -0500 Subject: [PATCH 14/14] Update mappings --- connector/src/main/resources/mappings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index efc9db6b7..4d53e24f9 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit efc9db6b7d51bdf145230933ac23b321ac1c132d +Subproject commit 4d53e24f92d20e51909ee48ac28ed17f92cb819e