Tyler Daniels

if an anybody types /challenge in the chat box every minute a random naturally spawning block will be erased from the game.

Kodari AI
KodariSep 27 at 4:02 am 2
BlockEraserChallenge
Implements a chat command that triggers a global challenge where a random naturally-spawning block is erased from the world every minute. The plugin listens for the /challenge command, then initiates a repeating timer that selects a random block type from the natural spawn list, removes all instances of that block from the world, and announces the erased block to all players. The challenge continues indefinitely until the server restarts or the command is disabled.
  1. Register /challenge as a server command.

  2. Run one removal every 60 seconds while the challenge is active, avoiding player-modified blocks where detectable.

  3. Create the Fabric 26.2 project metadata and initializer.

Created the Fabric 26.2 mod. Any player can run /challenge; it starts or resets a timer that removes one surface block from an already-loaded area near a random online player every 60 seconds.

The mod skips positions players interact with after the challenge starts. It cannot identify player edits made before tracking begins.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 183
src/main/java/com/example/challenge/ChallengeMod.java
package com.example.challenge;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionResult;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.Heightmap;

public class ChallengeMod implements ModInitializer {
	private static final int TICKS_PER_MINUTE = 20 * 60;
	private static final Map<MinecraftServer, ChallengeState> CHALLENGES = new HashMap<>();

	@Override
	public void onInitialize() {
		CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
				Commands.literal("challenge").executes(context -> {
					MinecraftServer server = context.getSource().getServer();
					ChallengeState state = CHALLENGES.computeIfAbsent(server, ignored -> new ChallengeState());
					state.ticksUntilRemoval = TICKS_PER_MINUTE;
					context.getSource().sendSuccess(
							() -> Component.literal("Challenge started: one surface block will be removed every 60 seconds."),
							false
					);
					return 1;
				})
		));

		ServerTickEvents.END_SERVER_TICK.register(server -> {
			ChallengeState state = CHALLENGES.get(server);
			if (state == null) {
				return;
			}

			if (--state.ticksUntilRemoval <= 0) {
				state.ticksUntilRemoval = TICKS_PER_MINUTE;
				eraseRandomNaturalBlock(server, state);
			}
		});

		UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
			if (world instanceof ServerLevel level) {
				ChallengeState state = CHALLENGES.get(level.getServer());
				if (state != null) {
					BlockPos clicked = hitResult.getBlockPos();
					state.playerModifiedBlocks.add(new ModifiedBlock(level, clicked));
					state.playerModifiedBlocks.add(new ModifiedBlock(level, clicked.relative(hitResult.getDirection())));
				}
			}
			return InteractionResult.PASS;
		});

		ServerLifecycleEvents.SERVER_STOPPED.register(CHALLENGES::remove);
	}

	private static void eraseRandomNaturalBlock(MinecraftServer server, ChallengeState state) {
		List<ServerLevel> levelsWithPlayers = new ArrayList<>();
		for (ServerLevel level : server.getAllLevels()) {
			if (!level.players().isEmpty()) {
				levelsWithPlayers.add(level);
			}
		}
		if (levelsWithPlayers.isEmpty()) {
			return;
		}

		ServerLevel level = levelsWithPlayers.get(ThreadLocalRandom.current().nextInt(levelsWithPlayers.size()));
		var random = level.getRandom();
		var players = level.players();
		BlockPos center = players.get(random.nextInt(players.size())).blockPosition();

		for (int attempt = 0; attempt < 32; attempt++) {
			int x = center.getX() + random.nextInt(65) - 32;
			int z = center.getZ() + random.nextInt(65) - 32;
			if (!level.isLoaded(new BlockPos(x, level.getMinY(), z))) {
				continue;
			}

			int y = level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z) - 1;
			if (y < level.getMinY()) {
				continue;
			}
			BlockPos target = new BlockPos(x, y, z);
			if (state.playerModifiedBlocks.contains(new ModifiedBlock(level, target))
					|| level.getBlockState(target).isAir()) {
				continue;
			}

			level.setBlock(target, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
			return;
		}
	}

	private static final class ChallengeState {
		private int ticksUntilRemoval = TICKS_PER_MINUTE;
		private final Set<ModifiedBlock> playerModifiedBlocks = new HashSet<>();
	}

	private record ModifiedBlock(ServerLevel level, BlockPos pos) {
	}
}