From d40c143afc9c40e4a76afdb98e8df3727d5c7454 Mon Sep 17 00:00:00 2001 From: JeromSar Date: Fri, 9 Aug 2013 15:40:08 +0200 Subject: [PATCH] Reworked /health, should be thread-safe now Updated version number to reflect latest changes --- appinfo.properties | 8 +- buildnumber.properties | 4 +- src/config.yml | 2 +- .../Commands/Command_health.java | 79 +++++++++++-------- .../Listener/TFM_BlockListener.java | 5 -- .../TotalFreedomMod/TFM_TickMeter.java | 66 ++++++++-------- src/plugin.yml | 2 +- 7 files changed, 87 insertions(+), 79 deletions(-) diff --git a/appinfo.properties b/appinfo.properties index fd5a0e01..23471388 100644 --- a/appinfo.properties +++ b/appinfo.properties @@ -1,6 +1,6 @@ -#Sat, 03 Aug 2013 14:59:27 -0400 +#Fri, 09 Aug 2013 15:38:23 +0200 -program.VERSION=2.21 -program.BUILDNUM=375 -program.BUILDDATE=08/03/2013 02\:59 PM +program.VERSION=2.22 +program.BUILDNUM=378 +program.BUILDDATE=08/09/2013 03\:38 PM diff --git a/buildnumber.properties b/buildnumber.properties index 6fc46c45..e221c716 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Sat Aug 03 14:59:27 EDT 2013 -build.number=376 +#Fri Aug 09 15:38:23 CEST 2013 +build.number=379 diff --git a/src/config.yml b/src/config.yml index c2b28173..3a8b8bae 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,4 +1,4 @@ -# TotalFreedomMod v2.21 Configuration +# TotalFreedomMod v2.22 Configuration # by Madgeek1450 and DarthSalamon # Block placement prevention: diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_health.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_health.java index 2ebc7ab1..e33323ea 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_health.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_health.java @@ -1,8 +1,15 @@ package me.StevenLawson.TotalFreedomMod.Commands; +import java.text.DecimalFormat; +import me.StevenLawson.TotalFreedomMod.TFM_Log; +import me.StevenLawson.TotalFreedomMod.TFM_TickMeter; +import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; +import org.apache.commons.lang.exception.ExceptionUtils; +import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; @CommandPermissions(level = AdminLevel.SUPER, source = SourceType.BOTH) @CommandParameters(description = "View ticks-per-second", usage = "/") @@ -11,39 +18,45 @@ public class Command_health extends TFM_Command @Override public boolean run(final CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { -// -- MADGEEK: This is not thread safe. It needs to be rewritten using proper Bukkit scheduler API. -// -// Runnable task; -// task = new Runnable() // async -// { -// @Override -// public void run() -// { -// try -// { -// TFM_TickMeter meter = new TFM_TickMeter(plugin); -// meter.startTicking(); -// Thread.sleep(1000); // per second -// meter.stopTicking(); -// -// Runtime runtime = Runtime.getRuntime(); -// int mb = 1048576; // 1024 * 1024 -// -// float usedMem = runtime.totalMemory() - runtime.freeMemory(); -// -// playerMsg("Reserved Memory: " + runtime.totalMemory() / mb + "mb"); -// playerMsg("Used Memory: " + new DecimalFormat("#").format(usedMem / mb) + "mb (" + new DecimalFormat("#").format(usedMem / runtime.totalMemory() * 100) + "%)"); -// playerMsg("Max Memory: " + runtime.maxMemory() / mb + "mb"); -// playerMsg("Ticks per second: " + (meter.getTicks() == 20 ? ChatColor.GREEN : ChatColor.RED) + meter.getTicks()); -// } -// catch (Exception iex) -// { -// TFM_Log.warning("Exception in TFM_TickMeter: Thread was interupted in sleeping process."); -// TFM_Log.warning(ExceptionUtils.getStackTrace(iex)); -// } -// } -// }; -// new Thread(task, "TickMeter").start(); + + new BukkitRunnable() + { + + @Override + public void run() + { + try + { + final TFM_TickMeter meter = new TFM_TickMeter(plugin); + meter.startTicking(); + Thread.sleep(1000); // per second + meter.stopTicking(); + + final Runtime runtime = Runtime.getRuntime(); + final int mb = 1048576; // 1024 * 1024 + + final float usedMem = runtime.totalMemory() - runtime.freeMemory(); + + new BukkitRunnable() + { + @Override + public void run() { + playerMsg("Reserved Memory: " + runtime.totalMemory() / mb + "mb"); + playerMsg("Used Memory: " + new DecimalFormat("#").format(usedMem / mb) + "mb (" + new DecimalFormat("#").format((usedMem / runtime.totalMemory()) * 100) + "%)"); + playerMsg("Max Memory: " + runtime.maxMemory() / mb + "mb"); + playerMsg("Ticks per second: " + (meter.getTicks() == 20 ? ChatColor.GREEN : ChatColor.RED) + meter.getTicks()); + } + }.runTask(TotalFreedomMod.plugin); + } + catch (Exception iex) + { + TFM_Log.warning("Exception in TFM_TickMeter: Thread was interupted in sleeping process."); + TFM_Log.warning(ExceptionUtils.getStackTrace(iex)); + } + } + + }.runTaskAsynchronously(TotalFreedomMod.plugin); + return true; } } diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_BlockListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_BlockListener.java index 617ad17a..a244663a 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_BlockListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_BlockListener.java @@ -94,7 +94,6 @@ public class TFM_BlockListener implements Listener if (TFM_ProtectedArea.isInProtectedArea(block_pos)) { event.setCancelled(true); - return; } } } @@ -185,7 +184,6 @@ public class TFM_BlockListener implements Listener p.sendMessage(ChatColor.GRAY + "Lava placement is currently disabled."); event.setCancelled(true); - return; } break; } @@ -204,7 +202,6 @@ public class TFM_BlockListener implements Listener p.sendMessage(ChatColor.GRAY + "Water placement is currently disabled."); event.setCancelled(true); - return; } break; } @@ -222,7 +219,6 @@ public class TFM_BlockListener implements Listener p.sendMessage(ChatColor.GRAY + "Fire placement is currently disabled."); event.setCancelled(true); - return; } break; } @@ -240,7 +236,6 @@ public class TFM_BlockListener implements Listener p.sendMessage(ChatColor.GRAY + "TNT is currently disabled."); event.setCancelled(true); - return; } break; } diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_TickMeter.java b/src/me/StevenLawson/TotalFreedomMod/TFM_TickMeter.java index 3e68b223..093e1512 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_TickMeter.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_TickMeter.java @@ -2,37 +2,37 @@ package me.StevenLawson.TotalFreedomMod; public class TFM_TickMeter { -// int ticks; -// int taskId; -// final TotalFreedomMod plugin; -// -// public TFM_TickMeter(TotalFreedomMod plugin) -// { -// this.plugin = plugin; -// } -// -// public int startTicking() -// { -// int tId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() -// { -// @Override -// public void run() -// { -// ticks += 1; -// } -// }, 1L, 1L); // ticks (20 in 1 second) -// -// taskId = tId; -// return tId; -// } -// -// public void stopTicking() -// { -// plugin.getServer().getScheduler().cancelTask(taskId); -// } -// -// public int getTicks() -// { -// return ticks; -// } + int ticks; + int taskId; + final TotalFreedomMod plugin; + + public TFM_TickMeter(TotalFreedomMod plugin) + { + this.plugin = plugin; + } + + public int startTicking() + { + int tId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() + { + @Override + public void run() + { + ticks += 1; + } + }, 1L, 1L); // ticks (20 in 1 second) + + taskId = tId; + return tId; + } + + public void stopTicking() + { + plugin.getServer().getScheduler().cancelTask(taskId); + } + + public int getTicks() + { + return ticks; + } } diff --git a/src/plugin.yml b/src/plugin.yml index 6e377cc2..e00e57b0 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: TotalFreedomMod main: me.StevenLawson.TotalFreedomMod.TotalFreedomMod -version: 2.21 +version: 2.22 description: Plugin for the Total Freedom server. authors: [StevenLawson / Madgeek1450, JeromSar / DarthSalamon]