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.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;
|
||||
@ -26,49 +28,43 @@ public class HotbarReplace implements ClientModInitializer {
|
||||
}
|
||||
|
||||
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();
|
||||
if (player.isSpectator())
|
||||
return;
|
||||
|
||||
// Creative inventories don't run out of blocks anyway
|
||||
if (player.getAbilities().creativeMode)
|
||||
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) {
|
||||
// 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;
|
||||
if (client == null)
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}, click_delay, TimeUnit.MILLISECONDS);
|
||||
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user