1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-16 13:11:19 +01:00

Reseting limits at specific time of the day

This commit is contained in:
Zrips 2023-04-24 14:11:32 +03:00
parent 9b291b299b
commit 3bfb0f60cc
6 changed files with 255 additions and 84 deletions

View File

@ -10,6 +10,7 @@ import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.economy.PaymentData;
import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.Time.CMITimeManager;
@ -54,8 +55,10 @@ public class limit implements Cmd {
continue;
PaymentData limit = JPlayer.getPaymentLimit();
if (limit.getLeftTime(type) <= 0)
limit.resetLimits(type);
if (limit.getLeftTime(type) <= 0) {
CMIDebug.d("reset", type);
limit.resetLimits(type);
}
if (limit.getLeftTime(type) > 0) {
String typeName = type.getName().toLowerCase();

View File

@ -95,7 +95,8 @@ public class GeneralConfigManager {
SignsColorizeJobName, ShowToplistInScoreboard, useGlobalTimer, useSilkTouchProtection, UseCustomNames,
PreventSlimeSplit, PreventMagmaCubeSplit, PreventHopperFillUps, PreventBrewingStandFillUps, informOnPaymentDisable,
BrowseUseNewLook, payExploringWhenGliding = false, resetExploringData = false, disablePaymentIfMaxLevelReached, disablePaymentIfRiding,
boostedItemsInOffHand = false, boostedItemsInMainHand, boostedArmorItems, boostedItemsSlotSpecific, multiplyBoostedExtraValues, addPermissionBoost, highestPermissionBoost /*, preventCropResizePayment*/,
boostedItemsInOffHand = false, boostedItemsInMainHand, boostedArmorItems, boostedItemsSlotSpecific, multiplyBoostedExtraValues, addPermissionBoost,
highestPermissionBoost /*, preventCropResizePayment*/,
payItemDurabilityLoss,
applyToNegativeIncome, useMinimumOveralPayment, useMinimumOveralPoints, useMinimumOveralExp, useBreederFinder,
CancelCowMilking, fixAtMaxLevel, TitleChangeChat, TitleChangeActionBar, LevelChangeChat,
@ -543,7 +544,7 @@ public class GeneralConfigManager {
CMIMaterial mat = CMIMaterial.get(mName);
if (mat == CMIMaterial.NONE) {
CMIMessages.consoleMessage("Failed to recognize " + one + " entry from config file");
CMIMessages.consoleMessage("Failed to recognize " + one + " entry from config file");
continue;
}
@ -702,8 +703,14 @@ public class GeneralConfigManager {
Jobs.getPluginLogger().warning("MoneyLimit has an invalid value. Disabling money limit!");
limit.setEnabled(false);
}
c.addComment("Economy.Limit.Money.TimeLimit", "Time in seconds: 60 = 1 min, 3600 = 1 hour, 86400 = 24 hours");
limit.setTimeLimit(c.get("Economy.Limit.Money.TimeLimit", 3600));
c.addComment("Economy.Limit.Money.ResetTime", "Time in 24 hour format when limit should reset. This will override TimeLimit if defined",
"Example: '00:00:00' will reset timer at midnight, '04:30:00' will reset at 4:30 in the morning", "Set to empty field if you want to use TimeLimit");
limit.setResetsAt(c.get("Economy.Limit.Money.ResetTime", ""));
c.addComment("Economy.Limit.Money.AnnouncementDelay", "Delay between announcements about reached money limit",
"Keep this from 30 to 5 min (300), as players can get annoyed of constant message displaying");
limit.setAnnouncementDelay(c.get("Economy.Limit.Money.AnnouncementDelay", 30));
@ -736,6 +743,11 @@ public class GeneralConfigManager {
}
c.addComment("Economy.Limit.Point.TimeLimit", "Time in seconds: 60 = 1 min, 3600 = 1 hour, 86400 = 24 hours");
limit.setTimeLimit(c.get("Economy.Limit.Point.TimeLimit", 3600));
c.addComment("Economy.Limit.Point.ResetTime", "Time in 24 hour format when limit should reset. This will override TimeLimit if defined",
"Example: '00:00:00' will reset timer at midnight, '04:30:00' will reset at 4:30 in the morning", "Set to empty field if you want to use TimeLimit");
limit.setResetsAt(c.get("Economy.Limit.Point.ResetTime", ""));
c.addComment("Economy.Limit.Point.AnnouncementDelay", "Delay between announcements about reached limit",
"Keep this from 30 to 5 min (300), as players can get annoyed of constant message displaying");
limit.setAnnouncementDelay(c.get("Economy.Limit.Point.AnnouncementDelay", 30));
@ -768,6 +780,11 @@ public class GeneralConfigManager {
}
c.addComment("Economy.Limit.Exp.TimeLimit", "Time in seconds: 60 = 1 min, 3600 = 1 hour, 86400 = 24 hours");
limit.setTimeLimit(c.get("Economy.Limit.Exp.TimeLimit", 3600));
c.addComment("Economy.Limit.Exp.ResetTime", "Time in 24 hour format when limit should reset. This will override TimeLimit if defined",
"Example: '00:00:00' will reset timer at midnight, '04:30:00' will reset at 4:30 in the morning", "Set to empty field if you want to use TimeLimit");
limit.setResetsAt(c.get("Economy.Limit.Exp.ResetTime", ""));
c.addComment("Economy.Limit.Exp.AnnouncementDelay", "Delay between announcements about reached Exp limit",
"Keep this from 30 to 5 min (300), as players can get annoyed of constant message displaying");
limit.setAnnouncementDelay(c.get("Economy.Limit.Exp.AnnouncementDelay", 30));

View File

@ -4,66 +4,104 @@ import java.util.ArrayList;
import java.util.List;
import net.Zrips.CMILib.Equations.Parser;
import net.Zrips.CMILib.Messages.CMIMessages;
public class CurrencyLimit {
private boolean enabled;
private List<CurrencyType> stopWith;
private int timeLimit;
private int timeLimit = 0;
private resetTime resetsAt = null;
private int announcementDelay;
private Parser maxEquation;
public CurrencyLimit(boolean enabled, List<CurrencyType> stopWith, int timeLimit, int announcementDelay, Parser maxEquation) {
this.enabled = enabled;
setStopWith(stopWith);
this.timeLimit = timeLimit;
this.announcementDelay = announcementDelay;
this.enabled = enabled;
setStopWith(stopWith);
this.timeLimit = timeLimit;
this.announcementDelay = announcementDelay;
setMaxEquation(maxEquation);
setMaxEquation(maxEquation);
}
public CurrencyLimit() {
}
public boolean isEnabled() {
return enabled;
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
this.enabled = enabled;
}
public List<CurrencyType> getStopWith() {
return stopWith;
return stopWith;
}
public void setStopWith(List<CurrencyType> stopWith) {
this.stopWith = stopWith == null ? new ArrayList<>() : stopWith;
this.stopWith = stopWith == null ? new ArrayList<>() : stopWith;
}
public int getTimeLimit() {
return timeLimit;
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
this.timeLimit = timeLimit;
}
public int getAnnouncementDelay() {
return announcementDelay;
return announcementDelay;
}
public void setAnnouncementDelay(int announcementDelay) {
this.announcementDelay = announcementDelay;
this.announcementDelay = announcementDelay;
}
public Parser getMaxEquation() {
return maxEquation;
return maxEquation;
}
public void setMaxEquation(Parser maxEquation) {
if (maxEquation != null)
this.maxEquation = maxEquation;
if (maxEquation != null)
this.maxEquation = maxEquation;
}
public resetTime getResetsAt() {
return resetsAt;
}
public void setResetsAt(int hour, int minute, int second) {
this.resetsAt = new resetTime(hour, minute, second);
}
public void setResetsAt(String resetsAt) {
if (resetsAt.isEmpty())
return;
int hour = 0;
int minute = 0;
int second = 0;
String[] split = resetsAt.split(":");
try {
hour = Integer.parseInt(split[0]);
if (split.length >= 2) {
minute = Integer.parseInt(split[1]);
}
if (split.length >= 3) {
second = Integer.parseInt(split[2]);
}
} catch (Throwable e) {
CMIMessages.consoleMessage("Failed to recognize reset time as " + resetsAt);
return;
}
setResetsAt(hour, minute, second);
}
}

View File

@ -0,0 +1,58 @@
package com.gamingmesh.jobs.container;
import java.util.Calendar;
import java.util.Date;
public class resetTime {
private int hour = 0;
private int minute = 0;
private int second = 0;
public resetTime(int hour, int minute, int second) {
this.hour = hour % 24;
this.minute = minute % 60;
this.second = second % 60;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public long toMili() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
return calendar.getTimeInMillis();
}
}

View File

@ -1,57 +1,94 @@
package com.gamingmesh.jobs.economy;
import java.util.Calendar;
import java.util.Date;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.CurrencyLimit;
import com.gamingmesh.jobs.container.CurrencyType;
public class LimitsData {
private CurrencyType type = null;
private double amount = 0D;
private long paymentsTime = 0L;
private long resetsAt = 0L;
private boolean reseted = false;
@Deprecated
public LimitsData(CurrencyType type, long paymentsTime) {
this(type, paymentsTime, 0D);
this(type, paymentsTime, 0D);
}
@Deprecated
public LimitsData(CurrencyType type, long paymentsTime, double payment) {
this.type = type;
this.paymentsTime = paymentsTime;
this.amount = payment;
this.type = type;
this.paymentsTime = paymentsTime;
CurrencyLimit limit = Jobs.getGCManager().getLimit(type);
if (limit.getResetsAt() != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
resetsAt = limit.getResetsAt().toMili();
} else
resetsAt = (limit.getTimeLimit() * 100L) + System.currentTimeMillis();
this.amount = payment;
}
public LimitsData(CurrencyType type, double payment) {
this.type = type;
this.paymentsTime = System.currentTimeMillis();
CurrencyLimit limit = Jobs.getGCManager().getLimit(type);
if (limit.getResetsAt() != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
resetsAt = limit.getResetsAt().toMili();
} else
resetsAt = (limit.getTimeLimit() * 100L) + System.currentTimeMillis();
this.amount = payment;
}
public boolean isReseted() {
return reseted;
return reseted;
}
public void setReseted(boolean reseted) {
this.reseted = reseted;
this.reseted = reseted;
}
public CurrencyType getType() {
return type;
return type;
}
public void setType(CurrencyType type) {
this.type = type;
this.type = type;
}
public double getAmount() {
return this.amount;
return this.amount;
}
public double addAmount(double amount) {
return this.amount += amount;
return this.amount += amount;
}
public void setAmount(double payment) {
this.amount = payment;
this.amount = payment;
}
public long getPaymentsTime() {
return paymentsTime;
return paymentsTime;
}
public void setPaymentsTime(long paymentsTime) {
this.paymentsTime = paymentsTime;
this.paymentsTime = paymentsTime;
}
public long getResetsAt() {
return resetsAt;
}
public void setResetsAt(long resetsAt) {
this.resetsAt = resetsAt;
}
}

View File

@ -3,6 +3,7 @@ package com.gamingmesh.jobs.economy;
import java.util.HashMap;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.CurrencyLimit;
import com.gamingmesh.jobs.container.CurrencyType;
public class PaymentData {
@ -13,119 +14,136 @@ public class PaymentData {
private boolean informed = false;
public PaymentData(Long time, Double Payment, Double Points, Double Exp, Long lastAnnouced, boolean Informed) {
payments.put(CurrencyType.EXP, new LimitsData(CurrencyType.EXP, time, Exp));
payments.put(CurrencyType.MONEY, new LimitsData(CurrencyType.MONEY, time, Payment));
payments.put(CurrencyType.POINTS, new LimitsData(CurrencyType.POINTS, time, Points));
this.lastAnnouced = lastAnnouced;
this.informed = Informed;
}
// public PaymentData(Long time, Double Payment, Double Points, Double Exp, Long lastAnnouced, boolean Informed) {
// payments.put(CurrencyType.EXP, new LimitsData(CurrencyType.EXP, time, Exp));
// payments.put(CurrencyType.MONEY, new LimitsData(CurrencyType.MONEY, time, Payment));
// payments.put(CurrencyType.POINTS, new LimitsData(CurrencyType.POINTS, time, Points));
// this.lastAnnouced = lastAnnouced;
// this.informed = Informed;
// }
public PaymentData(CurrencyType type, Double amount) {
for (CurrencyType one : CurrencyType.values()) {
if (one != type)
payments.put(one, new LimitsData(one, System.currentTimeMillis(), 0D));
}
payments.put(type, new LimitsData(type, System.currentTimeMillis(), amount));
for (CurrencyType one : CurrencyType.values()) {
if (one != type)
payments.put(one, new LimitsData(one, 0D));
}
payments.put(type, new LimitsData(type, amount));
}
public PaymentData() {
resetLimits();
resetLimits();
}
public Long getTime(CurrencyType type) {
return payments.get(type).getPaymentsTime();
return payments.get(type).getPaymentsTime();
}
public void setReseted(CurrencyType type, boolean reseted) {
payments.get(type).setReseted(reseted);
payments.get(type).setReseted(reseted);
}
public boolean isReseted(CurrencyType type) {
return payments.get(type).isReseted();
return payments.get(type).isReseted();
}
public double getAmount(CurrencyType type) {
if (type == null)
return 0D;
if (type == null)
return 0D;
LimitsData data = payments.get(type);
return data == null ? 0D : (int) (data.getAmount() * 100) / 100D;
LimitsData data = payments.get(type);
return data == null ? 0D : (int) (data.getAmount() * 100) / 100D;
}
public long getLastAnnounced() {
return lastAnnouced;
return lastAnnouced;
}
public boolean isAnnounceTime(int t) {
if (lastAnnouced + (t * 1000) > System.currentTimeMillis())
return false;
if (lastAnnouced + (t * 1000) > System.currentTimeMillis())
return false;
setAnnouncementTime();
return true;
setAnnouncementTime();
return true;
}
public void setAnnouncementTime() {
this.lastAnnouced = System.currentTimeMillis();
this.lastAnnouced = System.currentTimeMillis();
}
public void addNewAmount(CurrencyType type, Double Payment) {
addNewAmount(type, Payment, null);
addNewAmount(type, Payment, null);
}
public void addNewAmount(CurrencyType type, Double Payment, Long time) {
payments.put(type, new LimitsData(type, time == null ? System.currentTimeMillis() : time, Payment));
// payments.put(type, new LimitsData(type, time == null ? System.currentTimeMillis() : time, Payment));
LimitsData data = new LimitsData(type, Payment);
if (time != null)
data.setPaymentsTime(time);
payments.put(type, data);
}
public void addAmount(CurrencyType type, double payment) {
payments.get(type).addAmount(payment);
payments.get(type).addAmount(payment);
}
public long getLeftTime(CurrencyType type) {
long left = getTime(type) + (Jobs.getGCManager().getLimit(type).getTimeLimit() * 1000);
return left > System.currentTimeMillis() ? left - System.currentTimeMillis() : 0L;
CurrencyLimit limit = Jobs.getGCManager().getLimit(type);
if (limit.getResetsAt() != null) {
LimitsData data = payments.get(type);
if (data.getResetsAt() < System.currentTimeMillis())
return 0L;
return data.getResetsAt() - System.currentTimeMillis();
}
long left = getTime(type) + (limit.getTimeLimit() * 1000);
return left > System.currentTimeMillis() ? left - System.currentTimeMillis() : 0L;
}
public boolean isOverLimit(CurrencyType type, int limit) {
return payments.get(type).getAmount() >= limit;
return payments.get(type).getAmount() >= limit;
}
public double percentOverLimit(CurrencyType type, int limit) {
return ((payments.get(type).getAmount() / limit) - 1) * 100;
return ((payments.get(type).getAmount() / limit) - 1) * 100;
}
public boolean isOverTimeLimit(CurrencyType type) {
if (getTime(type) + (Jobs.getGCManager().getLimit(type).getTimeLimit() * 1000) > System.currentTimeMillis()) {
return false;
}
if (informed)
informed = false;
resetLimits();
return true;
if (getLeftTime(type) > 0) {
return false;
}
if (informed)
informed = false;
resetLimits();
return true;
}
public void resetLimits() {
for (CurrencyType type : CurrencyType.values()) {
addNewAmount(type, 0D);
setReseted(type, true);
}
for (CurrencyType type : CurrencyType.values()) {
addNewAmount(type, 0D);
setReseted(type, true);
}
}
public void resetLimits(CurrencyType type) {
addNewAmount(type, 0D);
setReseted(type, true);
addNewAmount(type, 0D);
setReseted(type, true);
}
public boolean isReachedLimit(CurrencyType type, int money) {
isOverTimeLimit(type);
return isOverLimit(type, money);
isOverTimeLimit(type);
return isOverLimit(type, money);
}
public boolean isInformed() {
return informed;
return informed;
}
public void setInformed(boolean informed) {
this.informed = informed;
this.informed = informed;
}
}