/
BlockBuiltScriptEvent.java
108 lines (97 loc) · 3.78 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
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
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.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
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 <block>)
//
// @Group Block
//
// @Location true
//
// @Cancellable true
//
// @Triggers when an attempt is made to build a block on another block. Not necessarily caused by players. Does not normally fire when players place blocks. Prefer <@link event player places block> for that.
//
// @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.
//
// @Player when the event is triggered in relation to a player that is causing the block build.
//
// -->
public BlockBuiltScriptEvent() {
registerCouldMatcher("<block> being built (on <block>)");
}
public LocationTag location;
public MaterialTag old_material;
public MaterialTag new_material;
public BlockCanBuildEvent event;
@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}
String mat2 = path.eventArgLowerAt(4);
if (mat2.length() > 0 && !old_material.tryAdvancedMatcher(mat2)) {
return false;
}
if (!new_material.tryAdvancedMatcher(path.eventArgLowerAt(0))) {
return false;
}
return super.matches(path);
}
@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 true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@Override
public void cancellationChanged() {
event.setBuildable(!cancelled);
super.cancellationChanged();
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "new_material": return new_material;
case "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());
new_material = new MaterialTag(event.getBlockData());
cancelled = !event.isBuildable();
this.event = event;
fire(event);
}
}