/
HorseJumpsScriptEvent.java
115 lines (104 loc) · 3.63 KB
/
HorseJumpsScriptEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.entity.Horse;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.HorseJumpEvent;
public class HorseJumpsScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// horse jumps
// <entity> jumps
//
// @Group Entity
//
// @Location true
//
// @Cancellable true
//
// @Triggers when a horse jumps.
//
// @Context
// <context.entity> returns the EntityTag of the horse.
// <context.color> returns an ElementTag of the horse's color.
// <context.power> returns an ElementTag(Decimal) of the jump's power.
//
// @Determine
// ElementTag(Decimal) to set the power of the jump.
//
// -->
public HorseJumpsScriptEvent() {
instance = this;
registerCouldMatcher("<entity> jumps");
}
public static HorseJumpsScriptEvent instance;
public EntityTag entity;
public ElementTag color;
public HorseJumpEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!super.couldMatch(path)) {
if (path.eventArgLowerAt(2).equals("jumps") && couldMatchEntity(path.eventArgLowerAt(1))) {
Deprecations.horseJumpsFormat.warn(path.container);
return true;
}
return false;
}
if (path.eventLower.startsWith("player")) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String arg1 = path.eventArgLowerAt(0);
String arg2 = path.eventArgLowerAt(1);
String tamed = arg2.equals("jumps") ? arg1 : arg2;
if (!entity.tryAdvancedMatcher(tamed)) {
return false;
}
if (path.eventArgLowerAt(2).equals("jumps") && (color == null || !arg1.equals(CoreUtilities.toLowerCase(color.toString())))) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "HorseJumps";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag && ((ElementTag) determinationObj).isFloat()) {
event.setPower(((ElementTag) determinationObj).asFloat());
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity;
case "color":
return color;
case "power":
return new ElementTag(event.getPower());
}
return super.getContext(name);
}
@EventHandler
public void onHorseJumps(HorseJumpEvent event) {
entity = new EntityTag(event.getEntity());
color = event.getEntity() instanceof Horse ? new ElementTag(((Horse) event.getEntity()).getColor().name()) : null;
this.event = event;
fire(event);
}
}