Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
10 1,034
src/client/java/com/kodari/advancedhotbarmanager/AdvancedHotbarManagerClient.java
package com.kodari.advancedhotbarmanager;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ContainerInput;
import net.minecraft.world.item.ItemStack;
import org.lwjgl.glfw.GLFW;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class AdvancedHotbarManagerClient implements ClientModInitializer {
private static final Logger LOGGER = Logger.getLogger("AdvancedHotbarManager");
private static final boolean[] SAVE_CHORD_WAS_DOWN = new boolean[9];
private static final boolean[] LOAD_CHORD_WAS_DOWN = new boolean[9];
private static KeyMapping openManagerKey;
@Override
public void onInitializeClient() {
HotbarStore.load();
openManagerKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.advancedhotbarmanager.open",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_O,
KeyMapping.Category.MISC
));
ClientTickEvents.END_CLIENT_TICK.register(AdvancedHotbarManagerClient::onClientTick);
}
private static void onClientTick(Minecraft client) {
if (openManagerKey.consumeClick() && client.player != null && client.level != null && client.gui.screen() == null) {
client.gui.setScreen(new HotbarManagerScreen(-1));
}
boolean canUseChord = client.player != null
&& client.level != null
&& client.gameMode != null
&& client.gui.screen() == null
&& !client.player.hasInfiniteMaterials();
if (!canUseChord) {
Arrays.fill(SAVE_CHORD_WAS_DOWN, false);
Arrays.fill(LOAD_CHORD_WAS_DOWN, false);
return;
}
boolean saveActivatorDown = client.options.keySaveHotbarActivator.isDown();
boolean loadActivatorDown = client.options.keyLoadHotbarActivator.isDown();
for (int slot = 0; slot < Inventory.getSelectionSize(); slot++) {
boolean saveDown = saveActivatorDown && client.options.keyHotbarSlots[slot].isDown();
boolean loadDown = loadActivatorDown && client.options.keyHotbarSlots[slot].isDown();
if (saveDown && !SAVE_CHORD_WAS_DOWN[slot]) {
HotbarStore.SavedSet existing = HotbarStore.findQuickSlot(HotbarStore.activeTab(), slot);
if (existing == null) {
try {
HotbarStore.saveQuickSlot(slot, client.player);
} catch (RuntimeException exception) {
LOGGER.log(Level.WARNING, "Nie udało się zapisać skrótu hotbara", exception);
}
} else {
client.gui.setScreen(new HotbarManagerScreen(slot));
}
}
if (loadDown && !LOAD_CHORD_WAS_DOWN[slot]) {
HotbarStore.SavedSet set = HotbarStore.findQuickSlot(HotbarStore.activeTab(), slot);
if (set != null) {
applySet(client, set);
}
}
SAVE_CHORD_WAS_DOWN[slot] = saveDown;
LOAD_CHORD_WAS_DOWN[slot] = loadDown;
}
}
static void applySet(Minecraft client, HotbarStore.SavedSet set) {
LocalPlayer player = client.player;
if (player == null || client.level == null || client.gameMode == null) {
return;
}
List<ItemStack> desired;
try {
desired = HotbarStore.decode(set, player);
} catch (RuntimeException exception) {
LOGGER.log(Level.WARNING, "Nie udało się odczytać profilu hotbara " + set.name, exception);
return;
}
if (player.hasInfiniteMaterials()) {
Inventory inventory = player.getInventory();
for (int slot = 0; slot < Inventory.getSelectionSize(); slot++) {
ItemStack stack = desired.get(slot).copy();
inventory.setItem(slot, stack);
client.gameMode.handleCreativeModeItemAdd(stack, 36 + slot);
}
player.inventoryMenu.broadcastChanges();
return;
}
if (player.containerMenu != player.inventoryMenu || client.gameMode.isServerControlledInventory()) {
return;
}
arrangeOwnedItems(client, player, desired);
}
private static void arrangeOwnedItems(Minecraft client, LocalPlayer player, List<ItemStack> desired) {
Inventory inventory = player.getInventory();
boolean[] reservedHotbarSlots = new boolean[Inventory.getSelectionSize()];
for (int hotbarSlot = 0; hotbarSlot < Inventory.getSelectionSize(); hotbarSlot++) {
ItemStack wanted = desired.get(hotbarSlot);
if (wanted.isEmpty()) {
continue;
}
ItemStack current = inventory.getItem(hotbarSlot);
if (!current.isEmpty() && ItemStack.isSameItemSameComponents(current, wanted)) {
reservedHotbarSlots[hotbarSlot] = true;
continue;
}
int sourceInventorySlot = -1;
for (int inventorySlot = 0; inventorySlot < 36; inventorySlot++) {
if (inventorySlot < Inventory.getSelectionSize() && reservedHotbarSlots[inventorySlot]) {
continue;
}
ItemStack available = inventory.getItem(inventorySlot);
if (!available.isEmpty() && ItemStack.isSameItemSameComponents(available, wanted)) {
sourceInventorySlot = inventorySlot;
break;
}
}
if (sourceInventorySlot < 0) {
continue;
}
int sourceMenuSlot = sourceInventorySlot < Inventory.getSelectionSize() ? 36 + sourceInventorySlot : sourceInventorySlot;
int destinationMenuSlot = 36 + hotbarSlot;
clickInventorySlot(client, player, sourceMenuSlot);
clickInventorySlot(client, player, destinationMenuSlot);
clickInventorySlot(client, player, sourceMenuSlot);
reservedHotbarSlots[hotbarSlot] = true;
}
}
private static void clickInventorySlot(Minecraft client, LocalPlayer player, int menuSlot) {
client.gameMode.handleContainerInput(
player.inventoryMenu.containerId,
menuSlot,
0,
ContainerInput.PICKUP,
player
);
}
}