-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
VillagerChangesProfessionScriptEvent.java
96 lines (84 loc) · 3.2 KB
/
VillagerChangesProfessionScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.VillagerCareerChangeEvent;
public class VillagerChangesProfessionScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// villager changes profession
//
// @Regex ^on villager changes profession$
//
// @Group Entity
//
// @Location true
//
// @Cancellable true
//
// @Triggers when a villager changes profession.
//
// @Context
// <context.entity> returns the EntityTag of the villager.
// <context.profession> returns the name of the new profession. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Villager.Profession.html>
// <context.reason> returns the reason for the change. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/VillagerCareerChangeEvent.ChangeReason.html>
//
// @Determine
// ElementTag to change the profession.
// -->
public VillagerChangesProfessionScriptEvent() {
instance = this;
}
public static VillagerChangesProfessionScriptEvent instance;
public EntityTag entity;
public VillagerCareerChangeEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("villager changes profession");
}
@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "VillagerChangesProfession";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (!isDefaultDetermination(determinationObj)) {
String determination = determinationObj.toString();
if (exactMatchesEnum(determination, Villager.Profession.values())) {
Villager.Profession newProfession = Villager.Profession.valueOf(determination.toUpperCase());
event.setProfession(newProfession);
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity;
case "reason":
return new ElementTag(event.getReason().toString());
case "profession":
return new ElementTag(event.getProfession().toString());
}
return super.getContext(name);
}
@EventHandler
public void onVillagerChangesProfession(VillagerCareerChangeEvent event) {
this.event = event;
this.entity = new EntityTag(event.getEntity());
fire(event);
}
}