Skip to content

Commit 2985a79

Browse files
committed
Some refactor
1 parent 7c68ec1 commit 2985a79

File tree

12 files changed

+164
-211
lines changed

12 files changed

+164
-211
lines changed

src/main/java/com/mengcraft/script/EventMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.google.common.collect.Multimap;
77
import com.google.common.collect.Sets;
88
import com.google.common.io.Files;
9-
import com.mengcraft.script.util.ArrayHelper;
9+
import com.mengcraft.script.util.Utils;
1010
import lombok.Data;
1111
import lombok.SneakyThrows;
1212
import lombok.val;
@@ -126,7 +126,7 @@ public Object filter(String regex) {// Export to scripts
126126
list.add(key);
127127
}
128128
});
129-
return ArrayHelper.toJSArray(list.toArray());
129+
return Utils.fromJava(list);
130130
}
131131

132132
protected static HandlerList getHandler(Mapping mapping) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.mengcraft.script;
2+
3+
import com.mengcraft.script.util.Utils;
4+
import lombok.RequiredArgsConstructor;
5+
import org.bukkit.ChatColor;
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandExecutor;
8+
import org.bukkit.command.CommandSender;
9+
10+
import javax.script.Bindings;
11+
import java.util.function.BiConsumer;
12+
13+
/**
14+
* Created on 16-10-17.
15+
*/
16+
@RequiredArgsConstructor
17+
public class HandledCommand implements CommandExecutor {
18+
19+
private final ScriptPlugin script;
20+
private final String label;
21+
private final BiConsumer<CommandSender, Bindings> executor;
22+
private final String permission;
23+
24+
public boolean execute(CommandSender sender, Bindings params) {
25+
if (permission == null || sender.hasPermission(permission)) {
26+
executor.accept(sender, params);
27+
return true;
28+
} else {
29+
sender.sendMessage(ChatColor.RED + "你没有执行权限");
30+
}
31+
return false;
32+
}
33+
34+
35+
public boolean remove() {
36+
return script.remove(this);
37+
}
38+
39+
public String getLabel() {
40+
return label;
41+
}
42+
43+
@Override
44+
public boolean onCommand(CommandSender sender, Command command, String label, String[] params) {
45+
return execute(sender, Utils.fromJava(params));
46+
}
47+
}

src/main/java/com/mengcraft/script/HandledExecutor.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/com/mengcraft/script/ScriptBootstrap.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import com.mengcraft.script.loader.ScriptLoader;
77
import com.mengcraft.script.loader.ScriptPluginException;
88
import com.mengcraft.script.plugin.ScriptingLoader;
9-
import com.mengcraft.script.util.ArrayHelper;
109
import com.mengcraft.script.util.BossBarWrapper;
1110
import com.mengcraft.script.util.Named;
1211
import lombok.RequiredArgsConstructor;
1312
import lombok.SneakyThrows;
13+
import lombok.experimental.var;
1414
import lombok.val;
1515
import org.bukkit.Bukkit;
1616
import org.bukkit.ChatColor;
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939
import java.util.logging.Level;
4040

41+
import static com.mengcraft.script.util.Utils.as;
4142
import static org.bukkit.util.NumberConversions.toInt;
4243

4344
/**
@@ -46,9 +47,9 @@
4647
public final class ScriptBootstrap extends JavaPlugin implements IScriptSpi {
4748

4849
private static ScriptBootstrap plugin;
50+
private final ScriptCommand scriptCommand = new ScriptCommand();
4951
private Map<String, Named> scripts;
5052
private ScriptEngine jsEngine;
51-
private final Map<String, HandledExecutor> executor = new HashMap<>();
5253
private ScriptLoader scriptLoader;
5354
private Unsafe unsafe;
5455

@@ -89,9 +90,8 @@ public void onLoad() {
8990
@Override
9091
public void onEnable() {
9192
scriptLoader = new ScriptLoader();
92-
getServer().getConsoleSender().sendMessage(ArrayHelper.toArray(
93-
ChatColor.GREEN + "梦梦家高性能服务器出租店",
94-
ChatColor.GREEN + "shop105595113.taobao.com"));
93+
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "梦梦家高性能服务器出租店");
94+
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "shop105595113.taobao.com");
9595

9696
EventMapping.INSTANCE.loadClasses();// Register build-in event
9797
getLogger().info("Initialized " + (EventMapping.INSTANCE.getKnownClasses().size() / 2) + " build-in event(s)");
@@ -100,7 +100,7 @@ public void onEnable() {
100100
Formatter.setReplacePlaceholder(true);
101101
}
102102

103-
getCommand("script").setExecutor(new MainCommand(this, executor));
103+
getCommand("script").setExecutor(scriptCommand);
104104

105105
saveDefaultConfig();
106106
unsafe = new Unsafe(this);
@@ -176,7 +176,7 @@ public ScriptLoader.ScriptBinding loadScript(ScriptLoader.ScriptInfo info) throw
176176
ScriptPlugin loaded = binding.getPlugin();
177177
if (loaded.isHandled() && !loaded.isIdled()) {
178178
String name = loaded.getDescription("name");
179-
Named named = (Named) scripts.get(name);
179+
Named named = scripts.get(name);
180180
if (named != null) {
181181
ScriptPluginException.thr(loaded, "Name conflict with " + named.getId());
182182
}
@@ -185,7 +185,7 @@ public ScriptLoader.ScriptBinding loadScript(ScriptLoader.ScriptInfo info) throw
185185
return binding;
186186
}
187187

188-
Named lookById(String id) {
188+
private Named lookById(String id) {
189189
for (Object obj : scripts.values()) {
190190
Named named = (Named) obj;
191191
if (named.getId().equals(id)) {
@@ -200,30 +200,26 @@ public ImmutableList<String> list() {
200200
}
201201

202202
@SuppressWarnings("unchecked")
203-
protected void addExecutor(HandledExecutor handled) {
204-
String label = handled.getLabel();
205-
Preconditions.checkState(!executor.containsKey(label));
206-
try {
207-
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
208-
field.setAccessible(true);
209-
SimpleCommandMap i = SimpleCommandMap.class.cast(field.get(getServer().getPluginManager()));
210-
Field f = SimpleCommandMap.class.getDeclaredField("knownCommands");
211-
f.setAccessible(true);
212-
Map handler = Map.class.cast(f.get(i));
213-
PluginCommand command = getCommand("script");
214-
handler.putIfAbsent(label, command);
215-
handler.putIfAbsent("script:" + label, command);
216-
executor.put(label, handled);
217-
executor.put("script:" + label, handled);
218-
} catch (ReflectiveOperationException e) {
219-
throw new RuntimeException(e.getMessage());
220-
}
203+
@SneakyThrows
204+
protected void addExecutor(HandledCommand executor) {
205+
Preconditions.checkState(!scriptCommand.containsKey(executor.getLabel()));
206+
String label = executor.getLabel();
207+
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
208+
field.setAccessible(true);
209+
var commandMap = as(field.get(getServer().getPluginManager()), SimpleCommandMap.class);
210+
Field f = SimpleCommandMap.class.getDeclaredField("knownCommands");
211+
f.setAccessible(true);
212+
var knownCommands = as(f.get(commandMap), Map.class);
213+
PluginCommand command = getCommand("script");
214+
knownCommands.putIfAbsent(label, command);
215+
knownCommands.putIfAbsent("script:" + label, command);
216+
scriptCommand.put(label, executor);
217+
scriptCommand.put("script:" + label, executor);
221218
}
222219

223220
@SneakyThrows
224-
protected boolean remove(HandledExecutor handled) {
225-
boolean b = executor.containsKey(handled.getLabel());
226-
if (b) {
221+
protected boolean remove(HandledCommand handled) {
222+
if (scriptCommand.containsKey(handled.getLabel())) {
227223
String label = handled.getLabel();
228224
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
229225
field.setAccessible(true);
@@ -234,10 +230,11 @@ protected boolean remove(HandledExecutor handled) {
234230
PluginCommand command = getCommand("script");
235231
handler.remove(label, command);
236232
handler.remove("script:" + label, command);
237-
executor.remove(label);
238-
executor.remove("script:" + label);
233+
scriptCommand.remove(label);
234+
scriptCommand.remove("script:" + label);
235+
return true;
239236
}
240-
return b;
237+
return false;
241238
}
242239

243240
boolean unload(ScriptPlugin script) {

src/main/java/com/mengcraft/script/MainCommand.java renamed to src/main/java/com/mengcraft/script/ScriptCommand.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.mengcraft.script;
22

3+
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Maps;
35
import com.mengcraft.script.loader.ScriptPluginException;
4-
import com.mengcraft.script.util.ArrayHelper;
6+
import com.mengcraft.script.util.Utils;
7+
import lombok.AccessLevel;
8+
import lombok.NoArgsConstructor;
59
import org.bukkit.ChatColor;
610
import org.bukkit.command.Command;
711
import org.bukkit.command.CommandExecutor;
@@ -15,30 +19,41 @@
1519
/**
1620
* Created on 16-10-25.
1721
*/
18-
public class MainCommand implements CommandExecutor {
22+
@NoArgsConstructor(access = AccessLevel.PACKAGE)
23+
public class ScriptCommand implements CommandExecutor {
1924

20-
private final Map<String, HandledExecutor> executor;
21-
private final ScriptBootstrap main;
25+
private final Map<String, HandledCommand> commands = Maps.newHashMap();
2226

23-
MainCommand(ScriptBootstrap main, Map<String, HandledExecutor> executor) {
24-
this.main = main;
25-
this.executor = executor;
27+
public boolean containsKey(Object key) {
28+
return commands.containsKey(key);
29+
}
30+
31+
public HandledCommand get(Object key) {
32+
return commands.get(key);
33+
}
34+
35+
public HandledCommand put(String key, HandledCommand value) {
36+
return commands.put(key, value);
37+
}
38+
39+
public HandledCommand remove(Object key) {
40+
return commands.remove(key);
2641
}
2742

2843
@Override
29-
public boolean onCommand(CommandSender who, Command i, String label, String[] j) {
44+
public boolean onCommand(CommandSender who, Command _command, String label, String[] params) {
3045
if (label.equals("script") || label.equals("script:script")) {
3146
if (who.hasPermission("script.admin")) {
32-
return admin(who, Arrays.asList(j).iterator());
47+
return admin(who, Arrays.asList(params).iterator());
3348
} else {
3449
who.sendMessage(ChatColor.RED + "你没有执行权限");
3550
}
3651
} else {
37-
HandledExecutor handled = executor.get(label);
38-
if (handled == null) {
52+
HandledCommand executor = commands.get(label);
53+
if (executor == null) {
3954
throw new IllegalStateException("喵");
4055
}
41-
return handled.execute(who, ArrayHelper.toJSArray(j));
56+
return executor.onCommand(who, _command, label, params);
4257
}
4358
return false;
4459
}
@@ -50,7 +65,7 @@ private boolean admin(CommandSender who, Iterator<String> i) {
5065
return list(who);
5166
}
5267
if (label.equals("load")) {
53-
return i.hasNext() && load(who, i.next(), i.hasNext() ? ArrayHelper.toJSArray(i) : null);
68+
return i.hasNext() && load(who, i.next(), i.hasNext() ? Utils.fromJava(Lists.newArrayList(i)) : null);
5469
}
5570
if (label.equals("unload")) {
5671
return i.hasNext() && unload(who, i.next());
@@ -68,13 +83,13 @@ private boolean admin(CommandSender who, Iterator<String> i) {
6883
}
6984

7085
private boolean reload(CommandSender who) {
71-
main.reload();
86+
ScriptBootstrap.get().reload();
7287
who.sendMessage(ChatColor.GREEN + "O-kay!");
7388
return true;
7489
}
7590

7691
private boolean unload(CommandSender who, String i) {
77-
if (main.unload(i)) {
92+
if (ScriptBootstrap.get().unload(i)) {
7893
who.sendMessage(ChatColor.GREEN + "O-kay!");
7994
return true;
8095
}
@@ -83,15 +98,15 @@ private boolean unload(CommandSender who, String i) {
8398

8499
private boolean list(CommandSender who) {
85100
who.sendMessage(ChatColor.GREEN + "> Loaded script(s)");
86-
for (String l : main.list()) {
101+
for (String l : ScriptBootstrap.get().list()) {
87102
who.sendMessage(ChatColor.GREEN + "- " + l);
88103
}
89104
return true;
90105
}
91106

92107
private boolean load(CommandSender who, String load, Object arg) {
93108
try {
94-
main.load(who, new File(main.getDataFolder(), load), arg);
109+
ScriptBootstrap.get().load(who, new File(ScriptBootstrap.get().getDataFolder(), load), arg);
95110
who.sendMessage(ChatColor.GREEN + "O-kay!");
96111
return true;
97112
} catch (IllegalArgumentException | ScriptPluginException e) {

src/main/java/com/mengcraft/script/ScriptExecutor.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)