Skip to content

Commit

Permalink
Recode of multi-proxy backend for prep of redis multi-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCodez committed Feb 19, 2024
1 parent 1c5c33b commit 6f37829
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -50,6 +51,9 @@
import com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler;
import com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver;
import com.bencodez.advancedcore.bungeeapi.time.BungeeTimeChecker;
import com.bencodez.votingplugin.bungee.global.multiproxy.MultiProxyHandler;
import com.bencodez.votingplugin.bungee.global.multiproxy.MultiProxyServerSocketConfiguration;
import com.bencodez.votingplugin.bungee.global.multiproxy.MultiProxyServerSocketConfigurationBungee;
import com.bencodez.votingplugin.timequeue.VoteTimeQueue;
import com.bencodez.votingplugin.topvoter.TopVoter;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -79,10 +83,6 @@ public class VotingPluginBungee extends Plugin implements Listener {

private HashMap<String, ClientHandler> clientHandles;

private HashMap<String, ClientHandler> multiproxyClientHandles;

private SocketHandler multiproxySocketHandler;

@Getter
private Config config;

Expand Down Expand Up @@ -178,7 +178,7 @@ public synchronized void checkOnlineVotes(ProxiedPlayer player, String uuid, Str
}
cachedOnlineVotes.put(uuid, new ArrayList<OfflineBungeeVote>());
if (getConfig().getMultiProxySupport() && getConfig().getMultiProxyOneGlobalReward()) {
sendMultiProxyServerMessage("ClearVote", player.getName(), uuid);
multiProxyHandler.sendMultiProxyServerMessage("ClearVote", player.getName(), uuid);
}
}
}
Expand Down Expand Up @@ -375,6 +375,7 @@ public void onTimeChangedFinished(TimeType type) {
}

processQueue();


}

Expand Down Expand Up @@ -557,6 +558,9 @@ public void onDisable() {

private VoteEventBungee voteEventBungee;

@Getter
private MultiProxyHandler multiProxyHandler;

@Override
public void onEnable() {
enabled = true;
Expand Down Expand Up @@ -899,9 +903,9 @@ public void run() {
currentVotePartyVotesRequired = getConfig().getVotePartyVotesRequired()
+ voteCacheFile.getVotePartyInreaseVotesRequired();
votePartyVotes = voteCacheFile.getVotePartyCurrentVotes();
}

loadMultiProxySupport();
loadMultiProxySupport();
}

if (!votifierEnabled) {
if (!(getConfig().getMultiProxySupport() && !getConfig().getPrimaryServer())) {
Expand Down Expand Up @@ -954,70 +958,98 @@ public void run() {

}

public void sendServerNameMessage() {
if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
for (String s : getAvailableAllServers()) {
sendPluginMessageServer(s, "ServerName", s);
}
private void loadMultiProxySupport() {
if (multiProxyHandler != null) {
multiProxyHandler.close();
}
multiProxyHandler = new MultiProxyHandler() {

}
@Override
public void triggerVote(String player, String service, boolean realVote, boolean timeQueue, long queueTime,
BungeeMessageData text, String uuid) {
vote(player, service, realVote, timeQueue, queueTime, text, uuid);
}

public void loadMultiProxySupport() {
if (getConfig().getMultiProxySupport()) {
if (encryptionHandler == null) {
encryptionHandler = new EncryptionHandler(new File(getDataFolder(), "secretkey.key"));
@Override
public void setEncryptionHandler(EncryptionHandler encryptionHandler1) {
encryptionHandler = encryptionHandler1;
}

if (multiproxySocketHandler != null) {
multiproxySocketHandler.closeConnection();
multiproxySocketHandler = null;
@Override
public void logInfo(String msg) {
getLogger().info(msg);
}
multiproxySocketHandler = new SocketHandler(getDescription().getVersion(),
config.getMultiProxySocketHostHost(), config.getMultiProxySocketHostPort(), encryptionHandler,
config.getDebug()) {

@Override
public void log(String str) {
getLogger().info(str);
}
};
@Override
public String getVersion() {
return getDescription().getVersion();
}

multiproxySocketHandler.add(new SocketReceiver() {
@Override
public boolean getMultiProxySupportEnabled() {
return config.getMultiProxySupport();
}

@Override
public void onReceive(String[] data) {
if (data.length > 0) {
if (data[0].equalsIgnoreCase("Status")) {
getLogger().info("Multi-proxy status message received");
} else if (data[0].equalsIgnoreCase("ClearVote")) {
cachedOnlineVotes.remove(data[2]);
} else if (data[0].equalsIgnoreCase("login")) {
nonVotedPlayersCache.addPlayer(data[1], data[2]);
}
}
if (data.length > 8) {
if (data[0].equalsIgnoreCase("Vote")) {
vote(data[2], data[3], Boolean.valueOf(data[7]), true, 0, new BungeeMessageData(data[8]),
data[1]);
} else if (data[0].equalsIgnoreCase("VoteOnline")) {
vote(data[2], data[3], Boolean.valueOf(data[7]), true, 0, new BungeeMessageData(data[8]),
data[1]);
}
}
@Override
protected int getMultiProxySocketHostPort() {
return config.getMultiProxySocketHostPort();
}

}
});
@Override
protected String getMultiProxySocketHostHost() {
return config.getMultiProxySocketHostHost();
}

@Override
protected MultiProxyServerSocketConfiguration getMultiProxyServersConfiguration(String s) {
return new MultiProxyServerSocketConfigurationBungee(s, config.getMultiProxyServersConfiguration(s));
}

multiproxyClientHandles = new HashMap<String, ClientHandler>();
for (String s : config.getMultiProxyServers()) {
Configuration d = config.getMultiProxyServersConfiguration(s);
multiproxyClientHandles.put(s, new ClientHandler(d.getString("Host", ""), d.getInt("Port", 1234),
encryptionHandler, config.getDebug()));
@Override
protected Collection<String> getMultiProxyServers() {
return config.getMultiProxyServers();
}

@Override
public EncryptionHandler getEncryptionHandler() {
return encryptionHandler;
}

@Override
public boolean getDebug() {
return config.getDebug();
}

@Override
public File getPluginDataFolder() {
return getDataFolder();
}

getLogger().info("Loaded multi-proxy support");
@Override
protected void clearVote(String string) {
cachedOnlineVotes.remove(string);
}

@Override
protected void addNonVotedPlayerCache(String uuid, String player) {
nonVotedPlayersCache.addPlayer(uuid, player);
}

@Override
public boolean getPrimaryServer() {
return config.getPrimaryServer();
}
};
multiProxyHandler.loadMultiProxySupport();
}

public void sendServerNameMessage() {
if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
for (String s : getAvailableAllServers()) {
sendPluginMessageServer(s, "ServerName", s);
}
}

}

@EventHandler
Expand Down Expand Up @@ -1053,24 +1085,14 @@ public void run() {
if (isOnline(p)) {
checkCachedVotes(server);
checkOnlineVotes(proxiedPlayer, proxiedPlayer.getUniqueId().toString(), server);
multiProxyLogin(proxiedPlayer);
multiProxyHandler.login(proxiedPlayer.getUniqueId().toString(), proxiedPlayer.getName());
}
}
}, 1, TimeUnit.SECONDS);
}

}

public void multiProxyLogin(ProxiedPlayer p) {
if (!config.getMultiProxySupport()) {
return;
}
if (config.getPrimaryServer()) {
return;
}
sendMultiProxyServerMessage("Login", p.getUniqueId().toString(), p.getName());
}

@EventHandler
public void onPluginMessage(PluginMessageEvent ev) {
if (!ev.getTag().equals("vp:vp".toLowerCase())) {
Expand Down Expand Up @@ -1206,12 +1228,6 @@ public void sendServerMessage(String... messageData) {
}
}

public void sendMultiProxyServerMessage(String... messageData) {
for (ClientHandler h : multiproxyClientHandles.values()) {
h.sendMessage(messageData);
}
}

public void sendServerMessageServer(String server, String... messageData) {
if (clientHandles.containsKey(server)) {
clientHandles.get(server).sendMessage(messageData);
Expand Down Expand Up @@ -1414,7 +1430,7 @@ public synchronized void vote(String player, String service, boolean realVote, b
"" + getConfig().getBungeeManageTotals(), "" + BungeeVersion.getPluginMessageVersion(),
"" + config.getBroadcast(), "1", "1");
if (getConfig().getMultiProxySupport() && getConfig().getMultiProxyOneGlobalReward()) {
sendMultiProxyServerMessage("ClearVote", player, uuid);
multiProxyHandler.sendMultiProxyServerMessage("ClearVote", player, uuid);
}
} else {
if (!cachedOnlineVotes.containsKey(uuid)) {
Expand Down Expand Up @@ -1443,14 +1459,15 @@ public synchronized void vote(String player, String service, boolean realVote, b
if (getConfig().getMultiProxySupport() && getConfig().getPrimaryServer()) {
if (!getConfig().getMultiProxyOneGlobalReward()) {
debug("Seending global proxy vote message");
sendMultiProxyServerMessage("Vote", uuid, player, service, "" + votePartyVotes,
multiProxyHandler.sendMultiProxyServerMessage("Vote", uuid, player, service, "" + votePartyVotes,
"" + currentVotePartyVotesRequired, "" + time, "" + realVote, text.toString());
} else {
// check if reward should've already been given
if (!(isOnline(p) && !config.getBlockedServers().contains(p.getServer().getInfo().getName()))) {
debug("Sending global proxy voteonline message");
sendMultiProxyServerMessage("VoteOnline", uuid, player, service, "" + votePartyVotes,
"" + currentVotePartyVotesRequired, "" + time, "" + realVote, text.toString());
multiProxyHandler.sendMultiProxyServerMessage("VoteOnline", uuid, player, service,
"" + votePartyVotes, "" + currentVotePartyVotesRequired, "" + time, "" + realVote,
text.toString());
} else {
debug("Not sending global proxy message for voteonline, player already got reward");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void execute(CommandSender sender, String[] args) {
}
}
if (args[0].equalsIgnoreCase("multiproxystatus")) {
bungee.sendMultiProxyServerMessage("Status");
bungee.getMultiProxyHandler().sendMultiProxyServerMessage("Status");
sender.sendMessage(new TextComponent("&aSending status message"));
}
if (args[0].equalsIgnoreCase("help")) {
Expand Down
Loading

0 comments on commit 6f37829

Please sign in to comment.