Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 263
src/client/java/com/kodari/trial/TrialClient.java
package com.kodari.trial;
import com.kodari.trial.mixin.MinecraftClientInvoker;
import net.fabricmc.api.ClientModInitializer;
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.option.KeyBinding;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public final class TrialClient implements ClientModInitializer {
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("trial", "controls"));
private static final KeyBinding USE_MACE = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.trial.use_mace", GLFW.GLFW_KEY_V, CATEGORY)
);
private static final KeyBinding USE_SPEAR = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.trial.use_spear", GLFW.GLFW_KEY_B, CATEGORY)
);
private static boolean maceWasDown;
private static boolean spearWasDown;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(TrialClient::onClientTick);
}
private static void onClientTick(MinecraftClient client) {
boolean maceDown = USE_MACE.isPressed();
boolean maceQueued = drainQueuedPresses(USE_MACE);
boolean useMace = !maceWasDown && (maceDown || maceQueued);
maceWasDown = maceDown;
boolean spearDown = USE_SPEAR.isPressed();
boolean spearQueued = drainQueuedPresses(USE_SPEAR);
boolean useSpear = !spearWasDown && (spearDown || spearQueued);
spearWasDown = spearDown;
if (client.player == null || client.interactionManager == null || client.currentScreen != null) {
return;
}
if (useMace) {
activateWeapon(client, false);
}
if (useSpear) {
activateWeapon(client, true);
}
}
private static boolean drainQueuedPresses(KeyBinding keyBinding) {
boolean queued = false;
while (keyBinding.wasPressed()) {
queued = true;
}
return queued;
}
private static void activateWeapon(MinecraftClient client, boolean spear) {
var player = client.player;
PlayerInventory inventory = player.getInventory();
int weaponInventorySlot = findWeaponSlot(inventory, spear);
if (weaponInventorySlot < 0) {
return;
}
int selectedHotbarSlot = inventory.getSelectedSlot();
if (weaponInventorySlot == selectedHotbarSlot) {
((MinecraftClientInvoker) (Object) client).trial$doAttack();
return;
}
ScreenHandler handler = player.currentScreenHandler;
int handlerSlotId = findHandlerSlot(handler, inventory, weaponInventorySlot);
if (handlerSlotId < 0) {
return;
}
client.interactionManager.clickSlot(
handler.syncId,
handlerSlotId,
selectedHotbarSlot,
SlotActionType.SWAP,
player
);
try {
((MinecraftClientInvoker) (Object) client).trial$doAttack();
} finally {
client.interactionManager.clickSlot(
handler.syncId,
handlerSlotId,
selectedHotbarSlot,
SlotActionType.SWAP,
player
);
}
}
private static int findWeaponSlot(PlayerInventory inventory, boolean spear) {
for (int slot = 0; slot < 36; slot++) {
if (isWeapon(inventory.getStack(slot), spear)) {
return slot;
}
}
return -1;
}
private static boolean isWeapon(ItemStack stack, boolean spear) {
var item = stack.getItem();
if (!spear) {
return item == Items.MACE;
}
return item == Items.WOODEN_SPEAR
|| item == Items.STONE_SPEAR
|| item == Items.COPPER_SPEAR
|| item == Items.IRON_SPEAR
|| item == Items.GOLDEN_SPEAR
|| item == Items.DIAMOND_SPEAR
|| item == Items.NETHERITE_SPEAR;
}
private static int findHandlerSlot(ScreenHandler handler, PlayerInventory inventory, int inventorySlot) {
for (int slotId = 0; slotId < handler.slots.size(); slotId++) {
Slot slot = handler.getSlot(slotId);
if (slot.inventory == inventory && slot.getIndex() == inventorySlot) {
return slotId;
}
}
return -1;
}
}