Skip to content

Commit 3af5905

Browse files
committed
Refactor vote broadcasting logic and migrate configuration handling
1 parent dd3bcae commit 3af5905

File tree

16 files changed

+1208
-255
lines changed

16 files changed

+1208
-255
lines changed

VotingPlugin/src/main/java/com/bencodez/votingplugin/BungeeHandler.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -293,21 +293,14 @@ public void onReceive(ArrayList<String> args) {
293293
if (!plugin.getBungeeSettings().isBungeeBroadcast()
294294
&& !plugin.getBungeeSettings().isDisableBroadcast()) {
295295
if (wasOnline || plugin.getBungeeSettings().isBungeeBroadcastAlways()) {
296-
if (plugin.getConfigFile().isFormatOnlyOneOfflineBroadcast() && !wasOnline) {
297-
if (num == 1) {
298-
user.offlineBroadcast(user, plugin.getBungeeSettings().isUseBungeecoord(),
299-
numberOfVotes);
300-
}
296+
VoteSite site = plugin.getVoteSiteManager().getVoteSite(service, true);
297+
if (site != null) {
298+
plugin.getBroadcastHandler().broadcastVote(user.getJavaUUID(), user.getPlayerName(),
299+
site.getDisplayName(), false);
300+
broadcast = false;
301301
} else {
302-
VoteSite site = plugin.getVoteSiteManager().getVoteSite(service, true);
303-
if (site != null) {
304-
site.broadcastVote(user, false);
305-
broadcast = false;
306-
} else {
307-
plugin.getLogger().warning("No votesite for " + service);
308-
}
302+
plugin.getLogger().warning("No votesite for " + service);
309303
}
310-
311304
}
312305
}
313306
} else {
@@ -380,21 +373,14 @@ public void onReceive(ArrayList<String> args) {
380373
if (!plugin.getBungeeSettings().isBungeeBroadcast()
381374
&& !plugin.getBungeeSettings().isDisableBroadcast()) {
382375
if (wasOnline || plugin.getBungeeSettings().isBungeeBroadcastAlways()) {
383-
if (plugin.getConfigFile().isFormatOnlyOneOfflineBroadcast() && !wasOnline) {
384-
if (num == 1) {
385-
user.offlineBroadcast(user, plugin.getBungeeSettings().isUseBungeecoord(),
386-
numberOfVotes);
387-
}
376+
VoteSite site = plugin.getVoteSiteManager().getVoteSite(service, true);
377+
if (site != null) {
378+
plugin.getBroadcastHandler().broadcastVote(user.getJavaUUID(), user.getPlayerName(),
379+
site.getDisplayName(), false);
380+
broadcast = false;
388381
} else {
389-
VoteSite site = plugin.getVoteSiteManager().getVoteSite(service, true);
390-
if (site != null) {
391-
site.broadcastVote(user, false);
392-
broadcast = false;
393-
} else {
394-
plugin.getLogger().warning("No votesite for " + service);
395-
}
382+
plugin.getLogger().warning("No votesite for " + service);
396383
}
397-
398384
}
399385
}
400386
} else {
@@ -473,7 +459,8 @@ public void onReceive(ArrayList<String> args) {
473459
.getVotingPluginUser(UUID.fromString(uuid), args.get(1));
474460
VoteSite site = plugin.getVoteSiteManager().getVoteSite(service, true);
475461
if (site != null) {
476-
site.broadcastVote(user, false);
462+
plugin.getBroadcastHandler().broadcastVote(user.getJavaUUID(), user.getPlayerName(),
463+
site.getDisplayName(), false);
477464
} else {
478465
plugin.getLogger().warning("No votesite for " + service);
479466
}
@@ -489,7 +476,7 @@ public void onReceive(ArrayList<String> args) {
489476
String votes = args.get(2);
490477
VotingPluginUser user = plugin.getVotingPluginUserManager()
491478
.getVotingPluginUser(UUID.fromString(uuid), args.get(1));
492-
user.offlineBroadcast(user, false, Integer.parseInt(votes));
479+
//user.offlineBroadcast(user, false, Integer.parseInt(votes));
493480
}
494481
}
495482
});

VotingPlugin/src/main/java/com/bencodez/votingplugin/VotingPluginMain.java

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.bencodez.simpleapi.sql.mysql.config.MysqlConfigSpigot;
6060
import com.bencodez.simpleapi.updater.Updater;
6161
import com.bencodez.votingplugin.broadcast.BroadcastHandler;
62+
import com.bencodez.votingplugin.broadcast.BroadcastSettings;
6263
import com.bencodez.votingplugin.commands.CommandLoader;
6364
import com.bencodez.votingplugin.commands.executers.CommandAdminVote;
6465
import com.bencodez.votingplugin.commands.executers.CommandVote;
@@ -1109,6 +1110,9 @@ public void onPostLoad() {
11091110
registerCommands();
11101111
checkVotifier();
11111112
registerEvents();
1113+
1114+
loadVoteBroadcast();
1115+
11121116
loadDirectlyDefined();
11131117
checkUpdate = new CheckUpdate(this);
11141118
checkUpdate.startUp();
@@ -1295,11 +1299,6 @@ public String getValue(Reward reward, com.bencodez.advancedcore.api.user.Advance
12951299
});
12961300
}
12971301

1298-
if (plugin.getConfigFile().isFormatAlternateBroadcastEnabled()) {
1299-
broadcastHandler = new BroadcastHandler(plugin, plugin.getConfigFile().getFormatAlternateBroadcastDelay());
1300-
plugin.debug("Using alternate broadcast method");
1301-
}
1302-
13031302
plugin.getLogger().info("Enabled VotingPlugin " + plugin.getDescription().getVersion());
13041303
if (plugin.getDescription().getVersion().contains("SNAPSHOT")) {
13051304
plugin.getLogger().info(
@@ -1352,6 +1351,57 @@ public void run() {
13521351

13531352
}
13541353

1354+
private void migrateVoteBroadcast(Config configFile) {
1355+
ConfigurationSection cfg = configFile.getData();
1356+
// If new section exists, do nothing
1357+
if (cfg.isConfigurationSection("VoteBroadcast")) {
1358+
return;
1359+
}
1360+
1361+
// Create VoteBroadcast section
1362+
org.bukkit.configuration.ConfigurationSection vb = cfg.createSection("VoteBroadcast");
1363+
1364+
// Detect old AlternateBroadcast
1365+
boolean altEnabled = cfg.getBoolean("Format.AlternateBroadcast.Enabled", false);
1366+
int altDelay = cfg.getInt("Format.AlternateBroadcast.Delay", 30);
1367+
String altMsg = cfg.getString("Format.AlternateBroadcast.Broadcast",
1368+
"&6[Vote] &a%numberofplayers% voted recently! /vote");
1369+
1370+
// Old single vote message
1371+
String oldBroadcastMsg = cfg.getString("Format.BroadcastMsg",
1372+
"&6[Vote] &aThanks &e%player% &afor voting on &e%SiteName%");
1373+
1374+
// Map old -> new
1375+
if (altEnabled) {
1376+
vb.set("Type", "INTERVAL_SUMMARY_GLOBAL");
1377+
vb.set("Duration", altDelay + "m");
1378+
vb.set("MaxSitesListed", 0);
1379+
1380+
org.bukkit.configuration.ConfigurationSection fmt = vb.createSection("Format");
1381+
1382+
// Header uses the old interval broadcast line (make it clearer + include new
1383+
// placeholders)
1384+
fmt.set("Header", altMsg.replace("%numberofplayers%", "%numberofplayers%").replace("%players%", "%players%")
1385+
.replace("%numberofsites%", "%numberofsites%").replace("%sites%", "%sites%"));
1386+
1387+
// Default: list entries like "Player (N)" (the handler feeds that as item text)
1388+
fmt.set("ListLine", "&7 - &6%site%");
1389+
fmt.set("BroadcastMsg", "&6[Vote] &aThanks &e%player% &afor voting on &e%site%&a!");
1390+
1391+
} else {
1392+
vb.set("Type", "EVERY_VOTE");
1393+
vb.set("Duration", "2m");
1394+
vb.set("MaxSitesListed", 0);
1395+
1396+
org.bukkit.configuration.ConfigurationSection fmt = vb.createSection("Format");
1397+
fmt.set("BroadcastMsg", oldBroadcastMsg.replace("%SiteName%", "%site%")); // convert placeholder name
1398+
fmt.set("Header", "&6[Vote] &aThanks &e%player% &afor voting on &e%sites_count% &asites:");
1399+
fmt.set("ListLine", "&7 - &e%site%");
1400+
}
1401+
plugin.getLogger().info("Migrated vote broadcast settings to new format.");
1402+
configFile.saveData();
1403+
}
1404+
13551405
/*
13561406
* (non-Javadoc)
13571407
*
@@ -1539,10 +1589,6 @@ private void reloadPlugin(boolean userStorage) {
15391589
}
15401590
checkYMLError();
15411591

1542-
if (broadcastHandler != null) {
1543-
broadcastHandler.schedule(getConfigFile().getFormatAlternateBroadcastDelay());
1544-
}
1545-
15461592
plugin.loadVoteSites();
15471593

15481594
getOptions().setServer(bungeeSettings.getServer());
@@ -1556,17 +1602,34 @@ private void reloadPlugin(boolean userStorage) {
15561602

15571603
getVoteStreakHandler().reload();
15581604

1605+
loadVoteBroadcast();
1606+
15591607
loadDirectlyDefined();
15601608

15611609
setUpdate(true);
15621610
}
15631611

1612+
private void loadVoteBroadcast() {
1613+
ConfigurationSection sec = getConfigFile().getData().getConfigurationSection("VoteBroadcast");
1614+
BroadcastSettings settings = BroadcastSettings.load(sec);
1615+
1616+
if (broadcastHandler == null) {
1617+
// Backend servers only: create once
1618+
broadcastHandler = new BroadcastHandler(this, settings, ZoneId.systemDefault());
1619+
} else {
1620+
// Reload-safe: just update settings + reschedule interval if needed
1621+
broadcastHandler.setSettings(settings);
1622+
}
1623+
}
1624+
15641625
private void setupFiles() {
15651626
configFile = new Config(this);
15661627
configFile.setup();
15671628
configFile.setIgnoreCase(plugin.getConfigFile().isCaseInsensitiveYMLFiles());
15681629
configFile.reloadData();
15691630

1631+
migrateVoteBroadcast(configFile);
1632+
15701633
configVoteSites = new ConfigVoteSites(this);
15711634
configVoteSites.setup();
15721635

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.bencodez.votingplugin.broadcast;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.StringJoiner;
8+
9+
/**
10+
* Defines HOW vote broadcasts are rendered.
11+
*
12+
* Terminology:
13+
* - BroadcastMsg : used when EXACTLY ONE item exists (single-site vote)
14+
* - Header : used ONCE when MULTIPLE items exist (batch/interval)
15+
* - ListLine : used ONCE PER ITEM when MULTIPLE items exist
16+
*
17+
* Placeholders (always available):
18+
* - %player% : player name (or "Server" for interval summaries)
19+
* - %site% : the current site/item for this line
20+
* - %sites_count% : number of items in the list
21+
* - %sites% : csv of items in the list
22+
* - %reason% : internal reason string ("vote", "batch", "interval", etc)
23+
*
24+
* Extra context placeholders (INTERVAL_SUMMARY_GLOBAL):
25+
* - %players% / %numberofplayers%
26+
* - %sites% / %numberofsites% (note: %sites% is csv; %numberofsites% is count)
27+
*/
28+
public final class BroadcastFormat {
29+
30+
private final String broadcastMsg;
31+
private final String header;
32+
private final String listLine;
33+
34+
public BroadcastFormat(String broadcastMsg, String header, String listLine) {
35+
this.broadcastMsg = broadcastMsg == null ? "" : broadcastMsg;
36+
this.header = header == null ? "" : header;
37+
this.listLine = listLine == null ? "" : listLine;
38+
}
39+
40+
public List<String> render(String playerName, List<String> items, String reason) {
41+
return render(playerName, items, reason, Collections.<String, String>emptyMap());
42+
}
43+
44+
public List<String> render(String playerName, List<String> items, String reason, Map<String, String> context) {
45+
List<String> output = new ArrayList<String>();
46+
int count = items == null ? 0 : items.size();
47+
48+
String csv = "";
49+
if (count > 0) {
50+
StringJoiner joiner = new StringJoiner(", ");
51+
for (String s : items) {
52+
if (s != null && !s.isEmpty()) {
53+
joiner.add(s);
54+
}
55+
}
56+
csv = joiner.toString();
57+
}
58+
59+
// SINGLE ITEM → BroadcastMsg
60+
if (count == 1 && !broadcastMsg.isEmpty()) {
61+
output.add(apply(broadcastMsg, playerName, items.get(0), count, csv, reason, context));
62+
return output;
63+
}
64+
65+
// MULTI ITEM → Header + ListLine per item
66+
if (!header.isEmpty()) {
67+
output.add(apply(header, playerName, "", count, csv, reason, context));
68+
}
69+
70+
if (count > 0 && !listLine.isEmpty()) {
71+
for (String s : items) {
72+
output.add(apply(listLine, playerName, s, count, csv, reason, context));
73+
}
74+
}
75+
76+
return output;
77+
}
78+
79+
private String apply(String template,
80+
String player,
81+
String item,
82+
int count,
83+
String csv,
84+
String reason,
85+
Map<String, String> context) {
86+
87+
String out = template
88+
.replace("%player%", player == null ? "" : player)
89+
.replace("%site%", item == null ? "" : item)
90+
.replace("%sites_count%", String.valueOf(count))
91+
.replace("%sites%", csv == null ? "" : csv)
92+
.replace("%reason%", reason == null ? "" : reason);
93+
94+
if (context != null && !context.isEmpty()) {
95+
for (Map.Entry<String, String> e : context.entrySet()) {
96+
out = out.replace("%" + e.getKey() + "%", e.getValue());
97+
}
98+
}
99+
100+
return out;
101+
}
102+
}

0 commit comments

Comments
 (0)