Files
src
build.gradle
gradle.properties
Code
7 317
src/client/java/com/quickactions/QuickActionsClient.java
package com.quickactions;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ChargedProjectilesComponent;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import com.quickactions.mixin.PlayerInventoryAccessor;
import org.lwjgl.glfw.GLFW;
import java.util.List;
@Environment(EnvType.CLIENT)
public class QuickActionsClient implements ClientModInitializer {
private static final String MOD_ID = "quickactions";
public static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(
Identifier.of(MOD_ID, "main")
);
public static final KeyBinding KEY_CROSSBOW_LOAD = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.quickactions.crossbow_load", GLFW.GLFW_KEY_R, CATEGORY)
);
public static final KeyBinding KEY_PLACE_MINECART = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.quickactions.place_minecart", GLFW.GLFW_KEY_V, CATEGORY)
);
public static final KeyBinding KEY_INSTA_CRAFT = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.quickactions.insta_craft", GLFW.GLFW_KEY_C, CATEGORY)
);
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.player == null || client.world == null) return;
while (KEY_CROSSBOW_LOAD.wasPressed()) {
handleCrossbowLoad(client);
}
while (KEY_PLACE_MINECART.wasPressed()) {
handlePlaceMinecart(client);
}
while (KEY_INSTA_CRAFT.wasPressed()) {
handleInstaCraft(client);
}
});
}
// ── Crossbow Quick-Load ──────────────────────────────────────────────────
private void handleCrossbowLoad(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ItemStack held = player.getMainHandStack();
if (!(held.getItem() instanceof CrossbowItem)) {
held = player.getOffHandStack();
if (!(held.getItem() instanceof CrossbowItem)) return;
}
if (CrossbowItem.isCharged(held)) return;
// Find an arrow or firework rocket in inventory to load with
ItemStack ammo = findAmmoInInventory(player);
if (ammo.isEmpty()) return;
// Mark the crossbow as charged with the found ammo
ItemStack projectileCopy = ammo.copyWithCount(1);
held.set(DataComponentTypes.CHARGED_PROJECTILES, ChargedProjectilesComponent.of(projectileCopy));
// Consume one ammo item if not in creative
if (!player.getAbilities().creativeMode) {
ammo.decrement(1);
}
}
private ItemStack findAmmoInInventory(ClientPlayerEntity player) {
for (int i = 0; i < player.getInventory().size(); i++) {
ItemStack stack = player.getInventory().getStack(i);
if (stack.getItem() == Items.ARROW
|| stack.getItem() == Items.TIPPED_ARROW
|| stack.getItem() == Items.SPECTRAL_ARROW
|| stack.getItem() == Items.FIREWORK_ROCKET) {
return stack;
}
}
return ItemStack.EMPTY;
}
// ── Minecart Place ───────────────────────────────────────────────────────
private void handlePlaceMinecart(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ClientWorld world = client.world;
// Find a minecart in the inventory
int minecartSlot = findMinecartSlot(player);
if (minecartSlot == -1) return;
// Find the nearest rail block the player is looking at or nearby
BlockPos railPos = findNearbyRail(player, world);
if (railPos == null) return;
// Send a use-item-on-block packet to place the minecart
ItemStack minecartStack = player.getInventory().getStack(minecartSlot);
// Switch to minecart slot if it's a hotbar slot, then interact
int prevSlot = ((PlayerInventoryAccessor) player.getInventory()).quickactions$getSelectedSlot();
if (minecartSlot < 9) {
((PlayerInventoryAccessor) player.getInventory()).quickactions$setSelectedSlot(minecartSlot);
}
client.interactionManager.interactBlock(
player,
net.minecraft.util.Hand.MAIN_HAND,
new BlockHitResult(
Vec3d.ofCenter(railPos).add(0, 0.5, 0),
Direction.UP,
railPos,
false
)
);
if (minecartSlot < 9) {
((PlayerInventoryAccessor) player.getInventory()).quickactions$setSelectedSlot(prevSlot);
}
}
private int findMinecartSlot(ClientPlayerEntity player) {
for (int i = 0; i < player.getInventory().size(); i++) {
ItemStack stack = player.getInventory().getStack(i);
if (stack.getItem() == Items.MINECART
|| stack.getItem() == Items.CHEST_MINECART
|| stack.getItem() == Items.HOPPER_MINECART
|| stack.getItem() == Items.TNT_MINECART
|| stack.getItem() == Items.FURNACE_MINECART) {
return i;
}
}
return -1;
}
private BlockPos findNearbyRail(ClientPlayerEntity player, ClientWorld world) {
// First check if the crosshair is aimed at a rail
if (client_crosshairRail(player, world) != null) {
return client_crosshairRail(player, world);
}
// Scan a 3-block radius around the player's feet
BlockPos origin = player.getBlockPos();
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
BlockPos pos = origin.add(dx, 0, dz);
if (isRail(world, pos)) return pos;
pos = origin.add(dx, -1, dz);
if (isRail(world, pos)) return pos;
pos = origin.add(dx, 1, dz);
if (isRail(world, pos)) return pos;
}
}
return null;
}
private BlockPos client_crosshairRail(ClientPlayerEntity player, ClientWorld world) {
if (!(MinecraftClient.getInstance().crosshairTarget instanceof BlockHitResult hit)) return null;
BlockPos pos = hit.getBlockPos();
return isRail(world, pos) ? pos : null;
}
private boolean isRail(World world, BlockPos pos) {
net.minecraft.block.Block block = world.getBlockState(pos).getBlock();
return block == net.minecraft.block.Blocks.RAIL
|| block == net.minecraft.block.Blocks.POWERED_RAIL
|| block == net.minecraft.block.Blocks.DETECTOR_RAIL
|| block == net.minecraft.block.Blocks.ACTIVATOR_RAIL;
}
// ── Insta-Craft ──────────────────────────────────────────────────────────
private void handleInstaCraft(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ScreenHandler handler = player.currentScreenHandler;