/
PlayerStatisticIncrementsScriptEvent.java
113 lines (100 loc) · 3.92 KB
/
PlayerStatisticIncrementsScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Statistic;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerStatisticIncrementEvent;
public class PlayerStatisticIncrementsScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player statistic increments
// player statistic <'statistic'> increments
//
// @Group Player
//
// @Cancellable true
//
// @Triggers when a player's statistics increment.
//
// @Context
// <context.statistic> returns the statistic that incremented. Statistic names: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Statistic.html>
// <context.previous_value> returns the old value of the statistic.
// <context.new_value> returns the new value of the statistic.
// <context.qualifier> returns the qualifier (EntityTag/MaterialTag) if any.
//
// @Player Always.
//
// -->
public PlayerStatisticIncrementsScriptEvent() {
instance = this;
registerCouldMatcher("player statistic increments");
registerCouldMatcher("player statistic <'statistic'> increments");
}
public static PlayerStatisticIncrementsScriptEvent instance;
public Statistic statistic;
public PlayerStatisticIncrementEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!super.couldMatch(path)) {
return false;
}
if (!path.eventArgLowerAt(2).equals("increments")) {
if (!path.eventArgLowerAt(3).equals("increments") || !couldMatchEnum(path.eventArgLowerAt(2), Statistic.values())) {
return false;
}
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String stat = path.eventArgLowerAt(2);
if (!stat.equals("increments") && !stat.equals(CoreUtilities.toLowerCase(statistic.toString()))) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerStatisticIncrements";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "statistic":
return new ElementTag(statistic.toString());
case "previous_value":
return new ElementTag(event.getPreviousValue());
case "new_value":
return new ElementTag(event.getNewValue());
case "qualifier":
if (statistic.getType() == Statistic.Type.BLOCK || statistic.getType() == Statistic.Type.ITEM) {
return new MaterialTag(event.getMaterial());
}
else if (statistic.getType() == Statistic.Type.ENTITY) {
return new EntityTag(event.getEntityType());
}
break;
}
return super.getContext(name);
}
@EventHandler
public void onPlayerStatisticIncrements(PlayerStatisticIncrementEvent event) {
if (EntityTag.isNPC(event.getPlayer())) {
return;
}
statistic = event.getStatistic();
this.event = event;
fire(event);
}
}