-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PlayerFishesScriptEvent.java
142 lines (127 loc) · 4.94 KB
/
PlayerFishesScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent;
public class PlayerFishesScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player fishes (<entity>/<item>) (while <state>)
//
// @Regex ^on player fishes( [^\s]+)?( while [^\s]+)?$
// @Switch in <area>
//
// @Cancellable true
//
// @Triggers when a player uses a fishing rod.
//
// @Context
// <context.hook> returns a EntityTag of the hook.
// <context.state> returns an ElementTag of the fishing state. Valid states: <@link url https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/PlayerFishEvent.State.html>
// <context.entity> returns a EntityTag of the entity that got caught.
// <context.item> returns a ItemTag of the item gotten, if any.
//
// -->
public PlayerFishesScriptEvent() {
instance = this;
}
public static PlayerFishesScriptEvent instance;
public EntityTag hook;
public ElementTag state;
public EntityTag entity;
public ItemTag item;
public PlayerFishEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.toLowerCase(s).startsWith("player fishes");
}
@Override
public boolean matches(ScriptPath path) {
String fish = path.eventArgLowerAt(2);
if (!fish.isEmpty() && !fish.equals("in") && !fish.equals("while")) {
if (entity == null) {
return false;
}
if (!tryEntity(entity, fish)) {
if (item == null) {
return false;
}
if (!tryItem(item, fish)) {
return false;
}
}
}
String[] data = path.eventArgsLower;
for (int index = 2; index < data.length; index++) {
if (data[index].equals("while") && !data[index + 1].equalsIgnoreCase(state.asString())) {
return false;
}
}
if (!runInCheck(path, hook.getLocation())) {
return false;
}
return true;
}
@Override
public String getName() {
return "PlayerFishes";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(EntityTag.isPlayer(event.getPlayer()) ? EntityTag.getPlayerFrom(event.getPlayer()) :
EntityTag.isPlayer(event.getCaught()) ? EntityTag.getPlayerFrom(event.getCaught()) : null,
EntityTag.isCitizensNPC(event.getPlayer()) ? EntityTag.getNPCFrom(event.getPlayer()) :
EntityTag.isCitizensNPC(event.getCaught()) ? EntityTag.getNPCFrom(event.getCaught()) : null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("hook")) {
return hook;
}
else if (name.equals("entity") && entity != null) {
return entity.getDenizenObject();
}
else if (name.equals("item") && item != null) {
return item;
}
else if (name.equals("state")) {
return state;
}
return super.getContext(name);
}
@EventHandler
public void onPlayerFishes(PlayerFishEvent event) {
if (EntityTag.isNPC(event.getPlayer())) {
return;
}
Entity hookEntity = NMSHandler.getInstance().getEntityHelper().getFishHook(event);
EntityTag.rememberEntity(hookEntity);
hook = new EntityTag(hookEntity);
state = new ElementTag(event.getState().toString());
item = null;
entity = null;
Entity caughtEntity = event.getCaught();
if (caughtEntity != null) {
EntityTag.rememberEntity(caughtEntity);
entity = new EntityTag(caughtEntity);
if (caughtEntity instanceof Item) {
item = new ItemTag(((Item) caughtEntity).getItemStack());
}
}
this.event = event;
fire(event);
EntityTag.forgetEntity(hookEntity);
EntityTag.forgetEntity(caughtEntity);
}
}