Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-01 20:03:53 +01:00
parent 2d7e11adaf
commit 8c72bee1dd
13 changed files with 54 additions and 81 deletions

View File

@ -7,7 +7,8 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record DeathCombatEventPacket(int playerId, int entityId, Component message) implements ServerPacket {
public record DeathCombatEventPacket(int playerId, int entityId,
@NotNull Component message) implements ServerPacket {
public DeathCombatEventPacket(BinaryReader reader) {
this(reader.readVarInt(), reader.readInt(), reader.readComponent());
}

View File

@ -12,7 +12,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.function.UnaryOperator;
public record DisconnectPacket(Component message) implements ComponentHoldingServerPacket {
public record DisconnectPacket(@NotNull Component message) implements ComponentHoldingServerPacket {
public DisconnectPacket(BinaryReader reader) {
this(reader.readComponent());
}

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record EntityEffectPacket(int entityId, Potion potion) implements ServerPacket {
public record EntityEffectPacket(int entityId, @NotNull Potion potion) implements ServerPacket {
public EntityEffectPacket(BinaryReader reader) {
this(reader.readVarInt(), new Potion(reader));
}

View File

@ -11,7 +11,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.EnumMap;
import java.util.Map;
public record EntityEquipmentPacket(int entityId, Map<EquipmentSlot, ItemStack> equipments) implements ServerPacket {
public record EntityEquipmentPacket(int entityId,
@NotNull Map<EquipmentSlot, ItemStack> equipments) implements ServerPacket {
public EntityEquipmentPacket {
equipments = Map.copyOf(equipments);
}
@ -39,11 +40,10 @@ public record EntityEquipmentPacket(int entityId, Map<EquipmentSlot, ItemStack>
}
private static Map<EquipmentSlot, ItemStack> readEquipments(BinaryReader reader) {
EnumMap<EquipmentSlot, ItemStack> equipments = new EnumMap<>(EquipmentSlot.class);
boolean hasRemaining = true;
while (hasRemaining) {
byte slotEnum = reader.readByte();
hasRemaining = (slotEnum & 0x80) == 0x80;
Map<EquipmentSlot, ItemStack> equipments = new EnumMap<>(EquipmentSlot.class);
while (true) {
final byte slotEnum = reader.readByte();
if ((slotEnum & 0x80) != 0x80) break;
equipments.put(EquipmentSlot.values()[slotEnum & 0x7F], reader.readItemStack());
}
return equipments;

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record OpenBookPacket(Player.Hand hand) implements ServerPacket {
public record OpenBookPacket(@NotNull Player.Hand hand) implements ServerPacket {
public OpenBookPacket(BinaryReader reader) {
this(Player.Hand.values()[reader.readVarInt()]);
}

View File

@ -8,39 +8,30 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.UnaryOperator;
public record PlayerListHeaderAndFooterPacket(Component header,
Component footer) implements ComponentHoldingServerPacket {
public record PlayerListHeaderAndFooterPacket(@NotNull Component header,
@NotNull Component footer) implements ComponentHoldingServerPacket {
public PlayerListHeaderAndFooterPacket(BinaryReader reader) {
this(reader.readComponent(), reader.readComponent());
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeComponent(Objects.requireNonNullElseGet(header, Component::empty));
writer.writeComponent(Objects.requireNonNullElseGet(footer, Component::empty));
writer.writeComponent(header);
writer.writeComponent(footer);
}
@Override
public @NotNull Collection<Component> components() {
List<Component> components = new ArrayList<>();
if (header != null) {
components.add(header);
}
if (footer != null) {
components.add(footer);
}
return components;
return List.of(header, footer);
}
@Override
public @NotNull ServerPacket copyWithOperator(@NotNull UnaryOperator<Component> operator) {
return new PlayerListHeaderAndFooterPacket(header == null ? null : operator.apply(header), footer == null ? null : operator.apply(footer));
return new PlayerListHeaderAndFooterPacket(operator.apply(header), operator.apply(footer));
}
@Override

View File

@ -7,9 +7,11 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record RemoveEntityEffectPacket(int entityId, PotionEffect potionEffect) implements ServerPacket {
import java.util.Objects;
public record RemoveEntityEffectPacket(int entityId, @NotNull PotionEffect potionEffect) implements ServerPacket {
public RemoveEntityEffectPacket(BinaryReader reader) {
this(reader.readVarInt(), PotionEffect.fromId(reader.readByte()));
this(reader.readVarInt(), Objects.requireNonNull(PotionEffect.fromId(reader.readByte())));
}
@Override

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.world.Difficulty;
import org.jetbrains.annotations.NotNull;
public record ServerDifficultyPacket(Difficulty difficulty, boolean locked) implements ServerPacket {
public record ServerDifficultyPacket(@NotNull Difficulty difficulty, boolean locked) implements ServerPacket {
public ServerDifficultyPacket(BinaryReader reader) {
this(Difficulty.values()[reader.readByte()], reader.readBoolean());
}

View File

@ -7,9 +7,11 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record SetSlotPacket(byte windowId, int stateId, short slot, ItemStack itemStack) implements ServerPacket {
public record SetSlotPacket(byte windowId, int stateId, short slot,
@NotNull ItemStack itemStack) implements ServerPacket {
public SetSlotPacket(BinaryReader reader) {
this(reader.readByte(), reader.readVarInt(), reader.readShort(), reader.readItemStack());
this(reader.readByte(), reader.readVarInt(), reader.readShort(),
reader.readItemStack());
}
@Override

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record SetTitleSubTitlePacket(Component subtitle) implements ServerPacket {
public record SetTitleSubTitlePacket(@NotNull Component subtitle) implements ServerPacket {
public SetTitleSubTitlePacket(BinaryReader reader) {
this(reader.readComponent());
}

View File

@ -7,7 +7,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record SetTitleTextPacket(Component title) implements ServerPacket {
public record SetTitleTextPacket(@NotNull Component title) implements ServerPacket {
public SetTitleTextPacket(BinaryReader reader) {
this(reader.readComponent());
}

View File

@ -10,7 +10,8 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record SoundEffectPacket(int soundId, Source source, int x, int y, int z,
public record SoundEffectPacket(int soundId, @NotNull Source source,
int x, int y, int z,
float volume, float pitch) implements ServerPacket {
public SoundEffectPacket(BinaryReader reader) {
this(reader.readVarInt(), Source.values()[reader.readVarInt()],
@ -18,8 +19,10 @@ public record SoundEffectPacket(int soundId, Source source, int x, int y, int z,
reader.readFloat(), reader.readFloat());
}
public SoundEffectPacket(SoundEvent sound, Source source, Point position, float volume, float pitch) {
this(sound.id(), source, (int) position.x(), (int) position.y(), (int) position.z(), volume, pitch);
public SoundEffectPacket(@NotNull SoundEvent sound, @NotNull Source source,
@NotNull Point position, float volume, float pitch) {
this(sound.id(), source,
(int) position.x(), (int) position.y(), (int) position.z(), volume, pitch);
}
@Override

View File

@ -7,66 +7,40 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
public final class StopSoundPacket implements ServerPacket {
private final byte flags;
private final Sound.Source source;
private final String sound;
public StopSoundPacket(byte flags, Sound.Source source, String sound) {
this.flags = flags;
this.source = source;
this.sound = sound;
public record StopSoundPacket(byte flags, @Nullable Sound.Source source,
@Nullable String sound) implements ServerPacket {
public StopSoundPacket(BinaryReader reader) {
this(read(reader));
}
public StopSoundPacket(BinaryReader reader) {
this.flags = reader.readByte();
if (flags == 3 || flags == 1) {
this.source = Sound.Source.values()[reader.readVarInt()];
} else this.source = null;
if (flags == 2 || flags == 3) {
this.sound = reader.readSizedString();
} else this.sound = null;
private StopSoundPacket(StopSoundPacket packet) {
this(packet.flags, packet.source, packet.sound);
}
private static StopSoundPacket read(BinaryReader reader) {
var flags = reader.readByte();
var source = flags == 3 || flags == 1 ? Sound.Source.values()[reader.readVarInt()] : null;
var sound = flags == 2 || flags == 3 ? reader.readSizedString() : null;
return new StopSoundPacket(flags, source, sound);
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeByte(flags);
if (flags == 3 || flags == 1)
if (flags == 3 || flags == 1) {
assert source != null;
writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(source));
if (flags == 2 || flags == 3)
}
if (flags == 2 || flags == 3) {
assert sound != null;
writer.writeSizedString(sound);
}
public byte flags() {
return flags;
}
public Sound.Source source() {
return source;
}
public String sound() {
return sound;
}
}
@Override
public int getId() {
return ServerPacketIdentifier.STOP_SOUND;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StopSoundPacket that)) return false;
return flags == that.flags && source == that.source &&
Objects.equals(sound, that.sound);
}
@Override
public int hashCode() {
return Objects.hash(flags, source, sound);
}
}