Skip to content

Commit 6030992

Browse files
committed
Add modifiers for Sounds and Hands
1 parent 688da1a commit 6030992

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javax.annotation.Nullable;
4343

4444
import org.bukkit.Material;
45+
import org.bukkit.Sound;
4546
import org.bukkit.World;
4647
import org.bukkit.WorldType;
4748
import org.bukkit.entity.Entity;
@@ -80,6 +81,7 @@
8081
import com.comphenix.protocol.wrappers.EnumWrappers.CombatEventType;
8182
import com.comphenix.protocol.wrappers.EnumWrappers.Difficulty;
8283
import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction;
84+
import com.comphenix.protocol.wrappers.EnumWrappers.Hand;
8385
import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot;
8486
import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode;
8587
import com.comphenix.protocol.wrappers.EnumWrappers.Particle;
@@ -867,6 +869,16 @@ public StructureModifier<SoundCategory> getSoundCategories() {
867869
EnumWrappers.getSoundCategoryClass(), EnumWrappers.getSoundCategoryConverter());
868870
}
869871

872+
/**
873+
* Retrieve a read/write structure for the SoundEffect enum in 1.9.
874+
* @return A modifier for SoundEffect enum fields.
875+
*/
876+
public StructureModifier<Sound> getSoundEffects() {
877+
// Convert to and from Bukkit
878+
return structureModifier.<Sound>withType(
879+
MinecraftReflection.getSoundEffectClass(), BukkitConverters.getSoundConverter());
880+
}
881+
870882
/**
871883
* Retrive a read/write structure for the ItemSlot enum in 1.9.
872884
* @return A modifier for ItemSlot enum fields.
@@ -876,6 +888,15 @@ public StructureModifier<ItemSlot> getItemSlots() {
876888
EnumWrappers.getItemSlotClass(), EnumWrappers.getItemSlotConverter());
877889
}
878890

891+
/**
892+
* Retrive a read/write structure for the Hand enum in 1.9.
893+
* @return A modifier for Hand enum fields.
894+
*/
895+
public StructureModifier<Hand> getHands() {
896+
return structureModifier.<Hand>withType(
897+
EnumWrappers.getHandClass(), EnumWrappers.getHandConverter());
898+
}
899+
879900
/**
880901
* Retrieves the ID of this packet.
881902
* <p>

modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,16 @@ public static Class<?> getMobEffectListClass() {
13471347
return getMinecraftClass("MobEffectList");
13481348
}
13491349

1350+
public static Class<?> getSoundEffectClass() {
1351+
try {
1352+
return getMinecraftClass("SoundEffect");
1353+
} catch (RuntimeException ex) {
1354+
FuzzyReflection fuzzy = FuzzyReflection.fromClass(PacketType.Play.Server.NAMED_SOUND_EFFECT.getPacketClass(), true);
1355+
Field field = fuzzy.getFieldByType("(.*)(Sound)(.*)");
1356+
return setMinecraftClass("SoundEffect", field.getType());
1357+
}
1358+
}
1359+
13501360
/**
13511361
* Retrieve the ServerConnection abstract class.
13521362
* @return The ServerConnection class.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ protected Object getGenericValue(Class<?> genericType, Sound specific) {
980980
if (soundGetter == null) {
981981
Class<?> soundEffects = MinecraftReflection.getMinecraftClass("SoundEffects");
982982
FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffects, true);
983-
soundGetter = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getSound", MinecraftReflection.getMinecraftClass("SoundEffect"), String.class));
983+
soundGetter = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getSound", MinecraftReflection.getSoundEffectClass(), new Class<?>[] { String.class }));
984984
}
985985

986986
MinecraftKey key = MinecraftKey.fromEnum(specific);
@@ -992,7 +992,7 @@ protected Sound getSpecificValue(Object generic) {
992992
if (soundKey == null) {
993993
Class<?> soundEffect = generic.getClass();
994994
FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffect, true);
995-
soundKey = Accessors.getFieldAccessor(fuzzy.getFieldByType("key", MinecraftReflection.getMinecraftClass("MinecraftKey")));
995+
soundKey = Accessors.getFieldAccessor(fuzzy.getFieldByType("key", MinecraftReflection.getMinecraftKeyClass()));
996996
}
997997

998998
MinecraftKey key = MinecraftKey.fromHandle(soundKey.get(generic));

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ public enum ItemSlot {
315315
HEAD;
316316
}
317317

318+
public enum Hand {
319+
MAIN_HAND,
320+
OFF_HAND;
321+
}
322+
318323
private static Class<?> PROTOCOL_CLASS = null;
319324
private static Class<?> CLIENT_COMMAND_CLASS = null;
320325
private static Class<?> CHAT_VISIBILITY_CLASS = null;
@@ -332,6 +337,7 @@ public enum ItemSlot {
332337
private static Class<?> PARTICLE_CLASS = null;
333338
private static Class<?> SOUND_CATEGORY_CLASS = null;
334339
private static Class<?> ITEM_SLOT_CLASS = null;
340+
private static Class<?> HAND_CLASS = null;
335341

336342
private static boolean INITIALIZED = false;
337343
private static Map<Class<?>, EquivalentConverter<?>> FROM_NATIVE = Maps.newHashMap();
@@ -366,6 +372,7 @@ private static void initialize() {
366372
PARTICLE_CLASS = getEnum(PacketType.Play.Server.WORLD_PARTICLES.getPacketClass(), 0);
367373
SOUND_CATEGORY_CLASS = getEnum(PacketType.Play.Server.CUSTOM_SOUND_EFFECT.getPacketClass(), 0);
368374
ITEM_SLOT_CLASS = getEnum(PacketType.Play.Server.ENTITY_EQUIPMENT.getPacketClass(), 0);
375+
HAND_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 1);
369376

370377
associate(PROTOCOL_CLASS, Protocol.class, getClientCommandConverter());
371378
associate(CLIENT_COMMAND_CLASS, ClientCommand.class, getClientCommandConverter());
@@ -384,6 +391,7 @@ private static void initialize() {
384391
associate(PARTICLE_CLASS, Particle.class, getParticleConverter());
385392
associate(SOUND_CATEGORY_CLASS, SoundCategory.class, getSoundCategoryConverter());
386393
associate(ITEM_SLOT_CLASS, ItemSlot.class, getItemSlotConverter());
394+
associate(HAND_CLASS, Hand.class, getHandConverter());
387395
INITIALIZED = true;
388396
}
389397

@@ -502,6 +510,11 @@ public static Class<?> getItemSlotClass() {
502510
return ITEM_SLOT_CLASS;
503511
}
504512

513+
public static Class<?> getHandClass() {
514+
initialize();
515+
return HAND_CLASS;
516+
}
517+
505518
// Get the converters
506519
public static EquivalentConverter<Protocol> getProtocolConverter() {
507520
return new EnumConverter<Protocol>(Protocol.class);
@@ -571,6 +584,10 @@ public static EquivalentConverter<ItemSlot> getItemSlotConverter() {
571584
return new EnumConverter<ItemSlot>(ItemSlot.class);
572585
}
573586

587+
public static EquivalentConverter<Hand> getHandConverter() {
588+
return new EnumConverter<Hand>(Hand.class);
589+
}
590+
574591
/**
575592
* Retrieve a generic enum converter for use with StructureModifiers.
576593
* @param enumClass - Enum class

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.bukkit.ChatColor;
4343
import org.bukkit.DyeColor;
4444
import org.bukkit.Material;
45+
import org.bukkit.Sound;
4546
import org.bukkit.WorldType;
4647
import org.bukkit.inventory.ItemStack;
4748
import org.bukkit.inventory.meta.ItemMeta;
@@ -492,6 +493,14 @@ public void testSoundCategory() {
492493
assertEquals(container.getSoundCategories().read(0), SoundCategory.PLAYERS);
493494
}
494495

496+
@Test
497+
public void testSoundEffects() {
498+
PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT);
499+
container.getSoundEffects().write(0, Sound.ENTITY_CAT_HISS);
500+
501+
assertEquals(container.getSoundEffects().read(0), Sound.ENTITY_CAT_HISS);
502+
}
503+
495504
private static final List<PacketType> BLACKLISTED = Util.asList(
496505
PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD,
497506
PacketType.Play.Server.SET_COOLDOWN

0 commit comments

Comments
 (0)