1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Fixed timed boosts

This commit is contained in:
Zrips 2022-01-19 17:18:12 +02:00
parent 33d14cf927
commit 3b0cfc662f
3 changed files with 18 additions and 84 deletions

View File

@ -5,8 +5,7 @@ import java.util.HashMap;
public class BoostMultiplier implements Cloneable {
private final java.util.Map<CurrencyType, Double> map = new HashMap<>();
private Long time = 0L;
private final java.util.Map<CurrencyType, Long> timers = new HashMap<>();
@Override
public BoostMultiplier clone() {
@ -29,7 +28,7 @@ public class BoostMultiplier implements Cloneable {
}
public BoostMultiplier add(CurrencyType type, double amount, long time) {
this.time = time;
timers.put(type, time);
return add(type, amount);
}
@ -39,27 +38,31 @@ public class BoostMultiplier implements Cloneable {
map.put(one, amount);
}
}
return this;
}
public double get(CurrencyType type) {
isValid(type); // Call without check to make sure map cache is removed
if (!isValid(type))
return 0D;
return map.getOrDefault(type, 0D);
}
public Long getTime() {
return time;
public Long getTime(CurrencyType type) {
return timers.get(type);
}
public boolean isValid(CurrencyType type) {
boolean valid = time > System.currentTimeMillis();
if (time != 0L && !valid) {
Long time = getTime(type);
if (time == null)
return true;
if (time < System.currentTimeMillis()) {
map.remove(type);
time = 0L;
timers.remove(type);
return false;
}
return time == 0L || valid;
return true;
}
public void add(BoostMultiplier armorboost) {

View File

@ -158,34 +158,16 @@ public class Job {
*
* @param type the type of {@link CurrencyType}}
* @param point the amount of boost to add
* @param times the array of integer of when to remove the boost
* @param duration boost duration in seconds
*/
public void addBoost(CurrencyType type, double point, int[] times) {
if (times.length < 3)
return;
public void addBoost(CurrencyType type, double point, long duration) {
final int h = times[2], m = times[1], s = times[0];
if (h == 0 && m == 0 && s == 0) {
if (duration <= 0) {
addBoost(type, point);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
if (h > 0) {
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + h);
}
if (m > 0) {
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + m);
}
if (s > 0) {
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + s);
}
boost.add(type, point, cal.getTimeInMillis());
boost.add(type, point, System.currentTimeMillis() + (duration * 1000L));
}
public void setBoost(BoostMultiplier boost) {

View File

@ -99,57 +99,6 @@ public final class Util {
}
}
public static int[] parseTime(String[] args) {
int[] arr = new int[3];
if (args.length < 2) {
return arr;
}
String time = args[1].toLowerCase();
if (time.isEmpty()) {
return arr;
}
String[] split = time.split("h|hour", 2);
if (split.length > 0) {
try {
arr[2] = Integer.parseInt(split[0]);
} catch (NumberFormatException e) {
arr[2] = 0;
}
time = time.replaceAll(arr[2] + "+[h|hour]+", "");
}
if ((split = time.split("m|minute", 2)).length > 0) {
try {
arr[1] = Integer.parseInt(split[0]);
} catch (NumberFormatException e) {
arr[1] = 0;
}
time = time.replaceAll(arr[1] + "+[m|minute]+", "");
}
if ((split = time.split("s|second", 2)).length > 0) {
try {
arr[0] = Integer.parseInt(split[0]);
} catch (NumberFormatException e) {
arr[0] = 0;
}
time = time.replaceAll(arr[0] + "+[s|second]+", "");
}
if (arr[0] == 0 && arr[1] == 0 && arr[2] == 0) {
return new int[3];
}
return arr;
}
public static String getRealType(Entity entity) {
if (Version.isCurrentEqualOrHigher(Version.v1_11_R1)) {
return entity.getType().name();