Skip to content

Commit

Permalink
Some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Dec 17, 2019
1 parent 7c68ec1 commit 2985a79
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 211 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/mengcraft/script/EventMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.io.Files;
import com.mengcraft.script.util.ArrayHelper;
import com.mengcraft.script.util.Utils;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.val;
Expand Down Expand Up @@ -126,7 +126,7 @@ public Object filter(String regex) {// Export to scripts
list.add(key);
}
});
return ArrayHelper.toJSArray(list.toArray());
return Utils.fromJava(list);
}

protected static HandlerList getHandler(Mapping mapping) {
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/mengcraft/script/HandledCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mengcraft.script;

import com.mengcraft.script.util.Utils;
import lombok.RequiredArgsConstructor;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import javax.script.Bindings;
import java.util.function.BiConsumer;

/**
* Created on 16-10-17.
*/
@RequiredArgsConstructor
public class HandledCommand implements CommandExecutor {

private final ScriptPlugin script;
private final String label;
private final BiConsumer<CommandSender, Bindings> executor;
private final String permission;

public boolean execute(CommandSender sender, Bindings params) {
if (permission == null || sender.hasPermission(permission)) {
executor.accept(sender, params);
return true;
} else {
sender.sendMessage(ChatColor.RED + "你没有执行权限");
}
return false;
}


public boolean remove() {
return script.remove(this);
}

public String getLabel() {
return label;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] params) {
return execute(sender, Utils.fromJava(params));
}
}
42 changes: 0 additions & 42 deletions src/main/java/com/mengcraft/script/HandledExecutor.java

This file was deleted.

61 changes: 29 additions & 32 deletions src/main/java/com/mengcraft/script/ScriptBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.mengcraft.script.loader.ScriptLoader;
import com.mengcraft.script.loader.ScriptPluginException;
import com.mengcraft.script.plugin.ScriptingLoader;
import com.mengcraft.script.util.ArrayHelper;
import com.mengcraft.script.util.BossBarWrapper;
import com.mengcraft.script.util.Named;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.var;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -38,6 +38,7 @@
import java.util.Map;
import java.util.logging.Level;

import static com.mengcraft.script.util.Utils.as;
import static org.bukkit.util.NumberConversions.toInt;

/**
Expand All @@ -46,9 +47,9 @@
public final class ScriptBootstrap extends JavaPlugin implements IScriptSpi {

private static ScriptBootstrap plugin;
private final ScriptCommand scriptCommand = new ScriptCommand();
private Map<String, Named> scripts;
private ScriptEngine jsEngine;
private final Map<String, HandledExecutor> executor = new HashMap<>();
private ScriptLoader scriptLoader;
private Unsafe unsafe;

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

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

getCommand("script").setExecutor(new MainCommand(this, executor));
getCommand("script").setExecutor(scriptCommand);

saveDefaultConfig();
unsafe = new Unsafe(this);
Expand Down Expand Up @@ -176,7 +176,7 @@ public ScriptLoader.ScriptBinding loadScript(ScriptLoader.ScriptInfo info) throw
ScriptPlugin loaded = binding.getPlugin();
if (loaded.isHandled() && !loaded.isIdled()) {
String name = loaded.getDescription("name");
Named named = (Named) scripts.get(name);
Named named = scripts.get(name);
if (named != null) {
ScriptPluginException.thr(loaded, "Name conflict with " + named.getId());
}
Expand All @@ -185,7 +185,7 @@ public ScriptLoader.ScriptBinding loadScript(ScriptLoader.ScriptInfo info) throw
return binding;
}

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

@SuppressWarnings("unchecked")
protected void addExecutor(HandledExecutor handled) {
String label = handled.getLabel();
Preconditions.checkState(!executor.containsKey(label));
try {
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
field.setAccessible(true);
SimpleCommandMap i = SimpleCommandMap.class.cast(field.get(getServer().getPluginManager()));
Field f = SimpleCommandMap.class.getDeclaredField("knownCommands");
f.setAccessible(true);
Map handler = Map.class.cast(f.get(i));
PluginCommand command = getCommand("script");
handler.putIfAbsent(label, command);
handler.putIfAbsent("script:" + label, command);
executor.put(label, handled);
executor.put("script:" + label, handled);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e.getMessage());
}
@SneakyThrows
protected void addExecutor(HandledCommand executor) {
Preconditions.checkState(!scriptCommand.containsKey(executor.getLabel()));
String label = executor.getLabel();
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
field.setAccessible(true);
var commandMap = as(field.get(getServer().getPluginManager()), SimpleCommandMap.class);
Field f = SimpleCommandMap.class.getDeclaredField("knownCommands");
f.setAccessible(true);
var knownCommands = as(f.get(commandMap), Map.class);
PluginCommand command = getCommand("script");
knownCommands.putIfAbsent(label, command);
knownCommands.putIfAbsent("script:" + label, command);
scriptCommand.put(label, executor);
scriptCommand.put("script:" + label, executor);
}

@SneakyThrows
protected boolean remove(HandledExecutor handled) {
boolean b = executor.containsKey(handled.getLabel());
if (b) {
protected boolean remove(HandledCommand handled) {
if (scriptCommand.containsKey(handled.getLabel())) {
String label = handled.getLabel();
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
field.setAccessible(true);
Expand All @@ -234,10 +230,11 @@ protected boolean remove(HandledExecutor handled) {
PluginCommand command = getCommand("script");
handler.remove(label, command);
handler.remove("script:" + label, command);
executor.remove(label);
executor.remove("script:" + label);
scriptCommand.remove(label);
scriptCommand.remove("script:" + label);
return true;
}
return b;
return false;
}

boolean unload(ScriptPlugin script) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.mengcraft.script;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mengcraft.script.loader.ScriptPluginException;
import com.mengcraft.script.util.ArrayHelper;
import com.mengcraft.script.util.Utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
Expand All @@ -15,30 +19,41 @@
/**
* Created on 16-10-25.
*/
public class MainCommand implements CommandExecutor {
@NoArgsConstructor(access = AccessLevel.PACKAGE)
public class ScriptCommand implements CommandExecutor {

private final Map<String, HandledExecutor> executor;
private final ScriptBootstrap main;
private final Map<String, HandledCommand> commands = Maps.newHashMap();

MainCommand(ScriptBootstrap main, Map<String, HandledExecutor> executor) {
this.main = main;
this.executor = executor;
public boolean containsKey(Object key) {
return commands.containsKey(key);
}

public HandledCommand get(Object key) {
return commands.get(key);
}

public HandledCommand put(String key, HandledCommand value) {
return commands.put(key, value);
}

public HandledCommand remove(Object key) {
return commands.remove(key);
}

@Override
public boolean onCommand(CommandSender who, Command i, String label, String[] j) {
public boolean onCommand(CommandSender who, Command _command, String label, String[] params) {
if (label.equals("script") || label.equals("script:script")) {
if (who.hasPermission("script.admin")) {
return admin(who, Arrays.asList(j).iterator());
return admin(who, Arrays.asList(params).iterator());
} else {
who.sendMessage(ChatColor.RED + "你没有执行权限");
}
} else {
HandledExecutor handled = executor.get(label);
if (handled == null) {
HandledCommand executor = commands.get(label);
if (executor == null) {
throw new IllegalStateException("喵");
}
return handled.execute(who, ArrayHelper.toJSArray(j));
return executor.onCommand(who, _command, label, params);
}
return false;
}
Expand All @@ -50,7 +65,7 @@ private boolean admin(CommandSender who, Iterator<String> i) {
return list(who);
}
if (label.equals("load")) {
return i.hasNext() && load(who, i.next(), i.hasNext() ? ArrayHelper.toJSArray(i) : null);
return i.hasNext() && load(who, i.next(), i.hasNext() ? Utils.fromJava(Lists.newArrayList(i)) : null);
}
if (label.equals("unload")) {
return i.hasNext() && unload(who, i.next());
Expand All @@ -68,13 +83,13 @@ private boolean admin(CommandSender who, Iterator<String> i) {
}

private boolean reload(CommandSender who) {
main.reload();
ScriptBootstrap.get().reload();
who.sendMessage(ChatColor.GREEN + "O-kay!");
return true;
}

private boolean unload(CommandSender who, String i) {
if (main.unload(i)) {
if (ScriptBootstrap.get().unload(i)) {
who.sendMessage(ChatColor.GREEN + "O-kay!");
return true;
}
Expand All @@ -83,15 +98,15 @@ private boolean unload(CommandSender who, String i) {

private boolean list(CommandSender who) {
who.sendMessage(ChatColor.GREEN + "> Loaded script(s)");
for (String l : main.list()) {
for (String l : ScriptBootstrap.get().list()) {
who.sendMessage(ChatColor.GREEN + "- " + l);
}
return true;
}

private boolean load(CommandSender who, String load, Object arg) {
try {
main.load(who, new File(main.getDataFolder(), load), arg);
ScriptBootstrap.get().load(who, new File(ScriptBootstrap.get().getDataFolder(), load), arg);
who.sendMessage(ChatColor.GREEN + "O-kay!");
return true;
} catch (IllegalArgumentException | ScriptPluginException e) {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/mengcraft/script/ScriptExecutor.java

This file was deleted.

Loading

0 comments on commit 2985a79

Please sign in to comment.