Skip to content

Container functions#40

Open
edde746 wants to merge 1 commit into
maxuser0:mainfrom
edde746:main
Open

Container functions#40
edde746 wants to merge 1 commit into
maxuser0:mainfrom
edde746:main

Conversation

@edde746
Copy link
Copy Markdown

@edde746 edde746 commented Aug 12, 2025

Adds functions for interacting with containers, closes #32.

New functions

  • container_open(x, y, z) - Open containers (chests, furnaces, crafting tables) at specified coordinates
  • container_close() - Close the currently open container
  • container_get_info() - Get container metadata (slot count, title, type)
  • container_get_items() - Retrieve all items in the open container
  • container_get_slot(slot) - Get the item in a specific slot
  • container_find_item(item_id) - Find all slots containing a specific item
  • container_click_slot(slot, button, shift) - Click container slots with mouse simulation
  • container_swap_slots(slot1, slot2) - Swap items between two slots

Demo

Screen.Recording.2025-08-12.at.19.30.34.mov

@enbyte
Copy link
Copy Markdown

enbyte commented Aug 15, 2025

Peak, pls merge

@enbyte
Copy link
Copy Markdown

enbyte commented Aug 30, 2025

Any update @maxuser0 ? This seems like a useful PR.

@the-can-of-soup
Copy link
Copy Markdown

YES PLEASE MERGE

@maxuser0
Copy link
Copy Markdown
Owner

Thanks for the PR.

Given the power and flexibility of java.py and Pyjinn in Minescript 5.0, I'm not convinced of the need for these functions to be built into the mod. Adding functionality that relies on Minecraft APIs often makes it more difficult to port Minescript across game versions. The script functions already built into Minescript were added before java.py and Pyjinn were integrated into the mod. Going forward, the script functions that I'm likely to include in the mod are ones that provide a significant performance improvement in Java compared to Python or Pyjinn. E.g. I'm planning to add Java implementation for a function like get_blocks_in_region(position1, position2) because it's significantly faster than constructing the nested lists or tuples needed for getblocklist(positions) for thousands of blocks. But I don't think there would be any noticeable speed improvement for the container functions implemented in Java in this PR over an implementation in Python or Pyjinn.

@the-can-of-soup
Copy link
Copy Markdown

the-can-of-soup commented Sep 16, 2025

Could you provide an example implementation using Pyjinn? I don't know anything about Java (which is why I use this mod).
I'm using Fabric 1.21.8.

@brentspine
Copy link
Copy Markdown

Bump, would also like to know how to implement it using Pyjinn. Is there something speaking strictly AGAINST merging?

@brentspine
Copy link
Copy Markdown

Got it (I think this is an okay way to do it?)

# container_click.py
import minescript # type: ignore
from java import JavaClass # type: ignore
import datetime
from enum import Enum

class _MC:
    Minecraft = None
    ClickType = None
    
class EntityType(Enum):
    ARMOR_STAND = "entity.minecraft.armor_stand"
    PLAYER = "entity.minecraft.player"

def _ensure_classes_loaded():
    if _MC.Minecraft is None:
        _MC.Minecraft = JavaClass("net.minecraft.client.Minecraft")
    if _MC.ClickType is None:
        _MC.ClickType = JavaClass("net.minecraft.world.inventory.ClickType")
    return True

def _ctx():
    _ensure_classes_loaded()
    mc = _MC.Minecraft.getInstance()
    if mc is None:
        return None
    player = mc.player
    if player is None:
        return None
    menu = player.containerMenu
    if menu is None:
        return None
    return (mc, player, menu)

def click_slot(slot, button=0, shift=False, index_mode="absolute"):
    """
    Click a slot in the currently open container GUI.

    slot: index to click
    button: 0=left, 1=right, 2=middle
    shift: True => shift-click (QUICK_MOVE), False => normal click (PICKUP)
    index_mode: "absolute" | "container" | "player"
    """
    ctx = _ctx()
    if ctx is None:
        return False
    #minescript.log(f"Got context {datetime.datetime.now()}")
    mc, player, menu = ctx
    abs_index = slot

    click_type = _MC.ClickType.QUICK_MOVE if shift else _MC.ClickType.PICKUP
    #minescript.log(f"Executing click {datetime.datetime.now()}")
    mc.gameMode.handleInventoryMouseClick(menu.containerId, abs_index, button, click_type, player)
    minescript.log(f"Click done! {datetime.datetime.now()}")
    return True

@the-can-of-soup
Copy link
Copy Markdown

Thank you :)

@brentspine
Copy link
Copy Markdown

I made a slightly more fetched out util, feel free to edit:

https://github.com/brentspine/hypixel-scripts/blob/main/container_click.py

@brentspine
Copy link
Copy Markdown

The player_container_click function currently requires you to manually give the amount of slots in a potentially open container. I could maybe add a util function to approximate or get the size, but I have something else to do rn

@brentspine
Copy link
Copy Markdown

Btw: I think it might be better to use Pyjinn directly, see here:

https://github.com/maxuser0/minescript/blob/main/docs/pyjinn.md#embedding-pyjinn-in-python-scripts

@the-can-of-soup
Copy link
Copy Markdown

I'm too intimidated to learn how to use Pyjinn tbh, especially because I don't know the names of any internal classes or functions, but yeah that would be better

@brentspine
Copy link
Copy Markdown

I'm too intimidated to learn how to use Pyjinn tbh, especially because I don't know the names of any internal classes or functions, but yeah that would be better

Yea I still prefer using Minescript, but for some things, like checking inventory, Pyjinn is way better. Just because how slow Minescript would get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add direct container interaction support for Minescript API

5 participants