Skip to content

Commit 543e000

Browse files
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.
1 parent 0d98606 commit 543e000

File tree

11 files changed

+279
-44
lines changed

11 files changed

+279
-44
lines changed

addon/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.jaimemartz</groupId>
99
<artifactId>playerbalancer-parent</artifactId>
10-
<version>2.3.2-SNAPSHOT</version>
10+
<version>2.3.3</version>
1111
</parent>
1212

1313
<artifactId>playerbalancer-addon</artifactId>
@@ -29,13 +29,13 @@
2929
<dependency>
3030
<groupId>org.spigotmc</groupId>
3131
<artifactId>spigot-api</artifactId>
32-
<version>1.15.1-R0.1-SNAPSHOT</version>
32+
<version>1.18.2-R0.1-SNAPSHOT</version>
3333
<scope>provided</scope>
3434
</dependency>
3535
<dependency>
3636
<groupId>me.clip</groupId>
3737
<artifactId>placeholderapi</artifactId>
38-
<version>2.10.5</version>
38+
<version>2.11.1</version>
3939
<scope>provided</scope>
4040
</dependency>
4141
</dependencies>

addon/src/main/java/com/jaimemartz/playerbalanceraddon/MainCommand.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jaimemartz.playerbalanceraddon;
22

33
import com.google.common.base.Strings;
4+
import com.jaimemartz.playerbalanceraddon.util.Color;
45
import org.bukkit.ChatColor;
56
import org.bukkit.command.Command;
67
import org.bukkit.command.CommandExecutor;
@@ -26,16 +27,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
2627
Player player = plugin.getServer().getPlayer(args[2]);
2728
if (player != null) {
2829
plugin.getManager().connectPlayer(player, input);
29-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
30+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
3031
} else {
31-
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
32+
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
3233
}
3334
} else {
3435
if (sender instanceof Player) {
3536
plugin.getManager().connectPlayer((Player) sender, input);
36-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
37+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
3738
} else {
38-
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
39+
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
3940
}
4041
}
4142
} else {
@@ -49,16 +50,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
4950
Player player = plugin.getServer().getPlayer(args[1]);
5051
if (player != null) {
5152
plugin.getManager().fallbackPlayer((Player) sender);
52-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
53+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
5354
} else {
54-
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
55+
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
5556
}
5657
} else {
5758
if (sender instanceof Player) {
5859
plugin.getManager().fallbackPlayer((Player) sender);
59-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
60+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
6061
} else {
61-
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
62+
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
6263
}
6364
}
6465
break;
@@ -71,16 +72,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
7172
Player player = plugin.getServer().getPlayer(args[2]);
7273
if (player != null) {
7374
plugin.getManager().bypassConnect(player, input);
74-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
75+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
7576
} else {
76-
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
77+
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
7778
}
7879
} else {
7980
if (sender instanceof Player) {
8081
plugin.getManager().bypassConnect((Player) sender, input);
81-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
82+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
8283
} else {
83-
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
84+
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
8485
}
8586
}
8687
} else {
@@ -94,16 +95,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
9495
Player player = plugin.getServer().getPlayer(args[1]);
9596
if (player != null) {
9697
plugin.getManager().setPlayerBypass(player);
97-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
98+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
9899
} else {
99-
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
100+
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
100101
}
101102
} else {
102103
if (sender instanceof Player) {
103104
plugin.getManager().setPlayerBypass((Player) sender);
104-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
105+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
105106
} else {
106-
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
107+
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
107108
}
108109
}
109110
break;
@@ -114,16 +115,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
114115
Player player = plugin.getServer().getPlayer(args[1]);
115116
if (player != null) {
116117
plugin.getManager().clearPlayerBypass((Player) sender);
117-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
118+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
118119
} else {
119-
sender.sendMessage(ChatColor.RED + "There is no player with that name connected to this server");
120+
sender.sendMessage(Color.translate(plugin.getConfig().getString("NoPlayer")));
120121
}
121122
} else {
122123
if (sender instanceof Player) {
123124
plugin.getManager().clearPlayerBypass((Player) sender);
124-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
125+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
125126
} else {
126-
sender.sendMessage(ChatColor.RED + "This command variant can only be executed by a player");
127+
sender.sendMessage(Color.translate(plugin.getConfig().getString("ConsoleError")));
127128
}
128129
}
129130
break;
@@ -133,9 +134,9 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
133134
if (args.length >= 3) {
134135
if (args[2].equals("false") || args[2].equals("true")) {
135136
plugin.getManager().setStatusOverride(args[1], Boolean.valueOf(args[2]));
136-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
137+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
137138
} else {
138-
sender.sendMessage(ChatColor.RED + "The status parameter of this command variant has to be a boolean type, either false or true");
139+
sender.sendMessage(Color.translate(plugin.getConfig().getString("BooleanError")));
139140
}
140141
} else {
141142
sender.sendMessage(ChatColor.RED + "Usage: /section overridestatus <section> <status: false|true>");
@@ -146,7 +147,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
146147
case "clearoverride": {
147148
if (args.length >= 2) {
148149
plugin.getManager().clearStatusOverride(args[1]);
149-
sender.sendMessage(ChatColor.GREEN + "Successfully sent request to the plugin");
150+
sender.sendMessage(Color.translate(plugin.getConfig().getString("RequestSent")));
150151
} else {
151152
sender.sendMessage(ChatColor.RED + "Usage: /section clearoverride <server>");
152153
}

addon/src/main/java/com/jaimemartz/playerbalanceraddon/PlayerBalancerAddon.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,81 @@
11
package com.jaimemartz.playerbalanceraddon;
22

3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import com.jaimemartz.playerbalanceraddon.util.ConfigurationFile;
6+
import lombok.Getter;
37
import org.bukkit.plugin.java.JavaPlugin;
48

9+
import java.io.BufferedReader;
10+
import java.io.InputStreamReader;
11+
import java.net.HttpURLConnection;
12+
import java.net.URL;
13+
import java.util.logging.Level;
14+
15+
@Getter
516
public class PlayerBalancerAddon extends JavaPlugin {
617
private PluginMessageManager manager;
718
private PlayerBalancerPlaceholderExpansion expansion;
19+
private ConfigurationFile config;
820

921
@Override
1022
public void onEnable() {
23+
config = new ConfigurationFile(this, "config.yml");
1124
manager = new PluginMessageManager(this);
1225
getCommand("spb").setExecutor(new MainCommand(this));
13-
26+
updateCheck();
1427
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
1528
expansion = new PlayerBalancerPlaceholderExpansion(this);
1629
expansion.register();
1730
}
1831
}
1932

33+
public void updateCheck() {
34+
try {
35+
String urlString = "https://updatecheck.bghddevelopment.com";
36+
URL url = new URL(urlString);
37+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
38+
connection.setRequestMethod("GET");
39+
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
40+
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
41+
String input;
42+
StringBuffer response = new StringBuffer();
43+
while ((input = reader.readLine()) != null) {
44+
response.append(input);
45+
}
46+
reader.close();
47+
JsonObject object = new JsonParser().parse(response.toString()).getAsJsonObject();
48+
49+
if (object.has("plugins")) {
50+
JsonObject plugins = object.get("plugins").getAsJsonObject();
51+
JsonObject info = plugins.get("PlayerBalancer").getAsJsonObject();
52+
String version = info.get("version").getAsString();
53+
if (version.equals(getDescription().getVersion())) {
54+
getLogger().log(Level.INFO, ("PlayerBalancerAddon is on the latest version."));
55+
} else {
56+
getLogger().log(Level.WARNING, (""));
57+
getLogger().log(Level.WARNING, (""));
58+
getLogger().log(Level.WARNING, ("Your PlayerBalancerAddon version is out of date!"));
59+
getLogger().log(Level.WARNING, ("We recommend updating ASAP!"));
60+
getLogger().log(Level.WARNING, (""));
61+
getLogger().log(Level.WARNING, ("Your Version: &e" + getDescription().getVersion()));
62+
getLogger().log(Level.WARNING, ("Newest Version: &e" + version));
63+
getLogger().log(Level.WARNING, (""));
64+
getLogger().log(Level.WARNING, (""));
65+
return;
66+
}
67+
return;
68+
} else {
69+
getLogger().log(Level.SEVERE, ("&cWrong response from update API, contact plugin developer!"));
70+
return;
71+
}
72+
} catch (
73+
Exception ex) {
74+
getLogger().log(Level.SEVERE, ("&cFailed to get updater check. (" + ex.getMessage() + ")"));
75+
return;
76+
}
77+
}
78+
2079
@Override
2180
public void onDisable() {
2281
// Nothing to do...

addon/src/main/java/com/jaimemartz/playerbalanceraddon/PlayerBalancerPlaceholderExpansion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public String onRequest(OfflinePlayer player, String identifier) {
2020
String section = identifier.split("pc_")[1];
2121

2222
if (section == null)
23-
return null;
23+
return "Invalid Section";
2424

2525
// For the first call this placeholder will return 0
2626
// For the next one, the result of the previous one
@@ -41,7 +41,7 @@ public String getIdentifier() {
4141

4242
@Override
4343
public String getAuthor() {
44-
return "Jamezrin <jaime@jamezrin.name>";
44+
return "BGHDDevelopmentLLC";
4545
}
4646

4747
@Override
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jaimemartz.playerbalanceraddon.util;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.ChatColor;
5+
6+
import java.util.List;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
import java.util.stream.Collectors;
10+
11+
public class Color {
12+
13+
public static String translate(String message) {
14+
if (isOnePointSixteenPlus()) {
15+
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
16+
Matcher matcher = pattern.matcher(message);
17+
18+
while (matcher.find()) {
19+
String color = message.substring(matcher.start(), matcher.end());
20+
message = message.replace(color, net.md_5.bungee.api.ChatColor.of(color) + "");
21+
matcher = pattern.matcher(message);
22+
}
23+
}
24+
25+
return ChatColor.translateAlternateColorCodes('&', message);
26+
}
27+
28+
public static List<String> translate(List<String> source) {
29+
return source.stream().map(Color::translate).collect(Collectors.toList());
30+
}
31+
32+
public static boolean isOnePointSixteenPlus() {
33+
if (Bukkit.getVersion().contains("1.16") || Bukkit.getVersion().contains("1.17") || Bukkit.getVersion().contains("1.18")) {
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.jaimemartz.playerbalanceraddon.util;
2+
3+
import org.bukkit.configuration.InvalidConfigurationException;
4+
import org.bukkit.configuration.file.YamlConfiguration;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
public class ConfigurationFile extends YamlConfiguration {
13+
14+
private File file;
15+
private JavaPlugin plugin;
16+
private String name;
17+
18+
public ConfigurationFile(JavaPlugin plugin, String name) {
19+
this.file = new File(plugin.getDataFolder(), name);
20+
this.plugin = plugin;
21+
this.name = name;
22+
23+
if (!this.file.exists()) {
24+
plugin.saveResource(name, false);
25+
}
26+
27+
try {
28+
this.load(this.file);
29+
} catch (IOException | InvalidConfigurationException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
public void load() {
35+
this.file = new File(plugin.getDataFolder(), name);
36+
37+
if (!this.file.exists()) {
38+
plugin.saveResource(name, false);
39+
}
40+
try {
41+
this.load(this.file);
42+
} catch (IOException | InvalidConfigurationException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
public void save() {
48+
try {
49+
this.save(this.file);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
@Override
56+
public int getInt(String path) {
57+
return super.getInt(path, 0);
58+
}
59+
60+
@Override
61+
public double getDouble(String path) {
62+
return super.getDouble(path, 0.0);
63+
}
64+
65+
@Override
66+
public boolean getBoolean(String path) {
67+
return super.getBoolean(path, false);
68+
}
69+
70+
public String getString(String path, boolean check) {
71+
return super.getString(path, null);
72+
}
73+
74+
@Override
75+
public String getString(String path) {
76+
if (super.getString(path) == "") {
77+
78+
} else {
79+
return Color.translate(super.getString(path, "String at path '" + path + "' not found.")).replace("|", "\u2503");
80+
}
81+
return Color.translate(super.getString(path, "String at path '" + path + "' not found.")).replace("|", "\u2503");
82+
}
83+
84+
@Override
85+
public List<String> getStringList(String path) {
86+
return super.getStringList(path).stream().map(Color::translate).collect(Collectors.toList());
87+
}
88+
89+
public List<String> getStringList(String path, boolean check) {
90+
if (!super.contains(path)) return null;
91+
return super.getStringList(path).stream().map(Color::translate).collect(Collectors.toList());
92+
}
93+
94+
public boolean getOption(String option) {
95+
return this.getBoolean("options." + option);
96+
}
97+
}

0 commit comments

Comments
 (0)