Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 366
src/client/java/com/kodari/vaultloot/VaultLootClient.java
package com.kodari.vaultloot;
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.block.BlockState;
import net.minecraft.block.VaultBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.VaultBlockEntity;
import net.minecraft.block.enums.VaultState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
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 org.lwjgl.glfw.GLFW;
public class VaultLootClient implements ClientModInitializer {
private static final Identifier KEY_CATEGORY_ID = Identifier.of("kodari_vault_loot", "controls");
private static final KeyBinding OPEN_SCREEN = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.kodari_vault_loot.open",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_G,
KeyBinding.Category.create(KEY_CATEGORY_ID)
));
private static Item selectedItem;
private static int clientTicks;
private static int lastInteractionTick = -1000;
private static int lastVaultX;
private static int lastVaultY;
private static int lastVaultZ;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(VaultLootClient::tick);
}
public static Item getSelectedItem() {
return selectedItem;
}
public static void setSelectedItem(Item item) {
selectedItem = item;
}
private static void tick(MinecraftClient client) {
clientTicks++;
if (OPEN_SCREEN.wasPressed()) {
client.setScreen(new LootSelectionScreen());
}
tryUnlockMatchingVault(client);
}
private static void tryUnlockMatchingVault(MinecraftClient client) {
if (selectedItem == null || client.world == null || client.player == null || client.interactionManager == null) {
return;
}
if (clientTicks - lastInteractionTick < 10) {
return;
}
ClientPlayerEntity player = client.player;
Hand hand = findKeyHand(player);
if (hand == null) {
return;
}
BlockPos playerPos = player.getBlockPos();
for (BlockPos pos : BlockPos.iterate(
playerPos.getX() - 8,
playerPos.getY() - 5,
playerPos.getZ() - 8,
playerPos.getX() + 8,
playerPos.getY() + 5,
playerPos.getZ() + 8
)) {
if (pos.getX() == lastVaultX && pos.getY() == lastVaultY && pos.getZ() == lastVaultZ
&& clientTicks - lastInteractionTick < 100) {
continue;
}
BlockState state = client.world.getBlockState(pos);
if (!(state.getBlock() instanceof VaultBlock)
|| !state.get(VaultBlock.OMINOUS)
|| state.get(VaultBlock.VAULT_STATE) != VaultState.ACTIVE) {
continue;
}
BlockEntity blockEntity = client.world.getBlockEntity(pos);
if (!(blockEntity instanceof VaultBlockEntity vault)) {
continue;
}
ItemStack displayItem = vault.getSharedData().getDisplayItem();
if (displayItem.isEmpty() || !displayItem.isOf(selectedItem)) {
continue;
}
int previousSlot = player.getInventory().getSelectedSlot();
if (hand == Hand.MAIN_HAND) {
int keySlot = findHotbarKey(player);
if (keySlot >= 0 && keySlot != previousSlot) {
player.getInventory().setSelectedSlot(keySlot);
}
}
client.interactionManager.interactBlock(
player,
hand,
new BlockHitResult(Vec3d.ofCenter(pos), Direction.UP, pos, false)
);
if (hand == Hand.MAIN_HAND && player.getInventory().getSelectedSlot() != previousSlot) {
player.getInventory().setSelectedSlot(previousSlot);
}
lastInteractionTick = clientTicks;
lastVaultX = pos.getX();
lastVaultY = pos.getY();
lastVaultZ = pos.getZ();
return;
}
}
private static Hand findKeyHand(ClientPlayerEntity player) {
if (findHotbarKey(player) >= 0) {
return Hand.MAIN_HAND;
}
if (player.getInventory().getStack(PlayerInventorySlots.OFF_HAND).isOf(Items.OMINOUS_TRIAL_KEY)) {
return Hand.OFF_HAND;
}
return null;
}
private static int findHotbarKey(ClientPlayerEntity player) {
for (int slot = 0; slot < PlayerInventorySlots.HOTBAR_SIZE; slot++) {
if (player.getInventory().getStack(slot).isOf(Items.OMINOUS_TRIAL_KEY)) {
return slot;
}
}
return -1;
}
private static final class PlayerInventorySlots {
private static final int HOTBAR_SIZE = 9;
private static final int OFF_HAND = 40;
}
}