From b0f4894b1591044569ae75296698b4a2fc7a1d22 Mon Sep 17 00:00:00 2001 From: filoghost Date: Fri, 20 Feb 2015 22:25:25 +0100 Subject: [PATCH] Cache online players amount. --- ChestCommands/menu/example.yml | 16 +++++++++++++++ ChestCommands/plugin.yml | 2 +- .../chestcommands/internal/CachedGetters.java | 20 +++++++++++++++++++ .../chestcommands/internal/Variable.java | 4 ++-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CachedGetters.java diff --git a/ChestCommands/menu/example.yml b/ChestCommands/menu/example.yml index f53713a..be09faf 100644 --- a/ChestCommands/menu/example.yml +++ b/ChestCommands/menu/example.yml @@ -16,6 +16,11 @@ menu-settings: # Bind multiple commands using ; (command: 'menu; m; me') command: 'menu' + # auto-refresh - OPTIONAL + # How frequently the menu will be refreshed, in seconds. + # Useful if you have variables in items' descriptions. + auto-refresh: 5 + # This command command will be execute when the menu is opened. # Supports all the icon command types. open-action: 'sound: note pling; tell: &eYou opened the example menu.' @@ -153,6 +158,17 @@ a-talking-head: POSITION-Y: 2 +a-talking-head: + COMMAND: 'tell: This is a simple message, without using commands!' + NAME: '&3Tells you something.' + LORE: + - '&7It tells you something without commands.' + ID: head + DATA-VALUE: 3 + POSITION-X: 1 + POSITION-Y: 2 + + test-multiple-command: COMMAND: 'console: Say Did you know that...; console: say you can run multiple commands?;' NAME: '&aMultiple commands' diff --git a/ChestCommands/plugin.yml b/ChestCommands/plugin.yml index 7dcdf4e..07a6a64 100644 --- a/ChestCommands/plugin.yml +++ b/ChestCommands/plugin.yml @@ -1,6 +1,6 @@ name: ChestCommands main: com.gmail.filoghost.chestcommands.ChestCommands -version: 3.0.8 +version: 3.1 softdepend: [Vault, BarAPI, PlayerPoints] commands: diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CachedGetters.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CachedGetters.java new file mode 100644 index 0000000..90a7db8 --- /dev/null +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CachedGetters.java @@ -0,0 +1,20 @@ +package com.gmail.filoghost.chestcommands.internal; + +import org.bukkit.Bukkit; + +public class CachedGetters { + + private static long lastOnlinePlayersRefresh; + private static int onlinePlayers; + + public static int getOnlinePlayers() { + long now = System.currentTimeMillis(); + if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) { + // getOnlinePlayers() could be expensive if called frequently + onlinePlayers = Bukkit.getOnlinePlayers().length; + } + + return onlinePlayers; + } + +} diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/Variable.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/Variable.java index 2911058..82df26e 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/Variable.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/Variable.java @@ -7,7 +7,7 @@ import com.gmail.filoghost.chestcommands.bridge.EconomyBridge; import com.gmail.filoghost.chestcommands.bridge.PlayerPointsBridge; public enum Variable { - + PLAYER("{player}") { public String getReplacement(Player executor) { return executor.getName(); @@ -16,7 +16,7 @@ public enum Variable { ONLINE("{online}") { public String getReplacement(Player executor) { - return String.valueOf(Bukkit.getOnlinePlayers().length); + return String.valueOf(CachedGetters.getOnlinePlayers()); } },