ChestShop-3/com/Acrobot/ChestShop/ChestShop.java

326 lines
10 KiB
Java
Raw Normal View History

2011-05-15 19:33:03 +02:00
package com.Acrobot.ChestShop;
import com.Acrobot.Breeze.Configuration.Configuration;
2011-05-29 13:25:25 +02:00
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Commands.Version;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
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;
import com.Acrobot.ChestShop.Listeners.Block.BlockPlace;
2012-09-12 12:35:48 +02:00
import com.Acrobot.ChestShop.Listeners.Block.Break.ChestBreak;
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Listeners.Block.SignCreate;
import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect;
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
2012-09-12 12:35:48 +02:00
import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory;
import com.Acrobot.ChestShop.Listeners.Player.ShortNameSaver;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Listeners.PostShopCreation.CreationFeeGetter;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.MessageSender;
import com.Acrobot.ChestShop.Listeners.PostShopCreation.SignSticker;
import com.Acrobot.ChestShop.Listeners.PostTransaction.*;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Listeners.PreShopCreation.*;
import com.Acrobot.ChestShop.Listeners.PreTransaction.*;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Listeners.PreTransaction.ErrorMessageSender;
import com.Acrobot.ChestShop.Listeners.PreTransaction.PermissionChecker;
import com.Acrobot.ChestShop.Listeners.ShopRefundListener;
import com.Acrobot.ChestShop.Logging.FileFormatter;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Metadata.ItemDatabase;
2012-08-10 19:00:20 +02:00
import com.Acrobot.ChestShop.Signs.RestrictedSign;
import com.Acrobot.ChestShop.Utils.uName;
2011-05-29 13:25:25 +02:00
import com.avaje.ebean.EbeanServer;
import com.lennardf1989.bukkitex.Database;
import com.nijikokun.register.payment.forChestShop.Methods;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
2011-05-29 13:25:25 +02:00
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
2011-05-29 13:25:25 +02:00
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
2011-05-29 13:25:25 +02:00
/**
* Main file of the plugin
2011-05-29 13:25:25 +02:00
*
* @author Acrobot
*/
2011-05-15 19:33:03 +02:00
public class ChestShop extends JavaPlugin {
2013-01-24 22:35:28 +01:00
private static ChestShop plugin;
private static Server server;
private static PluginDescriptionFile description;
2013-01-24 22:35:28 +01:00
private static File dataFolder;
private static EbeanServer database;
2013-01-24 22:35:28 +01:00
private static ItemDatabase itemDatabase;
2013-01-24 22:35:28 +01:00
private static Logger logger;
private FileHandler handler;
public void onEnable() {
plugin = this;
logger = getLogger();
dataFolder = getDataFolder();
description = getDescription();
server = getServer();
2013-01-24 22:35:28 +01:00
itemDatabase = new ItemDatabase();
Configuration.pairFileAndClass(loadFile("config.yml"), Properties.class);
Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class);
uName.file = loadFile("longName.storage");
uName.load();
Methods.setPreferred(Properties.PREFERRED_ECONOMY_PLUGIN);
Dependencies.load();
2011-05-29 13:25:25 +02:00
registerEvents();
if (Properties.LOG_TO_DATABASE || Properties.GENERATE_STATISTICS_PAGE) {
setupDB();
}
2012-12-19 18:54:23 +01:00
if (Properties.GENERATE_STATISTICS_PAGE) {
File htmlFolder = new File(Properties.STATISTICS_PAGE_PATH);
scheduleTask(new Generator(htmlFolder), 300L, Properties.STATISTICS_PAGE_GENERATION_INTERVAL * 20L);
}
2012-12-19 18:54:23 +01:00
if (Properties.LOG_TO_FILE) {
File log = loadFile("ChestShop.log");
FileHandler handler = loadHandler(log.getAbsolutePath());
handler.setFormatter(new FileFormatter());
this.handler = handler;
logger.addHandler(handler);
}
2012-12-19 18:54:23 +01:00
if (!Properties.LOG_TO_CONSOLE) {
logger.setUseParentHandlers(false);
}
2011-05-29 13:25:25 +02:00
getCommand("iteminfo").setExecutor(new ItemInfo());
getCommand("csVersion").setExecutor(new Version());
startStatistics();
}
public static File loadFile(String string) {
File file = new File(dataFolder, string);
return loadFile(file);
}
private static File loadFile(File file) {
if (!file.exists()) {
try {
file.getParentFile().mkdirs();
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;
}
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
if (handler != null) {
handler.close();
getLogger().removeHandler(handler);
}
}
////////////////// REGISTER EVENTS, SCHEDULER & STATS ///////////////////////////
private void registerEvents() {
2012-06-25 17:15:32 +02:00
registerEvent(new com.Acrobot.ChestShop.Plugins.ChestShop()); //Chest protection
2013-01-24 22:35:28 +01:00
registerPreShopCreationEvents();
registerPreTransactionEvents();
registerPostShopCreationEvents();
registerPostTransactionEvents();
registerEvent(new SignBreak());
2013-01-24 22:35:28 +01:00
registerEvent(new SignCreate());
2012-09-12 12:35:48 +02:00
registerEvent(new ChestBreak());
registerEvent(new BlockPlace());
registerEvent(new PlayerConnect());
registerEvent(new PlayerInteract());
2012-09-12 12:35:48 +02:00
registerEvent(new PlayerInventory());
registerEvent(new ItemInfoListener());
2013-01-24 22:35:28 +01:00
registerEvent(new RestrictedSign());
registerEvent(new ShopRefundListener());
2013-01-24 22:35:28 +01:00
registerEvent(new ShortNameSaver());
}
2013-01-24 22:35:28 +01:00
private void registerPreShopCreationEvents() {
if (Properties.BLOCK_SHOPS_WITH_SELL_PRICE_HIGHER_THAN_BUY_PRICE) {
registerEvent(new PriceRatioChecker());
}
2013-01-24 22:35:28 +01:00
registerEvent(new ChestChecker());
registerEvent(new ItemChecker());
registerEvent(new MoneyChecker());
registerEvent(new NameChecker());
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.PermissionChecker());
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.ErrorMessageSender());
registerEvent(new PriceChecker());
registerEvent(new QuantityChecker());
registerEvent(new TerrainChecker());
}
2012-08-10 19:00:20 +02:00
2013-01-24 22:35:28 +01:00
private void registerPostShopCreationEvents() {
registerEvent(new CreationFeeGetter());
registerEvent(new MessageSender());
registerEvent(new SignSticker());
}
private void registerPreTransactionEvents() {
if (Properties.ALLOW_PARTIAL_TRANSACTIONS) {
registerEvent(new PartialTransactionModule());
} else {
registerEvent(new AmountAndPriceChecker());
}
2013-01-24 22:35:28 +01:00
registerEvent(new CreativeModeIgnorer());
registerEvent(new DiscountModule());
registerEvent(new ErrorMessageSender());
registerEvent(new PermissionChecker());
registerEvent(new PriceValidator());
registerEvent(new ShopValidator());
registerEvent(new SpamClickProtector());
registerEvent(new StockFittingChecker());
}
private void registerPostTransactionEvents() {
registerEvent(new EconomicModule());
registerEvent(new EmptyShopDeleter());
registerEvent(new ItemManager());
registerEvent(new TransactionLogger());
registerEvent(new TransactionMessageSender());
}
public void registerEvent(Listener listener) {
getServer().getPluginManager().registerEvents(listener, this);
}
private void scheduleTask(Runnable runnable, long startTime, long repetetionTime) {
2012-12-19 18:49:13 +01:00
server.getScheduler().runTaskTimerAsynchronously(this, runnable, startTime, repetetionTime);
}
private void startStatistics() {
try {
new Metrics(this).start();
} catch (IOException ex) {
ChestShop.getBukkitLogger().severe("There was an error while submitting statistics.");
}
}
2011-05-29 13:25:25 +02:00
///////////////////// DATABASE STUFF ////////////////////////////////
private void setupDB() {
Database DB;
DB = new Database(this) {
protected java.util.List<Class<?>> getDatabaseClasses() {
List<Class<?>> list = new ArrayList<Class<?>>();
list.add(Transaction.class);
return list;
2011-05-29 13:25:25 +02:00
}
};
FileConfiguration config = YamlConfiguration.loadConfiguration(new File("bukkit.yml"));
DB.initializeDatabase(
config.getString("database.driver"),
config.getString("database.url"),
config.getString("database.username"),
config.getString("database.password"),
config.getString("database.isolation")
);
database = DB.getDatabase();
2012-12-19 18:49:13 +01:00
scheduleTask(new Queue(), 200L, 200L);
2011-05-29 13:25:25 +02:00
}
2011-05-29 13:25:25 +02:00
@Override
public EbeanServer getDatabase() {
return database;
2011-05-29 13:25:25 +02:00
}
2013-01-24 22:35:28 +01:00
2011-05-29 13:25:25 +02:00
///////////////////////////////////////////////////////////////////////////////
2013-01-24 22:35:28 +01:00
public static ItemDatabase getItemDatabase() {
return itemDatabase;
}
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-29 13:25:25 +02:00
public static String getVersion() {
return description.getVersion();
2011-05-29 13:25:25 +02:00
}
public static String getPluginName() {
return description.getName();
2011-05-29 13:25:25 +02:00
}
2011-06-09 22:54:01 +02:00
public static EbeanServer getDB() {
return database;
}
2012-11-04 21:09:38 +01:00
public static List<String> getDependencies() {
return description.getSoftDepend();
}
2013-01-24 22:35:28 +01:00
public static ChestShop getPlugin() {
return plugin;
}
public static void registerListener(Listener listener) {
plugin.registerEvent(listener);
}
public static void callEvent(Event event) {
Bukkit.getPluginManager().callEvent(event);
}
}