Skip to content

Commit 8c928cb

Browse files
committed
Allow the usage of user-created enums
Basically this will allow developers to create their own enums instead of having to wait on me to make them. The only caveat is that enum constants will have to match up exactly with their NMS counterparts
1 parent 6030992 commit 8c928cb

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

modules/API/src/main/java/com/comphenix/protocol/events/PacketContainer.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.comphenix.protocol.wrappers.EnumWrappers.CombatEventType;
8282
import com.comphenix.protocol.wrappers.EnumWrappers.Difficulty;
8383
import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction;
84+
import com.comphenix.protocol.wrappers.EnumWrappers.EnumConverter;
8485
import com.comphenix.protocol.wrappers.EnumWrappers.Hand;
8586
import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot;
8687
import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode;
@@ -897,6 +898,33 @@ public StructureModifier<Hand> getHands() {
897898
EnumWrappers.getHandClass(), EnumWrappers.getHandConverter());
898899
}
899900

901+
/**
902+
* Retrieve a read/write structure for an enum. This allows for the use of
903+
* user-created enums that may not exist in ProtocolLib. The specific (user
904+
* created) enum constants must match up perfectly with their generic (NMS)
905+
* counterparts.
906+
*
907+
* @param enumClass The specific Enum class
908+
* @param nmsClass The generic Enum class
909+
* @return The modifier
910+
*/
911+
public <T extends Enum<T>> StructureModifier<T> getEnumModifier(Class<T> enumClass, Class<?> nmsClass) {
912+
return structureModifier.<T>withType(nmsClass, new EnumConverter<T>(enumClass));
913+
}
914+
915+
/**
916+
* Retrieve a read/write structure for an enum. This method is for convenience,
917+
* see {@link #getEnumModifier(Class, Class)} for more information.
918+
*
919+
* @param enumClass The specific Enum class
920+
* @param index Index of the generic Enum
921+
* @return The modifier
922+
* @see #getEnumModifier(Class, Class)
923+
*/
924+
public <T extends Enum<T>> StructureModifier<T> getEnumModifier(Class<T> enumClass, int index) {
925+
return getEnumModifier(enumClass, structureModifier.getField(index).getType());
926+
}
927+
900928
/**
901929
* Retrieves the ID of this packet.
902930
* <p>

modules/API/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ public static <T extends Enum<T>> EquivalentConverter<T> getGenericConverter(Cla
599599

600600
// The common enum converter
601601
@SuppressWarnings({ "rawtypes", "unchecked" })
602-
private static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> {
602+
public static class EnumConverter<T extends Enum<T>> implements EquivalentConverter<T> {
603603
private Class<T> specificType;
604604

605605
public EnumConverter(Class<T> specificType) {

modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.minecraft.server.v1_9_R1.EntityLightning;
3535
import net.minecraft.server.v1_9_R1.MobEffect;
3636
import net.minecraft.server.v1_9_R1.MobEffectList;
37+
import net.minecraft.server.v1_9_R1.PacketPlayOutBoss;
3738
import net.minecraft.server.v1_9_R1.PacketPlayOutUpdateAttributes;
3839
import net.minecraft.server.v1_9_R1.PacketPlayOutUpdateAttributes.AttributeSnapshot;
3940

@@ -501,6 +502,27 @@ public void testSoundEffects() {
501502
assertEquals(container.getSoundEffects().read(0), Sound.ENTITY_CAT_HISS);
502503
}
503504

505+
@Test
506+
public void testGenericEnums() {
507+
PacketContainer container = new PacketContainer(PacketType.Play.Server.BOSS);
508+
container.getEnumModifier(Action.class, 1).write(0, Action.UPDATE_PCT);
509+
510+
assertEquals(container.getEnumModifier(Action.class, PacketPlayOutBoss.Action.class).read(0), Action.UPDATE_PCT);
511+
}
512+
513+
/**
514+
* Actions from the outbound Boss packet. Used for testing generic enums.
515+
* @author dmulloy2
516+
*/
517+
public static enum Action {
518+
ADD,
519+
REMOVE,
520+
UPDATE_PCT,
521+
UPDATE_NAME,
522+
UPDATE_STYLE,
523+
UPDATE_PROPERTIES;
524+
}
525+
504526
private static final List<PacketType> BLACKLISTED = Util.asList(
505527
PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD,
506528
PacketType.Play.Server.SET_COOLDOWN

0 commit comments

Comments
 (0)