Skip to content

Commit

Permalink
Bottle with ship block renders water and oak plank
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgreen26 committed Jul 20, 2024
1 parent 16f8e6d commit 432cd52
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package g_mungus.ship_in_a_bottle;

import g_mungus.ship_in_a_bottle.block.ModBlocks;
import g_mungus.ship_in_a_bottle.block.entity.BottleShipEntityRenderer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;

public class ShipInABottleClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.BOTTLEWITHOUTSHIP, RenderLayer.getTranslucent());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.BOTTLEWITHSHIP, RenderLayer.getTranslucent());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.WATERBLOCK, RenderLayer.getTranslucent());

BlockEntityRendererFactories.register(ModBlocks.BOTTLEWITHSHIPENTITY, BottleShipEntityRenderer::new);
}
}
90 changes: 90 additions & 0 deletions src/main/java/g_mungus/ship_in_a_bottle/block/BottleWithShip.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package g_mungus.ship_in_a_bottle.block;

import g_mungus.ship_in_a_bottle.block.entity.BottleWithShipEntity;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
import org.jetbrains.annotations.Nullable;

public class BottleWithShip extends BlockWithEntity implements Waterloggable{

public BottleWithShip(Settings settings) {
super(settings);
setDefaultState(getDefaultState()
.with(Properties.FACING, Direction.NORTH)
.with(WATERLOGGED, false));
}



public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;

protected static final VoxelShape SHAPE_X = Block.createCuboidShape(0.0, 0.0, 2.0, 16.0, 12.0, 14.0);
protected static final VoxelShape SHAPE_Z = Block.createCuboidShape(2.0, 0.0, 0.0, 14.0, 12.0, 16.0);

public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return switch (state.get(Properties.FACING)) {
default -> SHAPE_Z;
case WEST, EAST -> SHAPE_X;
};
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.FACING, WATERLOGGED);
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return this.getDefaultState().with(Properties.FACING,ctx.getHorizontalPlayerFacing()).with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).isOf(Fluids.WATER));
}

@Override
public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
}

@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(Properties.FACING, rotation.rotate(state.get(Properties.FACING)));
}

@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return state.rotate(mirror.getRotation(state.get(Properties.FACING)));
}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(WATERLOGGED)) {
// This is for 1.17 and below: world.getFluidTickScheduler().schedule(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
}

return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new BottleWithShipEntity(pos, state);
}
}
15 changes: 8 additions & 7 deletions src/main/java/g_mungus/ship_in_a_bottle/block/ModBlocks.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package g_mungus.ship_in_a_bottle.block;

import g_mungus.ship_in_a_bottle.ShipInABottle;
import g_mungus.ship_in_a_bottle.item.ModItems;
import g_mungus.ship_in_a_bottle.block.entity.BottleWithShipEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.loot.LootTable;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
Expand All @@ -18,12 +14,17 @@
public class ModBlocks {
public static final Block SHIPASSEMBLER = registerBlock("ship_assembler", new ShipAssembler(AbstractBlock.Settings.create()));
public static final Block BOTTLEWITHOUTSHIP = registerBlock("bottle_without_ship", new BottleWithoutShip(FabricBlockSettings.create().drops(new Identifier(MOD_ID, "blocks/bottle_without_ship")).nonOpaque().hardness(0.8f)));
public static final Block BOTTLEWITHSHIP = registerBlock("bottle_with_ship", new BottleWithShip(FabricBlockSettings.create().nonOpaque().hardness(0.8f)));
public static final Block WATERBLOCK = registerBlock("water_block_util", new WaterBlock(AbstractBlock.Settings.copy(Blocks.BLUE_STAINED_GLASS)));
public static BlockEntityType<BottleWithShipEntity> BOTTLEWITHSHIPENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, Identifier.of(MOD_ID, "bottle_with_ship_entity"), BlockEntityType.Builder.create(BottleWithShipEntity::new, BOTTLEWITHSHIP).build(null));


private static Block registerBlock(String name, Block block) {
return Registry.register(Registries.BLOCK, Identifier.of(MOD_ID, name), block);
}



public static void registerModBlocks () {
ShipInABottle.LOGGER.info("Registering mod blocks for " + MOD_ID);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/g_mungus/ship_in_a_bottle/block/WaterBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package g_mungus.ship_in_a_bottle.block;

import net.minecraft.block.Block;

public class WaterBlock extends Block {
public WaterBlock(Settings settings) {
super(settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g_mungus.ship_in_a_bottle.block.entity;

import g_mungus.ship_in_a_bottle.block.ModBlocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.*;
import net.minecraft.client.render.block.BlockRenderManager;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.Direction;

public class BottleShipEntityRenderer implements BlockEntityRenderer<BottleWithShipEntity> {

public BottleShipEntityRenderer(BlockEntityRendererFactory.Context context){}

@Override
public void render(BottleWithShipEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
BlockRenderManager blockRenderManager = MinecraftClient.getInstance().getBlockRenderManager();

BlockState state = entity.getCachedState();
Direction direction = state.get(Properties.FACING);

matrices.push();

if (direction == Direction.NORTH) {
matrices.translate(0.15, 0.031, 0.025);
} else if (direction == Direction.SOUTH) {
matrices.translate(0.15, 0.031, 0.275);
} else if (direction == Direction.WEST) {
matrices.translate(0.025, 0.031, 0.15);
} else if (direction == Direction.EAST){
matrices.translate(0.275, 0.031, 0.15);
}

float waterHeight = 2.0F / 16.0F;

matrices.scale(0.7F, waterHeight, 0.7F);

blockRenderManager.renderBlockAsEntity(ModBlocks.WATERBLOCK.getDefaultState(), matrices, vertexConsumers, light, overlay);

matrices.translate(0.15, 0.08, 0.15);
matrices.scale(0.7F, (float) 0.49 / waterHeight, 0.7F);


blockRenderManager.renderBlockAsEntity(Blocks.OAK_PLANKS.getDefaultState(), matrices, vertexConsumers, light, overlay);


matrices.pop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g_mungus.ship_in_a_bottle.block.entity;

import g_mungus.ship_in_a_bottle.block.ModBlocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;

public class BottleWithShipEntity extends BlockEntity {

public BottleWithShipEntity(BlockPos pos, BlockState state) {
super(ModBlocks.BOTTLEWITHSHIPENTITY, pos, state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variants": {
"facing=east": {
"model": "ship-in-a-bottle:block/bottle_without_ship",
"y": 270
},
"facing=north": {
"model": "ship-in-a-bottle:block/bottle_without_ship",
"y": 180
},
"facing=south": {
"model": "ship-in-a-bottle:block/bottle_without_ship"
},
"facing=west": {
"model": "ship-in-a-bottle:block/bottle_without_ship",
"y": 90
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "ship-in-a-bottle:block/water_block_util"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "ship-in-a-bottle:block/underwater"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 432cd52

Please sign in to comment.