Skip to content

Commit

Permalink
1.4.1 fix sources directories, fix classpath scan, fix commandblock w…
Browse files Browse the repository at this point in the history
…orkspace name
  • Loading branch information
BuildTools committed Dec 30, 2021
1 parent e0520c4 commit 1f60cc9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<groupId>ru.dpohvar.varscript</groupId>
<artifactId>varscript</artifactId>
<version>1.4</version>
<version>1.4.1</version>
<packaging>jar</packaging>

<description>Powerful Scripting Plugin</description>
Expand Down
62 changes: 36 additions & 26 deletions src/main/java/ru/dpohvar/varscript/boot/BootHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -65,39 +66,48 @@ public static void loadExtensions(ClassLoader classLoader){
public static ArrayList<String> getClassNamesFromPackage(String packageName, boolean recursive) {
ClassLoader resourceClassLoader = VarScript.class.getClassLoader();
ArrayList<String> names = new ArrayList<String>();
URL packageURL = null;
packageName = packageName.replace('.','/');
if (!packageName.endsWith("/")) packageName += '/';

ClassLoader packageClassLoader = resourceClassLoader;
var urlList = new LinkedList<URL>();

while (packageURL == null && packageClassLoader != null) {
packageURL = packageClassLoader.getResource(packageName);
packageClassLoader = packageClassLoader.getParent();
var packageClassLoader = resourceClassLoader;
try {
while (packageClassLoader != null) {
var resources = packageClassLoader.getResources(packageName);
resources.asIterator().forEachRemaining(urlList::add);
packageClassLoader = packageClassLoader.getParent();
}
} catch (IOException e) {
throw new RuntimeException(e);
}

if( packageURL != null && packageURL.getProtocol().equals("jar")) try {
// build jar file name, then loop through zipped entries
String jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8");
jarFileName = jarFileName.substring(5,jarFileName.indexOf("!"));
JarFile jf = new JarFile(jarFileName);
Enumeration<JarEntry> jarEntries = jf.entries();
while (jarEntries.hasMoreElements()){
JarEntry entry = jarEntries.nextElement();
if (entry.isDirectory()) continue;
String entryName = entry.getName();
if (!entryName.startsWith(packageName)) continue;
if (!entryName.endsWith(".class")) continue;
if (entryName.contains("$")) continue;
if (!recursive) {
String end = entryName.substring(packageName.length());
if (end.contains("/")) continue;

for (URL packageURL : urlList) {
if(packageURL.getProtocol().equals("jar")) try {
// build jar file name, then loop through zipped entries
String jarFileName = URLDecoder.decode(packageURL.getFile(), StandardCharsets.UTF_8);
jarFileName = jarFileName.substring(5,jarFileName.indexOf("!"));
JarFile jf = new JarFile(jarFileName);
Enumeration<JarEntry> jarEntries = jf.entries();
while (jarEntries.hasMoreElements()){
JarEntry entry = jarEntries.nextElement();
if (entry.isDirectory()) continue;
String entryName = entry.getName();
if (!entryName.startsWith(packageName)) continue;
if (!entryName.endsWith(".class")) continue;
if (entryName.contains("$")) continue;
if (!recursive) {
String end = entryName.substring(packageName.length());
if (end.contains("/")) continue;
}
String name = entryName.substring(0,entryName.length()-6);
String className = name.replace('/','.');
names.add(className);
}
String name = entryName.substring(0,entryName.length()-6);
String className = name.replace('/','.');
names.add(className);
}
} catch (IOException ignored) {}
} catch (IOException ignored) {}

}
return names;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/ru/dpohvar/varscript/caller/Caller.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public CommandSender getSender() {
public void sendPrintMessage(CharSequence message, String source){
if (source == null) source = "";
String prefix = String.format(VarScript.printPrefix, source);
if (sender instanceof Conversable && ((Conversable) sender).isConversing()) {
((Conversable) sender).sendRawMessage(prefix + message);
if (sender instanceof Conversable cons && cons.isConversing()) {
cons.sendRawMessage(prefix + message);
} else {
sender.sendMessage(prefix + message);
}
Expand All @@ -56,8 +56,8 @@ public void sendPrintMessage(CharSequence message, String source){
public void sendMessage(CharSequence message, String source){
if (source == null) source = "";
String prefix = String.format(VarScript.prefix, source);
if (sender instanceof Conversable && ((Conversable) sender).isConversing()) {
((Conversable) sender).sendRawMessage(prefix + message);
if (sender instanceof Conversable cons && cons.isConversing()) {
cons.sendRawMessage(prefix + message);
} else {
sender.sendMessage(prefix + message);
}
Expand All @@ -66,8 +66,8 @@ public void sendMessage(CharSequence message, String source){
public void sendErrorMessage(CharSequence message, String source){
if (source == null) source = "";
String prefix = String.format(VarScript.errorPrefix, source);
if (sender instanceof Conversable && ((Conversable) sender).isConversing()) {
((Conversable) sender).sendRawMessage(prefix + message);
if (sender instanceof Conversable cons && cons.isConversing()) {
cons.sendRawMessage(prefix + message);
} else {
sender.sendMessage(prefix + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Workspace(WorkspaceService workspaceService, String name) {
compilerConfiguration.setScriptBaseClass(CallerScript.class.getName());
List<CompilationCustomizer> compilationCustomizers = compilerConfiguration.getCompilationCustomizers();
compilerConfiguration.getClasspath().addAll(workspaceService.getClassPath());
String encoding = workspaceService.getVarScript().getConfig().getString("sources.encoding");
String encoding = workspaceService.getVarScript().getConfig().getString("workspace-sources.encoding");
if (encoding != null) compilerConfiguration.setSourceEncoding(encoding);
compilationCustomizers.addAll(workspaceService.getCompilationCustomizers());
groovyClassLoader = new GroovyClassLoader(workspaceService.getGroovyClassLoader(), compilerConfiguration);
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/ru/dpohvar/varscript/workspace/WorkspaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import groovy.lang.*;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -73,13 +75,13 @@ private static String getClassShortName(String className){
}

public WorkspaceService(VarScript varscript){
VarScriptClassLoader libLoader = VarScript.libLoader;
this.varscript = varscript;
VarScriptClassLoader libLoader = VarScript.libLoader;
FileConfiguration config = varscript.getConfig();
this.autorunDirectory = new File(config.getString("sources.autorun"));
this.scriptsDirectory = new File(config.getString("sources.scripts"));
this.classesDirectory = new File(config.getString("sources.classes"));
this.serviceDirectory = new File(config.getString("sources.services"));
this.autorunDirectory = getRelativeDirInConfig("workspace-sources.autorun");
this.scriptsDirectory = getRelativeDirInConfig("workspace-sources.scripts");
this.classesDirectory = getRelativeDirInConfig("workspace-sources.classes");
this.serviceDirectory = getRelativeDirInConfig("workspace-sources.services");
ImportCustomizer importCustomizer = new ImportCustomizer();
compilationCustomizers.add(importCustomizer);
for (Map<?, ?> anImport : config.getMapList("import")) {
Expand Down Expand Up @@ -128,6 +130,12 @@ public WorkspaceService(VarScript varscript){
VarScript.libLoader.monitorFolder(groovyClassLoader, serviceDirectory);
}

File getRelativeDirInConfig(String configPath){
var configValue = varscript.getConfig().getString(configPath);
if (configValue.startsWith("/")) return new File(configValue);
return new File(this.varscript.getDataFolder(), configValue);
}

public CompilerConfiguration getCompilerConfiguration() {
return compilerConfiguration;
}
Expand Down Expand Up @@ -205,9 +213,15 @@ public List<String> getClassPath() {

public String getWorkspaceName(CommandSender sender){
FileConfiguration config = varscript.getConfig();
String workspaceName = config.getString("workspace." + sender.getName());
var name = sender.getName();
if (sender instanceof BlockCommandSender bs) {
CommandBlock state = (CommandBlock) bs.getBlock().getState();
name = state.getName();
return name;
}
String workspaceName = config.getString("workspace." + name);
if (workspaceName != null) return workspaceName;
else return sender.getName();
else return name;
}

public void setWorkspaceName(CommandSender sender, String workspaceName){
Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sources:
autorun: plugins/${project.artifactId}/autorun
scripts: plugins/${project.artifactId}/scripts
classes: plugins/${project.artifactId}/scripts
services: plugins/${project.artifactId}/autorun
workspace-sources:
autorun: autorun
scripts: scripts
classes: scripts
services: autorun
encoding: UTF-8
import:
- scan-package: org.bukkit
Expand All @@ -19,9 +19,11 @@ import:
- scan-package: org.bukkit.projectiles
- scan-package: org.bukkit.scoreboard
- scan-package: org.bukkit.potion
- scan-package: org.bukkit.generator
- class: org.bukkit.util.Vector
- class: org.bukkit.util.BlockVector
- class: org.bukkit.util.EulerAngle
- class: org.bukkit.event.EventPriority
- class: org.bukkit.util.EulerAngle
- class: org.bukkit.loot.LootTables
- class: org.bukkit.material.MaterialData

0 comments on commit 1f60cc9

Please sign in to comment.