zh_cn:tutorial:extendedscreenhandler
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
zh_cn:tutorial:extendedscreenhandler [2022/02/10 12:50] – created timothy_starman | zh_cn:tutorial:extendedscreenhandler [2022/02/10 13:26] – 注册我们的 ScreenHandler timothy_starman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 使用扩展的 ScreenHandler 同步数据 ====== | + | ====== |
- | 在本教程中,我们将使用 ExtendedScreenHandler 在 ScreenHandler 打开时将任意数据从服务器传输到客户端 ScreenHandler。 | + | 在本教程中,我们将使用 ExtendedScreenHandler |
+ | |||
在我们的示例中,我们将用方块的位置作为容器的标题。 | 在我们的示例中,我们将用方块的位置作为容器的标题。 | ||
- | 要理解本教程,您需要阅读第一个[[tutorial: | + | ====== 方块实体 ====== |
- | 此处没有代码的方法已在该教程中显示。 | + | 由于 Block 类根本不需要更改,我们将其留在这里。 |
+ | |||
+ | 我们的方块实体现在实现了 " | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | |||
+ | public class BoxBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, | ||
+ | private final DefaultedList< | ||
+ | |||
+ | public BoxBlockEntity() { | ||
+ | super(Test.BOX_BLOCK_ENTITY); | ||
+ | } | ||
+ | |||
+ | |||
+ | //来自 ImplementedInventory 接口 | ||
+ | |||
+ | @Override | ||
+ | public DefaultedList< | ||
+ | return inventory; | ||
+ | |||
+ | } | ||
+ | |||
+ | // | ||
+ | |||
+ | @Override | ||
+ | public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, | ||
+ | // | ||
+ | // | ||
+ | return new BoxScreenHandler(syncId, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public Text getDisplayName() { | ||
+ | return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | |||
+ | // | ||
+ | //您写入 packetByteBuf 的内容将自动以(数据)包的形式传输到客户端 | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | @Override | ||
+ | public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, | ||
+ | //pos 字段是 BlockEntity 的公共字段 | ||
+ | packetByteBuf.writeBlockPos(pos); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== 新的 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, | ||
+ | //[...] | ||
+ | // | ||
+ | |||
+ | //想一想,为什么我们在这里使用 BlockPos.ORIGIN? | ||
+ | //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, | ||
+ | // | ||
+ | } | ||
+ | |||
+ | // | ||
+ | //我们在此处获取具有正确 BlockPos | ||
+ | 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 { | ||
+ | [...] | ||
+ | |||
+ | //我们现在使用 registerExtended 作为我们的 screenHandler 现在在其构造函数中接受一个 packetByteBuf | ||
+ | BOX_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(BOX, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ |
zh_cn/tutorial/extendedscreenhandler.txt · Last modified: 2022/02/10 13:32 by timothy_starman