-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PushableTrait.java
210 lines (193 loc) · 6.85 KB
/
PushableTrait.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.minecraft.server.v1_8_R3.EntityLiving;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
// <--[language]
// @name Pushable Trait
// @group NPC Traits
// @description
// By default, NPCs created will allow players to 'noclip' them, or go right through. This is to
// avoid NPCs moving from their set location, but often times, this behavior may be undesired.
// The pushable trait allows NPCs to move when collided with, and optionally return to their
// original location after a specified amount of time.
//
// To enable the trait, use the '/npc pushable' command on any selected NPC. Once the trait is
// enabled, the '-r' option can be used to toggle returnable, and the '--delay #' option can be
// used to specify the number of seconds before the npc returns.
//
// Care should be taken when allowing NPCs to be pushable. Allowing NPCs to be pushed around
// complex structures can result in stuck NPCs. If the NPC is stuck, it may not return. Keeping
// a small delay, in situations like this, can be a good trade-off. Typically the lower the
// delay, the shorter distance a Player is able to push the NPC. The default delay is 2 seconds.
//
// The pushable trait also implements some actions that can be used in assignment scripts.
// This includes 'on push' and 'on push return'.
// -->
public class PushableTrait extends Trait implements Listener {
// Saved to the C2 saves.yml
@Persist("toggle")
private boolean pushable = true;
@Persist("returnable")
private boolean returnable = false;
@Persist("delay")
private int delay = 2;
// Used internally
private boolean pushed = false;
private Location returnLocation = null;
private long pushedTimer = 0;
public PushableTrait() {
super("pushable");
}
/**
* Gets the delay, as set by {@link #setDelay(int)}.
*
* @return delay, in seconds
*/
public int getDelay() {
return delay;
}
/**
* Checks if this NPCs pushable setting is toggled.
*
* @return true if pushable
*/
public boolean isPushable() {
return pushable;
}
/**
* Checks if this NPC is returnable when pushed.
* Note: Does not take into account whether the NPC
* is pushable, use {@link #isPushable()}
*
* @return true if returnable
*/
public boolean isReturnable() {
return returnable;
}
/**
* Sets the delay, in seconds, in which the NPC will
* return to its position after being pushed.
* Note: Must be pushable.
*
* @param delay time in seconds to return after being pushed
*/
public void setDelay(int delay) {
this.delay = delay;
}
/**
* Indicates that the NPC should be pushable. By default,
* C2 NPCs are not pushable.
*
* @param pushable whether the NPC should be pushable
*/
public void setPushable(boolean pushable) {
this.pushable = pushable;
}
/**
* Indicates that the NPC should return to its location
* after being pushed. Takes into account a delay which
* can be set with {@link #setDelay(int)} and
* checked with {@link #getDelay()}}.
*
* @param returnable whether the NPC can return
*/
public void setReturnable(boolean returnable) {
this.returnable = returnable;
}
/**
* Toggles the NPCs current pushable setting.
*
* @return {@link #isPushable()} after setting
*/
public boolean toggle() {
pushable = !pushable;
if (!pushable) returnable = false;
return pushable;
}
// <--[action]
// @Actions
// push
//
// @Triggers when the NPC is pushed by a player
//
// @Context
// None
//
// -->
/**
* Fires an 'On Push:' action upon being pushed.
*
*/
@EventHandler
public void NPCPush (NPCPushEvent event) {
if (event.getNPC() == npc && pushable) {
event.setCancelled(false);
// On Push action / Push Trigger
if (System.currentTimeMillis() > pushedTimer) {
// Get pusher
Player pusher = null;
for (Entity le : event.getNPC().getEntity().getNearbyEntities(1, 1, 1))
if (le instanceof Player) pusher = (Player) le;
if (pusher != null) {
DenizenAPI.getDenizenNPC(npc).action("push", dPlayer.mirrorBukkitPlayer(pusher));
pushedTimer = System.currentTimeMillis() + (delay * 1000);
}
} // End push action
if (!pushed && returnable) {
pushed = true;
returnLocation = npc.getEntity().getLocation().clone();
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(
DenizenAPI.getCurrentInstance(), new Runnable() {
@Override public void run() { navigateBack(); } }, delay * 20);
}
}
}
// <--[action]
// @Actions
// push return
//
// @Triggers when the NPC returns to its center after being pushed by a player.
//
// @Context
// None
//
// -->
/**
* Fires a 'On Push Return:' action upon return after being pushed.
*
*/
@EventHandler
public void NPCCompleteDestination (NavigationCompleteEvent event) {
if (event.getNPC() == npc && pushed) {
EntityLiving handle = ((CraftLivingEntity) npc.getEntity()).getHandle();
handle.yaw = returnLocation.getYaw();
handle.pitch = returnLocation.getPitch();
// !--- START NMS OBFUSCATED
handle.az = handle.yaw; // The head's yaw
// !--- END NMS OBFUSCATED
pushed = false;
// Push Return action
DenizenAPI.getDenizenNPC(npc).action("push return", null);
}
}
protected void navigateBack() {
if (npc.getNavigator().isNavigating()) {
pushed = false;
} else if (pushed) {
pushed = false; // Avoids NPCCompleteDestination from triggering
npc.getNavigator().setTarget(returnLocation);
pushed = true;
}
}
}