zh_cn:tutorial:extendedscreenhandler
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:extendedscreenhandler [2022/02/10 13:02] – [方块实体] timothy_starman | zh_cn:tutorial:extendedscreenhandler [2022/02/10 13:32] (current) – Result timothy_starman | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 使用扩展的 ScreenHandler 同步数据 ====== | ||
+ | 在本教程中,我们将使用 ExtendedScreenHandler 在 ScreenHandler 打开时将任意数据从服务器传输到客户端 ScreenHandler。 | ||
+ | |||
+ | 在我们的示例中,我们将用方块的位置作为容器的标题。 | ||
+ | |||
====== 方块实体 ====== | ====== 方块实体 ====== | ||
由于 Block 类根本不需要更改,我们将其留在这里。 | 由于 Block 类根本不需要更改,我们将其留在这里。 | ||
- | 我们的方块实体现在实现了 | + | 我们的方块实体现在实现了 |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 51: | Line 56: | ||
</ | </ | ||
+ | |||
+ | ====== 新的 ExtendedScreenHandler (实例) ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public class BoxScreenHandler extends ScreenHandler { | ||
+ | // | ||
+ | private BlockPos pos; | ||
+ | private final Inventory inventory; | ||
+ | |||
+ | // | ||
+ | // | ||
+ | |||
+ | // | ||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
+ | this(syncId, | ||
+ | pos = buf.readBlockPos(); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
+ | //[...] | ||
+ | // | ||
+ | | ||
+ | // | ||
+ | //This is because the packetByteBuf with our blockPosition is only availible on the Client, so we need a placeholder | ||
+ | //value here. This is not a problem however, as the Server version of the ScreenHandler does not really need this | ||
+ | // | ||
+ | // | ||
+ | //因为 ScreenHandler 的服务器版本并不真正需要此信息。 | ||
+ | pos = BlockPos.ORIGIN; | ||
+ | |||
+ | [...] | ||
+ | } | ||
+ | |||
+ | //这个 getter 将被我们的 Screen 类使用 | ||
+ | public BlockPos getPos() { | ||
+ | return pos; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean canUse(PlayerEntity player) { | ||
+ | return this.inventory.canPlayerUse(player); | ||
+ | } | ||
+ | |||
+ | // 参阅 Screenhandler 教程 | ||
+ | // Shift + Player Inv Slot | ||
+ | @Override | ||
+ | public ItemStack transferSlot(PlayerEntity player, int invSlot); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== 在 Screen 中使用 ExtendedScreenHandler 信息 ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | |||
+ | public class BoxScreen extends HandledScreen< | ||
+ | private static final Identifier TEXTURE = new Identifier(" | ||
+ | |||
+ | public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) { | ||
+ | super(handler, | ||
+ | // | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // | ||
+ | private static Optional< | ||
+ | if (handler instanceof BoxScreenHandler) { | ||
+ | BlockPos pos = ((BoxScreenHandler) handler).getPos(); | ||
+ | return pos != null ? Optional.of(new LiteralText(" | ||
+ | } 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() { [...] } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== 注册我们的 ScreenHandler ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | |||
+ | [...] | ||
+ | public static final ScreenHandlerType< | ||
+ | |||
+ | static { | ||
+ | [...] | ||
+ | |||
+ | // | ||
+ | BOX_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(BOX, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== 结果 ====== | ||
+ | 您现在已经了解了如何在 ScreenHandler 打开时传输数据。 在图像中您可以看到结果:方块的标题现在是它的位置。 | ||
+ | 请注意,这只是一个演示,还有更简单的方法可以将位置设置为标题。 | ||
+ | |||
+ | 您可能想知道:// | ||
+ | 这可以通过在屏幕打开后发送自定义数据包来实现。 (see: [[tutorial: | ||
+ | 您可能还想看看'' | ||
+ | |||
+ | 如果您只想同步整数值,您可以使用'' | ||
+ | |||
+ | {{: | ||
+ | |||
zh_cn/tutorial/extendedscreenhandler.1644498137.txt.gz · Last modified: 2022/02/10 13:02 by timothy_starman