Check if bucket sounds should be played in adventure mode

Fixes #2608
This commit is contained in:
Camotoy 2021-11-01 21:49:20 -04:00
parent 6da6636a98
commit 6cda15cb8d
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 46 additions and 1 deletions

View file

@ -25,10 +25,16 @@
package org.geysermc.connector.network.translators.sound;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
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 com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.registry.Registries;
import org.geysermc.connector.utils.BlockUtils;
import java.util.Map;
@ -88,4 +94,38 @@ public interface BlockSoundInteractionHandler extends SoundInteractionHandler<St
((BlockSoundInteractionHandler) interactionEntry.getValue()).handleInteraction(session, position, identifier);
}
}
/**
* Determines if the adventure gamemode would prevent this item from actually succeeding
*/
static boolean canInteract(GeyserSession session, GeyserItemStack itemInHand, String blockIdentifier) {
if (session.getGameMode() != GameMode.ADVENTURE) {
// There are no restrictions on the item
return true;
}
CompoundTag tag = itemInHand.getNbt();
if (tag == null) {
// No CanPlaceOn tag can exist
return false;
}
ListTag canPlaceOn = tag.get("CanPlaceOn");
if (canPlaceOn == null || canPlaceOn.size() == 0) {
return false;
}
String cleanIdentifier = BlockUtils.getCleanIdentifier(blockIdentifier);
for (Tag t : canPlaceOn) {
if (t instanceof StringTag stringTag) {
if (cleanIdentifier.equals(stringTag.getValue())) {
// This operation would/could be a success!
return true;
}
}
}
// The block in world is not present in the CanPlaceOn tag on the item
return false;
}
}

View file

@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators.sound.block;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.sound.BlockSoundInteractionHandler;
import org.geysermc.connector.network.translators.sound.SoundHandler;
@ -40,7 +41,11 @@ public class BucketSoundInteractionHandler implements BlockSoundInteractionHandl
if (session.getBucketScheduledFuture() == null) {
return; // No bucket was really interacted with
}
String handItemIdentifier = session.getPlayerInventory().getItemInHand().getMapping(session).getJavaIdentifier();
GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
String handItemIdentifier = itemStack.getMapping(session).getJavaIdentifier();
if (!BlockSoundInteractionHandler.canInteract(session, itemStack, identifier)) {
return;
}
LevelSoundEventPacket soundEventPacket = new LevelSoundEventPacket();
soundEventPacket.setPosition(position);
soundEventPacket.setIdentifier(":");