Skip to content

Commit

Permalink
feat: Read player mining statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
NriotHrreion committed Jan 28, 2025
1 parent df0283d commit f2e1265
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/net/nocpiun/diggingcount/DiggingCountPlugin.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> defaultConfig = new HashMap<>();
Expand All @@ -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));
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f2e1265

Please sign in to comment.