2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Commands.ItemInfo;
|
|
|
|
import com.Acrobot.ChestShop.Commands.Version;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.DB.Generator;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.DB.Queue;
|
|
|
|
import com.Acrobot.ChestShop.DB.Transaction;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.BlockBreak;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.BlockPlace;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.EntityExplode;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.SignChange;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Player.ShortNameSaver;
|
2012-08-10 18:56:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PostTransaction.EconomicModule;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PostTransaction.ItemManager;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PreTransaction.*;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PostTransaction.EmptyShopDeleter;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PostTransaction.TransactionLogger;
|
|
|
|
import com.Acrobot.ChestShop.Listeners.Shop.PostTransaction.TransactionMessageSender;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Logging.FileFormatter;
|
2012-08-10 19:00:20 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.RestrictedSign;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.avaje.ebean.EbeanServer;
|
2011-07-23 21:00:47 +02:00
|
|
|
import com.lennardf1989.bukkitex.Database;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.Server;
|
2012-03-01 22:03:59 +01:00
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.event.Event;
|
|
|
|
import org.bukkit.event.Listener;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.plugin.PluginDescriptionFile;
|
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
import java.io.File;
|
2012-05-10 16:32:25 +02:00
|
|
|
import java.io.IOException;
|
2011-07-24 16:56:18 +02:00
|
|
|
import java.util.ArrayList;
|
2011-05-29 13:25:25 +02:00
|
|
|
import java.util.List;
|
2012-05-10 16:32:25 +02:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.Logger;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
import static com.Acrobot.ChestShop.Config.Property.ALLOW_PARTIAL_TRANSACTIONS;
|
|
|
|
import static com.Acrobot.ChestShop.Config.Property.SHOP_INTERACTION_INTERVAL;
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* Main file of the plugin
|
2011-05-29 13:25:25 +02:00
|
|
|
*
|
2011-05-15 18:16:25 +02:00
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-05-15 19:33:03 +02:00
|
|
|
public class ChestShop extends JavaPlugin {
|
2012-05-10 16:32:25 +02:00
|
|
|
public static File dataFolder = new File("plugins/ChestShop");
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private static EbeanServer DB;
|
|
|
|
private static PluginDescriptionFile description;
|
2011-05-15 18:16:25 +02:00
|
|
|
private static Server server;
|
2012-05-10 16:32:25 +02:00
|
|
|
private static Logger logger;
|
|
|
|
private static PluginManager pluginManager;
|
|
|
|
private static ChestShop plugin;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private FileHandler handler;
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
public void onEnable() {
|
2012-05-10 16:32:25 +02:00
|
|
|
plugin = this;
|
|
|
|
logger = getLogger();
|
|
|
|
pluginManager = getServer().getPluginManager();
|
|
|
|
dataFolder = getDataFolder();
|
|
|
|
description = getDescription();
|
|
|
|
server = getServer();
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
Config.setup();
|
|
|
|
Dependencies.load();
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
registerEvents();
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (Config.getBoolean(Property.LOG_TO_DATABASE) || Config.getBoolean(Property.GENERATE_STATISTICS_PAGE)) {
|
|
|
|
setupDB();
|
|
|
|
}
|
|
|
|
if (Config.getBoolean(Property.GENERATE_STATISTICS_PAGE)) {
|
2012-06-08 15:28:36 +02:00
|
|
|
File htmlFolder = new File(Config.getString(Property.STATISTICS_PAGE_PATH));
|
2012-05-10 16:32:25 +02:00
|
|
|
scheduleTask(new Generator(htmlFolder), 300L, (long) Config.getDouble(Property.STATISTICS_PAGE_GENERATION_INTERVAL) * 20L);
|
|
|
|
}
|
|
|
|
if (Config.getBoolean(Property.LOG_TO_FILE)) {
|
|
|
|
File log = loadFile(new File(ChestShop.getFolder(), "ChestShop.log"));
|
2011-07-05 19:08:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
FileHandler handler = loadHandler(log.getAbsolutePath());
|
|
|
|
handler.setFormatter(new FileFormatter());
|
2012-03-06 19:41:14 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
this.handler = handler;
|
|
|
|
logger.addHandler(handler);
|
|
|
|
}
|
|
|
|
if (!Config.getBoolean(Property.LOG_TO_CONSOLE)) {
|
|
|
|
logger.setUseParentHandlers(false);
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
//Register our commands!
|
|
|
|
getCommand("iteminfo").setExecutor(new ItemInfo());
|
|
|
|
getCommand("csVersion").setExecutor(new Version());
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
//Start the statistics pinger
|
|
|
|
startStatistics();
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private static File loadFile(File file) {
|
|
|
|
if (!file.exists()) {
|
|
|
|
try {
|
|
|
|
file.createNewFile();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static FileHandler loadHandler(String path) {
|
|
|
|
FileHandler handler = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
handler = new FileHandler(path, true);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
public void onDisable() {
|
2012-03-01 22:03:59 +01:00
|
|
|
getServer().getScheduler().cancelTasks(this);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (handler != null) {
|
|
|
|
handler.close();
|
|
|
|
getLogger().removeHandler(handler);
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
////////////////// REGISTER EVENTS, SCHEDULER & STATS ///////////////////////////
|
2011-09-06 19:01:57 +02:00
|
|
|
private void registerEvents() {
|
2012-06-25 17:15:32 +02:00
|
|
|
registerEvent(new com.Acrobot.ChestShop.Plugins.ChestShop()); //Chest protection
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
registerEvent(new BlockBreak());
|
|
|
|
registerEvent(new BlockPlace());
|
|
|
|
registerEvent(new SignChange());
|
|
|
|
registerEvent(new EntityExplode());
|
2012-06-08 15:28:36 +02:00
|
|
|
registerEvent(new PlayerConnect());
|
2012-08-10 18:56:16 +02:00
|
|
|
registerEvent(new PlayerInteract());
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
registerEvent(new ItemInfoListener());
|
|
|
|
|
|
|
|
registerEvent(new EmptyShopDeleter());
|
|
|
|
registerEvent(new TransactionLogger());
|
|
|
|
registerEvent(new TransactionMessageSender());
|
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
registerEvent(new EconomicModule());
|
|
|
|
registerEvent(new ItemManager());
|
|
|
|
|
|
|
|
registerEvent(new CreativeModeIgnorer());
|
|
|
|
registerEvent(new PermissionChecker());
|
|
|
|
registerEvent(new PriceValidator());
|
|
|
|
registerEvent(new ShopValidator());
|
|
|
|
registerEvent(new StockFittingChecker());
|
|
|
|
registerEvent(new ErrorMessageSender());
|
|
|
|
registerEvent(new SpamClickProtector(Config.getInteger(SHOP_INTERACTION_INTERVAL)));
|
|
|
|
|
2012-08-10 19:00:20 +02:00
|
|
|
registerEvent(new RestrictedSign());
|
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
if (Config.getBoolean(ALLOW_PARTIAL_TRANSACTIONS)) {
|
|
|
|
registerEvent(new PartialTransactionModule());
|
|
|
|
} else {
|
|
|
|
registerEvent(new AmountAndPriceChecker());
|
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-08-10 18:56:16 +02:00
|
|
|
registerEvent(new ShortNameSaver());
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public void registerEvent(Listener listener) {
|
|
|
|
getServer().getPluginManager().registerEvents(listener, this);
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void scheduleTask(Runnable runnable, long startTime, long repetetionTime) {
|
|
|
|
server.getScheduler().scheduleAsyncRepeatingTask(this, runnable, startTime, repetetionTime);
|
|
|
|
}
|
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
private void startStatistics() {
|
|
|
|
try {
|
2012-04-16 16:34:00 +02:00
|
|
|
new Metrics(this).start();
|
2012-05-10 16:32:25 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
ChestShop.getBukkitLogger().severe("There was an error while submitting statistics.");
|
2012-03-17 15:00:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
///////////////////// DATABASE STUFF ////////////////////////////////
|
2012-03-01 22:03:59 +01:00
|
|
|
private static YamlConfiguration getBukkitConfig() {
|
|
|
|
return YamlConfiguration.loadConfiguration(new File("bukkit.yml"));
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-07-24 16:56:18 +02:00
|
|
|
private static Database database;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
private void setupDB() {
|
|
|
|
database = new Database(this) {
|
|
|
|
protected java.util.List<Class<?>> getDatabaseClasses() {
|
2011-07-24 16:56:18 +02:00
|
|
|
List<Class<?>> list = new ArrayList<Class<?>>();
|
2011-07-23 21:00:47 +02:00
|
|
|
list.add(Transaction.class);
|
|
|
|
return list;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
};
|
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
YamlConfiguration config = getBukkitConfig();
|
2011-07-23 21:00:47 +02:00
|
|
|
|
|
|
|
database.initializeDatabase(
|
|
|
|
config.getString("database.driver"),
|
|
|
|
config.getString("database.url"),
|
|
|
|
config.getString("database.username"),
|
|
|
|
config.getString("database.password"),
|
2011-09-22 15:56:06 +02:00
|
|
|
config.getString("database.isolation")
|
2011-07-23 21:00:47 +02:00
|
|
|
);
|
2011-07-24 16:56:18 +02:00
|
|
|
|
|
|
|
DB = database.getDatabase();
|
2011-09-06 19:01:57 +02:00
|
|
|
getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Queue(), 200L, 200L);
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
@Override
|
2011-07-23 21:00:47 +02:00
|
|
|
public EbeanServer getDatabase() {
|
|
|
|
return database.getDatabase();
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public static File getFolder() {
|
|
|
|
return dataFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Logger getBukkitLogger() {
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static Server getBukkitServer() {
|
|
|
|
return server;
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static String getVersion() {
|
2011-07-23 21:00:47 +02:00
|
|
|
return description.getVersion();
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String getPluginName() {
|
2011-07-23 21:00:47 +02:00
|
|
|
return description.getName();
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
public static EbeanServer getDB() {
|
2011-07-23 21:00:47 +02:00
|
|
|
return DB;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2012-03-01 22:03:59 +01:00
|
|
|
public static List getDependencies() {
|
2012-05-10 16:32:25 +02:00
|
|
|
return description.getSoftDepend();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerListener(Listener listener) {
|
|
|
|
plugin.registerEvent(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void callEvent(Event event) {
|
|
|
|
pluginManager.callEvent(event);
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
public static void scheduleRepeating(Runnable runnable, int delay) {
|
|
|
|
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, runnable, 0, delay);
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|