From 79185fabb55600da337fa408da2e69a425c9da52 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Thu, 23 Apr 2020 01:01:33 -0500 Subject: [PATCH] Only send block sounds when a block is being placed Previously, any time a block was clicked whether it be with a block in the hand or not, a sound would play. This checks if the item in the players hand is indeed a block as well as the same block in the UpdateBlockPacket, and properly plays the packet. --- connector/pom.xml | 6 ++++ .../network/session/GeyserSession.java | 3 ++ ...BedrockInventoryTransactionTranslator.java | 30 ++++++++++--------- .../translators/block/BlockTranslator.java | 8 ++++- .../network/translators/item/ItemEntry.java | 4 ++- .../translators/item/ToolItemEntry.java | 4 +-- .../java/world/JavaBlockChangeTranslator.java | 16 ++++++++++ .../org/geysermc/connector/utils/Toolbox.java | 9 ++++-- 8 files changed, 59 insertions(+), 21 deletions(-) diff --git a/connector/pom.xml b/connector/pom.xml index 9e223d4e..a0142efb 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -90,6 +90,12 @@ 8.3.1 compile + + com.google.guava + guava + 29.0-jre + compile + com.github.steveice10 opennbt 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 7aaed75f..bb0878b6 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 @@ -133,6 +133,9 @@ public class GeyserSession implements CommandSender { @Setter private Vector3i lastBlockPlacePosition; + @Setter + private String lastBlockPlacedId; + @Setter private boolean switchingDimension = false; private boolean manyDimPackets = false; 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 c6a5e7bd..f17df09b 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 @@ -26,19 +26,6 @@ package org.geysermc.connector.network.translators.bedrock; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPlaceBlockPacket; -import com.nukkitx.math.vector.Vector3i; -import org.geysermc.connector.entity.Entity; -import org.geysermc.connector.inventory.Inventory; -import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.ItemStackTranslator; -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.block.BlockTranslator; -import org.geysermc.connector.network.translators.item.ItemTranslator; -import org.geysermc.connector.utils.InventoryUtils; - -import com.nukkitx.math.vector.Vector3f; import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; @@ -48,8 +35,19 @@ import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerActionPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket; +import com.nukkitx.math.vector.Vector3i; +import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.packet.InventoryTransactionPacket; +import org.geysermc.connector.entity.Entity; +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.Translator; +import org.geysermc.connector.network.translators.Translators; +import org.geysermc.connector.network.translators.item.ItemEntry; +import org.geysermc.connector.utils.InventoryUtils; + @Translator(packet = InventoryTransactionPacket.class) public class BedrockInventoryTransactionTranslator extends PacketTranslator { @@ -99,7 +97,11 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator BEDROCK_TO_JAVA_BLOCK_MAP = new Int2ObjectOpenHashMap<>(); - private static final Map JAVA_ID_BLOCK_MAP = new HashMap<>(); + private static final BiMap JAVA_ID_BLOCK_MAP = HashBiMap.create(); private static final IntSet WATERLOGGED = new IntOpenHashSet(); // Bedrock carpet ID, used in LlamaEntity.java for decoration @@ -265,6 +267,10 @@ public class BlockTranslator { return WATERLOGGED.contains(state.getId()); } + public static BiMap getJavaIdBlockMap() { + return JAVA_ID_BLOCK_MAP; + } + public static BlockState getJavaWaterloggedState(int bedrockId) { return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemEntry.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemEntry.java index e579c20e..e5c80329 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemEntry.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemEntry.java @@ -32,7 +32,7 @@ import lombok.Getter; @AllArgsConstructor public class ItemEntry { - public static ItemEntry AIR = new ItemEntry("minecraft:air", 0, 0, 0); + public static ItemEntry AIR = new ItemEntry("minecraft:air", 0, 0, 0, false); private final String javaIdentifier; private final int javaId; @@ -40,6 +40,8 @@ public class ItemEntry { private final int bedrockId; private final int bedrockData; + private final boolean block; + @Override public boolean equals(Object obj) { return obj == this || (obj instanceof ItemEntry && ((ItemEntry) obj).getBedrockId() == this.getBedrockId() && ((ItemEntry) obj).getJavaIdentifier().equals(this.getJavaIdentifier())); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/ToolItemEntry.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/ToolItemEntry.java index 5d1ddd26..cfc05a4a 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/ToolItemEntry.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/ToolItemEntry.java @@ -7,8 +7,8 @@ public class ToolItemEntry extends ItemEntry { private final String toolType; private final String toolTier; - public ToolItemEntry(String javaIdentifier, int javaId, int bedrockId, int bedrockData, String toolType, String toolTier) { - super(javaIdentifier, javaId, bedrockId, bedrockData); + public ToolItemEntry(String javaIdentifier, int javaId, int bedrockId, int bedrockData, String toolType, String toolTier, boolean isBlock) { + super(javaIdentifier, javaId, bedrockId, bedrockData, isBlock); this.toolType = toolType; this.toolTier = toolTier; } 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 08bd3cb6..ec40e27a 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 @@ -51,6 +51,21 @@ public class JavaBlockChangeTranslator extends PacketTranslator