Skip to content

Commit

Permalink
Add ClientboundSetEquipmentPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Sep 12, 2024
1 parent 0f314ca commit 2393b87
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ entities.
- Powerful factory classes for creating packets
- Wrapper classes for Minecraft packets and entities
- Support for multiple Minecraft versions
- No third-party dependencies and works out of the box

## Supported packets

Expand Down Expand Up @@ -49,8 +50,8 @@ supported:
- [x] ClientboundRemoveEntitiesPacket
- [x] ClientboundTeleportEntityPacket
- [x] ClientboundRotateHeadPacket
- [x] ClientboundSetEquipmentPacket
- [ ] ClientboundSetPlayerTeamPacket
- [ ] ClientboundSetEquipmentPacket
- [ ] ClientboundUpdateAttributesPacket
- [ ] ClientboundSetEntityDataPacket
- [ ] ClientboundAnimatePacket
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.oliver.fancysitula.api.entities;

import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FS_Player extends FS_Entity {

protected Map<FS_EquipmentSlot, ItemStack> equipment;

public FS_Player() {
super(EntityType.PLAYER);

this.equipment = new ConcurrentHashMap<>();
}

public Map<FS_EquipmentSlot, ItemStack> getEquipment() {
return equipment;
}

public void setEquipment(Map<FS_EquipmentSlot, ItemStack> equipment) {
this.equipment = equipment;
}

public void setEquipment(FS_EquipmentSlot slot, ItemStack item) {
this.equipment.put(slot, item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.oliver.fancysitula.api.packets;

import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

public abstract class FS_ClientboundSetEquipmentPacket implements FS_ClientboundPacket {

protected int entityId;
protected Map<FS_EquipmentSlot, ItemStack> equipment;

public FS_ClientboundSetEquipmentPacket(int entityId, Map<FS_EquipmentSlot, ItemStack> equipment) {
this.entityId = entityId;
this.equipment = equipment;
}

public int getEntityId() {
return entityId;
}

public void setEntityId(int entityId) {
this.entityId = entityId;
}

public Map<FS_EquipmentSlot, ItemStack> getEquipment() {
return equipment;
}

public void setEquipment(Map<FS_EquipmentSlot, ItemStack> equipment) {
this.equipment = equipment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.oliver.fancysitula.api.utils;

public enum FS_EquipmentSlot {
MAINHAND,
OFFHAND,
FEET,
LEGS,
CHEST,
HEAD,
BODY,
;

public static FS_EquipmentSlot fromBukkit(org.bukkit.inventory.EquipmentSlot equipmentSlot) {
return switch (equipmentSlot) {
case HAND -> MAINHAND;
case OFF_HAND -> OFFHAND;
case FEET -> FEET;
case LEGS -> LEGS;
case CHEST -> CHEST;
case HEAD -> HEAD;
case BODY -> BODY;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.oliver.fancysitula.factories;

import de.oliver.fancysitula.api.entities.FS_Entity;
import de.oliver.fancysitula.api.entities.FS_Player;
import de.oliver.fancysitula.api.entities.FS_RealPlayer;

import java.util.List;
Expand Down Expand Up @@ -45,4 +46,11 @@ public void setEntityDataFor(FS_RealPlayer player, FS_Entity entity) {
).send(player);
}

public void setEntityEquipmentFor(FS_RealPlayer player, FS_Player entity) {
FancySitula.PACKET_FACTORY.createSetEquipmentPacket(
entity.getId(),
entity.getEquipment()
).send(player);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package de.oliver.fancysitula.factories;

import de.oliver.fancysitula.api.packets.*;
import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import de.oliver.fancysitula.api.utils.ServerVersion;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
Expand Down Expand Up @@ -262,4 +265,29 @@ public FS_ClientboundSetEntityDataPacket createSetEntityDataPacket(
public FS_ClientboundSetEntityDataPacket createSetEntityDataPacket(int entityId, List<FS_ClientboundSetEntityDataPacket.EntityData> entityData) {
return createSetEntityDataPacket(ServerVersion.getCurrentVersion(), entityId, entityData);
}

/**
* Creates a new FS_ClientboundSetEquipmentPacket instance based on the server version
*
* @param entityId ID of the entity to set the equipment of
* @param equipment Map of {@link org.bukkit.inventory.EquipmentSlot} and {@link org.bukkit.inventory.ItemStack} to set
*/
public FS_ClientboundSetEquipmentPacket createSetEquipmentPacket(ServerVersion serverVersion, int entityId, Map<FS_EquipmentSlot, ItemStack> equipment) {
switch (serverVersion) {
case v1_20_5, v1_20_6, v1_21, v1_21_1 -> {
return new de.oliver.fancysitula.versions.v1_20_6.packets.ClientboundSetEquipmentPacketImpl(entityId, equipment);
}
default -> throw new IllegalArgumentException("Unsupported server version: " + serverVersion.getVersion());
}
}

/**
* Creates a new FS_ClientboundSetEquipmentPacket instance based on the current server version
*
* @param entityId ID of the entity to set the equipment of
* @param equipment Map of {@link org.bukkit.inventory.EquipmentSlot} and {@link org.bukkit.inventory.ItemStack} to set
*/
public FS_ClientboundSetEquipmentPacket createSetEquipmentPacket(int entityId, Map<FS_EquipmentSlot, ItemStack> equipment) {
return createSetEquipmentPacket(ServerVersion.getCurrentVersion(), entityId, equipment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.oliver.fancysitula.versions.v1_20_6.packets;

import com.mojang.datafixers.util.Pair;
import de.oliver.fancysitula.api.entities.FS_RealPlayer;
import de.oliver.fancysitula.api.packets.FS_ClientboundSetEquipmentPacket;
import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import de.oliver.fancysitula.versions.v1_20_6.utils.VanillaPlayerAdapter;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EquipmentSlot;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClientboundSetEquipmentPacketImpl extends FS_ClientboundSetEquipmentPacket {

public ClientboundSetEquipmentPacketImpl(int entityId, Map<FS_EquipmentSlot, ItemStack> equipment) {
super(entityId, equipment);
}

@Override
public Object createPacket() {
List<Pair<net.minecraft.world.entity.EquipmentSlot, net.minecraft.world.item.ItemStack>> slots = new ArrayList<>();

for (Map.Entry<FS_EquipmentSlot, ItemStack> entry : equipment.entrySet()) {
EquipmentSlot equipmentSlot = net.minecraft.world.entity.EquipmentSlot.byName(entry.getKey().name().toLowerCase());
net.minecraft.world.item.ItemStack itemStack = CraftItemStack.asNMSCopy(entry.getValue());

slots.add(Pair.of(equipmentSlot, itemStack));
}

return new ClientboundSetEquipmentPacket(entityId, slots);
}

@Override
public void send(FS_RealPlayer player) {
ClientboundSetEquipmentPacket packet = (ClientboundSetEquipmentPacket) createPacket();

ServerPlayer vanillaPlayer = VanillaPlayerAdapter.asVanilla(player.getBukkitPlayer());
vanillaPlayer.connection.send(packet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.oliver.fancysitula.versions.v1_20_6.packets;

import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

class ClientboundSetEquipmentPacketImplTest {

//TODO: Fix this test (registry problems)
// @Test
void createPacket() {
// Setup packet
Map<FS_EquipmentSlot, ItemStack> equipment = Map.of(
FS_EquipmentSlot.MAINHAND, new ItemStack(Material.DIAMOND_SWORD),
FS_EquipmentSlot.OFFHAND, new ItemStack(Material.SHIELD),
FS_EquipmentSlot.HEAD, new ItemStack(Material.DIAMOND_HELMET)
);

ClientboundSetEquipmentPacketImpl packet = new ClientboundSetEquipmentPacketImpl(42, equipment);
ClientboundSetEquipmentPacket createdPacket = (ClientboundSetEquipmentPacket) packet.createPacket();

assert createdPacket.getEntity() == 42;
assert createdPacket.getSlots().size() == 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package packets;

import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import de.oliver.fancysitula.versions.v1_20_6.packets.ClientboundSetEquipmentPacketImpl;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

class ClientboundSetEquipmentPacketImplTest {

//TODO: Fix this test (registry problems)
// @Test
void createPacket() {
// Setup packet
Map<FS_EquipmentSlot, ItemStack> equipment = Map.of(
FS_EquipmentSlot.MAINHAND, new ItemStack(Material.DIAMOND_SWORD),
FS_EquipmentSlot.OFFHAND, new ItemStack(Material.SHIELD),
FS_EquipmentSlot.HEAD, new ItemStack(Material.DIAMOND_HELMET)
);

ClientboundSetEquipmentPacketImpl packet = new ClientboundSetEquipmentPacketImpl(42, equipment);
ClientboundSetEquipmentPacket createdPacket = (ClientboundSetEquipmentPacket) packet.createPacket();

assert createdPacket.getEntity() == 42;
assert createdPacket.getSlots().size() == 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package packets;

import de.oliver.fancysitula.api.utils.FS_EquipmentSlot;
import de.oliver.fancysitula.versions.v1_20_6.packets.ClientboundSetEquipmentPacketImpl;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

class ClientboundSetEquipmentPacketImplTest {

//TODO: Fix this test (registry problems)
// @Test
void createPacket() {
// Setup packet
Map<FS_EquipmentSlot, ItemStack> equipment = Map.of(
FS_EquipmentSlot.MAINHAND, new ItemStack(Material.DIAMOND_SWORD),
FS_EquipmentSlot.OFFHAND, new ItemStack(Material.SHIELD),
FS_EquipmentSlot.HEAD, new ItemStack(Material.DIAMOND_HELMET)
);

ClientboundSetEquipmentPacketImpl packet = new ClientboundSetEquipmentPacketImpl(42, equipment);
ClientboundSetEquipmentPacket createdPacket = (ClientboundSetEquipmentPacket) packet.createPacket();

assert createdPacket.getEntity() == 42;
assert createdPacket.getSlots().size() == 3;
}
}

0 comments on commit 2393b87

Please sign in to comment.