/
DenizenNPCHelper.java
172 lines (157 loc) · 5.39 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
package com.denizenscript.denizen.npc;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.entity.EntityDespawnScriptEvent;
import com.denizenscript.denizen.npc.actions.ActionHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
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 {
private ActionHandler actionHandler;
public DenizenNPCHelper() {
if (Depends.citizens != null) {
Bukkit.getPluginManager().registerEvents(this, Denizen.getInstance());
INVENTORY_TRAIT_VIEW = ReflectionHelper.getFields(net.citizensnpcs.api.trait.trait.Inventory.class).get("view");
}
actionHandler = new ActionHandler();
}
/**
* Gets the currently loaded instance of the ActionHandler
*
* @return ActionHandler
*/
public ActionHandler getActionHandler() {
return actionHandler;
}
/**
* 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 {
NPCTag npcTag = new NPCTag(npc);
Inventory inv = (Inventory) INVENTORY_TRAIT_VIEW.get(npcTag.getInventoryTrait());
if (inv != null) {
return inv;
}
else {
// TODO: ???
Inventory npcInventory = Bukkit.getServer().createInventory(npcTag, InventoryType.PLAYER);
npcInventory.setContents(Arrays.copyOf(npcTag.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();
new NPCTag(npc).action("remove", null);
}
@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));
}
}
}
}