-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added velocity support #641
base: master
Are you sure you want to change the base?
Changes from 1 commit
bba5a66
9c900f8
6d8de6f
de7ed99
d9f9305
36d3cec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,10 @@ | |
import me.leoko.advancedban.utils.Punishment; | ||
import me.leoko.advancedban.utils.tabcompletion.TabCompleter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
@@ -45,11 +44,9 @@ public class VelocityMethods implements MethodInterface { | |
private final File configFile; | ||
private final File messageFile; | ||
private final File layoutFile; | ||
private final File mysqlFile; | ||
private YamlFile config; | ||
private YamlFile messages; | ||
private YamlFile layouts; | ||
private YamlFile mysql; | ||
|
||
|
||
|
||
|
@@ -62,7 +59,6 @@ public VelocityMethods(ProxyServer server, @DataDirectory Path dataDirectory, Lo | |
this.configFile = new File(getDataFolder(), "config.yml"); | ||
this.messageFile =new File(getDataFolder(), "Messages.yml"); | ||
this.layoutFile = new File(getDataFolder(), "Layouts.yml"); | ||
this.mysqlFile = new File(getDataFolder(), "MySQL.yml"); | ||
|
||
if (server.getPluginManager().getPlugin("luckperms").isPresent()) { | ||
luckPermsSupport = true; | ||
|
@@ -79,27 +75,25 @@ public VelocityMethods(ProxyServer server, @DataDirectory Path dataDirectory, Lo | |
@Override | ||
public void loadFiles() { | ||
try { | ||
|
||
|
||
config = new YamlFile(configFile); | ||
messages = new YamlFile(messageFile); | ||
layouts = new YamlFile(layoutFile); | ||
mysql = new YamlFile(mysqlFile); | ||
|
||
if (!configFile.exists()) { | ||
config.createNewFile(); | ||
saveResource("config.yml", true); | ||
} | ||
if (!messageFile.exists()) { | ||
messages.createNewFile(); | ||
saveResource("Messages.yml", true); | ||
} | ||
if (!layoutFile.exists()) { | ||
layouts.createNewFile(); | ||
} | ||
if (!mysqlFile.exists()) { | ||
mysql.createNewFile(); | ||
saveResource("Layouts.yml", true); | ||
} | ||
|
||
config.load(); | ||
messages.load(); | ||
layouts.load(); | ||
mysql.load(); | ||
|
||
|
||
} catch (IOException e) { | ||
|
@@ -143,17 +137,44 @@ public String[] getKeys(Object file, String path) { | |
|
||
@Override | ||
public Object getConfig() { | ||
return config; | ||
try{ | ||
if(config == null){ | ||
config = new YamlFile(configFile); | ||
config.load(); | ||
} | ||
return config; | ||
}catch (Exception e){ | ||
logger.error(e.getMessage()); | ||
return config; | ||
} | ||
} | ||
|
||
@Override | ||
public Object getMessages() { | ||
return messages; | ||
try{ | ||
if(messages == null){ | ||
messages = new YamlFile(messageFile); | ||
messages.load(); | ||
} | ||
return messages; | ||
}catch (Exception e){ | ||
logger.error(e.getMessage()); | ||
return messages; | ||
} | ||
} | ||
|
||
@Override | ||
public Object getLayouts() { | ||
return layouts; | ||
try{ | ||
if(layouts == null){ | ||
layouts = new YamlFile(layoutFile); | ||
layouts.load(); | ||
} | ||
return layouts; | ||
}catch (Exception e){ | ||
logger.error(e.getMessage()); | ||
return layouts; | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -425,4 +446,67 @@ public void log(String s) { | |
public boolean isUnitTesting() { | ||
return false; | ||
} | ||
|
||
//Extracted from https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java | ||
|
||
public void saveResource(String resourcePath, boolean replace) { | ||
if (resourcePath == null || resourcePath.equals("")) { | ||
throw new IllegalArgumentException("ResourcePath cannot be null or empty"); | ||
} | ||
|
||
resourcePath = resourcePath.replace('\\', '/'); | ||
InputStream in = getResource(resourcePath); | ||
if (in == null) { | ||
throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found"); | ||
} | ||
|
||
File outFile = new File(getDataFolder(), resourcePath); | ||
int lastIndex = resourcePath.lastIndexOf('/'); | ||
File outDir = new File(getDataFolder(), resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0)); | ||
|
||
if (!outDir.exists()) { | ||
outDir.mkdirs(); | ||
} | ||
|
||
try { | ||
if (!outFile.exists() || replace) { | ||
OutputStream out = new FileOutputStream(outFile); | ||
byte[] buf = new byte[1024]; | ||
int len; | ||
while ((len = in.read(buf)) > 0) { | ||
out.write(buf, 0, len); | ||
} | ||
out.close(); | ||
in.close(); | ||
} else { | ||
logger.warn("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary warn when file exists (99% going to be the case after first use) |
||
} | ||
} catch (IOException ex) { | ||
logger.error("Could not save " + outFile.getName() + " to " + outFile, ex); | ||
} | ||
} | ||
|
||
|
||
public InputStream getResource(String filename) { | ||
if (filename == null) { | ||
throw new IllegalArgumentException("Filename cannot be null"); | ||
} | ||
|
||
try { | ||
URL url = getClass().getClassLoader().getResource(filename); | ||
|
||
if (url == null) { | ||
return null; | ||
} | ||
|
||
URLConnection connection = url.openConnection(); | ||
connection.setUseCaches(false); | ||
return connection.getInputStream(); | ||
} catch (IOException ex) { | ||
return null; | ||
} | ||
} | ||
|
||
Comment on lines
+452
to
+509
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this implementation is based off of bukkit, does this work reliably in bungeecord and velocity too? |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible, please specify which packages from java.io are used (avoid unecessary imports using * wildcards)