Begin adding working screen to TVs

This commit is contained in:
Andrew-71 2023-08-07 23:03:27 +03:00
parent 8033255f7d
commit 497ae9149b
5 changed files with 86 additions and 12 deletions

View file

@ -4,7 +4,9 @@ import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry;
import net.minecraft.client.render.RenderLayer;
import su.a71.new_soviet.entity.TVBlockEntity;
import su.a71.new_soviet.registration.NSE_Blocks;
import su.a71.new_soviet.registration.NSE_Custom;
@ -17,5 +19,7 @@ public class NewSovietClient implements ClientModInitializer {
BlockRenderLayerMap.INSTANCE.putBlock(NSE_Custom.LAMP, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(NSE_Custom.CEILING_FAN, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(NSE_Custom.SIREN, RenderLayer.getCutout());
BlockEntityRendererRegistry.register(NSE_Custom.TV_BLOCK_ENTITY, TVBlockEntityRenderer::new);
}
}

View file

@ -0,0 +1,22 @@
package su.a71.new_soviet;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.util.math.MatrixStack;
import su.a71.new_soviet.entity.TVBlockEntity;
@Environment(EnvType.CLIENT)
public class TVBlockEntityRenderer implements BlockEntityRenderer<TVBlockEntity> {
public TVBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {}
@Override
public void render(TVBlockEntity blockEntity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
matrices.push();
// Rendering stuff here
matrices.pop();
}
}

View file

@ -1,7 +1,7 @@
package su.a71.new_soviet.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.sound.BlockSoundGroup;
@ -12,10 +12,11 @@ import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import su.a71.new_soviet.entity.TVBlockEntity;
public class TVBlock extends HorizontalFacingBlock {
public class TVBlock extends HorizontalFacingBlock implements BlockEntityProvider {
public TVBlock(AbstractBlock.Settings settings) {
super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f));
super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.BLOCK).strength(1f, 2f));
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
}
@ -38,4 +39,9 @@ public class TVBlock extends HorizontalFacingBlock {
public BlockState getPlacementState(ItemPlacementContext ctx) {
return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
}
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new TVBlockEntity(pos, state);
}
}

View file

@ -1,10 +1,18 @@
package su.a71.new_soviet.entity;
import io.netty.channel.unix.Errors;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;
import su.a71.new_soviet.registration.NSE_Custom;
import java.util.Arrays;
public class TVBlockEntity extends BlockEntity {
/*
@ -19,18 +27,45 @@ public class TVBlockEntity extends BlockEntity {
7 white
*/
public int[][] display;
public TVBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
public int DISPLAY_SIZE = 8; // The monitor is 8x8
public TVBlockEntity(BlockPos pos, BlockState state) {
super(NSE_Custom.TV_BLOCK_ENTITY, pos, state);
display = new int[8][8];
Arrays.fill(display, new int[]{0, 0, 0, 0, 0, 0, 0, 0});
}
public int getPixel(int x, int y) {
if (x > 7 || y > 7 || x < 0 || y < 0) {
throw new IndexOutOfBoundsException("Coordinates must be within 0-7 range");
}
return display[x][y];
}
@Override
protected void writeNbt(NbtCompound nbt) {
//nbt.putIntArray();
for (int i = 0; i < DISPLAY_SIZE; i++) {
nbt.putIntArray("display_row_" + i, display[i]);
}
super.writeNbt(nbt);
}
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
display = new int[8][8];
for (int i = 0; i < DISPLAY_SIZE; i++) {
display[i] = nbt.getIntArray("display_row_" + i);
}
}
@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
@Override
public NbtCompound toInitialChunkDataNbt() {
return createNbt();
}
}

View file

@ -4,8 +4,10 @@ import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.MapColor;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
@ -18,24 +20,29 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.blocks.*;
import su.a71.new_soviet.entity.TVBlockEntity;
import java.util.Optional;
import java.util.function.Supplier;
public class NSE_Custom {
public static final TVBlock TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_YELLOW));
public static final TVBlock RED_TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_RED));
public static final TVBlock BROWN_TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_BROWN));
public static final BlockEntityType<TVBlockEntity> TV_BLOCK_ENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE,
new Identifier(NewSoviet.MOD_ID, "tv_block_entity"),
FabricBlockEntityTypeBuilder.create(TVBlockEntity::new, TV, RED_TV, BROWN_TV).build()
);
public static final RadioBlock RADIO = new RadioBlock();
public static final LampBlock LAMP = new LampBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.LANTERN).strength(1f, 1.5f).mapColor(MapColor.WHITE));
public static final LightBulbBlock LIGHT_BULB = new LightBulbBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.GLASS).strength(1f, 1.5f).mapColor(MapColor.WHITE));
public static final SoundEvent LIGHT_BULB_BROKEN_SOUND = SoundEvent.of(new Identifier("new_soviet", "light_bulb_broken_sound"));
public static final SoundEvent LIGHT_BULB_BROKEN_SOUND = SoundEvent.of(new Identifier(NewSoviet.MOD_ID, "light_bulb_broken_sound"));
public static final CeilingFanBlock CEILING_FAN = new CeilingFanBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1f, 1.5f).mapColor(MapColor.WHITE));
public static final SirenBlock SIREN = new SirenBlock();
public static final SoundEvent SIREN_SOUND = SoundEvent.of(new Identifier("new_soviet", "siren_sound"));
public static final SoundEvent SIREN_SOUND = SoundEvent.of(new Identifier(NewSoviet.MOD_ID, "siren_sound"));
public static final LandMineBlock LANDMINE = new LandMineBlock(FabricBlockSettings.create().mapColor(MapColor.LIGHT_GRAY));
@ -57,7 +64,7 @@ public class NSE_Custom {
}
public static void init() {
Registry.register(Registries.ITEM_GROUP, new Identifier("new_soviet", "custom"), NSE_CUSTOM_TAB);
Registry.register(Registries.ITEM_GROUP, new Identifier(NewSoviet.MOD_ID, "custom"), NSE_CUSTOM_TAB);
register("tv", () -> TV, NSE_CUSTOM_TAB);
register("red_tv", () -> RED_TV, NSE_CUSTOM_TAB);
register("brown_tv", () -> BROWN_TV, NSE_CUSTOM_TAB);
@ -68,7 +75,7 @@ public class NSE_Custom {
register("siren", () -> SIREN, NSE_CUSTOM_TAB);
register("landmine", () -> LANDMINE, NSE_CUSTOM_TAB);
Registry.register(Registries.SOUND_EVENT, new Identifier("new_soviet", "siren_sound"), SIREN_SOUND);
Registry.register(Registries.SOUND_EVENT, new Identifier("new_soviet", "light_bulb_broken_sound"), LIGHT_BULB_BROKEN_SOUND);
Registry.register(Registries.SOUND_EVENT, new Identifier(NewSoviet.MOD_ID, "siren_sound"), SIREN_SOUND);
Registry.register(Registries.SOUND_EVENT, new Identifier(NewSoviet.MOD_ID, "light_bulb_broken_sound"), LIGHT_BULB_BROKEN_SOUND);
}
}