Add sound handlers for block interaction

These sound handlers had to be added since on Minecraft: Java Edition, these sounds are handled clientside whilst Minecraft: Bedrock Edition expects something from the server. To counter this, we emulate the sounds of that on Minecraft: Java Edition within Geyser and send it to the client. The code may seem a tadredundant, but there is not much of an alternative unfortunately.
This commit is contained in:
RednedEpic 2020-04-30 00:21:02 -05:00
parent 4d32f6ab70
commit ad596cdccb
14 changed files with 542 additions and 8 deletions

View file

@ -118,6 +118,10 @@ public class GeyserSession implements CommandSender {
private GameMode gameMode = GameMode.SURVIVAL; private GameMode gameMode = GameMode.SURVIVAL;
private final AtomicInteger pendingDimSwitches = new AtomicInteger(0); private final AtomicInteger pendingDimSwitches = new AtomicInteger(0);
@Setter
private boolean sneaking;
@Setter @Setter
private boolean sprinting; private boolean sprinting;
@ -133,6 +137,12 @@ public class GeyserSession implements CommandSender {
@Setter @Setter
private String lastBlockPlacedId; private String lastBlockPlacedId;
@Setter
private boolean interacting;
@Setter
private Vector3i lastInteractionPosition;
@Setter @Setter
private boolean switchingDimension = false; private boolean switchingDimension = false;
private boolean manyDimPackets = false; private boolean manyDimPackets = false;

View file

@ -78,10 +78,12 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
case START_SNEAK: case START_SNEAK:
ClientPlayerStatePacket startSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SNEAKING); ClientPlayerStatePacket startSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SNEAKING);
session.getDownstream().getSession().send(startSneakPacket); session.getDownstream().getSession().send(startSneakPacket);
session.setSneaking(true);
break; break;
case STOP_SNEAK: case STOP_SNEAK:
ClientPlayerStatePacket stopSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SNEAKING); ClientPlayerStatePacket stopSneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.STOP_SNEAKING);
session.getDownstream().getSession().send(stopSneakPacket); session.getDownstream().getSession().send(stopSneakPacket);
session.setSneaking(false);
break; break;
case START_SPRINT: case START_SPRINT:
ClientPlayerStatePacket startSprintPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SPRINTING); ClientPlayerStatePacket startSprintPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SPRINTING);

View file

@ -107,6 +107,8 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
session.setLastBlockPlacePosition(blockPos); session.setLastBlockPlacePosition(blockPos);
session.setLastBlockPlacedId(handItem.getJavaIdentifier()); session.setLastBlockPlacedId(handItem.getJavaIdentifier());
} }
session.setLastInteractionPosition(packet.getBlockPosition());
session.setInteracting(true);
break; break;
case 1: case 1:
if (session.getInventory().getItem(session.getInventory().getHeldItemSlot() + 36).getId() == ItemTranslator.SHIELD) { if (session.getInventory().getItem(session.getInventory().getHeldItemSlot() + 36).getId() == ItemTranslator.SHIELD) {

View file

@ -31,6 +31,7 @@ import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translator;
import org.geysermc.connector.network.translators.sound.SoundInteractionHandler;
import org.geysermc.connector.network.translators.world.block.BlockTranslator; import org.geysermc.connector.network.translators.world.block.BlockTranslator;
import org.geysermc.connector.utils.ChunkUtils; import org.geysermc.connector.utils.ChunkUtils;
@ -42,14 +43,19 @@ public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChang
@Override @Override
public void translate(ServerBlockChangePacket packet, GeyserSession session) { public void translate(ServerBlockChangePacket packet, GeyserSession session) {
ChunkUtils.updateBlock(session, packet.getRecord().getBlock(), packet.getRecord().getPosition()); ChunkUtils.updateBlock(session, packet.getRecord().getBlock(), packet.getRecord().getPosition());
this.checkInteract(session, packet);
this.checkPlace(session, packet);
}
private boolean checkPlace(GeyserSession session, ServerBlockChangePacket packet) {
Vector3i lastPlacePos = session.getLastBlockPlacePosition(); Vector3i lastPlacePos = session.getLastBlockPlacePosition();
if (lastPlacePos == null) { if (lastPlacePos == null) {
return; return false;
} }
if (lastPlacePos.getX() != packet.getRecord().getPosition().getX() if ((lastPlacePos.getX() != packet.getRecord().getPosition().getX()
|| lastPlacePos.getY() != packet.getRecord().getPosition().getY() || lastPlacePos.getY() != packet.getRecord().getPosition().getY()
|| lastPlacePos.getZ() != packet.getRecord().getPosition().getZ()) { || lastPlacePos.getZ() != packet.getRecord().getPosition().getZ())) {
return; return false;
} }
// We need to check if the identifier is the same, else a packet with the sound of what the // We need to check if the identifier is the same, else a packet with the sound of what the
@ -63,7 +69,7 @@ public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChang
if (!contains) { if (!contains) {
session.setLastBlockPlacePosition(null); session.setLastBlockPlacePosition(null);
session.setLastBlockPlacedId(null); session.setLastBlockPlacedId(null);
return; return false;
} }
// This is not sent from the server, so we need to send it this way // This is not sent from the server, so we need to send it this way
@ -76,5 +82,22 @@ public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChang
session.getUpstream().sendPacket(placeBlockSoundPacket); session.getUpstream().sendPacket(placeBlockSoundPacket);
session.setLastBlockPlacePosition(null); session.setLastBlockPlacePosition(null);
session.setLastBlockPlacedId(null); session.setLastBlockPlacedId(null);
return true;
}
private void checkInteract(GeyserSession session, ServerBlockChangePacket packet) {
Vector3i lastInteractPos = session.getLastInteractionPosition();
if (lastInteractPos == null || !session.isInteracting()) {
return;
}
if ((lastInteractPos.getX() != packet.getRecord().getPosition().getX()
|| lastInteractPos.getY() != packet.getRecord().getPosition().getY()
|| lastInteractPos.getZ() != packet.getRecord().getPosition().getZ())) {
return;
}
String identifier = BlockTranslator.getJavaIdBlockMap().inverse().get(packet.getRecord().getBlock());
session.setInteracting(false);
session.setLastInteractionPosition(null);
SoundInteractionHandler.handleBlockInteraction(session, lastInteractPos.toFloat(), identifier);
} }
} }

View file

@ -32,7 +32,7 @@ import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType; import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.data.SoundEvent; import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket; import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEvent2Packet; import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
@ -96,7 +96,7 @@ public class JavaPlayEffectTranslator extends PacketTranslator<ServerPlayEffectP
// TODO: Opening doors also does not work as the player // TODO: Opening doors also does not work as the player
effect.setType(LevelEventType.valueOf(geyserEffect.getBedrockName())); effect.setType(LevelEventType.valueOf(geyserEffect.getBedrockName()));
} else if (geyserEffect.getType().equals("soundEvent")) { } else if (geyserEffect.getType().equals("soundEvent")) {
LevelSoundEvent2Packet soundEvent = new LevelSoundEvent2Packet(); LevelSoundEventPacket soundEvent = new LevelSoundEventPacket();
// Separate case since each RecordEffectData in Java is an individual track in Bedrock // Separate case since each RecordEffectData in Java is an individual track in Bedrock
if (geyserEffect.getJavaName().equals("RECORD")) { if (geyserEffect.getJavaName().equals("RECORD")) {
RecordEffectData recordEffectData = (RecordEffectData) packet.getData(); RecordEffectData recordEffectData = (RecordEffectData) packet.getData();

View file

@ -0,0 +1,72 @@
/*
* 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.sound;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.Translators;
@SoundHandler(items = "bucket")
public class BucketSoundInteractionHandler implements SoundInteractionHandler {
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
String handItemIdentifier = Translators.getItemTranslator().getItem(session.getInventory().getItemInHand()).getJavaIdentifier();
LevelSoundEventPacket soundEventPacket = new LevelSoundEventPacket();
soundEventPacket.setPosition(position);
soundEventPacket.setIdentifier(":");
soundEventPacket.setRelativeVolumeDisabled(false);
soundEventPacket.setBabySound(false);
soundEventPacket.setExtraData(-1);
SoundEvent soundEvent = null;
switch (handItemIdentifier) {
case "minecraft:bucket":
if (identifier.contains("water[")) {
soundEvent = SoundEvent.BUCKET_FILL_WATER;
} else if (identifier.contains("lava[")) {
soundEvent = SoundEvent.BUCKET_FILL_LAVA;
}
break;
case "minecraft:lava_bucket":
soundEvent = SoundEvent.BUCKET_EMPTY_LAVA;
break;
case "minecraft:fish_bucket":
soundEvent = SoundEvent.BUCKET_EMPTY_FISH;
break;
case "minecraft:water_bucket":
soundEvent = SoundEvent.BUCKET_EMPTY_WATER;
break;
}
if (soundEvent != null) {
soundEventPacket.setSound(soundEvent);
session.getUpstream().sendPacket(soundEventPacket);
}
}
}

View file

@ -0,0 +1,46 @@
/*
* 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.sound;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
@SoundHandler(blocks = "door")
public class DoorSoundInteractionHandler implements SoundInteractionHandler {
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setType(LevelEventType.SOUND_DOOR);
levelEventPacket.setPosition(position);
levelEventPacket.setData(0);
session.getUpstream().sendPacket(levelEventPacket);
}
}

View file

@ -0,0 +1,49 @@
/*
* 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.sound;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
@SoundHandler(items = "flint_and_steel", ignoreSneakingWhileHolding = true)
public class FlintAndSteelInteractionHandler implements SoundInteractionHandler {
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position);
levelSoundEventPacket.setBabySound(false);
levelSoundEventPacket.setRelativeVolumeDisabled(false);
levelSoundEventPacket.setIdentifier(":");
levelSoundEventPacket.setSound(SoundEvent.IGNITE);
levelSoundEventPacket.setExtraData(-1);
session.getUpstream().sendPacket(levelSoundEventPacket);
}
}

View file

@ -0,0 +1,50 @@
/*
* 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.sound;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
@SoundHandler(blocks = "grass_path", items = "shovel", ignoreSneakingWhileHolding = true)
public class GrassPathInteractionHandler implements SoundInteractionHandler {
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position);
levelSoundEventPacket.setBabySound(false);
levelSoundEventPacket.setRelativeVolumeDisabled(false);
levelSoundEventPacket.setIdentifier(":");
levelSoundEventPacket.setSound(SoundEvent.ITEM_USE_ON);
levelSoundEventPacket.setExtraData(BlockTranslator.getBedrockBlockId(BlockTranslator.getJavaBlockState(identifier)));
session.getUpstream().sendPacket(levelSoundEventPacket);
}
}

View file

@ -0,0 +1,46 @@
/*
* 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.sound;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
@SoundHandler(blocks = "lever")
public class LeverSoundInteractionHandler implements SoundInteractionHandler {
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
boolean powered = identifier.contains("powered=true");
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setPosition(position);
levelEventPacket.setType(LevelEventType.REDSTONE_TRIGGER);
levelEventPacket.setData(powered ? 500 : 600);
session.getUpstream().sendPacket(levelEventPacket);
}
}

View file

@ -0,0 +1,66 @@
/*
* 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.sound;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Marks if a class should be handled as a
* {@link SoundInteractionHandler}.
*/
@Retention(value = RetentionPolicy.RUNTIME)
public @interface SoundHandler {
/**
* The identifier(s) that the placed block must contain
* one of.
* Leave empty to ignore.
*
* @return the value the interacted block must contain
*/
String[] blocks() default {};
/**
* The identifier(s) that the player's hand item
* must contain one of.
* Leave empty to ignore.
*
* @return the value the item in the player's hand must contain
*/
String[] items() default {};
/**
* Controls if the interaction should still be
* called even if the player is sneaking while
* holding something in their hand.
*
* @return if the interaction should continue when player
* is holding something in their hand
*/
boolean ignoreSneakingWhileHolding() default false;
}

View file

@ -0,0 +1,69 @@
/*
* 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.sound;
import org.reflections.Reflections;
import java.util.HashMap;
import java.util.Map;
/**
* Registry that holds {@link SoundInteractionHandler}s.
*/
public class SoundHandlerRegistry {
static final Map<SoundHandler, SoundInteractionHandler> INTERACTION_HANDLERS = new HashMap<>();
static {
Reflections ref = new Reflections("org.geysermc.connector.network.translators.sound");
for (Class<?> clazz : ref.getTypesAnnotatedWith(SoundHandler.class)) {
try {
SoundInteractionHandler interactionHandler = (SoundInteractionHandler) clazz.newInstance();
SoundHandler annotation = clazz.getAnnotation(SoundHandler.class);
INTERACTION_HANDLERS.put(annotation, interactionHandler);
} catch (InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
}
private SoundHandlerRegistry() {
}
public static void init() {
// no-op
}
/**
* Returns a map of the interaction handlers
*
* @return a map of the interaction handlers
*/
public static Map<SoundHandler, SoundInteractionHandler> getInteractionHandlers() {
return INTERACTION_HANDLERS;
}
}

View file

@ -0,0 +1,99 @@
/*
* 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.sound;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.Translators;
import java.util.Map;
/**
* Handler for playing sounds when right-clicking
* blocks. Due to Minecraft: Bedrock Edition
* expecting interaction sounds to be played serverside
* and Minecraft: Java Edition handling them clientside,
* this had to be made to handle scenarios like that.
*/
public interface SoundInteractionHandler {
/**
* Handles the interaction when a player
* right-clicks a block.
*
* @param session the session interacting with the block
* @param position the position of the block
* @param identifier the identifier of the block
*/
void handleInteraction(GeyserSession session, Vector3f position, String identifier);
/**
* Handles the block interaction when a player
* right-clicks a block.
*
* @param session the session interacting with the block
* @param position the position of the block
* @param identifier the identifier of the block
*/
static void handleBlockInteraction(GeyserSession session, Vector3f position, String identifier) {
for (Map.Entry<SoundHandler, SoundInteractionHandler> interactionEntry : SoundHandlerRegistry.INTERACTION_HANDLERS.entrySet()) {
if (interactionEntry.getKey().blocks().length != 0) {
boolean contains = false;
for (String blockIdentifier : interactionEntry.getKey().blocks()) {
if (identifier.contains(blockIdentifier)) {
contains = true;
break;
}
}
if (!contains) continue;
}
ItemStack itemInHand = session.getInventory().getItemInHand();
if (interactionEntry.getKey().items().length != 0) {
if (itemInHand == null || itemInHand.getId() == 0) {
continue;
}
String handIdentifier = Translators.getItemTranslator().getItem(session.getInventory().getItemInHand()).getJavaIdentifier();
boolean contains = false;
for (String itemIdentifier : interactionEntry.getKey().items()) {
if (handIdentifier.contains(itemIdentifier)) {
contains = true;
break;
}
}
if (!contains) continue;
}
if (session.isSneaking() && !interactionEntry.getKey().ignoreSneakingWhileHolding()) {
if (session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() != 0) {
continue;
}
}
interactionEntry.getValue().handleInteraction(session, position, identifier);
}
}
}

@ -1 +1 @@
Subproject commit 4d53e24f92d20e51909ee48ac28ed17f92cb819e Subproject commit e28b127030a81330d9fabef30efda8cbfdf4e5e3