User Tools

Site Tools


zh_cn:tutorial:extendedscreenhandler

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
zh_cn:tutorial:extendedscreenhandler [2022/02/10 13:19] – [使用扩展的 ScreenHandler 同步数据] timothy_starmanzh_cn:tutorial:extendedscreenhandler [2022/02/10 13:24] – 在 Screen 中使用 ExtendedScreenHandler 信息 timothy_starman
Line 105: Line 105:
     public ItemStack transferSlot(PlayerEntity player, int invSlot);     public ItemStack transferSlot(PlayerEntity player, int invSlot);
 } }
 +</code>
 +
 +====== 在 Screen 中使用 ExtendedScreenHandler 信息 ======
 +
 +<code java [enable_line_numbers="true"] BoxScreen.java>
 +
 +public class BoxScreen extends HandledScreen<ScreenHandler> {
 +    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<Text> 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() { [...] }
 +}
 +
 </code> </code>
  
zh_cn/tutorial/extendedscreenhandler.txt · Last modified: 2022/02/10 13:32 by timothy_starman