/
PlayerBreaksBlockScriptEvent.java
160 lines (146 loc) · 5.55 KB
/
PlayerBreaksBlockScriptEvent.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.*;
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 org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import java.util.Arrays;
import java.util.HashSet;
public class PlayerBreaksBlockScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player breaks block
// player breaks <material>
//
// @Regex ^on player breaks [^\s]+$
//
// @Synonyms player mines block,player mines ore,player digs block
//
// @Group Player
//
// @Location true
// @Switch with:<item> to only process the event when the player is breaking the block with a specified item.
//
// @Cancellable true
//
// @Triggers when a player breaks a block.
//
// @Context
// <context.location> returns the LocationTag the block was broken at.
// <context.material> returns the MaterialTag of the block that was broken.
// <context.xp> returns how much XP will be dropped.
// <context.should_drop_items> returns whether the event will drop items.
//
// @Determine
// "NOTHING" to make the block drop no items.
// ListTag(ItemTag) to make the block drop a specified list of items.
// ElementTag(Number) to set the amount of xp to drop.
//
// @Player Always.
//
// -->
public PlayerBreaksBlockScriptEvent() {
instance = this;
}
public static PlayerBreaksBlockScriptEvent instance;
public LocationTag location;
public MaterialTag material;
public BlockBreakEvent event;
public static HashSet<String> notRelevantBreakables = new HashSet<>(Arrays.asList("item", "held", "hanging", "painting", "item_frame", "leash_hitch"));
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("player breaks")) {
return false;
}
if (notRelevantBreakables.contains(path.eventArgLowerAt(2))) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(2))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String mat = path.eventArgLowerAt(2);
if (!tryMaterial(material, mat)) {
return false;
}
if (!runInCheck(path, location)) {
return false;
}
if (!runWithCheck(path, new ItemTag(event.getPlayer().getEquipment().getItemInMainHand()))) {
return false;
}
// Deprecated in favor of with: format
if (path.eventArgLowerAt(3).equals("with")
&& !tryItem(new ItemTag(event.getPlayer().getEquipment().getItemInMainHand()), path.eventArgLowerAt(4))) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerBreaksBlock";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
Block block = event.getBlock();
if (determinationObj instanceof ElementTag) {
String lower = CoreUtilities.toLowerCase(determination);
if (lower.equals("nothing")) {
event.setExpToDrop(0);
event.setDropItems(false);
return true;
}
else if (((ElementTag) determinationObj).isInt()) {
event.setExpToDrop(((ElementTag) determinationObj).asInt());
return true;
}
}
if (Argument.valueOf(determination).matchesArgumentList(ItemTag.class)) {
event.setDropItems(false);
for (ItemTag newItem : ListTag.valueOf(determination, getTagContext(path)).filter(ItemTag.class, path.container, true)) {
block.getWorld().dropItemNaturally(block.getLocation(), newItem.getItemStack()); // Drop each item
}
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location":
return location;
case "material":
return material;
case "xp":
return new ElementTag(event.getExpToDrop());
case "should_drop_items":
return new ElementTag(event.isDropItems());
}
return super.getContext(name);
}
@EventHandler
public void onPlayerBreaksBlock(BlockBreakEvent event) {
if (EntityTag.isNPC(event.getPlayer())) {
return;
}
material = new MaterialTag(event.getBlock());
location = new LocationTag(event.getBlock().getLocation());
this.event = event;
fire(event);
}
}