Skip to content

add atribute swapping EXAMPLE CODE INCLUDED (Refinening might be reqired) #6048

@syrahnet

Description

@syrahnet

Describe the feature

you could add a module that the player has options to select either hotbar slot witch then the player can add 2 hotbar slots one in first and the other in second and when they attack it will swap between them or it will swap from whatever hotbar slot is active to the other number then back of the other mode where players can select 2 items to swap between and can create presets that can be hooked up to keybinds so for example if the player pressed the M key it will be whatever preset the player set for example between a mace and a sword and if the player presses the S key for example it will activate the coresponding preset for example sword and axe or sword and spear allowing players to atribute swap with ease
Below i have included some example code that may reqire refining
HotbarSwapModule.java
`package your.mod.module.hotbarswap;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;

public class HotbarSwapModule {

private final SwapState state = new SwapState();
private final PresetRegistry presets = new PresetRegistry();

public SwapState getState() {
    return state;
}

public PresetRegistry getPresets() {
    return presets;
}

/**
 * Call this from your attack event / mixin / input hook
 */
public void onAttack() {
    if (!state.attackSwapEnabled) return;

    PlayerEntity player = MinecraftClient.getInstance().player;
    if (player == null) return;

    HotbarSwapLogic.swapOnAttack(player, state);
}

/**
 * Call this from your keybind logic
 */
public void activatePreset(String presetId) {
    PlayerEntity player = MinecraftClient.getInstance().player;
    if (player == null) return;

    presets.activate(player, presetId);
}

}
SwapState.javapackage your.mod.module.hotbarswap;

public class SwapState {

public boolean attackSwapEnabled = false;

// true = active slot ↔ target slot
// false = slotA ↔ slotB toggle
public boolean activeToOther = false;

public int slotA = 0; // 0–8
public int slotB = 1;

boolean internalToggle = false;

}
HotbarSwapLogic.javapackage your.mod.module.hotbarswap;

import net.minecraft.entity.player.PlayerEntity;

public final class HotbarSwapLogic {

private HotbarSwapLogic() {}

public static void swapOnAttack(PlayerEntity player, SwapState state) {
    int current = player.getInventory().selectedSlot;

    if (state.activeToOther) {
        int target = current == state.slotA
                ? state.slotB
                : state.slotA;

        player.getInventory().selectedSlot = target;
    } else {
        player.getInventory().selectedSlot =
                state.internalToggle ? state.slotA : state.slotB;

        state.internalToggle = !state.internalToggle;
    }
}

}
PresetRegistry.javapackage your.mod.module.hotbarswap;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.util.*;

public class PresetRegistry {

private final Map<String, Preset> presets = new HashMap<>();

public void register(String id, Preset preset) {
    presets.put(id, preset);
}

public void remove(String id) {
    presets.remove(id);
}

public void activate(PlayerEntity player, String id) {
    Preset preset = presets.get(id);
    if (preset == null) return;

    preset.apply(player);
}

public Set<String> ids() {
    return presets.keySet();
}

}
Preset.javapackage your.mod.module.hotbarswap;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.util.List;

public class Preset {

private final List<Identifier> items;

public Preset(List<Identifier> items) {
    this.items = items;
}

public void apply(PlayerEntity player) {
    for (int slot = 0; slot < 9; slot++) {
        ItemStack stack = player.getInventory().getStack(slot);
        if (stack.isEmpty()) continue;

        Identifier id = Registries.ITEM.getId(stack.getItem());
        if (items.contains(id)) {
            player.getInventory().selectedSlot = slot;
            return;
        }
    }
}

}
`

Before submitting a suggestion

  • This feature doesn't already exist in the client. (I have checked every module and their settings on the latest dev build)

  • This wasn't already suggested. (I have searched suggestions on GitHub and read the FAQ)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions