-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
BlockDispensesScriptEvent.java
146 lines (131 loc) · 4.96 KB
/
BlockDispensesScriptEvent.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package net.aufdemrand.denizen.events.block;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDispenseEvent;
public class BlockDispensesScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// block dispenses item (in <area>)
// block dispenses <item> (in <area>)
// <block> dispenses item (in <area>)
// <block> dispenses <item> (in <area>)
//
// @Regex ^on [^\s]+ dispense [^\s]+ ( in ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Cancellable true
//
// @Triggers when a block dispenses an item.
//
// @Context
// <context.location> returns the dLocation of the dispenser.
// <context.item> returns the dItem of the item being dispensed.
// <context.velocity> returns a dLocation vector of the velocity the item will be shot at.
//
// @Determine
// Element(Decimal) (DEPRECATED) to multiply the velocity by the given amount.
// dLocation to set the velocity the item will be shot at.
// dItem to set the item being shot.
//
// -->
public BlockDispensesScriptEvent() {
instance = this;
}
public static BlockDispensesScriptEvent instance;
public dLocation location;
public dItem item;
private dLocation velocity;
private dMaterial material;
public BlockDispenseEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
return cmd.equals("dispenses") && lower.length() >= 3;
}
@Override
public boolean matches(ScriptPath path) {
String s = path.event;
String lower = path.eventLower;
if (!runInCheck(path, location)) {
return false;
}
String dispenser = CoreUtilities.getXthArg(0, lower);
String iTest = CoreUtilities.getXthArg(2, lower);
return tryMaterial(material, dispenser) && (iTest.equals("item") || tryItem(item, iTest));
}
@Override
public String getName() {
return "BlockDispenses";
}
@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void destroy() {
BlockDispenseEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (aH.matchesDouble(determination)) {
velocity = new dLocation(velocity.multiply(aH.getDoubleFrom(determination)));
return true;
}
else if (dLocation.matches(determination)) {
dLocation vel = dLocation.valueOf(determination);
if (vel == null) {
dB.echoError("[" + getName() + "] Invalid velocity!");
}
else {
velocity = vel;
}
}
else if (dItem.matches(determination)) {
dItem it = dItem.valueOf(determination);
if (it == null) {
dB.echoError("[" + getName() + "] Invalid item!");
}
else {
item = it;
}
}
return super.applyDetermination(container, determination);
}
@Override
public dObject getContext(String name) {
if (name.equals("location")) {
return location;
}
else if (name.equals("item")) {
return item;
}
else if (name.equals("velocity")) {
return velocity;
}
return super.getContext(name);
}
@EventHandler
public void onBlockDispenses(BlockDispenseEvent event) {
location = new dLocation(event.getBlock().getLocation());
material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
item = new dItem(event.getItem());
cancelled = event.isCancelled();
velocity = new dLocation(null, event.getVelocity().getX(), event.getVelocity().getY(), event.getVelocity().getZ());
this.event = event;
fire();
event.setVelocity(velocity.toVector());
event.setItem(item.getItemStack());
event.setCancelled(cancelled);
}
}