Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
6 281
src/main/java/nl/kodari/sneeuwschep/SneeuwschepMod.java
package nl.kodari.sneeuwschep;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.permissions.Permissions;
import net.minecraft.util.Prediction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.phys.Vec3;
public final class SneeuwschepMod implements ModInitializer {
private static final String SHOVEL_TAG = "sneeuwschep:sneeuwschep";
@Override
public void onInitialize() {
PlayerBlockBreakEvents.BEFORE.register((level, player, pos, state, blockEntity) -> {
boolean sneeuwblok = isSnowBlock(state);
boolean sneeuwschep = isSneeuwschep(player.getMainHandItem());
if (sneeuwblok) {
return sneeuwschep;
}
return !sneeuwschep;
});
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
Commands.literal("sneeuwschep")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR))
.then(Commands.literal("geef")
.then(Commands.argument("speler", EntityArgument.player())
.executes(context -> {
ServerPlayer speler = EntityArgument.getPlayer(context, "speler");
speler.getInventory().placeItemBackInInventory(createSneeuwschep(), Prediction.SERVER_ONLY);
context.getSource().sendSuccess(() -> Component.literal("Sneeuwschep gegeven."), false);
return 1;
})
)
)
.then(Commands.literal("smelt")
.executes(context -> {
long aantal = smeltSneeuwEnIJs(context.getSource());
context.getSource().sendSuccess(
() -> Component.literal("Sneeuw verwijderd en ijs gesmolten in " + aantal + " blokken binnen 1000 blokken (alleen geladen chunks)."),
false
);
return 1;
})
)
));
}
private static long smeltSneeuwEnIJs(CommandSourceStack source) {
ServerLevel level = source.getLevel();
Vec3 origin = source.getPosition();
int radius = 1000;
int minBlockX = (int) Math.floor(origin.x() - radius);
int maxBlockX = (int) Math.floor(origin.x() + radius);
int minBlockZ = (int) Math.floor(origin.z() - radius);
int maxBlockZ = (int) Math.floor(origin.z() + radius);
int minChunkX = Math.floorDiv(minBlockX, 16);
int maxChunkX = Math.floorDiv(maxBlockX, 16);
int minChunkZ = Math.floorDiv(minBlockZ, 16);
int maxChunkZ = Math.floorDiv(maxBlockZ, 16);
double radiusSquared = (double) radius * radius;
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
ServerChunkCache chunks = level.getChunkSource();
long changed = 0;
for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
LevelChunk chunk = chunks.getChunkNow(chunkX, chunkZ);
if (chunk == null) {
continue;
}
int minX = Math.max(minBlockX, chunkX * 16);
int maxX = Math.min(maxBlockX, chunkX * 16 + 15);
int minZ = Math.max(minBlockZ, chunkZ * 16);
int maxZ = Math.min(maxBlockZ, chunkZ * 16 + 15);
for (int x = minX; x <= maxX; x++) {
double dx = x + 0.5 - origin.x();
for (int z = minZ; z <= maxZ; z++) {
double dz = z + 0.5 - origin.z();
double horizontalDistanceSquared = dx * dx + dz * dz;
if (horizontalDistanceSquared > radiusSquared) {
continue;
}
for (int y = level.getMinY(); y <= level.getMaxY(); y++) {
double dy = y + 0.5 - origin.y();
if (horizontalDistanceSquared + dy * dy > radiusSquared) {
continue;
}
BlockState state = chunk.getBlockState(pos.set(x, y, z));
Block replacement;
if (isSnowBlock(state)) {
replacement = Blocks.AIR;
} else if (isIceBlock(state)) {
replacement = Blocks.WATER;
} else {
continue;
}
if (level.setBlock(pos, replacement.defaultBlockState(), Block.UPDATE_ALL, Block.UPDATE_LIMIT)) {
changed++;
}
}
}
}
}
}
return changed;
}
private static boolean isSnowBlock(BlockState state) {
return state.getBlock() == Blocks.SNOW
|| state.getBlock() == Blocks.SNOW_BLOCK
|| state.getBlock() == Blocks.POWDER_SNOW;
}
private static boolean isIceBlock(BlockState state) {
return state.getBlock() == Blocks.ICE
|| state.getBlock() == Blocks.PACKED_ICE
|| state.getBlock() == Blocks.BLUE_ICE
|| state.getBlock() == Blocks.FROSTED_ICE;
}
private static boolean isSneeuwschep(ItemStack stack) {
if (stack.getItem() != Items.NETHERITE_SHOVEL) {
return false;
}
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
return customData != null && customData.copyTag().getBoolean(SHOVEL_TAG).orElse(false);
}
private static ItemStack createSneeuwschep() {
ItemStack stack = new ItemStack(Items.NETHERITE_SHOVEL);
CompoundTag tag = new CompoundTag();
tag.putBoolean(SHOVEL_TAG, true);
stack.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
stack.set(DataComponents.CUSTOM_NAME, Component.literal("Sneeuwschep"));
return stack;
}
}