Support replacing blocks when using your off-hand
Some checks failed
Java CI with Gradle / build (push) Failing after 16m7s
Some checks failed
Java CI with Gradle / build (push) Failing after 16m7s
Co-authored-by: termilu <47648082+termilu@users.noreply.github.com>
This commit is contained in:
parent
4ea309bbe9
commit
91dd7f9811
@ -7,6 +7,8 @@ import net.minecraft.entity.player.PlayerInventory;
|
|||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemPlacementContext;
|
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;
|
||||||
@ -26,49 +28,43 @@ public class HotbarReplace implements ClientModInitializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void tryReplaceSlot(ItemPlacementContext context, Item item) {
|
public static void tryReplaceSlot(ItemPlacementContext context, Item item) {
|
||||||
// Return immediately if player is a spectator
|
// Return immediately if player is a spectator or in creative
|
||||||
PlayerEntity player = context.getPlayer();
|
PlayerEntity player = context.getPlayer();
|
||||||
if (player.isSpectator())
|
if (player == null || player.isSpectator() || player.getAbilities().creativeMode)
|
||||||
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 = (context.getHand() == 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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user