Skip to content

Commit

Permalink
fixed swaps inside thinventories
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-159 committed Feb 10, 2024
1 parent b32bc41 commit 3cb25f8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 81 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/github/matt159/dws/DoubleWideSurprise.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ public class DoubleWideSurprise {
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
proxy.preInit(event);
proxy.registerHandlers();
}

@Mod.EventHandler
public void init(FMLInitializationEvent event) {
proxy.init(event);
proxy.registerKeyBindings();
}

@Mod.EventHandler
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import net.minecraft.client.Minecraft;

public class HotbarKeyhandler extends KeyHandler {
public HotbarKeyhandler(String description, int keyCode, String category, int index) {
public class HotbarKey extends KeyHandler {
public HotbarKey(String description, int keyCode, String category, int index) {
super(description, keyCode, category, new HotbarKeyhandlerCallback(index));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.matt159.dws.events.keybinds;

import com.github.matt159.dws.Tags;
import com.github.matt159.dws.network.DWSInventorySwapPacket;
import com.github.matt159.dws.network.PacketHandler;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.StatCollector;
import org.lwjgl.input.Keyboard;

@SideOnly(Side.CLIENT)
public class InventorySwapKey extends KeyHandler {
public InventorySwapKey() {
super(StatCollector.translateToLocal("dws.keybind.inventoryswap.name"),
Keyboard.KEY_H,
Tags.MODNAME,
new SwapKeyCallback());
}

private static class SwapKeyCallback implements KeyHandler.CallBack {
@Override
public void onPress() {
PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(false));
}

@Override
public void tick() {

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.matt159.dws.mixin.mixins.client.minecraft;

import com.github.matt159.dws.events.keybinds.HotbarKeyhandler;
import com.github.matt159.dws.events.keybinds.HotbarKey;
import lombok.val;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -25,10 +25,10 @@ private KeyBinding[] redirectHotbarKeybind(GameSettings instance) {
for (int index = 0; index < hotbarKeybinds.length; index++) {
val hotbarKeybind = hotbarKeybinds[index];

val hotbarKeyhandler = new HotbarKeyhandler(hotbarKeybind.getKeyDescription(),
hotbarKeybind.getKeyCode(),
hotbarKeybind.getKeyCategory(),
index);
val hotbarKeyhandler = new HotbarKey(hotbarKeybind.getKeyDescription(),
hotbarKeybind.getKeyCode(),
hotbarKeybind.getKeyCategory(),
index);

hotbarKeyhandlers[index] = hotbarKeyhandler;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.matt159.dws.mixin.mixins.client.minecraft.gui;

import com.github.matt159.dws.config.DWSConfig;
import com.github.matt159.dws.events.keybinds.InventorySwapKey;
import com.github.matt159.dws.interfaces.IDWSGui;
import com.github.matt159.dws.inventory.slots.SlotDWS;
import com.github.matt159.dws.network.DWSInventorySwapPacket;
Expand All @@ -15,6 +16,8 @@
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

import com.github.matt159.dws.registry.Keybindings;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -47,21 +50,9 @@ private int modifyDefaultXSize(int constant) {
@Inject(method = "keyTyped",
at = @At(value = "TAIL"),
require = 1)
private void injectDWSHotkey(char p_73869_1_, int key, CallbackInfo ci) {
KeyBinding swapKey;
try {
String description = StatCollector.translateToLocal("keybind.inventoryswap");

swapKey = Arrays.stream(Minecraft.getMinecraft().gameSettings.keyBindings)
.filter(keyBind -> keyBind.getKeyDescription().equals(description))
.findFirst()
.get();
} catch (NoSuchElementException e) {
swapKey = null;
}

if (swapKey != null && swapKey.getKeyCode() == key) {
PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(!(this instanceof IDWSGui)));
private void injectDWSHotkey(char typedChar, int key, CallbackInfo ci) {
if (Keybindings.SwapKey.getKeyCode() == key && !(this instanceof IDWSGui)) {
PacketHandler.INSTANCE.sendToServer(new DWSInventorySwapPacket(true));
}
}

Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/github/matt159/dws/proxy/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package com.github.matt159.dws.proxy;

import com.github.matt159.dws.events.keybinds.DWSSwapKeyHandler;
import com.github.matt159.dws.inventory.slots.SlotType;
import com.github.matt159.dws.registry.Keybindings;

import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
import cpw.mods.fml.common.event.FMLServerStartedEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLServerStoppedEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;

public class ClientProxy extends CommonProxy {
public DWSSwapKeyHandler keyHandler;

public void preInit(FMLPreInitializationEvent event) {
super.preInit(event);
}
Expand All @@ -31,11 +23,6 @@ public void postInit(FMLPostInitializationEvent event) {
super.postInit(event);
}

@Override
public void registerKeyBindings() {
keyHandler = new DWSSwapKeyHandler();
}

@Override
public ResourceLocation getSlotHintTexture(SlotType type) {
return type.getSlotHintTexture();
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/com/github/matt159/dws/proxy/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,13 @@ public void preInit(FMLPreInitializationEvent event) {
}

public void init(FMLInitializationEvent event) {
this.registerHandlers();
MinecraftForge.EVENT_BUS.register(new PlayerOpenContainerEventHandler());
}

public void postInit(FMLPostInitializationEvent event) {
this.disableOtherInventoryButtons();
}

public void registerHandlers() {
MinecraftForge.EVENT_BUS.register(new PlayerOpenContainerEventHandler());
}

public void registerKeyBindings() {
}

@SuppressWarnings("unchecked")
public void disableOtherInventoryButtons() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.matt159.dws.registry;

import com.github.matt159.dws.events.keybinds.InventorySwapKey;
import com.github.matt159.dws.events.keybinds.KeyHandler;
import com.github.matt159.dws.events.keybinds.ModifierKey;
import lombok.experimental.UtilityClass;
Expand All @@ -9,8 +10,10 @@
@UtilityClass
public class Keybindings {
public static KeyHandler ModifierKey;
public static KeyHandler SwapKey;

public static void register() {
ClientRegistry.registerKeyBinding(ModifierKey = new ModifierKey());
ClientRegistry.registerKeyBinding(SwapKey = new InventorySwapKey());
}
}

0 comments on commit 3cb25f8

Please sign in to comment.