-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/main/java/ch/njol/skript/expressions/ExprEvtHand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.skript.registrations.Classes; | ||
import ch.njol.skript.util.slot.EquipmentSlot; | ||
import ch.njol.util.Kleenean; | ||
import java.lang.reflect.Method; | ||
import java.util.WeakHashMap; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.entity.EntityEvent; | ||
import org.bukkit.event.player.PlayerEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Name("Hand") | ||
@Description({"Returns which hand player uses in some InteractEvents"}) | ||
@Examples({ | ||
"on click:", | ||
" send \"%event-hand%\" to event-player", | ||
" if event-hand is off hand", | ||
" send \"It is off hand\" to event-player" | ||
}) | ||
@Since("INSERT VERSION") | ||
public class ExprEvtHand extends SimpleExpression<EquipmentSlot> { | ||
private static final WeakHashMap<Class<? extends Event>, Method> GET_HAND_METHODS = new WeakHashMap<>(); | ||
|
||
static { | ||
Skript.registerExpression(ExprEvtHand.class, EquipmentSlot.class, ExpressionType.SIMPLE, "[event( |-)]hand"); | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event e, boolean debug) { | ||
if (e == null) | ||
return "the hand"; | ||
return Classes.getDebugMessage(getSingle(e)); | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Class<? extends EquipmentSlot> getReturnType() { | ||
return EquipmentSlot.class; | ||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { | ||
if (getParser().getCurrentEvents() == null) { | ||
Skript.error("There is no 'event-hand' without a event"); | ||
return false; | ||
} | ||
for (Class<? extends Event> currentEvent : getParser().getCurrentEvents()) { | ||
if (Skript.methodExists(currentEvent, "getHand")) { | ||
try { | ||
GET_HAND_METHODS.put(currentEvent, currentEvent.getDeclaredMethod("getHand")); //Cache it when init | ||
return true; | ||
} catch (NoSuchMethodException ignored) { | ||
} | ||
} | ||
} | ||
Skript.error("There is no 'event-hand' in this event: " + getParser().getCurrentEventName()); | ||
return false; | ||
} | ||
|
||
@Override | ||
protected @Nullable EquipmentSlot[] get(Event event) { | ||
Method getHandMethod = GET_HAND_METHODS.get(event.getClass()); | ||
if (getHandMethod == null) { | ||
try { | ||
getHandMethod = event.getClass().getDeclaredMethod("getHand"); | ||
GET_HAND_METHODS.put(event.getClass(), getHandMethod); | ||
} catch (NoSuchMethodException ignored) { | ||
return null; //If it is not cached, and cannot get the method, stop trigger | ||
} | ||
} | ||
|
||
|
||
LivingEntity entity = null; | ||
if (event instanceof EntityEvent) { | ||
if (((EntityEvent) event).getEntity() instanceof LivingEntity) { | ||
entity = (LivingEntity) ((EntityEvent) event).getEntity(); | ||
} | ||
} else if (event instanceof PlayerEvent) { | ||
entity = ((PlayerEvent) event).getPlayer(); | ||
} | ||
if (entity == null || entity.getEquipment() == null) return null; | ||
|
||
|
||
try { | ||
org.bukkit.inventory.EquipmentSlot bkEquip = (org.bukkit.inventory.EquipmentSlot) getHandMethod.invoke(event); | ||
return new EquipmentSlot[]{ | ||
new EquipmentSlot(entity.getEquipment(), | ||
bkEquip == org.bukkit.inventory.EquipmentSlot.HAND | ||
? EquipmentSlot.EquipSlot.TOOL | ||
: EquipmentSlot.EquipSlot.OFF_HAND | ||
) | ||
}; | ||
} catch (ReflectiveOperationException ignored) { | ||
return null; | ||
} | ||
} | ||
} |