# 自定义容器

# 概述

该功能类似自定义箱子,使用原生c++驱动背包界面,实现自定义容器界面的方块。 包含长按分堆、双击合堆、右键拖放等功能,方块破坏后内部物品会掉落,方便开发者制作自定义熔炉等界面,而无需考虑复杂的UI逻辑,数据同步逻辑等。

# base_block新增netease_container

base_block指定现在可以指定为netease_container

# netease:block_container

类型 默认值 解释
custom_description str 容器名 容器ui打开时的title(复用原版ui时生效)
screen_name str ui名 交互方块时打开的ui,形如namespace.screenName
container_size int 容器容量 方块容器能够存放的物品槽位数量,取值范围1-108,需与ui槽位匹配。若ui槽位大于配置容量,超过配置的部分无法放入物品;若ui槽位小于配置容量,按shift可放入,但无法在ui中显示
hopper str {} 配置支持漏斗漏入漏出,详细请见下面hopper字段说明

# hopper字段说明

  • hopper:漏斗字段,无此字段或字段为空时无用,dict
  • input:是否支持漏入,True为使用input_solt字段中漏入规则;False为不支持漏入,input_solt不会起作用。缺省代表支持全部漏入,input_slot不会起作用
  • input_slot:支持漏入字段,list,当input字段存在且为True时,漏入规则才会生效;无此字段、字段为空、input字段不存在时代表所有朝向支持漏入
  • face:支持漏入的面,int
  • slot:支持漏入netease_container的槽位,如[1,10]代表1和10槽位支持漏入,其余槽位不支持漏入,无此字段和字段为空时代表所有槽位支持漏入 list
  • output:为true时代表支持自定义容器底部漏出功能,bool,无此字段代表支持底部漏出

# hopper字段举例

"hopper": {
    "input": true,
	"input_slot": [
		{
			"face": 1,
			"slot": [1,10]
		}
	],
	"output": true
}

# 注意事项

  • netease_container的size限定为1-108,不在该范围会导致注册失败,需在组件中配置

  • netease_ui_container的size为108,ui中如果指定了大于108的slot将无法放入物品,无需配置

# 自定义容器事件

  1. PlayerTryPutCustomContainerItemServerEvent事件,玩家尝试改变自定义容器物品时触发该事件,开发者可以监听该事件实现其他逻辑

# 示例

自定义容器方块配置:

{
	"format_version": "1.16.100",
	"minecraft:block": {
		"components": {
			"netease:block_container": {
				"custom_description": "自定义方块容器",
				"screen_name": "netease_container.netease_custom_container_screen", // 交互该方块时打开的ui
				"container_size": 27 // 关闭后保存在方块中的容器size,对应ui中的netease_container扩展写法
			}
		},
		"description": {
			"base_block": "netease_container",
			"category": "construction",
			"identifier": "test:my_custom_container"
		}
	}
}

ui配置:

此处以原版箱子的json为基础进行修改

注意:

  • collection_name指定为netease_container

  • ui的grid空格数量如果大于组件中设置的size,那超出的那一部分无法放置物品

{
    "namespace": "netease_container",
    "test_collection": {
        "type": "stack_panel",
        "size": [ "100%", "100%c" ],
        "controls": [
            {
                "row_1": {
                    "type": "stack_panel",
                    "orientation": "horizontal",
                    "size": [ "100%", "100%cm" ],
                    "collection_name": "netease_container",
                    "controls": [
                        {
                            "a@netease_container.custom_grid_item": {
                                "collection_index": 27
                            }
                        },
                        {
                            "padding": {
                                "type": "panel",
                                "size": [ 4, 0 ]
                            }
                        },
                        {
                            "b@netease_container.custom_grid_item": {
                                "collection_index": 28
                            }
                        }
                    ]
                }
            },
            {
                "padding_1": {
                    "type": "panel",
                    "size": [ "100%", 4 ]
                }
            },
            {
                "row_2": {
                    "type": "stack_panel",
                    "orientation": "horizontal",
                    "size": [ "100%", "100%cm" ],
                    "collection_name": "netease_container",
                    "controls": [
                        {
                            "c@netease_container.custom_grid_item": {
                                "collection_index": 29
                            }
                        },
                        {
                            "padding": {
                                "type": "panel",
                                "size": [ 4, 0 ]
                            }
                        },
                        {
                            "d@netease_container.custom_grid_item": {
                                "collection_index": 30
                            }
                        }
                    ]
                }
            },
            {
                "padding_2": {
                    "type": "panel",
                    "size": [ "100%", 4 ]
                }
            }
        ]
    },
    "custom_grid_item@common.container_item": {
        "$item_collection_name": "netease_container"
    },
    "custom_drop_grid_item@common.container_item": {
        "$item_collection_name": "netease_ui_container"
    },
    "netease_custom_container_grid": {
        "type": "grid",
        "size": [ 162, "100.0%c" ],
        "anchor_from": "top_left",
        "anchor_to": "top_left",
        "grid_dimensions": [ 9, 3 ],
        "grid_item_template": "netease_container.custom_grid_item",
        "collection_name": "netease_container",
        "maximum_grid_items": 27
    },
    "netease_drop_container_grid": {
        "type": "grid",
        "size": [ 162, "100.0%c" ],
        "anchor_from": "top_left",
        "anchor_to": "top_left",
        "grid_dimensions": [ 9, 2 ],
        "grid_item_template": "netease_container.custom_drop_grid_item",
        "collection_name": "netease_ui_container",
        "maximum_grid_items": 18
    },
    "container_label": {
        "type": "label",
        "offset": [ 7, -1 ],
        "anchor_from": "top_left",
        "anchor_to": "top_left",
        "text": "$container_title",
        "size": [ "90%", "default" ],
        "color": "$title_text_color",
        "layer": 2
    },
    "selected_item_details@common.selected_item_details": {
        "offset": [ 0, 0 ]
    },
    "netease_custom_container_panel_top_half": {
        "type": "panel",
        "size": [ "100%", "100.0%c" ],
        "offset": [ 0, 11 ],
        "anchor_to": "top_left",
        "anchor_from": "top_left",
        "controls": [
            {
                "container_label@netease_container.container_label": {}
            },
            {
                "netease_custom_container_grid1@netease_container.netease_custom_container_grid": {
                    "offset": [ 7, 10 ]
                }
            },
            {
                "netease_custom_container_grid2@netease_container.netease_drop_container_grid": {
                    "offset": [ 7, 70 ]
                }
            }
        ]
    },
    "netease_custom_container_panel": {
        "type": "panel",
        "controls": [
            {
                "container_gamepad_helpers@common.container_gamepad_helpers": {}
            },
            {
                "flying_item_renderer@common.flying_item_renderer": {
                    "layer": 11
                }
            },
            {
                "selected_item_details_factory@common.selected_item_details_factory": {
                    "control_name": "@netease_container.selected_item_details"
                }
            },
            {
                "item_lock_notification_factory@common.item_lock_notification_factory": {
                    "control_name": "@common.item_lock_notification"
                }
            },
            {
                "root_panel@common.root_panel": {
                    "size": [ 176, 220 ],
                    "layer": 1,
                    "controls": [
                        {
                            "common_panel@common.common_panel": {
                                "size": [ "100%", "100.0%c" ],
                                "$dialog_background|default": "common.dialog_background_opaque",
                                "controls": [
                                    {
                                        "bg_image@$dialog_background": {
                                            "size": [ "100%", "100.0%c+-37.0px" ],
                                            "layer": 1,
                                            "controls": [
                                                {
                                                    "chest_panel": {
                                                        "type": "panel",
                                                        "layer": 5,
                                                        "size": [ "100%", "100.0%c+-7.0px" ],
                                                        "controls": [
                                                            {
                                                                "netease_custom_container_panel_top_half@netease_container.netease_custom_container_panel_top_half": {}
                                                            },
                                                            {
                                                                "inventory_panel_bottom_half_with_label@common.inventory_panel_bottom_half_with_label": {}
                                                            },
                                                            {
                                                                "hotbar_grid@common.hotbar_grid_template": {}
                                                            }
                                                        ]
                                                    }
                                                },
                                                {
                                                    "inventory_selected_icon_button@common.inventory_selected_icon_button": {}
                                                },
                                                {
                                                    "gamepad_cursor@common.gamepad_cursor_button": {}
                                                },
                                                {
                                                    "close@common.close_button": {
                                                        "layer": "$close_button_layer",
                                                        "offset": "$close_button_offset",
                                                        "ignored": "$use_compact_close_button"
                                                    }
                                                },
                                                {
                                                    "compact_close@common.compact_close_button": {
                                                        "layer": "$close_button_layer",
                                                        "offset": "$close_button_offset",
                                                        "ignored": "(not $use_compact_close_button)"
                                                    }
                                                }
                                            ],
                                            "bindings": [
                                                {
                                                    "binding_name": "$close_button_visible_binding_name",
                                                    "binding_name_override": "#visible"
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "netease_custom_container_screen@common.inventory_screen_common": {
        "$close_on_player_hurt|default": true,
        "close_on_player_hurt": "$close_on_player_hurt",
        "variables": [
            {
                "requires": "$desktop_screen",
                "$screen_content": "netease_container.netease_custom_container_panel",
                "$screen_bg_content": "common.screen_background",
                "$screen_background_alpha": 0.4
            },
            {
                "requires": "$pocket_screen",
                "$screen_content": "netease_container_pocket.netease_custom_container_panel"
            }
        ]
    }
}