Skip to content

Commit

Permalink
Add coin block
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Sep 3, 2024
1 parent a02c03c commit 443c880
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public boolean skipRendering(BlockState state, BlockState neighborState, Directi
}

@Override
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor worldAccessor, BlockPos pos, BlockPos pos2) {
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor worldAccessor, BlockPos pos, BlockPos neighborPos) {
Block blockAbove = worldAccessor.getBlockState(pos.above()).getBlock();
Block blockBelow = worldAccessor.getBlockState(pos.below()).getBlock();
Block blockNorth = worldAccessor.getBlockState(pos.north()).getBlock();
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/com/wenxin2/marioverse/blocks/CoinBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.wenxin2.marioverse.blocks;

import com.wenxin2.marioverse.init.BlockRegistry;
import java.util.Properties;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.RotationSegment;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

public class CoinBlock extends Block implements SimpleWaterloggedBlock {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16;
public static final int MAX = RotationSegment.getMaxSegmentIndex();
private static final int ROTATIONS = MAX + 1;

protected static final VoxelShape SHAPE = Block.box(4.0, 0.0, 4.0, 12.0, 8.0, 12.0);
protected static final VoxelShape ANGLED = Block.box(4.0, 0.0, 4.0, 12.0, 8.0, 12.0);

public CoinBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(WATERLOGGED, Boolean.FALSE).setValue(ROTATION, 0));
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) {
stateBuilder.add(ROTATION, WATERLOGGED);
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext placeContext) {
FluidState fluidState = placeContext.getLevel().getFluidState(placeContext.getClickedPos());

return this.defaultBlockState().setValue(WATERLOGGED, fluidState.is(FluidTags.WATER) && fluidState.getAmount() == 8)
.setValue(ROTATION, RotationSegment.convertToSegment(placeContext.getRotation()));
}

@Override
public void onPlace(BlockState state, Level world, BlockPos pos, BlockState oldState, boolean notify) {
if (!world.isClientSide()) {
world.scheduleTick(pos, this, 1);
}
super.onPlace(state, world, pos, oldState, notify);
}

@Override
protected void neighborChanged(BlockState state, Level world, BlockPos pos, Block blockNeighbor, BlockPos posNeighbor, boolean isMoving) {
if (!world.isClientSide()) {
world.scheduleTick(pos, this, 1);
}
super.neighborChanged(state, world, pos, blockNeighbor, posNeighbor, isMoving);
}

@NotNull
@Override
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor worldAccessor, BlockPos pos, BlockPos neighborPos) {
if (state.getValue(WATERLOGGED)) {
worldAccessor.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(worldAccessor));
}

return super.updateShape(state, direction, neighborState, worldAccessor, pos, neighborPos);
}

@Override
protected void tick(BlockState state, ServerLevel serverWorld, BlockPos pos, RandomSource random) {
int currentRotation = state.getValue(ROTATION);
int nextRotation = (currentRotation + 1) % 16; // Cycle through all 16 rotation states

serverWorld.setBlock(pos, state.setValue(ROTATION, nextRotation), 3);

serverWorld.scheduleTick(pos, this, 1);
super.tick(state, serverWorld, pos, random);
}

@NotNull
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(ROTATION, rotation.rotate(state.getValue(ROTATION), ROTATIONS));
}

@NotNull
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.setValue(ROTATION, mirror.mirror(state.getValue(ROTATION), ROTATIONS));
}

@Override
protected void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) {
ItemStack coinItem = new ItemStack(this.asItem());

if (entity instanceof Player player) {
world.removeBlock(pos, Boolean.TRUE);
player.addItem(coinItem);

if (!player.addItem(coinItem)) {
player.drop(coinItem, false);
}
}
super.entityInside(state, world, pos, entity);
}

@NotNull
@Override
public FluidState getFluidState(final BlockState state)
{
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/wenxin2/marioverse/init/BlockRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.wenxin2.marioverse.Marioverse;
import com.wenxin2.marioverse.blocks.ClearWarpPipeBlock;
import com.wenxin2.marioverse.blocks.CoinBlock;
import com.wenxin2.marioverse.blocks.PipeBubblesBlock;
import com.wenxin2.marioverse.blocks.WarpPipeBlock;
import com.wenxin2.marioverse.blocks.WaterSpoutBlock;
Expand Down Expand Up @@ -34,12 +35,18 @@
public class BlockRegistry {
public static final EnumMap<DyeColor, DeferredBlock<Block>> WARP_PIPES =
new EnumMap<>(DyeColor.class);
public static final DeferredBlock<Block> COIN;
public static final DeferredBlock<Block> CLEAR_WARP_PIPE;
public static final DeferredBlock<Block> PIPE_BUBBLES;
public static final DeferredBlock<Block> WATER_SPOUT;

static
{
COIN = registerBlock("coin",
() -> new CoinBlock(BlockBehaviour.Properties.of().mapColor(MapColor.GOLD)
.sound(SoundType.NETHERITE_BLOCK).isSuffocating(BlockRegistry::never).isViewBlocking(BlockRegistry::never)
.strength(0.5F, 0.5F).instabreak().noCollission()));

CLEAR_WARP_PIPE = registerBlock("clear_warp_pipe",
() -> new ClearWarpPipeBlock(BlockBehaviour.Properties.of().mapColor(MapColor.NONE)
.sound(SoundType.GLASS).isSuffocating(BlockRegistry::never).isViewBlocking(BlockRegistry::never)
Expand Down
64 changes: 64 additions & 0 deletions src/main/resources/assets/marioverse/blockstates/coin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"variants": {
"rotation=0": {
"model": "marioverse:block/coin/coin"
},
"rotation=1": {
"model": "marioverse:block/coin/coin_22"
},
"rotation=2": {
"model": "marioverse:block/coin/coin_45"
},
"rotation=3": {
"model": "marioverse:block/coin/coin_67"
},
"rotation=4": {
"model": "marioverse:block/coin/coin",
"y": 90
},
"rotation=5": {
"model": "marioverse:block/coin/coin_22",
"y": 90
},
"rotation=6": {
"model": "marioverse:block/coin/coin_45",
"y": 90
},
"rotation=7": {
"model": "marioverse:block/coin/coin_67",
"y": 90
},
"rotation=8": {
"model": "marioverse:block/coin/coin",
"y": 180
},
"rotation=9": {
"model": "marioverse:block/coin/coin_22",
"y": 180
},
"rotation=10": {
"model": "marioverse:block/coin/coin_45",
"y": 180
},
"rotation=11": {
"model": "marioverse:block/coin/coin_67",
"y": 180
},
"rotation=12": {
"model": "marioverse:block/coin/coin",
"y": 270
},
"rotation=13": {
"model": "marioverse:block/coin/coin_22",
"y": 270
},
"rotation=14": {
"model": "marioverse:block/coin/coin_45",
"y": 270
},
"rotation=15": {
"model": "marioverse:block/coin/coin_67",
"y": 270
}
}
}
25 changes: 25 additions & 0 deletions src/main/resources/assets/marioverse/models/block/coin/coin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"credit": "Made with Blockbench",
"parent": "minecraft:block/cube_bottom_top",
"render_type": "cutout",
"textures": {
"particle": "marioverse:block/coin",
"coin": "marioverse:block/coin"
},
"elements": [
{
"name": "coin",
"from": [3, 3, 7],
"to": [13, 15, 9],
"rotation": {"angle": 0, "axis": "y", "origin": [8, 9, 8]},
"faces": {
"north": {"uv": [3, 2, 13, 14], "texture": "#coin"},
"east": {"uv": [0, 2, 2, 14], "texture": "#coin"},
"south": {"uv": [13, 2, 3, 14], "texture": "#coin"},
"west": {"uv": [14, 2, 16, 14], "texture": "#coin"},
"up": {"uv": [3, 0, 13, 2], "rotation": 180, "texture": "#coin"},
"down": {"uv": [3, 14, 13, 16], "rotation": 180, "texture": "#coin"}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"credit": "Made with Blockbench",
"parent": "minecraft:block/cube_bottom_top",
"render_type": "cutout",
"textures": {
"particle": "marioverse:block/coin",
"coin": "marioverse:block/coin"
},
"elements": [
{
"name": "coin",
"from": [3, 3, 7],
"to": [13, 15, 9],
"rotation": {"angle": -22.5, "axis": "y", "origin": [8, 9, 8]},
"faces": {
"north": {"uv": [3, 2, 13, 14], "texture": "#coin"},
"east": {"uv": [0, 2, 2, 14], "texture": "#coin"},
"south": {"uv": [13, 2, 3, 14], "texture": "#coin"},
"west": {"uv": [14, 2, 16, 14], "texture": "#coin"},
"up": {"uv": [3, 0, 13, 2], "rotation": 180, "texture": "#coin"},
"down": {"uv": [3, 14, 13, 16], "rotation": 180, "texture": "#coin"}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"credit": "Made with Blockbench",
"parent": "minecraft:block/cube_bottom_top",
"render_type": "cutout",
"textures": {
"particle": "marioverse:block/coin",
"coin": "marioverse:block/coin"
},
"elements": [
{
"name": "coin",
"from": [3, 3, 7],
"to": [13, 15, 9],
"rotation": {"angle": -45, "axis": "y", "origin": [8, 9, 8]},
"faces": {
"north": {"uv": [3, 2, 13, 14], "texture": "#coin"},
"east": {"uv": [0, 2, 2, 14], "texture": "#coin"},
"south": {"uv": [13, 2, 3, 14], "texture": "#coin"},
"west": {"uv": [14, 2, 16, 14], "texture": "#coin"},
"up": {"uv": [3, 0, 13, 2], "rotation": 180, "texture": "#coin"},
"down": {"uv": [3, 14, 13, 16], "rotation": 180, "texture": "#coin"}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"credit": "Made with Blockbench",
"parent": "minecraft:block/cube_bottom_top",
"render_type": "cutout",
"textures": {
"particle": "marioverse:block/coin",
"coin": "marioverse:block/coin"
},
"elements": [
{
"name": "coin",
"from": [7, 3, 3],
"to": [9, 15, 13],
"rotation": {"angle": 22.5, "axis": "y", "origin": [8, 9, 8]},
"faces": {
"north": {"uv": [14, 2, 16, 14], "texture": "#coin"},
"east": {"uv": [3, 2, 13, 14], "texture": "#coin"},
"south": {"uv": [0, 2, 2, 14], "texture": "#coin"},
"west": {"uv": [13, 2, 3, 14], "texture": "#coin"},
"up": {"uv": [3, 0, 13, 2], "rotation": 270, "texture": "#coin"},
"down": {"uv": [3, 14, 13, 16], "rotation": 90, "texture": "#coin"}
}
}
]
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/marioverse/models/item/coin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "marioverse:item/coin"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 443c880

Please sign in to comment.