Skip to content

Commit

Permalink
🦾 Spigot command & Group permissions & fixed bug that started servers…
Browse files Browse the repository at this point in the history
… after going ingame & added lombok

This is a bit bigger release. Please update Spigot & BungeeCord and update permissions!
  • Loading branch information
Luuuuuis committed Sep 25, 2020
1 parent 2ff1311 commit d744429
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 123 deletions.
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.luuuuuis</groupId>
<artifactId>PrivateServer</artifactId>
<version>0.9</version>
<version>0.10</version>

<build>
<plugins>
Expand Down Expand Up @@ -88,14 +88,23 @@
<scope>provided</scope>
</dependency>

<!--Spigot API-->
<!--Spigot API built with spigot-buildtools locally-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>


<!-- https://github.com/MinecraftLibraries/NPCLib -->
<dependency>
<groupId>net.jitse</groupId>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/de/luuuuuis/privateserver/bungee/PrivateServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import com.google.gson.GsonBuilder;
import de.luuuuuis.privateserver.bungee.commands.PrivateServerCmd;
import de.luuuuuis.privateserver.bungee.events.DisconnectListener;
import de.luuuuuis.privateserver.bungee.events.PluginMessageReceive;
import de.luuuuuis.privateserver.bungee.events.ServerSwitch;
import de.luuuuuis.privateserver.bungee.events.TabComplete;
import de.luuuuuis.privateserver.bungee.util.CloudServer;
import de.luuuuuis.privateserver.bungee.util.Config;
import de.luuuuuis.privateserver.bungee.util.Metrics;
import de.luuuuuis.privateserver.bungee.util.Updater;
import lombok.Getter;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginManager;

public class PrivateServer extends Plugin {

public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
@Getter
private static PrivateServer instance;

public static PrivateServer getInstance() {
return instance;
}

@Override
public void onDisable() {
Expand Down Expand Up @@ -54,15 +54,22 @@ public void onEnable() {
//updater
new Updater();


//commands
PluginManager pluginManager = getProxy().getPluginManager();
pluginManager.registerCommand(this, new PrivateServerCmd());
pluginManager.registerListener(this, new TabComplete());
pluginManager.registerListener(this, new ServerSwitch());
pluginManager.registerListener(this, new DisconnectListener());

// receive plugin messages and execute spigot commands on BungeeCord
getProxy().registerChannel("pv:cmd");
pluginManager.registerListener(this, new PluginMessageReceive());


/*
bStats Metrics https://github.com/Bastian/bStats-Metrics/blob/master/bstats-bungeecord/src/examples/java/ExamplePlugin.java
Available here: https://bstats.org/plugin/bungeecord/PrivateServer/8521
to disable these metrics change the bStats config and copy it into you template folder but please don't :C
*/
Metrics metrics = new Metrics(this, 8521);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void execute(CommandSender sender, String[] strings) {

Invitee invitee = Invitee.getInvitee(p, CloudServer.getCloudServer(strings[1]));
if (invitee == null) {
CloudServer cs = CloudServer.getCloudServers().stream().filter(cloudServer1 -> cloudServer1.getOwner().getUniqueId().equals(p.getUniqueId())).findFirst().orElse(null);
CloudServer cs = CloudServer.getCloudServers().stream().filter(cloudServer1 -> cloudServer1.getOwner().getPlayer().getUniqueId().equals(p.getUniqueId())).findFirst().orElse(null);
if (cs != null) {
playerExecutorBridge.sendPlayer(cloudPlayer, cs.getName());
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.luuuuuis.privateserver.bungee.events;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

public class PluginMessageReceive implements Listener {

/**
* access and execute every privateserver command on bungee. No need to check anything on spigot cause bungee does :3
*
* @param e event
*/
@EventHandler
public void onReceive(PluginMessageEvent e) {
if (!e.getTag().equals("pv:cmd")) return;

@SuppressWarnings("UnstableApiUsage") ByteArrayDataInput in = ByteStreams.newDataInput(e.getData());
String utf_in = in.readUTF();
if (!(e.getReceiver() instanceof ProxiedPlayer)) return; // ah shit, i guess,,,

ProxiedPlayer player = (ProxiedPlayer) e.getReceiver();

ProxyServer.getInstance().getPluginManager().dispatchCommand(player, "privateserver " + utf_in);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
import de.dytanic.cloudnet.lib.server.template.Template;
import de.dytanic.cloudnet.lib.server.template.TemplateResource;
import de.dytanic.cloudnet.lib.utility.document.Document;
import lombok.Getter;
import lombok.Setter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.*;
import java.util.stream.Collectors;

@Getter
public class CloudServer {

private final static List<CloudServer> cloudServers = new ArrayList<>();
private final String group;
private final Owner owner;
private final ServerGroupMode groupMode;
@Setter
private String template = Config.getInstance().getTemplate();
private String name;

Expand All @@ -44,7 +48,7 @@ public void start() {


CloudAPI.getInstance().startGameServer(CloudAPI.getInstance().getServerGroupData(group),
new ServerConfig(true, "null", new Document("uniqueId", owner.getUniqueId()), System.currentTimeMillis()),
new ServerConfig(true, "null", new Document("uniqueId", owner.getPlayer().getUniqueId()), System.currentTimeMillis()),
Config.getInstance().getMemory(),
new String[0],
new Template(template, TemplateResource.LOCAL, null, new String[0], Collections.emptyList()),
Expand Down Expand Up @@ -110,6 +114,12 @@ private boolean isAllowed() {
return false;
}

// check if user has permission to start this group
if (!owner.getPlayer().hasPermission("privateserver.start." + group)) {
owner.sendMessage(Config.getInstance().getPrefix() + "You are not allowed to start this group.");
return false;
}

return true;
}

Expand All @@ -124,9 +134,6 @@ private String createName() {
return name;
}

public void setTemplate(String template) {
this.template = template;
}

public List<String> getPlayers() {
return CloudAPI.getInstance().getServerInfo(name).getPlayers();
Expand All @@ -136,27 +143,12 @@ public int getMaxPlayers() {
return CloudAPI.getInstance().getServerInfo(name).getMaxPlayers();
}

public String getName() {
return name;
}

public void setName(int ID) {
if (cloudServers.stream().noneMatch(server -> server.getName().equals(server.getOwner().getPlayer().getName() + "-" + ID))) {
this.name = owner.getPlayer().getName() + "-" + ID;
}
}

public String getGroup() {
return group;
}

public ServerGroupMode getGroupMode() {
return groupMode;
}

public Owner getOwner() {
return owner;
}


}
30 changes: 2 additions & 28 deletions src/main/java/de/luuuuuis/privateserver/bungee/util/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.luuuuuis.privateserver.bungee.util;

import de.luuuuuis.privateserver.bungee.PrivateServer;
import lombok.Getter;

import java.io.File;
import java.io.FileReader;
Expand All @@ -12,6 +13,7 @@
import java.util.HashMap;
import java.util.Objects;

@Getter
public class Config {

private static Config instance;
Expand Down Expand Up @@ -65,32 +67,4 @@ private static void read(String path) {
e.printStackTrace();
}
}

public String getPrefix() {
return prefix;
}

public String getTemplate() {
return template;
}

public HashMap<String, Object> getMessages() {
return messages;
}

public ArrayList<String> getGroups() {
return groups;
}

public int getMaxServersRunning() {
return maxServersRunning;
}

public int getMaxServersPerUser() {
return maxServersPerUser;
}

public int getMemory() {
return memory;
}
}
14 changes: 2 additions & 12 deletions src/main/java/de/luuuuuis/privateserver/bungee/util/Invitee.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.luuuuuis.privateserver.bungee.util;

import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
Expand All @@ -12,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;

@Getter
public class Invitee {

private final static List<Invitee> invitees = new ArrayList<>();
Expand Down Expand Up @@ -74,16 +76,4 @@ public void revoke() {
public void sendMessage(String message) {
player.sendMessage(TextComponent.fromLegacyText(message));
}

public Owner getOwner() {
return owner;
}

public ProxiedPlayer getPlayer() {
return player;
}

public CloudServer getCloudServer() {
return cloudServer;
}
}
60 changes: 43 additions & 17 deletions src/main/java/de/luuuuuis/privateserver/bungee/util/Owner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import de.dytanic.cloudnet.api.CloudAPI;
import de.dytanic.cloudnet.api.player.PlayerExecutorBridge;
import de.dytanic.cloudnet.lib.player.CloudPlayer;
import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;

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

@Getter
public class Owner {

private final static List<Owner> owners = new ArrayList<>();
Expand Down Expand Up @@ -45,30 +46,33 @@ public static Owner getOwner(ProxiedPlayer player) {
* @param cloudServer the CloudServer you want to send the player
*/
public void sendTitle(CloudServer cloudServer) {
Thread th = new Thread(() -> {
new Thread(() -> {
//send title & to server
int i = 0;
while (CloudAPI.getInstance().getServerInfo(cloudServer.getName()) == null || !CloudAPI.getInstance().getServerInfo(cloudServer.getName()).isOnline()) {
while (CloudAPI.getInstance().getServerInfo(cloudServer.getName()) == null ||
!CloudAPI.getInstance().getServerInfo(cloudServer.getName()).isOnline()) {
i++;
StringBuilder dots = new StringBuilder();
for (int j = 0; j < i; j++) {
dots.append(".");
}

playerExecutorBridge.sendTitle(cloudPlayer, "", String.format(Config.getInstance().getMessages().get("startingTitle").toString(), dots), 0, 20, 0);
playerExecutorBridge.sendTitle(cloudPlayer, "",
String.format(Config.getInstance().getMessages().get("startingTitle").toString(), dots),
0, 20, 0);

if (i >= 3) i = 0;

try {
//noinspection BusyWait
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
sendPlayer(cloudServer);

});
th.start();
}).start();
}

private void sendPlayer(CloudServer cloudServer) {
Expand All @@ -93,16 +97,38 @@ public void removeServer(CloudServer cloudServer) {
if (servers.isEmpty())
owners.remove(this);
}
}

public UUID getUniqueId() {
return player.getUniqueId();
}

public ProxiedPlayer getPlayer() {
return player;
}

public List<CloudServer> getServers() {
return servers;
}
}
//@Data class Task implements Callable<Boolean> {
//
// private final PlayerExecutorBridge playerExecutorBridge;
// private final CloudPlayer cloudPlayer;
// private final CloudServer cloudServer;
//
// @Override
// public Boolean call() throws InterruptedException {
//
// //send title
// int i = 0;
// do {
// i++;
// StringBuilder dots = new StringBuilder();
// for (int j = 0; j < i; j++) {
// dots.append(".");
// }
//
// playerExecutorBridge.sendTitle(cloudPlayer, "", String.format(Config.getInstance().getMessages().get("startingTitle").toString(), dots), 0, 20, 0);
//
// if (i >= 3) i = 0;
//
//
// Thread.sleep(750);
// } while ((CloudAPI.getInstance().getServerInfo(cloudServer.getName()) == null
// || !CloudAPI.getInstance().getServerInfo(cloudServer.getName()).isOnline())
// && !Thread.interrupted());
//
// return true;
// }
//
//}
Loading

0 comments on commit d744429

Please sign in to comment.