Config added for PlayerBalancerAddon

New update check system
Added error message to PlaceholderAPI when it can't find a section rather than returning null
Updated addon to the latest spigot build and added support for HEX colors in messages.
This commit is contained in:
Stephan 2022-04-10 09:43:10 -07:00
parent 0d98606702
commit 543e000fe6
11 changed files with 279 additions and 44 deletions

View File

@ -7,7 +7,7 @@
<parent>
<groupId>com.jaimemartz</groupId>
<artifactId>playerbalancer-parent</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.3</version>
</parent>
<artifactId>playerbalancer-addon</artifactId>
@ -29,13 +29,13 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15.1-R0.1-SNAPSHOT</version>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.5</version>
<version>2.11.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -1,6 +1,7 @@
package com.jaimemartz.playerbalanceraddon;
import com.google.common.base.Strings;
import com.jaimemartz.playerbalanceraddon.util.Color;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -26,16 +27,16 @@ public class MainCommand implements CommandExecutor {
Player player = plugin.getServer().getPlayer(args[2]);
if (player != null) {
plugin.getManager().connectPlayer(player, input);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
}
} else {
if (sender instanceof Player) {
plugin.getManager().connectPlayer((Player) sender, input);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
}
}
} else {
@ -49,16 +50,16 @@ public class MainCommand implements CommandExecutor {
Player player = plugin.getServer().getPlayer(args[1]);
if (player != null) {
plugin.getManager().fallbackPlayer((Player) sender);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
}
} else {
if (sender instanceof Player) {
plugin.getManager().fallbackPlayer((Player) sender);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
}
}
break;
@ -71,16 +72,16 @@ public class MainCommand implements CommandExecutor {
Player player = plugin.getServer().getPlayer(args[2]);
if (player != null) {
plugin.getManager().bypassConnect(player, input);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
}
} else {
if (sender instanceof Player) {
plugin.getManager().bypassConnect((Player) sender, input);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
}
}
} else {
@ -94,16 +95,16 @@ public class MainCommand implements CommandExecutor {
Player player = plugin.getServer().getPlayer(args[1]);
if (player != null) {
plugin.getManager().setPlayerBypass(player);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
}
} else {
if (sender instanceof Player) {
plugin.getManager().setPlayerBypass((Player) sender);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
}
}
break;
@ -114,16 +115,16 @@ public class MainCommand implements CommandExecutor {
Player player = plugin.getServer().getPlayer(args[1]);
if (player != null) {
plugin.getManager().clearPlayerBypass((Player) sender);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
}
} else {
if (sender instanceof Player) {
plugin.getManager().clearPlayerBypass((Player) sender);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
}
}
break;
@ -133,9 +134,9 @@ public class MainCommand implements CommandExecutor {
if (args.length >= 3) {
if (args[2].equals("false") || args[2].equals("true")) {
plugin.getManager().setStatusOverride(args[1], Boolean.valueOf(args[2]));
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "The status parameter of this command variant has to be a boolean type, either false or true");
sender.sendMessage(Color.translate(plugin.getConfig().getString("BooleanError")));
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /section overridestatus <section> <status: false|true>");
@ -146,7 +147,7 @@ public class MainCommand implements CommandExecutor {
case "clearoverride": {
if (args.length >= 2) {
plugin.getManager().clearStatusOverride(args[1]);
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
} else {
sender.sendMessage(ChatColor.RED + "Usage: /section clearoverride <server>");
}

View File

@ -1,22 +1,81 @@
package com.jaimemartz.playerbalanceraddon;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jaimemartz.playerbalanceraddon.util.ConfigurationFile;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
@Getter
public class PlayerBalancerAddon extends JavaPlugin {
private PluginMessageManager manager;
private PlayerBalancerPlaceholderExpansion expansion;
private ConfigurationFile config;
@Override
public void onEnable() {
config = new ConfigurationFile(this, "config.yml");
manager = new PluginMessageManager(this);
getCommand("spb").setExecutor(new MainCommand(this));
updateCheck();
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
expansion = new PlayerBalancerPlaceholderExpansion(this);
expansion.register();
}
}
public void updateCheck() {
try {
String urlString = "https://updatecheck.bghddevelopment.com";
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
StringBuffer response = new StringBuffer();
while ((input = reader.readLine()) != null) {
response.append(input);
}
reader.close();
JsonObject object = new JsonParser().parse(response.toString()).getAsJsonObject();
if (object.has("plugins")) {
JsonObject plugins = object.get("plugins").getAsJsonObject();
JsonObject info = plugins.get("PlayerBalancer").getAsJsonObject();
String version = info.get("version").getAsString();
if (version.equals(getDescription().getVersion())) {
getLogger().log(Level.INFO, ("PlayerBalancerAddon is on the latest version."));
} else {
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, ("Your PlayerBalancerAddon version is out of date!"));
getLogger().log(Level.WARNING, ("We recommend updating ASAP!"));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, ("Your Version: &e" + getDescription().getVersion()));
getLogger().log(Level.WARNING, ("Newest Version: &e" + version));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, (""));
return;
}
return;
} else {
getLogger().log(Level.SEVERE, ("&cWrong response from update API, contact plugin developer!"));
return;
}
} catch (
Exception ex) {
getLogger().log(Level.SEVERE, ("&cFailed to get updater check. (" + ex.getMessage() + ")"));
return;
}
}
@Override
public void onDisable() {
// Nothing to do...

View File

@ -20,7 +20,7 @@ public class PlayerBalancerPlaceholderExpansion extends PlaceholderExpansion {
String section = identifier.split("pc_")[1];
if (section == null)
return null;
return "Invalid Section";
// For the first call this placeholder will return 0
// For the next one, the result of the previous one
@ -41,7 +41,7 @@ public class PlayerBalancerPlaceholderExpansion extends PlaceholderExpansion {
@Override
public String getAuthor() {
return "Jamezrin <jaime@jamezrin.name>";
return "BGHDDevelopmentLLC";
}
@Override

View File

@ -0,0 +1,39 @@
package com.jaimemartz.playerbalanceraddon.util;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Color {
public static String translate(String message) {
if (isOnePointSixteenPlus()) {
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
String color = message.substring(matcher.start(), matcher.end());
message = message.replace(color, net.md_5.bungee.api.ChatColor.of(color) + "");
matcher = pattern.matcher(message);
}
}
return ChatColor.translateAlternateColorCodes('&', message);
}
public static List<String> translate(List<String> source) {
return source.stream().map(Color::translate).collect(Collectors.toList());
}
public static boolean isOnePointSixteenPlus() {
if (Bukkit.getVersion().contains("1.16") || Bukkit.getVersion().contains("1.17") || Bukkit.getVersion().contains("1.18")) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,97 @@
package com.jaimemartz.playerbalanceraddon.util;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class ConfigurationFile extends YamlConfiguration {
private File file;
private JavaPlugin plugin;
private String name;
public ConfigurationFile(JavaPlugin plugin, String name) {
this.file = new File(plugin.getDataFolder(), name);
this.plugin = plugin;
this.name = name;
if (!this.file.exists()) {
plugin.saveResource(name, false);
}
try {
this.load(this.file);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
public void load() {
this.file = new File(plugin.getDataFolder(), name);
if (!this.file.exists()) {
plugin.saveResource(name, false);
}
try {
this.load(this.file);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
public void save() {
try {
this.save(this.file);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public int getInt(String path) {
return super.getInt(path, 0);
}
@Override
public double getDouble(String path) {
return super.getDouble(path, 0.0);
}
@Override
public boolean getBoolean(String path) {
return super.getBoolean(path, false);
}
public String getString(String path, boolean check) {
return super.getString(path, null);
}
@Override
public String getString(String path) {
if (super.getString(path) == "") {
} else {
return Color.translate(super.getString(path, "String at path '" + path + "' not found.")).replace("|", "\u2503");
}
return Color.translate(super.getString(path, "String at path '" + path + "' not found.")).replace("|", "\u2503");
}
@Override
public List<String> getStringList(String path) {
return super.getStringList(path).stream().map(Color::translate).collect(Collectors.toList());
}
public List<String> getStringList(String path, boolean check) {
if (!super.contains(path)) return null;
return super.getStringList(path).stream().map(Color::translate).collect(Collectors.toList());
}
public boolean getOption(String option) {
return this.getBoolean("options." + option);
}
}

View File

@ -0,0 +1,4 @@
RequestSent: "&aSuccessfully sent request to the plugin"
NoPlayer: "&cThere is no player with that name connected to this server"
ConsoleError: "&cThis command variant can only be executed by a player"
BooleanError: "&cThe status parameter of this command variant has to be a boolean type, either false or true"

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.jaimemartz</groupId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.3</version>
<artifactId>playerbalancer-parent</artifactId>
</parent>

View File

@ -1,6 +1,8 @@
package com.jaimemartz.playerbalancer;
import com.google.common.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
import com.jaimemartz.playerbalancer.commands.MainCommand;
import com.jaimemartz.playerbalancer.commands.ManageCommand;
@ -13,6 +15,7 @@ import com.jaimemartz.playerbalancer.ping.StatusManager;
import com.jaimemartz.playerbalancer.section.SectionManager;
import com.jaimemartz.playerbalancer.settings.SettingsHolder;
import com.jaimemartz.playerbalancer.utils.CustomFormatter;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
@ -23,6 +26,7 @@ import org.bstats.bungeecord.Metrics;
import org.bstats.bungeecord.Metrics.SingleLineChart;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
@ -67,24 +71,55 @@ public class PlayerBalancer extends Plugin {
}
}));
if (!checkUpToDate()) {
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
getLogger().info("You might want to update to benefit of new features, improvements and fixes");
getLogger().info("Access the plugin page at https://www.spigotmc.org/resources/55011");
}
updateCheck();
this.execStart();
}
public boolean checkUpToDate() {
public void updateCheck() {
try {
URLConnection con = new URL("https://api.spigotmc.org/legacy/update.php?resource=55011").openConnection();
String reply = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
return getDescription().getVersion().equals(reply);
} catch (IOException e) {
getLogger().log(Level.WARNING, "Could not access Spigot API, maybe you have a firewall or something that blocks request to it", e);
String urlString = "https://updatecheck.bghddevelopment.com";
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
StringBuffer response = new StringBuffer();
while ((input = reader.readLine()) != null) {
response.append(input);
}
reader.close();
JsonObject object = new JsonParser().parse(response.toString()).getAsJsonObject();
if (object.has("plugins")) {
JsonObject plugins = object.get("plugins").getAsJsonObject();
JsonObject info = plugins.get("PlayerBalancer").getAsJsonObject();
String version = info.get("version").getAsString();
if (version.equals(getDescription().getVersion())) {
getLogger().log(Level.INFO, ("PlayerBalancer is on the latest version."));
} else {
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, ("Your PlayerBalancer version is out of date!"));
getLogger().log(Level.WARNING, ("We recommend updating ASAP!"));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, ("Your Version: &e" + getDescription().getVersion()));
getLogger().log(Level.WARNING, ("Newest Version: &e" + version));
getLogger().log(Level.WARNING, (""));
getLogger().log(Level.WARNING, (""));
return;
}
return;
} else {
getLogger().log(Level.SEVERE, ("&cWrong response from update API, contact plugin developer!"));
return;
}
} catch (
Exception ex) {
getLogger().log(Level.SEVERE, ("&cFailed to get updater check. (" + ex.getMessage() + ")"));
return;
}
return true;
}
@Override

View File

@ -6,7 +6,7 @@
<groupId>com.bghddevelopment.partyandfriendsaddon</groupId>
<artifactId>PBServerConnector</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.3</version>
<packaging>jar</packaging>
<name>PartyAndFriendsAddon</name>

View File

@ -6,7 +6,7 @@
<groupId>com.jaimemartz</groupId>
<artifactId>playerbalancer-parent</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.3</version>
<packaging>pom</packaging>
<modules>