-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ae9fba
commit bf33dce
Showing
9 changed files
with
245 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
src/main/java/org/shanerx/tradeshoplog/listeners/LOGRestoreRegionEventListener.java
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/main/java/org/shanerx/tradeshoplog/listeners/SuccessfulTradeEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/shanerx/tradeshoplog/utils/logging/LoggerOutputType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/main/java/org/shanerx/tradeshoplog/utils/logging/TransactionLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.