Build correctly for 1.21
This commit is contained in:
76
src/client/java/xyz/twokilohertz/HotbarReplace.java
Normal file
76
src/client/java/xyz/twokilohertz/HotbarReplace.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java
Normal file
35
src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java
Normal 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);
|
||||
}
|
||||
}
|
||||
BIN
src/client/resources/assets/hotbarreplace/icon.png
Normal file
BIN
src/client/resources/assets/hotbarreplace/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
35
src/client/resources/fabric.mod.json
Normal file
35
src/client/resources/fabric.mod.json
Normal 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": "*"
|
||||
}
|
||||
}
|
||||
12
src/client/resources/hotbarreplace.mixins.json
Normal file
12
src/client/resources/hotbarreplace.mixins.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"required": true,
|
||||
"package": "xyz.twokilohertz.mixin",
|
||||
"compatibilityLevel": "JAVA_21",
|
||||
"mixins": [],
|
||||
"client": [
|
||||
"BlockItemMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user