-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TabCompleteScriptEvent.java
115 lines (102 loc) · 3.73 KB
/
TabCompleteScriptEvent.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
package com.denizenscript.denizen.events.server;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.TabCompleteEvent;
public class TabCompleteScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// tab complete
//
// @Regex ^on tab complete$
//
// @Group Server
//
// @Cancellable true
//
// @Triggers when a player or the console is sent a list of available tab completions.
//
// @Context
// <context.buffer> returns the full raw command buffer.
// <context.command> returns the command name.
// <context.current_arg> returns the current argument for completion.
// <context.completions> returns a list of available tab completions.
// <context.server> returns true if the tab completion was triggered from the console.
//
// @Determine
// ListTag to set the list of available tab completions.
//
// @Player when the tab completion is done by a player.
//
// -->
public TabCompleteScriptEvent() {
instance = this;
}
public static TabCompleteScriptEvent instance;
public TabCompleteEvent event;
public String getCommand() {
String[] args = event.getBuffer().trim().split(" ");
String cmd = args.length > 0 ? args[0] : "";
if (event.getSender() instanceof Player) {
cmd = cmd.replaceFirst("/", "");
}
return cmd;
}
public String getCurrentArg() {
int i = event.getBuffer().lastIndexOf(' ');
return i > 0 ? event.getBuffer().substring(i + 1) : getCommand();
}
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("tab complete")) {
return false;
}
return true;
}
@Override
public String getName() {
return "TabComplete";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
if (determination.length() > 0 && !isDefaultDetermination(determinationObj)) {
event.setCompletions(ListTag.valueOf(determination, getTagContext(path)));
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getSender() instanceof Player ? new PlayerTag((Player) event.getSender()) : null, null);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "buffer":
return new ElementTag(event.getBuffer());
case "command":
return new ElementTag(getCommand());
case "current_arg":
return new ElementTag(getCurrentArg());
case "completions":
return new ListTag(event.getCompletions());
case "server":
return new ElementTag(event.getSender() instanceof ConsoleCommandSender);
}
return super.getContext(name);
}
@EventHandler
public void onTabComplete(TabCompleteEvent event) {
this.event = event;
fire(event);
}
}