2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
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;
|
2012-11-23 21:04:13 +01:00
|
|
|
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;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.BlockPlace;
|
2012-09-12 12:35:48 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.Break.ChestBreak;
|
2012-10-16 17:03:45 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
|
2012-06-08 15:28:36 +02:00
|
|
|
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;
|
2012-09-12 12:35:48 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Player.ShortNameSaver;
|
2012-10-16 17:03:45 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.PostTransaction.*;
|
2012-08-10 21:44:32 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.PreTransaction.*;
|
2012-10-16 17:03:45 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.ShopRefundListener;
|
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;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
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-11-23 21:04:13 +01:00
|
|
|
import com.nijikokun.register.payment.forChestShop.Methods;
|
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-11-23 21:04:13 +01:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
2012-11-25 23:34:21 +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.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
|
|
|
|
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
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
private static EbeanServer database;
|
2011-07-23 21:00:47 +02:00
|
|
|
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 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();
|
|
|
|
dataFolder = getDataFolder();
|
|
|
|
description = getDescription();
|
|
|
|
server = getServer();
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
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);
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
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-11-23 21:04:13 +01:00
|
|
|
if (Properties.LOG_TO_DATABASE || Properties.GENERATE_STATISTICS_PAGE) {
|
2012-05-10 16:32:25 +02:00
|
|
|
setupDB();
|
|
|
|
}
|
2012-11-23 21:04:13 +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-05-10 16:32:25 +02:00
|
|
|
}
|
2012-11-23 21:04:13 +01:00
|
|
|
if (Properties.LOG_TO_FILE) {
|
|
|
|
File log = loadFile("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);
|
|
|
|
}
|
2012-11-23 21:04:13 +01:00
|
|
|
if (!Properties.LOG_TO_CONSOLE) {
|
2012-05-10 16:32:25 +02:00
|
|
|
logger.setUseParentHandlers(false);
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
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
|
|
|
startStatistics();
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
public static File loadFile(String string) {
|
|
|
|
File file = new File(dataFolder, string);
|
|
|
|
|
|
|
|
return loadFile(file);
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private static File loadFile(File file) {
|
|
|
|
if (!file.exists()) {
|
|
|
|
try {
|
2012-11-25 23:34:21 +01:00
|
|
|
file.getParentFile().mkdirs();
|
2012-05-10 16:32:25 +02:00
|
|
|
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-10-16 17:03:45 +02:00
|
|
|
registerEvent(new SignBreak());
|
2012-09-12 12:35:48 +02:00
|
|
|
registerEvent(new ChestBreak());
|
2012-10-16 17:03:45 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
registerEvent(new BlockPlace());
|
|
|
|
registerEvent(new SignChange());
|
2012-06-08 15:28:36 +02:00
|
|
|
registerEvent(new PlayerConnect());
|
2012-08-10 18:56:16 +02:00
|
|
|
registerEvent(new PlayerInteract());
|
2012-09-12 12:35:48 +02:00
|
|
|
registerEvent(new PlayerInventory());
|
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());
|
2012-11-23 21:04:13 +01:00
|
|
|
registerEvent(new SpamClickProtector());
|
2012-08-10 18:56:16 +02:00
|
|
|
|
2012-08-10 19:00:20 +02:00
|
|
|
registerEvent(new RestrictedSign());
|
2012-08-24 10:24:20 +02:00
|
|
|
registerEvent(new DiscountModule());
|
2012-10-16 17:03:45 +02:00
|
|
|
registerEvent(new ShopRefundListener());
|
2012-08-10 19:00:20 +02:00
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
if (Properties.ALLOW_PARTIAL_TRANSACTIONS) {
|
2012-08-10 18:56:16 +02:00
|
|
|
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 ////////////////////////////////
|
2011-07-23 21:00:47 +02:00
|
|
|
private void setupDB() {
|
2012-11-23 21:04:13 +01:00
|
|
|
Database DB;
|
|
|
|
|
|
|
|
DB = new Database(this) {
|
2011-07-23 21:00:47 +02:00
|
|
|
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-11-25 23:34:21 +01:00
|
|
|
FileConfiguration config = YamlConfiguration.loadConfiguration(new File("bukkit.yml"));
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
DB.initializeDatabase(
|
2011-07-23 21:00:47 +02:00
|
|
|
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
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
database = DB.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() {
|
2012-11-23 21:04:13 +01:00
|
|
|
return database;
|
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() {
|
2012-11-23 21:04:13 +01:00
|
|
|
return database;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2012-11-04 21:09:38 +01:00
|
|
|
public static List<String> 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) {
|
2012-11-23 21:04:13 +01:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|