Add modifiers for Sounds and Hands

This commit is contained in:
Dan Mulloy 2016-04-04 21:37:45 -04:00
parent 688da1a1bb
commit 6030992af4
5 changed files with 59 additions and 2 deletions

View File

@ -42,6 +42,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -80,6 +81,7 @@ import com.comphenix.protocol.wrappers.EnumWrappers.ClientCommand;
import com.comphenix.protocol.wrappers.EnumWrappers.CombatEventType; import com.comphenix.protocol.wrappers.EnumWrappers.CombatEventType;
import com.comphenix.protocol.wrappers.EnumWrappers.Difficulty; import com.comphenix.protocol.wrappers.EnumWrappers.Difficulty;
import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction; import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction;
import com.comphenix.protocol.wrappers.EnumWrappers.Hand;
import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot; import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot;
import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode; import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode;
import com.comphenix.protocol.wrappers.EnumWrappers.Particle; import com.comphenix.protocol.wrappers.EnumWrappers.Particle;
@ -867,6 +869,16 @@ public class PacketContainer implements Serializable {
EnumWrappers.getSoundCategoryClass(), EnumWrappers.getSoundCategoryConverter()); EnumWrappers.getSoundCategoryClass(), EnumWrappers.getSoundCategoryConverter());
} }
/**
* Retrieve a read/write structure for the SoundEffect enum in 1.9.
* @return A modifier for SoundEffect enum fields.
*/
public StructureModifier<Sound> getSoundEffects() {
// Convert to and from Bukkit
return structureModifier.<Sound>withType(
MinecraftReflection.getSoundEffectClass(), BukkitConverters.getSoundConverter());
}
/** /**
* Retrive a read/write structure for the ItemSlot enum in 1.9. * Retrive a read/write structure for the ItemSlot enum in 1.9.
* @return A modifier for ItemSlot enum fields. * @return A modifier for ItemSlot enum fields.
@ -876,6 +888,15 @@ public class PacketContainer implements Serializable {
EnumWrappers.getItemSlotClass(), EnumWrappers.getItemSlotConverter()); EnumWrappers.getItemSlotClass(), EnumWrappers.getItemSlotConverter());
} }
/**
* Retrive a read/write structure for the Hand enum in 1.9.
* @return A modifier for Hand enum fields.
*/
public StructureModifier<Hand> getHands() {
return structureModifier.<Hand>withType(
EnumWrappers.getHandClass(), EnumWrappers.getHandConverter());
}
/** /**
* Retrieves the ID of this packet. * Retrieves the ID of this packet.
* <p> * <p>

View File

@ -1347,6 +1347,16 @@ public class MinecraftReflection {
return getMinecraftClass("MobEffectList"); return getMinecraftClass("MobEffectList");
} }
public static Class<?> getSoundEffectClass() {
try {
return getMinecraftClass("SoundEffect");
} catch (RuntimeException ex) {
FuzzyReflection fuzzy = FuzzyReflection.fromClass(PacketType.Play.Server.NAMED_SOUND_EFFECT.getPacketClass(), true);
Field field = fuzzy.getFieldByType("(.*)(Sound)(.*)");
return setMinecraftClass("SoundEffect", field.getType());
}
}
/** /**
* Retrieve the ServerConnection abstract class. * Retrieve the ServerConnection abstract class.
* @return The ServerConnection class. * @return The ServerConnection class.

View File

@ -980,7 +980,7 @@ public class BukkitConverters {
if (soundGetter == null) { if (soundGetter == null) {
Class<?> soundEffects = MinecraftReflection.getMinecraftClass("SoundEffects"); Class<?> soundEffects = MinecraftReflection.getMinecraftClass("SoundEffects");
FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffects, true); FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffects, true);
soundGetter = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getSound", MinecraftReflection.getMinecraftClass("SoundEffect"), String.class)); soundGetter = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getSound", MinecraftReflection.getSoundEffectClass(), new Class<?>[] { String.class }));
} }
MinecraftKey key = MinecraftKey.fromEnum(specific); MinecraftKey key = MinecraftKey.fromEnum(specific);
@ -992,7 +992,7 @@ public class BukkitConverters {
if (soundKey == null) { if (soundKey == null) {
Class<?> soundEffect = generic.getClass(); Class<?> soundEffect = generic.getClass();
FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffect, true); FuzzyReflection fuzzy = FuzzyReflection.fromClass(soundEffect, true);
soundKey = Accessors.getFieldAccessor(fuzzy.getFieldByType("key", MinecraftReflection.getMinecraftClass("MinecraftKey"))); soundKey = Accessors.getFieldAccessor(fuzzy.getFieldByType("key", MinecraftReflection.getMinecraftKeyClass()));
} }
MinecraftKey key = MinecraftKey.fromHandle(soundKey.get(generic)); MinecraftKey key = MinecraftKey.fromHandle(soundKey.get(generic));

View File

@ -315,6 +315,11 @@ public abstract class EnumWrappers {
HEAD; HEAD;
} }
public enum Hand {
MAIN_HAND,
OFF_HAND;
}
private static Class<?> PROTOCOL_CLASS = null; private static Class<?> PROTOCOL_CLASS = null;
private static Class<?> CLIENT_COMMAND_CLASS = null; private static Class<?> CLIENT_COMMAND_CLASS = null;
private static Class<?> CHAT_VISIBILITY_CLASS = null; private static Class<?> CHAT_VISIBILITY_CLASS = null;
@ -332,6 +337,7 @@ public abstract class EnumWrappers {
private static Class<?> PARTICLE_CLASS = null; private static Class<?> PARTICLE_CLASS = null;
private static Class<?> SOUND_CATEGORY_CLASS = null; private static Class<?> SOUND_CATEGORY_CLASS = null;
private static Class<?> ITEM_SLOT_CLASS = null; private static Class<?> ITEM_SLOT_CLASS = null;
private static Class<?> HAND_CLASS = null;
private static boolean INITIALIZED = false; private static boolean INITIALIZED = false;
private static Map<Class<?>, EquivalentConverter<?>> FROM_NATIVE = Maps.newHashMap(); private static Map<Class<?>, EquivalentConverter<?>> FROM_NATIVE = Maps.newHashMap();
@ -366,6 +372,7 @@ public abstract class EnumWrappers {
PARTICLE_CLASS = getEnum(PacketType.Play.Server.WORLD_PARTICLES.getPacketClass(), 0); PARTICLE_CLASS = getEnum(PacketType.Play.Server.WORLD_PARTICLES.getPacketClass(), 0);
SOUND_CATEGORY_CLASS = getEnum(PacketType.Play.Server.CUSTOM_SOUND_EFFECT.getPacketClass(), 0); SOUND_CATEGORY_CLASS = getEnum(PacketType.Play.Server.CUSTOM_SOUND_EFFECT.getPacketClass(), 0);
ITEM_SLOT_CLASS = getEnum(PacketType.Play.Server.ENTITY_EQUIPMENT.getPacketClass(), 0); ITEM_SLOT_CLASS = getEnum(PacketType.Play.Server.ENTITY_EQUIPMENT.getPacketClass(), 0);
HAND_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 1);
associate(PROTOCOL_CLASS, Protocol.class, getClientCommandConverter()); associate(PROTOCOL_CLASS, Protocol.class, getClientCommandConverter());
associate(CLIENT_COMMAND_CLASS, ClientCommand.class, getClientCommandConverter()); associate(CLIENT_COMMAND_CLASS, ClientCommand.class, getClientCommandConverter());
@ -384,6 +391,7 @@ public abstract class EnumWrappers {
associate(PARTICLE_CLASS, Particle.class, getParticleConverter()); associate(PARTICLE_CLASS, Particle.class, getParticleConverter());
associate(SOUND_CATEGORY_CLASS, SoundCategory.class, getSoundCategoryConverter()); associate(SOUND_CATEGORY_CLASS, SoundCategory.class, getSoundCategoryConverter());
associate(ITEM_SLOT_CLASS, ItemSlot.class, getItemSlotConverter()); associate(ITEM_SLOT_CLASS, ItemSlot.class, getItemSlotConverter());
associate(HAND_CLASS, Hand.class, getHandConverter());
INITIALIZED = true; INITIALIZED = true;
} }
@ -502,6 +510,11 @@ public abstract class EnumWrappers {
return ITEM_SLOT_CLASS; return ITEM_SLOT_CLASS;
} }
public static Class<?> getHandClass() {
initialize();
return HAND_CLASS;
}
// Get the converters // Get the converters
public static EquivalentConverter<Protocol> getProtocolConverter() { public static EquivalentConverter<Protocol> getProtocolConverter() {
return new EnumConverter<Protocol>(Protocol.class); return new EnumConverter<Protocol>(Protocol.class);
@ -571,6 +584,10 @@ public abstract class EnumWrappers {
return new EnumConverter<ItemSlot>(ItemSlot.class); return new EnumConverter<ItemSlot>(ItemSlot.class);
} }
public static EquivalentConverter<Hand> getHandConverter() {
return new EnumConverter<Hand>(Hand.class);
}
/** /**
* Retrieve a generic enum converter for use with StructureModifiers. * Retrieve a generic enum converter for use with StructureModifiers.
* @param enumClass - Enum class * @param enumClass - Enum class

View File

@ -42,6 +42,7 @@ import org.apache.commons.lang3.builder.EqualsBuilder;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -492,6 +493,14 @@ public class PacketContainerTest {
assertEquals(container.getSoundCategories().read(0), SoundCategory.PLAYERS); assertEquals(container.getSoundCategories().read(0), SoundCategory.PLAYERS);
} }
@Test
public void testSoundEffects() {
PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT);
container.getSoundEffects().write(0, Sound.ENTITY_CAT_HISS);
assertEquals(container.getSoundEffects().read(0), Sound.ENTITY_CAT_HISS);
}
private static final List<PacketType> BLACKLISTED = Util.asList( private static final List<PacketType> BLACKLISTED = Util.asList(
PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD, PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD,
PacketType.Play.Server.SET_COOLDOWN PacketType.Play.Server.SET_COOLDOWN