diff --git a/pom.xml b/pom.xml index 70ea8a6..c730091 100644 --- a/pom.xml +++ b/pom.xml @@ -123,13 +123,6 @@ provided - - org.bstats - bstats-bukkit - 2.2.1 - compile - - com.github.Tradeshop TradeShop diff --git a/src/main/java/org/shanerx/tradeshoplog/TradeShopLOG.java b/src/main/java/org/shanerx/tradeshoplog/TradeShopLOG.java index b8210eb..a22e1e3 100644 --- a/src/main/java/org/shanerx/tradeshoplog/TradeShopLOG.java +++ b/src/main/java/org/shanerx/tradeshoplog/TradeShopLOG.java @@ -23,44 +23,41 @@ * */ -package org.shanerx.tradeshoparm; +package org.shanerx.tradeshoplog; -import org.bstats.bukkit.Metrics; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.shanerx.tradeshop.utils.Updater; -import org.shanerx.tradeshoparm.enumys.Setting; -import org.shanerx.tradeshoparm.listeners.*; +import org.shanerx.tradeshoplog.enumys.Setting; +import org.shanerx.tradeshoplog.listeners.*; +import org.shanerx.tradeshoplog.utils.logging.TransactionLogger; -public class TradeShopARM extends JavaPlugin { +public class TradeShopLOG extends JavaPlugin { - private final int bStatsPluginID = 12515; - private Metrics metrics; + private TransactionLogger transactionLogger; @Override public void onEnable() { Setting.reload(); + transactionLogger = new TransactionLogger(this); + PluginManager pm = getServer().getPluginManager(); - pm.registerEvents(new ARMRestoreRegionEventListener(this), this); + pm.registerEvents(new SuccessfulTradeEventListener(this), this); if (Setting.CHECK_UPDATES.getBoolean()) { new Thread(() -> getUpdater().checkCurrentVersion()).start(); } - if (Setting.ALLOW_METRICS.getBoolean()) { - metrics = new Metrics(this, bStatsPluginID); - getLogger().info("Metrics successfully initialized!"); - - } else { - getLogger().warning("Metrics are disabled! Please consider enabling them to support the authors!"); - } - } public Updater getUpdater() { - return new Updater(getDescription(), "https://raw.githubusercontent.com/Tradeshop/tradeshop-ARM/master/version.txt", "https://github.com/Tradeshop/tradeshop-ARM/releases"); + return new Updater(getDescription(), "https://raw.githubusercontent.com/Tradeshop/tradeshop-LOG/master/version.txt", "https://github.com/Tradeshop/tradeshop-LOG/releases"); + } + + public TransactionLogger getTransactionLogger() { + return transactionLogger; } } diff --git a/src/main/java/org/shanerx/tradeshoplog/enumys/Setting.java b/src/main/java/org/shanerx/tradeshoplog/enumys/Setting.java index 67c78ea..6d062d6 100644 --- a/src/main/java/org/shanerx/tradeshoplog/enumys/Setting.java +++ b/src/main/java/org/shanerx/tradeshoplog/enumys/Setting.java @@ -23,7 +23,7 @@ * */ -package org.shanerx.tradeshoparm.enumys; +package org.shanerx.tradeshoplog.enumys; import com.google.common.base.Charsets; import com.google.common.collect.Sets; @@ -32,7 +32,7 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.shanerx.tradeshoparm.TradeShopARM; +import org.shanerx.tradeshoplog.TradeShopLOG; import org.yaml.snakeyaml.Yaml; import org.shanerx.tradeshop.enumys.SettingSectionKeys; @@ -47,10 +47,14 @@ public enum Setting { ENABLE_DEBUG(SettingSectionKeys.SYSTEM_OPTIONS, "enable-debug", 0, "What debug code should be run. this will add significant amounts of spam to the console/log, generally not used unless requested by Devs (must be a whole number)"), CHECK_UPDATES(SettingSectionKeys.SYSTEM_OPTIONS, "check-updates", true, "Should we check for updates when the server starts"), - ALLOW_METRICS(SettingSectionKeys.SYSTEM_OPTIONS, "allow-metrics", true, "Allow us to connect anonymous metrics so we can see how our plugin is being used to better develop it"); + OUTPUT_TYPE(SettingSectionKeys.SYSTEM_OPTIONS, "output-type", "TSV", "How should the output file be formatted\n # Options: `TSV` - Tab Separated Value file"), + LOG_TIME_SEPARATION(SettingSectionKeys.SYSTEM_OPTIONS, "log-time-separation", "H", "How often should we create new log files.\n # Options: `M` - Month, `d` - Day, `H` - Hour, `m` - Minute, `s` - Second"), + TRANSACTION_LOG_FORMAT(SettingSectionKeys.SYSTEM_OPTIONS, "transaction-log-format", "%Date_@_%Time_@_%ShopType_@_%Owner_@_%TradingPlayer_@_%World_@_%X_@_%Y_@_%Z_@_%CostList_@_%ProductList" + , "How should the output entries be formatted, each value must be seperated by `_@_`\n # Changes to this may cause formatting errors in the current output log, please remove old output logs once you have changed this." + + "\n # Values: \n # %Date \n # %Time \n # %ShopType \n # %Owner \n # %TradingPlayer \n # %ShopLocation \n # %World \n # %X \n # %Y \n # %Z \n # %CostList \n # %ProductList"); - private static TradeShopARM plugin = (TradeShopARM) Bukkit.getPluginManager().getPlugin("TradeShop-ARM"); + private static TradeShopLOG plugin = (TradeShopLOG) Bukkit.getPluginManager().getPlugin("TradeShop-LOG"); private static File file = new File(plugin.getDataFolder(), "config.yml"); private static FileConfiguration config = YamlConfiguration.loadConfiguration(file); private final String key; @@ -112,7 +116,7 @@ private static void save() { StringBuilder data = new StringBuilder(); - data.append("##########################\n").append("# TradeShopARM Config #\n").append("##########################\n"); + data.append("##########################\n").append("# TradeShopLOG Config #\n").append("##########################\n"); Set settingSectionKeys = Sets.newHashSet(SettingSectionKeys.values()); for (Setting setting : values()) { diff --git a/src/main/java/org/shanerx/tradeshoplog/listeners/LOGRestoreRegionEventListener.java b/src/main/java/org/shanerx/tradeshoplog/listeners/LOGRestoreRegionEventListener.java deleted file mode 100644 index ea0bb08..0000000 --- a/src/main/java/org/shanerx/tradeshoplog/listeners/LOGRestoreRegionEventListener.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * - * Copyright (c) 2016-2019 - * SparklingComet @ http://shanerx.org - * KillerOfPie @ http://killerofpie.github.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * NOTICE: All modifications made by others to the source code belong - * to the respective contributor. No contributor should be held liable for - * any damages of any kind, whether be material or moral, which were - * caused by their contribution(s) to the project. See the full License for more information. - * - */ - -package org.shanerx.tradeshoparm.listeners; - -import net.alex9849.arm.regions.Region; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.util.Vector; -import org.shanerx.tradeshop.TradeShop; -import org.shanerx.tradeshop.objects.Shop; -import org.shanerx.tradeshop.objects.ShopLocation; -import org.shanerx.tradeshop.utils.data.DataStorage; -import org.shanerx.tradeshoparm.TradeShopARM; -import net.alex9849.arm.events.RestoreRegionEvent; - - -public class ARMRestoreRegionEventListener implements Listener { - - private final TradeShop tradeShop = (TradeShop) Bukkit.getPluginManager().getPlugin("TradeShop"); - private final TradeShopARM tradeShopARM; - - public ARMRestoreRegionEventListener(TradeShopARM tradeShopARM) { - this.tradeShopARM = tradeShopARM; - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onRegionRestore(RestoreRegionEvent event) { - if(!event.isCancelled()) { - DataStorage dataStorage = tradeShop.getDataStorage(); - Region region = event.getRegion(); - World world = region.getRegionworld(); - - for (Vector point : region.getRegion().getPoints()) { - ShopLocation sl = new ShopLocation(world, point.getBlockX(), point.getBlockY(), point.getBlockZ()); - Shop shop = dataStorage.loadShopFromSign(sl); - if (shop != null) - shop.remove(); - dataStorage.removeChestLinkage(sl); - } - } - } -} diff --git a/src/main/java/org/shanerx/tradeshoplog/listeners/SuccessfulTradeEventListener.java b/src/main/java/org/shanerx/tradeshoplog/listeners/SuccessfulTradeEventListener.java new file mode 100644 index 0000000..79b7f53 --- /dev/null +++ b/src/main/java/org/shanerx/tradeshoplog/listeners/SuccessfulTradeEventListener.java @@ -0,0 +1,49 @@ +/* + * + * Copyright (c) 2016-2019 + * SparklingComet @ http://shanerx.org + * KillerOfPie @ http://killerofpie.github.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * NOTICE: All modifications made by others to the source code belong + * to the respective contributor. No contributor should be held liable for + * any damages of any kind, whether be material or moral, which were + * caused by their contribution(s) to the project. See the full License for more information. + * + */ + +package org.shanerx.tradeshoplog.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.shanerx.tradeshop.framework.events.SuccessfulTradeEvent; +import org.shanerx.tradeshoplog.TradeShopLOG; + + +public class SuccessfulTradeEventListener implements Listener { + + private final TradeShopLOG tradeShopLOG; + + public SuccessfulTradeEventListener(TradeShopLOG tradeShopARM) { + this.tradeShopLOG = tradeShopARM; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onSuccessfulTrade(SuccessfulTradeEvent event) { + if(!event.isCancelled()) { + tradeShopLOG.getTransactionLogger().logTransaction(event); + } + } +} diff --git a/src/main/java/org/shanerx/tradeshoplog/utils/logging/LoggerOutputType.java b/src/main/java/org/shanerx/tradeshoplog/utils/logging/LoggerOutputType.java new file mode 100644 index 0000000..8d6a480 --- /dev/null +++ b/src/main/java/org/shanerx/tradeshoplog/utils/logging/LoggerOutputType.java @@ -0,0 +1,23 @@ +package org.shanerx.tradeshoplog.utils.logging; + +public enum LoggerOutputType { + + + + TSV("tsv", "\t"); + + private final String fileExtension, delimiter; + + LoggerOutputType(String fileExtension, String delimiter) { + this.fileExtension = fileExtension; + this.delimiter = delimiter; + } + + public String getFileExtension() { + return fileExtension; + } + + public String getDelimiter() { + return delimiter; + } +} diff --git a/src/main/java/org/shanerx/tradeshoplog/utils/logging/TransactionLogger.java b/src/main/java/org/shanerx/tradeshoplog/utils/logging/TransactionLogger.java new file mode 100644 index 0000000..c79c264 --- /dev/null +++ b/src/main/java/org/shanerx/tradeshoplog/utils/logging/TransactionLogger.java @@ -0,0 +1,126 @@ +package org.shanerx.tradeshoplog.utils.logging; + +import com.google.common.io.Files; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import org.shanerx.tradeshop.framework.events.SuccessfulTradeEvent; +import org.shanerx.tradeshop.objects.Shop; +import org.shanerx.tradeshop.objects.ShopItemStack; +import org.shanerx.tradeshoplog.TradeShopLOG; +import org.shanerx.tradeshoplog.enumys.Setting; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +public class TransactionLogger { + + private final TradeShopLOG tradeShopLOG; + private LoggerOutputType loggerOutputType; + private boolean newFile = false; + + public TransactionLogger(TradeShopLOG tradeShopLOG) { + this.tradeShopLOG = tradeShopLOG; + + try { + loggerOutputType = LoggerOutputType.valueOf(Setting.OUTPUT_TYPE.getString()); + } catch (IllegalArgumentException | NullPointerException ex) { + loggerOutputType = LoggerOutputType.TSV; + } + } + + private PrintWriter buildWriter() { + File file = new File(tradeShopLOG.getDataFolder(), "TransactionLogs" + File.separator + "TradeshopTransactionLog-" + getFileTimeString() + "." + loggerOutputType.getFileExtension()); + + try { + Files.createParentDirs(file); + + if (!file.exists()) { + file.createNewFile(); + newFile = true; + } + + FileWriter fileWriter = new FileWriter(file, true); + return new PrintWriter(fileWriter); + + } catch (IOException ex) { + ex.printStackTrace(); + } + + return null; + } + + public void logTransaction(SuccessfulTradeEvent event) { + PrintWriter printWriter = buildWriter(); + assert printWriter != null; + Shop shop = event.getShop(); + String localFormat = Setting.TRANSACTION_LOG_FORMAT.getString().replaceAll("_@_", loggerOutputType.getDelimiter()); + + if(newFile) { + printWriter.println(localFormat.replaceAll("%", "")); + newFile = false; + } + printWriter.println(localFormat + .replaceAll("%Date", getTransactionDate()) + .replaceAll("%Time", getTransactionTime()) + .replaceAll("%ShopType", shop.getShopType().toString()) + .replaceAll("%Owner", shop.getOwner().getName()) + .replaceAll("%TradingPlayer", event.getPlayer().getName()) + .replaceAll("%ShopLocation", shop.getShopLocationAsSL().toString()) + .replaceAll("%World", shop.getShopLocationAsSL().getWorldName()) + .replaceAll("%X", shop.getShopLocationAsSL().getX() + "") + .replaceAll("%Y", shop.getShopLocationAsSL().getY() + "") + .replaceAll("%Z", shop.getShopLocationAsSL().getZ() + "") + .replaceAll("%CostList", "\"" + getItemListAsString(event.getCost()) + "\"") + .replaceAll("%ProductList", "\"" + getItemListAsString(event.getProduct())) + "\""); + printWriter.close(); + } + + private String getItemListAsString(List itemList) { + final Gson gson = new GsonBuilder().serializeNulls().create(); + JsonObject jsonObj = new JsonObject(); + for (int i = 0; i < itemList.size(); i++) { + jsonObj.add(i + "", gson.toJsonTree(itemList.get(i).getItemStack())); + } + + return gson.toJson(jsonObj); + } + + private String getFileTimeString() { + String dateFormat = "yyyy"; + switch(Setting.LOG_TIME_SEPARATION.getString()) { + case "M": + dateFormat += "-MM"; + break; + case "d": + dateFormat += "-MM-dd"; + break; + case "m": + dateFormat += "-MM-dd'T'HH-mm"; + break; + case "s": + dateFormat += "-MM-dd'T'HH-mm-ss"; + break; + default: + case "H": + dateFormat += "-MM-dd'T'HH"; + break; + } + + return new SimpleDateFormat(dateFormat).format(new Date()); + } + + private String getTransactionTime() { + return new SimpleDateFormat("HH-mm-ss").format(new Date()); + } + + private String getTransactionDate() { + return new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c179d6b..088b909 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: TradeShop-LOG version: ${project.version} -main: org.shanerx.tradeshoparm.TradeShopLOG +main: org.shanerx.tradeshoplog.TradeShopLOG api-version: 1.14 load: POSTWORLD depend: [TradeShop] diff --git a/tradeshop-log.iml b/tradeshop-log.iml index 9560b66..0f389a8 100644 --- a/tradeshop-log.iml +++ b/tradeshop-log.iml @@ -1,36 +1,24 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file