Skip to content

Commit

Permalink
Release version 2.1.1 (#30)
Browse files Browse the repository at this point in the history
* Use charset enum instead of string literal

* Move util wrapper thread pool to field

* Add close method for util wrapper

* Add call to util close method in plugin shutdown event

* Set default async command state to true

* Fix NoSuchField exception when accessing protected field on newer servers

* Bump version information to 2.1.1
  • Loading branch information
jupjohn committed Apr 28, 2020
1 parent 2bc6d35 commit 68b4f88
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@

<groupId>unwrittenfun.minecraft</groupId>
<artifactId>Lukkit</artifactId>
<version>2.1.0</version>
<version>2.1.1</version>

<packaging>jar</packaging>

Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.luaj.vm2.lib.jse.CoerceJavaToLua;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
Expand All @@ -51,6 +52,7 @@ public class LukkitPlugin implements Plugin {
private final Logger logger;
private final List<LukkitCommand> commands = new ArrayList<>();
private final HashMap<Class<? extends Event>, ArrayList<LuaFunction>> eventListeners = new HashMap<>();
private final UtilitiesWrapper utilitiesWrapper;
private LuaFunction loadCB;
private LuaFunction enableCB;
private LuaFunction disableCB;
Expand Down Expand Up @@ -80,11 +82,7 @@ public LukkitPlugin(LukkitPluginLoader loader, LukkitPluginFile file) throws Inv
this.logger = new PluginLogger(this);
Globals globals = LuaEnvironment.getNewGlobals(this);

try {
this.pluginMain = globals.load(new InputStreamReader(this.pluginFile.getResource(this.descriptor.getMain()), "UTF-8"), this.descriptor.getMain());
} catch (UnsupportedEncodingException e) {
throw new InvalidPluginException("File could not be loaded using UTF-8.", e.getCause());
}
this.pluginMain = globals.load(new InputStreamReader(this.pluginFile.getResource(this.descriptor.getMain()), StandardCharsets.UTF_8), this.descriptor.getMain());
this.dataFolder = new File(Main.instance.getDataFolder().getParentFile().getAbsolutePath() + File.separator + this.name);
if (!this.dataFolder.exists()) //noinspection ResultOfMethodCallIgnored
this.dataFolder.mkdir();
Expand All @@ -95,7 +93,9 @@ public LukkitPlugin(LukkitPluginLoader loader, LukkitPluginFile file) throws Inv

globals.set("plugin", new PluginWrapper(this));
globals.set("logger", new LoggerWrapper(this));
globals.set("util", new UtilitiesWrapper(this));
// use a member as its internal threadpool needs to be shutdown upon disabling the plugin
utilitiesWrapper = new UtilitiesWrapper(this);
globals.set("util", utilitiesWrapper);
globals.set("config", new ConfigWrapper(this));

OneArgFunction oldRequire = (OneArgFunction) globals.get("require");
Expand Down Expand Up @@ -301,6 +301,7 @@ public void onDisable() {
LuaEnvironment.addError(e);
}
unregisterAllCommands();
utilitiesWrapper.close();
}

@Override
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class LukkitCommand extends Command {
private final LukkitPlugin plugin;
private LuaFunction tabComleteFunction;
private boolean registered = false;
private boolean runAsync = false;
private boolean runAsync = true;
private int minArgs = 0;
private int maxArgs = -1;
// TODO: Add options to use min/max args, set permission, set run async, and more helper functions
Expand Down Expand Up @@ -85,8 +85,14 @@ public void register() throws NoSuchFieldException, IllegalAccessException {
public void unregister() throws NoSuchFieldException, IllegalAccessException {
Object result = getPrivateField(Bukkit.getServer().getPluginManager(), "commandMap");
SimpleCommandMap commandMap = (SimpleCommandMap) result;
Object map = getPrivateField(commandMap, "knownCommands");
HashMap<String, Command> knownCommands = (HashMap<String, Command>) map;
Object knownCommandsMap;
try {
knownCommandsMap = getPrivateField(commandMap, "knownCommands");
} catch (Exception ignored) {
// Early exit as there's nothing to do
return;
}
HashMap<String, Command> knownCommands = (HashMap<String, Command>) knownCommandsMap;
knownCommands.remove(getName());
for (String alias : getAliases()) {
if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(this.getName())) {
Expand Down
@@ -1,11 +1,11 @@
package nz.co.jammehcow.lukkit.environment.wrappers;

import nz.co.jammehcow.lukkit.environment.LuaEnvironment;
import nz.co.jammehcow.lukkit.environment.LuaEnvironment.ObjectType;
import nz.co.jammehcow.lukkit.environment.plugin.LukkitPlugin;
import nz.co.jammehcow.lukkit.environment.plugin.LukkitPluginException;
import org.bukkit.inventory.ItemStack;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaUserdata;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
Expand All @@ -22,9 +22,11 @@
public class UtilitiesWrapper extends LuaTable {

private LukkitPlugin plugin;
private ScheduledExecutorService runDelayedThreadPool;

public UtilitiesWrapper(LukkitPlugin plugin) {
this.plugin = plugin;
this.runDelayedThreadPool = Executors.newScheduledThreadPool(1);

set("getTableFromList", new OneArgFunction() {
@Override
Expand Down Expand Up @@ -107,24 +109,18 @@ public LuaValue call(LuaValue function, LuaValue delay) {
set("runDelayed", new TwoArgFunction() {
// Delay is in milliseconds.
@Override
public synchronized LuaValue call(LuaValue function, LuaValue time) {
System.out.println("before");
public LuaValue call(LuaValue function, LuaValue time) {
ScheduledFuture<LuaValue> future = runDelayedThreadPool.schedule((Callable<LuaValue>) function::call, time.checklong(), TimeUnit.MILLISECONDS);

ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
ScheduledFuture future = execService.schedule((Callable<LuaValue>) function::call, time.checklong(), TimeUnit.MILLISECONDS);

while (!future.isDone()) {
try {
wait();
} catch (InterruptedException e) {
future.cancel(true);
plugin.getLogger().warning("A sync method was killed due to the future being interrupted. Dumping the stack trace for debug purposes");
e.printStackTrace();
}
try {
// Blocking call, we don't care about the value
future.get();
} catch (Exception e) {
plugin.getLogger().warning("The thread spawned by runDelayed was terminated or threw an exception");
LuaEnvironment.addError(e);
e.printStackTrace();
}

execService.shutdown();
notify();
return LuaValue.NIL;
}
});
Expand Down Expand Up @@ -199,6 +195,10 @@ public LuaValue call(LuaValue userdata, LuaValue clazz) {
});
}

public void close() {
runDelayedThreadPool.shutdown();
}

@Override
public String typename() {
return ObjectType.WRAPPER.name;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
@@ -1,6 +1,6 @@
name: Lukkit
main: nz.co.jammehcow.lukkit.Main
version: 2.1.0
version: 2.1.1

author: ArtexDevelopment
authors: [jammehcow, AL_1, mathhulk]
Expand Down

0 comments on commit 68b4f88

Please sign in to comment.