Skip to content

Commit

Permalink
1.5.0 - cherry-pick from 1.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazingTwist committed Nov 17, 2024
1 parent 905f892 commit 974c513
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.10

# Mod Properties
mod_version = 1.4.1
mod_version = 1.5.0
maven_group = blazingtwist
archives_base_name = itemcounts

Expand Down
70 changes: 62 additions & 8 deletions src/main/java/blazingtwist/itemcounts/ItemCounts.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package blazingtwist.itemcounts;

import blazingtwist.itemcounts.config.AutoConfigKeybind;
import blazingtwist.itemcounts.config.ItemCountsConfig;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigHolder;
import me.shedaniel.autoconfig.gui.registry.GuiRegistry;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.util.InputUtil;

public class ItemCounts implements ModInitializer {
import java.lang.reflect.Field;
import java.util.Collections;

public class ItemCounts implements ClientModInitializer {
public static final float FONT_HEIGHT = 8; // sitting here because mixin doesn't like it in 'InGameHudMixin'
public static final float FONT_Y_OFFSET = 8;
public static final float HOTBAR_X_OFFSET = 8;
Expand All @@ -15,18 +23,64 @@ public class ItemCounts implements ModInitializer {

private static ConfigHolder<ItemCountsConfig> configHolder;

public static ItemCountsConfig getConfig(){
public static ItemCountsConfig getConfig() {
return configHolder.getConfig();
}

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.

public void onInitializeClient() {
AutoConfig.register(ItemCountsConfig.class, GsonConfigSerializer::new);
registerAutoconfigTypes();

configHolder = AutoConfig.getConfigHolder(ItemCountsConfig.class);
registerKeybindListener();
}

private static void registerAutoconfigTypes() {
GuiRegistry configRegistry = AutoConfig.getGuiRegistry(ItemCountsConfig.class);
configRegistry.registerAnnotationProvider((i13n, field, config, defaults, guiProvider) -> {
ConfigEntryBuilder entry = ConfigEntryBuilder.create();
int key = getUnsafe(field, config, -1);
int keyDef = getUnsafe(field, defaults, -1);

return Collections.singletonList(entry
.startKeyCodeField(net.minecraft.text.Text.translatable(i13n), key > 0 ? InputUtil.fromKeyCode(key, -1) : InputUtil.UNKNOWN_KEY)
.setDefaultValue(keyDef > 0 ? InputUtil.fromKeyCode(keyDef, -1) : InputUtil.UNKNOWN_KEY)
.setKeySaveConsumer(saveKey -> setUnsafe(field, config, saveKey.getCode()))
.build()
);
}, AutoConfigKeybind.class);
}

private static <T> T getUnsafe(Field field, Object obj, T fallback) {
T result;
try {
//noinspection unchecked
result = (T) field.get(obj);
} catch (IllegalAccessException e) {
System.err.println("failed to access field '" + field.getName() + "' on obj: " + obj);
result = null;
}
return result == null ? fallback : result;
}

private static <T> void setUnsafe(Field field, Object obj, T value) {
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
System.err.println("failed to set field '" + field.getName() + "' on obj: " + obj);
}
}

private static void registerKeybindListener() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
long windowHandle = client.getWindow().getHandle();
ItemCountsConfig config = configHolder.get();
config.hotbar_relativeToHotbarConfig.handleKeys(windowHandle);
config.mainHand_relativeToCrosshairConfig.handleKeys(windowHandle);
config.mainHand_relativeToHotbarConfig.handleKeys(windowHandle);
config.offHand_relativeToCrosshairConfig.handleKeys(windowHandle);
config.offHand_relativeToHotbarConfig.handleKeys(windowHandle);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package blazingtwist.itemcounts.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AutoConfigKeybind {
}
35 changes: 32 additions & 3 deletions src/main/java/blazingtwist/itemcounts/config/ItemCountsConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package blazingtwist.itemcounts.config;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;

import java.util.stream.IntStream;
import java.util.stream.Stream;

@Config(name = "itemcounts")
public class ItemCountsConfig implements ConfigData {
@ConfigEntry.Category("mainHand")
Expand Down Expand Up @@ -103,6 +104,10 @@ public ItemRenderConfig(boolean enabled, CountDisplayOption countOption,
}

public boolean enabled = false;
@AutoConfigKeybind
public int toggleKeyCode = -1;
@AutoConfigKeybind
public int holdKeyCode = -1;
public CountDisplayOption countOption = CountDisplayOption.NEVER;
public DurabilityItemOption durabilityFilter = DurabilityItemOption.NONE;
public DurabilityDisplayOption durabilityOption = DurabilityDisplayOption.NEVER;
Expand All @@ -112,6 +117,30 @@ public ItemRenderConfig(boolean enabled, CountDisplayOption countOption,
@ConfigEntry.Gui.CollapsibleObject()
public HudColors colors = new HudColors();

@ConfigEntry.Gui.Excluded
private boolean toggleKeyActive = false;
@ConfigEntry.Gui.Excluded
private boolean wasToggleKeyDown = false;
@ConfigEntry.Gui.Excluded
private boolean wasHoldKeyDown = false;

public boolean isEnabled() {
return enabled ^ toggleKeyActive ^ wasHoldKeyDown;
}

public void handleKeys(long windowHandle) {
if (toggleKeyCode > 0) {
boolean toggleKeyDown = InputUtil.isKeyPressed(windowHandle, toggleKeyCode);
if (toggleKeyDown && !wasToggleKeyDown) {
toggleKeyActive = !toggleKeyActive;
}
wasToggleKeyDown = toggleKeyDown;
}
if (holdKeyCode > 0) {
wasHoldKeyDown = InputUtil.isKeyPressed(windowHandle, holdKeyCode);
}
}

}

public static class HudOffset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -33,9 +34,10 @@ public abstract class InGameHudMixin {
@Final
private MinecraftClient client;

@Unique
private void renderItemOverlay(DrawContext context, ItemCountsConfig.ItemRenderConfig config, boolean onHotbar,
PlayerEntity player, ItemStack stack, int x, int y) {
if (!config.enabled) {
if (!config.isEnabled()) {
return;
}
if (!onHotbar) {
Expand All @@ -62,6 +64,7 @@ private boolean shouldRenderItem(ItemCountsConfig.ItemRenderConfig config, Playe
}
}

@Unique
private void renderItemText(DrawContext context, ItemCountsConfig.ItemRenderConfig config, boolean onHotbar,
PlayerEntity player, ItemStack stack, int x, int y) {
final String text;
Expand All @@ -84,6 +87,7 @@ private void renderItemText(DrawContext context, ItemCountsConfig.ItemRenderConf
renderTextAt(context, config, text, color, x, y, onHotbar);
}

@Unique
private void renderTextAt(DrawContext context, ItemCountsConfig.ItemRenderConfig config,
String text, int color, int x, int y, boolean isOnHotbar) {
float scaleFactor = config.offset.textScale;
Expand Down Expand Up @@ -113,6 +117,7 @@ private void renderTextAt(DrawContext context, ItemCountsConfig.ItemRenderConfig
matrices.pop();
}

@Unique
private void renderItemAt(DrawContext context, ItemStack item, int x, int y, float scaleFactor, boolean isOnHotbar) {
ItemRenderer itemRenderer = client.getItemRenderer();
BakedModel model = itemRenderer.getModel(item, null, null, 0);
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/assets/itemcounts/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig": "Configure overlay relative to hotbar for active item",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.enabled": "enable this overlay",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.toggleKeyCode": "Hotkey: toggle overlay",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.holdKeyCode": "Hotkey: enable/disable while held",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.countOption": "Show item-count",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.durabilityFilter": "Tools of this Type show durability instead of item-count",
"text.autoconfig.itemcounts.option.mainHand_relativeToHotbarConfig.durabilityOption": "Show durability",
Expand All @@ -24,6 +26,8 @@

"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig": "Configure overlay relative to crosshair for active item",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.enabled": "enable this overlay",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.toggleKeyCode": "Hotkey: toggle overlay",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.holdKeyCode": "Hotkey: enable/disable while held",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.countOption": "Show item-count",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.durabilityFilter": "Tools of this Type show durability instead of item-count",
"text.autoconfig.itemcounts.option.mainHand_relativeToCrosshairConfig.durabilityOption": "Show durability",
Expand All @@ -41,6 +45,8 @@

"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig": "Configure overlay relative to hotbar for offhand item",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.enabled": "enable this overlay",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.toggleKeyCode": "Hotkey: toggle overlay",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.holdKeyCode": "Hotkey: enable/disable while held",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.countOption": "Show item-count",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.durabilityFilter": "Tools of this Type show durability instead of item-count",
"text.autoconfig.itemcounts.option.offHand_relativeToHotbarConfig.durabilityOption": "Show durability",
Expand All @@ -58,6 +64,8 @@

"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig": "Configure overlay relative to crosshair for offhand item",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.enabled": "enable this overlay",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.toggleKeyCode": "Hotkey: toggle overlay",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.holdKeyCode": "Hotkey: enable/disable while held",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.countOption": "Show item-count",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.durabilityFilter": "Tools of this Type show durability instead of item-count",
"text.autoconfig.itemcounts.option.offHand_relativeToCrosshairConfig.durabilityOption": "Show durability",
Expand All @@ -75,6 +83,8 @@

"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig": "Configure overlay relative to hotbar for hotbar items",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.enabled": "enable this overlay",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.toggleKeyCode": "Hotkey: toggle overlay",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.holdKeyCode": "Hotkey: enable/disable while held",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.countOption": "Show item-count",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.durabilityFilter": "Tools of this Type show durability instead of item-count",
"text.autoconfig.itemcounts.option.hotbar_relativeToHotbarConfig.durabilityOption": "Show durability",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

"environment": "*",
"entrypoints": {
"main": [
"client": [
"blazingtwist.itemcounts.ItemCounts"
],
"modmenu": [
Expand Down
Binary file added wiki-resources/ConfiguringAnOverlay_Hotkeys.webp
Binary file not shown.

0 comments on commit 974c513

Please sign in to comment.