From 7e087187918d0d23ea89e165e74997a7300c3125 Mon Sep 17 00:00:00 2001 From: Fido2603 Date: Sun, 26 May 2019 22:06:56 +0200 Subject: [PATCH] v0.0.7 - Option for a day where UHC (no natural regeneration), is active. Is enabled by default, for own server --- pom.xml | 2 +- .../fido2603/semihardcore/SemiHardcore.java | 59 ++++++++++++++++++- .../fido2603/semihardcore/TimeConverter.java | 9 +++ .../listeners/PlayerListener.java | 4 +- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index c485b10..58be95a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 dk.fido2603 semihardcore - 0.0.6 + 0.0.7 Semi-Hardcore diff --git a/src/main/java/dk/fido2603/semihardcore/SemiHardcore.java b/src/main/java/dk/fido2603/semihardcore/SemiHardcore.java index 7e7b588..b002370 100644 --- a/src/main/java/dk/fido2603/semihardcore/SemiHardcore.java +++ b/src/main/java/dk/fido2603/semihardcore/SemiHardcore.java @@ -2,9 +2,12 @@ import dk.fido2603.semihardcore.listeners.PlayerListener; import net.milkbowl.vault.economy.Economy; + +import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; @@ -15,6 +18,9 @@ public class SemiHardcore extends JavaPlugin { public static boolean pluginEnabled = false; public boolean vaultEnabled = false; + public boolean uhcDayEnabled = true; + public Integer uhcDay = 2; + public boolean isUHCDay = false; public static Server server = null; public boolean debug = false; private String timeToBanString = "24h"; @@ -28,6 +34,7 @@ public class SemiHardcore extends JavaPlugin private Commands commands = null; private static SemiHardcore plugin; + private ConsoleCommandSender console; public static PermissionsManager getPermissionsManager() { @@ -43,6 +50,14 @@ public static Economy getEconomy() { return economy; } + + public void sendInfoAll(String message) { + String translatedMessage = ChatColor.translateAlternateColorCodes('&', message); + for (Player player : plugin.getServer().getOnlinePlayers()) + { + player.sendMessage(translatedMessage); + } + } public void sendInfo(Player player, String message) { @@ -60,6 +75,16 @@ public void onDisable() { saveSettings(); reloadSettings(); + + if (this.uhcDayEnabled) { + logDebug("Checking to turn off UHC"); + + if (isUHCDay) { + getServer().dispatchCommand(console, "gamerule naturalRegeneration true"); + isUHCDay = false; + log("Turned off UHC!"); + } + } pluginEnabled = false; } @@ -72,6 +97,7 @@ public void onEnable() server = getServer(); config = getConfig(); + this.console = server.getConsoleSender(); this.commands = new Commands(this); pluginEnabled = true; @@ -97,7 +123,7 @@ public void onEnable() } else { - plugin.log("Vault not found."); + log("Vault not found."); } } else @@ -109,6 +135,33 @@ public void onEnable() loadSettings(); saveSettings(); + + if (this.uhcDayEnabled) { + logDebug("UHC Day is enabled... checking"); + logDebug("Current day of week: " + TimeConverter.getDayOfWeek() + ". UHC Day: " + uhcDay); + + // now let's check every half minute, if it's UHC day + getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() + { + public void run() + { + if (TimeConverter.getDayOfWeek() == uhcDay && !isUHCDay) { + log("It's time for UHC day, switching the gamerule"); + logDebug("Day: " + TimeConverter.getDayOfWeek() + " - UHCDay: " + plugin.uhcDay.toString()); + getServer().dispatchCommand(console, "gamerule naturalRegeneration false"); + isUHCDay = true; + sendInfoAll("&6It is now UHC day! Take care, no more natural regen!"); + } + else if (!(TimeConverter.getDayOfWeek() == uhcDay) && isUHCDay) { + log("It's not UHC day anymore, switching the gamerule..."); + logDebug("Day: " + TimeConverter.getDayOfWeek() + " - UHCDay: " + plugin.uhcDay.toString()); + getServer().dispatchCommand(console, "gamerule naturalRegeneration true"); + isUHCDay = false; + sendInfoAll("&6It is no longer UHC day!"); + } + } + }, 20L, 600L); // 1200 is the ideal number of ticks for a minute, so we'll check each half minute - waiting about a second for the first check though, to get the things started + } permissionsManager.load(); playerManager.load(); @@ -141,6 +194,8 @@ public void loadSettings() this.debug = config.getBoolean("Settings.Debug", false); this.timeToBanString = config.getString("Settings.TimeToBan", "24h"); + this.uhcDayEnabled = config.getBoolean("Settings.UHCDayEnabled", true); + this.uhcDay = config.getInt("Settings.UHCDay", 2); this.timeToBan = TimeConverter.parseStringToMillis(timeToBanString); @@ -152,6 +207,8 @@ public void saveSettings() { config.set("Settings.Debug", Boolean.valueOf(this.debug)); config.set("Settings.TimeToBan", this.timeToBanString); + config.set("Settings.UHCDayEnabled", Boolean.valueOf(this.uhcDayEnabled)); + config.set("Settings.UHCDay", this.uhcDay); saveConfig(); } diff --git a/src/main/java/dk/fido2603/semihardcore/TimeConverter.java b/src/main/java/dk/fido2603/semihardcore/TimeConverter.java index d0d722a..bcf2c44 100644 --- a/src/main/java/dk/fido2603/semihardcore/TimeConverter.java +++ b/src/main/java/dk/fido2603/semihardcore/TimeConverter.java @@ -1,5 +1,7 @@ package dk.fido2603.semihardcore; +import java.util.Calendar; +import java.util.Date; import java.util.concurrent.TimeUnit; public class TimeConverter @@ -87,4 +89,11 @@ public static String parseMillisToUFString(long millis) { return String.format("%d %s", banMinutes, m); } + public static Integer getDayOfWeek() { + Calendar calendar = Calendar.getInstance(); + Date currentTime = new Date(); + calendar.setTime(currentTime); + return calendar.get(Calendar.DAY_OF_WEEK); + } + } \ No newline at end of file diff --git a/src/main/java/dk/fido2603/semihardcore/listeners/PlayerListener.java b/src/main/java/dk/fido2603/semihardcore/listeners/PlayerListener.java index 1c518aa..f379877 100644 --- a/src/main/java/dk/fido2603/semihardcore/listeners/PlayerListener.java +++ b/src/main/java/dk/fido2603/semihardcore/listeners/PlayerListener.java @@ -25,7 +25,7 @@ public void onPlayerJoin(PlayerJoinEvent event) { return; } - if (!event.getPlayer().isOp() || (!SemiHardcore.getPermissionsManager().hasPermission(event.getPlayer(), "semihardcore.exempt"))) { + if (!event.getPlayer().isOp() && (!SemiHardcore.getPermissionsManager().hasPermission(event.getPlayer(), "semihardcore.exempt"))) { plugin.logDebug("No op or exempt, checking player"); SemiHardcore.getPlayerManager().newPlayerCheck(event.getPlayer()); return; @@ -36,7 +36,7 @@ public void onPlayerJoin(PlayerJoinEvent event) @EventHandler(priority = EventPriority.HIGH) public void onPlayerDeath(PlayerDeathEvent event) { - if (!event.getEntity().isOp() || (!SemiHardcore.getPermissionsManager().hasPermission(event.getEntity(), "semihardcore.exempt"))) { + if (!event.getEntity().isOp() && (!SemiHardcore.getPermissionsManager().hasPermission(event.getEntity(), "semihardcore.exempt"))) { plugin.logDebug("No op or exempt, banning player"); SemiHardcore.getPlayerManager().banPlayer(event.getEntity(), event.getEntity().getUniqueId()); return;