public class BoxScreen extends HandledScreen { private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/container/dispenser.png"); public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) { super(handler, inventory, getPositionText(handler).orElse(title)); //我们尝试获取方块位置以将其用作我们的标题,如果由于某种原因失败,我们将使用默认标题 } //此方法将尝试从 ScreenHandler 获取位置,因为 ScreenRendering 仅发生在客户端上, //我们在此处获取具有正确 BlockPos 的 ScreenHandler 实例! private static Optional getPositionText(ScreenHandler handler) { if (handler instanceof BoxScreenHandler) { BlockPos pos = ((BoxScreenHandler) handler).getPos(); return pos != null ? Optional.of(new LiteralText("(" + pos.toShortString() + ")")) : Optional.empty(); } else { return Optional.empty(); } } @Override protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) { [...] } @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { [...] } @Override protected void init() { [...] } }