Skip to content

Commit

Permalink
1.4.2 fix libraries directory, fix tab-completer for entities "/> id("
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jan 1, 2022
1 parent 1f60cc9 commit 4fdda4e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 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.1</version>
<version>1.4.2</version>
<packaging>jar</packaging>

<description>Powerful Scripting Plugin</description>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/ru/dpohvar/varscript/VarScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import ru.dpohvar.varscript.workspace.WorkspaceService;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ServiceLoader;
import java.util.jar.JarFile;

import static org.bukkit.ChatColor.translateAlternateColorCodes;

Expand All @@ -33,9 +38,15 @@ public class VarScript extends JavaPlugin {
static {
pluginName = "VarScript";
pluginsFolder = new File("plugins");
dataFolder = new File(pluginsFolder, pluginName);

URL resPluginYml = VarScript.class.getClassLoader().getResource("plugin.yml");
String jarFileName = URLDecoder.decode(resPluginYml.getFile(), StandardCharsets.UTF_8);
String pluginJarDir = jarFileName.substring(5,jarFileName.indexOf("!"));
var pluginFolderDir = pluginJarDir.substring(0, pluginJarDir.length() - 4);
dataFolder = new File(pluginFolderDir);
libLoader = new VarScriptClassLoader(VarScript.class.getClassLoader(), Bukkit.getPluginManager());
ExpandoMetaClassCreationHandle.enable();
BootHelper.loadLibraries(libLoader);
BootHelper.loadExtensions(VarScript.class.getClassLoader());
}

Expand Down
82 changes: 82 additions & 0 deletions src/main/java/ru/dpohvar/varscript/boot/BootHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner;
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
import org.codehaus.groovy.util.FastArray;
import org.yaml.snakeyaml.Yaml;
import ru.dpohvar.varscript.VarScript;

import java.io.*;
Expand Down Expand Up @@ -111,5 +112,86 @@ public static ArrayList<String> getClassNamesFromPackage(String packageName, boo
return names;
}

private static final Yaml yaml = new Yaml();

public static void loadLibraries(VarScriptClassLoader libLoader){
ClassLoader resourceClassLoader = VarScript.class.getClassLoader();
File configFile = new File(VarScript.dataFolder, "config.yml");
Map<?,?> config = null;
if (configFile.isFile()) config = readYaml(configFile);
Object librariesValue = null;
if (config != null) librariesValue = config.get("libraries");
if (librariesValue == null) {
config = readYaml(resourceClassLoader.getResource("config.yml"));
librariesValue = config.get("libraries");
}
try {
if (librariesValue instanceof String libPath && !libPath.isEmpty()) {
File librariesFolder = getRelativeDir(libPath);
if (!librariesFolder.exists()) librariesFolder.mkdirs();
if (!librariesFolder.isDirectory()) return;
libLoader.addURL(librariesFolder.toURI().toURL());

File[] files = librariesFolder.listFiles();
if (files != null) for (File file : files) {
if (file.getName().toLowerCase().endsWith(".jar")) {
libLoader.addURL(file.toURI().toURL());
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

/**
* Read yaml map from url
*
* @param url url
* @return parsed map
*/
private static Map<?,?> readYaml(URL url){
InputStream is = null;
try{
is = url.openStream();
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
return yaml.load(reader);
} catch (Exception e) {
throw new RuntimeException("Can not load yaml: "+url,e);
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* Read yaml map from file
* @param file file
* @return parsed map
*/
private static Map<?,?> readYaml(File file){
InputStream is = null;
try{
is = new FileInputStream(file);
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
return yaml.load(reader);
} catch (Exception e) {
throw new RuntimeException("Can not load yaml: "+file, e);
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

static File getRelativeDir(String path){
if (path.startsWith("/")) return new File(path);
return new File(VarScript.dataFolder, path);
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package ru.dpohvar.varscript.extension.completer;

import com.google.common.base.Strings;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Comparator;
import static java.util.Comparator.comparingDouble;
import java.util.List;
import java.util.TreeSet;

public class EntityIdCompleter implements TabCompleter{

@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] strings) {

String expression = strings[strings.length-1];
if (!expression.equals("id") && !expression.equals("id(")) return null;
Expand All @@ -29,21 +31,16 @@ public List<String> onTabComplete(CommandSender commandSender, Command command,
location = null;
}
if (location != null) {
List<String> result = new ArrayList<String>();
TreeSet<Entity> entities = new TreeSet<Entity>(new Comparator<Entity>() {
@Override
public int compare(Entity o1, Entity o2) {
double d1 = o1.getLocation().distance(location);
double d2 = o2.getLocation().distance(location);
if (d1 < d2) return -1;
else if (d1 > d2) return 1;
return 0;
}
});
List<String> result = new ArrayList<>();
TreeSet<Entity> entities = new TreeSet<>(comparingDouble(e -> e.getLocation().distance(location)));
entities.addAll( location.getWorld().getEntities() );
var pow = (int) Math.ceil(Math.log10(entities.size()));
var i = 0;
for (Entity entity : entities) {
var prefix = pow == 0 ? "" : Strings.padStart(String.valueOf(i), pow, ' ')+":";
if (entity == commandSender) continue;
result.add("id("+entity.getEntityId()+"/*"+entity.getType()+"*/)");
result.add("id(/*" + prefix + entity.getName() + "*/" + entity.getEntityId() + ")");
i++;
}
return result;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ workspace-sources:
classes: scripts
services: autorun
encoding: UTF-8
libraries: lib
import:
- scan-package: org.bukkit
- scan-package: org.bukkit.block
Expand Down

0 comments on commit 4fdda4e

Please sign in to comment.