diff --git a/src/main/java/org/shanerx/tradeshop/TradeShop.java b/src/main/java/org/shanerx/tradeshop/TradeShop.java index 8f87aaf5..b14be19d 100644 --- a/src/main/java/org/shanerx/tradeshop/TradeShop.java +++ b/src/main/java/org/shanerx/tradeshop/TradeShop.java @@ -44,12 +44,14 @@ import org.shanerx.tradeshop.objects.Debug; import org.shanerx.tradeshop.objects.ListManager; import org.shanerx.tradeshop.utils.BukkitVersion; +import org.shanerx.tradeshop.utils.Expirer; import org.shanerx.tradeshop.utils.Updater; import org.shanerx.tradeshop.utils.data.DataStorage; import org.shanerx.tradeshop.utils.data.DataType; public class TradeShop extends JavaPlugin { + private Expirer expirer = new Expirer(this); private final NamespacedKey storageKey = new NamespacedKey(this, "tradeshop-storage-data"); private final NamespacedKey signKey = new NamespacedKey(this, "tradeshop-sign-data"); @@ -70,6 +72,10 @@ public class TradeShop extends JavaPlugin { @Override public void onEnable() { + if (!expirer.initiateDevExpiration()) { + expirer = null; + } + version = new BukkitVersion(); if (version.isBelow(1, 9)) { diff --git a/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java b/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java index de09f81f..200aaf48 100644 --- a/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java +++ b/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java @@ -29,6 +29,7 @@ public enum DebugLevels { + DEV_EXPIRATION(-2, Level.SEVERE), DATA_ERROR(-1, Level.SEVERE), DISABLED(0, Level.INFO), // 0 STARTUP(1, Level.INFO), // 1 diff --git a/src/main/java/org/shanerx/tradeshop/objects/Debug.java b/src/main/java/org/shanerx/tradeshop/objects/Debug.java index d86ca222..9e756707 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/Debug.java +++ b/src/main/java/org/shanerx/tradeshop/objects/Debug.java @@ -69,4 +69,8 @@ public void log(String message, DebugLevels level) { Bukkit.getLogger().log(level.getLogLevel(), PREFIX.replace("%level%", level.getPrefix()) + message); } } + + public String getFormattedPrefix(DebugLevels debugLevel) { + return PREFIX.replace("%level%", debugLevel.getPrefix()); + } } \ No newline at end of file diff --git a/src/main/java/org/shanerx/tradeshop/utils/Expirer.java b/src/main/java/org/shanerx/tradeshop/utils/Expirer.java new file mode 100644 index 00000000..c8bd0203 --- /dev/null +++ b/src/main/java/org/shanerx/tradeshop/utils/Expirer.java @@ -0,0 +1,133 @@ +/* + * + * Copyright (c) 2016-2019 + * SparklingComet @ http://shanerx.org + * KillerOfPie @ http://killerofpie.github.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * NOTICE: All modifications made by others to the source code belong + * to the respective contributor. No contributor should be held liable for + * any damages of any kind, whether be material or moral, which were + * caused by their contribution(s) to the project. See the full License for more information. + * + */ + +package org.shanerx.tradeshop.utils; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.scheduler.BukkitScheduler; +import org.shanerx.tradeshop.TradeShop; +import org.shanerx.tradeshop.enumys.DebugLevels; +import org.shanerx.tradeshop.enumys.Permissions; + +import java.io.InputStreamReader; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.chrono.ChronoLocalDateTime; + +public class Expirer { + private final TradeShop plugin; + + public Expirer(TradeShop plugin) { + this.plugin = plugin; + } + + public boolean initiateDevExpiration() { + if (!plugin.getDescription().getVersion().toUpperCase().endsWith("DEV") || plugin.getResource("builddate.yml") == null) + return false; + + YamlConfiguration buildDateFile = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("builddate.yml"))); + + LocalDateTime buildTime = LocalDateTime.parse(buildDateFile.getString("buildtime").replace(".", ":")); + BukkitScheduler scheduler = plugin.getServer().getScheduler(); + + final int devExpirationDays = 60, disableDayMessages = 25, expiredMessagesPerDay = 10; + final long postExpirationRunTimeInTicks = 1728000, ticksPerDay = 1728000; + + + if (buildTime.plusDays(devExpirationDays).isBefore(ChronoLocalDateTime.from(LocalDateTime.now()))) { + + // Sends a repeating message notifying of updates + scheduler.runTaskTimerAsynchronously(plugin, new Runnable() { + @Override + public void run() { + plugin.getDebugger().log("You are currently running a DEV version that is past its expiration date. The plugin will disable soon!", DebugLevels.DEV_EXPIRATION); + plugin.getDebugger().log("Please update IMMEDIATELY to a newer DEV version for testing or BETA/RELEASE if on a live server.", DebugLevels.DEV_EXPIRATION); + } + }, 5L, postExpirationRunTimeInTicks / disableDayMessages); + + // Sends a messages when 90% of the run time is up + scheduler.runTaskLaterAsynchronously(plugin, new Runnable() { + @Override + public void run() { + // Calculates then amount of seconds of 10% of the total time + long remainingTime = (long) (postExpirationRunTimeInTicks * 0.1 / 20); + + plugin.getDebugger().log("Post expiration use time will expire and the plugin will be disabled in: " + formatSeconds(remainingTime), DebugLevels.DEV_EXPIRATION); + plugin.getServer().broadcast(colorize(plugin.getDebugger().getFormattedPrefix(DebugLevels.DEV_EXPIRATION) + "&4Post expiration use time will expire and the plugin will be disabled in: " + formatSeconds(remainingTime)), Permissions.ADMIN.getPerm().toString()); + } + }, (long) (postExpirationRunTimeInTicks * 0.9)); + + // Sends a messages when 95% of the run time is up + scheduler.runTaskLaterAsynchronously(plugin, new Runnable() { + @Override + public void run() { + // Calculates then amount of seconds of 5% of the total time + long remainingTime = (long) (postExpirationRunTimeInTicks * 0.05 / 20); + + plugin.getDebugger().log("Post expiration use time will expire and the plugin will be disabled in: " + formatSeconds(remainingTime), DebugLevels.DEV_EXPIRATION); + plugin.getServer().broadcast(colorize(plugin.getDebugger().getFormattedPrefix(DebugLevels.DEV_EXPIRATION) + "&4Post expiration use time will expire and the plugin will be disabled in: " + formatSeconds(remainingTime)), Permissions.ADMIN.getPerm().toString()); + } + }, (long) (postExpirationRunTimeInTicks * 0.95)); + + // Disables the plugin when time is up + scheduler.runTaskLaterAsynchronously(plugin, new Runnable() { + @Override + public void run() { + plugin.getDebugger().log("Post expiration use time has Expired! The plugin will now disable.", DebugLevels.DEV_EXPIRATION); + plugin.getServer().broadcast(colorize(plugin.getDebugger().getFormattedPrefix(DebugLevels.DEV_EXPIRATION) + "&4Post expiration use time has Expired! The plugin will now disable."), Permissions.ADMIN.getPerm().toString()); + plugin.getServer().getPluginManager().disablePlugin(plugin); + } + }, postExpirationRunTimeInTicks); + + return true; + + // Messages start after half of the expiration time + } else if (buildTime.plusDays(devExpirationDays / 2).isBefore(ChronoLocalDateTime.from(LocalDateTime.now()))) { + // Sends a repeating message notifying of updates + scheduler.runTaskTimerAsynchronously(plugin, new Runnable() { + @Override + public void run() { + plugin.getDebugger().log("You are currently running a DEV version that is past its expiration date.", DebugLevels.DEV_EXPIRATION); + plugin.getDebugger().log("Please update to a newer DEV version for testing or BETA/RELEASE if on a live server.", DebugLevels.DEV_EXPIRATION); + } + }, 5L, ticksPerDay / expiredMessagesPerDay); + + return true; + } + + return false; + } + + public String colorize(String msg) { + return ChatColor.translateAlternateColorCodes('&', msg); + } + + public String formatSeconds(long seconds) { + Duration duration = Duration.ofSeconds(seconds); + + return String.format("%02d:%02d:%02d", duration.toHours(), duration.toMinutes() % 60, seconds % 60); + } +}