Build correctly for 1.21

This commit is contained in:
Adam Macdonald
2024-07-21 13:41:04 +01:00
parent 737656c28d
commit f3631bdedc
8 changed files with 54 additions and 72 deletions

View File

@@ -0,0 +1,76 @@
package xyz.twokilohertz;
import net.fabricmc.api.ClientModInitializer;
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 org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class HotbarReplace implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("hotbarreplace");
private static final MinecraftClient client = MinecraftClient.getInstance();
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
@Override
public void onInitializeClient() {
LOGGER.info("HotbarReplace v0.1.2 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)
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)
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);
}
return;
}
}
}
}

View File

@@ -0,0 +1,35 @@
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);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,35 @@
{
"schemaVersion": 1,
"id": "hotbarreplace",
"version": "${version}",
"name": "HotbarReplace",
"description": "Replace blocks in your hotbar when you run out with blocks from your inventory",
"authors": [
"Adam Macdonald"
],
"contact": {
"homepage": "https://github.com/twokilohertz/HotbarReplace",
"sources": "https://github.com/twokilohertz/HotbarReplace",
"issues": "https://github.com/twokilohertz/HotbarReplace/issues"
},
"license": "MIT",
"icon": "assets/hotbarreplace/icon.png",
"environment": "client",
"entrypoints": {
"client": [
"xyz.twokilohertz.HotbarReplace"
]
},
"mixins": [
{
"config": "hotbarreplace.mixins.json",
"environment": "client"
}
],
"depends": {
"fabricloader": ">=0.15.11",
"minecraft": "~1.21",
"java": ">=21",
"fabric-api": "*"
}
}

View File

@@ -0,0 +1,12 @@
{
"required": true,
"package": "xyz.twokilohertz.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [],
"client": [
"BlockItemMixin"
],
"injectors": {
"defaultRequire": 1
}
}