Skip to content

Commit

Permalink
First full commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KillerOfPie committed Aug 26, 2021
1 parent 5ae9fba commit bf33dce
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 133 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.github.Tradeshop</groupId>
<artifactId>TradeShop</artifactId>
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/org/shanerx/tradeshoplog/TradeShopLOG.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 9 additions & 5 deletions src/main/java/org/shanerx/tradeshoplog/enumys/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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> settingSectionKeys = Sets.newHashSet(SettingSectionKeys.values());

for (Setting setting : values()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ShopItemStack> 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());
}

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Loading

0 comments on commit bf33dce

Please sign in to comment.