Skip to content

Commit c893a3f

Browse files
committed
Fix player action enum in 1.15
1 parent 73c71e0 commit c893a3f

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

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

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashMap;
55
import java.util.Locale;
66
import java.util.Map;
7+
import java.util.concurrent.ConcurrentHashMap;
78

89
import com.comphenix.protocol.PacketType;
910
import com.comphenix.protocol.PacketType.Protocol;
@@ -159,16 +160,26 @@ public enum PlayerDigType {
159160
SWAP_HELD_ITEMS
160161
}
161162

162-
public enum PlayerAction {
163-
START_SNEAKING,
164-
STOP_SNEAKING,
163+
public enum PlayerAction implements AliasedEnum {
164+
PRESS_SHIFT_KEY("START_SNEAKING"),
165+
RELEASE_SHIFT_KEY("STOP_SNEAKING"),
165166
STOP_SLEEPING,
166167
START_SPRINTING,
167168
STOP_SPRINTING,
168169
START_RIDING_JUMP,
169170
STOP_RIDING_JUMP,
170171
OPEN_INVENTORY,
171-
START_FALL_FLYING
172+
START_FALL_FLYING;
173+
174+
String[] aliases;
175+
PlayerAction(String... aliases) {
176+
this.aliases = aliases;
177+
}
178+
179+
@Override
180+
public String[] getAliases() {
181+
return aliases;
182+
}
172183
}
173184

174185
public enum ScoreboardAction {
@@ -617,7 +628,7 @@ public static EquivalentConverter<PlayerDigType> getPlayerDiggingActionConverter
617628
}
618629

619630
public static EquivalentConverter<PlayerAction> getEntityActionConverter() {
620-
return new EnumConverter<>(getPlayerActionClass(), PlayerAction.class);
631+
return new AliasedEnumConverter<>(getPlayerActionClass(), PlayerAction.class);
621632
}
622633

623634
public static EquivalentConverter<ScoreboardAction> getUpdateScoreActionConverter() {
@@ -697,6 +708,68 @@ void setGenericType(Class<?> genericType) {
697708
}
698709
}
699710

711+
public interface AliasedEnum {
712+
String[] getAliases();
713+
}
714+
715+
public static class AliasedEnumConverter<T extends Enum<T> & AliasedEnum> implements EquivalentConverter<T> {
716+
private Class<?> genericType;
717+
private Class<T> specificType;
718+
719+
public AliasedEnumConverter(Class<?> genericType, Class<T> specificType) {
720+
this.genericType = genericType;
721+
this.specificType = specificType;
722+
}
723+
724+
@Override
725+
public T getSpecific(Object generic) {
726+
String name = ((Enum) generic).name();
727+
728+
try {
729+
return Enum.valueOf(specificType, name);
730+
} catch (Exception ex) {
731+
// TODO would caching help much, if at all?
732+
for (T elem : specificType.getEnumConstants()) {
733+
for (String alias : elem.getAliases()) {
734+
if (alias.equals(name)) {
735+
return elem;
736+
}
737+
}
738+
}
739+
}
740+
741+
throw new IllegalArgumentException("Unknown enum constant " + name);
742+
}
743+
744+
@Override
745+
public Object getGeneric(T specific) {
746+
String name = specific.name();
747+
748+
try {
749+
return Enum.valueOf((Class) genericType, specific.name());
750+
} catch (Exception ex) {
751+
for (Object elem : genericType.getEnumConstants()) {
752+
for (String alias : specific.getAliases()) {
753+
if (alias.equals(name)) {
754+
return elem;
755+
}
756+
}
757+
}
758+
}
759+
760+
throw new IllegalArgumentException("Unknown enum constant " + name);
761+
}
762+
763+
@Override
764+
public Class<T> getSpecificType() {
765+
return specificType;
766+
}
767+
768+
void setGenericType(Class<?> genericType) {
769+
this.genericType = genericType;
770+
}
771+
}
772+
700773
/**
701774
* Used for classes where it's an enum in everything but name
702775
* @param <T> Generic type

src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,12 @@ public NbtBase<?> getValueOrDefault(String key, NbtType type) {
201201
NbtBase<?> nbt = getValue(key);
202202

203203
// Create or get a compound
204-
if (nbt == null)
205-
put(nbt = NbtFactory.ofWrapper(type, key));
206-
else if (nbt.getType() != type)
204+
if (nbt == null) {
205+
nbt = NbtFactory.ofWrapper(type, key);
206+
put(nbt);
207+
} else if (nbt.getType() != type) {
207208
throw new IllegalArgumentException("Cannot get tag " + nbt + ": Not a " + type);
209+
}
208210

209211
return nbt;
210212
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,14 @@ public void testPotionEffect() {
453453
assertEquals(e, (byte) packet.getBytes().read(2));
454454
}
455455

456+
@Test
457+
public void testPlayerAction() {
458+
PacketContainer container = new PacketContainer(PacketType.Play.Client.ENTITY_ACTION);
459+
container.getPlayerActions().write(0, EnumWrappers.PlayerAction.PRESS_SHIFT_KEY);
460+
461+
assertEquals(container.getPlayerActions().read(0), EnumWrappers.PlayerAction.PRESS_SHIFT_KEY);
462+
}
463+
456464
@Test
457465
public void testMobEffectList() {
458466
PacketContainer container = new PacketContainer(PacketType.Play.Server.REMOVE_ENTITY_EFFECT);

0 commit comments

Comments
 (0)