/
DenizenNPCHelper.java
196 lines (177 loc) · 6.18 KB
/
DenizenNPCHelper.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
package com.denizenscript.denizen.npc;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.npc.actions.ActionHandler;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.events.OldEventManager;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import java.lang.reflect.Field;
import java.util.*;
public class DenizenNPCHelper implements Listener {
public static DenizenNPCHelper getCurrentInstance() {
return DenizenAPI.getCurrentInstance().getNPCHelper();
}
private Denizen plugin;
private ActionHandler actionHandler;
public DenizenNPCHelper(Denizen denizen) {
plugin = denizen;
if (Depends.citizens != null) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
INVENTORY_TRAIT_VIEW = ReflectionHelper.getFields(net.citizensnpcs.api.trait.trait.Inventory.class).get("view");
}
actionHandler = new ActionHandler(plugin);
}
/**
* Gets the currently loaded instance of the ActionHandler
*
* @return ActionHandler
*/
public ActionHandler getActionHandler() {
return actionHandler;
}
/**
* Returns a NPCTag object when given a valid NPC. DenizenNPCs have some methods
* specific to Denizen functionality as well as easy access to the attached NPC and LivingEntity.
*
* @param npc the Citizens NPC
* @return a NPCTag
*/
public static NPCTag getDenizen(NPC npc) {
return new NPCTag(npc);
}
public static NPCTag getDenizen(int id) {
return new NPCTag(CitizensAPI.getNPCRegistry().getById(id));
}
/**
* Returns a InventoryTag object from the Inventory trait of a valid NPC.
*
* @param npc the Citizens NPC
* @return the NPC's InventoryTag
*/
public static Inventory getInventory(NPC npc) {
if (npc == null) {
return null;
}
/*if (!npcInventories.containsKey(npc.getId())) {
_registerNPC(npc);
}
return npcInventories.get(npc.getId());*/
if (npc.isSpawned() && npc.getEntity() instanceof InventoryHolder) {
return ((InventoryHolder) npc.getEntity()).getInventory();
}
else {
try {
Inventory inv = (Inventory) INVENTORY_TRAIT_VIEW.get(getDenizen(npc).getInventoryTrait());
if (inv != null) {
return inv;
}
else {
// TODO: ???
Inventory npcInventory = Bukkit.getServer().createInventory(getDenizen(npc), InventoryType.PLAYER);
npcInventory.setContents(Arrays.copyOf(getDenizen(npc).getInventoryTrait().getContents(), npcInventory.getSize()));
return npcInventory;
}
}
catch (Exception e) {
Debug.echoError(e);
return null;
}
}
}
public static Field INVENTORY_TRAIT_VIEW;
// <--[action]
// @Actions
// spawn
// @Triggers when the NPC is spawned.
// This will fire whenever an NPC's chunk is loaded, or a spawn command is issued.
//
// @Context
// None
// -->
/**
* Fires the 'On Spawn:' action in an NPCs Assignment, if set.
*
* @param event NPCSpawnEvent
*/
@EventHandler
public void onSpawn(NPCSpawnEvent event) {
if (event.getNPC() == null) {
Debug.echoError("Null NPC spawned!");
return;
}
// On Spawn action
new NPCTag(event.getNPC()).action("spawn", null);
}
// <--[action]
// @Actions
// despawn
// @Triggers when the NPC is despawned.
// This can be because a command was issued, or a chunk has been unloaded.
//
// @Context
// None
// -->
/**
* Fires a world script event and then NPC action when the NPC despawns.
*
* @param event NPCDespawnEvent
*/
@EventHandler
public void despawn(NPCDespawnEvent event) {
NPCTag npc = new NPCTag(event.getNPC());
if (npc.isValid()) {
EntityDespawnScriptEvent.instance.entity = new EntityTag(event.getNPC().getEntity());
EntityDespawnScriptEvent.instance.cause = new ElementTag("CITIZENS");
EntityDespawnScriptEvent.instance.fire(event);
npc.action("despawn", null);
}
}
// <--[action]
// @Actions
// remove
// @Triggers when the NPC is removed.
//
// @Context
// None
// -->
/**
* Removes an NPC from the Registry when removed from Citizens.
*
* @param event NPCRemoveEvent
*/
@EventHandler
public void onRemove(NPCRemoveEvent event) {
NPC npc = event.getNPC();
getDenizen(npc).action("remove", null);
FlagManager.clearNPCFlags(npc.getId());
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Inventory inventory = event.getInventory();
if (inventory.getHolder() instanceof NPCTag) {
NPCTag npc = (NPCTag) inventory.getHolder();
npc.getInventory().setContents(inventory.getContents());
Equipment equipment = npc.getEquipmentTrait();
for (int i = 0; i < 5; i++) {
equipment.set(i, inventory.getItem(i));
}
}
}
}