Add implementation for ClientEmoteEvent

Also, a few random changes I've stored since forever.
This commit is contained in:
Camotoy 2023-02-15 00:17:14 -05:00
parent 6a6a601efc
commit ee754c529b
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
10 changed files with 27 additions and 30 deletions

View File

@ -33,11 +33,11 @@ import org.geysermc.geyser.api.event.connection.ConnectionEvent;
/** /**
* Called whenever a Bedrock player performs an emote on their end, before it is broadcasted to the rest of the server. * Called whenever a Bedrock player performs an emote on their end, before it is broadcasted to the rest of the server.
*/ */
public final class BedrockEmoteEvent extends ConnectionEvent implements Cancellable { public final class ClientEmoteEvent extends ConnectionEvent implements Cancellable {
private final String emoteId; private final String emoteId;
private boolean cancelled; private boolean cancelled;
public BedrockEmoteEvent(@NonNull GeyserConnection connection, @NonNull String emoteId) { public ClientEmoteEvent(@NonNull GeyserConnection connection, @NonNull String emoteId) {
super(connection); super(connection);
this.emoteId = emoteId; this.emoteId = emoteId;
} }

View File

@ -25,10 +25,6 @@
package org.geysermc.geyser.command.defaults; package org.geysermc.geyser.command.defaults;
import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
import com.nukkitx.math.vector.Vector3i;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.GeyserCommand; import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource; import org.geysermc.geyser.command.GeyserCommandSource;
@ -46,9 +42,7 @@ public class OffhandCommand extends GeyserCommand {
return; return;
} }
ServerboundPlayerActionPacket releaseItemPacket = new ServerboundPlayerActionPacket(PlayerAction.SWAP_HANDS, Vector3i.ZERO, session.requestOffhandSwap();
Direction.DOWN, 0);
session.sendDownstreamPacket(releaseItemPacket);
} }
@Override @Override

View File

@ -37,7 +37,7 @@ public enum ToolTier {
DIAMOND(8), DIAMOND(8),
NETHERITE(9); NETHERITE(9);
public static final ToolTier[] VALUES = values(); private static final ToolTier[] VALUES = values();
private final int speed; private final int speed;

View File

@ -29,9 +29,7 @@ import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder; import com.nukkitx.nbt.NbtMapBuilder;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.geysermc.geyser.level.block.BlockStateValues;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.ChunkCache;
import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator; import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator;
public class GeyserWorldManager extends WorldManager { public class GeyserWorldManager extends WorldManager {
@ -39,11 +37,7 @@ public class GeyserWorldManager extends WorldManager {
@Override @Override
public int getBlockAt(GeyserSession session, int x, int y, int z) { public int getBlockAt(GeyserSession session, int x, int y, int z) {
ChunkCache chunkCache = session.getChunkCache(); return session.getChunkCache().getBlockAt(x, y, z);
if (chunkCache != null) { // Chunk cache can be null if the session is closed asynchronously
return chunkCache.getBlockAt(x, y, z);
}
return BlockStateValues.JAVA_AIR_ID;
} }
@Override @Override

View File

@ -33,7 +33,7 @@ public enum PistonBehavior {
DESTROY, DESTROY,
PUSH_ONLY; PUSH_ONLY;
public static final PistonBehavior[] VALUES = values(); private static final PistonBehavior[] VALUES = values();
public static PistonBehavior getByName(String name) { public static PistonBehavior getByName(String name) {
String upperCase = name.toUpperCase(Locale.ROOT); String upperCase = name.toUpperCase(Locale.ROOT);

View File

@ -28,12 +28,13 @@ package org.geysermc.geyser.registry.type;
import com.nukkitx.protocol.bedrock.data.LevelEventType; import com.nukkitx.protocol.bedrock.data.LevelEventType;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNullableByDefault; import javax.annotation.ParametersAreNullableByDefault;
@ParametersAreNullableByDefault @ParametersAreNullableByDefault
public record ParticleMapping(LevelEventType levelEventType, String identifier) { public record ParticleMapping(LevelEventType levelEventType, String identifier) {
public int getParticleId(GeyserSession session) { public int getParticleId(@Nonnull GeyserSession session) {
if (this.levelEventType == null) { if (this.levelEventType == null) {
return -1; return -1;
} }

View File

@ -180,7 +180,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
private final AdvancementsCache advancementsCache; private final AdvancementsCache advancementsCache;
private final BookEditCache bookEditCache; private final BookEditCache bookEditCache;
private final ChunkCache chunkCache; private @org.checkerframework.checker.nullness.qual.NonNull final ChunkCache chunkCache;
private final EntityCache entityCache; private final EntityCache entityCache;
private final EntityEffectCache effectCache; private final EntityEffectCache effectCache;
private final FormCache formCache; private final FormCache formCache;
@ -1354,6 +1354,12 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
return false; return false;
} }
public void requestOffhandSwap() {
ServerboundPlayerActionPacket swapHandsPacket = new ServerboundPlayerActionPacket(PlayerAction.SWAP_HANDS, Vector3i.ZERO,
Direction.DOWN, 0);
sendDownstreamPacket(swapHandsPacket);
}
/** /**
* Will be overwritten for GeyserConnect. * Will be overwritten for GeyserConnect.
*/ */

View File

@ -73,8 +73,7 @@ public final class LodestoneCache {
} }
} }
for (Int2ObjectMap.Entry<LodestonePos> entry : this.lodestones.int2ObjectEntrySet()) { for (LodestonePos pos : this.lodestones.values()) {
LodestonePos pos = entry.getValue();
if (pos.equals(x, y, z, dim)) { if (pos.equals(x, y, z, dim)) {
// Use this existing position instead // Use this existing position instead
this.activeLodestones.put(itemStack, pos); this.activeLodestones.put(itemStack, pos);

View File

@ -43,6 +43,7 @@ import org.geysermc.geyser.inventory.updater.UIInventoryUpdater;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
public class EnchantingInventoryTranslator extends AbstractBlockInventoryTranslator { public class EnchantingInventoryTranslator extends AbstractBlockInventoryTranslator {
public EnchantingInventoryTranslator() { public EnchantingInventoryTranslator() {
@ -71,7 +72,7 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
// The Bedrock index might need changed, so let's look it up and see. // The Bedrock index might need changed, so let's look it up and see.
int bedrockIndex = value; int bedrockIndex = value;
if (bedrockIndex != -1) { if (bedrockIndex != -1) {
Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + Enchantment.JavaEnchantment.of(bedrockIndex).name().toLowerCase()); Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + Enchantment.JavaEnchantment.of(bedrockIndex).name().toLowerCase(Locale.ROOT));
if (enchantment != null) { if (enchantment != null) {
// Convert the Java enchantment index to Bedrock's // Convert the Java enchantment index to Bedrock's
bedrockIndex = enchantment.ordinal(); bedrockIndex = enchantment.ordinal();

View File

@ -25,11 +25,8 @@
package org.geysermc.geyser.translator.protocol.bedrock.entity.player; package org.geysermc.geyser.translator.protocol.bedrock.entity.player;
import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.EmotePacket; import com.nukkitx.protocol.bedrock.packet.EmotePacket;
import org.geysermc.geyser.api.event.bedrock.ClientEmoteEvent;
import org.geysermc.geyser.configuration.EmoteOffhandWorkaroundOption; import org.geysermc.geyser.configuration.EmoteOffhandWorkaroundOption;
import org.geysermc.geyser.entity.type.Entity; import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
@ -43,15 +40,20 @@ public class BedrockEmoteTranslator extends PacketTranslator<EmotePacket> {
public void translate(GeyserSession session, EmotePacket packet) { public void translate(GeyserSession session, EmotePacket packet) {
if (session.getGeyser().getConfig().getEmoteOffhandWorkaround() != EmoteOffhandWorkaroundOption.DISABLED) { if (session.getGeyser().getConfig().getEmoteOffhandWorkaround() != EmoteOffhandWorkaroundOption.DISABLED) {
// Activate the workaround - we should trigger the offhand now // Activate the workaround - we should trigger the offhand now
ServerboundPlayerActionPacket swapHandsPacket = new ServerboundPlayerActionPacket(PlayerAction.SWAP_HANDS, Vector3i.ZERO, session.requestOffhandSwap();
Direction.DOWN, 0);
session.sendDownstreamPacket(swapHandsPacket);
if (session.getGeyser().getConfig().getEmoteOffhandWorkaround() == EmoteOffhandWorkaroundOption.NO_EMOTES) { if (session.getGeyser().getConfig().getEmoteOffhandWorkaround() == EmoteOffhandWorkaroundOption.NO_EMOTES) {
return; return;
} }
} }
// For the future: could have a method that exposes which players will see the emote
ClientEmoteEvent event = new ClientEmoteEvent(session, packet.getEmoteId());
session.getGeyser().eventBus().fire(event);
if (event.isCancelled()) {
return;
}
int javaId = session.getPlayerEntity().getEntityId(); int javaId = session.getPlayerEntity().getEntityId();
for (GeyserSession otherSession : session.getGeyser().getSessionManager().getSessions().values()) { for (GeyserSession otherSession : session.getGeyser().getSessionManager().getSessions().values()) {
if (otherSession != session) { if (otherSession != session) {