mirror of
https://github.com/songoda/EpicBosses.git
synced 2025-03-12 22:49:13 +01:00
1.0.0-SNAPSHOT-U169
+ Implemented TimeUnit and TimeUtil + Started working on the AutoSpawn system + Started implementing the ActiveAutoSpawnHolders
This commit is contained in:
parent
6badd928b7
commit
67feb1343b
@ -3,6 +3,7 @@ package com.songoda.epicbosses.api;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.songoda.epicbosses.CustomBosses;
|
||||
import com.songoda.epicbosses.autospawns.AutoSpawn;
|
||||
import com.songoda.epicbosses.droptable.DropTable;
|
||||
import com.songoda.epicbosses.droptable.elements.DropTableElement;
|
||||
import com.songoda.epicbosses.droptable.elements.GiveTableElement;
|
||||
@ -167,6 +168,21 @@ public class BossAPI {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get the AutoSpawn configuration section
|
||||
* name from a AutoSpawn instance.
|
||||
*
|
||||
* @param autoSpawn - the autospawn instance
|
||||
* @return name of the skill from the SkillsFileManager or null if not found.
|
||||
*/
|
||||
public static String getAutoSpawnName(AutoSpawn autoSpawn) {
|
||||
for(Map.Entry<String, AutoSpawn> entry : PLUGIN.getAutoSpawnFileManager().getAutoSpawnMap().entrySet()) {
|
||||
if(entry.getValue().equals(autoSpawn)) return entry.getKey();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get the DropTable configuration section
|
||||
* name from a DropTable instance.
|
||||
|
@ -37,4 +37,6 @@ public class AutoSpawn {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -8,43 +8,43 @@ import java.util.List;
|
||||
* @version 1.0.0
|
||||
* @since 02-Jan-19
|
||||
*/
|
||||
public enum SpawnTypes {
|
||||
public enum SpawnType {
|
||||
|
||||
BLANK(0),
|
||||
INTERVAL(1);
|
||||
|
||||
private int rank;
|
||||
|
||||
SpawnTypes(int rank) {
|
||||
SpawnType(int rank) {
|
||||
this.rank = rank;
|
||||
}
|
||||
|
||||
public SpawnTypes getNext() {
|
||||
public SpawnType getNext() {
|
||||
return get(this.rank+1);
|
||||
}
|
||||
|
||||
public static SpawnTypes getCurrent(String input) {
|
||||
public static SpawnType getCurrent(String input) {
|
||||
if(input == null || input.isEmpty()) return BLANK;
|
||||
|
||||
for(SpawnTypes spawnTypes : values()) {
|
||||
for(SpawnType spawnTypes : values()) {
|
||||
if(spawnTypes.name().equalsIgnoreCase(input)) return spawnTypes;
|
||||
}
|
||||
|
||||
return BLANK;
|
||||
}
|
||||
|
||||
public static List<SpawnTypes> getSpawnTypes() {
|
||||
List<SpawnTypes> list = new ArrayList<>();
|
||||
public static List<SpawnType> getSpawnTypes() {
|
||||
List<SpawnType> list = new ArrayList<>();
|
||||
|
||||
for(SpawnTypes spawnTypes : values()) {
|
||||
for(SpawnType spawnTypes : values()) {
|
||||
if(spawnTypes.rank > 0) list.add(spawnTypes);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static SpawnTypes get(int rank) {
|
||||
for(SpawnTypes spawnTypes : values()) {
|
||||
private static SpawnType get(int rank) {
|
||||
for(SpawnType spawnTypes : values()) {
|
||||
if(spawnTypes.rank == rank) {
|
||||
return spawnTypes;
|
||||
}
|
@ -20,4 +20,8 @@ public class IntervalSpawnElement {
|
||||
this.spawnRate = spawnRate;
|
||||
}
|
||||
|
||||
public void attemptSpawn() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.songoda.epicbosses.holder;
|
||||
|
||||
import com.songoda.epicbosses.autospawns.AutoSpawn;
|
||||
import com.songoda.epicbosses.autospawns.SpawnType;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Charles Cullen
|
||||
* @version 1.0.0
|
||||
* @since 03-Jan-19
|
||||
*/
|
||||
public class ActiveAutoSpawnHolder {
|
||||
|
||||
@Getter private final SpawnType spawnType;
|
||||
@Getter private final AutoSpawn autoSpawn;
|
||||
|
||||
@Getter private List<ActiveBossHolder> activeBossHolders = new ArrayList<>();
|
||||
|
||||
public ActiveAutoSpawnHolder(SpawnType spawnType, AutoSpawn autoSpawn) {
|
||||
this.autoSpawn = autoSpawn;
|
||||
this.spawnType = spawnType;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.songoda.epicbosses.holder.autospawn;
|
||||
|
||||
import com.songoda.epicbosses.CustomBosses;
|
||||
import com.songoda.epicbosses.api.BossAPI;
|
||||
import com.songoda.epicbosses.autospawns.AutoSpawn;
|
||||
import com.songoda.epicbosses.autospawns.SpawnType;
|
||||
import com.songoda.epicbosses.holder.ActiveAutoSpawnHolder;
|
||||
import com.songoda.epicbosses.utils.Debug;
|
||||
import com.songoda.epicbosses.utils.ServerUtils;
|
||||
import com.songoda.epicbosses.utils.time.TimeUnit;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
/**
|
||||
* @author Charles Cullen
|
||||
* @version 1.0.0
|
||||
* @since 03-Jan-19
|
||||
*/
|
||||
public class ActiveIntervalAutoSpawnHolder extends ActiveAutoSpawnHolder {
|
||||
|
||||
@Getter private BukkitTask intervalTask = null;
|
||||
@Getter private long nextCompletedTime = 0L;
|
||||
|
||||
public ActiveIntervalAutoSpawnHolder(SpawnType spawnType, AutoSpawn autoSpawn) {
|
||||
super(spawnType, autoSpawn);
|
||||
}
|
||||
|
||||
public void restartInterval() {
|
||||
stopInterval();
|
||||
|
||||
if(getAutoSpawn().getEditing() != null && getAutoSpawn().getEditing()) return;
|
||||
|
||||
Integer delay = getAutoSpawn().getIntervalSpawnData().getSpawnRate();
|
||||
|
||||
if(delay == null) {
|
||||
Debug.AUTOSPAWN_INTERVALNOTREAL.debug("null", BossAPI.getAutoSpawnName(getAutoSpawn()));
|
||||
return;
|
||||
}
|
||||
|
||||
long currentMs = System.currentTimeMillis();
|
||||
long delayMs = (long) TimeUnit.SECONDS.to(TimeUnit.MILLISECONDS, delay);
|
||||
|
||||
this.nextCompletedTime = currentMs + delayMs;
|
||||
this.intervalTask = new BukkitRunnable() {
|
||||
private int timerTime = delay;
|
||||
|
||||
public void run() {
|
||||
this.timerTime -= 1;
|
||||
|
||||
if(this.timerTime <= 0) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}.runTaskTimer(CustomBosses.get(), 20L, 20L);
|
||||
}
|
||||
|
||||
public long getRemainingMs() {
|
||||
long currentMs = System.currentTimeMillis();
|
||||
|
||||
if(currentMs > this.nextCompletedTime) return 0;
|
||||
|
||||
return this.nextCompletedTime - currentMs;
|
||||
}
|
||||
|
||||
public void stopInterval() {
|
||||
if(this.intervalTask != null) ServerUtils.get().cancelTask(this.intervalTask);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -57,7 +57,9 @@ public enum Debug {
|
||||
SKILL_POTIONS_ARE_EMPTY("The potions list for the skill {0} is empty."),
|
||||
SKILL_CAGE_INVALID_MATERIAL("Invalid block type {0} for the skill {1}."),
|
||||
SKILL_EMPTY_SKILLS("The skills list for the {0} boss is empty."),
|
||||
SKILL_NOT_FOUND("The specified skill was not found!");
|
||||
SKILL_NOT_FOUND("The specified skill was not found!"),
|
||||
|
||||
AUTOSPAWN_INTERVALNOTREAL("The specified interval of {0} is not a valid integer for the auto spawn interval table {1}.");
|
||||
|
||||
private static CustomBosses PLUGIN;
|
||||
|
||||
|
@ -0,0 +1,107 @@
|
||||
package com.songoda.epicbosses.utils.time;
|
||||
|
||||
/**
|
||||
* @author AMinecraftDev
|
||||
* @version 1.0.0
|
||||
* @since 23-May-17
|
||||
*/
|
||||
/**
|
||||
* Makes it easy to convert
|
||||
* time
|
||||
*
|
||||
* @author Debugged
|
||||
* @version 1.0.0
|
||||
* @since 3-1-2017
|
||||
*/
|
||||
public enum TimeUnit {
|
||||
|
||||
YEARS(31556926),
|
||||
MONTHS(2629744),
|
||||
WEEKS(604800),
|
||||
DAYS(86400),
|
||||
HOURS(3600),
|
||||
MINUTES(60),
|
||||
SECONDS(1),
|
||||
MILLISECONDS(0.001);
|
||||
|
||||
private double seconds;
|
||||
|
||||
TimeUnit(double seconds) {
|
||||
this.seconds = seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a double, representing
|
||||
* the amount of time in the requested
|
||||
* TimeUnit
|
||||
*
|
||||
* @param timeUnit TimeUnit
|
||||
* @param input double
|
||||
* @return double
|
||||
*/
|
||||
public double to(TimeUnit timeUnit, double input) {
|
||||
return (input*seconds) / timeUnit.getSeconds();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the amound of seconds
|
||||
* that fit into this TimeUnit
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
public double getSeconds() {
|
||||
return seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the TimeUnit that belongs
|
||||
* to the specified String
|
||||
*
|
||||
* @param string TimeUnit
|
||||
* @return TimeUnit
|
||||
*/
|
||||
public static TimeUnit fromString(String string) {
|
||||
switch (string.toLowerCase()) {
|
||||
case "y":
|
||||
case "year":
|
||||
case "years":
|
||||
return TimeUnit.YEARS;
|
||||
case "m":
|
||||
case "month":
|
||||
case "months":
|
||||
return TimeUnit.MONTHS;
|
||||
case "w":
|
||||
case "week":
|
||||
case "weeks":
|
||||
return TimeUnit.WEEKS;
|
||||
case "d":
|
||||
case "day":
|
||||
case "days":
|
||||
return TimeUnit.DAYS;
|
||||
case "h":
|
||||
case "hour":
|
||||
case "hours":
|
||||
return TimeUnit.HOURS;
|
||||
case "mins":
|
||||
case "min":
|
||||
case "minute":
|
||||
case "minutes":
|
||||
return TimeUnit.MINUTES;
|
||||
case "s":
|
||||
case "sec":
|
||||
case "secs":
|
||||
case "second":
|
||||
case "seconds":
|
||||
return TimeUnit.SECONDS;
|
||||
case "ms":
|
||||
case "millis":
|
||||
case "millisecs":
|
||||
case "millisecond":
|
||||
case "milliseconds":
|
||||
return TimeUnit.MILLISECONDS;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,161 @@
|
||||
package com.songoda.epicbosses.utils.time;
|
||||
|
||||
public class TimeUtil {
|
||||
|
||||
/**
|
||||
* Returns the amount of time in the
|
||||
* requested TimeUnit from a formatted string
|
||||
*
|
||||
* @param string String
|
||||
* @param timeUnit TimeUnit
|
||||
* @return double
|
||||
* @throws IllegalArgumentException when the string is formatted wrongly
|
||||
*/
|
||||
public static double getTime(String string, TimeUnit timeUnit) throws IllegalArgumentException {
|
||||
double seconds = 0;
|
||||
|
||||
String timeString = string.trim().toLowerCase();
|
||||
|
||||
if(timeString.contains(" ")) {
|
||||
String[] split = timeString.split(" ");
|
||||
|
||||
for(String line : split) {
|
||||
int index = 0;
|
||||
while(isInteger(String.valueOf(line.charAt(index)))) {
|
||||
index++;
|
||||
}
|
||||
|
||||
int time = Integer.parseInt(line.substring(0, index));
|
||||
|
||||
String unit = line.substring(index+1, line.length());
|
||||
TimeUnit tempUnit = TimeUnit.fromString(unit);
|
||||
if(tempUnit == null) throw new IllegalArgumentException("Invalid time format: " + unit);
|
||||
|
||||
seconds += tempUnit.to(TimeUnit.SECONDS, time);
|
||||
}
|
||||
} else {
|
||||
int index = 0;
|
||||
int prevIndex = 0;
|
||||
while(index < timeString.length()-1) {
|
||||
double time;
|
||||
while(isInteger(String.valueOf(timeString.charAt(index)))) {
|
||||
index++;
|
||||
}
|
||||
time = Integer.parseInt(timeString.substring(prevIndex, index));
|
||||
prevIndex = index;
|
||||
|
||||
while(!isInteger(String.valueOf(timeString.charAt(index)))) {
|
||||
index++;
|
||||
if(index >= timeString.length()) break;
|
||||
}
|
||||
String unit = timeString.substring(prevIndex, index);
|
||||
TimeUnit tempUnit = TimeUnit.fromString(unit);
|
||||
if(tempUnit == null) throw new IllegalArgumentException("Invalid time format: " + unit);
|
||||
|
||||
seconds += tempUnit.to(TimeUnit.SECONDS, time);
|
||||
|
||||
prevIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
return TimeUnit.SECONDS.to(timeUnit, seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted time string
|
||||
* belonging to this amount of time
|
||||
*
|
||||
* @param unit TimeUnit
|
||||
* @param a int amount of time
|
||||
* @return String
|
||||
*/
|
||||
public static String getFormattedTime(TimeUnit unit, int a) {
|
||||
return getFormattedTime(unit, a, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted time string
|
||||
* belonging to this amount of time
|
||||
*
|
||||
* @param unit TimeUnit
|
||||
* @param a int amount of time
|
||||
* @param splitter String
|
||||
* @return String
|
||||
*/
|
||||
public static String getFormattedTime(TimeUnit unit, int a, String splitter) {
|
||||
int amount = a;
|
||||
int years = (int) unit.to(TimeUnit.YEARS, amount);
|
||||
amount -= TimeUnit.YEARS.to(unit, years);
|
||||
int months = (int) unit.to(TimeUnit.MONTHS, amount);
|
||||
amount -= TimeUnit.MONTHS.to(unit, months);
|
||||
int weeks = (int) unit.to(TimeUnit.WEEKS, amount);
|
||||
amount -= TimeUnit.WEEKS.to(unit, weeks);
|
||||
int days = (int) unit.to(TimeUnit.DAYS, amount);
|
||||
amount -= TimeUnit.DAYS.to(unit, days);
|
||||
int hours = (int) unit.to(TimeUnit.HOURS, amount);
|
||||
amount -= TimeUnit.HOURS.to(unit, hours);
|
||||
int minutes = (int) unit.to(TimeUnit.MINUTES, amount);
|
||||
amount -= TimeUnit.MINUTES.to(unit, minutes);
|
||||
int seconds = (int) unit.to(TimeUnit.SECONDS, amount);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if(splitter == null) {
|
||||
if (years > 0) {
|
||||
sb.append(years).append(" year").append(years != 1 ? "s" : "").append(", ");
|
||||
}
|
||||
if (months > 0 || sb.length() > 0) {
|
||||
sb.append(months).append(" month").append(months != 1 ? "s" : "").append(", ");
|
||||
}
|
||||
if (weeks > 0 || sb.length() > 0) {
|
||||
sb.append(weeks).append(" week").append(weeks != 1 ? "s" : "").append(", ");
|
||||
}
|
||||
if (days > 0 || sb.length() > 0) {
|
||||
sb.append(days).append(" day").append(days != 1 ? "s" : "").append(", ");
|
||||
}
|
||||
if (hours > 0 || sb.length() > 0) {
|
||||
sb.append(hours).append(" hour").append(hours != 1 ? "s" : "").append(", ");
|
||||
}
|
||||
if (minutes > 0 || sb.length() > 0) {
|
||||
sb.append(minutes).append(" minute").append(minutes != 1 ? "s" : "").append(" ");
|
||||
}
|
||||
if (seconds > 0 || sb.length() > 0) {
|
||||
sb.append(minutes > 0 ? "and " : "").append(seconds).append(" second").append(seconds != 1 ? "s" : "");
|
||||
}
|
||||
} else {
|
||||
if(years > 0) {
|
||||
sb.append(years).append(splitter);
|
||||
}
|
||||
if(months > 0 || sb.length() > 0) {
|
||||
sb.append(months).append(splitter);
|
||||
}
|
||||
if(weeks > 0 || sb.length() > 0) {
|
||||
sb.append(weeks).append(splitter);
|
||||
}
|
||||
if(days > 0 || sb.length() > 0) {
|
||||
sb.append(days).append(splitter);
|
||||
}
|
||||
if(hours > 0 || sb.length() > 0) {
|
||||
sb.append(hours).append(splitter);
|
||||
}
|
||||
if(minutes > 0 || sb.length() > 0) {
|
||||
sb.append(minutes).append(splitter);
|
||||
}
|
||||
if(seconds > 0 || sb.length() > 0) {
|
||||
sb.append(seconds);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static boolean isInteger(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
2
pom.xml
2
pom.xml
@ -20,7 +20,7 @@
|
||||
|
||||
<properties>
|
||||
<!--<plugin.version>maven-version-number-SNAPSHOT-U90</plugin.version>-->
|
||||
<plugin.version>1.0.0-U168</plugin.version>
|
||||
<plugin.version>1.0.0-U169</plugin.version>
|
||||
<plugin.name>EpicBosses</plugin.name>
|
||||
<plugin.main>com.songoda.epicbosses.CustomBosses</plugin.main>
|
||||
<plugin.author>AMinecraftDev</plugin.author>
|
||||
|
Loading…
Reference in New Issue
Block a user