diff --git a/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java b/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java index c980410..a05c6f6 100644 --- a/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java +++ b/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java @@ -2,12 +2,14 @@ package io.github.cottonmc.slopetest.block; import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity; import net.fabricmc.fabric.api.block.FabricBlockSettings; -import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.EntityContext; +import net.minecraft.entity.ItemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; import net.minecraft.state.StateFactory; import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.Properties; @@ -15,6 +17,8 @@ import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; +import net.minecraft.util.shape.VoxelShape; +import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.World; @@ -23,6 +27,12 @@ import javax.annotation.Nullable; public class SlopeTestBlock extends Block implements BlockEntityProvider { public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; + public static final VoxelShape BASE = VoxelShapes.cuboid(0f, 0f, 0f, 1f, 0.5f, 1f); + public static final VoxelShape NORTH = VoxelShapes.cuboid(0f, 0.5f, 0f, 1f, 1f, 0.5f); + public static final VoxelShape SOUTH = VoxelShapes.cuboid(0f, 0.5f, 0.5f, 1f, 1f, 1f); + public static final VoxelShape EAST = VoxelShapes.cuboid(0.5f, 0.5f, 0f, 1f, 1f, 1f); + public static final VoxelShape WEST = VoxelShapes.cuboid(0f, 0.5f, 0f, 0.5f, 1f, 1f); + public SlopeTestBlock() { super(FabricBlockSettings.of(Material.WOOD).build()); this.setDefaultState(this.getStateFactory().getDefaultState().with(FACING, Direction.NORTH)); @@ -50,9 +60,13 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider { if (world.isClient || !(world.getBlockEntity(pos) instanceof SlopeTestEntity)) return true; if (player.getStackInHand(hand).getItem() instanceof BlockItem) { Block block = ((BlockItem)player.getStackInHand(hand).getItem()).getBlock(); - SlopeTestEntity be = (SlopeTestEntity)world.getBlockEntity(pos); - be.setRenderedBlock(block); - player.getStackInHand(hand).decrement(1); + if (block.getDefaultState().isSimpleFullBlock(world, pos)) { + SlopeTestEntity be = (SlopeTestEntity) world.getBlockEntity(pos); + if (be.getRenderedBlock() == Blocks.AIR) { + be.setRenderedBlock(block); + if (!player.abilities.creativeMode) player.getStackInHand(hand).decrement(1); + } + } } return true; } @@ -62,8 +76,49 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider { return false; } + @Override + public boolean isSimpleFullBlock(BlockState state, BlockView view, BlockPos pos) { + return false; + } + @Override public BlockRenderType getRenderType(BlockState state) { return BlockRenderType.INVISIBLE; } + + @Override + public void onBlockRemoved(BlockState state, World world, BlockPos pos, BlockState newState, boolean boolean_1) { + BlockEntity be = world.getBlockEntity(pos); + if (be instanceof SlopeTestEntity) { + SlopeTestEntity slope = (SlopeTestEntity)be; + if (slope.getRenderedBlock() != Blocks.AIR) { + ItemStack stack = new ItemStack(slope.getRenderedBlock()); + ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack); + world.spawnEntity(entity); + } + } + super.onBlockRemoved(state, world, pos, newState, boolean_1); + } + + @Override + public VoxelShape getCollisionShape(BlockState state, BlockView view, BlockPos pos, EntityContext ctx) { + Direction dir = state.get(FACING); + switch(dir) { + case NORTH: + return VoxelShapes.union(BASE, NORTH); + case SOUTH: + return VoxelShapes.union(BASE, SOUTH); + case EAST: + return VoxelShapes.union(BASE, EAST); + case WEST: + return VoxelShapes.union(BASE, WEST); + default: + return VoxelShapes.fullCube(); + } + } + + @Override + public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, EntityContext ctx) { + return getCollisionShape(state, view, pos, ctx); + } } diff --git a/src/main/java/io/github/cottonmc/slopetest/block/entity/SlopeTestEntity.java b/src/main/java/io/github/cottonmc/slopetest/block/entity/SlopeTestEntity.java index 8ddcedf..d9ee266 100644 --- a/src/main/java/io/github/cottonmc/slopetest/block/entity/SlopeTestEntity.java +++ b/src/main/java/io/github/cottonmc/slopetest/block/entity/SlopeTestEntity.java @@ -2,15 +2,17 @@ package io.github.cottonmc.slopetest.block.entity; import io.github.cottonmc.slopetest.SlopeTest; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; +import net.fabricmc.fabric.api.server.PlayerStream; import net.minecraft.block.Block; import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSerializable { - private Block renderedBlock = Blocks.DIAMOND_ORE; + private Block renderedBlock = Blocks.AIR; public SlopeTestEntity() { super(SlopeTest.SLOPE_ENTITY); @@ -28,7 +30,7 @@ public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSer @Override public void fromTag(CompoundTag tag) { super.fromTag(tag); -// renderedBlock = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); + renderedBlock = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); } @Override @@ -47,4 +49,15 @@ public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSer public CompoundTag toClientTag(CompoundTag tag) { return toTag(tag); } + + @Override + public void markDirty() { + super.markDirty(); + if (!this.world.isClient) { + for (Object obj : PlayerStream.watching(this).toArray()) { + ServerPlayerEntity player = (ServerPlayerEntity) obj; + player.networkHandler.sendPacket(this.toUpdatePacket()); + } + } + } } diff --git a/src/main/java/io/github/cottonmc/slopetest/block/entity/render/SlopeTestRenderer.java b/src/main/java/io/github/cottonmc/slopetest/block/entity/render/SlopeTestRenderer.java index 8a95642..00876b6 100644 --- a/src/main/java/io/github/cottonmc/slopetest/block/entity/render/SlopeTestRenderer.java +++ b/src/main/java/io/github/cottonmc/slopetest/block/entity/render/SlopeTestRenderer.java @@ -30,22 +30,25 @@ public class SlopeTestRenderer extends BlockEntityRenderer { renderManager.textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX); BlockState state = getWorld().getBlockState(be.getPos()); Direction dir = state.get(Properties.HORIZONTAL_FACING); + Sprite sprite; if (be.getRenderedBlock() != Blocks.AIR) { BlockState blockDefaultState = be.getRenderedBlock().getDefaultState(); BakedModel model = minecraft.getBlockRenderManager().getModel(blockDefaultState); - Sprite sprite = model.getSprite(); - if (sprite != null) { - buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV_COLOR); - drawSlope(dir, sprite, buffer); - drawLeftSide(dir, sprite, buffer); - drawRightSide(dir, sprite, buffer); - drawBack(dir, sprite, buffer); - drawBottom(sprite, buffer); - GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); - tessellator.draw(); - GlStateManager.disableBlend(); - GlStateManager.enableAlphaTest(); - } + sprite = model.getSprite(); + } else { + sprite = minecraft.getSpriteAtlas().getSprite("minecraft:block/scaffolding_top"); + } + if (sprite != null) { + buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV_COLOR); + drawSlope(dir, sprite, buffer); + drawLeftSide(dir, sprite, buffer); + drawRightSide(dir, sprite, buffer); + drawBack(dir, sprite, buffer); + drawBottom(sprite, buffer); + GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); + tessellator.draw(); + GlStateManager.disableBlend(); + GlStateManager.enableAlphaTest(); } buffer.setOffset(0.0d, 0.0d, 0.0d); super.render(be, x, y, z, partialTicks, destroyStage);