From f2e1265d5ecfa511264d8e6f5f1901c4efe0783e Mon Sep 17 00:00:00 2001 From: NriotHrreion Date: Tue, 28 Jan 2025 21:58:33 +0800 Subject: [PATCH] feat: Read player mining statistics --- .../diggingcount/DiggingCountPlugin.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java b/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java index 999fd85..08dbe60 100644 --- a/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java +++ b/src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java @@ -1,21 +1,30 @@ package net.nocpiun.diggingcount; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.loader.api.FabricLoader; 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.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.stat.Stats; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.nocpiun.diggingcount.board.Board; import net.nocpiun.diggingcount.command.DiggingCommand; +import net.nocpiun.diggingcount.log.Log; import java.io.*; import java.util.HashMap; +import java.util.concurrent.atomic.AtomicInteger; public class DiggingCountPlugin { public final static HashMap defaultConfig = new HashMap<>(); @@ -36,6 +45,7 @@ public DiggingCountPlugin() { ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStart); ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStop); + ServerPlayConnectionEvents.JOIN.register(this::onPlayerJoinServer); PlayerBlockBreakEvents.AFTER.register(this::onPlayerBreakBlock); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> new DiggingCommand(dispatcher, this)); } @@ -79,7 +89,22 @@ private void onServerStop(MinecraftServer server) { this.server = null; } + private void onPlayerJoinServer(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server) { + ServerPlayerEntity player = handler.getPlayer(); + + AtomicInteger sum = new AtomicInteger(); + Stats.MINED.forEach((stat) -> { + sum.addAndGet(player.getStatHandler().getStat(stat)); + }); + + board.setCount(player, sum.get()); + counterMap.put(player.getName().getString(), sum.get()); + saveData(); + } + private void onPlayerBreakBlock(World world, PlayerEntity player, BlockPos pos, BlockState state, BlockEntity entity) { + if(player.isCreative()) return; + int currentCount = board.getCount(player) + 1; board.setCount(player, currentCount); counterMap.put(player.getName().getString(), currentCount);