Use TimeUnit for time calculations

This commit is contained in:
P0nk
2021-09-10 18:56:03 +02:00
parent d52aedac4f
commit cdc17ef3dd
49 changed files with 268 additions and 161 deletions

View File

@@ -93,6 +93,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.regex.Pattern;
import static java.util.concurrent.TimeUnit.*;
public class Character extends AbstractCharacterObject {
private static final ItemInformationProvider ii = ItemInformationProvider.getInstance();
private static final String LEVEL_200 = "[Congrats] %s has reached Level %d! Congratulate %s on such an amazing achievement!";
@@ -1255,7 +1257,7 @@ public class Character extends AbstractCharacterObject {
}
public boolean canRecoverLastBanish() {
return System.currentTimeMillis() - this.banishTime < 5 * 60 * 1000;
return System.currentTimeMillis() - this.banishTime < MINUTES.toMillis(5);
}
public Pair<Integer, Integer> getLastBanishData() {
@@ -2082,7 +2084,7 @@ public class Character extends AbstractCharacterObject {
Skill battleship = SkillFactory.getSkill(Corsair.BATTLE_SHIP);
int cooldown = battleship.getEffect(getSkillLevel(battleship)).getCooldown();
sendPacket(PacketCreator.skillCooldown(Corsair.BATTLE_SHIP, cooldown));
addCooldown(Corsair.BATTLE_SHIP, Server.getInstance().getCurrentTime(), cooldown * 1000);
addCooldown(Corsair.BATTLE_SHIP, Server.getInstance().getCurrentTime(), SECONDS.toMillis(cooldown));
removeCooldown(5221999);
cancelEffectFromBuffStat(BuffStat.MONSTER_RIDING);
} else {
@@ -2369,7 +2371,7 @@ public class Character extends AbstractCharacterObject {
private static Pair<Integer, Pair<Integer, Integer>> getChairTaskIntervalRate(int maxhp, int maxmp) {
float toHeal = Math.max(maxhp, maxmp);
float maxDuration = YamlConfig.config.server.CHAIR_EXTRA_HEAL_MAX_DELAY * 1000;
float maxDuration = SECONDS.toMillis(YamlConfig.config.server.CHAIR_EXTRA_HEAL_MAX_DELAY);
int rate = 0;
int minRegen = 1, maxRegen = (256 * YamlConfig.config.server.CHAIR_EXTRA_HEAL_MULTIPLIER) - 1, midRegen = 1;
@@ -4394,7 +4396,7 @@ public class Character extends AbstractCharacterObject {
int bHealingLvl = getSkillLevel(bHealing);
if (bHealingLvl > 0) {
final StatEffect healEffect = bHealing.getEffect(bHealingLvl);
int healInterval = healEffect.getX() * 1000;
int healInterval = (int) SECONDS.toMillis(healEffect.getX());
beholderHealingSchedule = TimerManager.getInstance().register(new Runnable() {
@Override
public void run() {
@@ -4412,7 +4414,7 @@ public class Character extends AbstractCharacterObject {
Skill bBuff = SkillFactory.getSkill(DarkKnight.HEX_OF_BEHOLDER);
if (getSkillLevel(bBuff) > 0) {
final StatEffect buffEffect = bBuff.getEffect(getSkillLevel(bBuff));
int buffInterval = buffEffect.getX() * 1000;
int buffInterval = (int) SECONDS.toMillis(buffEffect.getX());
beholderBuffSchedule = TimerManager.getInstance().register(new Runnable() {
@Override
public void run() {
@@ -6065,7 +6067,7 @@ public class Character extends AbstractCharacterObject {
}
private long getNextBuybackTime() {
return lastBuyback + YamlConfig.config.server.BUYBACK_COOLDOWN_MINUTES * 60 * 1000;
return lastBuyback + MINUTES.toMillis(YamlConfig.config.server.BUYBACK_COOLDOWN_MINUTES);
}
private boolean isBuybackInvincible() {
@@ -6091,12 +6093,12 @@ public class Character extends AbstractCharacterObject {
boolean avail = true;
if (!isAlive()) {
long timeLapsed = timeNow - lastDeathtime;
long timeRemaining = YamlConfig.config.server.BUYBACK_RETURN_MINUTES * 60 * 1000 - (timeLapsed + Math.max(0, getNextBuybackTime() - timeNow));
long timeRemaining = MINUTES.toMillis(YamlConfig.config.server.BUYBACK_RETURN_MINUTES) - (timeLapsed + Math.max(0, getNextBuybackTime() - timeNow));
if (timeRemaining < 1) {
s += "Buyback #e#rUNAVAILABLE#k#n";
avail = false;
} else {
s += "Buyback countdown: #e#b" + getTimeRemaining(YamlConfig.config.server.BUYBACK_RETURN_MINUTES * 60 * 1000 - timeLapsed) + "#k#n";
s += "Buyback countdown: #e#b" + getTimeRemaining(MINUTES.toMillis(YamlConfig.config.server.BUYBACK_RETURN_MINUTES) - timeLapsed) + "#k#n";
}
s += "\r\n";
}
@@ -6112,8 +6114,8 @@ public class Character extends AbstractCharacterObject {
}
private static String getTimeRemaining(long timeLeft) {
int seconds = (int) Math.floor(timeLeft / 1000) % 60;
int minutes = (int) Math.floor(timeLeft / (1000 * 60)) % 60;
int seconds = (int) Math.floor(timeLeft / SECONDS.toMillis(1)) % 60;
int minutes = (int) Math.floor(timeLeft / MINUTES.toMillis(1)) % 60;
return (minutes > 0 ? (String.format("%02d", minutes) + " minutes, ") : "") + String.format("%02d", seconds) + " seconds";
}
@@ -6121,7 +6123,7 @@ public class Character extends AbstractCharacterObject {
public boolean couldBuyback() { // Ronan's buyback system
long timeNow = Server.getInstance().getCurrentTime();
if (timeNow - lastDeathtime > YamlConfig.config.server.BUYBACK_RETURN_MINUTES * 60 * 1000) {
if (timeNow - lastDeathtime > MINUTES.toMillis(YamlConfig.config.server.BUYBACK_RETURN_MINUTES)) {
this.dropMessage(5, "The period of time to decide has expired, therefore you are unable to buyback.");
return false;
}
@@ -7247,7 +7249,7 @@ public class Character extends AbstractCharacterObject {
QuestStatus status = new QuestStatus(q, QuestStatus.Status.getById(rs.getInt("status")));
long cTime = rs.getLong("time");
if (cTime > -1) {
status.setCompletionTime(cTime * 1000);
status.setCompletionTime(SECONDS.toMillis(cTime));
}
long eTime = rs.getLong("expires");
@@ -9618,7 +9620,7 @@ public class Character extends AbstractCharacterObject {
public void showUnderleveledInfo(Monster mob) {
long curTime = Server.getInstance().getCurrentTime();
if (nextWarningTime < curTime) {
nextWarningTime = curTime + (60 * 1000); // show underlevel info again after 1 minute
nextWarningTime = curTime + MINUTES.toMillis(1); // show underlevel info again after 1 minute
showHint("You have gained #rno experience#k from defeating #e#b" + mob.getName() + "#k#n (lv. #b" + mob.getLevel() + "#k)! Take note you must have around the same level as the mob to start earning EXP from it.");
}
@@ -9627,7 +9629,7 @@ public class Character extends AbstractCharacterObject {
public void showMapOwnershipInfo(Character mapOwner) {
long curTime = Server.getInstance().getCurrentTime();
if (nextWarningTime < curTime) {
nextWarningTime = curTime + (60 * 1000); // show underlevel info again after 1 minute
nextWarningTime = curTime + MINUTES.toMillis(1); // show underlevel info again after 1 minute
String medal = "";
Item medalItem = mapOwner.getInventory(InventoryType.EQUIPPED).getItem((short) -49);
@@ -9980,7 +9982,7 @@ public class Character extends AbstractCharacterObject {
public void run() {
runQuestExpireTask();
}
}, 10 * 1000);
}, SECONDS.toMillis(10));
}
}
} finally {
@@ -10025,7 +10027,7 @@ public class Character extends AbstractCharacterObject {
public void run() {
runQuestExpireTask();
}
}, 10 * 1000);
}, SECONDS.toMillis(10));
}
questExpirations.put(quest, Server.getInstance().getCurrentTime() + time);
@@ -10035,8 +10037,8 @@ public class Character extends AbstractCharacterObject {
}
public void questTimeLimit(final Quest quest, int seconds) {
registerQuestExpire(quest, seconds * 1000);
sendPacket(PacketCreator.addQuestTimeLimit(quest.getId(), seconds * 1000));
registerQuestExpire(quest, SECONDS.toMillis(seconds));
sendPacket(PacketCreator.addQuestTimeLimit(quest.getId(), (int) SECONDS.toMillis(seconds)));
}
public void questTimeLimit2(final Quest quest, long expires) {
@@ -10550,7 +10552,7 @@ public class Character extends AbstractCharacterObject {
}
inventory = null;
}
}, 5 * 60 * 1000);
}, MINUTES.toMillis(5));
}
}
@@ -10920,7 +10922,7 @@ public class Character extends AbstractCharacterObject {
public int checkWorldTransferEligibility() {
if (getLevel() < 20) {
return 2;
} else if (getClient().getTempBanCalendar() != null && getClient().getTempBanCalendar().getTimeInMillis() + (30 * 24 * 60 * 60 * 1000) < Calendar.getInstance().getTimeInMillis()) {
} else if (getClient().getTempBanCalendar() != null && getClient().getTempBanCalendar().getTimeInMillis() + (int) DAYS.toMillis(30) < Calendar.getInstance().getTimeInMillis()) {
return 3;
} else if (isMarried()) {
return 4;

View File

@@ -75,9 +75,10 @@ import java.sql.*;
import java.util.Date;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Client extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(Client.class);
@@ -1151,7 +1152,7 @@ public class Client extends ChannelInboundHandlerAdapter {
} catch (NullPointerException e) {
e.printStackTrace();
}
}, TimeUnit.SECONDS.toMillis(15));
}, SECONDS.toMillis(15));
}
public Set<String> getMacs() {
@@ -1421,7 +1422,7 @@ public class Client extends ChannelInboundHandlerAdapter {
int targetHash = player.getTargetHpBarHash();
if (mobHash != targetHash) {
if (timeNow - player.getTargetHpBarTime() >= 5 * 1000) {
if (timeNow - player.getTargetHpBarTime() >= SECONDS.toMillis(5)) {
// is there a way to INTERRUPT this annoying thread running on the client that drops the boss bar after some time at every attack?
announceDisableServerMessage();
sendPacket(packet);

View File

@@ -29,6 +29,9 @@ import net.server.Server;
import tools.FilePrinter;
import tools.PacketCreator;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* @author kevintjuh93
*/
@@ -36,22 +39,22 @@ public enum AutobanFactory {
MOB_COUNT,
GENERAL,
FIX_DAMAGE,
DAMAGE_HACK(15, 60 * 1000),
DISTANCE_HACK(10, 120 * 1000),
PORTAL_DISTANCE(5, 30000),
DAMAGE_HACK(15, MINUTES.toMillis(1)),
DISTANCE_HACK(10, MINUTES.toMillis(2)),
PORTAL_DISTANCE(5, SECONDS.toMillis(30)),
PACKET_EDIT,
ACC_HACK,
CREATION_GENERATOR,
HIGH_HP_HEALING,
FAST_HP_HEALING(15),
FAST_MP_HEALING(20, 30000),
FAST_MP_HEALING(20, SECONDS.toMillis(30)),
GACHA_EXP,
TUBI(20, 15000),
TUBI(20, SECONDS.toMillis(15)),
SHORT_ITEM_VAC,
ITEM_VAC,
FAST_ITEM_PICKUP(5, 30000),
FAST_ATTACK(10, 30000),
MPCON(25, 30000);
FAST_ITEM_PICKUP(5, SECONDS.toMillis(30)),
FAST_ATTACK(10, SECONDS.toMillis(30)),
MPCON(25, SECONDS.toMillis(30));
private final int points;
private final long expiretime;
@@ -83,7 +86,7 @@ public enum AutobanFactory {
}
public void alert(Character chr, String reason) {
if (YamlConfig.config.server.USE_AUTOBAN == true) {
if (YamlConfig.config.server.USE_AUTOBAN) {
if (chr != null && MapleLogger.ignored.contains(chr.getId())) {
return;
}
@@ -95,7 +98,7 @@ public enum AutobanFactory {
}
public void autoban(Character chr, String value) {
if (YamlConfig.config.server.USE_AUTOBAN == true) {
if (YamlConfig.config.server.USE_AUTOBAN) {
chr.autoban("Autobanned for (" + this.name() + ": " + value + ")");
//chr.sendPolice("You will be disconnected for (" + this.name() + ": " + value + ")");
}

View File

@@ -27,6 +27,8 @@ import client.Client;
import client.command.Command;
import net.server.Server;
import static java.util.concurrent.TimeUnit.*;
public class UptimeCommand extends Command {
{
setDescription("Show server online time.");
@@ -35,10 +37,10 @@ public class UptimeCommand extends Command {
@Override
public void execute(Client c, String[] params) {
long milliseconds = System.currentTimeMillis() - Server.uptime;
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
int days = (int) ((milliseconds / (1000 * 60 * 60 * 24)));
int seconds = (int) (milliseconds / SECONDS.toMillis(1)) % 60;
int minutes = (int) ((milliseconds / MINUTES.toMillis(1)) % 60);
int hours = (int) ((milliseconds / HOURS.toMillis(1)) % 24);
int days = (int) ((milliseconds / DAYS.toMillis(1)));
c.getPlayer().yellowMessage("Server has been online for " + days + " days " + hours + " hours " + minutes + " minutes and " + seconds + " seconds.");
}
}

View File

@@ -32,6 +32,8 @@ import config.YamlConfig;
import constants.inventory.ItemConstants;
import server.ItemInformationProvider;
import static java.util.concurrent.TimeUnit.DAYS;
public class ItemCommand extends Command {
{
setDescription("Spawn an item into your inventory.");
@@ -68,7 +70,7 @@ public class ItemCommand extends Command {
if (params.length >= 2) { // thanks to istreety & TacoBell
quantity = 1;
long days = Math.max(1, Integer.parseInt(params[1]));
long expiration = System.currentTimeMillis() + (days * 24 * 60 * 60 * 1000);
long expiration = System.currentTimeMillis() + DAYS.toMillis(days);
int petid = Pet.createPet(itemId);
InventoryManipulator.addById(c, itemId, quantity, player.getName(), petid, expiration);

View File

@@ -33,6 +33,8 @@ import config.YamlConfig;
import constants.inventory.ItemConstants;
import server.ItemInformationProvider;
import static java.util.concurrent.TimeUnit.DAYS;
public class ItemDropCommand extends Command {
{
setDescription("Spawn an item onto the ground.");
@@ -69,7 +71,7 @@ public class ItemDropCommand extends Command {
if (params.length >= 2) { // thanks to istreety & TacoBell
quantity = 1;
long days = Math.max(1, Integer.parseInt(params[1]));
long expiration = System.currentTimeMillis() + (days * 24 * 60 * 60 * 1000);
long expiration = System.currentTimeMillis() + DAYS.toMillis(days);
int petid = Pet.createPet(itemId);
Item toDrop = new Item(itemId, (short) 0, quantity, petid);

View File

@@ -29,6 +29,8 @@ import client.command.Command;
import server.maps.MapleMap;
import server.maps.Portal;
import static java.util.concurrent.TimeUnit.MINUTES;
public class JailCommand extends Command {
{
setDescription("Move a player to the jail.");
@@ -53,7 +55,7 @@ public class JailCommand extends Command {
Character victim = c.getWorldServer().getPlayerStorage().getCharacterByName(params[0]);
if (victim != null) {
victim.addJailExpirationTime(minutesJailed * 60 * 1000);
victim.addJailExpirationTime(MINUTES.toMillis(minutesJailed));
int mapid = 300000012;

View File

@@ -30,6 +30,8 @@ import net.server.Server;
import net.server.world.World;
import server.TimerManager;
import static java.util.concurrent.TimeUnit.*;
public class ShutdownCommand extends Command {
{
setDescription("Shut down the server.");
@@ -51,10 +53,10 @@ public class ShutdownCommand extends Command {
}
if (time > 1) {
int seconds = (time / 1000) % 60;
int minutes = (time / (1000 * 60)) % 60;
int hours = (time / (1000 * 60 * 60)) % 24;
int days = (time / (1000 * 60 * 60 * 24));
int seconds = (time / (int) SECONDS.toMillis(1)) % 60;
int minutes = (time / (int) MINUTES.toMillis(1)) % 60;
int hours = (time / (int) HOURS.toMillis(1)) % 24;
int days = (time / (int) DAYS.toMillis(1));
String strTime = "";
if (days > 0) {

View File

@@ -33,6 +33,8 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.HOURS;
/**
* @author Ronan - credits to Eric for showing the New Year opcodes and handler layout
*/
@@ -262,7 +264,7 @@ public class NewYearCardRecord {
if (target != null && target.isLoggedinWorld()) {
target.sendPacket(PacketCreator.onNewYearCardRes(target, NewYearCardRecord.this, 0xC, 0));
}
}, 1000 * 60 * 60); //1 Hour
}, HOURS.toMillis(1));
}
public void stopNewYearCardTask() {

View File

@@ -43,6 +43,7 @@ import java.sql.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author RonanLana - synchronization of Fredrick modules and operation results
@@ -80,7 +81,7 @@ public class FredrickProcessor {
}
public static int timestampElapsedDays(Timestamp then, long timeNow) {
return (int) ((timeNow - then.getTime()) / (1000 * 60 * 60 * 24));
return (int) ((timeNow - then.getTime()) / TimeUnit.DAYS.toMillis(1));
}
private static String fredrickReminderMessage(int daynotes) {