66import com .mengcraft .script .loader .ScriptLoader ;
77import com .mengcraft .script .loader .ScriptPluginException ;
88import com .mengcraft .script .plugin .ScriptingLoader ;
9- import com .mengcraft .script .util .ArrayHelper ;
109import com .mengcraft .script .util .BossBarWrapper ;
1110import com .mengcraft .script .util .Named ;
1211import lombok .RequiredArgsConstructor ;
1312import lombok .SneakyThrows ;
13+ import lombok .experimental .var ;
1414import lombok .val ;
1515import org .bukkit .Bukkit ;
1616import org .bukkit .ChatColor ;
3838import java .util .Map ;
3939import java .util .logging .Level ;
4040
41+ import static com .mengcraft .script .util .Utils .as ;
4142import static org .bukkit .util .NumberConversions .toInt ;
4243
4344/**
4647public 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 ) {
0 commit comments