tutorial:blockentityrenderers
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:blockentityrenderers [2019/08/13 12:46] – fudge | tutorial:blockentityrenderers [2019/12/14 00:02] – 1.15 juuz | ||
---|---|---|---|
Line 16: | Line 16: | ||
// A jukebox itemstack | // A jukebox itemstack | ||
private static ItemStack stack = new ItemStack(Items.JUKEBOX, | private static ItemStack stack = new ItemStack(Items.JUKEBOX, | ||
+ | | ||
+ | public MyBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) { | ||
+ | super(dispatcher); | ||
+ | } | ||
| | ||
@Override | @Override | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
} | } | ||
} | } | ||
</ | </ | ||
- | And register | + | We're going to need to register |
+ | This wouldn' | ||
+ | However, in a multiplayer setting, where the server runs in a different process than the client, the server code | ||
+ | has no concept of a " | ||
+ | To run initialization code only for the client, we need to setup a '' | ||
+ | |||
+ | Create a new class next to your main class that implements '' | ||
<code java> | <code java> | ||
- | @Override | + | public class ExampleModClient implements ClientModInitializer { |
- | public void onInitialize() { | + | |
+ | public void onInitializeClient() { | ||
+ | // Here we will put client-only registration code | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Set this class as the '' | ||
+ | <code javascript " | ||
+ | " | ||
[...] | [...] | ||
- | BlockEntityRendererRegistry.INSTANCE.register(DemoBlockEntity.class, | + | |
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And register the '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public void onInitializeClient() { | ||
+ | | ||
} | } | ||
</ | </ | ||
We override the '' | We override the '' | ||
- | - for starters, call '' | + | - for starters, call '' |
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
- | GlStateManager.pushMatrix(); | + | matrices.push(); |
} | } | ||
</ | </ | ||
- | We then perform the movement of the jukebox (GlStateManager.translatef) and rotation (GlStateManager.rotatef). | + | We then perform the movement of the jukebox (matrices.translate) and rotation (matrices.multiply). |
- | There are two parts to the translation: | + | There are two parts to the translation: |
The second part is the part that changes: the offset in the y value. The offset is the height of the item for any given frame. | The second part is the part that changes: the offset in the y value. The offset is the height of the item for any given frame. | ||
We recalculate this each time because we want it to be animating bouncing up and down. We calculate this by: | We recalculate this each time because we want it to be animating bouncing up and down. We calculate this by: | ||
Line 47: | Line 78: | ||
* Dividing that by 4 to compress the sine wave vertically so the item doesn’t move up and down as much. | * Dividing that by 4 to compress the sine wave vertically so the item doesn’t move up and down as much. | ||
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
// Calculate the current offset in the y value | // Calculate the current offset in the y value | ||
- | double offset = Math.sin((blockEntity.getWorld().getTime() + partialTicks) / 8.0) / 4.0; | + | double offset = Math.sin((blockEntity.getWorld().getTime() + tickDelta) / 8.0) / 4.0; |
// Move the item | // Move the item | ||
- | | + | |
// Rotate the item | // Rotate the item | ||
- | | + | |
} | } | ||
</ | </ | ||
Line 61: | Line 92: | ||
We also pass '' | We also pass '' | ||
an item lying on the ground. Try experimenting with this value and see what happens (it's an enum). | an item lying on the ground. Try experimenting with this value and see what happens (it's an enum). | ||
- | We also need to call '' | + | We also need to call '' |
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
- | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, | + | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, |
// Mandatory call after GL calls | // Mandatory call after GL calls | ||
- | | + | |
} | } | ||
</ | </ | ||
Line 78: | Line 109: | ||
To fix this, we will tell Minecraft to receive light from //one block above// the location of the block entity. | To fix this, we will tell Minecraft to receive light from //one block above// the location of the block entity. | ||
- | To get the light, we call '' | + | To get the light, we call '' |
- | and to use the light we call '' | + | and to use the light we use it in '' |
<code java> | <code java> | ||
@Override | @Override | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
| | ||
- | | + | |
- | int light = blockEntity.getWorld().getLightmapIndex(blockEntity.getPos().up(), 0); | + | |
- | | + | |
| | ||
[...] | [...] |
tutorial/blockentityrenderers.txt · Last modified: 2023/02/09 13:14 by mschae23