tutorial:screenhandler
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:screenhandler [2021/01/03 23:02] – [Registering our Screen and ScreenHandler] technici4n | tutorial:screenhandler [2022/12/03 13:52] – corrected my previous edit ayutac | ||
---|---|---|---|
Line 23: | Line 23: | ||
@Override | @Override | ||
- | public BlockEntity createBlockEntity(BlockView world) { | + | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { |
- | return new BoxBlockEntity(); | + | return new BoxBlockEntity(pos, state); |
} | } | ||
Line 70: | Line 70: | ||
@Override | @Override | ||
public int getComparatorOutput(BlockState state, World world, BlockPos pos) { | public int getComparatorOutput(BlockState state, World world, BlockPos pos) { | ||
- | return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos)) | + | return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos)); |
} | } | ||
} | } | ||
Line 81: | Line 81: | ||
private final DefaultedList< | private final DefaultedList< | ||
- | public BoxBlockEntity() { | + | public BoxBlockEntity(BlockPos pos, BlockState state) { |
- | super(ExampleMod.BOX_BLOCK_ENTITY); | + | super(ExampleMod.BOX_BLOCK_ENTITY, pos, state); |
} | } | ||
Line 91: | Line 91: | ||
public DefaultedList< | public DefaultedList< | ||
return inventory; | return inventory; | ||
- | |||
} | } | ||
Line 107: | Line 106: | ||
@Override | @Override | ||
public Text getDisplayName() { | public Text getDisplayName() { | ||
- | return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | + | |
+ | return Text.translatable(getCachedState().getBlock().getTranslationKey()); | ||
+ | // for earlier versions | ||
+ | // return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | ||
} | } | ||
| | ||
@Override | @Override | ||
- | public void fromTag(BlockState state, CompoundTag tag) { | + | public void readNbt(NbtCompound nbt) { |
- | super.fromTag(state, tag); | + | super.readNbt(nbt); |
- | Inventories.fromTag(tag, this.inventory); | + | Inventories.readNbt(nbt, this.inventory); |
} | } | ||
@Override | @Override | ||
- | public | + | public |
- | super.toTag(tag); | + | super.writeNbt(nbt); |
- | Inventories.toTag(tag, this.inventory); | + | Inventories.writeNbt(nbt, this.inventory); |
- | return | + | return |
} | } | ||
} | } | ||
Line 146: | Line 148: | ||
//The parameter of build at the very end is always null, do not worry about it | //The parameter of build at the very end is always null, do not worry about it | ||
+ | // pre-1.17 | ||
BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | ||
+ | // In 1.17 use FabricBlockEntityTypeBuilder instead of BlockEntityType.Builder | ||
+ | BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | ||
} | } | ||
Line 240: | Line 245: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class BoxScreen extends HandledScreen< | + | public class BoxScreen extends HandledScreen< |
//A path to the gui texture. In this example we use the texture from the dispenser | //A path to the gui texture. In this example we use the texture from the dispenser | ||
private static final Identifier TEXTURE = new Identifier(" | private static final Identifier TEXTURE = new Identifier(" | ||
- | public BoxScreen(ScreenHandler | + | public BoxScreen(BoxScreenHandler |
super(handler, | super(handler, | ||
} | } | ||
Line 250: | Line 255: | ||
@Override | @Override | ||
protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) { | protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) { | ||
- | RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); | + | RenderSystem.setShader(GameRenderer:: |
- | | + | RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); |
+ | | ||
int x = (width - backgroundWidth) / 2; | int x = (width - backgroundWidth) / 2; | ||
int y = (height - backgroundHeight) / 2; | int y = (height - backgroundHeight) / 2; | ||
Line 284: | Line 290: | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
- | | + | |
} | } | ||
} | } |
tutorial/screenhandler.txt · Last modified: 2024/08/27 04:49 by solidblock