Skip to content

Commit efabe04

Browse files
authored
Merge pull request #13 from astorks/dev
v1.1.6 Dev -> Master
2 parents 7e95eb8 + 98336ab commit efabe04

File tree

7 files changed

+95
-5
lines changed

7 files changed

+95
-5
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.pixlfox.scriptablemc"
9-
version = "1.1.5"
9+
version = "1.1.6"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8

src/main/kotlin/com/pixlfox/scriptablemc/ScriptablePluginCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class ScriptablePluginJsCommand(private val basePlugin: ScriptablePluginMain) :
5757
basePlugin.scriptEngine = ScriptablePluginEngine(
5858
basePlugin,
5959
basePlugin.config.getString("root_scripts_folder", "./scripts").orEmpty(),
60-
basePlugin.config.getBoolean("debug", false)
60+
basePlugin.config.getBoolean("debug", false),
61+
basePlugin.config.getBoolean("extract_libs", true)
6162
)
6263

6364
basePlugin.scriptEngine!!.start()

src/main/kotlin/com/pixlfox/scriptablemc/ScriptablePluginMain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ScriptablePluginMain : JavaPlugin(), Listener {
2424
saveDefaultConfig()
2525

2626
try {
27-
scriptEngine = ScriptablePluginEngine(this, config.getString("root_scripts_folder", "./scripts").orEmpty(), config.getBoolean("debug", false))
27+
scriptEngine = ScriptablePluginEngine(this, config.getString("root_scripts_folder", "./scripts").orEmpty(), config.getBoolean("debug", false), config.getBoolean("extract_libs", true))
2828
scriptEngine!!.start()
2929
logger.info("Scriptable plugin engine started.")
3030
} catch (e: IllegalStateException) {

src/main/kotlin/com/pixlfox/scriptablemc/core/ScriptablePluginEngine.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pixlfox.scriptablemc.core
22

3+
import com.pixlfox.scriptablemc.utils.UnzipUtility
34
import fr.minuskube.inv.InventoryManager
45
import org.bukkit.Bukkit
56
import org.bukkit.command.CommandSender
@@ -11,7 +12,7 @@ import java.util.*
1112

1213

1314
@Suppress("MemberVisibilityCanBePrivate", "unused")
14-
class ScriptablePluginEngine(val bootstrapPlugin: JavaPlugin, val rootScriptsFolder: String = "./scripts", val debugEnabled: Boolean = false): Listener {
15+
class ScriptablePluginEngine(val bootstrapPlugin: JavaPlugin, val rootScriptsFolder: String = "./scripts", val debugEnabled: Boolean = false, val extractLibs: Boolean = true): Listener {
1516
private val graalContext: Context = Context
1617
.newBuilder()
1718
.allowAllAccess(true)
@@ -35,6 +36,14 @@ class ScriptablePluginEngine(val bootstrapPlugin: JavaPlugin, val rootScriptsFol
3536
mainScriptFile.parentFile.mkdirs()
3637
}
3738

39+
if(extractLibs) {
40+
val librariesResource = bootstrapPlugin.getResource("libraries.zip")
41+
if (librariesResource != null) {
42+
val unzipUtil = UnzipUtility()
43+
unzipUtil.unzip(librariesResource, "${rootScriptsFolder}/lib")
44+
}
45+
}
46+
3847
if(mainScriptFile.exists()) {
3948
eval(
4049
Source.newBuilder("js", mainScriptFile)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.pixlfox.scriptablemc.utils
2+
3+
import java.io.*
4+
import java.util.zip.ZipInputStream
5+
6+
7+
/**
8+
* This utility extracts files and directories of a standard zip file to
9+
* a destination directory.
10+
* @author www.codejava.net
11+
*/
12+
class UnzipUtility {
13+
/**
14+
* Extracts a zip file specified by the zipFilePath to a directory specified by
15+
* destDirectory (will be created if does not exists)
16+
* @param zipFilePath
17+
* @param destDirectory
18+
* @throws IOException
19+
*/
20+
@Throws(IOException::class)
21+
fun unzip(zipStream: InputStream, destDirectory: String) {
22+
val destDir = File(destDirectory)
23+
if (!destDir.exists()) {
24+
destDir.mkdirs()
25+
}
26+
val zipIn = ZipInputStream(zipStream)
27+
var entry = zipIn.nextEntry
28+
// iterates over entries in the zip file
29+
while (entry != null) {
30+
val filePath = destDirectory + File.separator + entry.name
31+
if (!entry.isDirectory) { // if the entry is a file, extracts it
32+
extractFile(zipIn, filePath)
33+
} else { // if the entry is a directory, make the directory
34+
val dir = File(filePath)
35+
dir.mkdir()
36+
}
37+
zipIn.closeEntry()
38+
entry = zipIn.nextEntry
39+
}
40+
zipIn.close()
41+
}
42+
43+
/**
44+
* Extracts a zip file specified by the zipFilePath to a directory specified by
45+
* destDirectory (will be created if does not exists)
46+
* @param zipFilePath
47+
* @param destDirectory
48+
* @throws IOException
49+
*/
50+
@Throws(IOException::class)
51+
fun unzip(zipFilePath: String, destDirectory: String) = unzip(FileInputStream(zipFilePath), destDirectory)
52+
53+
/**
54+
* Extracts a zip entry (file entry)
55+
* @param zipIn
56+
* @param filePath
57+
* @throws IOException
58+
*/
59+
@Throws(IOException::class)
60+
private fun extractFile(zipIn: ZipInputStream, filePath: String) {
61+
val bos = BufferedOutputStream(FileOutputStream(filePath))
62+
val bytesIn = ByteArray(BUFFER_SIZE)
63+
var read: Int
64+
while (zipIn.read(bytesIn).also { read = it } != -1) {
65+
bos.write(bytesIn, 0, read)
66+
}
67+
bos.close()
68+
}
69+
70+
companion object {
71+
/**
72+
* Size of the buffer to read/write data
73+
*/
74+
private const val BUFFER_SIZE = 4096
75+
}
76+
}

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ root_scripts_folder: './scripts'
44

55
# Prints some additional debugging messages
66
debug: false
7+
8+
# Automatically extract js libraries to scripts lib folder if lib folder is missing
9+
# If you set this to false you'll need to download and extract the libs manually.
10+
extract_libs: true

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ScriptableMC
22
description: 'Run JavaScript/TypeScript plugins for Bukkit/Spigot/Paper Minecraft 1.15'
3-
version: '1.1.5'
3+
version: '1.1.6'
44
api-version: '1.15'
55
author: AStorks
66
main: com.pixlfox.scriptablemc.ScriptablePluginMain

0 commit comments

Comments
 (0)