Skip to content

Commit

Permalink
work in progress: working facade blocks rendering in cables
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Sep 24, 2024
1 parent d59dffe commit ccbd4f1
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lothrazar.storagenetwork.api.EnumConnectType;
import com.lothrazar.storagenetwork.block.cable.BlockCable;
import com.lothrazar.storagenetwork.block.cable.TileCable;
import com.lothrazar.storagenetwork.block.main.TileMain;
import com.lothrazar.storagenetwork.util.UtilConnections;
import net.minecraft.core.BlockPos;
Expand All @@ -10,9 +11,9 @@
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public class TileCableWithFacing extends TileConnectable {
public class TileCableWithFacing extends TileCable {

Direction direction = null;
private Direction direction = null;

public TileCableWithFacing(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) {
super(tileEntityTypeIn, pos, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.lothrazar.storagenetwork.api.IConnectable;
import com.lothrazar.storagenetwork.api.IConnectableItemAutoIO;
import com.lothrazar.storagenetwork.capability.CapabilityConnectableAutoIO;
import com.lothrazar.storagenetwork.registry.ConfigRegistry;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
import com.lothrazar.storagenetwork.util.ShapeBuilder;
import com.lothrazar.storagenetwork.util.UtilConnections;
Expand Down Expand Up @@ -124,6 +125,12 @@ public static BlockState cleanBlockState(BlockState state) {

@Override
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
TileCable tile = TileCable.getTileCable(worldIn, pos);
if (tile != null
&& ConfigRegistry.facadesUseCollisionBoundingBox.get()
&& tile.getFacadeState() != null) {
return tile.getFacadeState().getShape(worldIn, pos, context);
}
return ShapeCache.getOrCreate(state, ShapeBuilder::createShape);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import com.lothrazar.storagenetwork.block.TileConnectable;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

/**
Expand All @@ -11,7 +17,55 @@
*/
public class TileCable extends TileConnectable {

private static final String NBT_FACADE = "facade";
private CompoundTag facadeState = null;

public TileCable(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) {
super(tileEntityTypeIn, pos, state);
}

public TileCable(BlockPos pos, BlockState state) {
super(SsnRegistry.Tiles.KABEL.get(), pos, state);
}

@Override
public void load(CompoundTag compound) {
super.load(compound);
if (compound.contains(NBT_FACADE)) {
setFacadeState(compound.getCompound(NBT_FACADE));
}
else {
setFacadeState(null);
}

}

@Override
public void saveAdditional(CompoundTag compound) {
super.saveAdditional(compound);
if (facadeState != null) {
compound.put(NBT_FACADE, facadeState);
}
}

public static TileCable getTileCable(BlockGetter world, BlockPos pos) {
BlockEntity tile = world.getBlockEntity(pos);
if (tile instanceof TileCable te) {
return te;
}
return null;
}

public BlockState getFacadeState() {
if (level == null || facadeState == null || facadeState.isEmpty()) {
return null; // level is null on world load
}
BlockState stateFound = NbtUtils.readBlockState(level.holderLookup(Registries.BLOCK), facadeState);
return stateFound;
}

public void setFacadeState(CompoundTag facadeState) {
this.facadeState = facadeState;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lothrazar.storagenetwork.block.facade;

import com.lothrazar.storagenetwork.block.cable.TileCable;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraftforge.client.model.data.ModelData;

public class TesrCable implements BlockEntityRenderer<TileCable> {

private BlockRenderDispatcher brd;

public TesrCable(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
}

@Override
public boolean shouldRenderOffScreen(TileCable te) {
return true;
}

@Override
public void render(TileCable te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int partialTicks, int destroyStage) {
if (te.getFacadeState() != null) {
brd.renderSingleBlock(te.getFacadeState(), matrixStack, ibuffer, partialTicks, destroyStage, ModelData.EMPTY, RenderType.solid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lothrazar.storagenetwork.block.facade;

import com.lothrazar.storagenetwork.block.cable.export.TileCableExport;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraftforge.client.model.data.ModelData;

public class TesrExportCable implements BlockEntityRenderer<TileCableExport> {

private BlockRenderDispatcher brd;

public TesrExportCable(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
}

@Override
public boolean shouldRenderOffScreen(TileCableExport te) {
return true;
}

@Override
public void render(TileCableExport te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int partialTicks, int destroyStage) {
if (te.getFacadeState() != null) {
brd.renderSingleBlock(te.getFacadeState(), matrixStack, ibuffer, partialTicks, destroyStage, ModelData.EMPTY, RenderType.solid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lothrazar.storagenetwork.block.facade;

import com.lothrazar.storagenetwork.block.cable.input.TileCableIO;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraftforge.client.model.data.ModelData;

public class TesrImportCable implements BlockEntityRenderer<TileCableIO> {

private BlockRenderDispatcher brd;

public TesrImportCable(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
}

@Override
public boolean shouldRenderOffScreen(TileCableIO te) {
return true;
}

@Override
public void render(TileCableIO te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int partialTicks, int destroyStage) {
if (te.getFacadeState() != null) {
brd.renderSingleBlock(te.getFacadeState(), matrixStack, ibuffer, partialTicks, destroyStage, ModelData.EMPTY, RenderType.solid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lothrazar.storagenetwork.block.facade;

import com.lothrazar.storagenetwork.block.cable.inputfilter.TileCableImportFilter;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraftforge.client.model.data.ModelData;

public class TesrImportFilterCable implements BlockEntityRenderer<TileCableImportFilter> {

private BlockRenderDispatcher brd;

public TesrImportFilterCable(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
}

@Override
public boolean shouldRenderOffScreen(TileCableImportFilter te) {
return true;
}

@Override
public void render(TileCableImportFilter te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int partialTicks, int destroyStage) {
if (te.getFacadeState() != null) {
brd.renderSingleBlock(te.getFacadeState(), matrixStack, ibuffer, partialTicks, destroyStage, ModelData.EMPTY, RenderType.solid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lothrazar.storagenetwork.block.facade;

import com.lothrazar.storagenetwork.block.cable.link.TileCableLink;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraftforge.client.model.data.ModelData;

public class TesrStorageCable implements BlockEntityRenderer<TileCableLink> {

private BlockRenderDispatcher brd;

public TesrStorageCable(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
}

@Override
public boolean shouldRenderOffScreen(TileCableLink te) {
return true;
}

@Override
public void render(TileCableLink te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int partialTicks, int destroyStage) {
if (te.getFacadeState() != null) {
brd.renderSingleBlock(te.getFacadeState(), matrixStack, ibuffer, partialTicks, destroyStage, ModelData.EMPTY, RenderType.solid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.lothrazar.storagenetwork.network;

import java.util.function.Supplier;
import com.lothrazar.storagenetwork.block.cable.TileCable;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;

public class CableFacadeMessage {

//sync sort data TO client gui FROM server
private BlockPos pos;
private boolean erase = false;
private CompoundTag blockStateTag = new CompoundTag();

private CableFacadeMessage() {}

public CableFacadeMessage(BlockPos pos, CompoundTag state) {
this.pos = pos;
this.blockStateTag = state;
this.erase = false;
}

public CableFacadeMessage(BlockPos pos, boolean eraseIn) {
this.pos = pos;
this.erase = eraseIn;
blockStateTag = new CompoundTag();
}

public static void handle(CableFacadeMessage message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
ServerPlayer player = ctx.get().getSender();
ServerLevel serverWorld = (ServerLevel) player.level();
TileCable tile = TileCable.getTileCable(serverWorld, message.pos);
if (tile != null) {
if (message.erase) {
// StorageNetworkMod.log("Network Packet facade SAVE NULL EMPTY ERASE " + message.blockStateTag);
tile.setFacadeState(null);
}
else {
// StorageNetworkMod.log("Network Packet facade SAVE " + message.blockStateTag);
tile.setFacadeState(message.blockStateTag);
}
serverWorld.markAndNotifyBlock(message.pos, serverWorld.getChunkAt(message.pos),
tile.getBlockState(), tile.getBlockState(), 3, 1);
}
});
ctx.get().setPacketHandled(true);
}

public static CableFacadeMessage decode(FriendlyByteBuf buf) {
CableFacadeMessage message = new CableFacadeMessage();
message.erase = buf.readBoolean();
message.pos = buf.readBlockPos();
message.blockStateTag = buf.readNbt();
return message;
}

public static void encode(CableFacadeMessage msg, FriendlyByteBuf buf) {
buf.writeBoolean(msg.erase);
buf.writeBlockPos(msg.pos);
buf.writeNbt(msg.blockStateTag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@

import org.lwjgl.glfw.GLFW;
import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.block.facade.TesrCable;
import com.lothrazar.storagenetwork.block.facade.TesrExportCable;
import com.lothrazar.storagenetwork.block.facade.TesrImportCable;
import com.lothrazar.storagenetwork.block.facade.TesrImportFilterCable;
import com.lothrazar.storagenetwork.block.facade.TesrStorageCable;
import com.lothrazar.storagenetwork.registry.SsnRegistry.Tiles;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ClientEventRegistry {

public static final ResourceLocation SLOT = new ResourceLocation(StorageNetworkMod.MODID, "textures/gui/slot.png");
public static final KeyMapping INVENTORY_KEY = new KeyMapping("key.storagenetwork.remote", KeyConflictContext.UNIVERSAL, InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_I, "key.categories.inventory");

@SubscribeEvent
public static void onRegisterRenderers(EntityRenderersEvent.RegisterRenderers event) {
event.registerBlockEntityRenderer(Tiles.KABEL.get(), TesrCable::new);
event.registerBlockEntityRenderer(Tiles.EXPORT_KABEL.get(), TesrExportCable::new);
event.registerBlockEntityRenderer(Tiles.STORAGE_KABEL.get(), TesrStorageCable::new);
event.registerBlockEntityRenderer(Tiles.IMPORT_FILTER_KABEL.get(), TesrImportFilterCable::new);
event.registerBlockEntityRenderer(Tiles.IMPORT_KABEL.get(), TesrImportCable::new);
}
}
Loading

0 comments on commit ccbd4f1

Please sign in to comment.