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;
|
2015-09-09 12:27:23 +02:00
|
|
|
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;
|
2015-09-03 20:51:30 +02:00
|
|
|
import de.epiceric.shopchest.interfaces.Utils;
|
2016-04-08 14:52:11 +02:00
|
|
|
import de.epiceric.shopchest.interfaces.utils.*;
|
2015-09-02 13:06:48 +02:00
|
|
|
import de.epiceric.shopchest.shop.Shop;
|
2016-04-13 17:30:34 +02:00
|
|
|
import de.epiceric.shopchest.shop.Shop.ShopType;
|
2015-09-06 11:58:25 +02:00
|
|
|
import de.epiceric.shopchest.sql.SQLite;
|
2015-09-03 20:51:30 +02:00
|
|
|
import de.epiceric.shopchest.interfaces.JsonBuilder;
|
2016-04-08 14:52:11 +02:00
|
|
|
import de.epiceric.shopchest.interfaces.jsonbuilder.*;
|
2015-09-02 13:06:48 +02:00
|
|
|
import de.epiceric.shopchest.utils.Metrics;
|
2015-09-16 21:16:21 +02:00
|
|
|
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;
|
2016-04-09 16:34:26 +02:00
|
|
|
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
|
|
|
|
2015-09-03 20:51:30 +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
|
2016-04-09 16:34:26 +02:00
|
|
|
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);
|
2015-09-16 21:16:21 +02:00
|
|
|
Graph shopType = metrics.createGraph("Shop Type");
|
|
|
|
|
|
|
|
shopType.addPlotter(new Plotter("Infinite") {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getValue() {
|
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
for (Shop shop : ShopUtils.getShops()) {
|
2016-04-13 17:30:34 +02:00
|
|
|
if (shop.getShopType() == ShopType.INFINITE) value++;
|
2015-09-16 21:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
shopType.addPlotter(new Plotter("Normal") {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getValue() {
|
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
for (Shop shop : ShopUtils.getShops()) {
|
2016-04-13 17:30:34 +02:00
|
|
|
if (shop.getShopType() == ShopType.NORMAL) value++;
|
2015-09-16 21:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-13 17:30:34 +02:00
|
|
|
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();
|
|
|
|
|
2015-09-03 20:51:30 +02:00
|
|
|
switch (Utils.getVersion(getServer())) {
|
|
|
|
|
2016-04-08 14:52:11 +02:00
|
|
|
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;
|
2015-09-03 20:51:30 +02:00
|
|
|
default:
|
2016-04-08 14:52:11 +02:00
|
|
|
logger.severe("Incompatible Server Version: " + Utils.getVersion(getServer()) + "!");
|
2015-09-03 20:51:30 +02:00
|
|
|
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
|
|
|
|
2016-04-09 16:34:26 +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")) {
|
2015-09-03 20:51:30 +02:00
|
|
|
JsonBuilder jb;
|
|
|
|
switch (Utils.getVersion(getServer())) {
|
2016-04-09 16:34:26 +02:00
|
|
|
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;
|
2015-09-03 20:51:30 +02:00
|
|
|
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();}
|
|
|
|
|
2015-09-04 20:16:33 +02:00
|
|
|
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);
|
2015-09-09 12:27:23 +02:00
|
|
|
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() {
|
|
|
|
|
2015-09-03 20:51:30 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|