Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 18 additions & 55 deletions bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>me.leoko.advancedban</groupId>
<artifactId>AdvancedBan</artifactId>
Expand All @@ -26,38 +26,36 @@
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Add velocity module -->
<dependency>
<groupId>me.leoko.advancedban</groupId>
<artifactId>AdvancedBan-Proxy</artifactId>
<artifactId>AdvancedBan-Velocity</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<relocations>
<relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>${project.groupId}.shaded.org.apache.commons</shadedPattern>
</relocation>
<!-- <relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>${project.groupId}.shaded.org.slf4j</shadedPattern>
</relocation> -->
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>${project.groupId}.shaded.com.zaxxer.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>org.hsqldb</pattern>
<shadedPattern>${project.groupId}.shaded.org.hsqldb</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>${project.groupId}.shaded.org.apache.commons</shadedPattern>
</relocation>
<!-- Removed org.slf4j relocation making issues with velocity enabling plugin -->
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>${project.groupId}.shaded.com.zaxxer.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>org.hsqldb</pattern>
<shadedPattern>${project.groupId}.shaded.org.hsqldb</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>${project.groupId}.shaded.org.bstats</shadedPattern>
Expand All @@ -76,41 +74,6 @@
</execution>
</executions>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
<configuration>
<classifier>sources</classifier>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>javadoc</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 1 addition & 1 deletion bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<!-- RedisBungee -->
<repository>
<id>md_5-snapshots</id>
<url>http://repo.md-5.net/content/repositories/snapshots/</url>
<url>https://repo.md-5.net/content/repositories/snapshots/</url>
</repository>
<!-- CloudNet -->
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class DynamicDataSource {
public DynamicDataSource(boolean preferMySQL) throws ClassNotFoundException {
MethodInterface mi = Universal.get().getMethods();
if (preferMySQL) {
String ip = mi.getString(mi.getMySQLFile(), "MySQL.IP", "Unknown");
String dbName = mi.getString(mi.getMySQLFile(), "MySQL.DB-Name", "Unknown");
String usrName = mi.getString(mi.getMySQLFile(), "MySQL.Username", "Unknown");
String password = mi.getString(mi.getMySQLFile(), "MySQL.Password", "Unknown");
String properties = mi.getString(mi.getMySQLFile(), "MySQL.Properties", "verifyServerCertificate=false&useSSL=false&useUnicode=true&characterEncoding=utf8");
int port = mi.getInteger(mi.getMySQLFile(), "MySQL.Port", 3306);
String ip = mi.getString(mi.getConfig(), "MySQL.IP", "Unknown");
String dbName = mi.getString(mi.getConfig(), "MySQL.DB-Name", "Unknown");
String usrName = mi.getString(mi.getConfig(), "MySQL.Username", "Unknown");
String password = mi.getString(mi.getConfig(), "MySQL.Password", "Unknown");
String properties = mi.getString(mi.getConfig(), "MySQL.Properties", "verifyServerCertificate=false&useSSL=false&useUnicode=true&characterEncoding=utf8");
int port = mi.getInteger(mi.getConfig(), "MySQL.Port", 3306);

Class.forName("com.mysql.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?"+properties);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/me/leoko/advancedban/utils/Punishment.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public void create(boolean silent) {
if (mi.isOnline(getName())) {
final Object p = mi.getPlayer(getName());

PunishmentManager.get().getLoadedPunishments(false).add(this);

if (getType().getBasic() == PunishmentType.BAN || getType() == PunishmentType.KICK) {
mi.runSync(() -> mi.kickPlayer(getName(), getLayoutBSN()));
PunishmentManager.get().getLoadedPunishments(false).add(this);
} else {
if (getType().getBasic() != PunishmentType.NOTE)
for (String str : getLayout()) {
mi.sendMessage(p, str);
}
PunishmentManager.get().getLoadedPunishments(false).add(this);
}
}

Expand Down
14 changes: 8 additions & 6 deletions velocity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>me.leoko.advancedban</groupId>
<artifactId>AdvancedBan</artifactId>
<version>2.3.0</version>
</parent>


<artifactId>AdvancedBan-Proxy</artifactId>
<artifactId>AdvancedBan-Velocity</artifactId>
<packaging>jar</packaging>

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>



<repositories>
<repository>
<id>papermc-repo</id>
Expand Down Expand Up @@ -68,5 +64,11 @@
<version>1.7.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
</project>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public VelocityMain(ProxyServer server, @DataDirectory Path dataDirectory, Logge
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {

Universal.get().setup(new VelocityMethods(server, dataDirectory, logger));

server.getEventManager().register(this, new ConnectionListenerVelocity());
server.getEventManager().register(this, new ChatListenerVelocity());

Universal.get().setup(new VelocityMethods(server, dataDirectory, logger));

}

public static VelocityMain get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Copy link
Collaborator

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)

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;
Expand All @@ -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;



Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

}