Skip to content

Commit

Permalink
Bottle racks can now be placed against walls
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Dec 8, 2024
1 parent 220222e commit 035e3b4
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 32 deletions.
74 changes: 49 additions & 25 deletions src/main/java/ivorius/psychedelicraft/block/BottleRackBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import ivorius.psychedelicraft.block.entity.PSBlockEntities;
import ivorius.psychedelicraft.client.render.blocks.VoxelShapeUtil;
import ivorius.psychedelicraft.mixin.MixinAbstractBlockSettings;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import ivorius.psychedelicraft.block.entity.BottleRackBlockEntity;
import net.minecraft.block.*;
Expand All @@ -23,7 +23,7 @@
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Direction.Axis;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.*;
Expand All @@ -32,28 +32,48 @@
* Created by lukas on 16.11.14.
*/
public class BottleRackBlock extends BlockWithEntity {
public static final MapCodec<BottleRackBlock> CODEC = createCodec(BottleRackBlock::new);
public static final MapCodec<BottleRackBlock> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Codec.INT.fieldOf("z_offset").forGetter(BottleRackBlock::getZOffset),
createSettingsCodec()
).apply(instance, BottleRackBlock::new));
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;

private static final Map<Direction, VoxelShape> SHAPES = Arrays.stream(Direction.values())
.filter(d -> d.getAxis() != Axis.Y)
.collect(Collectors.toMap(
Function.identity(),
VoxelShapeUtil.rotator(VoxelShapes.union(
Block.createCuboidShape(0, 0, 3, 1, 16, 11),
Block.createCuboidShape(5, 0, 3, 6, 16, 11),
Block.createCuboidShape(10, 0, 3, 11, 16, 11),
Block.createCuboidShape(15, 0, 3, 16, 16, 11),

Block.createCuboidShape(1, 0, 2.75F, 15, 1, 12.35F),
Block.createCuboidShape(1, 5, 2.75F, 15, 6, 12.35F),
Block.createCuboidShape(1, 10, 2.75F, 15, 11, 12.35F),
Block.createCuboidShape(1, 15, 2.75F, 15, 16, 12.35F)
)))
);

public BottleRackBlock(Settings settings) {
super(settings.nonOpaque());
private static final VoxelShape BASE_SHAPE = VoxelShapes.union(
Block.createCuboidShape(0, 0, 3, 1, 16, 11),
Block.createCuboidShape(5, 0, 3, 6, 16, 11),
Block.createCuboidShape(10, 0, 3, 11, 16, 11),
Block.createCuboidShape(15, 0, 3, 16, 16, 11),

Block.createCuboidShape(1, 0, 2.75F, 15, 1, 12.35F),
Block.createCuboidShape(1, 5, 2.75F, 15, 6, 12.35F),
Block.createCuboidShape(1, 10, 2.75F, 15, 11, 12.35F),
Block.createCuboidShape(1, 15, 2.75F, 15, 16, 12.35F)
);

private final int zOffset;
private final Function<Direction, VoxelShape> shapes;

public BottleRackBlock(int zOffset, Settings settings) {
this(zOffset, settings,
Util.memoize(direction -> VoxelShapeUtil.rotate(new Vec3d(0, 0, -zOffset / 16D), direction)),
Util.memoize(VoxelShapeUtil.rotator(BASE_SHAPE.offset(0, 0, zOffset / 16D)))
);
}

public BottleRackBlock(int zOffset, Settings settings,
Function<Direction, Vec3d> offsets,
Function<Direction, VoxelShape> shapes) {
super(Util.make(settings, s -> {
if (zOffset != 0) {
((MixinAbstractBlockSettings)s).setOffsetter((state, world, pos) -> {
offsets.apply(state.get(FACING));
return VoxelShapeUtil.rotate(new Vec3d(0, 0, -3 / 16D), state.get(FACING));
});
}
}).nonOpaque().dynamicBounds());
this.shapes = shapes;
this.zOffset = zOffset;
}

@Override
Expand All @@ -66,6 +86,10 @@ protected BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
}

public int getZOffset() {
return zOffset;
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
Expand All @@ -80,7 +104,7 @@ public BlockState getPlacementState(ItemPlacementContext ctx) {
@Override
@Deprecated
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return SHAPES.getOrDefault(state.get(FACING), VoxelShapes.fullCube());
return shapes.apply(state.get(FACING));
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ivorius/psychedelicraft/block/PSBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public interface PSBlocks {

Block FLASK = register("flask", new FlaskBlock(Settings.create().sounds(BlockSoundGroup.COPPER).hardness(1).pistonBehavior(PistonBehavior.BLOCK)));
Block DISTILLERY = register("distillery", new DistilleryBlock(Settings.create().sounds(BlockSoundGroup.COPPER).hardness(1).pistonBehavior(PistonBehavior.BLOCK)));
Block BOTTLE_RACK = register("bottle_rack", new BottleRackBlock(Settings.create().mapColor(MapColor.OAK_TAN).sounds(BlockSoundGroup.WOOD).hardness(0.5F).burnable()));
Block BOTTLE_RACK = register("bottle_rack", new BottleRackBlock(0, Settings.create().mapColor(MapColor.OAK_TAN).sounds(BlockSoundGroup.WOOD).hardness(0.5F).burnable()));
Block WALL_BOTTLE_RACK = register("wall_bottle_rack", new BottleRackBlock(-3, Settings.create().mapColor(MapColor.OAK_TAN).sounds(BlockSoundGroup.WOOD).hardness(0.5F).burnable()));

Block DRYING_TABLE = register("drying_table", new DryingTableBlock(Settings.create().mapColor(MapColor.OAK_TAN).solid().sounds(BlockSoundGroup.WOOD).hardness(2).burnable()));
Block IRON_DRYING_TABLE = register("iron_drying_table", new DryingTableBlock(Settings.create().mapColor(MapColor.IRON_GRAY).sounds(BlockSoundGroup.METAL).hardness(5)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface PSBlockEntities {
BlockEntityType<RiftJarBlockEntity> RIFT_JAR = create("rift_jar", BlockEntityType.Builder.create(RiftJarBlockEntity::new, PSBlocks.RIFT_JAR));
BlockEntityType<PeyoteBlockEntity> PEYOTE = create("peyote", BlockEntityType.Builder.create(PeyoteBlockEntity::new, PSBlocks.PEYOTE));
BlockEntityType<DistilleryBlockEntity> DISTILLERY = create("distillery", BlockEntityType.Builder.create(DistilleryBlockEntity::new, PSBlocks.DISTILLERY));
BlockEntityType<BottleRackBlockEntity> BOTTLE_RACK = create("bottle_rack", BlockEntityType.Builder.create(BottleRackBlockEntity::new, PSBlocks.BOTTLE_RACK));
BlockEntityType<BottleRackBlockEntity> BOTTLE_RACK = create("bottle_rack", BlockEntityType.Builder.create(BottleRackBlockEntity::new, PSBlocks.BOTTLE_RACK, PSBlocks.WALL_BOTTLE_RACK));
BlockEntityType<FlaskBlockEntity> FLASK = create("flask", BlockEntityType.Builder.create(FlaskBlockEntity::new, PSBlocks.FLASK));
BlockEntityType<BarrelBlockEntity> BARREL = create("barrel", BlockEntityType.Builder.create(BarrelBlockEntity::new,
PSBlocks.OAK_BARREL, PSBlocks.SPRUCE_BARREL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public void render(BottleRackBlockEntity entity, float tickDelta, MatrixStack ma
float facing = direction.asRotation() + 90;

matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(facing));
matrices.translate(0.14F, -0.55F, -0.8F);

double offset = ((BottleRackBlock)entity.getCachedState().getBlock()).getZOffset() / 16D;

matrices.translate(0.14F - offset, -0.55F, -0.8F);
matrices.multiply(RotationAxis.NEGATIVE_Z.rotationDegrees(-90));

Random rng = RenderUtil.random(entity.getPos().asLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ static VoxelShape rotate(VoxelShape shape, Direction direction) {
.toArray(VoxelShape[]::new));
}

static Vec3d rotate(Vec3d vector, Direction direction) {
if (direction.asRotation() == 0) {
return vector;
}
if (direction.getAxis() == Axis.X) {
direction = direction.getOpposite();
}
float angle = (direction.asRotation()) * MathHelper.RADIANS_PER_DEGREE;
return vector.rotateY(angle);
}

static Vec3d rotate(double x, double z, float angle) {
return new Vec3d(x, 0, z).subtract(CENTER).rotateY(angle).add(CENTER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ public void generateBlockStateModels(BlockStateModelGenerator generator) {
BlockModels.registerDryingTable(generator, PSBlocks.DRYING_TABLE);
BlockModels.registerDryingTable(generator, PSBlocks.IRON_DRYING_TABLE);

generator.blockStateCollector.accept(VariantsBlockStateSupplier.create(PSBlocks.BOTTLE_RACK, BlockStateVariant.create()
.put(VariantSettings.MODEL, ModelIds.getBlockModelId(PSBlocks.BOTTLE_RACK)))
.coordinate(BlockStateModelGenerator.createNorthDefaultHorizontalRotationStates()));
List.of(PSBlocks.BOTTLE_RACK, PSBlocks.WALL_BOTTLE_RACK).forEach(block -> {
generator.blockStateCollector.accept(VariantsBlockStateSupplier.create(block, BlockStateVariant.create()
.put(VariantSettings.MODEL, ModelIds.getBlockModelId(PSBlocks.BOTTLE_RACK)))
.coordinate(BlockStateModelGenerator.createNorthDefaultHorizontalRotationStates()));
});
generator.blockStateCollector.accept(BlockStateModelGenerator.createSingletonBlockState(PSBlocks.FLASK, ModelIds.getBlockModelId(PSBlocks.FLASK)));
generator.registerStateWithModelReference(PSBlocks.FLAMMABLE_GAS, Blocks.AIR);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void generate() {
PSBlocks.TRAY,
PSBlocks.BOTTLE_RACK
).forEach(this::addDrop);
addDrop(PSBlocks.WALL_BOTTLE_RACK, PSItems.BOTTLE_RACK);

addDrop(PSBlocks.JUNIPER_LEAVES, block -> fruitLeavesDrop(block, PSBlocks.JUNIPER_SAPLING, PSItems.JUNIPER_BERRIES, SAPLING_DROP_CHANCE));
addDrop(PSBlocks.FRUITING_JUNIPER_LEAVES, block -> matureFruitLeavesDrop(block, PSBlocks.JUNIPER_SAPLING, PSItems.JUNIPER_BERRIES, SAPLING_DROP_CHANCE));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ivorius/psychedelicraft/item/PSItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Colors;
import net.minecraft.util.math.Direction;

/**
* Created by lukas on 25.04.14.
Expand Down Expand Up @@ -206,7 +207,7 @@ public interface PSItems {
Item LATTICE = register("lattice", PSBlocks.LATTICE);
Item WINE_GRAPE_LATTICE = register("wine_grape_lattice", PSBlocks.WINE_GRAPE_LATTICE);
Item MORNING_GLORY_LATTICE = register("morning_glory_lattice", PSBlocks.MORNING_GLORY_LATTICE);
Item BOTTLE_RACK = register("bottle_rack", PSBlocks.BOTTLE_RACK);
Item BOTTLE_RACK = register("bottle_rack", new VerticallyAttachableBlockItem(PSBlocks.BOTTLE_RACK, PSBlocks.WALL_BOTTLE_RACK, new Settings(), Direction.DOWN));
Item DRYING_TABLE = register("drying_table", PSBlocks.DRYING_TABLE);
Item IRON_DRYING_TABLE = register("iron_drying_table", PSBlocks.IRON_DRYING_TABLE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ivorius.psychedelicraft.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.block.AbstractBlock;

@Mixin(AbstractBlock.Settings.class)
public interface MixinAbstractBlockSettings {
@Accessor
void setOffsetter(AbstractBlock.Offsetter offsetter);
}
1 change: 1 addition & 0 deletions src/main/resources/psychedelicraft.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"refmap": "psychedelicraft.mixin.refmap.json",
"compatibilityLevel": "JAVA_16",
"mixins": [
"MixinAbstractBlockSettings",
"MixinAbstractFurnaceBlockEntity",
"MixinEntity",
"MixinLivingEntity",
Expand Down

0 comments on commit 035e3b4

Please sign in to comment.