Fixed high CPU usage

Closes #70
This commit is contained in:
Eric 2017-06-15 12:25:42 +02:00
parent 4fb31880d4
commit a78195037b
5 changed files with 50 additions and 97 deletions

View File

@ -1,10 +1,10 @@
package de.epiceric.shopchest.config; package de.epiceric.shopchest.config;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.event.ShopUpdateEvent;
import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.sql.Database; import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ItemUtils; import de.epiceric.shopchest.utils.ItemUtils;
import de.epiceric.shopchest.utils.ShopUpdater;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -25,7 +25,7 @@ public class Config {
private LanguageConfiguration langConfig; private LanguageConfiguration langConfig;
/** The quality of hologram and item updating (performance saving, or better quality) **/ /** The quality of hologram and item updating (performance saving, or better quality) **/
public ShopUpdateEvent.UpdateQuality update_quality; public ShopUpdater.UpdateQuality update_quality;
/** The item with which a player can click a shop to retrieve information **/ /** The item with which a player can click a shop to retrieve information **/
public ItemStack shop_info_item; public ItemStack shop_info_item;
@ -338,7 +338,7 @@ public class Config {
public void reload(boolean firstLoad, boolean langReload, boolean showMessages) { public void reload(boolean firstLoad, boolean langReload, boolean showMessages) {
plugin.reloadConfig(); plugin.reloadConfig();
update_quality = ShopUpdateEvent.UpdateQuality.valueOf(plugin.getConfig().getString("update-quality")); update_quality = ShopUpdater.UpdateQuality.valueOf(plugin.getConfig().getString("update-quality"));
shop_info_item = ItemUtils.getItemStack(plugin.getConfig().getString("shop-info-item")); shop_info_item = ItemUtils.getItemStack(plugin.getConfig().getString("shop-info-item"));
wg_allow_create_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.create-shop"); wg_allow_create_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.create-shop");
wg_allow_use_admin_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.use-admin-shop"); wg_allow_use_admin_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.use-admin-shop");

View File

@ -1,45 +0,0 @@
package de.epiceric.shopchest.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Called when the shop updater runs <br/>
* It's not recommended to listen to this event!
*/
public class ShopUpdateEvent extends Event {
public enum UpdateQuality {
SLOWEST(31L),
SLOWER(24L),
SLOW(17L),
NORMAL(10L),
FAST(7L),
FASTER(4L),
FASTEST(1L);
private long time;
UpdateQuality(long time) {
this.time = time;
}
public long getTime() {
return time;
}
}
private static final HandlerList handlers = new HandlerList();
public ShopUpdateEvent() {
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,11 +1,9 @@
package de.epiceric.shopchest.listeners; package de.epiceric.shopchest.listeners;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.event.ShopUpdateEvent;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.Callback; import de.epiceric.shopchest.utils.Callback;
import de.epiceric.shopchest.utils.ShopUpdater; import de.epiceric.shopchest.utils.ShopUpdater;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -24,13 +22,6 @@ public class ShopUpdateListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler
public void onShopUpdate(ShopUpdateEvent e) {
for (Player p : Bukkit.getOnlinePlayers()) {
plugin.getShopUtils().updateShops(p);
}
}
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
restartShopUpdater(e.getPlayer()); restartShopUpdater(e.getPlayer());

View File

@ -1,37 +1,59 @@
package de.epiceric.shopchest.utils; package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.event.ShopUpdateEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class ShopUpdater extends Thread { import java.util.Collection;
public class ShopUpdater extends BukkitRunnable {
public enum UpdateQuality {
SLOWEST(31L),
SLOWER(24L),
SLOW(17L),
NORMAL(10L),
FAST(7L),
FASTER(4L),
FASTEST(1L);
private long interval;
UpdateQuality(long interval) {
this.interval = interval;
}
public long getInterval() {
return interval;
}
}
private ShopChest plugin; private ShopChest plugin;
private boolean running; private boolean running;
private long maxDelta; private long interval;
private long lastTime;
public ShopUpdater(ShopChest plugin) { public ShopUpdater(ShopChest plugin) {
this.plugin = plugin; this.plugin = plugin;
setMaxDelta(plugin.getShopChestConfig().update_quality.getTime()); setInterval(plugin.getShopChestConfig().update_quality.getInterval());
} }
public synchronized void setMaxDelta(long maxDelta) { public synchronized void setInterval(long interval) {
this.maxDelta = maxDelta * 50; this.interval = interval;
}
public synchronized void start() {
super.runTaskTimerAsynchronously(plugin, interval, interval);
running = true;
} }
@Override @Override
public synchronized void start() {
super.start();
running = true;
lastTime = System.currentTimeMillis();
}
public synchronized void cancel() { public synchronized void cancel() {
running = false; if (running) {
super.interrupt(); running = false;
super.cancel();
}
} }
public boolean isRunning() { public boolean isRunning() {
@ -40,23 +62,14 @@ public class ShopUpdater extends Thread {
@Override @Override
public void run() { public void run() {
while(running) { Collection<? extends Player> players = Bukkit.getOnlinePlayers();
if (Bukkit.getOnlinePlayers().isEmpty()) {
cancel();
}
long timeNow = System.currentTimeMillis(); if (players.isEmpty()) {
long timeElapsed = timeNow - lastTime; cancel();
}
if (timeElapsed >= maxDelta) { for (Player p : players) {
new BukkitRunnable() { plugin.getShopUtils().updateShops(p);
@Override
public void run() {
Bukkit.getPluginManager().callEvent(new ShopUpdateEvent());
}
}.runTask(plugin);
lastTime = timeNow;
}
} }
} }
} }

View File

@ -5,7 +5,6 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
@ -216,7 +215,10 @@ public class ShopUtils {
if (reloadConfig) { if (reloadConfig) {
plugin.getShopChestConfig().reload(false, true, showConsoleMessages); plugin.getShopChestConfig().reload(false, true, showConsoleMessages);
plugin.getHologramFormat().reload(); plugin.getHologramFormat().reload();
plugin.getUpdater().setMaxDelta(plugin.getShopChestConfig().update_quality.getTime()); plugin.getUpdater().cancel();
plugin.setUpdater(new ShopUpdater(plugin));
plugin.getUpdater().start();
} }
plugin.getShopDatabase().connect(new Callback(plugin) { plugin.getShopDatabase().connect(new Callback(plugin) {
@ -362,8 +364,6 @@ public class ShopUtils {
* @param player Player to show the update * @param player Player to show the update
*/ */
public void updateShop(Shop shop, Player player) { public void updateShop(Shop shop, Player player) {
if (!shop.getLocation().getChunk().isLoaded()) return;
double holoDistSqr = Math.pow(plugin.getShopChestConfig().maximal_distance, 2); double holoDistSqr = Math.pow(plugin.getShopChestConfig().maximal_distance, 2);
double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2); double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2);
@ -372,12 +372,6 @@ public class ShopUtils {
if (distSqr <= holoDistSqr) { if (distSqr <= holoDistSqr) {
if (shop.getHologram() != null) { if (shop.getHologram() != null) {
Material type = shop.getLocation().getBlock().getType();
if (type != Material.CHEST && type != Material.TRAPPED_CHEST) {
plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error);
return;
}
if (!shop.getHologram().isVisible(player)) { if (!shop.getHologram().isVisible(player)) {
shop.getHologram().showPlayer(player); shop.getHologram().showPlayer(player);
} }