-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
MythicMobsDeathEvent.java
152 lines (138 loc) · 5.12 KB
/
MythicMobsDeathEvent.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
package com.denizenscript.depenizen.bukkit.events.mythicmobs;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.depenizen.bukkit.objects.mythicmobs.MythicMobsMobTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import io.lumine.xikage.mythicmobs.api.bukkit.events.MythicMobDeathEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class MythicMobsDeathEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// mythicmob mob dies
// mythicmob mob death
// mythicmob mob killed
// mythicmob <mob> dies
// mythicmob <mob> death
// mythicmob <mob> killed
//
// @Regex ^on mythicmob [^\s]+ (dies|death|killed)$
//
// @Location true
// @Switch by:<entity> to only process the event if the killer matches a specified entity type.
//
// @Triggers when a MythicMob dies.
//
// @Context
// <context.mob> Returns the MythicMob that has been killed.
// <context.entity> Returns the EntityTag for the MythicMob.
// <context.killer> returns the EntityTag that killed the MythicMob (if available).
// <context.level> Returns the level of the MythicMob.
// <context.drops> Returns a list of items dropped.
//
// @Determine
// ListTag(ItemTag) to specify new items to be dropped.
//
// @Plugin Depenizen, MythicMobs
//
// @Group Depenizen
//
// -->
public MythicMobsDeathEvent() {
instance = this;
}
public static MythicMobsDeathEvent instance;
public MythicMobDeathEvent event;
public MythicMobsMobTag mythicmob;
public EntityTag entity;
public EntityTag killer;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("mythicmob")) {
return false;
}
String cmd = path.eventArgLowerAt(2);
if (!cmd.equals("death") && !cmd.equals("dies") && !cmd.equals("killed")) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String mob = path.eventArgLowerAt(1);
if (!mob.equals("mob") && !runGenericCheck(mob, mythicmob.getMobType().getInternalName())) {
return false;
}
if ((path.eventArgLowerAt(3).equals("by")) && !tryEntity(killer, path.eventArgLowerAt(4))) {
return false;
}
if (path.switches.containsKey("by") && !tryEntity(killer, path.switches.get("by"))) {
return false;
}
if (!runInCheck(path, entity.getLocation()) && !runInCheck(path, killer.getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "MythicMobsDeath";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
if (Argument.valueOf(determination).matchesArgumentList(ItemTag.class)) {
List<ItemStack> newDrops = new ArrayList<>();
for (ItemTag item : ListTag.valueOf(determination, getTagContext(path)).filter(ItemTag.class, path.container, true)) {
if (item != null) {
newDrops.add(item.getItemStack());
}
}
event.setDrops(newDrops);
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(killer.isPlayer() ? killer.getDenizenPlayer() : null, killer.isNPC() ? killer.getDenizenNPC() : null);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "mob":
return mythicmob;
case "killer":
return killer;
case "entity":
return entity;
case "drops":
ListTag drops = new ListTag();
for (ItemStack i : event.getDrops()) {
drops.addObject(new ItemTag(i));
}
return drops;
case "level":
return new ElementTag(event.getMobLevel());
}
return super.getContext(name);
}
@EventHandler
public void onMythicMobDeath(MythicMobDeathEvent event) {
mythicmob = new MythicMobsMobTag(event.getMob());
entity = new EntityTag(event.getEntity());
EntityTag.rememberEntity(entity.getBukkitEntity());
killer = new EntityTag(event.getKiller());
this.event = event;
fire(event);
EntityTag.forgetEntity(entity.getBukkitEntity());
}
}