Added reload-timer (can be configured)

This commit is contained in:
Eric 2016-07-13 14:34:09 +02:00
parent 1d22e28954
commit 225c59c2ea
6 changed files with 33 additions and 11 deletions

View File

@ -232,7 +232,7 @@ public class Commands extends BukkitCommand {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return; if (event.isCancelled()) return;
player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops())))); player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops(true)))));
} }
/** /**

View File

@ -2,6 +2,7 @@ package de.epiceric.shopchest;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.event.ShopReloadEvent;
import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.listeners.*; import de.epiceric.shopchest.listeners.*;
@ -26,6 +27,7 @@ import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -187,6 +189,18 @@ public class ShopChest extends JavaPlugin {
database = new MySQL(this); database = new MySQL(this);
} }
if (config.auto_reload_time > 0) {
Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
@Override
public void run() {
ShopReloadEvent event = new ShopReloadEvent(Bukkit.getConsoleSender());
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) getLogger().info(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops(true)))));
}
}, config.auto_reload_time * 20, config.auto_reload_time * 20);
}
lockette = getServer().getPluginManager().getPlugin("Lockette") != null; lockette = getServer().getPluginManager().getPlugin("Lockette") != null;
lwc = getServer().getPluginManager().getPlugin("LWC") != null; lwc = getServer().getPluginManager().getPlugin("LWC") != null;
@ -287,7 +301,7 @@ public class ShopChest extends JavaPlugin {
* Initializes the shops * Initializes the shops
*/ */
private void initializeShops() { private void initializeShops() {
int count = ShopUtils.reloadShops(); int count = ShopUtils.reloadShops(false);
getLogger().info("Initialized " + String.valueOf(count) + " Shops"); getLogger().info("Initialized " + String.valueOf(count) + " Shops");
} }
@ -381,7 +395,7 @@ public class ShopChest extends JavaPlugin {
/** /**
* Provides a reader for a text file located inside the jar. * Provides a reader for a text file located inside the jar.
* The returned reader will read text with the UTF-8 charset. * The returned reader will read text with the UTF-8 charset.
* @param file - the filename of the resource to load * @param file the filename of the resource to load
* @return null if getResource(String) returns null * @return null if getResource(String) returns null
* @throws IllegalArgumentException - if file is null * @throws IllegalArgumentException - if file is null
*/ */

View File

@ -105,6 +105,9 @@ public class Config {
/** The default shop limit for players and groups that are not listed in {@link #shopLimits_player} or in {@link #shopLimits_group} **/ /** The default shop limit for players and groups that are not listed in {@link #shopLimits_player} or in {@link #shopLimits_group} **/
public int default_limit; public int default_limit;
/** The time between automatic shop reloads (if set to 0, the timer will be disabled) **/
public int auto_reload_time;
/** The main command of ShopChest <i>(default: shop)</i> **/ /** The main command of ShopChest <i>(default: shop)</i> **/
public String main_command_name; public String main_command_name;
@ -272,6 +275,7 @@ public class Config {
shop_creation_price_normal = plugin.getConfig().getDouble("shop-creation-price.normal"); shop_creation_price_normal = plugin.getConfig().getDouble("shop-creation-price.normal");
shop_creation_price_admin = plugin.getConfig().getDouble("shop-creation-price.admin"); shop_creation_price_admin = plugin.getConfig().getDouble("shop-creation-price.admin");
default_limit = plugin.getConfig().getInt("shop-limits.default"); default_limit = plugin.getConfig().getInt("shop-limits.default");
auto_reload_time = plugin.getConfig().getInt("auto-reload-time");
main_command_name = plugin.getConfig().getString("main-command-name"); main_command_name = plugin.getConfig().getString("main-command-name");
language_file = plugin.getConfig().getString("language-file"); language_file = plugin.getConfig().getString("language-file");

View File

@ -1,6 +1,6 @@
package de.epiceric.shopchest.event; package de.epiceric.shopchest.event;
import org.bukkit.entity.Player; import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -11,18 +11,18 @@ import org.bukkit.event.HandlerList;
public class ShopReloadEvent extends Event implements Cancellable { public class ShopReloadEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private Player player; private CommandSender sender;
private boolean cancelled; private boolean cancelled;
public ShopReloadEvent(Player player) { public ShopReloadEvent(CommandSender sender) {
this.player = player; this.sender = sender;
} }
/** /**
* @return Player who is involved in this event * @return Player who is involved in this event
*/ */
public Player getPlayer() { public CommandSender getSender() {
return player; return sender;
} }
@Override @Override

View File

@ -195,8 +195,8 @@ public class ShopUtils {
* Reload the shops * Reload the shops
* @return Amount of shops, which were reloaded * @return Amount of shops, which were reloaded
*/ */
public static int reloadShops() { public static int reloadShops(boolean reloadConfig) {
plugin.getShopChestConfig().reload(false, true); if (reloadConfig) plugin.getShopChestConfig().reload(false, true);
for (Shop shop : ShopUtils.getShops()) { for (Shop shop : ShopUtils.getShops()) {
ShopUtils.removeShop(shop, false); ShopUtils.removeShop(shop, false);

View File

@ -23,6 +23,10 @@ remove-shop-on-error: false
# Value MUST be a number (e.g. 1, 1.5, 2.75, ...) # Value MUST be a number (e.g. 1, 1.5, 2.75, ...)
maximal-distance: 1.75 maximal-distance: 1.75
# Set the time in seconds between automatic shop reloads.
# You can set this to 0 to disable automatic shop reloads.
auto-reload-time: 1200
# Set the price a player has to pay in order to create... # Set the price a player has to pay in order to create...
# You can set this to 0 to disable costs. # You can set this to 0 to disable costs.
# Value MUST be a number (e.g. 1, 1.5, 2.75, ...) # Value MUST be a number (e.g. 1, 1.5, 2.75, ...)