|
| 1 | +package com.comphenix.protocol.wrappers; |
| 2 | + |
| 3 | +import java.lang.reflect.Modifier; |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +import com.comphenix.protocol.PacketType; |
| 7 | +import com.comphenix.protocol.reflect.EquivalentConverter; |
| 8 | +import com.comphenix.protocol.reflect.FuzzyReflection; |
| 9 | +import com.comphenix.protocol.reflect.accessors.Accessors; |
| 10 | +import com.comphenix.protocol.reflect.accessors.ConstructorAccessor; |
| 11 | +import com.comphenix.protocol.reflect.accessors.FieldAccessor; |
| 12 | +import com.comphenix.protocol.reflect.accessors.MethodAccessor; |
| 13 | +import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract; |
| 14 | +import com.comphenix.protocol.utility.MinecraftReflection; |
| 15 | +import com.comphenix.protocol.wrappers.EnumWrappers.EntityUseAction; |
| 16 | +import com.comphenix.protocol.wrappers.EnumWrappers.Hand; |
| 17 | + |
| 18 | +import org.bukkit.util.Vector; |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents an entity used action used in the UseEntity packet sent by the client. |
| 22 | + * @author derklaro |
| 23 | + */ |
| 24 | +public class WrappedEnumEntityUseAction extends AbstractWrapper implements ClonableWrapper { |
| 25 | + |
| 26 | + public static final EquivalentConverter<WrappedEnumEntityUseAction> CONVERTER = Converters.handle(AbstractWrapper::getHandle, |
| 27 | + WrappedEnumEntityUseAction::fromHandle, WrappedEnumEntityUseAction.class); |
| 28 | + |
| 29 | + private static final Class<?> PACKET_CLASS = PacketType.Play.Client.USE_ENTITY.getPacketClass(); |
| 30 | + private static final Class<?>[] DECLARED_CLASSES = PACKET_CLASS.getDeclaredClasses(); |
| 31 | + |
| 32 | + private static final Class<?> HANDLE_TYPE = MinecraftReflection.getEnumEntityUseActionClass(); |
| 33 | + private static final MethodAccessor ACTION_USE = MinecraftReflection.getEntityUseActionEnumMethodAccessor(); |
| 34 | + |
| 35 | + private static final ConstructorAccessor INTERACT = useAction(EnumWrappers.getHandClass()); |
| 36 | + private static final ConstructorAccessor INTERACT_AT = useAction(EnumWrappers.getHandClass(), |
| 37 | + MinecraftReflection.getVec3DClass()); |
| 38 | + |
| 39 | + private static final Object ATTACK = Accessors.getFieldAccessor(FuzzyReflection.fromClass(PACKET_CLASS, true) |
| 40 | + .getField(FuzzyFieldContract.newBuilder() |
| 41 | + .requireModifier(Modifier.STATIC) |
| 42 | + .typeExact(MinecraftReflection.getEnumEntityUseActionClass()) |
| 43 | + .build()) |
| 44 | + ).get(null); |
| 45 | + private static final WrappedEnumEntityUseAction ATTACK_WRAPPER = new WrappedEnumEntityUseAction(ATTACK); |
| 46 | + |
| 47 | + private final EntityUseAction action; |
| 48 | + // these fields are only available for interact & interact_at |
| 49 | + private FieldAccessor handAccessor; |
| 50 | + private FieldAccessor positionAccessor; |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct a new wrapper for the entity use action class in the UseEntity packet. |
| 54 | + * @param handle - the NMS handle. |
| 55 | + */ |
| 56 | + private WrappedEnumEntityUseAction(Object handle) { |
| 57 | + super(HANDLE_TYPE); |
| 58 | + setHandle(handle); |
| 59 | + |
| 60 | + action = EnumWrappers.getEntityUseActionConverter().getSpecific(ACTION_USE.invoke(handle)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Finds a constructor of a declared class in the UseEntity class. Used to find the action class implementations. |
| 65 | + * @param parameterTypes - the types the constructor of the class must have. |
| 66 | + * @return a constructor for a matching class. |
| 67 | + * @throws IllegalArgumentException if no constructor was found. |
| 68 | + */ |
| 69 | + private static ConstructorAccessor useAction(Class<?>... parameterTypes) { |
| 70 | + for (Class<?> subClass : DECLARED_CLASSES) { |
| 71 | + ConstructorAccessor accessor = Accessors.getConstructorAccessorOrNull(subClass, parameterTypes); |
| 72 | + if (accessor != null) { |
| 73 | + return accessor; |
| 74 | + } |
| 75 | + } |
| 76 | + throw new IllegalArgumentException( |
| 77 | + "No constructor with " + Arrays.toString(parameterTypes) + " in " + PACKET_CLASS); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Construct a new wrapper for the entity use action class in the UseEntity packet. |
| 82 | + * @param handle - the NMS handle. |
| 83 | + * @return the created wrapper. |
| 84 | + */ |
| 85 | + public static WrappedEnumEntityUseAction fromHandle(Object handle) { |
| 86 | + return new WrappedEnumEntityUseAction(handle); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get the jvm static action for attacking an entity. |
| 91 | + * @return the action for an entity attack. |
| 92 | + */ |
| 93 | + public static WrappedEnumEntityUseAction attack() { |
| 94 | + return ATTACK_WRAPPER; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get an action for interacting with an entity. |
| 99 | + * @param hand - the hand used for the interact. |
| 100 | + * @return the action for an interact. |
| 101 | + */ |
| 102 | + public static WrappedEnumEntityUseAction interact(Hand hand) { |
| 103 | + Object handle = INTERACT.invoke(EnumWrappers.getHandConverter().getGeneric(hand)); |
| 104 | + return new WrappedEnumEntityUseAction(handle); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get an action for interacting with an entity at a specific location. |
| 109 | + * @param hand - the hand used for the interact. |
| 110 | + * @param vector - the position of the interact. |
| 111 | + * @return the action for an interact_at. |
| 112 | + */ |
| 113 | + public static WrappedEnumEntityUseAction interactAt(Hand hand, Vector vector) { |
| 114 | + Object handle = INTERACT_AT.invoke(EnumWrappers.getHandConverter().getGeneric(hand), |
| 115 | + BukkitConverters.getVectorConverter().getGeneric(vector)); |
| 116 | + return new WrappedEnumEntityUseAction(handle); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the action used for the interact. |
| 121 | + * @return the interact action. |
| 122 | + */ |
| 123 | + public EntityUseAction getAction() { |
| 124 | + return action; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get the hand used for the interact. Only available if this represents interact or interact_at. |
| 129 | + * @return the hand used for the interact. |
| 130 | + * @throws IllegalArgumentException if called for attack. |
| 131 | + */ |
| 132 | + public Hand getHand() { |
| 133 | + return EnumWrappers.getHandConverter().getSpecific(getHandAccessor().get(handle)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Sets the hand used for the interact. |
| 138 | + * @param hand the used hand. |
| 139 | + * @throws IllegalArgumentException if called for attack. |
| 140 | + */ |
| 141 | + public void setHand(Hand hand) { |
| 142 | + getHandAccessor().set(handle, EnumWrappers.getHandConverter().getGeneric(hand)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the position of the interact. Only available if this represents interact_at. |
| 147 | + * @return the position of the interact. |
| 148 | + * @throws IllegalArgumentException if called for attack or interact. |
| 149 | + */ |
| 150 | + public Vector getPosition() { |
| 151 | + return BukkitConverters.getVectorConverter().getSpecific(getPositionAccessor().get(handle)); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Sets the position of the interact. |
| 156 | + * @param position the position. |
| 157 | + * @throws IllegalArgumentException if called for attack or interact. |
| 158 | + */ |
| 159 | + public void setPosition(Vector position) { |
| 160 | + getPositionAccessor().set(handle, BukkitConverters.getVectorConverter().getGeneric(position)); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public WrappedEnumEntityUseAction deepClone() { |
| 165 | + switch (action) { |
| 166 | + case ATTACK: |
| 167 | + return WrappedEnumEntityUseAction.attack(); |
| 168 | + case INTERACT: |
| 169 | + return WrappedEnumEntityUseAction.interact(getHand()); |
| 170 | + case INTERACT_AT: |
| 171 | + return WrappedEnumEntityUseAction.interactAt(getHand(), getPosition()); |
| 172 | + default: |
| 173 | + throw new IllegalArgumentException("Invalid EntityUseAction: " + action); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Get a field accessor for the hand in the interact and interact_at type. |
| 179 | + * @return a field accessor for the hand field. |
| 180 | + * @throws IllegalArgumentException if called for attack. |
| 181 | + */ |
| 182 | + private FieldAccessor getHandAccessor() { |
| 183 | + if (handAccessor == null) { |
| 184 | + handAccessor = MinecraftReflection.getHandEntityUseActionEnumFieldAccessor(handle); |
| 185 | + } |
| 186 | + return handAccessor; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get a field accessor for the position in the interact_at type. |
| 191 | + * @return a field accessor for the position field. |
| 192 | + * @throws IllegalArgumentException if called for attack or interact. |
| 193 | + */ |
| 194 | + public FieldAccessor getPositionAccessor() { |
| 195 | + if (positionAccessor == null) { |
| 196 | + positionAccessor = MinecraftReflection.getVec3EntityUseActionEnumFieldAccessor(handle); |
| 197 | + } |
| 198 | + return positionAccessor; |
| 199 | + } |
| 200 | +} |
0 commit comments