-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
22 changed files
with
499 additions
and
17 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
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
18 changes: 12 additions & 6 deletions
18
fabric/src/main/java/com/lx862/jcm/mod/block/behavior/EnquiryMachineBehavior.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 |
---|---|---|
@@ -1,17 +1,23 @@ | ||
package com.lx862.jcm.mod.block.behavior; | ||
|
||
import com.lx862.jcm.mod.util.TextUtil; | ||
import com.lx862.jcm.mod.data.EnquiryScreenType; | ||
import com.lx862.jcm.mod.data.TransactionEntry; | ||
import com.lx862.jcm.mod.data.TransactionLog; | ||
import com.lx862.jcm.mod.network.gui.EnquiryUpdateGUIPacket; | ||
import com.lx862.jcm.mod.registry.Networking; | ||
import org.mtr.mapping.holder.PlayerEntity; | ||
import org.mtr.mapping.holder.SoundCategory; | ||
import org.mtr.mapping.holder.Text; | ||
import org.mtr.mapping.holder.World; | ||
import org.mtr.mod.SoundEvents; | ||
import org.mtr.mod.data.TicketSystem; | ||
|
||
import java.util.List; | ||
|
||
public interface EnquiryMachineBehavior { | ||
default void enquiry(World world, PlayerEntity player) { | ||
int score = TicketSystem.getBalance(world, player); | ||
player.sendMessage(Text.cast(TextUtil.translatable("gui.mtr.balance", String.valueOf(score))), true); | ||
world.playSound((PlayerEntity) null, player.getBlockPos(), SoundEvents.TICKET_PROCESSOR_ENTRY.get(), SoundCategory.BLOCKS, 1, 1); | ||
default void enquiry(EnquiryScreenType type, World world, PlayerEntity player) { | ||
world.playSound(null, player.getBlockPos(), SoundEvents.TICKET_PROCESSOR_ENTRY.get(), SoundCategory.BLOCKS, 1, 1); | ||
|
||
List<TransactionEntry> entries = TransactionLog.readLog(player, player.getUuidAsString()); | ||
Networking.sendPacketToClient(player, new EnquiryUpdateGUIPacket(type, entries, TicketSystem.getBalance(world, player))); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
fabric/src/main/java/com/lx862/jcm/mod/data/EnquiryScreenType.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,7 @@ | ||
package com.lx862.jcm.mod.data; | ||
|
||
public enum EnquiryScreenType { | ||
RV, | ||
CLASSIC, | ||
NONE | ||
} |
38 changes: 38 additions & 0 deletions
38
fabric/src/main/java/com/lx862/jcm/mod/data/TransactionEntry.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 com.lx862.jcm.mod.data; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import java.text.SimpleDateFormat; | ||
|
||
public class TransactionEntry { | ||
public static final SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm"); | ||
public final String source; | ||
public final long time; | ||
public final long amount; | ||
|
||
public TransactionEntry(String source, long amount, long time) { | ||
this.source = source; | ||
this.amount = amount; | ||
this.time = time; | ||
} | ||
|
||
public String getFormattedDate() { | ||
return formatter.format(time); | ||
} | ||
|
||
public static TransactionEntry fromJson(JsonObject jsonObject) { | ||
return new TransactionEntry( | ||
jsonObject.get("source").getAsString(), | ||
jsonObject.get("amount").getAsInt(), | ||
jsonObject.get("time").getAsLong() | ||
); | ||
} | ||
|
||
public JsonObject toJson() { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("source", source); | ||
jsonObject.addProperty("amount", amount); | ||
jsonObject.addProperty("time", time); | ||
return jsonObject; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
fabric/src/main/java/com/lx862/jcm/mod/data/TransactionLog.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,67 @@ | ||
package com.lx862.jcm.mod.data; | ||
|
||
import com.google.gson.*; | ||
import com.lx862.jcm.mod.Constants; | ||
import com.lx862.jcm.mod.util.JCMLogger; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.mtr.mapping.holder.PlayerEntity; | ||
import org.mtr.mapping.holder.WorldSavePath; | ||
|
||
public class TransactionLog { | ||
public static final int MAX_ENTRY_LIMIT = 50; | ||
|
||
public static void writeLog(PlayerEntity player, TransactionEntry entry) { | ||
Path savePath = getSavePath(player); | ||
savePath.getParent().toFile().mkdirs(); | ||
|
||
JsonObject jsonObject = new JsonObject(); | ||
JsonArray entryArray = new JsonArray(); | ||
|
||
try { | ||
if (Files.exists(savePath)) { | ||
jsonObject = new JsonParser().parse(String.join("", Files.readAllLines(savePath))).getAsJsonObject(); | ||
entryArray = jsonObject.getAsJsonArray("entries"); | ||
} | ||
|
||
entryArray.add(entry.toJson()); | ||
if(entryArray.size() > MAX_ENTRY_LIMIT) entryArray.remove(0); | ||
jsonObject.add("entries", entryArray); | ||
|
||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
Files.write(savePath, gson.toJson(jsonObject).getBytes()); | ||
} catch (IOException e) { | ||
JCMLogger.error("Error saving data to JSON file: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
public static List<TransactionEntry> readLog(PlayerEntity player, String playerName) { | ||
Path savePath = getSavePath(player); | ||
savePath.getParent().toFile().mkdirs(); | ||
|
||
List<TransactionEntry> entries = new ArrayList<>(); | ||
try { | ||
if (Files.exists(savePath)) { | ||
JsonObject jsonObject = new JsonParser().parse(String.join("", Files.readAllLines(savePath))).getAsJsonObject(); | ||
JsonArray playerDataArray = jsonObject.getAsJsonArray("entries"); | ||
for (JsonElement element : playerDataArray) { | ||
entries.add(TransactionEntry.fromJson(element.getAsJsonObject())); | ||
} | ||
} | ||
} catch (IOException e) { | ||
JCMLogger.error("Error reading data from JSON file: {}", e.getMessage()); | ||
} | ||
return entries; | ||
} | ||
|
||
public static Path getSavePath(PlayerEntity player) { | ||
Path saveDirectory = player.getServer().getSavePath(WorldSavePath.getRootMapped()).resolve(Constants.MOD_ID).resolve("player_data"); | ||
saveDirectory.toFile().mkdirs(); | ||
return saveDirectory.resolve(player.getUuidAsString() + ".json"); | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
fabric/src/main/java/com/lx862/jcm/mod/network/gui/EnquiryUpdateGUIPacket.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,59 @@ | ||
package com.lx862.jcm.mod.network.gui; | ||
|
||
import com.lx862.jcm.mod.data.EnquiryScreenType; | ||
import com.lx862.jcm.mod.data.TransactionEntry; | ||
import org.mtr.mapping.registry.PacketHandler; | ||
import org.mtr.mapping.tool.PacketBufferReceiver; | ||
import org.mtr.mapping.tool.PacketBufferSender; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class EnquiryUpdateGUIPacket extends PacketHandler { | ||
|
||
private final EnquiryScreenType type; | ||
private final List<TransactionEntry> entries; | ||
private final int entryCount; | ||
private final int remainingBalance; | ||
|
||
public EnquiryUpdateGUIPacket(PacketBufferReceiver packetBufferReceiver) { | ||
this.entries = new ArrayList<>(); | ||
this.type = EnquiryScreenType.valueOf(packetBufferReceiver.readString()); | ||
this.remainingBalance = packetBufferReceiver.readInt(); | ||
|
||
this.entryCount = packetBufferReceiver.readInt(); | ||
for (int i = 0; i < entryCount; i++) { | ||
String source = packetBufferReceiver.readString(); | ||
long amount = packetBufferReceiver.readLong(); | ||
long time = packetBufferReceiver.readLong(); | ||
entries.add(new TransactionEntry(source, amount, time)); | ||
} | ||
} | ||
|
||
public EnquiryUpdateGUIPacket(EnquiryScreenType type, List<TransactionEntry> entries, int remainingBalance) { | ||
this.type = type; | ||
this.entries = entries.stream().sorted((a, b) -> (int)(b.time - a.time)).collect(Collectors.toList()); | ||
this.entryCount = entries.size(); | ||
this.remainingBalance = remainingBalance; | ||
} | ||
|
||
@Override | ||
public void write(PacketBufferSender packetBufferSender) { | ||
packetBufferSender.writeString(type.toString()); | ||
packetBufferSender.writeInt(remainingBalance); | ||
packetBufferSender.writeInt(entries.size()); | ||
|
||
for (TransactionEntry transactionEntry : entries) { | ||
packetBufferSender.writeString(transactionEntry.source); | ||
packetBufferSender.writeLong(transactionEntry.amount); | ||
packetBufferSender.writeLong(transactionEntry.time); | ||
} | ||
} | ||
|
||
@Override | ||
public void runClient() { | ||
ClientHelper.openEnquiryScreen(type, entries, remainingBalance); | ||
} | ||
} |
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.