try out putting block states on slopes instead of blocks
This commit is contained in:
parent
bcd5fdd962
commit
e58edda102
@ -10,6 +10,7 @@ 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.item.ItemStack;
|
||||||
|
import net.minecraft.item.ItemUsageContext;
|
||||||
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;
|
||||||
@ -58,13 +59,15 @@ public class SlopeTestBlock extends Block implements BlockEntityProvider {
|
|||||||
@Override
|
@Override
|
||||||
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
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 (world.isClient || !(world.getBlockEntity(pos) instanceof SlopeTestEntity)) return true;
|
||||||
if (player.getStackInHand(hand).getItem() instanceof BlockItem) {
|
ItemStack stack = player.getStackInHand(hand);
|
||||||
Block block = ((BlockItem)player.getStackInHand(hand).getItem()).getBlock();
|
if (stack.getItem() instanceof BlockItem) {
|
||||||
if (block.getDefaultState().isSimpleFullBlock(world, pos)) {
|
Block block = ((BlockItem)stack.getItem()).getBlock();
|
||||||
|
if (block.getDefaultState().getOutlineShape(world, pos) == VoxelShapes.fullCube() && !(block instanceof BlockEntityProvider)) {
|
||||||
SlopeTestEntity be = (SlopeTestEntity) world.getBlockEntity(pos);
|
SlopeTestEntity be = (SlopeTestEntity) world.getBlockEntity(pos);
|
||||||
if (be.getRenderedBlock() == Blocks.AIR) {
|
if (be.getRenderedState().getBlock() == Blocks.AIR) {
|
||||||
be.setRenderedBlock(block);
|
ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit));
|
||||||
if (!player.abilities.creativeMode) player.getStackInHand(hand).decrement(1);
|
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);
|
BlockEntity be = world.getBlockEntity(pos);
|
||||||
if (be instanceof SlopeTestEntity) {
|
if (be instanceof SlopeTestEntity) {
|
||||||
SlopeTestEntity slope = (SlopeTestEntity)be;
|
SlopeTestEntity slope = (SlopeTestEntity)be;
|
||||||
if (slope.getRenderedBlock() != Blocks.AIR) {
|
if (slope.getRenderedState().getBlock() != Blocks.AIR) {
|
||||||
ItemStack stack = new ItemStack(slope.getRenderedBlock());
|
ItemStack stack = new ItemStack(slope.getRenderedState().getBlock());
|
||||||
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
||||||
world.spawnEntity(entity);
|
world.spawnEntity(entity);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package io.github.cottonmc.slopetest.block.entity;
|
package io.github.cottonmc.slopetest.block.entity;
|
||||||
|
|
||||||
import io.github.cottonmc.slopetest.SlopeTest;
|
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.block.entity.BlockEntityClientSerializable;
|
||||||
import net.fabricmc.fabric.api.server.PlayerStream;
|
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.Blocks;
|
||||||
import net.minecraft.block.entity.BlockEntity;
|
import net.minecraft.block.entity.BlockEntity;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
@ -12,31 +13,31 @@ 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.AIR;
|
private BlockState renderedState = Blocks.AIR.getDefaultState();
|
||||||
|
|
||||||
public SlopeTestEntity() {
|
public SlopeTestEntity() {
|
||||||
super(SlopeTest.SLOPE_ENTITY);
|
super(SlopeTest.SLOPE_ENTITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Block getRenderedBlock() {
|
public BlockState getRenderedState() {
|
||||||
return renderedBlock;
|
return renderedState;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRenderedBlock(Block block) {
|
public void setRenderedState(BlockState state) {
|
||||||
this.renderedBlock = block;
|
this.renderedState = state;
|
||||||
markDirty();
|
markDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@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")));
|
renderedState = BlockStateUtil.fromTag(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompoundTag toTag(CompoundTag tag) {
|
public CompoundTag toTag(CompoundTag tag) {
|
||||||
super.toTag(tag);
|
super.toTag(tag);
|
||||||
tag.putString("Block", Registry.BLOCK.getId(renderedBlock).toString());
|
BlockStateUtil.toTag(tag, renderedState);
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package io.github.cottonmc.slopetest.block.entity.render;
|
package io.github.cottonmc.slopetest.block.entity.render;
|
||||||
|
|
||||||
import com.mojang.blaze3d.platform.GlStateManager;
|
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 io.github.cottonmc.slopetest.block.entity.SlopeTestEntity;
|
||||||
import net.fabricmc.fabric.api.client.render.ColorProviderRegistry;
|
import net.fabricmc.fabric.api.client.render.ColorProviderRegistry;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
@ -35,13 +35,13 @@ public class SlopeTestRenderer extends BlockEntityRenderer<SlopeTestEntity> {
|
|||||||
Direction dir = state.get(Properties.HORIZONTAL_FACING);
|
Direction dir = state.get(Properties.HORIZONTAL_FACING);
|
||||||
SpriteSet sprites;
|
SpriteSet sprites;
|
||||||
int color = 0xffffff;
|
int color = 0xffffff;
|
||||||
if (be.getRenderedBlock() != Blocks.AIR) {
|
if (be.getRenderedState().getBlock() != Blocks.AIR) {
|
||||||
BlockState blockDefaultState = be.getRenderedBlock().getDefaultState();
|
BlockState renderedState = be.getRenderedState();
|
||||||
BakedModel model = minecraft.getBlockRenderManager().getModel(blockDefaultState);
|
BakedModel model = minecraft.getBlockRenderManager().getModel(renderedState);
|
||||||
sprites = new SpriteSet(model);
|
sprites = new SpriteSet(model);
|
||||||
BlockColorProvider blockColor = ColorProviderRegistry.BLOCK.get(be.getRenderedBlock());
|
BlockColorProvider blockColor = ColorProviderRegistry.BLOCK.get(be.getRenderedState().getBlock());
|
||||||
if (blockColor != null) {
|
if (blockColor != null) {
|
||||||
color = blockColor.getColor(blockDefaultState, be.getWorld(), be.getPos(), 1);
|
color = blockColor.getColor(renderedState, be.getWorld(), be.getPos(), 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sprites = new SpriteSet(minecraft.getSpriteAtlas().getSprite("minecraft:block/scaffolding_top"), false);
|
sprites = new SpriteSet(minecraft.getSpriteAtlas().getSprite("minecraft:block/scaffolding_top"), false);
|
||||||
|
@ -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<Block, BlockState> 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 <T extends Comparable<T>> void parseProperty(BlockState state, Property<T> property, String value) {
|
||||||
|
System.out.println(value);
|
||||||
|
Optional<T> optional = property.getValue(value);
|
||||||
|
if (optional.isPresent()) {
|
||||||
|
System.out.println(optional.get());
|
||||||
|
state.with(property, optional.get());
|
||||||
|
System.out.println(state.get(property));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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.BakedModel;
|
||||||
import net.minecraft.client.render.model.BakedQuad;
|
import net.minecraft.client.render.model.BakedQuad;
|
||||||
import net.minecraft.client.texture.Sprite;
|
import net.minecraft.client.texture.Sprite;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -11,10 +13,11 @@ import java.util.Map;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class SpriteSet {
|
public class SpriteSet {
|
||||||
Sprite global;
|
private Sprite global;
|
||||||
Map<Direction, BakedQuad> quads = new HashMap<>();
|
private Map<Direction, BakedQuad> quads;
|
||||||
boolean singleSprite = false;
|
private boolean singleSprite = false;
|
||||||
boolean globalHasColor = 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) {
|
public SpriteSet(Sprite allSprite, boolean hasColor) {
|
||||||
this.global = allSprite;
|
this.global = allSprite;
|
||||||
@ -24,19 +27,22 @@ public class SpriteSet {
|
|||||||
|
|
||||||
public SpriteSet(BakedModel model) {
|
public SpriteSet(BakedModel model) {
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
|
quads = new HashMap<>();
|
||||||
for (Direction dir : Direction.values()) {
|
for (Direction dir : Direction.values()) {
|
||||||
List<BakedQuad> quads = model.getQuads(null, dir, rand);
|
List<BakedQuad> 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) {
|
public Sprite getSprite(Direction dir) {
|
||||||
if (singleSprite) return global;
|
if (singleSprite) return global;
|
||||||
|
if (quads.get(dir) == null) return FALLBACK;
|
||||||
else return quads.get(dir).getSprite();
|
else return quads.get(dir).getSprite();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasColor(Direction dir) {
|
public boolean hasColor(Direction dir) {
|
||||||
if (singleSprite) return globalHasColor;
|
if (singleSprite) return globalHasColor;
|
||||||
|
if (quads.get(dir) == null) return false;
|
||||||
else return quads.get(dir).hasColor();
|
else return quads.get(dir).hasColor();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user