Skip to content

Commit

Permalink
feat: Enable / disable scoreboard in other slots & fix: Counting data…
Browse files Browse the repository at this point in the history
… is not stored into dat file
  • Loading branch information
NriotHrreion committed Jan 26, 2025
1 parent 41847cb commit e106493
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 18 deletions.
67 changes: 55 additions & 12 deletions src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@
import java.util.HashMap;

public class DiggingCountPlugin {
public final static HashMap<String, Object> defaultConfig = new HashMap<>();

private MinecraftServer server;

private File configFile;
Expand All @@ -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);
Expand All @@ -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());
}

Expand All @@ -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")
Expand Down Expand Up @@ -97,13 +116,28 @@ public <T> 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() {
Expand All @@ -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);
}
}
16 changes: 13 additions & 3 deletions src/main/java/net/nocpiun/diggingcount/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 19 additions & 3 deletions src/main/java/net/nocpiun/diggingcount/command/DiggingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,6 +48,18 @@ public DiggingCommand(CommandDispatcher<ServerCommandSource> 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()
)
Expand All @@ -69,13 +81,17 @@ public int run(CommandContext<ServerCommandSource> 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":
Expand Down

0 comments on commit e106493

Please sign in to comment.