ShopChest/src/de/epiceric/shopchest/ShopChest.java

299 lines
8.5 KiB
Java
Raw Normal View History

2015-09-02 13:06:48 +02:00
package de.epiceric.shopchest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2015-09-06 11:58:25 +02:00
import java.sql.Statement;
2015-09-02 13:06:48 +02:00
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
2015-09-04 12:31:19 +02:00
import org.bukkit.plugin.Plugin;
2015-09-02 13:06:48 +02:00
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
2015-09-04 12:31:19 +02:00
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.event.InteractShop;
import de.epiceric.shopchest.event.ItemCustomNameListener;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.event.NotifyUpdate;
import de.epiceric.shopchest.event.ProtectChest;
import de.epiceric.shopchest.event.RegenerateShopItem;
2015-09-02 16:18:32 +02:00
import de.epiceric.shopchest.event.RegenerateShopItemAfterRemove;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.event.UpdateHolograms;
import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.interfaces.utils.*;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType;
2015-09-06 11:58:25 +02:00
import de.epiceric.shopchest.sql.SQLite;
import de.epiceric.shopchest.interfaces.JsonBuilder;
import de.epiceric.shopchest.interfaces.jsonbuilder.*;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.utils.Metrics;
import de.epiceric.shopchest.utils.Metrics.Graph;
import de.epiceric.shopchest.utils.Metrics.Plotter;
2015-09-02 13:06:48 +02:00
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
public class ShopChest extends JavaPlugin{
private static ShopChest instance;
private static UpdateChecker uc;
2015-09-02 13:06:48 +02:00
2015-09-06 11:58:25 +02:00
public static Statement statement;
public static Logger logger;
2015-09-02 13:06:48 +02:00
public static Economy econ = null;
public static Permission perm = null;
2015-09-04 12:31:19 +02:00
public static LWC lwc = null;
public static boolean lockette = false;
2015-09-06 11:58:25 +02:00
public static SQLite sqlite;
2015-09-02 13:06:48 +02:00
public static boolean isUpdateNeeded = false;
public static String latestVersion = "";
public static String downloadLink = "";
2016-05-05 18:09:51 +02:00
public static String broadcast = null;
2015-09-02 13:06:48 +02:00
public static Utils utils;
2015-09-02 13:06:48 +02:00
public static ShopChest getInstance() {
return instance;
}
private boolean setupEconomy() {
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = rsp.getProvider();
return econ != null;
}
private boolean setupPermissions() {
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
perm = rsp.getProvider();
return perm != null;
}
2015-09-08 12:59:34 +02:00
2015-09-02 13:06:48 +02:00
@Override
public void onEnable() {
2015-09-02 13:06:48 +02:00
2015-09-06 11:58:25 +02:00
logger = getLogger();
2015-09-02 13:06:48 +02:00
if (getServer().getPluginManager().getPlugin("Vault") == null) {
2015-09-06 11:58:25 +02:00
logger.severe("Could not find plugin 'Vault'!");
2015-09-02 13:06:48 +02:00
getServer().getPluginManager().disablePlugin(this);
return;
}
if (!setupEconomy() ) {
2015-09-06 11:58:25 +02:00
logger.severe("Could not find any Vault dependency!");
2015-09-02 13:06:48 +02:00
getServer().getPluginManager().disablePlugin(this);
return;
}
try {
Metrics metrics = new Metrics(this);
Graph shopType = metrics.createGraph("Shop Type");
shopType.addPlotter(new Plotter("Infinite") {
@Override
public int getValue() {
int value = 0;
for (Shop shop : ShopUtils.getShops()) {
if (shop.getShopType() == ShopType.INFINITE) value++;
}
return value;
}
});
shopType.addPlotter(new Plotter("Normal") {
@Override
public int getValue() {
int value = 0;
for (Shop shop : ShopUtils.getShops()) {
if (shop.getShopType() == ShopType.NORMAL) value++;
}
return value;
}
});
shopType.addPlotter(new Plotter("Admin") {
@Override
public int getValue() {
int value = 0;
for (Shop shop : ShopUtils.getShops()) {
if (shop.getShopType() == ShopType.ADMIN) value++;
}
return value;
}
});
2015-09-02 13:06:48 +02:00
metrics.start();
} catch (IOException e) {
2015-09-06 11:58:25 +02:00
logger.severe("Could not submit stats.");
2015-09-02 13:06:48 +02:00
}
2015-09-06 11:58:25 +02:00
2015-09-08 12:59:34 +02:00
reloadConfig();
saveDefaultConfig();
2015-09-06 11:58:25 +02:00
sqlite = new SQLite(this);
sqlite.load();
switch (Utils.getVersion(getServer())) {
case "v1_8_R1": utils = new Utils_1_8_R1(); break;
case "v1_8_R2": utils = new Utils_1_8_R2(); break;
case "v1_8_R3": utils = new Utils_1_8_R3(); break;
case "v1_9_R1": utils = new Utils_1_9_R1(); break;
default:
logger.severe("Incompatible Server Version: " + Utils.getVersion(getServer()) + "!");
getServer().getPluginManager().disablePlugin(this);
return;
}
2015-09-04 12:31:19 +02:00
if (getServer().getPluginManager().getPlugin("LWC") != null) {
Plugin lwcp = getServer().getPluginManager().getPlugin("LWC");
lwc = ((LWCPlugin) lwcp).getLWC();
} else {
lwc = null;
}
if (getServer().getPluginManager().getPlugin("Lockette") != null) {
lockette = true;
} else {
lockette = false;
}
2015-09-02 13:06:48 +02:00
setupPermissions();
instance = this;
2015-09-08 12:59:34 +02:00
if (uc == null) uc = new UpdateChecker(this, getDescription().getWebsite());
2015-09-06 11:58:25 +02:00
logger.info("Checking for Updates");
2016-05-05 18:09:51 +02:00
if(uc.updateNeeded(Bukkit.getConsoleSender())) {
2015-09-02 13:06:48 +02:00
latestVersion = uc.getVersion();
downloadLink = uc.getLink();
2016-05-05 18:09:51 +02:00
broadcast = uc.getBroadcast();
2015-09-02 13:06:48 +02:00
isUpdateNeeded = true;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + ChatColor.GOLD + "New version available: " + ChatColor.RED + latestVersion);
2016-05-05 18:09:51 +02:00
if (broadcast != null && Config.enable_broadcast()) Bukkit.getConsoleSender().sendMessage("[ShopChest] " + broadcast);
2015-09-02 13:06:48 +02:00
} else {
2015-09-06 11:58:25 +02:00
logger.info("No new version available");
2015-09-02 13:06:48 +02:00
isUpdateNeeded = false;
}
if (isUpdateNeeded) {
for (Player p : getServer().getOnlinePlayers()) {
if (p.isOp() || perm.has(p, "shopchest.notification.update")) {
JsonBuilder jb;
switch (Utils.getVersion(getServer())) {
case "v1_8_R1": jb = new JsonBuilder_1_8_R1(Config.update_available(latestVersion)); break;
case "v1_8_R2": jb = new JsonBuilder_1_8_R2(Config.update_available(latestVersion)); break;
case "v1_8_R3": jb = new JsonBuilder_1_8_R3(Config.update_available(latestVersion)); break;
case "v1_9_R1": jb = new JsonBuilder_1_9_R1(Config.update_available(latestVersion)); break;
default: return;
2016-05-05 18:09:51 +02:00
}
2015-09-02 13:06:48 +02:00
jb.sendJson(p);
2016-05-05 18:09:51 +02:00
if (broadcast != null && Config.enable_broadcast()) p.sendMessage(broadcast);
2015-09-02 13:06:48 +02:00
}
}
2016-05-05 18:09:51 +02:00
}
2015-09-02 13:06:48 +02:00
File itemNamesFile = new File(getDataFolder(), "item_names.txt");
if (!itemNamesFile.exists())
try {itemNamesFile.createNewFile();} catch (IOException e) {e.printStackTrace();}
copy(getResource("item_names.txt"), itemNamesFile);
2015-09-06 11:58:25 +02:00
2015-09-02 13:06:48 +02:00
try {
Commands.registerCommand(new Commands(this, Config.main_command_name(), "Manage Shops.", "", new ArrayList<String>()), this);
} catch (Exception e) {
e.printStackTrace();
}
initializeShops();
getServer().getPluginManager().registerEvents(new UpdateHolograms(), this);
getServer().getPluginManager().registerEvents(new RegenerateShopItem(), this);
getServer().getPluginManager().registerEvents(new InteractShop(this), this);
getServer().getPluginManager().registerEvents(new NotifyUpdate(), this);
getServer().getPluginManager().registerEvents(new ProtectChest(), this);
getServer().getPluginManager().registerEvents(new ItemCustomNameListener(), this);
2015-09-02 16:18:32 +02:00
if (getServer().getPluginManager().getPlugin("ClearLag") != null) getServer().getPluginManager().registerEvents(new RegenerateShopItemAfterRemove(), this);
2015-09-02 13:06:48 +02:00
}
@Override
public void onDisable() {
utils.removeShops();
2015-09-02 13:06:48 +02:00
}
private void initializeShops() {
2015-09-06 11:58:25 +02:00
int count = 0;
2015-09-02 13:06:48 +02:00
2015-09-08 12:59:34 +02:00
for (int id = 1; id < sqlite.getHighestID() + 1; id++) {
2015-09-02 13:06:48 +02:00
2015-09-06 11:58:25 +02:00
try {
Shop shop = sqlite.getShop(id);
shop.createHologram();
shop.createItem();
ShopUtils.addShop(shop);
} catch (NullPointerException e) {
continue;
}
2015-09-02 13:06:48 +02:00
2015-09-06 11:58:25 +02:00
count++;
2015-09-02 13:06:48 +02:00
}
2015-09-06 11:58:25 +02:00
logger.info("Initialized " + String.valueOf(count) + " Shops");
2015-09-02 13:06:48 +02:00
}
public static void copy(InputStream in, File file) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}