Skip to content

Commit ed129d4

Browse files
committed
Successful rendering code for field projector and dish
1 parent 49ddebc commit ed129d4

File tree

11 files changed

+529
-105
lines changed

11 files changed

+529
-105
lines changed

src/main/java/com/robotgryphon/compactcrafting/CompactCrafting.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.robotgryphon.compactcrafting;
22

3+
import com.robotgryphon.compactcrafting.client.ClientSetup;
4+
import com.robotgryphon.compactcrafting.client.render.RenderTickCounter;
35
import com.robotgryphon.compactcrafting.core.Registration;
46
import net.minecraft.block.Blocks;
57
import net.minecraft.item.ItemGroup;
@@ -44,6 +46,12 @@ public CompactCrafting() {
4446
// Register ourselves for server and other game events we are interested in
4547
MinecraftForge.EVENT_BUS.register(this);
4648

49+
MinecraftForge.EVENT_BUS.register(RenderTickCounter.class);
50+
51+
modBus.addListener(ClientSetup::init);
52+
modBus.addListener(ClientSetup::registerSpecialModels);
53+
54+
4755
Registration.init();
4856
}
4957

src/main/java/com/robotgryphon/compactcrafting/blocks/FieldProjectorBlock.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package com.robotgryphon.compactcrafting.blocks;
22

3+
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
34
import com.robotgryphon.compactcrafting.core.Registration;
45
import mcjty.theoneprobe.api.IProbeHitData;
56
import mcjty.theoneprobe.api.IProbeInfo;
67
import mcjty.theoneprobe.api.IProbeInfoProvider;
78
import mcjty.theoneprobe.api.ProbeMode;
89
import net.minecraft.block.Block;
10+
import net.minecraft.block.BlockRenderType;
911
import net.minecraft.block.BlockState;
1012
import net.minecraft.entity.player.PlayerEntity;
1113
import net.minecraft.state.DirectionProperty;
1214
import net.minecraft.state.StateContainer;
15+
import net.minecraft.tileentity.TileEntity;
1316
import net.minecraft.util.Direction;
17+
import net.minecraft.util.math.BlockPos;
18+
import net.minecraft.util.math.shapes.VoxelShape;
19+
import net.minecraft.util.math.shapes.VoxelShapes;
20+
import net.minecraft.world.IBlockReader;
1421
import net.minecraft.world.World;
1522

23+
import javax.annotation.Nullable;
24+
1625
public class FieldProjectorBlock extends Block implements IProbeInfoProvider {
1726

1827
public static final DirectionProperty FACING = DirectionProperty.create("facing", Direction.Plane.HORIZONTAL);
@@ -24,15 +33,31 @@ public FieldProjectorBlock(Properties properties) {
2433
.with(FACING, Direction.NORTH));
2534
}
2635

36+
@Override
37+
public VoxelShape getRenderShape(BlockState state, IBlockReader worldIn, BlockPos pos) {
38+
return VoxelShapes.empty();
39+
}
40+
2741
@Override
2842
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
2943
super.fillStateContainer(builder);
3044
builder.add(FACING);
3145
}
3246

47+
@Override
48+
public boolean hasTileEntity(BlockState state) {
49+
return true;
50+
}
51+
52+
@Nullable
53+
@Override
54+
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
55+
return new FieldProjectorTile();
56+
}
57+
3358
@Override
3459
public String getID() {
35-
return Registration.PROJECTOR_BLOCK.getId().toString();
60+
return Registration.FIELD_PROJECTOR_BLOCK.getId().toString();
3661
}
3762

3863
@Override
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.robotgryphon.compactcrafting.blocks.tiles;
2+
3+
import com.robotgryphon.compactcrafting.core.Registration;
4+
import net.minecraft.tileentity.TileEntity;
5+
import net.minecraft.tileentity.TileEntityType;
6+
7+
public class FieldProjectorTile extends TileEntity {
8+
9+
public FieldProjectorTile() {
10+
super(Registration.FIELD_PROJECTOR_TILE.get());
11+
}
12+
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.robotgryphon.compactcrafting.client;
2+
3+
import com.robotgryphon.compactcrafting.CompactCrafting;
4+
import com.robotgryphon.compactcrafting.client.render.FieldProjectorRenderer;
5+
import com.robotgryphon.compactcrafting.core.Constants;
6+
import com.robotgryphon.compactcrafting.core.Registration;
7+
import net.minecraft.util.ResourceLocation;
8+
import net.minecraftforge.api.distmarker.Dist;
9+
import net.minecraftforge.client.event.ClientChatReceivedEvent;
10+
import net.minecraftforge.client.event.ModelRegistryEvent;
11+
import net.minecraftforge.client.model.ModelLoader;
12+
import net.minecraftforge.fml.client.registry.ClientRegistry;
13+
import net.minecraftforge.fml.client.registry.RenderingRegistry;
14+
import net.minecraftforge.fml.common.Mod;
15+
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
16+
17+
@Mod.EventBusSubscriber(modid = CompactCrafting.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
18+
public class ClientSetup {
19+
20+
public static void init(final FMLClientSetupEvent event) {
21+
//ScreenManager.registerFactory(Registration.FIRSTBLOCK_CONTAINER.get(), FirstBlockScreen::new);
22+
// ModelLoaderRegistry.registerLoader(new ResourceLocation(MyTutorial.MODID, "fancyloader"), new FancyModelLoader());
23+
// MagicRenderer.register();
24+
25+
// RenderTypeLookup.setRenderLayer(Registration.COMPLEX_MULTIPART.get(), RenderType.getTranslucent());
26+
// RenderTypeLookup.setRenderLayer(Registration.FANCYBLOCK.get(), (RenderType) -> true);
27+
28+
ClientRegistry.bindTileEntityRenderer(Registration.FIELD_PROJECTOR_TILE.get(), FieldProjectorRenderer::new);
29+
}
30+
31+
public static void registerSpecialModels(final ModelRegistryEvent registryEvent) {
32+
ModelLoader.addSpecialModel(Constants.FIELD_DISH_RL);
33+
}
34+
35+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.robotgryphon.compactcrafting.client.render;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import com.mojang.blaze3d.platform.GlStateManager;
5+
import com.mojang.blaze3d.vertex.IVertexBuilder;
6+
import com.robotgryphon.compactcrafting.CompactCrafting;
7+
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
8+
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
9+
import com.robotgryphon.compactcrafting.core.Constants;
10+
import com.robotgryphon.compactcrafting.core.Registration;
11+
import net.minecraft.block.BlockState;
12+
import net.minecraft.block.Blocks;
13+
import net.minecraft.client.Minecraft;
14+
import net.minecraft.client.renderer.*;
15+
import net.minecraft.client.renderer.model.IBakedModel;
16+
import net.minecraft.client.renderer.model.ModelManager;
17+
import net.minecraft.client.renderer.texture.AtlasTexture;
18+
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
19+
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
20+
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
21+
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
22+
import net.minecraft.util.Direction;
23+
import net.minecraft.util.ResourceLocation;
24+
import net.minecraft.util.math.BlockPos;
25+
import net.minecraft.util.math.vector.Quaternion;
26+
import net.minecraft.util.math.vector.Vector3f;
27+
import net.minecraft.world.World;
28+
import net.minecraftforge.client.model.ModelDataManager;
29+
import net.minecraftforge.client.model.ModelLoaderRegistry;
30+
import net.minecraftforge.client.model.data.EmptyModelData;
31+
import net.minecraftforge.client.model.data.IModelData;
32+
import org.lwjgl.opengl.GL11;
33+
import org.lwjgl.opengl.GLXEXTStereoTree;
34+
35+
public class FieldProjectorRenderer extends TileEntityRenderer<FieldProjectorTile> {
36+
37+
enum RotationSpeed {
38+
SLOW(5000),
39+
MEDIUM(2500),
40+
FAST(1000);
41+
42+
private int speed;
43+
RotationSpeed(int speed) {
44+
this.speed = speed;
45+
}
46+
47+
public int getSpeed() {
48+
return speed;
49+
}
50+
}
51+
52+
// private static IModel model;
53+
private static IBakedModel bakedModel;
54+
private static final ResourceLocation TEXTURE = new ResourceLocation(CompactCrafting.MOD_ID, "block/field_projector_dish");
55+
56+
public FieldProjectorRenderer(TileEntityRendererDispatcher rendererDispatcherIn) {
57+
super(rendererDispatcherIn);
58+
}
59+
60+
@Override
61+
public void render(FieldProjectorTile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
62+
renderDish(tileEntityIn, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn);
63+
}
64+
65+
private void renderDish(FieldProjectorTile te, MatrixStack mx, IRenderTypeBuffer buffer, int combinedLight, int combinedOverlay) {
66+
// GlStateManager.pushAttrib();
67+
// TextureAtlasSprite sprite = Minecraft.getInstance()
68+
// .getAtlasSpriteGetter(AtlasTexture.LOCATION_BLOCKS_TEXTURE)
69+
// .apply(TEXTURE);
70+
71+
72+
73+
mx.push();
74+
75+
mx.translate(.5, 0, .5);
76+
77+
RotationSpeed speed = RotationSpeed.SLOW;
78+
double yaw = Math.sin(Math.toDegrees(RenderTickCounter.renderTicks) / speed.getSpeed()) * 10;
79+
// double yaw = Math.random();
80+
81+
float yDiskOffset = -0.66f;
82+
mx.translate(0.0, -yDiskOffset, 0.0);
83+
mx.rotate(Vector3f.ZP.rotationDegrees((float) yaw));
84+
mx.translate(0.0, yDiskOffset, 0.0);
85+
86+
mx.translate(-.5, 0, -.5);
87+
88+
BlockRendererDispatcher blockRenderer = Minecraft.getInstance().getBlockRendererDispatcher();
89+
ModelManager models = Minecraft.getInstance()
90+
.getItemRenderer()
91+
.getItemModelMesher()
92+
.getModelManager();
93+
94+
IBakedModel bakedModel = models.getModel(Constants.FIELD_DISH_RL);
95+
BlockState state = te.getBlockState();
96+
IVertexBuilder cutoutBlocks = buffer.getBuffer(Atlases.getCutoutBlockType());
97+
IModelData model = ModelDataManager.getModelData(te.getWorld(), te.getPos());
98+
99+
blockRenderer.getBlockModelRenderer()
100+
.renderModel(mx.getLast(), cutoutBlocks, state, bakedModel, 1, 1, 1, combinedLight, combinedOverlay,
101+
model);
102+
103+
// blockRenderer.renderBlock(state, mx, buffer, combinedLight, combinedOverlay, EmptyModelData.INSTANCE);
104+
105+
mx.pop();
106+
107+
// GlStateManager.popAttrib();
108+
}
109+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.robotgryphon.compactcrafting.client.render;
2+
3+
import net.minecraftforge.event.TickEvent;
4+
import net.minecraftforge.eventbus.api.SubscribeEvent;
5+
6+
public class RenderTickCounter {
7+
public static int renderTicks = 0;
8+
9+
@SubscribeEvent
10+
public static void onRenderTick(TickEvent.RenderTickEvent event) {
11+
if(event.phase == TickEvent.RenderTickEvent.Phase.START) {
12+
renderTicks++;
13+
}
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.robotgryphon.compactcrafting.core;
2+
3+
import com.robotgryphon.compactcrafting.CompactCrafting;
4+
import net.minecraft.util.ResourceLocation;
5+
6+
public abstract class Constants {
7+
public static final ResourceLocation FIELD_DISH_RL = new ResourceLocation(CompactCrafting.MOD_ID, "block/field_projector_dish");
8+
}

src/main/java/com/robotgryphon/compactcrafting/core/Registration.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
5+
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
56
import net.minecraft.block.Block;
67
import net.minecraft.block.material.Material;
78
import net.minecraft.item.Item;
@@ -23,8 +24,7 @@ public class Registration {
2324
// ================================================================================================================
2425
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID);
2526
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
26-
private static final DeferredRegister<TileEntityType<?>> TILES_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, MOD_ID);
27-
// private static final DeferredRegister<Dimension> DIMENSIONS = DeferredRegister.create(ForgeRegistries.WORLD_CARVERS, MODID);;
27+
private static final DeferredRegister<TileEntityType<?>> TILE_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, MOD_ID);
2828

2929
// ================================================================================================================
3030
// PROPERTIES
@@ -43,19 +43,18 @@ public class Registration {
4343
// ================================================================================================================
4444
// BLOCKS
4545
// ================================================================================================================
46-
public static final RegistryObject<Block> PROJECTOR_BLOCK = BLOCKS.register("field_projector", () ->
46+
public static final RegistryObject<Block> FIELD_PROJECTOR_BLOCK = BLOCKS.register("field_projector", () ->
4747
new FieldProjectorBlock(Block.Properties.create(Material.IRON)
4848
.hardnessAndResistance(8, 20)
4949
));
5050

5151
// ================================================================================================================
5252
// TILE ENTITIES
5353
// ================================================================================================================
54-
// public static final RegistryObject<TileEntityType<CompactMachineTile>> MACHINE_TILE_ENTITY = TILES_ENTITIES.register("compact_machine", () ->
55-
// TileEntityType.Builder.create(CompactMachineTile::new,
56-
// MACHINE_BLOCK_TINY.get(), MACHINE_BLOCK_SMALL.get(), MACHINE_BLOCK_NORMAL.get(),
57-
// MACHINE_BLOCK_NORMAL.get(), MACHINE_BLOCK_GIANT.get(), MACHINE_BLOCK_MAXIMUM.get())
58-
// .build(null));
54+
public static final RegistryObject<TileEntityType<FieldProjectorTile>> FIELD_PROJECTOR_TILE = TILE_ENTITIES.register("field_projector", () ->
55+
TileEntityType.Builder
56+
.create(FieldProjectorTile::new, FIELD_PROJECTOR_BLOCK.get())
57+
.build(null));
5958

6059
// ================================================================================================================
6160
// INITIALIZATION
@@ -65,6 +64,6 @@ public static void init() {
6564

6665
BLOCKS.register(eventBus);
6766
ITEMS.register(eventBus);
68-
TILES_ENTITIES.register(eventBus);
67+
TILE_ENTITIES.register(eventBus);
6968
}
7069
}

0 commit comments

Comments
 (0)