Completely Reformatted GracePeriod with a timer now

This commit is contained in:
Driftay
2020-04-07 07:59:18 -04:00
parent 8fb697888f
commit d4ac0ab310
20 changed files with 531 additions and 226 deletions

View File

@@ -5,6 +5,7 @@ import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.tag.FactionTag;
import com.massivecraft.factions.tag.Tag;
import com.massivecraft.factions.util.timer.TimerManager;
import com.massivecraft.factions.zcore.util.TL;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational;
@@ -209,8 +210,8 @@ public class ClipPlaceholderAPIManager extends PlaceholderExpansion implements R
return String.valueOf(faction.getMaxVaults());
case "faction_relation_color":
return fPlayer.getColorTo(faction).toString();
case "faction_grace":
return Conf.gracePeriod ? "Enabled" : "Disabled";
case "faction_grace_time":
return String.valueOf(TimerManager.getRemaining(FactionsPlugin.getInstance().getTimerManager().graceTimer.getRemaining(), true));
case "faction_name_at_location":
Faction factionAtLocation = Board.getInstance().getFactionAt(new FLocation(player.getLocation()));
return factionAtLocation != null ? factionAtLocation.getTag() : Factions.getInstance().getWilderness().getTag();

View File

@@ -0,0 +1,82 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.FactionsPlugin;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.Objects;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public class Config extends YamlConfiguration {
private String fileName;
private FactionsPlugin plugin;
public Config(FactionsPlugin plugin, String fileName) {
this(plugin, fileName, ".yml");
}
public Config(FactionsPlugin plugin, String fileName, String fileExtension) {
this.plugin = plugin;
this.fileName = fileName + (fileName.endsWith(fileExtension) ? "" : fileExtension);
this.createFile();
}
public String getFileName() {
return this.fileName;
}
public FactionsPlugin getPlugin() {
return this.plugin;
}
private void createFile() {
File folder = this.plugin.getDataFolder();
try {
File ex = new File(folder, this.fileName);
if (!ex.exists()) {
if (this.plugin.getResource(this.fileName) != null) {
this.plugin.saveResource(this.fileName, false);
} else {
this.save(ex);
}
} else {
this.load(ex);
this.save(ex);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void save() {
File folder = this.plugin.getDataFolder();
try {
this.save(new File(folder, this.fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Config)) {
return false;
}
Config config = (Config) o;
if (this.fileName != null) {
if (this.fileName.equals(config.fileName)) {
return Objects.equals(this.plugin, config.plugin);
}
} else if (config.fileName == null) {
return Objects.equals(this.plugin, config.plugin);
}
return false;
}
}

View File

@@ -0,0 +1,29 @@
package com.massivecraft.factions.util.timer;
import com.massivecraft.factions.Conf;
import org.apache.commons.lang.time.FastDateFormat;
import java.text.DecimalFormat;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public final class DateTimeFormats {
public static final TimeZone SERVER_TIME_ZONE = TimeZone.getTimeZone(Conf.serverTimeZone);
public static final ZoneId SERVER_ZONE_ID = SERVER_TIME_ZONE.toZoneId();
public static final FastDateFormat DAY_MTH_HR_MIN_SECS = FastDateFormat.getInstance("dd/MM HH:mm:ss", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat DAY_MTH_YR_HR_MIN_AMPM = FastDateFormat.getInstance("dd/MM/yy hh:mma", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat DAY_MTH_HR_MIN_AMPM = FastDateFormat.getInstance("dd/MM hh:mma", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat HR_MIN_AMPM = FastDateFormat.getInstance("hh:mma", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat HR_MIN_AMPM_TIMEZONE = FastDateFormat.getInstance("hh:mma z", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat HR_MIN = FastDateFormat.getInstance("hh:mm", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final FastDateFormat MIN_SECS = FastDateFormat.getInstance("mm:ss", SERVER_TIME_ZONE, Locale.ENGLISH);
public static final ThreadLocal<DecimalFormat> SECONDS = ThreadLocal.withInitial(() -> new DecimalFormat("0"));
public static final ThreadLocal<DecimalFormat> REMAINING_SECONDS = ThreadLocal.withInitial(() -> new DecimalFormat("0.#"));
public static final ThreadLocal<DecimalFormat> REMAINING_SECONDS_TRAILING = ThreadLocal.withInitial(() -> new DecimalFormat("0.0"));
}

View File

@@ -0,0 +1,66 @@
package com.massivecraft.factions.util.timer;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public abstract class GlobalTimer extends Timer {
private TimerRunnable runnable;
public GlobalTimer(String name, long defaultCooldown) {
super(name, defaultCooldown);
}
public boolean clearCooldown() {
if (this.runnable != null) {
this.runnable.cancel();
this.runnable = null;
return true;
}
return false;
}
public boolean isPaused() {
return (this.runnable != null) && (this.runnable.isPaused());
}
public void setPaused(boolean paused) {
if ((this.runnable != null) && (this.runnable.isPaused() != paused)) {
this.runnable.setPaused(paused);
}
}
public long getRemaining() {
return this.runnable == null ? 0L : this.runnable.getRemaining();
}
public long getRemaining(long now) {
return this.runnable == null ? 0L : this.runnable.getRemaining(now);
}
public boolean setRemaining() {
return setRemaining(this.defaultCooldown, false);
}
public boolean setRemaining(long duration, boolean overwrite) {
boolean hadCooldown = false;
if (this.runnable != null) {
if (!overwrite) {
return false;
}
hadCooldown = this.runnable.getRemaining() > 0L;
this.runnable.setRemaining(duration);
} else {
this.runnable = new TimerRunnable(this, duration);
}
return !hadCooldown;
}
}

View File

@@ -0,0 +1,32 @@
package com.massivecraft.factions.util.timer;
import com.massivecraft.factions.FactionsPlugin;
import com.massivecraft.factions.util.Config;
import com.massivecraft.factions.util.timer.type.GraceTimer;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public abstract class Timer {
protected final String name;
public final long defaultCooldown;
public Timer(String name, long defaultCooldown) {
this.name = name;
this.defaultCooldown = defaultCooldown;
}
public String getName() {
return this.name;
}
public void load(Config config) {
}
public void save(Config config) {
}
}

View File

@@ -0,0 +1,86 @@
package com.massivecraft.factions.util.timer;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FactionsPlugin;
import com.massivecraft.factions.util.Config;
import com.massivecraft.factions.util.timer.type.GraceTimer;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.bukkit.event.Listener;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public class TimerManager implements Listener, Runnable {
private final Set<Timer> timers;
private final FactionsPlugin plugin;
private final List<TimerRunnable> timerRunnableList = new ArrayList<>();
private Config config;
public GraceTimer graceTimer;
private static final long MINUTE = TimeUnit.MINUTES.toMillis(1L);
private static final long HOUR = TimeUnit.HOURS.toMillis(1L);
private static final long MULTI_HOUR = TimeUnit.HOURS.toMillis(10);
public static String getRemaining(long millis, boolean milliseconds) {
return getRemaining(millis, milliseconds, true);
}
public static String getRemaining(long duration, boolean milliseconds, boolean trail) {
if ((milliseconds) && (duration < MINUTE)) {
return ( (trail ? DateTimeFormats.REMAINING_SECONDS_TRAILING : DateTimeFormats.REMAINING_SECONDS).get()).format(duration * 0.001D) + 's';
}
return DurationFormatUtils.formatDuration(duration, (duration >= HOUR ? (duration >= MULTI_HOUR ? "d" : "") + "d:" : "") + "HH:mm:ss");
}
public TimerManager(FactionsPlugin plugin) {
this.timers = new HashSet<>();
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
if(Conf.useGraceSystem) {
this.registerTimer(this.graceTimer = new GraceTimer());
}
plugin.getServer().getScheduler().runTaskTimer(plugin, this, 4, 4);
}
public Collection<Timer> getTimers() {
return this.timers;
}
public void registerTimer(Timer timer) {
this.timers.add(timer);
if (timer instanceof Listener) {
this.plugin.getServer().getPluginManager().registerEvents((Listener) timer, this.plugin);
}
}
public void unregisterTimer(Timer timer) {
this.timers.remove(timer);
}
public void reloadTimerData() {
this.config = new Config(this.plugin, "timers");
for (Timer timer : this.timers) {
timer.load(this.config);
}
}
public void saveTimerData() {
for (Timer timer : this.timers) {
timer.save(this.config);
}
this.config.save();
}
public void run() {
long now = System.currentTimeMillis();
timerRunnableList.removeIf(next -> next.check(now));
}
public List<TimerRunnable> getTimerRunnableList() {
return timerRunnableList;
}
}

View File

@@ -0,0 +1,118 @@
package com.massivecraft.factions.util.timer;
import com.massivecraft.factions.FactionsPlugin;
import com.massivecraft.factions.util.Config;
import java.util.UUID;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public class TimerRunnable {
private final Timer timer;
private long expiryMillis;
private long pauseMillis;
private boolean cancelled = false;
public TimerRunnable(Timer timer, long duration) {
this.timer = timer;
setRemaining(duration);
FactionsPlugin plugin = FactionsPlugin.getInstance();
plugin.getTimerManager().getTimerRunnableList().add(this);
}
public TimerRunnable(UUID playerUUID, Timer timer, long duration) {
this.timer = timer;
setRemaining(duration);
FactionsPlugin plugin = FactionsPlugin.getInstance();
plugin.getTimerManager().getTimerRunnableList().add(this);
}
public boolean isCancelled() {
return cancelled;
}
public Timer getTimer() {
return this.timer;
}
public long getRemaining() {
return getRemaining(false);
}
public long getRemaining(long now) {
return getRemaining(false, now);
}
public void setRemaining(long remaining) {
setExpiryMillis(remaining);
}
public long getRemaining(boolean ignorePaused) {
if ((!ignorePaused) && (this.pauseMillis != 0L)) return this.pauseMillis;
return this.expiryMillis - System.currentTimeMillis();
}
public long getRemaining(boolean ignorePaused, long now) {
if ((!ignorePaused) && (this.pauseMillis != 0L)) return this.pauseMillis;
return this.expiryMillis - now;
}
public long getExpiryMillis() {
return this.expiryMillis;
}
private void setExpiryMillis(long remainingMillis) {
long expiryMillis = System.currentTimeMillis() + remainingMillis;
if (expiryMillis == this.expiryMillis) return;
this.expiryMillis = expiryMillis;
}
public boolean check(long now) {
if (cancelled) return true;
return getRemaining(false, now) <= 0;
}
public long getPauseMillis() {
return this.pauseMillis;
}
public void setPauseMillis(long pauseMillis) {
this.pauseMillis = pauseMillis;
}
public boolean isPaused() {
return this.pauseMillis != 0L;
}
public void setPaused(boolean paused) {
if (paused == isPaused()) return;
if (paused) {
this.pauseMillis = getRemaining(true);
cancel();
} else {
setExpiryMillis(this.pauseMillis);
this.pauseMillis = 0L;
}
}
public void cancel() {
cancelled = true;
}
}

View File

@@ -0,0 +1,43 @@
package com.massivecraft.factions.util.timer.type;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.util.timer.GlobalTimer;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.concurrent.TimeUnit;
/**
* Factions - Developed by Driftay.
* All rights reserved 2020.
* Creation Date: 4/7/2020
*/
public class GraceTimer extends GlobalTimer implements Listener {
public GraceTimer() {
super("GRACE", TimeUnit.DAYS.toMillis(Conf.gracePeriodTimeDays));
}
@EventHandler
public void onBreak(EntityExplodeEvent e) {
if(getRemaining() > 0)
e.setCancelled(true);
}
@EventHandler
public void onTNTPlace(BlockPlaceEvent event) {
FPlayer fp = FPlayers.getInstance().getByPlayer(event.getPlayer());
if(getRemaining() > 0) {
if (!fp.isAdminBypassing()) {
if (event.getBlock().getType().equals(Material.TNT)) {
event.setCancelled(true);
}
}
}
}
}