Skip to content

Commit 2aa0edf

Browse files
committed
entity packet helper methods/ctor
1 parent 955ccac commit 2aa0edf

6 files changed

Lines changed: 107 additions & 23 deletions

File tree

src/main/java/com/comphenix/protocol/injector/EquivalentConstructor.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class EquivalentConstructor {
1313
private final PacketType packetType;
14-
private final List<Tuple<Class<?>, EquivalentConverter<?>>> converters = new ArrayList<>();
14+
private final List<Tuple<Class<?>, Object>> converters = new ArrayList<>();
1515

1616
private ConstructorAccessor constructorAccessor;
1717

@@ -24,6 +24,11 @@ public EquivalentConstructor withParam(Class<?> param, EquivalentConverter<?> co
2424
return this;
2525
}
2626

27+
public EquivalentConstructor withParam(Class<?> param, PacketConstructor.Unwrapper unwrapper) {
28+
converters.add(new Tuple<>(param, unwrapper));
29+
return this;
30+
}
31+
2732
public EquivalentConstructor withParam(Class<?> param) {
2833
converters.add(new Tuple<>(param, null));
2934
return this;
@@ -42,9 +47,15 @@ public Object create(Object... args) {
4247
Object[] convertedArgs = new Object[args.length];
4348

4449
int i = 0;
45-
for (Tuple<Class<?>, EquivalentConverter<?>> entry : converters) {
46-
EquivalentConverter converter = (EquivalentConverter) entry.second();
47-
convertedArgs[i] = converter != null ? converter.getGeneric(args[i]) : args[i];
50+
for (Tuple<Class<?>, Object> entry : converters) {
51+
Object rawConverter = entry.second();
52+
switch (rawConverter) {
53+
case EquivalentConverter converter -> convertedArgs[i] = converter.getGeneric(args[i]);
54+
case PacketConstructor.Unwrapper unwrapper -> convertedArgs[i] = unwrapper.unwrapItem(args[i]);
55+
case null -> convertedArgs[i] = args[i];
56+
default -> throw new IllegalStateException("Invalid converter type: " + rawConverter.getClass());
57+
}
58+
4859
i++;
4960
}
5061

src/main/java/net/dmulloy2/protocol/wrappers/game/clientbound/WrappedClientboundAnimatePacket.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
import com.comphenix.protocol.PacketType;
44
import com.comphenix.protocol.events.PacketContainer;
5+
import com.comphenix.protocol.injector.BukkitUnwrapper;
6+
import com.comphenix.protocol.injector.EquivalentConstructor;
7+
import com.comphenix.protocol.injector.PacketConstructor;
8+
import com.comphenix.protocol.utility.MinecraftReflection;
59
import net.dmulloy2.protocol.AbstractPacket;
10+
import net.minecraft.network.protocol.game.ClientboundAnimatePacket;
11+
import org.bukkit.World;
12+
import org.bukkit.entity.Entity;
613

714
/**
815
* Wrapper for {@code ClientboundAnimatePacket} (Play phase, clientbound).
@@ -26,6 +33,10 @@ public class WrappedClientboundAnimatePacket extends AbstractPacket {
2633

2734
public static final PacketType TYPE = PacketType.Play.Server.ANIMATION;
2835

36+
private static final EquivalentConstructor CONSTRUCTOR = new EquivalentConstructor(TYPE)
37+
.withParam(MinecraftReflection.getEntityClass(), BukkitUnwrapper.getInstance())
38+
.withParam(int.class);
39+
2940
public WrappedClientboundAnimatePacket() {
3041
super(new PacketContainer(TYPE), TYPE);
3142
}
@@ -36,10 +47,22 @@ public WrappedClientboundAnimatePacket(int entityId, int animationId) {
3647
setAnimationId(animationId);
3748
}
3849

50+
public WrappedClientboundAnimatePacket(Entity entity, int animationId) {
51+
this(new PacketContainer(TYPE, CONSTRUCTOR.create(entity, animationId)));
52+
}
53+
3954
public WrappedClientboundAnimatePacket(PacketContainer packet) {
4055
super(packet, TYPE);
4156
}
4257

58+
public Entity getEntity(World world) {
59+
return handle.getEntityModifier(world).readSafely(0);
60+
}
61+
62+
public void setEntity(Entity entity) {
63+
handle.getEntityModifier(entity.getWorld()).writeSafely(0, entity);
64+
}
65+
4366
public int getEntityId() {
4467
return handle.getIntegers().read(0);
4568
}

src/main/java/net/dmulloy2/protocol/wrappers/game/clientbound/WrappedClientboundEntityEventPacket.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.comphenix.protocol.PacketType;
44
import com.comphenix.protocol.events.PacketContainer;
5+
import com.comphenix.protocol.injector.BukkitUnwrapper;
6+
import com.comphenix.protocol.injector.EquivalentConstructor;
7+
import com.comphenix.protocol.utility.MinecraftReflection;
58
import net.dmulloy2.protocol.AbstractPacket;
69
import org.bukkit.World;
710
import org.bukkit.entity.Entity;
@@ -22,6 +25,10 @@ public class WrappedClientboundEntityEventPacket extends AbstractPacket {
2225

2326
public static final PacketType TYPE = PacketType.Play.Server.ENTITY_STATUS;
2427

28+
private static final EquivalentConstructor CONSTRUCTOR = new EquivalentConstructor(TYPE)
29+
.withParam(MinecraftReflection.getEntityClass(), BukkitUnwrapper.getInstance())
30+
.withParam(byte.class);
31+
2532
public WrappedClientboundEntityEventPacket() {
2633
super(new PacketContainer(TYPE), TYPE);
2734
}
@@ -32,27 +39,35 @@ public WrappedClientboundEntityEventPacket(int entityId, byte status) {
3239
setStatus(status);
3340
}
3441

42+
public WrappedClientboundEntityEventPacket(Entity entity, byte status) {
43+
this(new PacketContainer(TYPE, CONSTRUCTOR.create(entity, status)));
44+
}
45+
3546
public WrappedClientboundEntityEventPacket(PacketContainer packet) {
3647
super(packet, TYPE);
3748
}
3849

3950
public int getEntityId() {
40-
return handle.getIntegers().read(0);
51+
return handle.getIntegers().readSafely(0);
4152
}
4253

4354
public void setEntityId(int entityId) {
44-
handle.getIntegers().write(0, entityId);
55+
handle.getIntegers().writeSafely(0, entityId);
4556
}
4657

4758
public Entity getEntity(World world) {
48-
return handle.getEntityModifier(world).read(0);
59+
return handle.getEntityModifier(world).readSafely(0);
60+
}
61+
62+
public void setEntity(Entity entity) {
63+
handle.getEntityModifier(entity.getWorld()).writeSafely(0, entity);
4964
}
5065

5166
public byte getStatus() {
52-
return handle.getBytes().read(0);
67+
return handle.getBytes().readSafely(0);
5368
}
5469

5570
public void setStatus(byte status) {
56-
handle.getBytes().write(0, status);
71+
handle.getBytes().writeSafely(0, status);
5772
}
5873
}

src/main/java/net/dmulloy2/protocol/wrappers/game/clientbound/WrappedClientboundRotateHeadPacket.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.comphenix.protocol.PacketType;
44
import com.comphenix.protocol.events.PacketContainer;
5+
import com.comphenix.protocol.injector.BukkitUnwrapper;
6+
import com.comphenix.protocol.injector.EquivalentConstructor;
7+
import com.comphenix.protocol.utility.MinecraftReflection;
58
import net.dmulloy2.protocol.AbstractPacket;
69
import org.bukkit.World;
710
import org.bukkit.entity.Entity;
@@ -25,6 +28,10 @@ public class WrappedClientboundRotateHeadPacket extends AbstractPacket {
2528

2629
public static final PacketType TYPE = PacketType.Play.Server.ENTITY_HEAD_ROTATION;
2730

31+
private static final EquivalentConstructor CONSTRUCTOR = new EquivalentConstructor(TYPE)
32+
.withParam(MinecraftReflection.getEntityClass(), BukkitUnwrapper.getInstance())
33+
.withParam(byte.class);
34+
2835
public WrappedClientboundRotateHeadPacket() {
2936
super(new PacketContainer(TYPE), TYPE);
3037
}
@@ -35,28 +42,36 @@ public WrappedClientboundRotateHeadPacket(int entityId, byte yHeadRot) {
3542
setYHeadRot(yHeadRot);
3643
}
3744

45+
public WrappedClientboundRotateHeadPacket(Entity entity, byte yHeadRot) {
46+
this(new PacketContainer(TYPE, CONSTRUCTOR.create(entity, yHeadRot)));
47+
}
48+
3849
public WrappedClientboundRotateHeadPacket(PacketContainer packet) {
3950
super(packet, TYPE);
4051
}
4152

4253
public int getEntityId() {
43-
return handle.getIntegers().read(0);
54+
return handle.getIntegers().readSafely(0);
4455
}
4556

4657
public void setEntityId(int entityId) {
47-
handle.getIntegers().write(0, entityId);
58+
handle.getIntegers().writeSafely(0, entityId);
4859
}
4960

5061
public Entity getEntity(World world) {
51-
return handle.getEntityModifier(world).read(0);
62+
return handle.getEntityModifier(world).readSafely(0);
63+
}
64+
65+
public void setEntity(Entity entity) {
66+
handle.getEntityModifier(entity.getWorld()).writeSafely(0, entity);
5267
}
5368

5469
/** Returns the head yaw as a packed byte (256 units = 360°). */
5570
public byte getYHeadRot() {
56-
return handle.getBytes().read(0);
71+
return handle.getBytes().readSafely(0);
5772
}
5873

5974
public void setYHeadRot(byte yHeadRot) {
60-
handle.getBytes().write(0, yHeadRot);
75+
handle.getBytes().writeSafely(0, yHeadRot);
6176
}
6277
}

src/main/java/net/dmulloy2/protocol/wrappers/game/serverbound/WrappedServerboundPlayerCommandPacket.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
import com.comphenix.protocol.PacketType;
44
import com.comphenix.protocol.events.PacketContainer;
5+
import com.comphenix.protocol.injector.BukkitUnwrapper;
6+
import com.comphenix.protocol.injector.EquivalentConstructor;
7+
import com.comphenix.protocol.utility.MinecraftReflection;
58
import com.comphenix.protocol.wrappers.EnumWrappers;
69
import net.dmulloy2.protocol.AbstractPacket;
10+
import org.bukkit.World;
11+
import org.bukkit.entity.Entity;
712

813
/**
914
* Wrapper for {@code ServerboundPlayerCommandPacket} (Play phase, serverbound).
@@ -19,6 +24,11 @@ public class WrappedServerboundPlayerCommandPacket extends AbstractPacket {
1924

2025
public static final PacketType TYPE = PacketType.Play.Client.ENTITY_ACTION;
2126

27+
private static final EquivalentConstructor CONSTRUCTOR = new EquivalentConstructor(TYPE)
28+
.withParam(MinecraftReflection.getEntityClass(), BukkitUnwrapper.getInstance())
29+
.withParam(EnumWrappers.getPlayerActionClass(), EnumWrappers.getEntityActionConverter())
30+
.withParam(int.class);
31+
2232
public WrappedServerboundPlayerCommandPacket() {
2333
super(new PacketContainer(TYPE), TYPE);
2434
}
@@ -30,6 +40,10 @@ public WrappedServerboundPlayerCommandPacket(int entityId, EnumWrappers.PlayerAc
3040
setData(data);
3141
}
3242

43+
public WrappedServerboundPlayerCommandPacket(Entity entity, EnumWrappers.PlayerAction action, int data) {
44+
this(new PacketContainer(TYPE, CONSTRUCTOR.create(entity, action, data)));
45+
}
46+
3347
public WrappedServerboundPlayerCommandPacket(PacketContainer packet) {
3448
super(packet, TYPE);
3549
}
@@ -38,41 +52,49 @@ public WrappedServerboundPlayerCommandPacket(PacketContainer packet) {
3852
* Returns the entity ID of the player issuing the command.
3953
*/
4054
public int getEntityId() {
41-
return handle.getIntegers().read(0);
55+
return handle.getIntegers().readSafely(0);
4256
}
4357

4458
/**
4559
* Sets the entity ID of the player issuing the command.
4660
*/
4761
public void setEntityId(int entityId) {
48-
handle.getIntegers().write(0, entityId);
62+
handle.getIntegers().writeSafely(0, entityId);
63+
}
64+
65+
public Entity getEntity(World world) {
66+
return handle.getEntityModifier(world).readSafely(0);
67+
}
68+
69+
public void setEntity(Entity entity) {
70+
handle.getEntityModifier(entity.getWorld()).writeSafely(0, entity);
4971
}
5072

5173
/**
5274
* Returns the player action (e.g. start/stop sneaking, sprinting).
5375
*/
5476
public EnumWrappers.PlayerAction getAction() {
55-
return handle.getEnumModifier(EnumWrappers.PlayerAction.class, EnumWrappers.getPlayerActionClass()).read(0);
77+
return handle.getEnumModifier(EnumWrappers.PlayerAction.class, EnumWrappers.getPlayerActionClass()).readSafely(0);
5678
}
5779

5880
/**
5981
* Sets the player action (e.g. start/stop sneaking, sprinting).
6082
*/
6183
public void setAction(EnumWrappers.PlayerAction action) {
62-
handle.getEnumModifier(EnumWrappers.PlayerAction.class, EnumWrappers.getPlayerActionClass()).write(0, action);
84+
handle.getEnumModifier(EnumWrappers.PlayerAction.class, EnumWrappers.getPlayerActionClass()).writeSafely(0, action);
6385
}
6486

6587
/**
6688
* Returns the auxiliary data (e.g. jump boost level for horse jump).
6789
*/
6890
public int getData() {
69-
return handle.getIntegers().read(1);
91+
return handle.getIntegers().readSafely(1);
7092
}
7193

7294
/**
7395
* Sets the auxiliary data (e.g. jump boost level for horse jump).
7496
*/
7597
public void setData(int data) {
76-
handle.getIntegers().write(1, data);
98+
handle.getIntegers().writeSafely(1, data);
7799
}
78100
}

src/test/java/net/dmulloy2/protocol/wrappers/game/serverbound/WrappedServerboundContainerClickPacketTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ class WrappedServerboundContainerClickPacketTest {
1313
static void beforeAll() {
1414
BukkitInitialization.initializeAll();
1515
}
16-
17-
18-
16+
1917
@Test
2018
void testAllArgsCreate() {
2119
WrappedServerboundContainerClickPacket w = new WrappedServerboundContainerClickPacket(

0 commit comments

Comments
 (0)