/
BlockPhysicsSmartEvent.java
107 lines (91 loc) · 3.74 KB
/
BlockPhysicsSmartEvent.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
package net.aufdemrand.denizen.events.core;
import net.aufdemrand.denizen.scripts.containers.core.BukkitWorldScriptHelper;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BlockPhysicsSmartEvent implements OldSmartEvent, Listener {
///////////////////
// SMARTEVENT METHODS
///////////////
@Override
public boolean shouldInitialize(Set<String> events) {
// Loop through event names from loaded world script events
for (String event : events) {
// Use a regex pattern to narrow down matches
Matcher m = Pattern.compile("on (m@)?\\w+ physics( in (cu@)?\\w+)?", Pattern.CASE_INSENSITIVE)
.matcher(event);
if (m.matches()) {
// Event names are simple enough to just go ahead and pass on any match.
return true;
}
}
// No matches at all, just fail.
return false;
}
@Override
public void _initialize() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
dB.log("Loaded Block Physics SmartEvent.");
}
@Override
public void breakDown() {
BlockPhysicsEvent.getHandlerList().unregister(this);
}
//////////////
// MECHANICS
///////////
// <--[event]
// @Events
// block physics (in <notable cuboid>)
// <material> physics (in <notable cuboid>)
//
// @Warning This event may fire very rapidly.
//
// @Regex on (m@)?\w+ physics( in (cu@)?\w+)?
//
// @Triggers when a block's physics update.
// @Context
// <context.location> returns a dLocation of the block the physics is affecting.
// <context.new_material> returns a dMaterial of what the block is becoming.
//
// @Determine
// "CANCELLED" to stop the physics from occuring.
//
// -->
@EventHandler
public void onBlockPhysics(BlockPhysicsEvent event) {
List<String> events = new ArrayList<String>();
Map<String, dObject> context = new HashMap<String, dObject>();
// Add the default location context + default event
dLocation block = new dLocation(event.getBlock().getLocation());
dMaterial mat = dMaterial.getMaterialFrom(block.getBlock().getType(), block.getBlock().getData());
context.put("location", block);
context.put("new_material", dMaterial.getMaterialFrom(event.getChangedType()));
events.add("block physics");
events.add(mat.identifySimple() + " physics");
// Add all relevant cuboids
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(block);
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("block physics in " + cuboid.identifySimple());
events.add(mat.identifySimple() + " physics in " + cuboid.identifySimple());
cuboid_context.add(cuboid.identify());
}
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
// Fire event
String Determination = BukkitWorldScriptHelper.doEvents(events, null, null, context, true);
if (Determination.equalsIgnoreCase("CANCELLED"))
event.setCancelled(true);
}
}