From e1064930c9735cc64944708bb565ca1340851d0a Mon Sep 17 00:00:00 2001 From: NriotHrreion Date: Sun, 26 Jan 2025 21:07:48 +0800 Subject: [PATCH] feat: Enable / disable scoreboard in other slots & fix: Counting data is not stored into dat file --- .../diggingcount/DiggingCountPlugin.java | 67 +++++++++++++++---- .../net/nocpiun/diggingcount/board/Board.java | 16 ++++- .../diggingcount/command/DiggingCommand.java | 22 +++++- 3 files changed, 87 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java b/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java index 0d2e9fb..999fd85 100644 --- a/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java +++ b/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java @@ -7,6 +7,7 @@ import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.scoreboard.ScoreboardDisplaySlot; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -17,6 +18,8 @@ import java.util.HashMap; public class DiggingCountPlugin { + public final static HashMap defaultConfig = new HashMap<>(); + private MinecraftServer server; private File configFile; @@ -26,6 +29,11 @@ public class DiggingCountPlugin { private Board board; public DiggingCountPlugin() { + defaultConfig.put(ScoreboardDisplaySlot.LIST.name(), false); + defaultConfig.put(ScoreboardDisplaySlot.SIDEBAR.name(), true); + defaultConfig.put(ScoreboardDisplaySlot.BELOW_NAME.name(), false); + defaultConfig.put("title", "§7§lDigging Count"); + ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStart); ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStop); PlayerBlockBreakEvents.AFTER.register(this::onPlayerBreakBlock); @@ -39,23 +47,31 @@ private void onServerStart(MinecraftServer server) { configFile = new File(FabricLoader.getInstance().getConfigDir().toString(), "digging-count-config.dat"); if(configFile.exists()) { config = loadFile(configFile); + + defaultConfig.forEach((key, value) -> { + if(!config.containsKey(key)) { + config.put(key, value); + } + }); } else { // default config - config = new HashMap<>(); - config.put("enabled", true); - config.put("title", "§7§lDigging Count"); + config = new HashMap<>(defaultConfig); } - saveFile(configFile, config); + saveConfig(); // Initialize the data dataFile = new File(FabricLoader.getInstance().getConfigDir().toString(), "digging-count.dat"); if(dataFile.exists()) { counterMap = loadFile(dataFile); + } else { + counterMap = new HashMap<>(); } - saveFile(dataFile, counterMap); + saveData(); // Initialize the scoreboard board = new Board(this.server); - board.setVisible(getEnabled()); + board.setVisible(ScoreboardDisplaySlot.LIST, getEnabled(ScoreboardDisplaySlot.LIST)); + board.setVisible(ScoreboardDisplaySlot.SIDEBAR, getEnabled(ScoreboardDisplaySlot.SIDEBAR)); + board.setVisible(ScoreboardDisplaySlot.BELOW_NAME, getEnabled(ScoreboardDisplaySlot.BELOW_NAME)); board.setTitle(getTitle()); } @@ -64,7 +80,10 @@ private void onServerStop(MinecraftServer server) { } private void onPlayerBreakBlock(World world, PlayerEntity player, BlockPos pos, BlockState state, BlockEntity entity) { - board.setCount(player, board.getCount(player) + 1); + int currentCount = board.getCount(player) + 1; + board.setCount(player, currentCount); + counterMap.put(player.getName().getString(), currentCount); + saveData(); } @SuppressWarnings("unchecked") @@ -97,13 +116,28 @@ public void saveFile(File file, T obj) { } } - public boolean getEnabled() { - return (boolean) config.get("enabled"); + public boolean getEnabled(ScoreboardDisplaySlot slot) { + if( + slot == ScoreboardDisplaySlot.LIST + || slot == ScoreboardDisplaySlot.SIDEBAR + || slot == ScoreboardDisplaySlot.BELOW_NAME + ) { + return (boolean) config.get(slot.name()); + } + + return false; } - public void setEnabled(boolean enabled) { - config.put("enabled", enabled); - board.setVisible(enabled); + public void setEnabled(ScoreboardDisplaySlot slot, boolean enabled) { + if( + slot == ScoreboardDisplaySlot.LIST + || slot == ScoreboardDisplaySlot.SIDEBAR + || slot == ScoreboardDisplaySlot.BELOW_NAME + ) { + config.put(slot.name(), enabled); + board.setVisible(slot, enabled); + saveConfig(); + } } public String getTitle() { @@ -113,5 +147,14 @@ public String getTitle() { public void setTitle(String title) { config.put("title", title); board.setTitle(title); + saveConfig(); + } + + private void saveConfig() { + saveFile(configFile, config); + } + + private void saveData() { + saveFile(dataFile, counterMap); } } diff --git a/src/main/java/net/nocpiun/diggingcount/board/Board.java b/src/main/java/net/nocpiun/diggingcount/board/Board.java index 236779f..0c6a84c 100644 --- a/src/main/java/net/nocpiun/diggingcount/board/Board.java +++ b/src/main/java/net/nocpiun/diggingcount/board/Board.java @@ -38,15 +38,25 @@ public void setCount(PlayerEntity player, int count) { access.setScore(count); } - public void setVisible(boolean visible) { + public void setVisible(ScoreboardDisplaySlot slot, boolean visible) { if(visible) { - scoreboard.setObjectiveSlot(ScoreboardDisplaySlot.SIDEBAR, objective); + scoreboard.setObjectiveSlot(slot, objective); } else { - scoreboard.setObjectiveSlot(ScoreboardDisplaySlot.SIDEBAR, null); + scoreboard.setObjectiveSlot(slot, null); } } public void setTitle(String title) { objective.setDisplayName(Message.colorize(title)); } + + public static ScoreboardDisplaySlot slotToEnum(String slot) { + switch(slot) { + case "list": return ScoreboardDisplaySlot.LIST; + case "sidebar": return ScoreboardDisplaySlot.SIDEBAR; + case "below_name": return ScoreboardDisplaySlot.BELOW_NAME; + } + + return ScoreboardDisplaySlot.valueOf(slot); + } } diff --git a/src/main/java/net/nocpiun/diggingcount/command/DiggingCommand.java b/src/main/java/net/nocpiun/diggingcount/command/DiggingCommand.java index 3ca9c1c..7e08016 100644 --- a/src/main/java/net/nocpiun/diggingcount/command/DiggingCommand.java +++ b/src/main/java/net/nocpiun/diggingcount/command/DiggingCommand.java @@ -3,12 +3,12 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.tree.ArgumentCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.RootCommandNode; import net.minecraft.server.command.ServerCommandSource; import net.nocpiun.diggingcount.DiggingCountPlugin; +import net.nocpiun.diggingcount.board.Board; import net.nocpiun.diggingcount.log.Log; import net.nocpiun.diggingcount.log.Message; @@ -48,6 +48,18 @@ public DiggingCommand(CommandDispatcher dispatcher, Digging }) .then( argument("args", greedyString()) + .suggests((context, builder) -> { + builder = builder.createOffset(builder.getInput().lastIndexOf(" ") + 1); + String[] inputs = context.getInput().split(" "); + + if(inputs[1].equals("enable") || inputs[1].equals("disable")) { + builder.suggest("list"); + builder.suggest("sidebar"); + builder.suggest("below_name"); + } + + return builder.buildFuture(); + }) .executes(this) .build() ) @@ -69,13 +81,17 @@ public int run(CommandContext ctx) { return 0; } + if(inputs.length < 3) { + return 0; + } + switch(inputs[1].toLowerCase()) { case "enable": - plugin.setEnabled(true); + plugin.setEnabled(Board.slotToEnum(inputs[2]), true); source.sendMessage(Message.create("§aSuccessfully enabled the scoreboard.")); break; case "disable": - plugin.setEnabled(false); + plugin.setEnabled(Board.slotToEnum(inputs[2]), false); source.sendMessage(Message.create("§aSuccessfully disabled the scoreboard.")); break; case "title":