-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(DryingRack): renderer not rotating item based on direction
- Loading branch information
Showing
2 changed files
with
210 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,145 @@ | ||
package blocks; | ||
|
||
import blockentity.DryingRackBlockEntity; | ||
import init.BlockEntityInit; | ||
import init.ItemInit; | ||
|
||
import net.minecraft.core.BlockPos; | ||
|
||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
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.block.Block; | ||
import net.minecraft.world.level.block.EntityBlock; | ||
import net.minecraft.world.level.block.HorizontalDirectionalBlock; | ||
import net.minecraft.world.level.block.Mirror; | ||
import net.minecraft.world.level.block.Rotation; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
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.DirectionProperty; | ||
import net.minecraft.world.level.gameevent.GameEvent; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
|
||
public class DryingRack extends HorizontalDirectionalBlock implements EntityBlock { | ||
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; | ||
private static VoxelShape SHAPE = Block.box(0, 10, 14, 16, 12, 16); | ||
|
||
public DryingRack(Properties props) { | ||
super(props); | ||
|
||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, | ||
InteractionHand interactionHand, BlockHitResult hitResult) { | ||
|
||
BlockEntity dryingRack = level.getBlockEntity(pos); | ||
DryingRackBlockEntity dryingRackEntity = (DryingRackBlockEntity) dryingRack; | ||
|
||
if (!level.isClientSide() && interactionHand == InteractionHand.MAIN_HAND) { | ||
|
||
if (dryingRack instanceof DryingRackBlockEntity) { | ||
|
||
if (dryingRackEntity.inventory.getStackInSlot(0).isEmpty()) { | ||
if (player.getItemInHand(interactionHand).getItem().asItem() == ItemInit.BUD.get()) { | ||
dryingRackEntity.inventory.insertItem(0, new ItemStack(ItemInit.BUD.get()), false); | ||
dryingRackEntity.setActive(); | ||
dryingRackEntity.updateEntity(); | ||
|
||
} | ||
|
||
} else { | ||
player.drop(dryingRackEntity.inventory.getStackInSlot(0), false); | ||
dryingRackEntity.inventory.extractItem(0, 1, false); | ||
if (dryingRackEntity.getActive()) { | ||
dryingRackEntity.setActive(); | ||
} | ||
dryingRackEntity.updateEntity(); | ||
} | ||
|
||
} | ||
|
||
return InteractionResult.SUCCESS; | ||
} | ||
return super.use(state, level, pos, player, interactionHand, hitResult); | ||
} | ||
|
||
@Override | ||
public BlockState getStateForPlacement(BlockPlaceContext pContext) { | ||
// TODO Auto-generated method stub | ||
return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite()); | ||
} | ||
|
||
@Override | ||
public BlockState rotate(BlockState state, Rotation rotation) { | ||
// | ||
return state.setValue(FACING, rotation.rotate(state.getValue(FACING))); | ||
} | ||
|
||
@Override | ||
public BlockState mirror(BlockState state, Mirror mirror) { | ||
|
||
return state.rotate(mirror.getRotation(state.getValue(FACING))); | ||
} | ||
|
||
@Override | ||
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { | ||
|
||
return new DryingRackBlockEntity(pos, state); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { | ||
builder.add(FACING); | ||
|
||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState state, BlockGetter getter, BlockPos pos, CollisionContext collisionContext) { | ||
|
||
switch (state.getValue(FACING)) { | ||
|
||
case EAST: | ||
SHAPE = Block.box(0, 11, 0, 2, 15, 16); | ||
|
||
break; | ||
case NORTH: | ||
SHAPE = Block.box(0, 11, 14, 16, 15, 16); | ||
|
||
break; | ||
case SOUTH: | ||
SHAPE = Block.box(0, 11, 0, 16, 15, 2); | ||
break; | ||
|
||
case WEST: | ||
SHAPE = Block.box(14, 11, 0, 16, 15, 16); | ||
break; | ||
default: | ||
SHAPE = Block.box(0, 10, 14, 16, 12, 16); | ||
break; | ||
|
||
} | ||
|
||
return SHAPE; | ||
} | ||
|
||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, | ||
BlockEntityType<T> type) { | ||
|
||
return type == BlockEntityInit.DRYING_RACK.get() ? DryingRackBlockEntity::tick : null; | ||
} | ||
|
||
} | ||
package blocks; | ||
|
||
import blockentity.DryingRackBlockEntity; | ||
import init.BlockEntityInit; | ||
import init.ItemInit; | ||
|
||
import net.minecraft.core.BlockPos; | ||
|
||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
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.block.Block; | ||
import net.minecraft.world.level.block.EntityBlock; | ||
import net.minecraft.world.level.block.HorizontalDirectionalBlock; | ||
import net.minecraft.world.level.block.Mirror; | ||
import net.minecraft.world.level.block.Rotation; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
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.DirectionProperty; | ||
import net.minecraft.world.level.gameevent.GameEvent; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
|
||
public class DryingRack extends HorizontalDirectionalBlock implements EntityBlock { | ||
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; | ||
|
||
private static VoxelShape SHAPE = Block.box(0, 10, 14, 16, 12, 16); | ||
|
||
public DryingRack(Properties props) { | ||
super(props); | ||
|
||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, | ||
InteractionHand interactionHand, BlockHitResult hitResult) { | ||
|
||
BlockEntity dryingRack = level.getBlockEntity(pos); | ||
DryingRackBlockEntity dryingRackEntity = (DryingRackBlockEntity) dryingRack; | ||
|
||
if (!level.isClientSide() && interactionHand == InteractionHand.MAIN_HAND) { | ||
|
||
if (dryingRack instanceof DryingRackBlockEntity) { | ||
|
||
if (dryingRackEntity.inventory.getStackInSlot(0).isEmpty()) { | ||
if (player.getItemInHand(interactionHand).getItem().asItem() == ItemInit.BUD.get()) { | ||
dryingRackEntity.inventory.insertItem(0, new ItemStack(ItemInit.BUD.get()), false); | ||
dryingRackEntity.setActive(); | ||
dryingRackEntity.updateEntity(); | ||
|
||
} | ||
|
||
} else { | ||
player.drop(dryingRackEntity.inventory.getStackInSlot(0), false); | ||
dryingRackEntity.inventory.extractItem(0, 1, false); | ||
if (dryingRackEntity.getActive()) { | ||
dryingRackEntity.setActive(); | ||
} | ||
dryingRackEntity.updateEntity(); | ||
} | ||
|
||
} | ||
|
||
return InteractionResult.SUCCESS; | ||
} | ||
return super.use(state, level, pos, player, interactionHand, hitResult); | ||
} | ||
|
||
@Override | ||
public BlockState getStateForPlacement(BlockPlaceContext pContext) { | ||
// TODO Auto-generated method stub | ||
return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite()); | ||
} | ||
|
||
@Override | ||
public BlockState rotate(BlockState state, Rotation rotation) { | ||
// | ||
return state.setValue(FACING, rotation.rotate(state.getValue(FACING))); | ||
} | ||
|
||
@Override | ||
public BlockState mirror(BlockState state, Mirror mirror) { | ||
|
||
return state.rotate(mirror.getRotation(state.getValue(FACING))); | ||
} | ||
|
||
@Override | ||
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { | ||
|
||
return new DryingRackBlockEntity(pos, state); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { | ||
builder.add(FACING); | ||
|
||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState state, BlockGetter getter, BlockPos pos, CollisionContext collisionContext) { | ||
|
||
switch (state.getValue(FACING)) { | ||
|
||
case EAST: | ||
SHAPE = Block.box(0, 11, 0, 2, 15, 16); | ||
|
||
break; | ||
case NORTH: | ||
SHAPE = Block.box(0, 11, 14, 16, 15, 16); | ||
|
||
break; | ||
case SOUTH: | ||
SHAPE = Block.box(0, 11, 0, 16, 15, 2); | ||
break; | ||
|
||
case WEST: | ||
SHAPE = Block.box(14, 11, 0, 16, 15, 16); | ||
break; | ||
default: | ||
SHAPE = Block.box(0, 10, 14, 16, 12, 16); | ||
break; | ||
|
||
} | ||
|
||
return SHAPE; | ||
} | ||
|
||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, | ||
BlockEntityType<T> type) { | ||
|
||
return type == BlockEntityInit.DRYING_RACK.get() ? DryingRackBlockEntity::tick : null; | ||
} | ||
|
||
} |
105 changes: 65 additions & 40 deletions
105
src/main/java/renderer/DryingRackBlockEntityRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,65 @@ | ||
package renderer; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
|
||
import blockentity.DryingRackBlockEntity; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.block.model.ItemTransforms; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; | ||
import net.minecraft.client.renderer.entity.ItemRenderer; | ||
|
||
public class DryingRackBlockEntityRenderer implements BlockEntityRenderer<DryingRackBlockEntity> { | ||
|
||
public DryingRackBlockEntityRenderer(BlockEntityRendererProvider.Context context) { | ||
super(); | ||
|
||
} | ||
|
||
@Override | ||
public void render(DryingRackBlockEntity BE, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, | ||
int packedLight, int packedOverlay) { | ||
ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer(); | ||
int i = (int) BE.getBlockPos().asLong(); | ||
|
||
if (!BE.inventory.getStackInSlot(0).isEmpty()) { | ||
poseStack.pushPose(); | ||
|
||
poseStack.translate(0.5f, 0.45f, 0.9f); | ||
|
||
itemRenderer.renderStatic(BE.inventory.getStackInSlot(0), ItemTransforms.TransformType.FIXED, packedLight, | ||
packedOverlay, poseStack, bufferSource, i); | ||
|
||
poseStack.popPose(); | ||
} | ||
|
||
} | ||
|
||
} | ||
package renderer; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.math.Vector3f; | ||
|
||
import blockentity.DryingRackBlockEntity; | ||
import blocks.DryingRack; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.block.model.ItemTransforms; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; | ||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; | ||
import net.minecraft.client.renderer.entity.ItemRenderer; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.state.properties.DirectionProperty; | ||
|
||
public class DryingRackBlockEntityRenderer implements BlockEntityRenderer<DryingRackBlockEntity> { | ||
|
||
public DryingRackBlockEntityRenderer(BlockEntityRendererProvider.Context context) { | ||
super(); | ||
|
||
} | ||
|
||
@Override | ||
public void render(DryingRackBlockEntity BE, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, | ||
int packedLight, int packedOverlay) { | ||
ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer(); | ||
int i = (int) BE.getBlockPos().asLong(); | ||
|
||
if (BE.inventory.getStackInSlot(0).isEmpty()) { | ||
return; | ||
} | ||
|
||
poseStack.pushPose(); | ||
|
||
switch (BE.getBlockState().getValue(DryingRack.FACING)) { | ||
case EAST: | ||
poseStack.mulPose(Vector3f.YP.rotationDegrees(90)); | ||
poseStack.translate(-0.5f, 0.45f, 0.1f); | ||
|
||
break; | ||
|
||
case NORTH: | ||
poseStack.translate(0.5f, 0.48f, 0.9f); | ||
break; | ||
case SOUTH: | ||
poseStack.mulPose(Vector3f.YP.rotationDegrees(-180)); | ||
poseStack.translate(-0.5f, 0.46f, -0.1f); | ||
|
||
break; | ||
|
||
case WEST: | ||
poseStack.mulPose(Vector3f.YP.rotationDegrees(-90)); | ||
poseStack.translate(0.5f, 0.44f, -0.9f); | ||
|
||
break; | ||
|
||
} | ||
|
||
itemRenderer.renderStatic(BE.inventory.getStackInSlot(0), ItemTransforms.TransformType.FIXED, packedLight, | ||
packedOverlay, poseStack, bufferSource, i); | ||
|
||
poseStack.popPose(); | ||
} | ||
} |