-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
CrackShotPlayerStartsReloadingWeaponEvent.java
121 lines (110 loc) · 4.74 KB
/
CrackShotPlayerStartsReloadingWeaponEvent.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
package com.denizenscript.depenizen.bukkit.events.crackshot;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.shampaggon.crackshot.events.WeaponReloadEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.Arrays;
public class CrackShotPlayerStartsReloadingWeaponEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// crackshot player starts reloading weapon
//
// @Regex ^on crackshot player starts reloading weapon$
//
// @Triggers when a player starts reloading their CrackShot weapon.
//
// @Cancellable false
//
// @Context
// <context.weapon> returns the name of the weapon.
// <context.reload_speed> returns the rate at which the weapon is reloaded.
// <context.reload_time> returns the time taken to reload in ticks.
// <context.reload_sounds> returns a ListTag of the reload sounds.
//
// @Determine
// "RELOAD_SPEED: " + ElementTag(Number) to set the reload speed.
// The reload speed can be scaled with a decimal ranging from zero to infinity.
// For example, 0 is instantaneous, 1 is normal and 2 will double the reload time.
// "RELOAD_TIME:" + DurationTag to set the time taken to reload in ticks.
// "RELOAD_SOUNDS:" + ElementTag to set the reload sounds. Use "NONE" to have no sound. <@link url https://github.com/Shampaggon/CrackShot/wiki/The-Complete-Guide-to-CrackShot#sounds>
//
// @Plugin Depenizen, CrackShot
//
// @Player Always
//
// @Group Depenizen
//
// -->
public static CrackShotPlayerStartsReloadingWeaponEvent instance;
public WeaponReloadEvent event;
public CrackShotPlayerStartsReloadingWeaponEvent() {
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("reload_speed:")) {
ElementTag newReloadSpeed = new ElementTag(lower.substring("reload_speed:".length()));
if (!newReloadSpeed.isDouble()) {
Debug.echoError("Determination for 'reload_speed' must be a valid number.");
return false;
}
event.setReloadSpeed(newReloadSpeed.asDouble());
return true;
}
else if (lower.startsWith("reload_time:")) {
String time = lower.substring("reload_time:".length());
if (!DurationTag.matches(time)) {
Debug.echoError("Determination for 'reload_time' must be a valid DurationTag.");
return false;
}
DurationTag newReloadtime = DurationTag.valueOf(lower.substring("reload_time:".length()), getTagContext(path));
event.setReloadDuration(newReloadtime.getTicksAsInt());
return true;
}
else if (lower.startsWith("reload_sounds:")) {
String newReloadSounds = determination.substring("reload_sounds:".length());
if (CoreUtilities.equalsIgnoreCase(newReloadSounds, "none")) {
newReloadSounds = "";
}
event.setSounds(newReloadSounds);
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("crackshot player starts reloading weapon");
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "weapon":
return new ElementTag(event.getWeaponTitle());
case "reload_speed":
return new ElementTag(event.getReloadSpeed());
case "reload_time":
return new ElementTag(event.getReloadDuration());
case "reload_sounds":
return new ListTag(Arrays.asList(event.getSounds().split(",")));
}
return super.getContext(name);
}
@EventHandler
public void onStartsReloading(WeaponReloadEvent event) {
this.event = event;
fire(event);
}
}