|
1 | | -package com.laytonsmith.commandhelper; |
2 | | - |
3 | | -import com.laytonsmith.abstraction.MCPlayer; |
4 | | -import com.laytonsmith.abstraction.StaticLayer; |
5 | | -import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer; |
6 | | -import com.laytonsmith.abstraction.enums.MCChatColor; |
7 | | -import com.laytonsmith.core.MethodScriptCompiler; |
8 | | -import com.laytonsmith.core.MethodScriptComplete; |
9 | | -import com.laytonsmith.core.ParseTree; |
10 | | -import com.laytonsmith.core.Static; |
11 | | -import com.laytonsmith.core.compiler.TokenStream; |
12 | | -import com.laytonsmith.core.constructs.Token; |
13 | | -import com.laytonsmith.core.environments.CommandHelperEnvironment; |
14 | | -import com.laytonsmith.core.environments.Environment; |
15 | | -import com.laytonsmith.core.environments.GlobalEnv; |
16 | | -import com.laytonsmith.core.exceptions.CancelCommandException; |
17 | | -import com.laytonsmith.core.exceptions.ConfigCompileException; |
18 | | -import com.laytonsmith.core.exceptions.ConfigCompileGroupException; |
19 | | -import com.laytonsmith.core.exceptions.ConfigRuntimeException; |
20 | | -import com.laytonsmith.core.taskmanager.TaskManager; |
21 | | -import org.bukkit.event.EventHandler; |
22 | | -import org.bukkit.event.EventPriority; |
23 | | -import org.bukkit.event.Listener; |
24 | | -import org.bukkit.event.player.AsyncPlayerChatEvent; |
25 | | -import org.bukkit.event.player.PlayerCommandPreprocessEvent; |
26 | | -import org.bukkit.event.player.PlayerQuitEvent; |
27 | | - |
28 | | -import java.io.File; |
29 | | -import java.util.Collections; |
30 | | -import java.util.HashMap; |
31 | | -import java.util.HashSet; |
32 | | -import java.util.List; |
33 | | -import java.util.Map; |
34 | | -import java.util.Set; |
35 | | -import java.util.logging.Level; |
36 | | -import java.util.logging.Logger; |
37 | | - |
38 | | -/** |
39 | | - * |
40 | | - * |
41 | | - */ |
42 | | -public class CommandHelperInterpreterListener implements Listener { |
43 | | - |
44 | | - private Set<String> interpreterMode = Collections.synchronizedSet(new HashSet<String>()); |
45 | | - private CommandHelperPlugin plugin; |
46 | | - Map<String, String> multilineMode = new HashMap<String, String>(); |
47 | | - |
48 | | - public boolean isInInterpreterMode(String player) { |
49 | | - return (interpreterMode.contains(player)); |
50 | | - } |
51 | | - |
52 | | - public CommandHelperInterpreterListener(CommandHelperPlugin plugin) { |
53 | | - this.plugin = plugin; |
54 | | - } |
55 | | - |
56 | | - @EventHandler(priority = EventPriority.LOWEST) |
57 | | - public void onPlayerChat(final AsyncPlayerChatEvent event) { |
58 | | - if (interpreterMode.contains(event.getPlayer().getName())) { |
59 | | - final MCPlayer p = new BukkitMCPlayer(event.getPlayer()); |
60 | | - event.setCancelled(true); |
61 | | - StaticLayer.SetFutureRunnable(null, 0, new Runnable() { |
62 | | - |
63 | | - @Override |
64 | | - public void run() { |
65 | | - textLine(p, event.getMessage()); |
66 | | - } |
67 | | - }); |
68 | | - } |
69 | | - |
70 | | - } |
71 | | - |
72 | | - @EventHandler(priority = EventPriority.NORMAL) |
73 | | - public void onPlayerQuit(PlayerQuitEvent event) { |
74 | | - interpreterMode.remove(event.getPlayer().getName()); |
75 | | - multilineMode.remove(event.getPlayer().getName()); |
76 | | - } |
77 | | - |
78 | | - @EventHandler(priority = EventPriority.LOWEST) |
79 | | - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { |
80 | | - if (event.isCancelled()) { |
81 | | - return; |
82 | | - } |
83 | | - if (interpreterMode.contains(event.getPlayer().getName())) { |
84 | | - MCPlayer p = new BukkitMCPlayer(event.getPlayer()); |
85 | | - textLine(p, event.getMessage()); |
86 | | - event.setCancelled(true); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - public void textLine(MCPlayer p, String line) { |
91 | | - if (line.equals("-")) { |
92 | | - //Exit interpreter mode |
93 | | - interpreterMode.remove(p.getName()); |
94 | | - Static.SendMessage(p, MCChatColor.YELLOW + "Now exiting interpreter mode"); |
95 | | - } else if (line.equals(">>>")) { |
96 | | - //Start multiline mode |
97 | | - if (multilineMode.containsKey(p.getName())) { |
98 | | - Static.SendMessage(p, MCChatColor.RED + "You are already in multiline mode!"); |
99 | | - } else { |
100 | | - multilineMode.put(p.getName(), ""); |
101 | | - Static.SendMessage(p, MCChatColor.YELLOW + "You are now in multiline mode. Type <<< on a line by itself to execute."); |
102 | | - Static.SendMessage(p, ":" + MCChatColor.GRAY + ">>>"); |
103 | | - } |
104 | | - } else if (line.equals("<<<")) { |
105 | | - //Execute multiline |
106 | | - Static.SendMessage(p, ":" + MCChatColor.GRAY + "<<<"); |
107 | | - String script = multilineMode.get(p.getName()); |
108 | | - multilineMode.remove(p.getName()); |
109 | | - try { |
110 | | - execute(script, p); |
111 | | - } catch (ConfigCompileException e) { |
112 | | - Static.SendMessage(p, MCChatColor.RED + e.getMessage() + ":" + e.getLineNum()); |
113 | | - } catch (ConfigCompileGroupException ex) { |
114 | | - for (ConfigCompileException e : ex.getList()) { |
115 | | - Static.SendMessage(p, MCChatColor.RED + e.getMessage() + ":" + e.getLineNum()); |
116 | | - } |
117 | | - } |
118 | | - } else { |
119 | | - if (multilineMode.containsKey(p.getName())) { |
120 | | - //Queue multiline |
121 | | - multilineMode.put(p.getName(), multilineMode.get(p.getName()) + line + "\n"); |
122 | | - Static.SendMessage(p, ":" + MCChatColor.GRAY + line); |
123 | | - } else { |
124 | | - try { |
125 | | - //Execute single line |
126 | | - execute(line, p); |
127 | | - } catch (ConfigCompileException ex) { |
128 | | - Static.SendMessage(p, MCChatColor.RED + ex.getMessage()); |
129 | | - } catch (ConfigCompileGroupException e) { |
130 | | - for (ConfigCompileException ex : e.getList()) { |
131 | | - Static.SendMessage(p, MCChatColor.RED + ex.getMessage()); |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - public void reload() { |
139 | | - } |
140 | | - |
141 | | - public void execute(String script, final MCPlayer p) throws ConfigCompileException, ConfigCompileGroupException { |
142 | | - TokenStream stream = MethodScriptCompiler.lex(script, new File("Interpreter"), true); |
143 | | - ParseTree tree = MethodScriptCompiler.compile(stream); |
144 | | - interpreterMode.remove(p.getName()); |
145 | | - GlobalEnv gEnv = new GlobalEnv(plugin.executionQueue, plugin.profiler, plugin.persistenceNetwork, |
146 | | - CommandHelperFileLocations.getDefault().getConfigDirectory(), |
147 | | - plugin.profiles, new TaskManager()); |
148 | | - gEnv.SetDynamicScriptingMode(true); |
149 | | - CommandHelperEnvironment cEnv = new CommandHelperEnvironment(); |
150 | | - cEnv.SetPlayer(p); |
151 | | - Environment env = Environment.createEnvironment(gEnv, cEnv); |
152 | | - try { |
153 | | - MethodScriptCompiler.registerAutoIncludes(env, null); |
154 | | - MethodScriptCompiler.execute(tree, env, new MethodScriptComplete() { |
155 | | - |
156 | | - @Override |
157 | | - public void done(String output) { |
158 | | - output = output.trim(); |
159 | | - if (output.isEmpty()) { |
160 | | - Static.SendMessage(p, ":"); |
161 | | - } else { |
162 | | - if (output.startsWith("/")) { |
163 | | - //Run the command |
164 | | - Static.SendMessage(p, ":" + MCChatColor.YELLOW + output); |
165 | | - p.chat(output); |
166 | | - } else { |
167 | | - //output the results |
168 | | - Static.SendMessage(p, ":" + MCChatColor.GREEN + output); |
169 | | - } |
170 | | - } |
171 | | - interpreterMode.add(p.getName()); |
172 | | - } |
173 | | - }, null); |
174 | | - } catch (CancelCommandException e) { |
175 | | - interpreterMode.add(p.getName()); |
176 | | - } catch (ConfigRuntimeException e) { |
177 | | - ConfigRuntimeException.HandleUncaughtException(e, env); |
178 | | - Static.SendMessage(p, MCChatColor.RED + e.toString()); |
179 | | - interpreterMode.add(p.getName()); |
180 | | - } catch (Exception e) { |
181 | | - Static.SendMessage(p, MCChatColor.RED + e.toString()); |
182 | | - Logger.getLogger(CommandHelperInterpreterListener.class.getName()).log(Level.SEVERE, null, e); |
183 | | - interpreterMode.add(p.getName()); |
184 | | - } |
185 | | - } |
186 | | - |
187 | | - public void startInterpret(String playername) { |
188 | | - interpreterMode.add(playername); |
189 | | - } |
190 | | -} |
| 1 | +package com.laytonsmith.commandhelper; |
| 2 | + |
| 3 | +import com.laytonsmith.abstraction.MCPlayer; |
| 4 | +import com.laytonsmith.abstraction.StaticLayer; |
| 5 | +import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer; |
| 6 | +import com.laytonsmith.abstraction.enums.MCChatColor; |
| 7 | +import com.laytonsmith.core.MethodScriptCompiler; |
| 8 | +import com.laytonsmith.core.MethodScriptComplete; |
| 9 | +import com.laytonsmith.core.ParseTree; |
| 10 | +import com.laytonsmith.core.Static; |
| 11 | +import com.laytonsmith.core.compiler.TokenStream; |
| 12 | +import com.laytonsmith.core.constructs.Token; |
| 13 | +import com.laytonsmith.core.environments.CommandHelperEnvironment; |
| 14 | +import com.laytonsmith.core.environments.Environment; |
| 15 | +import com.laytonsmith.core.environments.GlobalEnv; |
| 16 | +import com.laytonsmith.core.exceptions.CancelCommandException; |
| 17 | +import com.laytonsmith.core.exceptions.ConfigCompileException; |
| 18 | +import com.laytonsmith.core.exceptions.ConfigCompileGroupException; |
| 19 | +import com.laytonsmith.core.exceptions.ConfigRuntimeException; |
| 20 | +import com.laytonsmith.core.taskmanager.TaskManager; |
| 21 | +import org.bukkit.event.EventHandler; |
| 22 | +import org.bukkit.event.EventPriority; |
| 23 | +import org.bukkit.event.Listener; |
| 24 | +import org.bukkit.event.player.AsyncPlayerChatEvent; |
| 25 | +import org.bukkit.event.player.PlayerCommandPreprocessEvent; |
| 26 | +import org.bukkit.event.player.PlayerQuitEvent; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | + |
| 38 | +/** |
| 39 | + * |
| 40 | + * |
| 41 | + */ |
| 42 | +public class CommandHelperInterpreterListener implements Listener { |
| 43 | + |
| 44 | + private Set<String> interpreterMode = Collections.synchronizedSet(new HashSet<String>()); |
| 45 | + private CommandHelperPlugin plugin; |
| 46 | + Map<String, String> multilineMode = new HashMap<String, String>(); |
| 47 | + |
| 48 | + public boolean isInInterpreterMode(String player) { |
| 49 | + return (interpreterMode.contains(player)); |
| 50 | + } |
| 51 | + |
| 52 | + public CommandHelperInterpreterListener(CommandHelperPlugin plugin) { |
| 53 | + this.plugin = plugin; |
| 54 | + } |
| 55 | + |
| 56 | + @EventHandler(priority = EventPriority.LOWEST) |
| 57 | + public void onPlayerChat(final AsyncPlayerChatEvent event) { |
| 58 | + if (interpreterMode.contains(event.getPlayer().getName())) { |
| 59 | + final MCPlayer p = new BukkitMCPlayer(event.getPlayer()); |
| 60 | + event.setCancelled(true); |
| 61 | + StaticLayer.SetFutureRunnable(null, 0, new Runnable() { |
| 62 | + |
| 63 | + @Override |
| 64 | + public void run() { |
| 65 | + textLine(p, event.getMessage()); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @EventHandler(priority = EventPriority.NORMAL) |
| 73 | + public void onPlayerQuit(PlayerQuitEvent event) { |
| 74 | + interpreterMode.remove(event.getPlayer().getName()); |
| 75 | + multilineMode.remove(event.getPlayer().getName()); |
| 76 | + } |
| 77 | + |
| 78 | + @EventHandler(priority = EventPriority.LOWEST) |
| 79 | + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { |
| 80 | + if (event.isCancelled()) { |
| 81 | + return; |
| 82 | + } |
| 83 | + if (interpreterMode.contains(event.getPlayer().getName())) { |
| 84 | + MCPlayer p = new BukkitMCPlayer(event.getPlayer()); |
| 85 | + textLine(p, event.getMessage()); |
| 86 | + event.setCancelled(true); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void textLine(MCPlayer p, String line) { |
| 91 | + if (line.equals("-")) { |
| 92 | + //Exit interpreter mode |
| 93 | + interpreterMode.remove(p.getName()); |
| 94 | + Static.SendMessage(p, MCChatColor.YELLOW + "Now exiting interpreter mode"); |
| 95 | + } else if (line.equals(">>>")) { |
| 96 | + //Start multiline mode |
| 97 | + if (multilineMode.containsKey(p.getName())) { |
| 98 | + Static.SendMessage(p, MCChatColor.RED + "You are already in multiline mode!"); |
| 99 | + } else { |
| 100 | + multilineMode.put(p.getName(), ""); |
| 101 | + Static.SendMessage(p, MCChatColor.YELLOW + "You are now in multiline mode. Type <<< on a line by itself to execute."); |
| 102 | + Static.SendMessage(p, ":" + MCChatColor.GRAY + ">>>"); |
| 103 | + } |
| 104 | + } else if (line.equals("<<<")) { |
| 105 | + //Execute multiline |
| 106 | + Static.SendMessage(p, ":" + MCChatColor.GRAY + "<<<"); |
| 107 | + String script = multilineMode.get(p.getName()); |
| 108 | + multilineMode.remove(p.getName()); |
| 109 | + try { |
| 110 | + execute(script, p); |
| 111 | + } catch (ConfigCompileException e) { |
| 112 | + Static.SendMessage(p, MCChatColor.RED + e.getMessage() + ":" + e.getLineNum()); |
| 113 | + } catch (ConfigCompileGroupException ex) { |
| 114 | + for (ConfigCompileException e : ex.getList()) { |
| 115 | + Static.SendMessage(p, MCChatColor.RED + e.getMessage() + ":" + e.getLineNum()); |
| 116 | + } |
| 117 | + } |
| 118 | + } else { |
| 119 | + if (multilineMode.containsKey(p.getName())) { |
| 120 | + //Queue multiline |
| 121 | + multilineMode.put(p.getName(), multilineMode.get(p.getName()) + line + "\n"); |
| 122 | + Static.SendMessage(p, ":" + MCChatColor.GRAY + line); |
| 123 | + } else { |
| 124 | + try { |
| 125 | + //Execute single line |
| 126 | + execute(line, p); |
| 127 | + } catch (ConfigCompileException ex) { |
| 128 | + Static.SendMessage(p, MCChatColor.RED + ex.getMessage()); |
| 129 | + } catch (ConfigCompileGroupException e) { |
| 130 | + for (ConfigCompileException ex : e.getList()) { |
| 131 | + Static.SendMessage(p, MCChatColor.RED + ex.getMessage()); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public void reload() { |
| 139 | + } |
| 140 | + |
| 141 | + public void execute(String script, final MCPlayer p) throws ConfigCompileException, ConfigCompileGroupException { |
| 142 | + TokenStream stream = MethodScriptCompiler.lex(script, new File("Interpreter"), true); |
| 143 | + ParseTree tree = MethodScriptCompiler.compile(stream); |
| 144 | + interpreterMode.remove(p.getName()); |
| 145 | + GlobalEnv gEnv = new GlobalEnv(plugin.executionQueue, plugin.profiler, plugin.persistenceNetwork, |
| 146 | + CommandHelperFileLocations.getDefault().getConfigDirectory(), |
| 147 | + plugin.profiles, new TaskManager()); |
| 148 | + gEnv.SetDynamicScriptingMode(true); |
| 149 | + CommandHelperEnvironment cEnv = new CommandHelperEnvironment(); |
| 150 | + cEnv.SetPlayer(p); |
| 151 | + Environment env = Environment.createEnvironment(gEnv, cEnv); |
| 152 | + try { |
| 153 | + MethodScriptCompiler.registerAutoIncludes(env, null); |
| 154 | + MethodScriptCompiler.execute(tree, env, new MethodScriptComplete() { |
| 155 | + |
| 156 | + @Override |
| 157 | + public void done(String output) { |
| 158 | + output = output.trim(); |
| 159 | + if (output.isEmpty()) { |
| 160 | + Static.SendMessage(p, ":"); |
| 161 | + } else { |
| 162 | + if (output.startsWith("/")) { |
| 163 | + //Run the command |
| 164 | + Static.SendMessage(p, ":" + MCChatColor.YELLOW + output); |
| 165 | + p.chat(output); |
| 166 | + } else { |
| 167 | + //output the results |
| 168 | + Static.SendMessage(p, ":" + MCChatColor.GREEN + output); |
| 169 | + } |
| 170 | + } |
| 171 | + interpreterMode.add(p.getName()); |
| 172 | + } |
| 173 | + }, null); |
| 174 | + } catch (CancelCommandException e) { |
| 175 | + interpreterMode.add(p.getName()); |
| 176 | + } catch (ConfigRuntimeException e) { |
| 177 | + ConfigRuntimeException.HandleUncaughtException(e, env); |
| 178 | + Static.SendMessage(p, MCChatColor.RED + e.toString()); |
| 179 | + interpreterMode.add(p.getName()); |
| 180 | + } catch (Exception e) { |
| 181 | + Static.SendMessage(p, MCChatColor.RED + e.toString()); |
| 182 | + Logger.getLogger(CommandHelperInterpreterListener.class.getName()).log(Level.SEVERE, null, e); |
| 183 | + interpreterMode.add(p.getName()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public void startInterpret(String playername) { |
| 188 | + interpreterMode.add(playername); |
| 189 | + } |
| 190 | +} |
0 commit comments