From 56618a4cf71b1187f34c2d66889f64cc98433dec Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sun, 6 Mar 2022 15:09:39 +0100 Subject: [PATCH 1/8] Plot History sorry for my bad code. but it works :> ![boolean](https://cdn.discordapp.com/attachments/604764182830645259/947410584746491954/what_does_a_boolean_mean.gif) --- gradle.properties | 2 +- .../mod/PlotHistoryRecorder.java | 71 +++++++++++++++ .../mod/commands/CommandHandler.java | 15 +-- .../commands/impl/other/HistoryCommand.java | 91 +++++++++++++++++++ .../mod/config/impl/MiscellaneousGroup.java | 5 + .../chat/message/checks/LocateCheck.java | 5 +- .../github/codeutilities/sys/SimplePlot.java | 60 ++++++++++++ .../assets/codeutilities/lang/de_de.json | 11 ++- .../assets/codeutilities/lang/en_us.json | 7 ++ 9 files changed, 251 insertions(+), 16 deletions(-) create mode 100644 src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java create mode 100644 src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java create mode 100644 src/main/java/io/github/codeutilities/sys/SimplePlot.java diff --git a/gradle.properties b/gradle.properties index 42e90caa..d543704b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.jvmargs = -Xmx1G #Fabric properties minecraft_version=1.16.5 yarn_mappings=1.16.5+build.5 -loader_version=0.11.2 +loader_version=0.13.3 #Mod properties mod_version = 2.2.2 diff --git a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java new file mode 100644 index 00000000..0c616110 --- /dev/null +++ b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java @@ -0,0 +1,71 @@ +package io.github.codeutilities.mod; + +import io.github.codeutilities.CodeUtilities; +import io.github.codeutilities.mod.config.Config; +import io.github.codeutilities.sys.SimplePlot; +import io.github.codeutilities.sys.networking.State; +import org.apache.logging.log4j.Level; + +import java.io.*; +import java.util.ArrayList; + +public class PlotHistoryRecorder { + private static final String FILE_PATH = "CodeUtilities/history.sff"; // sff means stupid file format + + private static String lastPlot = null; + public static void record(State.Plot statePlot) { + if(!Config.getBoolean("plotHistory")) return; + if(lastPlot != null && lastPlot.equals(statePlot.getId())) return; + + SimplePlot plot = SimplePlot.fromStatePlot(statePlot); + lastPlot = plot.getId(); + + String leFile = readLeFile(); + + ArrayList plots = SimplePlot.toPlotList(leFile); + while(plots.size() > Config.getInteger("plotHistoryLength")) { + plots.remove(0); + } + plots.add(plot); + writeLeFile(SimplePlot.serializePlots(plots)); + } + + public static ArrayList getHistory() { + return SimplePlot.toPlotList(readLeFile()); + } + + private static String readLeFile() { + File file = new File(FILE_PATH); + + try { + if(!file.exists()) { + boolean result = file.createNewFile(); + CodeUtilities.log(Level.INFO, FILE_PATH + " does not exist. creating it."); + if(!result) + CodeUtilities.log(Level.INFO, "failed creating " + FILE_PATH); + + } + + BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); + String line = reader.readLine(); + reader.close(); + return line == null ? "" : line; + } catch(IOException exception) { + CodeUtilities.log(Level.WARN, "Failed to read " + FILE_PATH); + exception.printStackTrace(); + return ""; + } + } + + private static void writeLeFile(String content) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); + writer.write(content); + writer.close(); + } catch (IOException e) { + CodeUtilities.log(Level.WARN, "Failed writing to " + FILE_PATH); + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java b/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java index 4d8a9855..33779fef 100644 --- a/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java +++ b/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java @@ -16,17 +16,7 @@ import io.github.codeutilities.mod.commands.impl.item.UnpackCommand; import io.github.codeutilities.mod.commands.impl.item.template.SendTemplateCommand; import io.github.codeutilities.mod.commands.impl.item.template.WebviewCommand; -import io.github.codeutilities.mod.commands.impl.other.CalcCommand; -import io.github.codeutilities.mod.commands.impl.other.CodeUtilitiesCommand; -import io.github.codeutilities.mod.commands.impl.other.NBSCommand; -import io.github.codeutilities.mod.commands.impl.other.NBSSearchCommand; -import io.github.codeutilities.mod.commands.impl.other.NodeCommand; -import io.github.codeutilities.mod.commands.impl.other.PJoinCommand; -import io.github.codeutilities.mod.commands.impl.other.PartnerBracketCommand; -import io.github.codeutilities.mod.commands.impl.other.PlotsCommand; -import io.github.codeutilities.mod.commands.impl.other.QueueCommand; -import io.github.codeutilities.mod.commands.impl.other.SchemCommand; -import io.github.codeutilities.mod.commands.impl.other.SearchCommand; +import io.github.codeutilities.mod.commands.impl.other.*; import io.github.codeutilities.mod.commands.impl.text.ActionbarCommand; import io.github.codeutilities.mod.commands.impl.text.ColorCommand; import io.github.codeutilities.mod.commands.impl.text.ColorsCommand; @@ -69,7 +59,8 @@ public void load() { new SubTitleCommand(), new ActionbarCommand(), new CalcCommand(), - new CustomTextureCommand() + new CustomTextureCommand(), + new HistoryCommand() ); if (Config.getBoolean("dfCommands")) { diff --git a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java new file mode 100644 index 00000000..4029c1ca --- /dev/null +++ b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java @@ -0,0 +1,91 @@ +package io.github.codeutilities.mod.commands.impl.other; + + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import io.github.codeutilities.CodeUtilities; +import io.github.codeutilities.mod.PlotHistoryRecorder; +import io.github.codeutilities.mod.commands.Command; +import io.github.codeutilities.mod.commands.arguments.ArgBuilder; +import io.github.codeutilities.mod.commands.arguments.types.FreeStringArgumentType; +import io.github.codeutilities.mod.config.Config; +import io.github.codeutilities.mod.features.commands.queue.QueueEntry; +import io.github.codeutilities.mod.features.social.tab.CodeUtilitiesServer; +import io.github.codeutilities.mod.features.social.tab.WebMessage; +import io.github.codeutilities.sys.SimplePlot; +import io.github.codeutilities.sys.player.chat.ChatUtil; +import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.sound.SoundEvents; +import net.minecraft.text.*; + +import java.util.ArrayList; +import java.util.LinkedHashSet; + +public class HistoryCommand extends Command { + + @Override + public void register(MinecraftClient mc, CommandDispatcher cd) { + LiteralArgumentBuilder cmd = ArgBuilder.literal("history") + .executes(ctx -> { + if(!Config.getBoolean("plotHistory")) { + ChatUtil.sendMessage(new LiteralText("§cYou need to enable §lplot history§c in the settings to use this command."), null); + return 1; + } + + ArrayList plots = PlotHistoryRecorder.getHistory(); + if(plots.size() == 0) { + ChatUtil.sendMessage(new LiteralText("§cno plots in the history."), null); + return 1; + } + ChatUtil.sendMessage( + new LiteralText("\n§r §r §r §r §r §r §r §r §r §r §r §r ").append( + new LiteralText("⏪ ") + .styled(style -> style.withColor(TextColor.fromRgb(0x1f9947))).append( + new LiteralText("CodeUtilities Plot History ") + .styled(style -> style.withColor(TextColor.fromRgb(0x33ffa7))).append( + new LiteralText("⏩") + .styled(style -> style.withColor(TextColor.fromRgb(0x1f9947))) + ))), null); + + for (SimplePlot entry : plots) { + MutableText entrymsg = new LiteralText("" + entry.getName()) + .styled(style -> style.withColor(TextColor.fromRgb(0x00bbff) + ).withClickEvent( + new ClickEvent( + ClickEvent.Action.RUN_COMMAND, + "/join " + entry.getId() + )) + .withHoverEvent( + new HoverEvent( + HoverEvent.Action.SHOW_TEXT, + new LiteralText("§7Click to join!") + ) + ) + ).append( + new LiteralText("§8 - ").append( + new LiteralText( "§7" + entry.getId()) + .append( + new LiteralText("§8 by " + entry.getOwner()) + + ))); + ChatUtil.sendMessage(entrymsg, null); + } + + return 1; + }); + cd.register(cmd); + } + + @Override + public String getDescription() { + return "[blue]/history[reset]\n" + + "\n" + + "lists your recently joined plots."; + } + + @Override + public String getName() { + return "/queue"; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/codeutilities/mod/config/impl/MiscellaneousGroup.java b/src/main/java/io/github/codeutilities/mod/config/impl/MiscellaneousGroup.java index 812cb043..dc14a50b 100644 --- a/src/main/java/io/github/codeutilities/mod/config/impl/MiscellaneousGroup.java +++ b/src/main/java/io/github/codeutilities/mod/config/impl/MiscellaneousGroup.java @@ -37,6 +37,11 @@ public void initialize() { quickNum.register(new DoubleSetting("quicknumTertiaryAmount", 0.1)); this.register(quickNum); + // Plot History + ConfigSubGroup plotHistory = new ConfigSubGroup("plotHistory"); + plotHistory.register(new BooleanSetting("plotHistory", true)); + plotHistory.register(new IntegerSetting("plotHistoryLength", 20)); + this.register(plotHistory); } diff --git a/src/main/java/io/github/codeutilities/mod/features/social/chat/message/checks/LocateCheck.java b/src/main/java/io/github/codeutilities/mod/features/social/chat/message/checks/LocateCheck.java index 49ad43a9..71d73f59 100644 --- a/src/main/java/io/github/codeutilities/mod/features/social/chat/message/checks/LocateCheck.java +++ b/src/main/java/io/github/codeutilities/mod/features/social/chat/message/checks/LocateCheck.java @@ -1,5 +1,6 @@ package io.github.codeutilities.mod.features.social.chat.message.checks; +import io.github.codeutilities.mod.PlotHistoryRecorder; import io.github.codeutilities.mod.features.social.chat.message.Message; import io.github.codeutilities.mod.features.social.chat.message.MessageCheck; import io.github.codeutilities.mod.features.social.chat.message.MessageType; @@ -22,6 +23,8 @@ public boolean check(Message message, String stripped) { @Override public void onReceive(Message message) { - DFInfo.setCurrentState(State.fromLocate(message)); + State state = State.fromLocate(message); + DFInfo.setCurrentState(state); + PlotHistoryRecorder.record(state.getPlot()); } } diff --git a/src/main/java/io/github/codeutilities/sys/SimplePlot.java b/src/main/java/io/github/codeutilities/sys/SimplePlot.java new file mode 100644 index 00000000..3f94c0ef --- /dev/null +++ b/src/main/java/io/github/codeutilities/sys/SimplePlot.java @@ -0,0 +1,60 @@ +package io.github.codeutilities.sys; + +import io.github.codeutilities.sys.networking.State; + +import java.util.ArrayList; +import java.util.Arrays; + +public class SimplePlot { + private final String id; + private final String name; + private final String owner; + + public SimplePlot(String id, String name, String owner) { + this.id = id; + this.name = name; + this.owner = owner; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getOwner() { + return owner; + } + + public static SimplePlot fromStatePlot(State.Plot plot) { + return new SimplePlot(plot.getId(), plot.getName().replaceAll("[,;]", "?"), plot.getOwner()); + } + + @Override + public String toString() { + return id + ";" + name + ";" + owner; + } + + public static ArrayList toPlotList(String serialized) { + String[] serializedPlots = serialized.length() == 0 ? null : serialized.split(","); + ArrayList plots = new ArrayList<>(); + if(serializedPlots == null) + return plots; + + for(String serializedPlot : serializedPlots) { + String[] props = serializedPlot.split(";"); + plots.add(new SimplePlot(props[0], props[1], props[2])); + } + return plots; + } + + public static String serializePlots(ArrayList plots) { + StringBuilder builder = new StringBuilder(); + for(SimplePlot plot : plots) { + builder.append(",").append(plot.toString()); + } + return builder.toString().replaceAll("^,", ""); + } +} diff --git a/src/main/resources/assets/codeutilities/lang/de_de.json b/src/main/resources/assets/codeutilities/lang/de_de.json index c6b72858..94c457ac 100644 --- a/src/main/resources/assets/codeutilities/lang/de_de.json +++ b/src/main/resources/assets/codeutilities/lang/de_de.json @@ -174,5 +174,12 @@ "key.codeutilities.node3": "Node 3", "key.codeutilities.node4": "Node 4", "key.codeutilities.node5": "Node 5", - "key.codeutilities.node_beta": "Node Beta" - } \ No newline at end of file + "key.codeutilities.node_beta": "Node Beta", + + "config.codeutilities.subcategory.misc_plotHistory": "Plot Verlauf", + "config.codeutilities.subcategory.misc_plotHistory.tooltip": "Verlauf von kürzlich betretenen plots.", + "config.codeutilities.option.plotHistory.tooltip": "dieses feature aktivieren.", + "config.codeutilities.option.plotHistory": "Aktiviert", + "config.codeutilities.option.plotHistoryLength": "Maximale Plots", + "config.codeutilities.option.plotHistoryLength.tooltip": "Maximale anzahl an plots im Plot Verlauf." +} \ No newline at end of file diff --git a/src/main/resources/assets/codeutilities/lang/en_us.json b/src/main/resources/assets/codeutilities/lang/en_us.json index 7d3fe06b..d01c3eec 100644 --- a/src/main/resources/assets/codeutilities/lang/en_us.json +++ b/src/main/resources/assets/codeutilities/lang/en_us.json @@ -219,6 +219,13 @@ "config.codeutilities.option.quicknumTertiaryAmount": "Secondary Amount", "config.codeutilities.option.quicknumTertiaryAmount.tooltip": "Change how much a scroll changes the number. (When holding SHIFT)", + "config.codeutilities.subcategory.misc_plotHistory": "Recent Plots History", + "config.codeutilities.subcategory.misc_plotHistory.tooltip": "History of recent plots you joined.", + "config.codeutilities.option.plotHistory.tooltip": "Enable this feature.", + "config.codeutilities.option.plotHistory": "Enabled", + "config.codeutilities.option.plotHistoryLength": "Maximum Plots", + "config.codeutilities.option.plotHistoryLength.tooltip": "How many plots can be in the history at a time.", + "config.codeutilities.option.chestToolTip": "Chest Tooltip", "config.codeutilities.option.chestToolTip.tooltip": "Display chest params when you are in dev", "config.codeutilities.option.chestToolTipType": "Chest Tooltip Style", From 224702293e3ee37c706aebf8b92bea076e9747aa Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sun, 6 Mar 2022 15:46:55 +0100 Subject: [PATCH 2/8] fixes & no more unused imports --- .../mod/commands/impl/other/HistoryCommand.java | 9 +-------- .../java/io/github/codeutilities/sys/SimplePlot.java | 1 - 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java index 4029c1ca..9ebd2810 100644 --- a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java +++ b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java @@ -3,24 +3,17 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; -import io.github.codeutilities.CodeUtilities; import io.github.codeutilities.mod.PlotHistoryRecorder; import io.github.codeutilities.mod.commands.Command; import io.github.codeutilities.mod.commands.arguments.ArgBuilder; -import io.github.codeutilities.mod.commands.arguments.types.FreeStringArgumentType; import io.github.codeutilities.mod.config.Config; -import io.github.codeutilities.mod.features.commands.queue.QueueEntry; -import io.github.codeutilities.mod.features.social.tab.CodeUtilitiesServer; -import io.github.codeutilities.mod.features.social.tab.WebMessage; import io.github.codeutilities.sys.SimplePlot; import io.github.codeutilities.sys.player.chat.ChatUtil; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.client.MinecraftClient; -import net.minecraft.sound.SoundEvents; import net.minecraft.text.*; import java.util.ArrayList; -import java.util.LinkedHashSet; public class HistoryCommand extends Command { @@ -86,6 +79,6 @@ public String getDescription() { @Override public String getName() { - return "/queue"; + return "/history"; } } \ No newline at end of file diff --git a/src/main/java/io/github/codeutilities/sys/SimplePlot.java b/src/main/java/io/github/codeutilities/sys/SimplePlot.java index 3f94c0ef..fcd9c07e 100644 --- a/src/main/java/io/github/codeutilities/sys/SimplePlot.java +++ b/src/main/java/io/github/codeutilities/sys/SimplePlot.java @@ -3,7 +3,6 @@ import io.github.codeutilities.sys.networking.State; import java.util.ArrayList; -import java.util.Arrays; public class SimplePlot { private final String id; From 75df21d4e6abf948dd0d069660ebc6878e8d2c42 Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sun, 6 Mar 2022 17:05:07 +0100 Subject: [PATCH 3/8] fix NullPointerException --- .../java/io/github/codeutilities/mod/PlotHistoryRecorder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java index 0c616110..28dbc614 100644 --- a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java +++ b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java @@ -14,6 +14,7 @@ public class PlotHistoryRecorder { private static String lastPlot = null; public static void record(State.Plot statePlot) { + if(statePlot == null) return; if(!Config.getBoolean("plotHistory")) return; if(lastPlot != null && lastPlot.equals(statePlot.getId())) return; From 828e68eefa152c6316bb8da455ff2fe3b0661aa3 Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sun, 6 Mar 2022 17:14:15 +0100 Subject: [PATCH 4/8] make method names unfunny --- .../github/codeutilities/mod/PlotHistoryRecorder.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java index 28dbc614..b8d417aa 100644 --- a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java +++ b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java @@ -21,21 +21,21 @@ public static void record(State.Plot statePlot) { SimplePlot plot = SimplePlot.fromStatePlot(statePlot); lastPlot = plot.getId(); - String leFile = readLeFile(); + String leFile = readFile(); ArrayList plots = SimplePlot.toPlotList(leFile); while(plots.size() > Config.getInteger("plotHistoryLength")) { plots.remove(0); } plots.add(plot); - writeLeFile(SimplePlot.serializePlots(plots)); + writeToFile(SimplePlot.serializePlots(plots)); } public static ArrayList getHistory() { - return SimplePlot.toPlotList(readLeFile()); + return SimplePlot.toPlotList(readFile()); } - private static String readLeFile() { + private static String readFile() { File file = new File(FILE_PATH); try { @@ -58,7 +58,7 @@ private static String readLeFile() { } } - private static void writeLeFile(String content) { + private static void writeToFile(String content) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); writer.write(content); From b966c6e36f7297b0db50728c22d886e3140364ca Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sun, 6 Mar 2022 21:58:04 +0100 Subject: [PATCH 5/8] invert plot order --- .../java/io/github/codeutilities/mod/PlotHistoryRecorder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java index b8d417aa..6695b94f 100644 --- a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java +++ b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java @@ -27,7 +27,7 @@ public static void record(State.Plot statePlot) { while(plots.size() > Config.getInteger("plotHistoryLength")) { plots.remove(0); } - plots.add(plot); + plots.add(0, plot); writeToFile(SimplePlot.serializePlots(plots)); } From 3051f0120880d781aa40483d925f01e9cb2f4451 Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Mon, 7 Mar 2022 14:18:46 +0100 Subject: [PATCH 6/8] =?UTF-8?q?Remove=20=C2=A7=20color=20codes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don't really understand why this is better, but ill just do it. --- .../mod/commands/CommandHandler.java | 2 +- .../commands/impl/other/HistoryCommand.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java b/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java index 33779fef..7863cc3c 100644 --- a/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java +++ b/src/main/java/io/github/codeutilities/mod/commands/CommandHandler.java @@ -60,7 +60,7 @@ public void load() { new ActionbarCommand(), new CalcCommand(), new CustomTextureCommand(), - new HistoryCommand() + new HistoryCommand() ); if (Config.getBoolean("dfCommands")) { diff --git a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java index 9ebd2810..539fffbd 100644 --- a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java +++ b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java @@ -8,6 +8,7 @@ import io.github.codeutilities.mod.commands.arguments.ArgBuilder; import io.github.codeutilities.mod.config.Config; import io.github.codeutilities.sys.SimplePlot; +import io.github.codeutilities.sys.player.chat.ChatType; import io.github.codeutilities.sys.player.chat.ChatUtil; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.client.MinecraftClient; @@ -22,27 +23,28 @@ public void register(MinecraftClient mc, CommandDispatcher cmd = ArgBuilder.literal("history") .executes(ctx -> { if(!Config.getBoolean("plotHistory")) { - ChatUtil.sendMessage(new LiteralText("§cYou need to enable §lplot history§c in the settings to use this command."), null); + ChatUtil.sendMessage(new LiteralText("Plot history is not enabled in the settings."), ChatType.FAIL); return 1; } ArrayList plots = PlotHistoryRecorder.getHistory(); if(plots.size() == 0) { - ChatUtil.sendMessage(new LiteralText("§cno plots in the history."), null); + ChatUtil.sendMessage(new LiteralText("no plots in the history."), ChatType.FAIL); return 1; } + + ChatUtil.sendMessage(""); ChatUtil.sendMessage( - new LiteralText("\n§r §r §r §r §r §r §r §r §r §r §r §r ").append( new LiteralText("⏪ ") .styled(style -> style.withColor(TextColor.fromRgb(0x1f9947))).append( - new LiteralText("CodeUtilities Plot History ") + new LiteralText(" CodeUtilities Plot History ") .styled(style -> style.withColor(TextColor.fromRgb(0x33ffa7))).append( new LiteralText("⏩") .styled(style -> style.withColor(TextColor.fromRgb(0x1f9947))) - ))), null); + )), null); for (SimplePlot entry : plots) { - MutableText entrymsg = new LiteralText("" + entry.getName()) + MutableText entrymsg = new LiteralText(entry.getName()) .styled(style -> style.withColor(TextColor.fromRgb(0x00bbff) ).withClickEvent( new ClickEvent( @@ -52,16 +54,14 @@ public void register(MinecraftClient mc, CommandDispatcher style2.withColor(TextColor.fromRgb(0xaaaaaa))) ) ) ).append( - new LiteralText("§8 - ").append( - new LiteralText( "§7" + entry.getId()) + new LiteralText(" - ").styled(style -> style.withColor(TextColor.fromRgb(0x555555))).append( + new LiteralText(entry.getId()).styled(style -> style.withColor(TextColor.fromRgb(0xaaaaaa))) .append( - new LiteralText("§8 by " + entry.getOwner()) - - ))); + new LiteralText(" by " + entry.getOwner()).styled(style -> style.withColor(TextColor.fromRgb(0x555555)))))); ChatUtil.sendMessage(entrymsg, null); } From 1b2d32c266b60828da78390bb12ef2eed4f89518 Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Fri, 11 Mar 2022 01:52:48 +0100 Subject: [PATCH 7/8] Improve HistoryCommand --- .../mod/PlotHistoryRecorder.java | 49 +++---------------- .../commands/impl/other/HistoryCommand.java | 5 +- .../codeutilities/sys/file/FileUtil.java | 43 +++++++++++++++- 3 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java index 6695b94f..b09fee64 100644 --- a/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java +++ b/src/main/java/io/github/codeutilities/mod/PlotHistoryRecorder.java @@ -1,16 +1,16 @@ package io.github.codeutilities.mod; -import io.github.codeutilities.CodeUtilities; import io.github.codeutilities.mod.config.Config; import io.github.codeutilities.sys.SimplePlot; +import io.github.codeutilities.sys.file.FileUtil; import io.github.codeutilities.sys.networking.State; -import org.apache.logging.log4j.Level; +import net.fabricmc.loader.api.FabricLoader; -import java.io.*; +import java.nio.file.Path; import java.util.ArrayList; public class PlotHistoryRecorder { - private static final String FILE_PATH = "CodeUtilities/history.sff"; // sff means stupid file format + private static final Path FILE_PATH = FabricLoader.getInstance().getGameDir().resolve("CodeUtilities/history.sff"); // sff means stupid file format private static String lastPlot = null; public static void record(State.Plot statePlot) { @@ -21,52 +21,19 @@ public static void record(State.Plot statePlot) { SimplePlot plot = SimplePlot.fromStatePlot(statePlot); lastPlot = plot.getId(); - String leFile = readFile(); + String fileContents = FileUtil.readFirstLine(FILE_PATH); - ArrayList plots = SimplePlot.toPlotList(leFile); + ArrayList plots = SimplePlot.toPlotList(fileContents); while(plots.size() > Config.getInteger("plotHistoryLength")) { plots.remove(0); } plots.add(0, plot); - writeToFile(SimplePlot.serializePlots(plots)); + FileUtil.writeStringToFile(FILE_PATH, SimplePlot.serializePlots(plots)); } public static ArrayList getHistory() { - return SimplePlot.toPlotList(readFile()); + return SimplePlot.toPlotList(FileUtil.readFirstLine(FILE_PATH)); } - private static String readFile() { - File file = new File(FILE_PATH); - - try { - if(!file.exists()) { - boolean result = file.createNewFile(); - CodeUtilities.log(Level.INFO, FILE_PATH + " does not exist. creating it."); - if(!result) - CodeUtilities.log(Level.INFO, "failed creating " + FILE_PATH); - - } - - BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); - String line = reader.readLine(); - reader.close(); - return line == null ? "" : line; - } catch(IOException exception) { - CodeUtilities.log(Level.WARN, "Failed to read " + FILE_PATH); - exception.printStackTrace(); - return ""; - } - } - - private static void writeToFile(String content) { - try { - BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); - writer.write(content); - writer.close(); - } catch (IOException e) { - CodeUtilities.log(Level.WARN, "Failed writing to " + FILE_PATH); - e.printStackTrace(); - } - } } diff --git a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java index 539fffbd..bf1e3bd9 100644 --- a/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java +++ b/src/main/java/io/github/codeutilities/mod/commands/impl/other/HistoryCommand.java @@ -33,11 +33,10 @@ public void register(MinecraftClient mc, CommandDispatcher style.withColor(TextColor.fromRgb(0x1f9947))).append( - new LiteralText(" CodeUtilities Plot History ") + new LiteralText(" CodeUtilities Plot History ") .styled(style -> style.withColor(TextColor.fromRgb(0x33ffa7))).append( new LiteralText("⏩") .styled(style -> style.withColor(TextColor.fromRgb(0x1f9947))) diff --git a/src/main/java/io/github/codeutilities/sys/file/FileUtil.java b/src/main/java/io/github/codeutilities/sys/file/FileUtil.java index c0606d61..70185709 100644 --- a/src/main/java/io/github/codeutilities/sys/file/FileUtil.java +++ b/src/main/java/io/github/codeutilities/sys/file/FileUtil.java @@ -1,8 +1,12 @@ package io.github.codeutilities.sys.file; -import java.io.IOException; +import io.github.codeutilities.CodeUtilities; +import org.apache.logging.log4j.Level; + +import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; public class FileUtil { @@ -11,4 +15,41 @@ public static String readFile(String path, Charset encoding) byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); } + + + public static String readFirstLine(Path path) { + File file = path.toFile(); + + try { + if(!file.exists()) { + boolean result = file.createNewFile(); + CodeUtilities.log(Level.INFO, path + " does not exist. creating it."); + if(!result) + CodeUtilities.log(Level.INFO, "failed creating " + path); + + } + + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line = reader.readLine(); + reader.close(); + return line == null ? "" : line; + } catch(IOException exception) { + CodeUtilities.log(Level.WARN, "Failed to read " + path); + exception.printStackTrace(); + return ""; + } + } + + public static void writeStringToFile(Path path, String content) { + try { + File file = path.toFile(); + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(content); + writer.close(); + } catch (IOException e) { + CodeUtilities.log(Level.WARN, "Failed writing to " + path); + e.printStackTrace(); + } + } + } From 04125790ea2a27d9c60989f75624141e29a4b24e Mon Sep 17 00:00:00 2001 From: CheaterTim <85891155+CheaterTim@users.noreply.github.com> Date: Sat, 12 Mar 2022 20:50:00 +0100 Subject: [PATCH 8/8] Migrate to java.nio.file.Files --- .../io/github/codeutilities/sys/file/FileUtil.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/codeutilities/sys/file/FileUtil.java b/src/main/java/io/github/codeutilities/sys/file/FileUtil.java index 70185709..7a04bc05 100644 --- a/src/main/java/io/github/codeutilities/sys/file/FileUtil.java +++ b/src/main/java/io/github/codeutilities/sys/file/FileUtil.java @@ -29,10 +29,9 @@ public static String readFirstLine(Path path) { } - BufferedReader reader = new BufferedReader(new FileReader(file)); - String line = reader.readLine(); - reader.close(); - return line == null ? "" : line; + String contents = Files.readString(path); + + return contents == null ? "" : contents; } catch(IOException exception) { CodeUtilities.log(Level.WARN, "Failed to read " + path); exception.printStackTrace(); @@ -42,10 +41,7 @@ public static String readFirstLine(Path path) { public static void writeStringToFile(Path path, String content) { try { - File file = path.toFile(); - BufferedWriter writer = new BufferedWriter(new FileWriter(file)); - writer.write(content); - writer.close(); + Files.writeString(path, content); } catch (IOException e) { CodeUtilities.log(Level.WARN, "Failed writing to " + path); e.printStackTrace();