Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
b11561b7c8 | |||
91dd7f9811 |
@@ -8,7 +8,7 @@ yarn_mappings=1.21.5+build.1
|
|||||||
loader_version=0.16.13
|
loader_version=0.16.13
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.1.3
|
mod_version = 0.1.4
|
||||||
maven_group = xyz.twokilohertz
|
maven_group = xyz.twokilohertz
|
||||||
archives_base_name = HotbarReplace
|
archives_base_name = HotbarReplace
|
||||||
|
|
||||||
|
@@ -5,8 +5,9 @@ import net.minecraft.client.MinecraftClient;
|
|||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemPlacementContext;
|
|
||||||
import net.minecraft.screen.slot.SlotActionType;
|
import net.minecraft.screen.slot.SlotActionType;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -22,53 +23,46 @@ public class HotbarReplace implements ClientModInitializer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
LOGGER.info("HotbarReplace v0.1.3 initialised");
|
LOGGER.info("HotbarReplace v0.1.4 initialised");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void tryReplaceSlot(ItemPlacementContext context, Item item) {
|
public static void tryReplaceSlot(PlayerEntity player, Hand hand, Item item) {
|
||||||
// Return immediately if player is a spectator
|
// Return immediately if player is a spectator or in creative
|
||||||
PlayerEntity player = context.getPlayer();
|
if (player == null || player.isSpectator() || player.getAbilities().creativeMode)
|
||||||
if (player.isSpectator())
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Creative inventories don't run out of blocks anyway
|
|
||||||
if (player.getAbilities().creativeMode)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Get reference to player's current inventory
|
// Get reference to player's current inventory
|
||||||
PlayerInventory inventory = player.getInventory();
|
PlayerInventory inventory = player.getInventory();
|
||||||
if (inventory == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Return if the inventory is empty
|
// Return if the inventory is empty
|
||||||
if (inventory.isEmpty())
|
if (inventory == null || inventory.isEmpty() || player.currentScreenHandler == null)
|
||||||
return;
|
|
||||||
|
|
||||||
// If current screen handler is null, return
|
|
||||||
if (player.currentScreenHandler == null)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Attempt to find a stack of matching items in the player's inventory
|
// Attempt to find a stack of matching items in the player's inventory
|
||||||
for (int i = 0; i < player.currentScreenHandler.slots.size(); i++) {
|
for (int i = 0; i < player.currentScreenHandler.slots.size(); i++) {
|
||||||
if (player.currentScreenHandler.slots.get(i).getStack().isOf(item)) {
|
if (player.currentScreenHandler.slots.get(i).getStack().isOf(item)) {
|
||||||
// Simulate moving the stack from one slot to another
|
// Simulate moving the stack from one slot to another
|
||||||
if (client != null) {
|
|
||||||
// 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.
|
|
||||||
|
|
||||||
int current_fps = client.getCurrentFps();
|
if (client == null)
|
||||||
int click_delay = Math.round(1.0f / (float) current_fps) * 1000;
|
return;
|
||||||
|
|
||||||
client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1,
|
// 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.
|
||||||
|
|
||||||
|
int current_fps = client.getCurrentFps();
|
||||||
|
int click_delay = Math.round(1.0f / (float) current_fps) * 1000;
|
||||||
|
|
||||||
|
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);
|
SlotActionType.PICKUP, player);
|
||||||
|
}, click_delay, TimeUnit.MILLISECONDS);
|
||||||
scheduler.schedule(() -> {
|
|
||||||
client.interactionManager.clickSlot(player.currentScreenHandler.syncId,
|
|
||||||
inventory.getSelectedSlot() + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
|
|
||||||
SlotActionType.PICKUP, player);
|
|
||||||
}, click_delay, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -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 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);
|
|
||||||
}
|
|
||||||
}
|
|
77
src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java
Normal file
77
src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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) {
|
||||||
|
lastUsedItem = player.getStackInHand(hand).getItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("HEAD"), method = "useOnBlock")
|
||||||
|
private void ItemStack_useOnBlock_HEAD(ItemUsageContext context,
|
||||||
|
CallbackInfoReturnable<ActionResult> info) {
|
||||||
|
lastUsedItem = context.getPlayer().getStackInHand(context.getHand()).getItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("HEAD"), method = "useOnEntity")
|
||||||
|
private void ItemStack_useOnEntity_HEAD(PlayerEntity player, LivingEntity entity, Hand hand,
|
||||||
|
CallbackInfoReturnable<ActionResult> info) {
|
||||||
|
lastUsedItem = player.getStackInHand(hand).getItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("TAIL"), method = "use")
|
||||||
|
private void ItemStack_use_TAIL(World world, PlayerEntity player, Hand hand,
|
||||||
|
CallbackInfoReturnable<ActionResult> info) {
|
||||||
|
if (info.getReturnValue() != ActionResult.SUCCESS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (player.getStackInHand(hand).getCount() != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject(at = @At("TAIL"), method = "useOnBlock")
|
||||||
|
private void ItemStack_useOnBlock_TAIL(ItemUsageContext context, CallbackInfoReturnable<ActionResult> info) {
|
||||||
|
if (info.getReturnValue() != ActionResult.SUCCESS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
PlayerEntity player = context.getPlayer();
|
||||||
|
Hand hand = context.getHand();
|
||||||
|
|
||||||
|
if (player.getStackInHand(hand).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;
|
||||||
|
|
||||||
|
if (player.getStackInHand(hand).getCount() != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem);
|
||||||
|
}
|
||||||
|
}
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "hotbarreplace",
|
"id": "hotbarreplace",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"name": "HotbarReplace",
|
"name": "HotbarReplace",
|
||||||
"description": "Replace blocks in your hotbar when you run out with blocks from your inventory",
|
"description": "Replace blocks in your hotbar when you run out with blocks from your inventory",
|
||||||
"authors": [
|
"authors": [
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
"compatibilityLevel": "JAVA_21",
|
"compatibilityLevel": "JAVA_21",
|
||||||
"mixins": [],
|
"mixins": [],
|
||||||
"client": [
|
"client": [
|
||||||
"BlockItemMixin"
|
"ItemStackMixin"
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": 1
|
"defaultRequire": 1
|
||||||
|
Reference in New Issue
Block a user