Skip to content

Commit

Permalink
Make shadow drawers also work on floor and ceiling
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Dec 10, 2023
1 parent 44a425c commit 0044c81
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.github.mattidragon.extendeddrawers.client.renderer;

import io.github.mattidragon.extendeddrawers.ExtendedDrawers;
import io.github.mattidragon.extendeddrawers.block.ShadowDrawerBlock;
import io.github.mattidragon.extendeddrawers.block.base.StorageDrawerBlock;
import io.github.mattidragon.extendeddrawers.block.entity.ShadowDrawerBlockEntity;
import net.minecraft.block.enums.BlockFace;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.WorldRenderer;
Expand All @@ -29,11 +28,14 @@ public int getRenderDistance() {

@Override
public void render(ShadowDrawerBlockEntity drawer, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
var dir = drawer.getCachedState().get(ShadowDrawerBlock.FACING);
var horizontalDir = drawer.getCachedState().get(StorageDrawerBlock.FACING);
var face = drawer.getCachedState().get(StorageDrawerBlock.FACE);
var dir = StorageDrawerBlock.getFront(drawer.getCachedState());

if (!shouldRender(drawer, dir)) return;

matrices.push();
alignMatrices(matrices, dir, BlockFace.WALL);
alignMatrices(matrices, horizontalDir, face);

light = WorldRenderer.getLightmapCoordinates(Objects.requireNonNull(drawer.getWorld()), drawer.getPos().offset(dir));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
{
"variants": {
"facing=east": {
"face=ceiling,facing=east": {
"model": "extended_drawers:block/shadow_drawer",
"x": 90,
"y": 90
},
"facing=north": {
"model": "extended_drawers:block/shadow_drawer"
"face=ceiling,facing=north": {
"model": "extended_drawers:block/shadow_drawer",
"x": 90,
"y": 0
},
"face=ceiling,facing=south": {
"model": "extended_drawers:block/shadow_drawer",
"x": 90,
"y": 180
},
"face=ceiling,facing=west": {
"model": "extended_drawers:block/shadow_drawer",
"x": 90,
"y": 270
},
"face=floor,facing=east": {
"model": "extended_drawers:block/shadow_drawer",
"x": 270,
"y": 90
},
"face=floor,facing=north": {
"model": "extended_drawers:block/shadow_drawer",
"x": 270,
"y": 0
},
"face=floor,facing=south": {
"model": "extended_drawers:block/shadow_drawer",
"x": 270,
"y": 180
},
"face=floor,facing=west": {
"model": "extended_drawers:block/shadow_drawer",
"x": 270,
"y": 270
},
"face=wall,facing=east": {
"model": "extended_drawers:block/shadow_drawer",
"y": 90
},
"face=wall,facing=north": {
"model": "extended_drawers:block/shadow_drawer",
"y": 0
},
"facing=south": {
"face=wall,facing=south": {
"model": "extended_drawers:block/shadow_drawer",
"y": 180
},
"facing=west": {
"face=wall,facing=west": {
"model": "extended_drawers:block/shadow_drawer",
"y": 270
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.enums.BlockFace;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
Expand All @@ -37,16 +39,16 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import static io.github.mattidragon.extendeddrawers.block.base.StorageDrawerBlock.FACE;
import static io.github.mattidragon.extendeddrawers.misc.DrawerInteractionStatusManager.getAndResetInsertStatus;

@SuppressWarnings({"deprecation"}) // transfer api and mojank block method deprecation
public class ShadowDrawerBlock extends NetworkBlockWithEntity<ShadowDrawerBlockEntity> implements CreativeBreakBlocker, DrawerInteractionHandler {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
public static final EnumProperty<BlockFace> FACE = Properties.BLOCK_FACE;

public ShadowDrawerBlock(Settings settings) {
super(settings);
setDefaultState(stateManager.getDefaultState().with(FACING, Direction.NORTH));
setDefaultState(stateManager.getDefaultState().with(FACING, Direction.NORTH).with(FACE, BlockFace.WALL));
}

@Override
Expand All @@ -67,16 +69,25 @@ private static Storage<ItemVariant> createStorage(ServerWorld world, BlockPos po
protected BlockEntityType<ShadowDrawerBlockEntity> getType() {
return ModBlocks.SHADOW_DRAWER_BLOCK_ENTITY;
}


@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
builder.add(FACING, FACE);
}

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return this.getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing().getOpposite());
var face = switch (ctx.getPlayerLookDirection().getOpposite()) {
case DOWN -> BlockFace.CEILING;
case UP -> BlockFace.FLOOR;
default -> BlockFace.WALL;
};

return this.getDefaultState()
.with(FACE, face)
.with(FACING, ctx.getHorizontalPlayerFacing().getOpposite());
}

@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(FACING, rotation.rotate(state.get(FACING)));
Expand All @@ -89,7 +100,7 @@ public BlockState mirror(BlockState state, BlockMirror mirror) {

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (hit.getSide() != state.get(FACING) || !player.canModifyBlocks()) return ActionResult.PASS;
if (!isFront(state, hit.getSide()) || !player.canModifyBlocks()) return ActionResult.PASS;
if (!(world instanceof ServerWorld serverWorld)) return ActionResult.CONSUME_PARTIAL;

var drawer = getBlockEntity(world, pos);
Expand Down Expand Up @@ -153,7 +164,11 @@ public void onBlockBreakStart(BlockState state, World world, BlockPos pos, Playe

@Override
public boolean isFront(BlockState state, Direction direction) {
return state.get(FACING) == direction;
return switch (state.get(FACE)) {
case FLOOR -> direction == Direction.UP;
case CEILING -> direction == Direction.DOWN;
case WALL -> direction == state.get(FACING);
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.block.enums.BlockFace;
import net.minecraft.data.client.*;
import net.minecraft.state.property.Properties;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;

import java.util.Optional;
Expand All @@ -28,9 +29,7 @@ public void generateBlockStateModels(BlockStateModelGenerator generator) {
registerDrawerModel(ModBlocks.DOUBLE_DRAWER, generator);
registerDrawerModel(ModBlocks.QUAD_DRAWER, generator);

generator.registerNorthDefaultHorizontalRotatable(ModBlocks.SHADOW_DRAWER,
TextureMap.sideEnd(id("block/shadow_drawer_side"), id("block/shadow_drawer_side")));

generateShadowDrawerModel(generator);
generateCompactingDrawerModel(generator);
}

Expand All @@ -47,6 +46,15 @@ public void generateItemModels(ItemModelGenerator generator) {
generator.register(ModItems.DUPE_WAND, Models.GENERATED);
}

private static void generateShadowDrawerModel(BlockStateModelGenerator generator) {
Identifier identifier = Models.ORIENTABLE.upload(ModBlocks.SHADOW_DRAWER, TextureMap.sideEnd(id("block/shadow_drawer_side"), id("block/shadow_drawer_side")).copyAndAdd(TextureKey.FRONT, TextureMap.getId(ModBlocks.SHADOW_DRAWER)), generator.modelCollector);
generator.blockStateCollector.accept(
VariantsBlockStateSupplier.create(
ModBlocks.SHADOW_DRAWER,
BlockStateVariant.create().put(VariantSettings.MODEL, identifier))
.coordinate(getBlockStateMap()));
}

private void generateCompactingDrawerModel(BlockStateModelGenerator generator) {
generator.blockStateCollector.accept(
VariantsBlockStateSupplier.create(
Expand Down

0 comments on commit 0044c81

Please sign in to comment.