-
-
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.
1 parent
35f1a54
commit 6df60e6
Showing
9 changed files
with
270 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package borg.locutus.moneyshot; | ||
|
||
import net.labymod.main.LabyMod; | ||
|
||
public class MessageUtils { | ||
public static void displayLocalMessage(String message) { | ||
LabyMod.getInstance().displayMessageInChat(message); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,85 @@ | ||
package borg.locutus.moneyshot; | ||
|
||
import borg.locutus.moneyshot.payments.IncomingTransactionListener; | ||
import borg.locutus.moneyshot.payments.OutgoingTransactionListener; | ||
import borg.locutus.moneyshot.scheduling.SyncTickScheduler; | ||
import net.labymod.api.LabyModAddon; | ||
import net.labymod.settings.elements.SettingsElement; | ||
import net.labymod.settings.elements.*; | ||
import net.labymod.utils.Consumer; | ||
import net.labymod.utils.Material; | ||
import net.minecraft.client.Minecraft; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class MoneyShot extends LabyModAddon { | ||
public static boolean incomingPayments = true; | ||
public static boolean outgoingPayments = true; | ||
public static int daysOfSaving = 7; | ||
public static String pathOfSaveFolder = ""; | ||
|
||
@Override | ||
public void onEnable() { | ||
getApi().getEventManager().register(new IncomingTransactionListener()); | ||
getApi().getEventManager().register(new OutgoingTransactionListener()); | ||
|
||
getApi().registerForgeListener(new SyncTickScheduler()); | ||
|
||
SyncTickScheduler.scheduleNewSyncTask(new Runnable() { | ||
@Override | ||
public void run() { | ||
deleteOldFiles(); | ||
} | ||
}, 200); | ||
} | ||
|
||
@Override | ||
public void loadConfig() { | ||
|
||
incomingPayments = !getConfig().has("incomingPayments") || getConfig().get("incomingPayments").getAsBoolean(); | ||
outgoingPayments = !getConfig().has("outgoingPayments") || getConfig().get("outgoingPayments").getAsBoolean(); | ||
daysOfSaving = getConfig().has( "daysOfSaving" ) ? getConfig().get( "daysOfSaving" ).getAsInt() : 7; | ||
pathOfSaveFolder = getConfig().has( "pathOfSaveFolder" ) ? getConfig().get( "pathOfSaveFolder" ).getAsString(): ""; | ||
} | ||
|
||
@Override | ||
protected void fillSettings(List<SettingsElement> list) { | ||
protected void fillSettings(List<SettingsElement> subSettings) { | ||
subSettings.add( new HeaderElement("Protokoll-Einstellungen")); | ||
subSettings.add( new BooleanElement( "Eingehende Zahlungen protokollieren", this, new ControlElement.IconData( Material.LEVER ), "incomingPayments", incomingPayments ) ); | ||
subSettings.add( new BooleanElement( "Ausgehende Zahlungen protokollieren", this, new ControlElement.IconData( Material.LEVER ), "outgoingPayments", outgoingPayments ) ); | ||
|
||
subSettings.add( new HeaderElement("Dateispeicherung")); | ||
subSettings.add( new SliderElement( "Speicherdauer in Tagen", this, new ControlElement.IconData( Material.ITEM_FRAME ), "daysOfSaving", daysOfSaving ).setRange( 1, 31 ) ); | ||
|
||
StringElement channelStringElement = new StringElement( "Dateispeicherpfad", this, new ControlElement.IconData( Material.PAPER ), "pathOfSaveFolder", pathOfSaveFolder); | ||
subSettings.add( channelStringElement ); | ||
subSettings.add( new HeaderElement("Pfad leer lassen, damit der Standard-Ordner von Minecraft verwendet wird")); | ||
} | ||
|
||
private void deleteOldFiles() { | ||
File dir = pathOfSaveFolder.equals("") ? new File(Minecraft.getMinecraft().mcDataDir, "screenshots") : new File(pathOfSaveFolder, "screenshots"); | ||
File[] directoryListing = dir.listFiles(); | ||
if (directoryListing != null) { | ||
for (File child : directoryListing) { | ||
if (child.isDirectory()) continue; | ||
if (!child.getName().contains("dispensing") && !child.getName().contains("received")) continue; | ||
|
||
long lastModified = child.lastModified(); | ||
long currentTime = System.currentTimeMillis(); | ||
|
||
long timePassed = currentTime - lastModified; | ||
long days = TimeUnit.MILLISECONDS.toDays(timePassed); | ||
|
||
if (days >= daysOfSaving) { | ||
if(child.delete()) { | ||
System.out.println("File deleted successfully"); | ||
} else { | ||
System.out.println("Failed to delete the file"); | ||
} | ||
} | ||
} | ||
} else { | ||
throw new RuntimeException("Der in der Config angegebene Pfad ist kein Ordner"); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/borg/locutus/moneyshot/payments/IncomingTransactionListener.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,37 @@ | ||
package borg.locutus.moneyshot.payments; | ||
|
||
|
||
import borg.locutus.moneyshot.MoneyShot; | ||
import borg.locutus.moneyshot.screenshot.ScreenshotCreator; | ||
import net.labymod.api.events.MessageReceiveEvent; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class IncomingTransactionListener implements MessageReceiveEvent { | ||
public boolean onReceive(String message, String unformattedMessage) { | ||
if (!MoneyShot.incomingPayments) { | ||
return false; | ||
} | ||
|
||
if (!checkIfMessageIdValidPayment(message)) { | ||
return false; | ||
} | ||
|
||
String[] args = unformattedMessage.split(" "); | ||
String playername = args[2]; | ||
String money = args[5].substring(1).replace(",", ""); | ||
money = StringUtils.substringBefore(money, "."); | ||
int moneyint = Integer.parseInt(money); | ||
if (moneyint == 0) | ||
return false; | ||
|
||
ScreenshotCreator.takeScreenshotOfValidPayment(playername, moneyint, "received"); | ||
|
||
return false; | ||
} | ||
|
||
private boolean checkIfMessageIdValidPayment(String message) { | ||
return message.startsWith("§r") && message.endsWith("§r") && message.contains("§a") && | ||
!message.contains(":") && message.contains("hat dir") && message.contains("gegeben") && message | ||
.contains("$") && !message.split(" ")[2].endsWith("§f"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/borg/locutus/moneyshot/payments/OutgoingTransactionListener.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,38 @@ | ||
package borg.locutus.moneyshot.payments; | ||
|
||
import borg.locutus.moneyshot.MoneyShot; | ||
import borg.locutus.moneyshot.screenshot.ScreenshotCreator; | ||
import net.labymod.api.events.MessageReceiveEvent; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class OutgoingTransactionListener implements MessageReceiveEvent { | ||
public boolean onReceive(String message, String unformattedMessage) { | ||
if (!MoneyShot.outgoingPayments) { | ||
return false; | ||
} | ||
|
||
if (!checkIfMessageIdValidPayment(message)) { | ||
return false; | ||
} | ||
|
||
String[] args = unformattedMessage.split(" "); | ||
String playername = args[4]; | ||
String money = args[5].substring(1).replace(",", ""); | ||
money = StringUtils.substringBefore(money, "."); | ||
int moneyint = Integer.parseInt(money); | ||
if (moneyint == 0) | ||
return false; | ||
|
||
ScreenshotCreator.takeScreenshotOfValidPayment(playername, moneyint, "dispensing"); | ||
|
||
return false; | ||
} | ||
|
||
private boolean checkIfMessageIdValidPayment(String message) { | ||
|
||
//§r§aDu hast §r§bDeveloper§r§8 ┃ §r§bLocutusVonBorg§r§a $1 gegeben.§r | ||
return message.startsWith("§r§aDu hast") && message.endsWith("§r") && message.contains("§a") && | ||
!message.contains(":") && message.contains("gegeben") && message | ||
.contains("$") && !message.split(" ")[2].endsWith("§f"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/borg/locutus/moneyshot/scheduling/SyncTask.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 borg.locutus.moneyshot.scheduling; | ||
|
||
public class SyncTask { | ||
private final long creationTime = System.currentTimeMillis(); | ||
private final int tickDelay; | ||
private final Runnable runnable; | ||
|
||
public SyncTask(int tickDelay, Runnable runnable) { | ||
this.tickDelay = tickDelay; | ||
this.runnable = runnable; | ||
} | ||
|
||
public void runTask() { | ||
runnable.run(); | ||
} | ||
|
||
public boolean waitTimeExceeded() { | ||
long delayInMillis = tickDelay * 50; | ||
long currentTime = System.currentTimeMillis(); | ||
|
||
return (creationTime + delayInMillis <= currentTime); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/borg/locutus/moneyshot/scheduling/SyncTickScheduler.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,36 @@ | ||
package borg.locutus.moneyshot.scheduling; | ||
|
||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||
import net.minecraftforge.fml.common.gameevent.TickEvent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class SyncTickScheduler { | ||
@SubscribeEvent | ||
public void onTick( TickEvent.ClientTickEvent event ) { | ||
|
||
List<SyncTask> ranTasks = new ArrayList<SyncTask>(); | ||
|
||
for (SyncTask task : tasks) { | ||
if (task.waitTimeExceeded()) { | ||
task.runTask(); | ||
ranTasks.add(task); | ||
} | ||
} | ||
|
||
for (SyncTask task : ranTasks) { | ||
tasks.remove(task); | ||
} | ||
} | ||
|
||
private static final List<SyncTask> tasks = new ArrayList<SyncTask>(); | ||
|
||
public static void scheduleNewSyncTask(Runnable runnable, int tickDelay) { | ||
SyncTask newTask = new SyncTask(tickDelay, runnable); | ||
|
||
tasks.add(newTask); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/borg/locutus/moneyshot/screenshot/ScreenshotCreator.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,50 @@ | ||
package borg.locutus.moneyshot.screenshot; | ||
|
||
import borg.locutus.moneyshot.MessageUtils; | ||
import borg.locutus.moneyshot.MoneyShot; | ||
import borg.locutus.moneyshot.scheduling.SyncTickScheduler; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.ScreenShotHelper; | ||
|
||
import java.io.File; | ||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class ScreenshotCreator { | ||
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); | ||
|
||
private static void saveProofScreenshot(String playerName, int amount, String suffix) { | ||
String screenshotFileName = generateNameForProofScreenshot(playerName, amount, suffix); | ||
|
||
if (MoneyShot.pathOfSaveFolder.equals("")) { | ||
ScreenShotHelper.saveScreenshot(Minecraft.getMinecraft().mcDataDir, screenshotFileName, Minecraft.getMinecraft().displayWidth, | ||
Minecraft.getMinecraft().displayHeight, Minecraft.getMinecraft().getFramebuffer()); | ||
} else { | ||
File directory = new File(MoneyShot.pathOfSaveFolder); | ||
|
||
if (!directory.exists()) { | ||
MessageUtils.displayLocalMessage("§f[§eMoneyShot§f]§r §cDas angegebene Verzeichnis §b" + MoneyShot.pathOfSaveFolder + "konnte nicht gefunden werden."); | ||
MessageUtils.displayLocalMessage("§4§lDER SCREENSHOT WURDE NICHT GESPEICHERT!"); | ||
return; | ||
} | ||
|
||
ScreenShotHelper.saveScreenshot(directory, screenshotFileName, Minecraft.getMinecraft().displayWidth, | ||
Minecraft.getMinecraft().displayHeight, Minecraft.getMinecraft().getFramebuffer()); | ||
} | ||
} | ||
|
||
private static String generateNameForProofScreenshot(String playerName, int amount, String suffix) { | ||
String dateString = dateFormat.format(new Date()); | ||
return dateString + "_" + playerName + "_" + amount + "_" + suffix + ".png"; | ||
} | ||
|
||
public static void takeScreenshotOfValidPayment(final String playerName, final int amount, final String suffix) { | ||
SyncTickScheduler.scheduleNewSyncTask(new Runnable() { | ||
@Override | ||
public void run() { | ||
ScreenshotCreator.saveProofScreenshot(playerName, amount, suffix); | ||
} | ||
}, 2); | ||
} | ||
} |
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