Skip to content

Commit

Permalink
Station Name Plate
Browse files Browse the repository at this point in the history
It's ugly, isn't it?
  • Loading branch information
ZiYueCommentary committed Jul 23, 2024
1 parent 2a1a8aa commit ae2d924
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public interface BlockEntityTypes
BlockEntityTypeRegistryObject<BlockPSDDoorTianjin.BlockEntity> PSD_DOOR_TIANJIN = Registry.registerBlockEntityType("psd_door_tianjin", BlockPSDDoorTianjin.BlockEntity::new, BlockList.PSD_DOOR_TIANJIN::get);
BlockEntityTypeRegistryObject<BlockPSDTopTianjin.BlockEntity> PSD_TOP_TIANJIN = Registry.registerBlockEntityType("psd_top_tianjin", BlockPSDTopTianjin.BlockEntity::new, BlockList.PSD_TOP_TIANJIN::get);
BlockEntityTypeRegistryObject<BlockStationNameWallLegacy.BlockEntity> STATION_NAME_WALL_LEGACY = Registry.registerBlockEntityType("station_name_wall_legacy", BlockStationNameWallLegacy.BlockEntity::new, BlockList.STATION_NAME_WALL_LEGACY::get);
BlockEntityTypeRegistryObject<BlockStationNamePlate.BlockEntity> STATION_NAME_PLATE = Registry.registerBlockEntityType("station_name_plate", BlockStationNamePlate.BlockEntity::new, BlockList.STATION_NAME_PLATE::get);

static void registerBlockEntities() {
// Calling this class to initialize constants
Expand Down
2 changes: 2 additions & 0 deletions fabric/src/main/java/ziyue/tjmetro/mod/BlockList.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public interface BlockList
BlockRegistryObject METAL_DETECTION_DOOR = Registry.registerBlockWithBlockItem("metal_detection_door", () -> new Block(new BlockMetalDetectionDoor()), DECORATION);
// BlockRegistryObject HIGH_SPEED_REPEATER = Registry.registerBlockWithBlockItem("high_speed_repeater", () -> new Block(new BlockHighSpeedRepeater()), MISCELLANEOUS);
BlockRegistryObject STATION_NAME_WALL_LEGACY = Registry.registerBlockWithBlockItem("station_name_wall_legacy", () -> new Block(new BlockStationNameWallLegacy()), SIGNS);
BlockRegistryObject STATION_NAME_PLATE = Registry.registerBlockWithBlockItem("station_name_plate", () -> new Block(new BlockStationNamePlate()), SIGNS);
BlockRegistryObject STATION_NAME_PLATE_MIDDLE = Registry.registerBlock("station_name_plate_middle", () -> new Block(new BlockStationNamePlate()));

static void registerBlocks() {
// Calling this class to initialize constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void init() {
RegistryClient.registerBlockEntityRenderer(BlockEntityTypes.PSD_TOP_TIANJIN, RenderPSDTopTianjin::new);
RegistryClient.registerBlockEntityRenderer(BlockEntityTypes.PSD_DOOR_TIANJIN, dispatcher -> new RenderPSDAPGDoor<>(dispatcher, 0));
RegistryClient.registerBlockEntityRenderer(BlockEntityTypes.STATION_NAME_WALL_LEGACY, RenderStationNameWallLegacy::new);
RegistryClient.registerBlockEntityRenderer(BlockEntityTypes.STATION_NAME_PLATE, RenderStationNamePlate::new);

RegistryClient.registerEntityRenderer(EntityTypes.SEAT, RenderSeat::new);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package ziyue.tjmetro.mod.block;

import org.jetbrains.annotations.NotNull;
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.BlockEntityExtension;
import org.mtr.mapping.mapper.BlockHelper;
import org.mtr.mapping.tool.HolderBase;
import org.mtr.mod.Items;
import org.mtr.mod.block.BlockRouteSignBase;
import org.mtr.mod.block.IBlock;
import ziyue.tjmetro.mod.BlockEntityTypes;
import ziyue.tjmetro.mod.BlockList;
import ziyue.tjmetro.mod.ItemList;
import ziyue.tjmetro.mod.Registry;
import ziyue.tjmetro.mod.block.base.BlockRailwaySignBase;
import ziyue.tjmetro.mod.block.base.IRailwaySign;
import ziyue.tjmetro.mod.packet.PacketOpenBlockEntityScreen;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class BlockStationNamePlate extends BlockRailwaySignBase
{
public static final IntegerProperty ARROW_DIRECTION = IntegerProperty.of("arrow_direction", 0, 2);

public BlockStationNamePlate() {
this(BlockHelper.createBlockSettings(true));
}

public BlockStationNamePlate(BlockSettings settings) {
super(settings, 8, false);
}

@Nonnull
@Override
public ActionResult onUse2(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
return IBlock.checkHoldingItem(world, player, item -> {
final Direction facing = IBlock.getStatePropertySafe(state, FACING);
final Direction hitSide = hit.getSide();
if (hitSide == facing || hitSide == facing.getOpposite()) {
final BlockPos checkPos = findEndWithDirection(world, pos, hitSide.getOpposite(), false);
if (checkPos != null) {
if (item == org.mtr.mod.Items.BRUSH.get()) {
world.setBlockState(checkPos, world.getBlockState(checkPos).cycle(new Property<>(ARROW_DIRECTION.data)));
} else {
Registry.sendPacketToClient(ServerPlayerEntity.cast(player), new PacketOpenBlockEntityScreen(checkPos));
}
}
}
}, null, Items.BRUSH.get(), ItemList.WRENCH.get());
}

@NotNull
@Override
public BlockState getStateForNeighborUpdate2(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
return IRailwaySign.getStateForNeighborUpdate(state, direction, neighborState, BlockList.STATION_NAME_PLATE_MIDDLE.get());
}

@Override
public void onPlaced2(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
if (world.isClient()) return;

final Direction facing = IBlock.getStatePropertySafe(state, FACING);
for (int i = 1; i <= getMiddleLength(); i++) {
world.setBlockState(pos.offset(facing.rotateYClockwise(), i), BlockList.STATION_NAME_PLATE_MIDDLE.get().getDefaultState().with(new Property<>(FACING.data), facing.data), 3);
}
world.setBlockState(pos.offset(facing.rotateYClockwise(), getMiddleLength() + 1), state.getBlock().getDefaultState().with(new Property<>(FACING.data), facing.getOpposite().data).with(new Property<>(ARROW_DIRECTION.data), 0), 3);
world.updateNeighbors(pos, Blocks.getAirMapped());
state.updateNeighbors(new WorldAccess(world.data), pos, 3);
}

@Nonnull
@Override
public VoxelShape getOutlineShape2(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
final Direction facing = IBlock.getStatePropertySafe(state, FACING);
if (this == BlockList.STATION_NAME_PLATE_MIDDLE.get().data) {
return IBlock.getVoxelShapeByDirection(0, 3, 7, 16, 12, 9, facing);
} else {
final VoxelShape main = IBlock.getVoxelShapeByDirection(0, 3, 7, 16, 12, 9, facing);
final VoxelShape pole = IBlock.getVoxelShapeByDirection(-1, 3, 7, 0.25, 16, 9, facing);
return VoxelShapes.union(main, pole);
}
}

@Override
protected int getMiddleLength() {
return 2;
}

@Nonnull
@Override
public String getTranslationKey2() {
return "block.tjmetro.station_name_plate";
}

@Override
public void addBlockProperties(List<HolderBase<?>> properties) {
properties.add(FACING);
properties.add(ARROW_DIRECTION);
}

@Override
protected BlockPos findEndWithDirection(World world, BlockPos startPos, Direction direction, boolean allowOpposite) {
return IRailwaySign.findEndWithDirection(world, startPos, direction, allowOpposite, BlockList.STATION_NAME_PLATE_MIDDLE.get());
}

@Override
public BlockEntityExtension createBlockEntity(BlockPos blockPos, BlockState blockState) {
if (this == BlockList.STATION_NAME_PLATE_MIDDLE.get().data) return null;
else return new BlockEntity(blockPos, blockState);
}

public static class BlockEntity extends BlockRouteSignBase.BlockEntityBase
{
public BlockEntity(BlockPos pos, BlockState state) {
super(BlockEntityTypes.STATION_NAME_PLATE.get(), pos, state);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.ResourceManagerHelper;
import org.mtr.mod.Init;
import org.mtr.mod.client.MinecraftClientData;
import org.mtr.mod.config.Config;
import org.mtr.mod.config.LanguageDisplay;
import org.mtr.mod.data.IGui;
Expand Down Expand Up @@ -106,6 +107,10 @@ public DynamicResource getStationNameEntrance(long stationId, long selectedId, i
return getResource(String.format("tjmetro_station_name_entrance_%s_%s_%s_%s_%s_%s", stationId, selectedId, style, stationName, isBMT, aspectRatio), () -> RouteMapGenerator.generateStationNameEntrance(stationId, selectedId, style, stationName, isBMT, aspectRatio), DefaultRenderingColor.TRANSPARENT);
}

public DynamicResource getStationNamePlate(long platformId, int arrowDirection, int backgroundColor, float paddingScale, float aspectRatio, int textColor, int transparentColor) {
return getResource(String.format("tjmetro_station_name_plate_%s_%s_%s_%s_%s_%s_%s", platformId, arrowDirection, backgroundColor, paddingScale, aspectRatio, textColor, transparentColor), () -> RouteMapGenerator.generateStationNamePlate(platformId, arrowDirection, backgroundColor, paddingScale, aspectRatio, textColor, transparentColor), DefaultRenderingColor.TRANSPARENT);
}

public Text getText(String text, int maxWidth, int maxHeight, int fontSizeCjk, int fontSize, int padding, IGui.HorizontalAlignment horizontalAlignment) {
return getText(text, maxWidth, maxHeight, fontSizeCjk, fontSize, padding, horizontalAlignment, false);
}
Expand All @@ -117,9 +122,7 @@ public Text getText(String text, int maxWidth, int maxHeight, int fontSizeCjk, i
return new DynamicTextureCache.Text(pixels, dimensions[0], dimensions[1], dimensions[0]);
}

if (maxWidth <= 0) {
return new Text(new byte[0], 0, 0, 0);
}
if (maxWidth <= 0) return new Text(new byte[0], 0, 0, 0);

final boolean customFont = ConfigClient.USE_TIANJIN_METRO_FONT.get();
final boolean oneRow = horizontalAlignment == null;
Expand Down Expand Up @@ -243,7 +246,7 @@ protected DynamicResource getResource(String key, Supplier<NativeImage> supplier
}

while (fontCjk == null) {
ResourceManagerHelper.readResource(ConfigClient.USE_TIANJIN_METRO_FONT.get() ? new Identifier(Reference.MOD_ID, "font/dengxian.ttf") : new Identifier(Init.MOD_ID, "font/noto-serif-cjk-tc-semibold.ttf"), inputStream -> {
ResourceManagerHelper.readResource(ConfigClient.USE_TIANJIN_METRO_FONT.get() ? new Identifier(Reference.MOD_ID, "font/dengxian.ttf") : new Identifier(Init.MOD_ID, "font/noto-serif-cjk-tc-semibold.ttf"), inputStream -> {
try {
fontCjk = Font.createFont(Font.TRUETYPE_FONT, inputStream);
} catch (Exception e) {
Expand Down Expand Up @@ -297,8 +300,6 @@ public record Text(byte[] pixels, int width, int height, int renderWidth)

}

// ¯\_(ツ)_/¯ protected are everywhere

/**
* @see org.mtr.mod.client.DynamicTextureCache.DynamicResource
*/
Expand Down Expand Up @@ -333,7 +334,8 @@ protected void remove() {
}
}

protected enum DefaultRenderingColor {
protected enum DefaultRenderingColor
{
BLACK(DEFAULT_BLACK_RESOURCE),
WHITE(DEFAULT_WHITE_RESOURCE),
TRANSPARENT(DEFAULT_TRANSPARENT_RESOURCE);
Expand Down
Loading

0 comments on commit ae2d924

Please sign in to comment.