Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ archivesBaseName = project.archives_base_name
version = project.mod_version + "+mc" + project.minecraft_version

repositories {
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases/" }

exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
}

dependencies {
Expand All @@ -18,6 +32,16 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"


modApi("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config}") {
exclude(group: "net.fabricmc.fabric-api")
}

modApi "com.terraformersmc:modmenu:${project.modmenu}"

// only in classpath
modCompileOnly "maven.modrinth:autohud:${project.autohud_version}"
}

processResources {
Expand Down
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ loader_version = 0.14.21

# Dependencies
fabric_version = 0.85.0+1.20.1

# These can be easily found here https://linkie.shedaniel.dev/dependencies
cloth_config = 11.1.106
modmenu = 7.1.0

autohud_version = 6.1+1.20
64 changes: 62 additions & 2 deletions src/main/java/com/turtlearmymc/slotswap/SwapManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.turtlearmymc.slotswap;

import com.turtlearmymc.slotswap.compat.LoadCompat;
import com.turtlearmymc.slotswap.config.SwapConfig;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.slot.SlotActionType;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

Expand All @@ -16,9 +22,12 @@ public class SwapManager implements ClientModInitializer {
public static final int ROW_COUNT = PlayerInventory.MAIN_SIZE / PlayerInventory.getHotbarSize();
private static int currentRow;
private static boolean active;
public static SwapConfig CONFIG;

private static @Nullable KeyBinding selectSlotKey = null;

private static LoadCompat.Action compatibilityAction;

public static boolean isSelectKeyDown() {
if (selectSlotKey != null) {
return selectSlotKey.isPressed();
Expand All @@ -44,6 +53,7 @@ public static void tryStartSwap() {
active = true;
currentRow = 0;
}
compatibilityAction.invoke();
}

public static void cancel() {
Expand All @@ -54,6 +64,10 @@ public static int getCurrentRow() {
return active ? currentRow : 0;
}

private static ClientPlayerInteractionManager getInteractionManager() {
return MinecraftClient.getInstance().interactionManager;
}

public static void scroll(PlayerInventory inv, int rowDelta) {
tryStartSwap();
final int col = inv.selectedSlot;
Expand All @@ -69,16 +83,62 @@ public static void scroll(PlayerInventory inv, int rowDelta) {
}
}

private static void swapSlots(PlayerInventory inv, int slotA, int slotB) {

// clickSlot has a weird indexing:
// first slot arg is slot index, what is kinda random
// https://videa.hu/videok/film-animacio/laputa-az-egi-palota-124-perc-1986-KZZvhnBm2KnCxTNx?start=1920.560579

// second item is normal slot index

final int slotAIndex = slotA < 9 ? slotA + PlayerInventory.MAIN_SIZE : slotA;
getInteractionManager().clickSlot(0, slotAIndex, slotB, SlotActionType.SWAP, MinecraftClient.getInstance().player);
}


public static void tryDoSwap(PlayerInventory inv) {
if (!active) return;
active = false;
if (currentRow == 0) return; // Hotbar selected
final int slot = rowColToSlot(currentRow, inv.selectedSlot);
MinecraftClient.getInstance().interactionManager.pickFromInventory(slot);
switch (CONFIG.swapMode) {
case COMPATIBLE -> {
final int slot = rowColToSlot(currentRow, inv.selectedSlot);
getInteractionManager().pickFromInventory(slot);
}
case SWAP -> {
final int slotA = rowColToSlot(currentRow, inv.selectedSlot);
final int slotB = rowColToSlot(0, inv.selectedSlot);
swapSlots(inv, slotA, slotB);
}
case SHIFT -> {
final int column = inv.selectedSlot;
final int distance = currentRow;

for (int cycleStart = 0, nMoved = 0; nMoved != ROW_COUNT; cycleStart++) {
int i = cycleStart;
int displaced = i;
do {
i += distance;
if (i >= ROW_COUNT)
i -= ROW_COUNT;
swapSlots(inv, rowColToSlot(displaced, column), rowColToSlot(i, column));
displaced = i;
nMoved++;
} while ((i + distance) % ROW_COUNT != cycleStart);
nMoved++;
}
}
}
}

@Override
public void onInitializeClient() {

AutoConfig.register(SwapConfig.class, JanksonConfigSerializer::new);
CONFIG = AutoConfig.getConfigHolder(SwapConfig.class).get();

compatibilityAction = LoadCompat.getActivationAction();

selectSlotKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.slotswap.swap",
InputUtil.Type.KEYSYM,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.turtlearmymc.slotswap.compat;

import mod.crend.autohud.component.Component;

public class AutoHudCompatibility implements LoadCompat.Action {

@Override
public void invoke() {
Component.Hotbar.reveal();
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/turtlearmymc/slotswap/compat/LoadCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.turtlearmymc.slotswap.compat;

import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.NotNull;

public class LoadCompat {


@NotNull
public static Action getActivationAction() {

return getAutoHudAction();
}

private static Action getAutoHudAction() {
if (FabricLoader.getInstance().isModLoaded("autohud")) {
return new AutoHudCompatibility();
} else {
return () -> {};
}
}


@FunctionalInterface
public interface Action {
void invoke();
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/turtlearmymc/slotswap/config/ModMenuEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.turtlearmymc.slotswap.config;

import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import me.shedaniel.autoconfig.AutoConfig;

public class ModMenuEntry implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> AutoConfig.getConfigScreen(SwapConfig.class, parent).get();
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/turtlearmymc/slotswap/config/SwapConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.turtlearmymc.slotswap.config;

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;

@Config(name = "slotswap")
public class SwapConfig implements ConfigData {

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public SwapMode swapMode = SwapMode.COMPATIBLE;

public enum SwapMode {
/**
* Swap items using vanilla pick-item packet, likely safe to use everywhere but having issues
*/
COMPATIBLE,

/**
* Use PlayerHandler swap packet, precisely swaps the items, may trigger anti-cheat
*/
SWAP,
/**
* Use same method as SWAP, but instead of swapping items, it cyclic-shift the column, it shouldn't shuffle items in inventory.
*/
SHIFT,
}
}
3 changes: 0 additions & 3 deletions src/main/resources/assets/slotswap/lang/en_US.json

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/resources/assets/slotswap/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key.slotswap.swap": "Slot swap",
"text.autoconfig.slotswap.option.swapMode": "Swap mode",
"text.autoconfig.slotswap.option.swapMode.@Tooltip": "COMPATIBLE: Item swap with item pick packet, it may swap items randomly but is unlikely to trigger anti-cheat\nSWAP: Simply swap the item with the selected\nSHIFT: Shift the whole column up/down when swapping items"
}
3 changes: 3 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"entrypoints": {
"client": [
"com.turtlearmymc.slotswap.SwapManager"
],
"modmenu": [
"com.turtlearmymc.slotswap.config.ModMenuEntry"
]
},
"mixins": [
Expand Down