Skip to content

Commit 0a9ed9c

Browse files
committed
add CommunityGoal module with management and placeholder support
1 parent 52fd313 commit 0a9ed9c

8 files changed

Lines changed: 371 additions & 0 deletions

File tree

‎bukkit/src/main/java/net/leaderos/plugin/Bukkit.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.leaderos.plugin.modules.ai.AiModule;
1919
import net.leaderos.plugin.modules.bazaar.BazaarModule;
2020
import net.leaderos.plugin.modules.cache.CacheModule;
21+
import net.leaderos.plugin.modules.communitygoal.CommunityGoalModule;
2122
import net.leaderos.plugin.modules.connect.ConnectModule;
2223
import net.leaderos.plugin.modules.credit.CreditModule;
2324
import net.leaderos.plugin.modules.discord.DiscordModule;
@@ -118,6 +119,7 @@ public void onEnable() {
118119
LeaderOSAPI.getModuleManager().registerModule(new BazaarModule());
119120
LeaderOSAPI.getModuleManager().registerModule(new VoucherModule());
120121
LeaderOSAPI.getModuleManager().registerModule(new DonationsModule());
122+
LeaderOSAPI.getModuleManager().registerModule(new CommunityGoalModule());
121123
LeaderOSAPI.getModuleManager().registerModule(new ConnectModule());
122124

123125
if (getConfigFile().getSettings().getUrl().equals("https://yourwebsite.com")) {

‎bukkit/src/main/java/net/leaderos/plugin/configuration/Modules.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,62 @@ public static class NextPage extends OkaeriConfig {
719719
}
720720
}
721721
}
722+
723+
/**
724+
* CommunityGoal module setting
725+
*/
726+
private CommunityGoal CommunityGoal = new CommunityGoal();
727+
728+
/**
729+
* CommunityGoal module settings
730+
*
731+
* @since 1.0
732+
* @author leaderos
733+
*/
734+
@Getter
735+
@Setter
736+
public static class CommunityGoal extends OkaeriConfig {
737+
/**
738+
* Status of CommunityGoal
739+
*/
740+
private boolean status = true;
741+
/**
742+
* CommunityGoal scheduler second
743+
*/
744+
private long updateSecond = 900;
745+
746+
/**
747+
* Progress bar settings
748+
*/
749+
private ProgressBar progressBar = new ProgressBar();
750+
751+
/**
752+
* ProgressBar settings
753+
* @author leaderos
754+
* @since 1.0
755+
*/
756+
@Getter
757+
@Setter
758+
public static class ProgressBar extends OkaeriConfig {
759+
/**
760+
* Filled color
761+
*/
762+
private String filledColor = "&a";
763+
764+
/**
765+
* Empty color
766+
*/
767+
private String emptyColor = "&f";
768+
769+
/**
770+
* Bar symbol
771+
*/
772+
private String symbol = "|";
773+
774+
/**
775+
* Bar length
776+
*/
777+
private int length = 10;
778+
}
779+
}
722780
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.leaderos.plugin.modules.communitygoal;
2+
3+
import net.leaderos.plugin.modules.communitygoal.timer.Timer;
4+
import net.leaderos.shared.modules.LeaderOSModule;
5+
6+
/**
7+
* CommunityGoal module main class
8+
* @author leaderos
9+
* @since 1.0
10+
*/
11+
public class CommunityGoalModule extends LeaderOSModule {
12+
13+
/**
14+
* onEnable method of module
15+
*/
16+
public void onEnable() {
17+
Timer.run();
18+
if( org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"))
19+
new Placeholders().register();
20+
}
21+
22+
/**
23+
* onDisable method of module
24+
*/
25+
public void onDisable() {
26+
if (Timer.task != null) {
27+
Timer.task.cancel();
28+
}
29+
if( org.bukkit.Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"))
30+
new Placeholders().unregister();
31+
}
32+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package net.leaderos.plugin.modules.communitygoal;
2+
3+
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
4+
import net.leaderos.plugin.Bukkit;
5+
import net.leaderos.plugin.configuration.Modules;
6+
import net.leaderos.plugin.modules.communitygoal.managers.CommunityGoalManager;
7+
import net.leaderos.plugin.modules.communitygoal.model.CommunityGoal;
8+
import net.leaderos.plugin.helpers.ChatUtil;
9+
import org.bukkit.entity.Player;
10+
11+
import java.text.DecimalFormat;
12+
import java.text.DecimalFormatSymbols;
13+
import java.util.Locale;
14+
15+
/**
16+
* Community Goal module placeholder system
17+
* @author leaderos
18+
* @since 1.0
19+
*/
20+
public class Placeholders extends PlaceholderExpansion {
21+
22+
/**
23+
* identifier of placeholder
24+
* @return identifier
25+
*/
26+
public String getIdentifier() {
27+
return "leaderos-communitygoal";
28+
}
29+
30+
/**
31+
* The author of the Placeholder
32+
* This cannot be null
33+
* @return author name
34+
*/
35+
public String getAuthor() {
36+
return "leaderos";
37+
}
38+
39+
/**
40+
* Same with #getAuthor() but for versioon
41+
* This cannot be null
42+
* @return version of plugin
43+
*/
44+
public String getVersion() {
45+
return "1.0";
46+
}
47+
48+
/**
49+
* PlaceholderRequest method
50+
* @param p player
51+
* @param identifier placeholder identifier
52+
* @return placeholder value
53+
*/
54+
@Override
55+
public String onPlaceholderRequest(Player p, String identifier) {
56+
CommunityGoal communityGoal = CommunityGoalManager.getCommunityGoal();
57+
if (communityGoal == null)
58+
return null;
59+
60+
if (identifier.equals("is_active")) {
61+
return String.valueOf(communityGoal.isActive());
62+
}
63+
else if (identifier.equals("currency")) {
64+
return communityGoal.getCurrency();
65+
}
66+
else if (identifier.equals("total_raised")) {
67+
return formatDouble(communityGoal.getTotalRaised());
68+
}
69+
else if (identifier.equals("goal_amount")) {
70+
return formatDouble(communityGoal.getGoalAmount());
71+
}
72+
else if (identifier.equals("percentage_achieved")) {
73+
return formatDouble(communityGoal.getPercentageAchieved());
74+
}
75+
else if (identifier.equals("progress_bar")) {
76+
return getProgressBar(communityGoal.getPercentageAchieved());
77+
}
78+
79+
return null;
80+
}
81+
82+
/**
83+
* Format double value
84+
* @param value double value
85+
* @return formatted string
86+
*/
87+
private String formatDouble(double value) {
88+
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
89+
DecimalFormat decimalFormat = new DecimalFormat("0.00", symbols);
90+
String formatted = decimalFormat.format(value);
91+
if (formatted.endsWith(".00")) {
92+
return formatted.substring(0, formatted.length() - 3);
93+
}
94+
return formatted;
95+
}
96+
97+
/**
98+
* Get progress bar
99+
* @param percentage percentage
100+
* @return progress bar
101+
*/
102+
private String getProgressBar(double percentage) {
103+
Modules.CommunityGoal.ProgressBar progressBar = Bukkit.getInstance().getModulesFile().getCommunityGoal().getProgressBar();
104+
int length = progressBar.getLength();
105+
String symbol = progressBar.getSymbol();
106+
String filledColor = progressBar.getFilledColor();
107+
String emptyColor = progressBar.getEmptyColor();
108+
109+
int filledAmount = (int) (percentage / 100 * length);
110+
if (filledAmount > length)
111+
filledAmount = length;
112+
113+
StringBuilder sb = new StringBuilder();
114+
sb.append(filledColor);
115+
for (int i = 0; i < filledAmount; i++) {
116+
sb.append(symbol);
117+
}
118+
sb.append(emptyColor);
119+
for (int i = 0; i < length - filledAmount; i++) {
120+
sb.append(symbol);
121+
}
122+
123+
return ChatUtil.color(sb.toString());
124+
}
125+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.leaderos.plugin.modules.communitygoal.managers;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import net.leaderos.plugin.modules.communitygoal.model.CommunityGoal;
6+
import net.leaderos.shared.model.Response;
7+
import net.leaderos.shared.model.request.impl.communitygoal.GetCommunityGoalRequest;
8+
import org.json.JSONObject;
9+
10+
import java.io.IOException;
11+
import java.net.HttpURLConnection;
12+
13+
/**
14+
* CommunityGoal manager class
15+
*
16+
* @author leaderos
17+
* @since 1.0
18+
*/
19+
@Getter
20+
@Setter
21+
public class CommunityGoalManager {
22+
23+
/**
24+
* CommunityGoal object
25+
*/
26+
@Getter
27+
private static CommunityGoal communityGoal;
28+
29+
/**
30+
* Update the community goal data
31+
*/
32+
public static void update() {
33+
try {
34+
Response response = new GetCommunityGoalRequest().getResponse();
35+
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
36+
JSONObject json = response.getResponseMessage();
37+
communityGoal = new CommunityGoal(json);
38+
}
39+
} catch (IOException ignored) {}
40+
}
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.leaderos.plugin.modules.communitygoal.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.json.JSONObject;
8+
9+
/**
10+
* CommunityGoal model class
11+
*
12+
* @author leaderos
13+
* @since 1.0
14+
*/
15+
@Getter
16+
@Setter
17+
@AllArgsConstructor
18+
public class CommunityGoal {
19+
20+
/**
21+
* is active
22+
*/
23+
private boolean isActive;
24+
25+
/**
26+
* currency
27+
*/
28+
private String currency;
29+
30+
/**
31+
* total raised
32+
*/
33+
private double totalRaised;
34+
35+
/**
36+
* goal amount
37+
*/
38+
private double goalAmount;
39+
40+
/**
41+
* percentage achieved
42+
*/
43+
private double percentageAchieved;
44+
45+
/**
46+
* CommunityGoal constructor from JSONObject
47+
*
48+
* @param json JSONObject
49+
*/
50+
public CommunityGoal(@NotNull JSONObject json) {
51+
this.isActive = json.getBoolean("is_active");
52+
this.currency = json.getString("currency");
53+
this.totalRaised = json.getDouble("total_raised");
54+
this.goalAmount = json.getDouble("goal_amount");
55+
this.percentageAchieved = json.getDouble("percentage_achieved");
56+
}
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.leaderos.plugin.modules.communitygoal.timer;
2+
3+
import com.tcoded.folialib.wrapper.task.WrappedTask;
4+
import net.leaderos.plugin.Bukkit;
5+
import net.leaderos.plugin.modules.communitygoal.managers.CommunityGoalManager;
6+
7+
/**
8+
* Time checker for update scheduler
9+
*
10+
* @author leaderos
11+
* @since 1.0
12+
*/
13+
public class Timer {
14+
15+
/**
16+
* Runnable id for cancel or resume
17+
*/
18+
public static WrappedTask task;
19+
20+
/**
21+
* Run the timer
22+
*/
23+
public static void run() {
24+
if (task != null) {
25+
task.cancel();
26+
task = null;
27+
}
28+
29+
task = Bukkit.getFoliaLib().getScheduler().runTimerAsync(() -> {
30+
CommunityGoalManager.update();
31+
}, 1L, 20 * Bukkit.getInstance().getModulesFile().getCommunityGoal().getUpdateSecond());
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.leaderos.shared.model.request.impl.communitygoal;
2+
3+
import net.leaderos.shared.model.request.GetRequest;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* GetCommunityGoalRequest class
9+
*
10+
* @author leaderos
11+
* @since 1.0
12+
*/
13+
public class GetCommunityGoalRequest extends GetRequest {
14+
15+
/**
16+
* Request constructor
17+
*
18+
* @throws IOException for HttpUrlConnection
19+
*/
20+
public GetCommunityGoalRequest() throws IOException {
21+
super("store/community-goal");
22+
}
23+
}

0 commit comments

Comments
 (0)