Skip to content

Commit

Permalink
Merge pull request #4 from ZorgBtw/dev
Browse files Browse the repository at this point in the history
Merge dev branch to master
  • Loading branch information
ZorgBtw authored Apr 23, 2021
2 parents f1dd60d + 012c667 commit a900d25
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 41 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2 align="center">BungeeSK</h2>
BungeeSk is a Skript addon that allows you to communicate with a bungeecord proxy easily !
BungeeSK is a Skript addon that allows you to communicate with a bungeecord proxy easily !

<br>

Expand All @@ -8,22 +8,22 @@ You can download the last available version of BungeeSK by clicking [here](https
Then you can drop your `BungeeSK.jar` file in your Bungeecord `plugins/` folder and in every Spigot `plugins/` folder too. Make sure you got Skript running on your Spigot servers. (If not, you can download Skript [here](https://github.com/SkriptLang/Skript/releases/latest))

## 🚀 How to run correctly BungeeSK ?
First, you will need to connected your Spigot servers (called as clients) to your Bungeecord proxy (call as server).
First, you will need to connect your Spigot servers (called as clients) to your Bungeecord proxy (call as server).
Here's an example of code to link everything:
```py
```applescript
on server start:
while client is not connected: #Using a while loop to try to connect if the client is not connected
create new bungee connection: #Creation of a new Bungeecord connection
set address of connection to "127.0.0.1" #Use this IP if the Bungeecord is on the same machine
set port of connection to 100 #This port as to be opened if the specified client is not hosted on the same machine as the server
set password of connection to "Strong password" #Complete your password here, this has to be the same as the one in the Bungeecord config
set name of connection to "hub" #Name has to be unique, if the same name is already connected, the connection will end
start new connection with last created connection #Sending connection request to the server
wait 30 seconds #Waiting 30 seconds between 2 connection tries, it's recommended to not decrease this value
while client is not connected: # Using a while loop to try to connect if the client is not connected
create new bungee connection: # Creation of a new Bungeecord connection
set address of connection to "127.0.0.1" # Use this IP if the Bungeecord is on the same machine
set port of connection to 100 # This port as to be opened if the specified client is not hosted on the same machine as the server
set password of connection to "Strong password" # Complete your password here, this has to be the same as the one in the Bungeecord config
set name of connection to "hub" # Name has to be unique, if the same name is already connected, the connection will end
start new connection with last created connection # Sending connection request to the server
wait 30 seconds # Waiting 30 seconds between 2 connection tries, it's recommended to not decrease this value
```

## 📚 Need support or be informed ?
- [**Discord server**](https://discord.gg/PCnyMDsTRA)
- Documentations:<br>
<a href="http://skripthub.net/docs/?addon=BungeeSK"> <img src="http://skripthub.net/static/addon/ViewTheDocsButton.png" height="75"></img></a>
<a href="https://docs.skunity.com/syntax/search/addon:bungeesk"> <img src="https://skunity.com/branding/buttons/get_on_docs_3.png" height="75"></img></a>
<a href="http://skripthub.net/docs/?addon=BungeeSK" target="_blank"> <img src="http://skripthub.net/static/addon/ViewTheDocsButton.png" height="75"></img></a>
<a href="https://docs.skunity.com/syntax/search/addon:bungeesk" target="_blank"> <img src="https://skunity.com/branding/buttons/get_on_docs_3.png" height="75"></img></a>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'fr.zorg'
version '1.0.1'
version '1.0.2'

def mcVersion = '1.16'
def subVersion = '.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Description("Make bungee or spigot execute command")
@Since("1.0.0")
@Examples({"make bungee console execute command \"alert This is an alert !\"",
"make server \"hub\" execute command \"say Hi everyone !\""})
"make server \"hub\" console execute command \"say Hi everyone !\""})

public class EffExecuteCommand extends Effect {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fr.zorg.bungeesk.bukkit.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import fr.zorg.bungeesk.bukkit.sockets.ConnectionClient;
import fr.zorg.bungeesk.bukkit.utils.BungeePlayer;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

@Name("All of the bungee players on specific server")
@Description("Returns every bungee player on a specific server")
@Examples("loop all bungee players on server \"lobby\":\n" +
"\tsend \"%loop-bungeeplayer%\"")
@Since("1.0.2")
public class ExprAllPlayersOnServer extends SimpleExpression<BungeePlayer> {

static {
Skript.registerExpression(ExprAllPlayersOnServer.class,
BungeePlayer.class,
ExpressionType.SIMPLE,
"[(all [[of] the]|the)] bungee players on server %string%");
}

private Expression<String> server;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
server = (Expression<String>) exprs[0];
return true;
}

@Nullable
@Override
protected BungeePlayer[] get(Event e) {
assert ConnectionClient.get() != null;
String result = ConnectionClient.get().future("ALLBUNGEEPLAYERSONSERVERµ" + server.getSingle(e));
result = result.replace("lobby^", "");
List<BungeePlayer> players = new ArrayList<>();
if (result.equals("NONE")) return null;
for (String player : result.split("\\^")) {
String name = player.split("\\$")[0];
String uuid = player.split("\\$")[1];
players.add(new BungeePlayer(name, uuid));
}
return players.toArray(new BungeePlayer[0]);
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<? extends BungeePlayer> getReturnType() {
return BungeePlayer.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "every bungee player connected on server " + server.toString(e, debug);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.zorg.bungeesk.bukkit.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import fr.zorg.bungeesk.bukkit.BungeeSK;
import fr.zorg.bungeesk.bukkit.sockets.ConnectionClient;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Client real name")
@Description("Returns the client's name in the Bungeecord proxy configuration")
@Examples("set {_name} to real name of this client")
@Since("1.0.2")
public class ExprClientRealName extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprClientRealName.class,
String.class,
ExpressionType.SIMPLE,
"real name of this client",
"this client's real name");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
return true;
}

@Nullable
@Override
protected String[] get(Event e) {
assert ConnectionClient.get() != null;
final String result = ConnectionClient.get().future("CLIENTREALNAMEµ" + BungeeSK.getInstance().getServer().getIp() + ":" + BungeeSK.getInstance().getServer().getPort());
return new String[] { result };
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "client's real name on the bungeecord";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public Class<?>[] acceptChange(ChangeMode mode) {
public void change(Event e, Object[] delta, ChangeMode mode) {
for (final ClientSettings clientSettings : getExpr().getArray(e)) {
final String password = (String) delta[0];
if (password.contains("µ")) {
BungeeSK.getInstance().getLogger().log(Level.SEVERE, "Password of a connection must not contain the µ character");
return;
}
clientSettings.setPassword(password);
}
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/fr/zorg/bungeesk/bukkit/sockets/ConnectionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.zorg.bungeesk.common.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.Nullable;

import java.io.*;
Expand Down Expand Up @@ -64,7 +65,7 @@ private ConnectionClient(final Socket socket, final String name, final String pa
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
this.writer = new PrintWriter(socket.getOutputStream(), true);
this.toComplete = new HashMap<>();
this.encryption = new AESEncryption(password);
this.encryption = new AESEncryption(password, BungeeSK.getInstance().getLogger());
this.write("name=" + name + "µpassword=" + password);
this.readThread = new Thread(this::read);
this.readThread.setDaemon(true);
Expand Down Expand Up @@ -107,12 +108,11 @@ public void read() {
}
case "CONNECTED_SUCCESSFULLY": {
final Event event = new ClientConnectEvent();
BungeeSK.getInstance().getServer().getPluginManager().callEvent(event);
Bukkit.getScheduler().runTask(BungeeSK.getInstance(), () -> Bukkit.getPluginManager().callEvent(event));
break;
}
case "SEND_SKRIPTS": {
final String[] flux = received.get(0).split("™");
System.out.println("flux.length = " + flux.length);
final File folder = new File("plugins/Skript/scripts/BungeeSK");
if (!folder.exists())
folder.mkdirs();
Expand Down Expand Up @@ -154,12 +154,12 @@ public void read() {
}
case "LOGINEVENT": {
final Event event = new BungeePlayerJoinEvent(new BungeePlayer(separateDatas[1], separateDatas[2]));
BungeeSK.getInstance().getServer().getPluginManager().callEvent(event);
Bukkit.getScheduler().runTask(BungeeSK.getInstance(), () -> Bukkit.getPluginManager().callEvent(event));
break;
}
case "LEAVEEVENT": {
final Event event = new BungeePlayerLeaveEvent(new BungeePlayer(separateDatas[1], separateDatas[2]));
BungeeSK.getInstance().getServer().getPluginManager().callEvent(event);
Bukkit.getScheduler().runTask(BungeeSK.getInstance(), () -> Bukkit.getPluginManager().callEvent(event));
break;
}
case "ALLBUNGEEPLAYERS": {
Expand All @@ -182,29 +182,37 @@ public void read() {
this.putFuture("ISCONNECTEDµ" + dataArray[0], dataArray[1]);
break;
}

case "SERVERSWITCHEVENT": {
final String[] dataArray = separateDatas[1].split("\\^");
final BungeePlayer bungeePlayer = new BungeePlayer(dataArray[0].split("\\$")[0], dataArray[1].split("\\$")[1]);
final Event event = new ServerSwitchEvent(bungeePlayer, dataArray[1]);
BungeeSK.getInstance().getServer().getPluginManager().callEvent(event);
Bukkit.getScheduler().runTask(BungeeSK.getInstance(), () -> Bukkit.getPluginManager().callEvent(event));
break;
}
case "ALLBUNGEESERVERS": {
this.putFuture("ALLBUNGEESERVERSµ", separateDatas[1]);
break;
}
case "CLIENTREALNAME": {
this.putFuture("CLIENTREALNAMEµ" + separateDatas[1].split("\\^")[0], separateDatas[1].split("\\^")[1]);
break;
}
case "ALLBUNGEEPLAYERSONSERVER": {
final String server = separateDatas[1].split("\\^")[0];
this.putFuture("ALLBUNGEEPLAYERSONSERVERµ" + server, separateDatas[1]);
}
}
}
} catch (
IOException e) {
} catch (IOException e) {
this.forceDisconnect();
}

}

public void disconnect() {
try {
this.writer.println("DISCONNECT");
this.writer.println(Arrays.toString("DISCONNECT".getBytes(StandardCharsets.UTF_8)));
} catch (final Exception ignored) {
}
this.forceDisconnect();
Expand All @@ -217,7 +225,7 @@ public void forceDisconnect() {
this.reader.close();
this.writer.close();
final Event event = new ClientDisconnectEvent();
BungeeSK.getInstance().getServer().getPluginManager().callEvent(event);
Bukkit.getScheduler().runTask(BungeeSK.getInstance(), () -> Bukkit.getPluginManager().callEvent(event));
this.readThread.interrupt();
}
} catch (IOException e) {
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/fr/zorg/bungeesk/bungee/sockets/ClientServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -52,8 +54,8 @@ public void read() {
String rawData = reader.readLine();
if (rawData == null) continue;
final Server server = BungeeSK.getInstance().getServer();
final String data = BungeeSK.getInstance().getServer().encryption.decrypt(Utils.getMessage(rawData));
if (this.name == null) {
final String data = BungeeSK.getInstance().getServer().encryption.decrypt(Utils.getMessage(rawData), false);
if (!server.isClient(socket)) {
if (!data.contains("µ")) {
this.disconnect();
Expand Down Expand Up @@ -104,6 +106,7 @@ public void read() {
continue;
}

final String data = BungeeSK.getInstance().getServer().encryption.decrypt(Utils.getMessage(rawData));
final String[] separateDatas = data.split("µ");

final String header = separateDatas[0];
Expand Down Expand Up @@ -198,6 +201,30 @@ else if (args.equalsIgnoreCase("all"))
this.write(builder.toString());
break;
}
case "CLIENTREALNAME": {
final SocketAddress address = new InetSocketAddress(args.split(":")[0], Integer.parseInt(args.split(":")[1]));
final String finalArgs = args;
BungeeSK.getInstance().getProxy().getServers().forEach((s, serverInfo) -> {
if (serverInfo.getSocketAddress().equals(address))
this.write("CLIENTREALNAMEµ" + finalArgs + "^" + s);
});
break;
}
case "ALLBUNGEEPLAYERSONSERVER": {
final ServerInfo bungeeServ = BungeeSK.getInstance().getProxy().getServerInfo(args);
if (bungeeServ == null || bungeeServ.getPlayers().toArray().length == 0) {
this.write("ALLBUNGEEPLAYERSONSERVERµ" + args + "^NONE");
break;
}
final StringBuilder builder = new StringBuilder("ALLBUNGEEPLAYERSONSERVERµ" + args + "^");
final Object lastPlayer = bungeeServ.getPlayers().toArray()[bungeeServ.getPlayers().size() - 1];
for (final ProxiedPlayer player : BungeeSK.getInstance().getProxy().getPlayers()) {
builder.append(player.getName()).append("$").append(player.getUniqueId().toString());
if (!player.equals(lastPlayer)) builder.append("^");
}
this.write(builder.toString());
break;
}
}
}
} catch (IOException e) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/fr/zorg/bungeesk/bungee/sockets/Server.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.zorg.bungeesk.bungee.sockets;

import fr.zorg.bungeesk.bungee.BungeeSK;
import fr.zorg.bungeesk.bungee.storage.BungeeConfig;
import fr.zorg.bungeesk.common.encryption.AESEncryption;
import fr.zorg.bungeesk.common.utils.Utils;
Expand All @@ -22,7 +23,7 @@ public Server(final int port, final String passwd) throws IOException {
this.password = passwd;
this.clients = new ArrayList<>();
this.servSocket = new ServerSocket(port);
this.encryption = new AESEncryption(passwd);
this.encryption = new AESEncryption(passwd, BungeeSK.getInstance().getLogger());

this.waitingConnection = new Thread(this::waitForConn);
this.waitingConnection.setDaemon(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private BungeeConfig() {

public void load() {
this.file = new File(BungeeSK.getInstance().getDataFolder(), "config.yml");
if (!BungeeSK.getInstance().getDataFolder().exists()) BungeeSK.getInstance().getDataFolder().mkdir();
if (!this.file.exists()) {
try (final InputStream in = BungeeSK.getInstance().getResourceAsStream("config.yml")) {
Files.copy(in, this.file.toPath());
Expand All @@ -67,10 +68,6 @@ public void load() {
} catch (final IOException | IllegalAccessException e) {
e.printStackTrace();
}
if (this.password.contains("µ")) {
BungeeSK.getInstance().getLogger().log(Level.SEVERE, "Password must not contain the µ character.");
//Disable plugin without errors
}
}

public int getPort() {
Expand Down
Loading

0 comments on commit a900d25

Please sign in to comment.