Add more packets to test

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-17 03:46:04 +01:00
parent 705a1e3e18
commit 987c563255
2 changed files with 61 additions and 9 deletions

View File

@ -15,6 +15,8 @@ public record EntityEquipmentPacket(int entityId,
@NotNull Map<EquipmentSlot, ItemStack> equipments) implements ServerPacket {
public EntityEquipmentPacket {
equipments = Map.copyOf(equipments);
if (equipments.isEmpty())
throw new IllegalArgumentException("Equipments cannot be empty");
}
public EntityEquipmentPacket(BinaryReader reader) {
@ -41,11 +43,11 @@ public record EntityEquipmentPacket(int entityId,
private static Map<EquipmentSlot, ItemStack> readEquipments(BinaryReader reader) {
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());
}
byte slot;
do {
slot = reader.readByte();
equipments.put(EquipmentSlot.values()[slot & 0x7F], reader.readItemStack());
} while ((slot & 0x80) == 0x80);
return equipments;
}
}

View File

@ -1,25 +1,34 @@
package network;
import com.google.gson.JsonObject;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.message.ChatPosition;
import net.minestom.server.network.packet.client.ClientPacket;
import net.minestom.server.network.packet.client.handshake.HandshakePacket;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.handshake.ResponsePacket;
import net.minestom.server.network.packet.server.login.LoginDisconnectPacket;
import net.minestom.server.network.packet.server.login.LoginSuccessPacket;
import net.minestom.server.network.packet.server.login.SetCompressionPacket;
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import net.minestom.server.network.packet.server.play.*;
import net.minestom.server.network.packet.server.status.PongPacket;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.Writeable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@ -33,16 +42,57 @@ public class PacketWriteReadTest {
private static final List<ServerPacket> SERVER_PACKETS = new ArrayList<>();
private static final List<ClientPacket> CLIENT_PACKETS = new ArrayList<>();
private static final Component COMPONENT = Component.text("Hey");
private static final Vec VEC = new Vec(5, 5, 5);
@BeforeAll
public static void setupServer() {
// Handshake
SERVER_PACKETS.add(new ResponsePacket(new JsonObject().toString()));
// Status
SERVER_PACKETS.add(new PongPacket(5));
// Login
//SERVER_PACKETS.add(new EncryptionRequestPacket("server", generateByteArray(16), generateByteArray(16)));
SERVER_PACKETS.add(new LoginDisconnectPacket(Component.text("Hey")));
SERVER_PACKETS.add(new LoginDisconnectPacket(COMPONENT));
//SERVER_PACKETS.add(new LoginPluginRequestPacket(5, "id", generateByteArray(16)));
SERVER_PACKETS.add(new LoginSuccessPacket(UUID.randomUUID(), "TheMode911"));
SERVER_PACKETS.add(new SetCompressionPacket(256));
SERVER_PACKETS.add(new ChatMessagePacket(Component.text("Hey"), ChatPosition.CHAT, UUID.randomUUID()));
// Play
SERVER_PACKETS.add(new AcknowledgePlayerDiggingPacket(VEC, 5, ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true));
SERVER_PACKETS.add(new ActionBarPacket(COMPONENT));
SERVER_PACKETS.add(new AttachEntityPacket(5, 10));
SERVER_PACKETS.add(new BlockActionPacket(VEC, (byte) 5, (byte) 5, 5));
SERVER_PACKETS.add(new BlockBreakAnimationPacket(5, VEC, (byte) 5));
SERVER_PACKETS.add(new BlockChangePacket(VEC, 0));
SERVER_PACKETS.add(new BlockEntityDataPacket(VEC, 5, NBT.Compound(Map.of("key", NBT.String("value")))));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.AddAction(COMPONENT, 5f, BossBar.Color.BLUE, BossBar.Overlay.PROGRESS, (byte) 2)));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.RemoveAction()));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.UpdateHealthAction(5f)));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.UpdateTitleAction(COMPONENT)));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.UpdateStyleAction(BossBar.Color.BLUE, BossBar.Overlay.PROGRESS)));
SERVER_PACKETS.add(new BossBarPacket(UUID.randomUUID(), new BossBarPacket.UpdateFlagsAction((byte) 5)));
SERVER_PACKETS.add(new CameraPacket(5));
SERVER_PACKETS.add(new ChangeGameStatePacket(ChangeGameStatePacket.Reason.RAIN_LEVEL_CHANGE, 2));
SERVER_PACKETS.add(new ChatMessagePacket(COMPONENT, ChatPosition.CHAT, UUID.randomUUID()));
SERVER_PACKETS.add(new ClearTitlesPacket(false));
SERVER_PACKETS.add(new CloseWindowPacket((byte) 2));
SERVER_PACKETS.add(new CollectItemPacket(5, 5, 5));
SERVER_PACKETS.add(new CraftRecipeResponse((byte) 2, "recipe"));
SERVER_PACKETS.add(new DeathCombatEventPacket(5, 5, COMPONENT));
SERVER_PACKETS.add(new DestroyEntitiesPacket(List.of(5, 5, 5)));
SERVER_PACKETS.add(new DisconnectPacket(COMPONENT));
SERVER_PACKETS.add(new DisplayScoreboardPacket((byte) 5, "scoreboard"));
SERVER_PACKETS.add(new EffectPacket(5, VEC, 5, false));
SERVER_PACKETS.add(new EndCombatEventPacket(5, 5));
SERVER_PACKETS.add(new EnterCombatEventPacket());
SERVER_PACKETS.add(new EntityAnimationPacket(5, EntityAnimationPacket.Animation.TAKE_DAMAGE));
SERVER_PACKETS.add(new EntityEquipmentPacket(6, Map.of(EquipmentSlot.MAIN_HAND, ItemStack.of(Material.DIAMOND_SWORD))));
SERVER_PACKETS.add(new EntityHeadLookPacket(5, 90f));
SERVER_PACKETS.add(new EntityMetaDataPacket(5, List.of()));
SERVER_PACKETS.add(new EntityPositionAndRotationPacket(5, (short) 0, (short) 0, (short) 0, 45f, 45f, false));
SERVER_PACKETS.add(new EntityPositionPacket(5, (short) 0, (short) 0, (short) 0, true));
SERVER_PACKETS.add(new EntityPropertiesPacket(5, List.of()));
SERVER_PACKETS.add(new EntityRotationPacket(5, 45f, 45f, false));
}
@BeforeAll
@ -69,7 +119,7 @@ public class PacketWriteReadTest {
BinaryReader reader = new BinaryReader(writer.toByteArray());
var createdPacket = readerConstructor.newInstance(reader);
assertEquals(writeable, createdPacket);
} catch (Exception e) {
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
fail(writeable.toString(), e);
}
}