tutorial:extendedscreenhandler
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:extendedscreenhandler [2020/08/14 16:18] – [BlockEntity] manymoney2 | tutorial:extendedscreenhandler [2020/08/14 16:27] – [Registering our ScreenHandler] manymoney2 | ||
---|---|---|---|
Line 60: | Line 60: | ||
</ | </ | ||
- | ====== Our new ScreenHandler | + | ====== Our new ExtendedScreenHandler |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class BoxScreenHandler extends ScreenHandler { | public class BoxScreenHandler extends ScreenHandler { | ||
//We save the blockPos we got from the Server and provide a getter for it so the BoxScreen can read that information | //We save the blockPos we got from the Server and provide a getter for it so the BoxScreen can read that information | ||
Line 82: | Line 82: | ||
public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
//[...] | //[...] | ||
- | // See [[tutorial: | + | // See first Screenhandler Tutorial for the rest of the code |
| | ||
//Why do we use BlockPos.ORIGIN here? | //Why do we use BlockPos.ORIGIN here? | ||
Line 93: | Line 93: | ||
} | } | ||
+ | //this getter will be used by our Screen class | ||
public BlockPos getPos() { | public BlockPos getPos() { | ||
return pos; | return pos; | ||
Line 102: | Line 103: | ||
} | } | ||
- | // See [[tutorial: | + | // See Screenhandler Tutorial |
// Shift + Player Inv Slot | // Shift + Player Inv Slot | ||
@Override | @Override | ||
public ItemStack transferSlot(PlayerEntity player, int invSlot); | public ItemStack transferSlot(PlayerEntity player, int invSlot); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Using the Information of the ExtendedScreenHandler in our Screen ====== | ||
+ | |||
+ | <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, | ||
+ | //We try to get the block position to use it as our title, if that fails for some reason we will use the default title | ||
+ | } | ||
+ | |||
+ | //This method will try to get the Position from the ScreenHandler, | ||
+ | //get the ScreenHandler instance here which has the correct BlockPos in it! | ||
+ | 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() { [...] } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Registering our ScreenHandler ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | |||
+ | public static final Block BOX_BLOCK; | ||
+ | public static final BlockItem BOX_BLOCK_ITEM; | ||
+ | public static final BlockEntityType< | ||
+ | public static final ScreenHandlerType< | ||
+ | |||
+ | static { | ||
+ | BOX_BLOCK = Registry.register(Registry.BLOCK, | ||
+ | BOX_BLOCK_ITEM = Registry.register(Registry.ITEM, | ||
+ | |||
+ | //The parameter of build at the very end is always null, do not worry about it | ||
+ | BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | ||
+ | | ||
+ | //we now use registerExtended as our screenHandler now accepts a packetByteBuf in its Constructor | ||
+ | BOX_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(BOX, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
} | } | ||
</ | </ | ||
tutorial/extendedscreenhandler.txt · Last modified: 2022/12/17 15:38 by miir