Skip to content

Commit

Permalink
Added support for BungeeCord plugins
Browse files Browse the repository at this point in the history
Took 1 hour 0 minutes
  • Loading branch information
SynkMC committed Aug 16, 2024
1 parent 8a17166 commit 5f77766
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 20 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,25 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bungeecord</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
10 changes: 3 additions & 7 deletions src/main/java/cc/synkdev/synkLibs/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public class Lang {
JavaPlugin plugin;
public File file;
public Lang(JavaPlugin plugin) {
this.plugin = plugin;
file = new File(plugin.getDataFolder().getParentFile(), "SynkLibs");
SynkLibs core = SynkLibs.getInstance();
public Lang(File dataFolder) {
file = new File(dataFolder.getParent(), "SynkLibs");
init();
}

public FileConfiguration config;
SynkLibs core = SynkLibs.getInstance();

public void init() {
if (!file.exists()) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/cc/synkdev/synkLibs/SynkLibs.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
public final class SynkLibs extends JavaPlugin implements SynkPlugin {
@Getter private static SynkLibs instance;
@Setter String prefix = ChatColor.translateAlternateColorCodes('&', "&8[&6SynkLibs&8] » &r");
@Setter @Getter SynkPlugin spl = null;
public Map<SynkPlugin, String> availableUpdates = new HashMap<>();
@Setter @Getter static SynkPlugin spl = null;
public static Map<SynkPlugin, String> availableUpdates = new HashMap<>();
public void log(String s) {
Bukkit.getConsoleSender().sendMessage(prefix+" "+s);
}
Expand All @@ -24,10 +24,9 @@ public void log(String s) {
public void onEnable() {
instance = this;
setSpl(this);
new Lang(this);
new Metrics(this, 23015);
Bukkit.getPluginManager().registerEvents(new Utils(this), this);
new Utils(this).checkUpdate();
Utils.checkUpdate(this, this);
}

@Override
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/cc/synkdev/synkLibs/Utils.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package cc.synkdev.synkLibs;

import cc.synkdev.synkLibs.bungee.SynkLibsBungee;
import cc.synkdev.synkLibs.components.SynkPlugin;
import net.md_5.bungee.api.plugin.Plugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Utils implements Listener {
private static SynkLibs score = SynkLibs.getInstance();
private SynkLibs core = SynkLibs.getInstance();
private SynkLibsBungee bcore = SynkLibsBungee.getInstance();
SynkPlugin spl;
static Lang slang;
public Utils(SynkPlugin spl) {
this.spl = spl;
}
Lang lang = new Lang(core);
Lang lang;
public static void log(String s) {
Bukkit.getConsoleSender().sendMessage(score.getSpl().prefix()+" "+s);
}
Expand All @@ -30,7 +36,16 @@ public void log(String s, Boolean prefix) {
Bukkit.getConsoleSender().sendMessage(s);
}

public void checkUpdate() {
public static void checkUpdate(SynkPlugin spl, JavaPlugin plugin) {
checkUpdate(spl, plugin.getDataFolder());
}

public static void checkUpdate(SynkPlugin spl, Plugin plugin) {
checkUpdate(spl, plugin.getDataFolder());
}

private static void checkUpdate(SynkPlugin spl, File dataFolder) {
slang = new Lang(dataFolder);
try {
URL url = new URL("https://synkdev.cc/ver/"+spl.name());
BufferedReader in = new BufferedReader(
Expand All @@ -39,12 +54,12 @@ public void checkUpdate() {
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equals(spl.ver())) {
log(ChatColor.GREEN + spl.name() + " " + lang.translate("upToDate"));
log(ChatColor.GREEN + spl.name() + " " + slang.translate("upToDate"));
} else {
log(ChatColor.GREEN + lang.translate("updateAvailable") + " " + spl.name() + ": v" + inputLine);
log(ChatColor.GREEN + lang.translate("downloadHere") + ": "+spl.dlLink());
if (core.availableUpdates.containsKey(spl)) core.availableUpdates.replace(spl, inputLine);
else core.availableUpdates.put(spl, inputLine);
log(ChatColor.GREEN + slang.translate("updateAvailable") + " " + spl.name() + ": v" + inputLine);
log(ChatColor.GREEN + slang.translate("downloadHere") + ": "+spl.dlLink());
if (SynkLibs.availableUpdates.containsKey(spl)) SynkLibs.availableUpdates.replace(spl, inputLine);
else SynkLibs.availableUpdates.put(spl, inputLine);
}
break;
}
Expand All @@ -56,10 +71,11 @@ public void checkUpdate() {

@EventHandler
public void join (PlayerJoinEvent event) {
lang = new Lang(core.getDataFolder());
if (((OfflinePlayer) event.getPlayer()).isOp()) core.availableUpdates.forEach((s, s2) -> {
Player p = event.getPlayer();
core.log(lang.translate("updateAvailable") + " "+s+"!");
core.log(lang.translate("downloadHere")+": "+s.dlLink());
p.sendMessage(lang.translate("updateAvailable") + " "+s+"!");
p.sendMessage(lang.translate("downloadHere")+": "+s.dlLink());
});
}
}
90 changes: 90 additions & 0 deletions src/main/java/cc/synkdev/synkLibs/bungee/LangBungee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cc.synkdev.synkLibs.bungee;

import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

import java.io.*;
import java.net.URL;

public class LangBungee {
public File file;
SynkLibsBungee core = SynkLibsBungee.getInstance();
public LangBungee(File dataFolder) {
file = new File(dataFolder.getParent(), "SynkLibs");
init();
}

public Configuration config;

public void init() {
if (!file.exists()) {
file.mkdirs();
}
file = new File(file, "lang.yml");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

try {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
load();
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void load() {
File tempFile = new File(file.getParent(), "temp-"+System.currentTimeMillis()+".yml");
try {
URL url = new URL("https://synkdev.cc/storage/lang-libs-bungee.php");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String line;
while ((line = reader.readLine()) != null) {
for (String s : line.split("<br>")) {
String id = s.split(":")[0];
if (config.contains(id)) {
writer.write(id + ": " + config.getString(id));
writer.newLine();
} else {
writer.write(s);
writer.newLine();
}
}
}
reader.close();
writer.close();
tempFile.renameTo(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public String translate(String s) {
return removeEnds(config.getString(s));
}

public String translate(String s, String s1) {
return translate(s).replace("%s1%", s1);
}

public String translate(String s, String s1, String s2) {
return translate(s, s1).replace("%s2%", s2);
}

public String translate(String s, String s1, String s2, String s3) {
return translate(s, s1, s2).replace("%s3%", s3);
}

public String removeEnds(String s) {
return s.split("\"")[0];
}
}
52 changes: 52 additions & 0 deletions src/main/java/cc/synkdev/synkLibs/bungee/SynkLibsBungee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cc.synkdev.synkLibs.bungee;

import cc.synkdev.synkLibs.components.SynkPlugin;
import lombok.Getter;
import lombok.Setter;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.plugin.Plugin;
import org.bstats.bungeecord.Metrics;

import java.util.HashMap;
import java.util.Map;

public class SynkLibsBungee extends Plugin implements SynkPlugin {
@Getter private static SynkLibsBungee instance;
@Getter String prefix = ChatColor.translateAlternateColorCodes('&', "&8[&6SynkLibs&8] » &r");
@Setter @Getter static SynkPlugin spl = null;
static public Map<SynkPlugin, String> availableUpdates = new HashMap<>();

@Override
public void onEnable() {
instance = this;
setSpl(this);
new Metrics(this, 23042);
getProxy().getPluginManager().registerListener(this, new UtilsBungee(this));
UtilsBungee.checkUpdate(this, this);
}

@Override
public void onDisable() {

}

@Override
public String name() {
return "SynkLibs";
}

@Override
public String ver() {
return "1.3";
}

@Override
public String dlLink() {
return "https://modrinth.com/plugin/synklibs";
}

@Override
public String prefix() {
return getPrefix();
}
}
70 changes: 70 additions & 0 deletions src/main/java/cc/synkdev/synkLibs/bungee/UtilsBungee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cc.synkdev.synkLibs.bungee;

import cc.synkdev.synkLibs.Lang;
import cc.synkdev.synkLibs.components.SynkPlugin;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class UtilsBungee implements Listener {
private SynkLibsBungee bcore = SynkLibsBungee.getInstance();
private static SynkLibsBungee score = SynkLibsBungee.getInstance();
SynkPlugin spl;
static LangBungee slang;
public UtilsBungee(SynkPlugin spl) {
this.spl = spl;
}
Lang lang;
public static void log(String s) {
score.getProxy().getConsole().sendMessage(score.getSpl().prefix()+" "+s);
}

public static void checkUpdate(SynkPlugin spl, Plugin plugin) {
checkUpdate(spl, plugin.getDataFolder());
}

private static void checkUpdate(SynkPlugin spl, File dataFolder) {
slang = new LangBungee(dataFolder);
try {
URL url = new URL("https://synkdev.cc/ver/"+spl.name());
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));

String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equals(spl.ver())) {
log(ChatColor.GREEN + spl.name() + " " + slang.translate("upToDate"));
} else {
log(ChatColor.GREEN + slang.translate("updateAvailable") + " " + spl.name() + ": v" + inputLine);
log(ChatColor.GREEN + slang.translate("downloadHere") + ": "+spl.dlLink());
if (SynkLibsBungee.availableUpdates.containsKey(spl)) SynkLibsBungee.availableUpdates.replace(spl, inputLine);
else SynkLibsBungee.availableUpdates.put(spl, inputLine);
}
break;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}


@EventHandler
public void joinBungee (PostLoginEvent event) {
lang = new Lang(bcore.getDataFolder());
if (event.getPlayer().hasPermission("synklibs.bungee.updatenotifier")) bcore.availableUpdates.forEach((s, s2) -> {
ProxiedPlayer p = event.getPlayer();
p.sendMessage(lang.translate("updateAvailable") + " "+s+"!");
p.sendMessage(lang.translate("downloadHere")+": "+s.dlLink());
});
}
}
4 changes: 4 additions & 0 deletions src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: SynkLibs
version: '${project.version}'
main: cc.synkdev.synkLibs.bungee.SynkLibsBungee
author: Synk

0 comments on commit 5f77766

Please sign in to comment.