Skip to content

Commit

Permalink
Compile and place helper lib in /Lib in built JAR file
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Jan 20, 2025
1 parent e414567 commit a218d30
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
9 changes: 8 additions & 1 deletion bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<dependency>
<groupId>dev.magicmq</groupId>
<artifactId>pyspigot-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.magicmq</groupId>
<artifactId>jython-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
Expand Down Expand Up @@ -51,6 +54,10 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<dependency>
<groupId>dev.magicmq</groupId>
<artifactId>pyspigot-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.magicmq</groupId>
<artifactId>jython-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
Expand All @@ -45,6 +48,10 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
File renamed without changes.
44 changes: 0 additions & 44 deletions core/src/main/java/dev/magicmq/pyspigot/PyCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void init() {
config.reload();

initFolders();
initHelperLib();

adapter.initCommands();
adapter.initListeners();
Expand Down Expand Up @@ -270,49 +269,6 @@ private void initFolders() {
}
}

private void initHelperLib() {
if (!config.shouldUpdatePySpigotLib()) {
return;
}

File pythonLibs = new File(adapter.getDataFolder(), "python-libs");
if (pythonLibs.exists()) {
File libFile = new File(pythonLibs, "pyspigot.py");
if (!libFile.exists()) {
saveResource("python-libs/pyspigot.py", true);
} else {
try {
FileInputStream savedFile = new FileInputStream(libFile);

URL url = adapter.getPluginClassLoader().getResource("python-libs/pyspigot.py");
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
InputStream jarFile = connection.getInputStream();

if (!checkFilesEqual(savedFile, jarFile)) {
saveResource("python-libs/pyspigot.py", true);
}
} catch (IOException e) {
adapter.getLogger().log(Level.SEVERE, "Error when initializing library files: ", e);
}
}
}
}

private boolean checkFilesEqual(InputStream is1, InputStream is2) {
try (BufferedInputStream bis1 = new BufferedInputStream(is1); BufferedInputStream bis2 = new BufferedInputStream(is2)) {
int ch;
while ((ch = bis1.read()) != -1) {
if (ch != bis2.read())
return false;
}
return bis2.read() == -1;
} catch (IOException e) {
adapter.getLogger().log(Level.SEVERE, "Error when initializing library files: ", e);
return false;
}
}

private InputStream getResourceAsStream(String name) {
return adapter.getPluginClassLoader().getResourceAsStream(name);
}
Expand Down
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
<groupId>dev.magicmq</groupId>
<artifactId>pyspigot-core</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.magicmq</groupId>
<artifactId>jython-compiler</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand All @@ -49,6 +56,28 @@
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>compile-py-sources</id>
<phase>process-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>dev.magicmq.jythoncompiler.Main</mainClass>
<arguments>
<argument>${basedir}/src/main/resources/Lib/</argument>
</arguments>
<classpathScope>provided</classpathScope>
<useMavenLogger>true</useMavenLogger>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -57,6 +86,16 @@
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>class</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down

0 comments on commit a218d30

Please sign in to comment.