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.
This commit is contained in:
RednedEpic 2020-04-23 01:01:33 -05:00
parent 09297a467c
commit 79185fabb5
8 changed files with 59 additions and 21 deletions

View file

@ -90,6 +90,12 @@
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.steveice10</groupId>
<artifactId>opennbt</artifactId>

View file

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

View file

@ -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<InventoryTransactionPacket> {
@ -99,7 +97,11 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
clickPos = clickPos.add(1, 0, 0);
break;
}
session.setLastBlockPlacePosition(clickPos);
ItemEntry handItem = Translators.getItemTranslator().getItem(packet.getItemInHand());
if (handItem.isBlock()) {
session.setLastBlockPlacePosition(clickPos);
session.setLastBlockPlacedId(handItem.getJavaIdentifier());
}
break;
case 1:
ClientPlayerUseItemPacket useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND);

View file

@ -27,6 +27,8 @@ package org.geysermc.connector.network.translators.block;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.nukkitx.nbt.CompoundTagBuilder;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTInputStream;
@ -59,7 +61,7 @@ public class BlockTranslator {
private static final Int2IntMap JAVA_TO_BEDROCK_BLOCK_MAP = new Int2IntOpenHashMap();
private static final Int2ObjectMap<BlockState> BEDROCK_TO_JAVA_BLOCK_MAP = new Int2ObjectOpenHashMap<>();
private static final Map<String, BlockState> JAVA_ID_BLOCK_MAP = new HashMap<>();
private static final BiMap<String, BlockState> 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<String, BlockState> getJavaIdBlockMap() {
return JAVA_ID_BLOCK_MAP;
}
public static BlockState getJavaWaterloggedState(int bedrockId) {
return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId);
}

View file

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

View file

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

View file

@ -51,6 +51,21 @@ public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChang
|| lastPlacePos.getZ() != packet.getRecord().getPosition().getZ()) {
return;
}
// We need to check if the identifier is the same, else a packet with the sound of what the
// player has in their hand is played, despite if the block is being placed or not
boolean contains = false;
String identifier = BlockTranslator.getJavaIdBlockMap().inverse().get(packet.getRecord().getBlock()).split("\\[")[0];
if (identifier.equals(session.getLastBlockPlacedId())) {
contains = true;
}
if (!contains) {
session.setLastBlockPlacePosition(null);
session.setLastBlockPlacedId(null);
return;
}
// This is not sent from the server, so we need to send it this way
LevelSoundEventPacket placeBlockSoundPacket = new LevelSoundEventPacket();
placeBlockSoundPacket.setSound(SoundEvent.PLACE);
@ -60,5 +75,6 @@ public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChang
placeBlockSoundPacket.setIdentifier(":");
session.getUpstream().sendPacket(placeBlockSoundPacket);
session.setLastBlockPlacePosition(null);
session.setLastBlockPlacedId(null);
}
}

View file

@ -111,20 +111,23 @@ public class Toolbox {
entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue(),
entry.getValue().get("tool_type").textValue(),
entry.getValue().get("tool_tier").textValue()));
entry.getValue().get("tool_tier").textValue(),
entry.getValue().get("is_block").booleanValue()));
} else {
ITEM_ENTRIES.put(itemIndex, new ToolItemEntry(
entry.getKey(), itemIndex,
entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue(),
entry.getValue().get("tool_type").textValue(),
""));
"",
entry.getValue().get("is_block").booleanValue()));
}
} else {
ITEM_ENTRIES.put(itemIndex, new ItemEntry(
entry.getKey(), itemIndex,
entry.getValue().get("bedrock_id").intValue(),
entry.getValue().get("bedrock_data").intValue()));
entry.getValue().get("bedrock_data").intValue(),
entry.getValue().get("is_block").booleanValue()));
}
itemIndex++;
}