Files
src
build.gradle
settings.gradle
Code
4 181
src/main/java/com/kodari/miningpickaxes/MiningPickaxesPlugin.java
package com.kodari.miningpickaxes;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.api.ModInitializer;
import net.minecraft.ChatFormatting;
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.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.phys.Vec3;
public final class MiningPickaxesPlugin implements ModInitializer {
private static final String PICKAXE_SIZE_KEY = "mining_pickaxe_size";
private static boolean areaMining;
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(Commands.literal("miningpickaxe")
.requires(source -> source.hasPermission(2))
.then(Commands.literal("give")
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("size", IntegerArgumentType.integer(2, 50))
.executes(context -> {
ServerPlayer target = EntityArgument.getPlayer(context, "player");
int size = IntegerArgumentType.getInteger(context, "size");
ItemStack pickaxe = createPickaxe(size);
target.getInventory().add(pickaxe);
if (!pickaxe.isEmpty()) {
target.drop(pickaxe, false);
}
context.getSource().sendSuccess(
() -> Component.literal("Gave a " + size + "x" + size
+ " mining pickaxe to " + target.getGameProfile().getName() + "."),
false
);
return 1;
}))))));
PlayerBlockBreakEvents.AFTER.register((world, player, pos, state, blockEntity) -> {
if (areaMining || !(world instanceof ServerLevel serverWorld)) {
return;
}
ItemStack tool = player.getMainHandItem();
CustomData customData = tool.get(DataComponents.CUSTOM_DATA);
if (customData == null) {
return;
}
int size = customData.copyTag().getInt(PICKAXE_SIZE_KEY);
if (size < 2 || size > 50) {
return;
}
Vec3 look = player.getLookAngle();
double x = Math.abs(look.x);
double y = Math.abs(look.y);
double z = Math.abs(look.z);
int minOffset = -(size / 2);
areaMining = true;
try {
for (int first = minOffset; first < minOffset + size; first++) {
for (int second = minOffset; second < minOffset + size; second++) {
int offsetX = 0;
int offsetY = 0;
int offsetZ = 0;
if (x >= y && x >= z) {
offsetY = first;
offsetZ = second;
} else if (y >= x && y >= z) {
offsetX = first;
offsetZ = second;
} else {
offsetX = first;
offsetY = second;
}
BlockPos targetPos = pos.offset(offsetX, offsetY, offsetZ);
if (targetPos.equals(pos) || !serverWorld.hasChunkAt(targetPos)) {
continue;
}
var targetState = serverWorld.getBlockState(targetPos);
var targetBlockEntity = serverWorld.getBlockEntity(targetPos);
if (targetState.isAir() || targetBlockEntity != null) {
continue;
}
if (!PlayerBlockBreakEvents.BEFORE.invoker().beforeBlockBreak(
serverWorld, player, targetPos, targetState, null
)) {
continue;
}
if (serverWorld.destroyBlock(targetPos, true, player)) {
PlayerBlockBreakEvents.AFTER.invoker().afterBlockBreak(
serverWorld, player, targetPos, targetState, null
);
}
}
}
} finally {
areaMining = false;
}
});
}
private static ItemStack createPickaxe(int size) {
ItemStack pickaxe = new ItemStack(Items.DIAMOND_PICKAXE);
pickaxe.set(DataComponents.CUSTOM_NAME, Component.literal(size + "x" + size + " Mining Pickaxe")
.withStyle(ChatFormatting.AQUA));
CompoundTag data = new CompoundTag();
data.putInt(PICKAXE_SIZE_KEY, size);
pickaxe.set(DataComponents.CUSTOM_DATA, CustomData.of(data));
return pickaxe;
}
}