get configuration working!

This commit is contained in:
Meredith Espinosa 2019-06-18 16:51:16 -07:00
parent f52e43efe6
commit b32276278e
3 changed files with 90 additions and 19 deletions

View File

@ -2,12 +2,14 @@ package io.github.cottonmc.slopetest.block;
import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity; import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity;
import net.fabricmc.fabric.api.block.FabricBlockSettings; import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.minecraft.block.*; import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity; 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.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateFactory; import net.minecraft.state.StateFactory;
import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties; 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.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; 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.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -23,6 +27,12 @@ import javax.annotation.Nullable;
public class SlopeTestBlock extends Block implements BlockEntityProvider { public class SlopeTestBlock extends Block implements BlockEntityProvider {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; 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() { public SlopeTestBlock() {
super(FabricBlockSettings.of(Material.WOOD).build()); super(FabricBlockSettings.of(Material.WOOD).build());
this.setDefaultState(this.getStateFactory().getDefaultState().with(FACING, Direction.NORTH)); 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 (world.isClient || !(world.getBlockEntity(pos) instanceof SlopeTestEntity)) return true;
if (player.getStackInHand(hand).getItem() instanceof BlockItem) { if (player.getStackInHand(hand).getItem() instanceof BlockItem) {
Block block = ((BlockItem)player.getStackInHand(hand).getItem()).getBlock(); Block block = ((BlockItem)player.getStackInHand(hand).getItem()).getBlock();
SlopeTestEntity be = (SlopeTestEntity)world.getBlockEntity(pos); if (block.getDefaultState().isSimpleFullBlock(world, pos)) {
SlopeTestEntity be = (SlopeTestEntity) world.getBlockEntity(pos);
if (be.getRenderedBlock() == Blocks.AIR) {
be.setRenderedBlock(block); be.setRenderedBlock(block);
player.getStackInHand(hand).decrement(1); if (!player.abilities.creativeMode) player.getStackInHand(hand).decrement(1);
}
}
} }
return true; return true;
} }
@ -62,8 +76,49 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider {
return false; return false;
} }
@Override
public boolean isSimpleFullBlock(BlockState state, BlockView view, BlockPos pos) {
return false;
}
@Override @Override
public BlockRenderType getRenderType(BlockState state) { public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.INVISIBLE; 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);
}
} }

View File

@ -2,15 +2,17 @@ package io.github.cottonmc.slopetest.block.entity;
import io.github.cottonmc.slopetest.SlopeTest; import io.github.cottonmc.slopetest.SlopeTest;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.fabricmc.fabric.api.server.PlayerStream;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSerializable { public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSerializable {
private Block renderedBlock = Blocks.DIAMOND_ORE; private Block renderedBlock = Blocks.AIR;
public SlopeTestEntity() { public SlopeTestEntity() {
super(SlopeTest.SLOPE_ENTITY); super(SlopeTest.SLOPE_ENTITY);
@ -28,7 +30,7 @@ public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSer
@Override @Override
public void fromTag(CompoundTag tag) { public void fromTag(CompoundTag tag) {
super.fromTag(tag); super.fromTag(tag);
// renderedBlock = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); renderedBlock = Registry.BLOCK.get(new Identifier(tag.getString("Block")));
} }
@Override @Override
@ -47,4 +49,15 @@ public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSer
public CompoundTag toClientTag(CompoundTag tag) { public CompoundTag toClientTag(CompoundTag tag) {
return toTag(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());
}
}
}
} }

View File

@ -30,10 +30,14 @@ public class SlopeTestRenderer extends BlockEntityRenderer<SlopeTestEntity> {
renderManager.textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX); renderManager.textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX);
BlockState state = getWorld().getBlockState(be.getPos()); BlockState state = getWorld().getBlockState(be.getPos());
Direction dir = state.get(Properties.HORIZONTAL_FACING); Direction dir = state.get(Properties.HORIZONTAL_FACING);
Sprite sprite;
if (be.getRenderedBlock() != Blocks.AIR) { if (be.getRenderedBlock() != Blocks.AIR) {
BlockState blockDefaultState = be.getRenderedBlock().getDefaultState(); BlockState blockDefaultState = be.getRenderedBlock().getDefaultState();
BakedModel model = minecraft.getBlockRenderManager().getModel(blockDefaultState); BakedModel model = minecraft.getBlockRenderManager().getModel(blockDefaultState);
Sprite sprite = model.getSprite(); sprite = model.getSprite();
} else {
sprite = minecraft.getSpriteAtlas().getSprite("minecraft:block/scaffolding_top");
}
if (sprite != null) { if (sprite != null) {
buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV_COLOR); buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV_COLOR);
drawSlope(dir, sprite, buffer); drawSlope(dir, sprite, buffer);
@ -46,7 +50,6 @@ public class SlopeTestRenderer extends BlockEntityRenderer<SlopeTestEntity> {
GlStateManager.disableBlend(); GlStateManager.disableBlend();
GlStateManager.enableAlphaTest(); GlStateManager.enableAlphaTest();
} }
}
buffer.setOffset(0.0d, 0.0d, 0.0d); buffer.setOffset(0.0d, 0.0d, 0.0d);
super.render(be, x, y, z, partialTicks, destroyStage); super.render(be, x, y, z, partialTicks, destroyStage);
} }