Files
src
build.gradle
gradle.properties
manifest.kod
Code
9 362
src/client/java/ru/kodari/attributeswap/client/AttributeSwapClient.java
package ru.kodari.attributeswap.client;
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.network.chat.Component;
import net.minecraft.world.inventory.ContainerInput;
public class AttributeSwapClient implements ClientModInitializer {
private static KeyMapping swapKey;
private static KeyMapping toggleKey;
@Override
public void onInitializeClient() {
AttributeSwapConfig.load();
swapKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.attributeswap.swap",
InputConstants.KEY_G,
KeyMapping.Category.INVENTORY
));
toggleKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.attributeswap.toggle",
InputConstants.KEY_H,
KeyMapping.Category.INVENTORY
));
ClientTickEvents.END_CLIENT_TICK.register(AttributeSwapClient::onEndClientTick);
}
private static void onEndClientTick(Minecraft client) {
while (toggleKey.consumeClick()) {
if (client.gui.screen() == null) {
toggleEnabled(client);
}
}
while (swapKey.consumeClick()) {
swapSlots(client);
}
}
private static void toggleEnabled(Minecraft client) {
AttributeSwapConfig config = AttributeSwapConfig.get();
config.enabled = !config.enabled;
AttributeSwapConfig.save();
if (config.notifyToggle && client.player != null) {
client.player.sendOverlayMessage(Component.translatable(config.enabled
? "message.attributeswap.enabled"
: "message.attributeswap.disabled"));
}
}
private static void swapSlots(Minecraft client) {
AttributeSwapConfig config = AttributeSwapConfig.get();
if (!config.enabled
|| client.player == null
|| client.gameMode == null
|| client.gui.screen() != null
|| client.player.hasContainerOpen()
|| config.requireSneaking && !client.player.isCrouching()) {
return;
}
int firstSlot = AttributeSwapConfig.firstSlot() - 1;
int secondSlot = AttributeSwapConfig.secondSlot() - 1;
if (firstSlot == secondSlot) {
return;
}
client.gameMode.handleContainerInput(
client.player.inventoryMenu.containerId,
36 + firstSlot,
secondSlot,
ContainerInput.SWAP,
client.player
);
if (config.notifySwap) {
client.player.sendOverlayMessage(Component.translatable(
"message.attributeswap.swapped",
firstSlot + 1,
secondSlot + 1
));
}
}
}