Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PlaceholderAPI Version and Add Server Status Check #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.6</version>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.clip.placeholderapi.expansion.Taskable;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitTask;
Expand All @@ -33,10 +34,12 @@ public final class BungeeExpansion extends PlaceholderExpansion implements Plugi

private static final Splitter SPLITTER = Splitter.on(",").trimResults();


private final Map<String, Integer> counts = new HashMap<>();
private final Map<String, Integer> counts = new HashMap<>();
private final AtomicReference<BukkitTask> cached = new AtomicReference<>();

private String online = "Online";
private String offline = "Offline";

private static Field inputField;

static {
Expand Down Expand Up @@ -73,6 +76,11 @@ public Map<String, Object> getDefaults() {
public String onRequest(final OfflinePlayer player, String identifier) {
final int value;

if (identifier.startsWith("online_")) {
final String server = identifier.substring(7).toLowerCase();
return counts.containsKey(server) ? online : offline;
}

switch (identifier.toLowerCase()) {
case "all":
case "total":
Expand All @@ -88,12 +96,16 @@ public String onRequest(final OfflinePlayer player, String identifier) {

@Override
public void start() {
if (hasString("online"))
this.online = getString("online", "Online");
if (hasString("offline"))
this.offline = getString("offline", "Offline");
Comment on lines +99 to +102

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making this extra method, just update the getDefaults() method to include these new entries and it should update accordingly, especially since the getString method returns the default given if returned path is null.


final BukkitTask task = Bukkit.getScheduler().runTaskTimer(getPlaceholderAPI(), () -> {

if (counts.isEmpty()) {
sendServersChannelMessage();
}
else {
} else {
counts.keySet().forEach(this::sendPlayersChannelMessage);
}

Expand All @@ -109,6 +121,13 @@ public void start() {
}
}

private boolean hasString(String path) {
ConfigurationSection section = this.getConfigSection();
if (section == null)
return false;
return section.contains(path);
}

Comment on lines +124 to +130

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed as per the above stated suggestions.

@Override
public void stop() {
final BukkitTask prev = cached.getAndSet(null);
Expand All @@ -135,13 +154,13 @@ public void onPluginMessageReceived(final String channel, final Player player, f
try {
DataInputStream stream = (DataInputStream) inputField.get(in);
switch (in.readUTF()) {
case PLAYERS_CHANNEL:
if (stream.available() == 0) return; // how ?
final String server = in.readUTF();
if (stream.available() == 0) { // how ? x2
getPlaceholderAPI().getLogger().log(Level.SEVERE, String.format("[%s] Could not get the player count from server %s.", getName(), server));
counts.put(server.toLowerCase(), 0);
} else counts.put(server.toLowerCase(), in.readInt());
case PLAYERS_CHANNEL:
if (stream.available() == 0) return; // how ?
final String server = in.readUTF();
if (stream.available() == 0) { // how ? x2
getPlaceholderAPI().getLogger().log(Level.SEVERE, String.format("[%s] Could not get the player count from server %s.", getName(), server));
counts.put(server.toLowerCase(), 0);
} else counts.put(server.toLowerCase(), in.readInt());
break;
case SERVERS_CHANNEL:
SPLITTER.split(in.readUTF()).forEach(serverName -> counts.putIfAbsent(serverName.toLowerCase(), 0));
Expand All @@ -154,7 +173,8 @@ public void onPluginMessageReceived(final String channel, final Player player, f


private void sendServersChannelMessage() {
sendMessage(SERVERS_CHANNEL, out -> { });
sendMessage(SERVERS_CHANNEL, out -> {
});
}

private void sendPlayersChannelMessage(final String serverName) {
Expand Down