mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 09:27:58 +01:00
add converter from adventure enums to packet values
This commit is contained in:
parent
c25dbaa936
commit
94b737812d
@ -0,0 +1,48 @@
|
||||
package net.minestom.server.adventure;
|
||||
|
||||
import net.kyori.adventure.audience.MessageType;
|
||||
import net.kyori.adventure.bossbar.BossBar;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Utility methods to convert adventure enums to their packet values.
|
||||
*/
|
||||
public class AdventurePacketConvertor {
|
||||
|
||||
public static int getBossBarOverlayValue(@NotNull BossBar.Overlay overlay) {
|
||||
return overlay.ordinal();
|
||||
}
|
||||
|
||||
public static byte getBossBarFlagValue(@NotNull Collection<BossBar.Flag> flags) {
|
||||
byte val = 0x0;
|
||||
for (BossBar.Flag flag : flags) {
|
||||
val |= flag.ordinal();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public static int getBossBarColorValue(@NotNull BossBar.Color color) {
|
||||
return color.ordinal();
|
||||
}
|
||||
|
||||
public static int getSoundCategoryValue(@NotNull Sound.Source source) {
|
||||
return source.ordinal();
|
||||
}
|
||||
|
||||
public static byte getMessageTypeValue(@NotNull MessageType messageType) {
|
||||
switch (messageType) {
|
||||
case CHAT: return 0x00;
|
||||
case SYSTEM: return 0x01;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Cannot get message type");
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package net.minestom.server.adventure;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.minestom.server.sound.Sound;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Adventure related utilities.
|
||||
*/
|
||||
public class AdventureUtils {
|
||||
|
||||
}
|
@ -166,10 +166,10 @@ public class BossBarManager implements BossBar.Listener {
|
||||
@NotNull BossBarPacket createAddPacket() {
|
||||
return this.createGenericPacket(ADD, packet -> {
|
||||
packet.title = MinecraftServer.getSerializationManager().serialize(bar.name());
|
||||
packet.color = bar.color().ordinal();
|
||||
packet.division = bar.overlay().ordinal();
|
||||
packet.color = AdventurePacketConvertor.getBossBarColorValue(bar.color());
|
||||
packet.division = AdventurePacketConvertor.getBossBarOverlayValue(bar.overlay());
|
||||
packet.health = bar.progress();
|
||||
packet.flags = serializeFlags(bar.flags());
|
||||
packet.flags = AdventurePacketConvertor.getBossBarFlagValue(bar.flags());
|
||||
});
|
||||
}
|
||||
|
||||
@ -180,7 +180,7 @@ public class BossBarManager implements BossBar.Listener {
|
||||
@NotNull BossBarPacket createColorUpdate(@NotNull Color color) {
|
||||
return this.createGenericPacket(UPDATE_STYLE, packet -> {
|
||||
packet.color = color.ordinal();
|
||||
packet.division = bar.overlay().ordinal();
|
||||
packet.division = AdventurePacketConvertor.getBossBarOverlayValue(bar.overlay());
|
||||
});
|
||||
}
|
||||
|
||||
@ -193,13 +193,13 @@ public class BossBarManager implements BossBar.Listener {
|
||||
}
|
||||
|
||||
@NotNull BossBarPacket createFlagsUpdate(@NotNull Set<Flag> newFlags) {
|
||||
return this.createGenericPacket(UPDATE_FLAGS, packet -> packet.flags = serializeFlags(bar.flags()));
|
||||
return this.createGenericPacket(UPDATE_FLAGS, packet -> packet.flags = AdventurePacketConvertor.getBossBarFlagValue(bar.flags()));
|
||||
}
|
||||
|
||||
@NotNull BossBarPacket createOverlayUpdate(@NotNull Overlay overlay) {
|
||||
return this.createGenericPacket(UPDATE_STYLE, packet -> {
|
||||
packet.division = overlay.ordinal();
|
||||
packet.color = bar.color().ordinal();
|
||||
packet.division = AdventurePacketConvertor.getBossBarOverlayValue(overlay);
|
||||
packet.color = AdventurePacketConvertor.getBossBarColorValue(bar.color());
|
||||
});
|
||||
}
|
||||
|
||||
@ -210,13 +210,5 @@ public class BossBarManager implements BossBar.Listener {
|
||||
consumer.accept(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
private static byte serializeFlags(@NotNull Set<Flag> flags) {
|
||||
byte val = 0x0;
|
||||
for (Flag flag : flags) {
|
||||
val |= flag.ordinal();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.minestom.server.network.packet.server.play;
|
||||
|
||||
import net.kyori.adventure.sound.Sound.Source;
|
||||
import net.minestom.server.adventure.AdventurePacketConvertor;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
||||
import net.minestom.server.sound.Sound;
|
||||
@ -43,7 +44,7 @@ public class SoundEffectPacket implements ServerPacket {
|
||||
public static SoundEffectPacket create(Source category, Sound sound, Position position, float volume, float pitch) {
|
||||
SoundEffectPacket packet = new SoundEffectPacket();
|
||||
packet.soundId = sound.getId();
|
||||
packet.soundCategory = category.ordinal();
|
||||
packet.soundCategory = AdventurePacketConvertor.getSoundCategoryValue(category);
|
||||
// *8 converts to fixed-point representation with 3 bits for fractional part
|
||||
packet.x = (int) position.getX();
|
||||
packet.y = (int) position.getY();
|
||||
|
Loading…
Reference in New Issue
Block a user