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 a05c6f6..b5918b4 100644 --- a/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java +++ b/src/main/java/io/github/cottonmc/slopetest/block/SlopeTestBlock.java @@ -10,6 +10,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsageContext; import net.minecraft.state.StateFactory; import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.Properties; @@ -58,13 +59,15 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider { @Override public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { 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(); - if (block.getDefaultState().isSimpleFullBlock(world, pos)) { + ItemStack stack = player.getStackInHand(hand); + if (stack.getItem() instanceof BlockItem) { + Block block = ((BlockItem)stack.getItem()).getBlock(); + if (block.getDefaultState().getOutlineShape(world, pos) == VoxelShapes.fullCube() && !(block instanceof BlockEntityProvider)) { SlopeTestEntity be = (SlopeTestEntity) world.getBlockEntity(pos); - if (be.getRenderedBlock() == Blocks.AIR) { - be.setRenderedBlock(block); - if (!player.abilities.creativeMode) player.getStackInHand(hand).decrement(1); + if (be.getRenderedState().getBlock() == Blocks.AIR) { + ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit)); + be.setRenderedState(block.getPlacementState(ctx)); + if (!player.abilities.creativeMode) stack.decrement(1); } } } @@ -91,8 +94,8 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider { BlockEntity be = world.getBlockEntity(pos); if (be instanceof SlopeTestEntity) { SlopeTestEntity slope = (SlopeTestEntity)be; - if (slope.getRenderedBlock() != Blocks.AIR) { - ItemStack stack = new ItemStack(slope.getRenderedBlock()); + if (slope.getRenderedState().getBlock() != Blocks.AIR) { + ItemStack stack = new ItemStack(slope.getRenderedState().getBlock()); ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack); world.spawnEntity(entity); } 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 d9ee266..0da7f23 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 @@ -1,9 +1,10 @@ package io.github.cottonmc.slopetest.block.entity; import io.github.cottonmc.slopetest.SlopeTest; +import io.github.cottonmc.slopetest.util.BlockStateUtil; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.fabricmc.fabric.api.server.PlayerStream; -import net.minecraft.block.Block; +import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.CompoundTag; @@ -12,31 +13,31 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; public class SlopeTestEntity extends BlockEntity implements BlockEntityClientSerializable { - private Block renderedBlock = Blocks.AIR; + private BlockState renderedState = Blocks.AIR.getDefaultState(); public SlopeTestEntity() { super(SlopeTest.SLOPE_ENTITY); } - public Block getRenderedBlock() { - return renderedBlock; + public BlockState getRenderedState() { + return renderedState; } - public void setRenderedBlock(Block block) { - this.renderedBlock = block; + public void setRenderedState(BlockState state) { + this.renderedState = state; markDirty(); } @Override public void fromTag(CompoundTag tag) { super.fromTag(tag); - renderedBlock = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); + renderedState = BlockStateUtil.fromTag(tag); } @Override public CompoundTag toTag(CompoundTag tag) { super.toTag(tag); - tag.putString("Block", Registry.BLOCK.getId(renderedBlock).toString()); + BlockStateUtil.toTag(tag, renderedState); return tag; } 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 434a6f9..6cbe9c3 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 @@ -1,7 +1,7 @@ package io.github.cottonmc.slopetest.block.entity.render; import com.mojang.blaze3d.platform.GlStateManager; -import io.github.cottonmc.slopetest.SpriteSet; +import io.github.cottonmc.slopetest.util.SpriteSet; import io.github.cottonmc.slopetest.block.entity.SlopeTestEntity; import net.fabricmc.fabric.api.client.render.ColorProviderRegistry; import net.minecraft.block.BlockState; @@ -35,13 +35,13 @@ public class SlopeTestRenderer extends BlockEntityRenderer { Direction dir = state.get(Properties.HORIZONTAL_FACING); SpriteSet sprites; int color = 0xffffff; - if (be.getRenderedBlock() != Blocks.AIR) { - BlockState blockDefaultState = be.getRenderedBlock().getDefaultState(); - BakedModel model = minecraft.getBlockRenderManager().getModel(blockDefaultState); + if (be.getRenderedState().getBlock() != Blocks.AIR) { + BlockState renderedState = be.getRenderedState(); + BakedModel model = minecraft.getBlockRenderManager().getModel(renderedState); sprites = new SpriteSet(model); - BlockColorProvider blockColor = ColorProviderRegistry.BLOCK.get(be.getRenderedBlock()); + BlockColorProvider blockColor = ColorProviderRegistry.BLOCK.get(be.getRenderedState().getBlock()); if (blockColor != null) { - color = blockColor.getColor(blockDefaultState, be.getWorld(), be.getPos(), 1); + color = blockColor.getColor(renderedState, be.getWorld(), be.getPos(), 1); } } else { sprites = new SpriteSet(minecraft.getSpriteAtlas().getSprite("minecraft:block/scaffolding_top"), false); diff --git a/src/main/java/io/github/cottonmc/slopetest/util/BlockStateUtil.java b/src/main/java/io/github/cottonmc/slopetest/util/BlockStateUtil.java new file mode 100644 index 0000000..75dd567 --- /dev/null +++ b/src/main/java/io/github/cottonmc/slopetest/util/BlockStateUtil.java @@ -0,0 +1,46 @@ +package io.github.cottonmc.slopetest.util; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.state.StateFactory; +import net.minecraft.state.property.Property; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; + +import java.util.Optional; + +public class BlockStateUtil { + public static BlockState fromTag(CompoundTag tag) { + Block block = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); + CompoundTag properties = tag.getCompound("Properties"); + StateFactory factory = block.getStateFactory(); + BlockState state = factory.getDefaultState(); + for (String key : properties.getKeys()) { + Property prop = factory.getProperty(key); + if (prop != null) parseProperty(state, prop, properties.getString(key)); + } + return state; + } + + public static CompoundTag toTag(CompoundTag tag, BlockState state) { + tag.putString("Block", Registry.BLOCK.getId(state.getBlock()).toString()); + CompoundTag properties = new CompoundTag(); + for (Property prop : state.getProperties()) { + String value = state.get(prop).toString(); + properties.putString(prop.getName(), value); + } + tag.put("Properties", properties); + return tag; + } + + public static > void parseProperty(BlockState state, Property property, String value) { + System.out.println(value); + Optional optional = property.getValue(value); + if (optional.isPresent()) { + System.out.println(optional.get()); + state.with(property, optional.get()); + System.out.println(state.get(property)); + } + } +} diff --git a/src/main/java/io/github/cottonmc/slopetest/SpriteSet.java b/src/main/java/io/github/cottonmc/slopetest/util/SpriteSet.java similarity index 59% rename from src/main/java/io/github/cottonmc/slopetest/SpriteSet.java rename to src/main/java/io/github/cottonmc/slopetest/util/SpriteSet.java index ede8d58..9bc4092 100644 --- a/src/main/java/io/github/cottonmc/slopetest/SpriteSet.java +++ b/src/main/java/io/github/cottonmc/slopetest/util/SpriteSet.java @@ -1,8 +1,10 @@ -package io.github.cottonmc.slopetest; +package io.github.cottonmc.slopetest.util; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.texture.Sprite; +import net.minecraft.util.Identifier; import net.minecraft.util.math.Direction; import java.util.HashMap; @@ -11,10 +13,11 @@ import java.util.Map; import java.util.Random; public class SpriteSet { - Sprite global; - Map quads = new HashMap<>(); - boolean singleSprite = false; - boolean globalHasColor = false; + private Sprite global; + private Map quads; + private boolean singleSprite = false; + private boolean globalHasColor = false; + public static final Sprite FALLBACK = MinecraftClient.getInstance().getSpriteAtlas().getSprite(new Identifier("minecraft:block/scaffolding_top")); public SpriteSet(Sprite allSprite, boolean hasColor) { this.global = allSprite; @@ -24,19 +27,22 @@ public class SpriteSet { public SpriteSet(BakedModel model) { Random rand = new Random(); + quads = new HashMap<>(); for (Direction dir : Direction.values()) { List quads = model.getQuads(null, dir, rand); - this.quads.put(dir, quads.get(0)); + if (!quads.isEmpty()) this.quads.put(dir, quads.get(0)); } } public Sprite getSprite(Direction dir) { if (singleSprite) return global; + if (quads.get(dir) == null) return FALLBACK; else return quads.get(dir).getSprite(); } public boolean hasColor(Direction dir) { if (singleSprite) return globalHasColor; + if (quads.get(dir) == null) return false; else return quads.get(dir).hasColor(); } }