IMTModGuiContainer 接口
基本信息
| 属性 |
值 |
| 接口 |
yalter.mousetweaks.api.IMTModGuiContainer |
| 对应容器 ID |
Constants.MTMODGUICONTAINER = 2 |
| 识别方式 |
screen instanceof IMTModGuiContainer(所有识别分支中优先级最高) |
| 方法总数 |
11 |
| 目的 |
让第三方 mod 的自定义容器把「栏位抽象 / 产物栏位 / 点击注入」交给 Mouse Tweaks |
功能
大部分 mod 界面不是原版 GuiContainer(而是继承 GuiForgeContainer、用 inventorySlots 字段
或干脆自绘)。IMTModGuiContainer 就是官方的接入点:实现它以后,Mouse Tweaks 会把
「有几个栏位 / 第 i 个栏位是什么 / 鼠标下是哪个栏位 / 怎么点它 / 怎么关掉原版拖拽」
全部委托回该 mod 自己实现。
ModCompatibility.getModGuiContainerID 第一条判断就是 instanceof IMTModGuiContainer,
命中后直接返回 2,后续所有 Forestry / CodeChickenCore / NEI / 原版分支都被跳过。
全部方法(11 个)
元信息
| 方法 |
返回 |
说明 |
int getAPIVersion() |
int |
API 版本,本版本应返回 1 |
String getModName() |
String |
用于日志输出的 mod 名(ModCompatibility.getModNameFromModGuiContainerID 会打印在 dev 模式的调试日志里) |
开关
| 方法 |
返回 |
语义 |
消费方 |
boolean isMouseTweaksDisabled() |
boolean |
true = 本界面完全禁用 Mouse Tweaks |
isDisabledForThisModContainer |
boolean isWheelTweaksDisabled() |
boolean |
true = 本界面仅禁用滚轮部分,其余 tweak 仍生效 |
isWheelDisabledForThisModContainer |
合成产物栏位
| 方法 |
参数 |
返回 |
说明 |
boolean isCraftingOutputSlot(Object modContainer, Object slot) |
容器 + 栏位 |
boolean |
供 右键拖拽 / 左键拖拽 / 滚轮搬运 排除产物栏位 |
容器与栏位抽象
| 方法 |
返回 |
说明 |
Object getModContainer() |
Object |
本 GuiContainer 使用的容器对象;若同时有多个容器,返回当前正在用的那个 |
int getModSlotCount(Object modContainer) |
int |
栏位总数;栏位数会变的界面(如 NEI 配方界面)必须返回当前值,不能缓存 |
Object getModSlot(Object modContainer, int slotNumber) |
Object |
取下标为 slotNumber 的栏位 |
Object getModSelectedSlot(Object modContainer, int slotCount) |
Object |
取鼠标光标下的栏位,无则返回 null |
操作注入
| 方法 |
签名要点 |
说明 |
void clickModSlot(Object modContainer, Object slot, int mouseButton, boolean shiftPressed) |
mouseButton:0 = 左键,1 = 右键 |
对指定栏位模拟一次点击 |
void disableRMBDragIfRequired(Object modContainer, Object firstSlot, boolean shouldClick) |
firstSlot = 拖拽起点栏位;shouldClick 表示是否需要补点起点 |
关掉本界面自己的右键拖拽实现;原生 GuiContainer 只需改两个 public 字段,此处须自行处理 |
委托关系对照
ContainerContext 的每个操作在 MTMODGUICONTAINER 下的委托目标:
ContainerContext 方法 |
委托给 |
getSlotCount |
getModSlotCount(modContainer) |
getSelectedSlot |
getModSelectedSlot(modContainer, slotCount) |
getSlot |
getModSlot(modContainer, slotNumber) |
clickSlot |
clickModSlot(modContainer, slot, mouseButton, shiftPressed) |
isCraftingOutputSlot |
isCraftingOutputSlot(modContainer, slot) |
disableRMBDragIfRequired |
disableRMBDragIfRequired(modContainer, firstSlot, shouldClick) |
isDisabledForCurrentContainer |
isMouseTweaksDisabled() |
isWheelDisabledForCurrentContainer |
isWheelTweakDisabled() |
getModContainer |
getModContainer() |
getModNameFromModGuiContainerID |
getModName() |
⚠️ ContainerContext.getContainerSlots()(供 滚轮方向语义 的
otherInventoryIsAbove 使用)没有 mod 分支——它无条件执行 getSlots(asContainer(container)),
即只对真正的 net.minecraft.inventory.Container 生效。实现本接口的界面若在此传入非 Container
对象,位置感知滚轮方向(值 2 / 3)会抛 ClassCastException。
实现建议
getAPIVersion() 返回 1;
- 栏位数会随运行时变化时,
getModSlotCount 每次实时计算(源码注释点名 NEI 配方界面就是这种情形);
- 只禁用滚轮用
isWheelTweakDisabled(),不要用 isMouseTweaksDisabled() 顺带关掉点击类 tweak;
getModContainer() 在多容器界面必须返回当前生效的那个,ContainerContext 只在
打开界面时取一次并缓存。
相关条目