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

Code Cleanup and Fix for Rare Occurrence of Extra Players #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitTask;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Stream;

public final class BungeeExpansion extends PlaceholderExpansion implements PluginMessageListener, Taskable, Configurable {

private static final String MESSAGE_CHANNEL = "BungeeCord";
private static final String SERVERS_CHANNEL = "GetServers";
private static final String PLAYERS_CHANNEL = "PlayerCount";
private static final String CONFIG_INTERVAL = "check_interval";

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


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


@Override
public String getIdentifier() {
return "bungee";
Expand All @@ -46,47 +45,39 @@ public String getAuthor() {

@Override
public String getVersion() {
return "2.2";
return "2.3";
}

@Override
public Map<String, Object> getDefaults() {
return Collections.singletonMap(CONFIG_INTERVAL, 30);
}


@Override
public String onRequest(final OfflinePlayer player, String identifier) {
final int value;

switch (identifier.toLowerCase()) {
case "all":
case "total":
value = counts.values().stream().mapToInt(Integer::intValue).sum();
break;
default:
value = counts.getOrDefault(identifier.toLowerCase(), 0);
break;
public String onRequest(OfflinePlayer player, String identifier) {
identifier = identifier.toLowerCase();
if (identifier.equals("all") || identifier.equals("total")) {
return String.valueOf(totalPlayerCount);
} else {
if (identifier.contains(",")) {
Stream<String> servers = Arrays.stream(identifier.split(","));
int value = servers.mapToInt(server -> counts.computeIfAbsent(server, count -> 0)).sum();
return String.valueOf(value);
} else {
int value = counts.computeIfAbsent(identifier, count -> 0);
return String.valueOf(value);
}
}

return String.valueOf(value);
}

@Override
public void start() {
final BukkitTask task = Bukkit.getScheduler().runTaskTimer(getPlaceholderAPI(), () -> {

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

}, 20L * 2L, 20L * getLong(CONFIG_INTERVAL, 30));
BukkitTask task = Bukkit.getScheduler().runTaskTimer(getPlaceholderAPI(), () -> {
sendPlayersChannelMessage("ALL");
counts.keySet().forEach(this::sendPlayersChannelMessage);
}, 40L, 20L * getLong(CONFIG_INTERVAL, 30));


final BukkitTask prev = cached.getAndSet(task);
BukkitTask prev = cached.getAndSet(task);
if (prev != null) {
prev.cancel();
} else {
Expand All @@ -97,7 +88,7 @@ public void start() {

@Override
public void stop() {
final BukkitTask prev = cached.getAndSet(null);
BukkitTask prev = cached.getAndSet(null);
if (prev == null) {
return;
}
Expand All @@ -109,47 +100,50 @@ public void stop() {
Bukkit.getMessenger().unregisterIncomingPluginChannel(getPlaceholderAPI(), MESSAGE_CHANNEL, this);
}


@Override
public void onPluginMessageReceived(final String channel, final Player player, final byte[] message) {
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!MESSAGE_CHANNEL.equals(channel)) {
return;
}

//noinspection UnstableApiUsage
final ByteArrayDataInput in = ByteStreams.newDataInput(message);
switch (in.readUTF()) {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String messageType = in.readUTF();
switch (messageType) {
case PLAYERS_CHANNEL:
counts.put(in.readUTF().toLowerCase(), in.readInt());
String server = in.readUTF();
int count = in.readInt();
if (server.equalsIgnoreCase("ALL")) {
totalPlayerCount = count;
} else {
counts.put(server.toLowerCase(), count);
}
break;
case SERVERS_CHANNEL:
SPLITTER.split(in.readUTF()).forEach(serverName -> counts.putIfAbsent(serverName.toLowerCase(), 0));
break;
}
}


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

private void sendPlayersChannelMessage(final String serverName) {
private void sendPlayersChannelMessage(String serverName) {
sendMessage(PLAYERS_CHANNEL, out -> out.writeUTF(serverName));
}

private void sendMessage(final String channel, final Consumer<ByteArrayDataOutput> consumer) {
final Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
private void sendMessage(String channel, Consumer<ByteArrayDataOutput> consumer) {
Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
if (player == null) {
return;
}

//noinspection UnstableApiUsage
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);

consumer.accept(out);

player.sendPluginMessage(getPlaceholderAPI(), MESSAGE_CHANNEL, out.toByteArray());
}

}