Compare commits

...

20 Commits

Author SHA1 Message Date
22bfa12981 0.1.5 RC 1
All checks were successful
Java CI with Gradle / build (push) Successful in 15m58s
2025-04-24 20:09:42 +01:00
9d44d319d1 Add support for food items
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
2025-04-24 20:06:24 +01:00
d8373f5469 Disable artefact uploading because it doesn't seem to work on Gitea
All checks were successful
Java CI with Gradle / build (push) Successful in 16m3s
2025-04-24 00:45:34 +01:00
b11561b7c8 Use ItemStack instead of BlockItem specifically
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
2025-04-24 00:29:56 +01:00
91dd7f9811 Support replacing blocks when using your off-hand
Some checks failed
Java CI with Gradle / build (push) Failing after 16m7s
Co-authored-by: termilu <47648082+termilu@users.noreply.github.com>
2025-04-23 23:06:42 +01:00
4ea309bbe9 Update to Minecraft 1.21.5
Some checks failed
Java CI with Gradle / build (push) Failing after 15m56s
2025-04-23 22:12:34 +01:00
f186c66ad7 remove permissions specification from workflow
Some checks failed
Java CI with Gradle / build (push) Failing after 25m2s
2025-04-23 21:14:15 +01:00
f88b167931 specify artifact retention and compression level in workflow
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
2025-04-23 21:12:00 +01:00
45c65560f1 Update GitHub (Gitea) workflow
Some checks failed
Java CI with Gradle / build (push) Failing after 25m5s
2025-04-23 19:33:24 +01:00
83d42630d8 Update GitHub (Gitea) workflow
Some checks failed
Java CI with Gradle / build (push) Failing after 15s
2025-04-23 19:32:43 +01:00
62bccc2452 Update .github/workflows/gradle.yml
Some checks failed
Java CI with Gradle / build (push) Failing after 3m11s
2025-04-23 19:24:01 +01:00
92f9a82ee5 Update README.md
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
2025-04-23 19:22:04 +01:00
Adam Macdonald
17aa218b6c Update "known issues" in README 2024-07-22 17:46:04 +01:00
Adam Macdonald
7114e853b8 Remove dependency graph generation from CI steps 2024-07-22 17:42:29 +01:00
Adam Macdonald
4d6dbc594d Add CI status badge 2024-07-22 17:39:47 +01:00
Adam Macdonald
d79f9b4edc Add GitHub action for building w/ Gradle 2024-07-22 17:36:16 +01:00
Adam Macdonald
064f65d50d Add link to the project on Modrinth in README 2024-07-22 17:24:49 +01:00
Adam Macdonald
cffea2e19e Release 0.1.3 2024-07-21 18:01:55 +01:00
Adam Macdonald
643014f9ac Specify correct sources in build.gradle 2024-07-21 15:35:54 +01:00
Adam Macdonald
687d5fd29e Change move-items-between-slots delay
This is still a bit of (very much) a hack.
2024-07-21 14:22:57 +01:00
12 changed files with 221 additions and 83 deletions

View File

@ -0,0 +1,38 @@
name: Java CI with Gradle
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
jobs:
build:
runs-on: archlinux-latest
steps:
- name: Install prerequisite packages
run: pacman -Syu --noconfirm && pacman -S --noconfirm git nodejs
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: "21"
distribution: "temurin"
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
- name: Build with Gradle Wrapper
run: ./gradlew build
# - name: Upload build artifacts
# uses: actions/upload-artifact@v4
# with:
# name: JARs
# path: build/libs
# retention-days: 30
# compression-level: 9

View File

@ -1,11 +1,18 @@
## HotbarReplace
[![Java CI with Gradle](https://git.2khz.xyz/adam/HotbarReplace/actions/workflows/gradle.yml/badge.svg)](https://git.2khz.xyz/adam/HotbarReplace/actions/workflows/gradle.yml)
HotbarReplace is a Minecraft mod for the Fabric modding framework which replaces blocks in your hotbar with blocks of the same type from your inventory upon running out while placing, see demo GIF below to see how it works!
### Known issues/Improvements
* Make the mod more resilient to network delay and packet loss (don't hardcode a 50 ms delay)
* [You tell me!](https://git.2khz.xyz/adam/HotbarReplace/issues) I'd like to see this mod be as simple & bug-free as possible!
### Demo GIF
### Where to find it?
- Here! On GitHub you can grab whichever release you like from the sidebar on the right-hand side.
- Otherwise, also on [Modrinth](https://modrinth.com/mod/hotbarreplace).
#### Demo GIF
![HotbarReplace Demo GIF](./docs/hotbarreplace-demo.gif)

View File

@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'fabric-loom' version '1.10-SNAPSHOT'
id 'maven-publish'
}
@ -19,8 +19,8 @@ loom {
mods {
"hotbarreplace" {
sourceSet sourceSets.main
// sourceSet sourceSets.client
// sourceSet sourceSets.main
sourceSet sourceSets.client
}
}

View File

@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true
# Fabric Properties
minecraft_version=1.21
yarn_mappings=1.21+build.9
loader_version=0.15.11
minecraft_version=1.21.5
yarn_mappings=1.21.5+build.1
loader_version=0.16.13
# Mod Properties
mod_version = 0.1.2
mod_version = 0.1.5
maven_group = xyz.twokilohertz
archives_base_name = HotbarReplace
# Dependencies
fabric_version=0.100.7+1.21
fabric_version=0.121.0+1.21.5

Binary file not shown.

View File

@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME

5
gradlew vendored
View File

@ -86,8 +86,7 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
@ -206,7 +205,7 @@ fi
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

View File

@ -5,8 +5,9 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Hand;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,52 +23,43 @@ public class HotbarReplace implements ClientModInitializer {
@Override
public void onInitializeClient() {
LOGGER.info("HotbarReplace v0.1.2 initialised");
LOGGER.info("HotbarReplace v0.1.5 initialised");
}
public static void tryReplaceSlot(ItemPlacementContext context, Item item) {
// Return immediately if player is a spectator
PlayerEntity player = context.getPlayer();
if (player.isSpectator())
return;
// Creative inventories don't run out of blocks anyway
if (player.getAbilities().creativeMode)
public static void tryReplaceSlot(PlayerEntity player, Hand hand, Item item) {
// Return immediately if player is a spectator or in creative
if (player == null || player.isSpectator() || player.getAbilities().creativeMode)
return;
// Get reference to player's current inventory
PlayerInventory inventory = player.getInventory();
if (inventory == null)
return;
// Return if the inventory is empty
if (inventory.isEmpty())
return;
// If current screen handler is null, return
if (player.currentScreenHandler == null)
if (inventory == null || inventory.isEmpty() || player.currentScreenHandler == null)
return;
// Attempt to find a stack of matching items in the player's inventory
for (int i = 0; i < player.currentScreenHandler.slots.size(); i++) {
if (player.currentScreenHandler.slots.get(i).getStack().isOf(item)) {
// Simulate moving the stack from one slot to another
if (client != null) {
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1,
SlotActionType.PICKUP, player);
/*
* Wait 50 milliseconds (on another thread) before attempting to move the new
* stack
* PlayerInventory.MAIN_SIZE added to the selected slot (hotbar slot) is the
* correct slot ID
*/
scheduler.schedule(() -> {
client.interactionManager.clickSlot(player.currentScreenHandler.syncId,
inventory.selectedSlot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
SlotActionType.PICKUP, player);
}, 50, TimeUnit.MILLISECONDS);
}
if (client == null)
return;
// TODO: This still feels like a bit of a hack
// I honestly do not know Minecraft internals enough to be sure that there won't
// be de-sync issues.
final int click_delay_ms = 50;
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1,
SlotActionType.PICKUP, player);
int slot = (hand == Hand.OFF_HAND ? 9 : inventory.getSelectedSlot());
scheduler.schedule(() -> client.interactionManager.clickSlot(player.currentScreenHandler.syncId,
slot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
SlotActionType.PICKUP, player), click_delay_ms, TimeUnit.MILLISECONDS);
return;
}

View File

@ -1,35 +0,0 @@
package xyz.twokilohertz.mixin;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.util.ActionResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xyz.twokilohertz.HotbarReplace;
@Mixin(BlockItem.class)
public class BlockItemMixin {
private Item lastPlacedItem;
@Inject(at = @At("HEAD"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;")
private void BlockItem_place_head(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> info) {
lastPlacedItem = context.getStack().getItem();
}
@Inject(at = @At("TAIL"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;")
private void mixin_BlockItem_place_tail(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> info) {
// Early return if the block place action would fail
if (info.getReturnValue() != ActionResult.SUCCESS)
return;
// Check if the stack is not empty, return if so
if (context.getStack().getCount() != 0)
return;
// Try to replace the hotbar slot
HotbarReplace.tryReplaceSlot(context, lastPlacedItem);
}
}

View File

@ -0,0 +1,136 @@
package xyz.twokilohertz.mixin;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xyz.twokilohertz.HotbarReplace;
@Mixin(ItemStack.class)
public class ItemStackMixin {
Item lastUsedItem = null;
@Inject(at = @At("HEAD"), method = "use")
private void ItemStack_use_HEAD(World world, PlayerEntity player, Hand hand,
CallbackInfoReturnable<ActionResult> info) {
if (!world.isClient())
return;
Item item = player.getStackInHand(hand).getItem();
if (item == null)
return;
lastUsedItem = item;
}
@Inject(at = @At("HEAD"), method = "useOnBlock")
private void ItemStack_useOnBlock_HEAD(ItemUsageContext context,
CallbackInfoReturnable<ActionResult> info) {
if (!context.getWorld().isClient())
return;
Item item = context.getPlayer().getStackInHand(context.getHand()).getItem();
if (item == null)
return;
lastUsedItem = item;
}
@Inject(at = @At("HEAD"), method = "useOnEntity")
private void ItemStack_useOnEntity_HEAD(PlayerEntity player, LivingEntity entity, Hand hand,
CallbackInfoReturnable<ActionResult> info) {
if (!player.getWorld().isClient())
return;
Item item = player.getStackInHand(hand).getItem();
if (item == null)
return;
lastUsedItem = item;
}
@Inject(at = @At("TAIL"), method = "use")
private void ItemStack_use_TAIL(World world, PlayerEntity player, Hand hand,
CallbackInfoReturnable<ActionResult> info) {
if (!world.isClient())
return;
if (info.getReturnValue() != ActionResult.SUCCESS)
return;
ItemStack stack = player.getStackInHand(hand);
if (stack.getCount() > 1)
return;
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
}
@Inject(at = @At("TAIL"), method = "useOnBlock")
private void ItemStack_useOnBlock_TAIL(ItemUsageContext context, CallbackInfoReturnable<ActionResult> info) {
if (!context.getWorld().isClient())
return;
if (info.getReturnValue() != ActionResult.SUCCESS)
return;
PlayerEntity player = context.getPlayer();
Hand hand = context.getHand();
ItemStack stack = player.getStackInHand(hand);
if (stack.getCount() != 0)
return;
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
}
@Inject(at = @At("TAIL"), method = "useOnEntity")
private void ItemStack_useOnEntity_TAIL(PlayerEntity player, LivingEntity entity, Hand hand,
CallbackInfoReturnable<ActionResult> info) {
if (info.getReturnValue() != ActionResult.SUCCESS)
return;
ItemStack stack = player.getStackInHand(hand);
if (stack.getCount() > 1)
return;
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
}
@Inject(at = @At("HEAD"), method = "Lnet/minecraft/item/ItemStack;finishUsing(Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;)Lnet/minecraft/item/ItemStack;")
private void ItemStack_finishUsing_HEAD(World world, LivingEntity user,
CallbackInfoReturnable<ItemStack> info) {
if (!world.isClient())
return;
ItemStack stack = (ItemStack) (Object) this;
// ItemStack stack = info.getReturnValue(); -- only applicable when injecting at TAIL
if (lastUsedItem != stack.getItem())
return;
// Food items disappear into thin (minecraft:)air before their stack size reaches 0
if (stack.getCount() > 1)
return;
if (!user.isPlayer())
return;
PlayerEntity player = (PlayerEntity) user;
if (!player.isMainPlayer())
return;
HotbarReplace.tryReplaceSlot(player, player.getActiveHand(), lastUsedItem);
}
}

View File

@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "hotbarreplace",
"version": "0.1.2",
"version": "0.1.5",
"name": "HotbarReplace",
"description": "Replace blocks in your hotbar when you run out with blocks from your inventory",
"authors": [

View File

@ -4,8 +4,9 @@
"compatibilityLevel": "JAVA_21",
"mixins": [],
"client": [
"BlockItemMixin"
"ItemStackMixin"
],
"server": [],
"injectors": {
"defaultRequire": 1
}