Skip to content

Commit

Permalink
Merge pull request #16 from slmnio/1.20.1
Browse files Browse the repository at this point in the history
Add half panel display
  • Loading branch information
MisterJulsen authored May 21, 2024
2 parents 8d6a9b9 + cec5d56 commit 89c53c9
Show file tree
Hide file tree
Showing 32 changed files with 1,685 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package de.mrjulsen.crn.block;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import de.mrjulsen.crn.data.EBlockAlignment;
import de.mrjulsen.mcdragonlib.data.Pair;
import de.mrjulsen.mcdragonlib.data.Tripple;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis;
import net.minecraft.core.Direction.AxisDirection;
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.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class AdvancedDisplayHalfPanelBlock extends AbstractAdvancedSidedDisplayBlock {

public static final EnumProperty<EBlockAlignment> Y_ALIGN = EnumProperty.create("y_alignment", EBlockAlignment.class);
public static final EnumProperty<EBlockAlignment> Z_ALIGN = EnumProperty.create("z_alignment", EBlockAlignment.class);

private static final Map<ShapeKey, VoxelShape> SHAPES = Map.ofEntries(
Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.NEGATIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 0 , 13, 16, 8 , 16)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.NEGATIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 0 , 0 , 16, 8 , 3 )),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.NEGATIVE, EBlockAlignment.NEGATIVE), Block.box(13, 0 , 0 , 16, 8 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.NEGATIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 0 , 0 , 3 , 8 , 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.CENTER, EBlockAlignment.NEGATIVE), Block.box(0 , 4 , 13, 16, 12, 16)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.CENTER, EBlockAlignment.NEGATIVE), Block.box(0 , 4 , 0 , 16, 8 , 3 )),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.CENTER, EBlockAlignment.NEGATIVE), Block.box(13, 4 , 0 , 16, 12, 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.CENTER, EBlockAlignment.NEGATIVE), Block.box(0 , 4 , 0 , 3 , 12, 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.POSITIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 8 , 13, 16, 16, 16)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.POSITIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 8 , 0 , 16, 16, 3 )),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.POSITIVE, EBlockAlignment.NEGATIVE), Block.box(13, 8 , 0 , 16, 16, 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.POSITIVE, EBlockAlignment.NEGATIVE), Block.box(0 , 8 , 0 , 3 , 16, 16)),



Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.NEGATIVE, EBlockAlignment.CENTER), Block.box(0 , 0 , 6.5, 16, 8, 9.5)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.NEGATIVE, EBlockAlignment.CENTER), Block.box(0 , 0 , 6.5, 16, 8, 9.5)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.NEGATIVE, EBlockAlignment.CENTER), Block.box(6.5, 0 , 0 , 9.5, 8 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.NEGATIVE, EBlockAlignment.CENTER), Block.box(6.5, 0 , 0 , 9.5, 8 , 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.CENTER, EBlockAlignment.CENTER), Block.box(0 , 4 , 6.5, 16, 12, 9.5)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.CENTER, EBlockAlignment.CENTER), Block.box(0 , 4 , 6.5, 16, 12, 9.5)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.CENTER, EBlockAlignment.CENTER), Block.box(6.5, 4 , 0 , 9.5, 12 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.CENTER, EBlockAlignment.CENTER), Block.box(6.5, 4 , 0 , 9.5, 12 , 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.POSITIVE, EBlockAlignment.CENTER), Block.box(0 , 8 , 6.5, 16, 16, 9.5)),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.POSITIVE, EBlockAlignment.CENTER), Block.box(0 , 8 , 6.5, 16, 16, 9.5)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.POSITIVE, EBlockAlignment.CENTER), Block.box(6.5, 8 , 0 , 9.5, 16 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.POSITIVE, EBlockAlignment.CENTER), Block.box(6.5, 8 , 0 , 9.5, 16 , 16)),



Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.NEGATIVE, EBlockAlignment.POSITIVE), Block.box(0 , 0 , 0 , 16, 8 , 3 )),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.NEGATIVE, EBlockAlignment.POSITIVE), Block.box(0 , 0 , 13, 16, 8 , 16)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.NEGATIVE, EBlockAlignment.POSITIVE), Block.box(0 , 0 , 0 , 3 , 8 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.NEGATIVE, EBlockAlignment.POSITIVE), Block.box(13, 0 , 0 , 16, 8 , 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.CENTER, EBlockAlignment.POSITIVE), Block.box(0 , 4 , 0 , 16, 12 , 3 )),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.CENTER, EBlockAlignment.POSITIVE), Block.box(0 , 4 , 13, 16, 12 , 16)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.CENTER, EBlockAlignment.POSITIVE), Block.box(0 , 4 , 0 , 3 , 12 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.CENTER, EBlockAlignment.POSITIVE), Block.box(13, 4 , 0 , 16, 12 , 16)),

Map.entry(new ShapeKey(Direction.SOUTH, EBlockAlignment.POSITIVE, EBlockAlignment.POSITIVE), Block.box(0 , 8 , 0 , 16, 16 , 3 )),
Map.entry(new ShapeKey(Direction.NORTH, EBlockAlignment.POSITIVE, EBlockAlignment.POSITIVE), Block.box(0 , 8 , 13, 16, 16 , 16)),
Map.entry(new ShapeKey(Direction.EAST, EBlockAlignment.POSITIVE, EBlockAlignment.POSITIVE), Block.box(0 , 8 , 0 , 3 , 16 , 16)),
Map.entry(new ShapeKey(Direction.WEST, EBlockAlignment.POSITIVE, EBlockAlignment.POSITIVE), Block.box(13, 8 , 0 , 16, 16 , 16))
);

public AdvancedDisplayHalfPanelBlock(Properties properties) {
super(properties);
registerDefaultState(defaultBlockState()
.setValue(Y_ALIGN, EBlockAlignment.CENTER)
.setValue(Z_ALIGN, EBlockAlignment.CENTER)
);
}

@Override
public Collection<Property<?>> getExcludedProperties() {
return List.of(Y_ALIGN, Z_ALIGN);
}

@Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return SHAPES.get(new ShapeKey(pState.getValue(FACING), pState.getValue(Y_ALIGN), pState.getValue(Z_ALIGN)));
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
BlockState stateForPlacement = super.getStateForPlacement(pContext);
Direction direction = pContext.getClickedFace();
Direction looking = pContext.getHorizontalDirection();
Axis axis = looking.getAxis();
AxisDirection axisDirection = looking.getAxisDirection();

double xzPos = 0.5f;
if (axis == Axis.X) {
xzPos = pContext.getClickLocation().x - pContext.getClickedPos().getX();
} else if (axis == Axis.Z) {
xzPos = pContext.getClickLocation().z - pContext.getClickedPos().getZ();
}

EBlockAlignment yAlign = EBlockAlignment.CENTER;
EBlockAlignment zAlign = EBlockAlignment.CENTER;

if (direction == Direction.UP || (pContext.getClickLocation().y - pContext.getClickedPos().getY() < 0.33333333D)) {
yAlign = EBlockAlignment.NEGATIVE;
} else if (direction == Direction.DOWN || (pContext.getClickLocation().y - pContext.getClickedPos().getY() > 0.66666666D)) {
yAlign = EBlockAlignment.POSITIVE;
}

if (direction == pContext.getPlayer().getDirection().getOpposite() || (axisDirection == AxisDirection.POSITIVE ? xzPos > 0.66666666D : xzPos < 0.33333333D)) {
zAlign = EBlockAlignment.POSITIVE;
} else if (direction == pContext.getPlayer().getDirection() || (axisDirection == AxisDirection.POSITIVE ? xzPos < 0.33333333D : xzPos > 0.66666666D)) {
zAlign = EBlockAlignment.NEGATIVE;
}

return stateForPlacement
.setValue(Y_ALIGN, yAlign)
.setValue(Z_ALIGN, zAlign);
}

@Override
public boolean canConnectWithBlock(BlockGetter level, BlockPos selfPos, BlockPos otherPos) {
return super.canConnectWithBlock(level, selfPos, otherPos) &&
level.getBlockState(selfPos).getValue(Y_ALIGN) == level.getBlockState(otherPos).getValue(Y_ALIGN) &&
level.getBlockState(selfPos).getValue(Z_ALIGN) == level.getBlockState(otherPos).getValue(Z_ALIGN);
}

@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
super.createBlockStateDefinition(pBuilder.add(Y_ALIGN, Z_ALIGN));
}

@Override
public Pair<Float, Float> getRenderAspectRatio(Level level, BlockState blockState, BlockPos pos) {
return Pair.of(1.0F, 0.5F);
}

@Override
public Pair<Float, Float> getRenderOffset(Level level, BlockState blockState, BlockPos pos) {
float y;
switch (blockState.getValue(Y_ALIGN)) {
case NEGATIVE:
y = 8.0f;
break;
case POSITIVE:
y = 0.0f;
break;
default:
y = 4.0f;
break;
}
return Pair.of(0.0f, y);
}

@Override
public Pair<Float, Float> getRenderZOffset(Level level, BlockState blockState, BlockPos pos) {
float z1;
float z2;
switch (blockState.getValue(Z_ALIGN)) {
case NEGATIVE:
z1 = 16.05f;
z2 = 3.05f;
break;
case POSITIVE:
z1 = 3.05f;
z2 = 16.05f;
break;
default:
z1 = 9.55f;
z2 = 9.55f;
break;
}
return Pair.of(z1, z2);
}

@Override
public Tripple<Float, Float, Float> getRenderRotation(Level level, BlockState blockState, BlockPos pos) {
return Tripple.of(0.0F, 0.0F, 0.0F);
}

@Override
public boolean isSingleLined() {
return false;
}

private static final class ShapeKey {
private final Direction facing;
private final EBlockAlignment yAlign;
private final EBlockAlignment zAlign;

public ShapeKey(Direction facing, EBlockAlignment yAlign, EBlockAlignment zAlign) {
this.facing = facing;
this.yAlign = yAlign;
this.zAlign = zAlign;
}

@Override
public boolean equals(Object o) {
if (o instanceof ShapeKey other) {
return facing == other.facing && yAlign == other.yAlign && zAlign == other.zAlign;
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(facing, yAlign, zAlign);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ModBlockEntities {
ModBlocks.ADVANCED_DISPLAY,
ModBlocks.ADVANCED_DISPLAY_BLOCK,
ModBlocks.ADVANCED_DISPLAY_PANEL,
ModBlocks.ADVANCED_DISPLAY_HALF_PANEL,
ModBlocks.ADVANCED_DISPLAY_SMALL,
ModBlocks.ADVANCED_DISPLAY_SLOPED
)
Expand Down
18 changes: 12 additions & 6 deletions common/src/main/java/de/mrjulsen/crn/registry/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import com.tterrag.registrate.util.nullness.NonNullConsumer;

import de.mrjulsen.crn.CreateRailwaysNavigator;
import de.mrjulsen.crn.block.AdvancedDisplayBlock;
import de.mrjulsen.crn.block.AdvancedDisplayBoardBlock;
import de.mrjulsen.crn.block.AdvancedDisplayPanelBlock;
import de.mrjulsen.crn.block.AdvancedDisplaySlopedBlock;
import de.mrjulsen.crn.block.AdvancedDisplaySmallBlock;
import de.mrjulsen.crn.block.TrainStationClockBlock;
import de.mrjulsen.crn.block.*;
import de.mrjulsen.crn.block.connected.AdvancedDisplayCTBehaviour;
import de.mrjulsen.crn.block.connected.AdvancedDisplaySmallCTBehaviour;
import de.mrjulsen.crn.block.display.AdvancedDisplayTarget;
Expand Down Expand Up @@ -74,6 +69,17 @@ public class ModBlocks {
.build()
.register();

public static final BlockEntry<AdvancedDisplayHalfPanelBlock> ADVANCED_DISPLAY_HALF_PANEL = CreateRailwaysNavigator.REGISTRATE.block("advanced_display_half_panel", AdvancedDisplayHalfPanelBlock::new)
.onRegister(connectedTextures(() -> new AdvancedDisplaySmallCTBehaviour(ClientWrapper.CT_HORIZONTAL_ADVANCED_DISPLAY_SMALL, ClientWrapper.CT_ADVANCED_DISPLAY_SMALL)))
.addLayer(() -> RenderType::cutout)
.initialProperties(SharedProperties::softMetal)
.transform(TagGen.pickaxeOnly())
.onRegister(AllDisplayBehaviours.assignDataBehaviour(new AdvancedDisplayTarget()))
.item()
.tab(ModCreativeModeTab.MAIN_TAB.getKey())
.build()
.register();

public static final BlockEntry<AdvancedDisplaySlopedBlock> ADVANCED_DISPLAY_SLOPED = CreateRailwaysNavigator.REGISTRATE.block("advanced_display_sloped", AdvancedDisplaySlopedBlock::new)
.onRegister(connectedTextures(() -> new AdvancedDisplaySmallCTBehaviour(ClientWrapper.CT_HORIZONTAL_ADVANCED_DISPLAY_SMALL, ClientWrapper.CT_ADVANCED_DISPLAY_SMALL)))
.addLayer(() -> RenderType::cutout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void register() {
.add(ModBlocks.ADVANCED_DISPLAY)
.add(ModBlocks.ADVANCED_DISPLAY_BLOCK)
.add(ModBlocks.ADVANCED_DISPLAY_PANEL)
.add(ModBlocks.ADVANCED_DISPLAY_HALF_PANEL)
.add(ModBlocks.ADVANCED_DISPLAY_SMALL)
;
}
Expand Down
Loading

0 comments on commit 89c53c9

Please sign in to comment.