Skip to content

Commit e69b078

Browse files
committed
Add date based reward requirements
1 parent 4320639 commit e69b078

File tree

3 files changed

+155
-11
lines changed

3 files changed

+155
-11
lines changed

AdvancedCore/src/main/java/com/bencodez/advancedcore/api/rewards/RewardHandler.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.File;
44
import java.text.DecimalFormat;
5+
import java.time.DayOfWeek;
56
import java.time.LocalDateTime;
7+
import java.time.Month;
68
import java.util.ArrayList;
79
import java.util.Collections;
810
import java.util.Comparator;
@@ -48,6 +50,7 @@
4850
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditAdvancedWorld;
4951
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditBossBar;
5052
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditChoices;
53+
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditDate;
5154
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditEXP;
5255
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditEXPLevels;
5356
import com.bencodez.advancedcore.api.rewards.editbuttons.RewardEditEffect;
@@ -1072,6 +1075,104 @@ public void onValidate(Reward reward, RequirementInject inject, ConfigurationSec
10721075
}
10731076
}));
10741077

1078+
injectedRequirements.add(new RequirementInjectConfigurationSection("Date") {
1079+
@Override
1080+
public boolean onRequirementsRequested(Reward reward, AdvancedCoreUser user, ConfigurationSection section,
1081+
RewardOptions rewardOptions) {
1082+
LocalDateTime now = LocalDateTime.now();
1083+
1084+
// Validate WeekDay
1085+
if (section.isString("WeekDay")) {
1086+
String requiredWeekDay = section.getString("WeekDay").toUpperCase();
1087+
if (!now.getDayOfWeek().name().equals(requiredWeekDay)) {
1088+
debug("WeekDay does not match: " + requiredWeekDay);
1089+
return false;
1090+
}
1091+
} else if (section.isInt("WeekDay")) {
1092+
int requiredWeekDay = section.getInt("WeekDay");
1093+
if (now.getDayOfWeek().getValue() != requiredWeekDay) {
1094+
debug("WeekDay does not match: " + requiredWeekDay);
1095+
return false;
1096+
}
1097+
}
1098+
1099+
// Validate DayOfMonth
1100+
if (section.isInt("DayOfMonth")) {
1101+
int requiredDayOfMonth = section.getInt("DayOfMonth");
1102+
if (now.getDayOfMonth() != requiredDayOfMonth) {
1103+
debug("DayOfMonth does not match: " + requiredDayOfMonth);
1104+
return false;
1105+
}
1106+
}
1107+
1108+
// Validate Month
1109+
if (section.isString("Month")) {
1110+
String requiredMonth = section.getString("Month").toUpperCase();
1111+
if (!now.getMonth().name().equals(requiredMonth)) {
1112+
debug("Month does not match: " + requiredMonth);
1113+
return false;
1114+
}
1115+
}
1116+
1117+
return true;
1118+
}
1119+
}.priority(90).validator(new RequirementInjectValidator() {
1120+
@Override
1121+
public void onValidate(Reward reward, RequirementInject inject, ConfigurationSection data) {
1122+
if (!data.isConfigurationSection("Date")) {
1123+
return;
1124+
}
1125+
1126+
ConfigurationSection section = data.getConfigurationSection("Date");
1127+
1128+
// Validate WeekDay
1129+
if (section.isString("WeekDay")) {
1130+
try {
1131+
DayOfWeek.valueOf(section.getString("WeekDay").toUpperCase());
1132+
} catch (IllegalArgumentException e) {
1133+
warning(reward, inject, "Invalid WeekDay: " + section.getString("WeekDay"));
1134+
}
1135+
} else if (section.isInt("WeekDay")) {
1136+
int weekDay = section.getInt("WeekDay");
1137+
if (weekDay < 1 || weekDay > 7) {
1138+
warning(reward, inject, "Invalid WeekDay: " + weekDay);
1139+
}
1140+
}
1141+
1142+
// Validate DayOfMonth
1143+
if (section.isInt("DayOfMonth")) {
1144+
int day = section.getInt("DayOfMonth");
1145+
if (day < 1 || day > 31) {
1146+
warning(reward, inject, "Invalid DayOfMonth: " + day);
1147+
}
1148+
}
1149+
1150+
// Validate Month
1151+
if (section.isString("Month")) {
1152+
try {
1153+
Month.valueOf(section.getString("Month").toUpperCase());
1154+
} catch (IllegalArgumentException e) {
1155+
warning(reward, inject, "Invalid Month: " + section.getString("Month"));
1156+
}
1157+
}
1158+
}
1159+
}).addEditButton(new EditGUIButton(new ItemBuilder("PAPER"), new EditGUIValueInventory("Date") {
1160+
1161+
@Override
1162+
public void openInventory(ClickEvent clickEvent) {
1163+
RewardEditData reward = (RewardEditData) getInv().getData("Reward");
1164+
new RewardEditDate() {
1165+
1166+
@Override
1167+
public void setVal(String key, Object value) {
1168+
RewardEditData reward = (RewardEditData) getInv().getData("Reward");
1169+
reward.setValue(key, value);
1170+
plugin.reloadAdvancedCore(false);
1171+
}
1172+
}.open(clickEvent.getPlayer(), reward);
1173+
}
1174+
}.addLore("Edit date-based requirements for the reward"))));
1175+
10751176
injectedRequirements.add(new RequirementInjectConfigurationSection("LocationDistance") {
10761177
@Override
10771178
public boolean onRequirementsRequested(Reward reward, AdvancedCoreUser user, ConfigurationSection section,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bencodez.advancedcore.api.rewards.editbuttons;
2+
3+
import org.bukkit.entity.Player;
4+
5+
import com.bencodez.advancedcore.api.inventory.editgui.EditGUI;
6+
import com.bencodez.advancedcore.api.rewards.RewardEditData;
7+
8+
public abstract class RewardEditDate extends RewardEdit {
9+
public RewardEditDate() {
10+
}
11+
12+
@Override
13+
public void open(Player player, RewardEditData reward) {
14+
EditGUI inv = new EditGUI("Edit Dzte: " + reward.getName());
15+
inv.addData("Reward", reward);
16+
17+
inv.addButton(getStringButton("Date.WeekDay", reward).addOptions("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
18+
"FRIDAY", "SATURDAY", "SUNDAY"));
19+
inv.addButton(getIntButton("Date.DayOfMonth", reward));
20+
inv.addButton(getStringButton("Date.Month", reward).addOptions("JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",
21+
"JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"));
22+
23+
inv.addButton(getBackButton(reward));
24+
25+
inv.openInventory(player);
26+
}
27+
}

AdvancedCore/src/main/resources/Rewards/ExampleAdvanced.yml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,7 @@ Permission: 'AdvancedCore.Reward.ExampleAdvanced'
111111
# Amount of time to still allow the reward to be executed
112112
# if reward is saved offline
113113
# Time in minutes
114-
#RewardExpiration: 1440
115-
116-
117-
# DayOfMonth requirement configuration
118-
DayOfMonth:
119-
# Enable or disable the DayOfMonth requirement
120-
Enabled: false
121-
# List of days of the month when the requirement should be met
122-
Days:
123-
- 1
124-
- 15
114+
#RewardExpiration: 1440
125115

126116
# Javascript
127117
# Only use this if you know what you are doing
@@ -266,6 +256,32 @@ RewardType: 'BOTH'
266256
# ONLY USE THIS IF YOU KNOW WHAT YOU ARE DOING
267257
ForceOffline: false
268258

259+
260+
# ------------------------------------
261+
# Date based rewards
262+
# ------------------------------------
263+
264+
Date:
265+
# Day required to give reward
266+
# Also supports using 1-7 for week day
267+
WeekDay: MONDAY
268+
269+
# Day of the month to give reward
270+
DayOfMonth: 15
271+
272+
# Month required to give reward
273+
Month: MARCH
274+
275+
# DayOfMonth requirement configuration
276+
DayOfMonth:
277+
# Enable or disable the DayOfMonth requirement
278+
Enabled: false
279+
# List of days of the month when the requirement should be met
280+
Days:
281+
- 1
282+
- 15
283+
284+
269285
# Vault group required to get reward
270286
#VaultGroup: 'NORMAL'
271287

0 commit comments

Comments
 (0)