/
BlockBuiltScriptEvent.java
123 lines (109 loc) · 3.96 KB
/
BlockBuiltScriptEvent.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
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.blocks.ModernBlockData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCanBuildEvent;
public class BlockBuiltScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// block being built (on <material>)
// <material> being built (on <material>)
//
// @Regex ^on [^\s]+ being built$
//
// @Group Block
//
// @Switch in <area>
//
// @Cancellable true
//
// @Triggers when an attempt is made to build a block on another block. Not necessarily caused by players.
//
// @Context
// <context.location> returns the LocationTag of the block the player is trying to build on.
// <context.old_material> returns the MaterialTag of the block the player is trying to build on.
// <context.new_material> returns the MaterialTag of the block the player is trying to build.
//
// @Determine
// "BUILDABLE" to allow the building.
//
// -->
public BlockBuiltScriptEvent() {
instance = this;
}
public static BlockBuiltScriptEvent instance;
public LocationTag location;
public MaterialTag old_material;
public MaterialTag new_material;
public BlockCanBuildEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("being") && path.eventArgLowerAt(2).equals("built");
}
@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}
String mat1 = path.eventArgLowerAt(0);
if (!tryMaterial(new_material, mat1)) {
return false;
}
String mat2 = path.eventArgLowerAt(4);
if (mat2.length() > 0 && !tryMaterial(old_material, mat2)) {
return false;
}
return true;
}
@Override
public String getName() {
return "BlockBuilt";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.equals("buildable")) {
cancelled = false;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("location")) {
return location;
}
else if (name.equals("new_material")) {
return new_material;
}
else if (name.equals("old_material")) {
return old_material;
}
return super.getContext(name);
}
@EventHandler
public void onBlockBuilt(BlockCanBuildEvent event) {
location = new LocationTag(event.getBlock().getLocation());
old_material = new MaterialTag(event.getBlock());
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
new_material = new MaterialTag(new ModernBlockData(event.getBlockData()));
}
else {
new_material = new MaterialTag(event.getMaterial());
}
cancelled = !event.isBuildable();
this.event = event;
fire(event);
event.setBuildable(!cancelled);
}
}