Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
- Keybinds now work when the Creative Inventory GUI menu is open.
- More documentation
  • Loading branch information
VideoGameSmash12 committed Apr 13, 2022
1 parent 5161c3d commit 729dc05
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@

package me.videogamesm12.hotbarsplus.legacy.mixin;

import me.videogamesm12.hotbarsplus.api.event.keybind.BackupBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.NextBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.PreviousBindPressEvent;
import me.videogamesm12.hotbarsplus.core.HBPCore;
import me.videogamesm12.hotbarsplus.legacy.gui.CustomButtons;
import me.videogamesm12.hotbarsplus.legacy.manager.KeybindManager;
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemGroup;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(CreativeInventoryScreen.class)
public abstract class CreativeInvScreenMixin extends AbstractInventoryScreen<CreativeInventoryScreen.CreativeContainer>
Expand All @@ -39,6 +45,8 @@ public abstract class CreativeInvScreenMixin extends AbstractInventoryScreen<Cre
public CustomButtons.BackupButton backup;
public CustomButtons.PreviousButton previous;

@Shadow private boolean field_2888;

public CreativeInvScreenMixin(CreativeInventoryScreen.CreativeContainer container, PlayerInventory playerInventory, Text text)
{
super(container, playerInventory, text);
Expand Down Expand Up @@ -95,6 +103,34 @@ public void injSetCreativeTab(ItemGroup group, CallbackInfo ci)
}
}

@Inject(method = "keyPressed", at = @At(value = "HEAD", shift = At.Shift.AFTER), cancellable = true)
public void injectKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir)
{
if (((CISAccessor) this).getSelectedTab() == ItemGroup.HOTBAR.getIndex())
{
KeybindManager manager = (KeybindManager) HBPCore.KEYBINDS;

if (manager.next.matchesKey(keyCode, scanCode))
{
NextBindPressEvent.EVENT.invoker().onNextPress();
field_2888 = true;
cir.setReturnValue(true);
}
else if (manager.backup.matchesKey(keyCode, scanCode))
{
BackupBindPressEvent.EVENT.invoker().onBackupPress();
field_2888 = true;
cir.setReturnValue(true);
}
else if (manager.previous.matchesKey(keyCode, scanCode))
{
PreviousBindPressEvent.EVENT.invoker().onPreviousPress();
field_2888 = true;
cir.setReturnValue(true);
}
}
}

@Mixin(CreativeInventoryScreen.class)
public interface CISAccessor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

package me.videogamesm12.hotbarsplus.v1_16.mixin;

import me.videogamesm12.hotbarsplus.api.event.keybind.BackupBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.NextBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.PreviousBindPressEvent;
import me.videogamesm12.hotbarsplus.core.HBPCore;
import me.videogamesm12.hotbarsplus.core.gui.CustomButtons;
import me.videogamesm12.hotbarsplus.v1_16.manager.KeybindManager;
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.entity.player.PlayerInventory;
Expand All @@ -30,6 +34,7 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

/**
* <b>CreativeInvScreenMixin</b>
Expand All @@ -43,6 +48,8 @@ public abstract class CreativeInvScreenMixin extends AbstractInventoryScreen<Cre
{
@Shadow public abstract int getSelectedTab();

@Shadow private boolean ignoreTypedCharacter;

public CustomButtons.NextButton next;
public CustomButtons.BackupButton backup;
public CustomButtons.PreviousButton previous;
Expand Down Expand Up @@ -103,6 +110,34 @@ public void injSetCreativeTab(ItemGroup group, CallbackInfo ci)
}
}

@Inject(method = "keyPressed", at = @At(value = "HEAD", shift = At.Shift.AFTER), cancellable = true)
public void injectKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir)
{
if (getSelectedTab() == ItemGroup.HOTBAR.getIndex())
{
KeybindManager manager = (KeybindManager) HBPCore.KEYBINDS;

if (manager.next.matchesKey(keyCode, scanCode))
{
NextBindPressEvent.EVENT.invoker().onNextPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
else if (manager.backup.matchesKey(keyCode, scanCode))
{
BackupBindPressEvent.EVENT.invoker().onBackupPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
else if (manager.previous.matchesKey(keyCode, scanCode))
{
PreviousBindPressEvent.EVENT.invoker().onPreviousPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
}
}

@Mixin(CreativeInventoryScreen.class)
public interface CISAccessor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

package me.videogamesm12.hotbarsplus.v1_17.mixin;

import me.videogamesm12.hotbarsplus.api.event.keybind.BackupBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.NextBindPressEvent;
import me.videogamesm12.hotbarsplus.api.event.keybind.PreviousBindPressEvent;
import me.videogamesm12.hotbarsplus.core.HBPCore;
import me.videogamesm12.hotbarsplus.core.gui.CustomButtons;
import me.videogamesm12.hotbarsplus.v1_17.manager.KeybindManager;
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.option.HotbarStorageEntry;
Expand All @@ -31,6 +35,7 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.concurrent.CompletableFuture;

Expand All @@ -40,6 +45,7 @@ public abstract class CreativeInvScreenMixin extends AbstractInventoryScreen<Cre
{
@Shadow public abstract int getSelectedTab();

@Shadow private boolean ignoreTypedCharacter;
public CustomButtons.NextButton next;
public CustomButtons.BackupButton backup;
public CustomButtons.PreviousButton previous;
Expand Down Expand Up @@ -100,6 +106,34 @@ public void injSetCreativeTab(ItemGroup group, CallbackInfo ci)
}
}

@Inject(method = "keyPressed", at = @At(value = "HEAD", shift = At.Shift.AFTER), cancellable = true)
public void injectKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir)
{
if (getSelectedTab() == ItemGroup.HOTBAR.getIndex())
{
KeybindManager manager = (KeybindManager) HBPCore.KEYBINDS;

if (manager.next.matchesKey(keyCode, scanCode))
{
NextBindPressEvent.EVENT.invoker().onNextPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
else if (manager.backup.matchesKey(keyCode, scanCode))
{
BackupBindPressEvent.EVENT.invoker().onBackupPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
else if (manager.previous.matchesKey(keyCode, scanCode))
{
PreviousBindPressEvent.EVENT.invoker().onPreviousPress();
ignoreTypedCharacter = true;
cir.setReturnValue(true);
}
}
}

@Mixin(CreativeInventoryScreen.class)
public interface CISAccessor
{
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ version '2.0-INDEV'

repositories {
mavenCentral()
maven { url "https://maven.terraformersmc.com/releases/" }
}

dependencies {
minecraft "com.mojang:minecraft:1.17.1"
mappings "net.fabricmc:yarn:1.17.1+build.65:v2"

modImplementation "net.fabricmc.fabric-api:fabric-api:0.46.0+1.17"
modImplementation "com.terraformersmc:modmenu:2.0.14"

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/videogamesm12/hotbarsplus/core/HBPCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
*/
public class HBPCore implements ClientModInitializer
{
// UTILITIES
public static Logger LOGGER = LogManager.getLogger("Hotbars+");

// VERSION SPECIFIC
public static IKeybindManager<?> KEYBINDS = null;
public static ICommandManager<?> COMMANDS = null;
Expand All @@ -45,9 +48,6 @@ public class HBPCore implements ClientModInitializer
public static ConfigurationManager UCL = new ConfigurationManager();
public static NotificationManager UNL = new NotificationManager();

// UTILITIES
public static Logger LOGGER = LogManager.getLogger("Hotbars+");

@Override
public void onInitializeClient()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public ConfigurationManager()

public void load()
{
HBPCore.LOGGER.info("Loading configuration from disk...");

// Try loading the configuration from disk.
try
{
Expand Down Expand Up @@ -85,6 +87,7 @@ public void save()
@Override
public void onClientStopping(MinecraftClient client)
{
// Automatically saves to disk.
save();
}
}

0 comments on commit 729dc05

Please sign in to comment.