Skip to content

Commit

Permalink
Continue work to remove the H2 MVStore library.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeronica committed Dec 18, 2023
1 parent 4ca15f1 commit 31ba2f4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/generated/resources/assets/mxtune/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"chat.mxtune.groupManager.player_cannot_join_too_many": "%s cannot join since group is full",
"chat.mxtune.groupManager.player_joined_the_group": "%s joined the group.",
"chat.mxtune.groupManager.you_joined_players_group": "You joined %s's group.",
"commands.mxtune.music.convert": "Dump converted to %s files",
"commands.mxtune.music.dump": "Wrote %s records",
"commands.mxtune.music.load": "Read %s records",
"config.mxtune.client.double_click_time_ms": "Double-click time in milliseconds for GUI widgets",
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import static aeronicamc.mods.mxtune.caches.FileHelper.*;
Expand Down Expand Up @@ -165,6 +167,43 @@ public static int loadDumpFile()
return size;
}

public static int convertDumpToFiles()
{
AtomicInteger size = new AtomicInteger(0);
if (fileExists(SERVER_MUSIC_FOLDER_DUMP_FOLDER, SERVER_DATA_STORE_DUMP_FILENAME, LogicalSide.SERVER))
{
try (Stream<String> stream = Files.lines(getCacheFile(SERVER_MUSIC_FOLDER_DUMP_FOLDER, SERVER_DATA_STORE_DUMP_FILENAME, LogicalSide.SERVER)))
{
LOGGER.info("Convert to Dump using: {}", SERVER_FOLDER );
stream.forEach(line ->
{
String[] pair = line.split("=");
LocalDateTime dateTime = (LocalDateTime.parse(pair[0]));
String filename = "--error--";
try
{
filename = toSafeFileNameKey(dateTime.toString())+".txt";
Path path = FileHelper.getCacheFile(SERVER_FOLDER, filename, LogicalSide.SERVER);
PrintWriter printWriter = new PrintWriter(path.toString(), "UTF-8");
printWriter.println(pair[1]);
printWriter.close();
LOGGER.info(" Created: {}", path.toString());
size.getAndIncrement();
} catch (IOException e)
{
LOGGER.error(" failed to create file: {}", filename, e);
}

});
} catch (IOException | SecurityException | DateTimeParseException e)
{
LOGGER.error(e);
}

} else return size.get();
return size.get();
}

private static LocalDateTime nextKey()
{
LocalDateTime now;
Expand Down Expand Up @@ -257,7 +296,7 @@ private static long reapSheetMusic(boolean whatIf)
}

/**
* Add add a MML format sting to the store. Returns a unique date-time string as the key to the MML.
* Add a MML format sting to the store. Returns a unique date-time string as the key to the MML.
* (At least providing there are no unexpected time shifts due to incorrect time on the server/pc etc.)
* @param musicText - the MML music text string to be stored.
* @return a unique date-time string (GMT0) as the key to the entry, or null if the add failed.
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/aeronicamc/mods/mxtune/command/CommandMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class CommandMusic
{
return Commands.literal("music")
.then(CommandMusicDump.register())
.then(CommandMusicLoad.register());
.then(CommandMusicLoad.register())
.then(CommandConvert.register());
}

private static class CommandMusicDump
Expand Down Expand Up @@ -48,4 +49,21 @@ private static class CommandMusicLoad
);
}
}

private static class CommandConvert
{
static ArgumentBuilder<CommandSource, ?> register()
{
return Commands.literal("convert")
.requires(cs->cs.hasPermission(4)) //permission
.executes(ctx ->
{
ctx.getSource().sendSuccess(
new TranslationTextComponent("commands.mxtune.music.convert",
String.format("%d", ModDataStore.convertDumpToFiles())), true);
return 0;
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private void addCommands()
{
addCommand("music.dump", "Wrote %s records");
addCommand("music.load", "Read %s records");
addCommand("music.convert", "Dump converted to %s files");
}

private void addConfigs()
Expand Down

0 comments on commit 31ba2f4

Please sign in to comment.