/
PlayerStepsOnSmartEvent.java
124 lines (107 loc) · 4.85 KB
/
PlayerStepsOnSmartEvent.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
package net.aufdemrand.denizen.events.core;
import net.aufdemrand.denizen.events.EventManager;
import net.aufdemrand.denizen.events.SmartEvent;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PlayerStepsOnSmartEvent implements SmartEvent, 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 player steps on (m@)?\\w+( 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 Player Steps On SmartEvent.");
}
@Override
public void breakDown() {
PlayerMoveEvent.getHandlerList().unregister(this);
PlayerTeleportEvent.getHandlerList().unregister(this);
}
//////////////
// MECHANICS
///////////
// <--[event]
// @Events
// player steps on block (in <notable cuboid>)
// player steps on <material> (in <notable cuboid>)
//
// @Warning This event may fire very rapidly.
//
// @Regex on player steps on (m@)?\w+( in (cu@)?\w+)?
//
// @Triggers when a player steps onto a material.
// @Context
// <context.location> returns a dLocation of the block the player is stepping on.
// <context.cuboids> returns a dList of all cuboids the player is inside.
// <context.previous_location> returns a dLocation of where the player was before stepping onto the block.
// <context.new_location> returns a dLocation of where the player is now.
//
// @Determine
// "CANCELLED" to move the player back off the block (useless if the player jumped onto it!)
//
// -->
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
onPlayerMove(new PlayerMoveEvent(event.getPlayer(), event.getFrom(), event.getTo()));
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
// Check that the block position changed (X/Y/Z)
if ((event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ())
// And the the block stood on is not air
&& event.getTo().clone().subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
// Required event lists
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.getTo().clone().subtract(0, 1, 0));
dMaterial mat = dMaterial.getMaterialFrom(block.getBlock().getType(), block.getBlock().getData());
context.put("location", block);
context.put("previous_location", new dLocation(event.getFrom()));
context.put("new_location", new dLocation(event.getTo()));
events.add("player steps on block");
events.add("player steps on " + mat.identifySimple());
// Add all relevant cuboids
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(block);
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("player steps on block in " + cuboid.identifySimple());
events.add("player steps on " + mat.identifySimple() + " 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 = EventManager.doEvents(events, null, new dPlayer(event.getPlayer()), context, true);
if (Determination.equalsIgnoreCase("CANCELLED"))
event.setCancelled(true);
}
}
}