From ccbd4f1df9c6c1b4680a5d4af36c2132a57a831a Mon Sep 17 00:00:00 2001 From: lothrazar Date: Mon, 23 Sep 2024 22:40:01 -0700 Subject: [PATCH] work in progress: working facade blocks rendering in cables --- .../block/TileCableWithFacing.java | 5 +- .../block/cable/BlockCable.java | 7 ++ .../storagenetwork/block/cable/TileCable.java | 54 +++++++++++++++ .../block/facade/TesrCable.java | 31 +++++++++ .../block/facade/TesrExportCable.java | 31 +++++++++ .../block/facade/TesrImportCable.java | 31 +++++++++ .../block/facade/TesrImportFilterCable.java | 31 +++++++++ .../block/facade/TesrStorageCable.java | 31 +++++++++ .../network/CableFacadeMessage.java | 67 +++++++++++++++++++ .../registry/ClientEventRegistry.java | 20 ++++++ .../registry/ConfigRegistry.java | 28 +++++++- .../registry/PacketRegistry.java | 2 + .../storagenetwork/registry/SsnEvents.java | 45 +++++++++++++ .../storagenetwork/registry/SsnRegistry.java | 3 + 14 files changed, 382 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/lothrazar/storagenetwork/block/facade/TesrCable.java create mode 100644 src/main/java/com/lothrazar/storagenetwork/block/facade/TesrExportCable.java create mode 100644 src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportCable.java create mode 100644 src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportFilterCable.java create mode 100644 src/main/java/com/lothrazar/storagenetwork/block/facade/TesrStorageCable.java create mode 100644 src/main/java/com/lothrazar/storagenetwork/network/CableFacadeMessage.java diff --git a/src/main/java/com/lothrazar/storagenetwork/block/TileCableWithFacing.java b/src/main/java/com/lothrazar/storagenetwork/block/TileCableWithFacing.java index 22b27004..d6ec58fa 100644 --- a/src/main/java/com/lothrazar/storagenetwork/block/TileCableWithFacing.java +++ b/src/main/java/com/lothrazar/storagenetwork/block/TileCableWithFacing.java @@ -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; @@ -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); diff --git a/src/main/java/com/lothrazar/storagenetwork/block/cable/BlockCable.java b/src/main/java/com/lothrazar/storagenetwork/block/cable/BlockCable.java index 8130ab3e..7a4af675 100644 --- a/src/main/java/com/lothrazar/storagenetwork/block/cable/BlockCable.java +++ b/src/main/java/com/lothrazar/storagenetwork/block/cable/BlockCable.java @@ -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; @@ -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); } diff --git a/src/main/java/com/lothrazar/storagenetwork/block/cable/TileCable.java b/src/main/java/com/lothrazar/storagenetwork/block/cable/TileCable.java index dc7f274c..b8c35e4c 100644 --- a/src/main/java/com/lothrazar/storagenetwork/block/cable/TileCable.java +++ b/src/main/java/com/lothrazar/storagenetwork/block/cable/TileCable.java @@ -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; /** @@ -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; + } + } diff --git a/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrCable.java b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrCable.java new file mode 100644 index 00000000..8e2ab3d3 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrCable.java @@ -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 { + + 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()); + } + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrExportCable.java b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrExportCable.java new file mode 100644 index 00000000..a64363c9 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrExportCable.java @@ -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 { + + 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()); + } + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportCable.java b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportCable.java new file mode 100644 index 00000000..908857a5 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportCable.java @@ -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 { + + 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()); + } + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportFilterCable.java b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportFilterCable.java new file mode 100644 index 00000000..18501353 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrImportFilterCable.java @@ -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 { + + 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()); + } + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrStorageCable.java b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrStorageCable.java new file mode 100644 index 00000000..6d9df564 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/block/facade/TesrStorageCable.java @@ -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 { + + 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()); + } + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/network/CableFacadeMessage.java b/src/main/java/com/lothrazar/storagenetwork/network/CableFacadeMessage.java new file mode 100644 index 00000000..83cbe105 --- /dev/null +++ b/src/main/java/com/lothrazar/storagenetwork/network/CableFacadeMessage.java @@ -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 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); + } +} diff --git a/src/main/java/com/lothrazar/storagenetwork/registry/ClientEventRegistry.java b/src/main/java/com/lothrazar/storagenetwork/registry/ClientEventRegistry.java index 6966fec4..55729dff 100644 --- a/src/main/java/com/lothrazar/storagenetwork/registry/ClientEventRegistry.java +++ b/src/main/java/com/lothrazar/storagenetwork/registry/ClientEventRegistry.java @@ -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); + } } diff --git a/src/main/java/com/lothrazar/storagenetwork/registry/ConfigRegistry.java b/src/main/java/com/lothrazar/storagenetwork/registry/ConfigRegistry.java index e382f6fb..50b19ec3 100644 --- a/src/main/java/com/lothrazar/storagenetwork/registry/ConfigRegistry.java +++ b/src/main/java/com/lothrazar/storagenetwork/registry/ConfigRegistry.java @@ -2,14 +2,19 @@ import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.electronwill.nightconfig.core.io.WritingMode; +import com.lothrazar.library.util.StringParseUtil; import com.lothrazar.storagenetwork.StorageNetworkMod; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec.BooleanValue; import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; import net.minecraftforge.common.ForgeConfigSpec.IntValue; +import net.minecraftforge.registries.ForgeRegistries; public class ConfigRegistry { @@ -22,10 +27,20 @@ public class ConfigRegistry { private static ConfigValue> IGNORELIST; public static IntValue ITEMRANGE; public static IntValue RECIPEMAXTAGS; + private static ConfigValue> CABLEIGNORELIST; + public static BooleanValue facadesUseCollisionBoundingBox; static { initConfig(); } + public static boolean isFacadeAllowed(ItemStack item) { + ResourceLocation itemId = ForgeRegistries.ITEMS.getKey(item.getItem()); + if (StringParseUtil.isInList(CABLEIGNORELIST.get(), itemId)) { + return false; + } + return true; + } + private static void initConfig() { COMMON_BUILDER.comment("General settings").push(StorageNetworkMod.MODID); LOGSPAM = COMMON_BUILDER.comment("Enable very spammy logs. Sometimes useful for debugging. ").define("logSpam", false); @@ -36,8 +51,9 @@ private static void initConfig() { REFRESHTICKS = COMMON_BUILDER.comment("\r\n How often to auto-refresh a network (one second is 20 ticks)").defineInRange("autoRefreshTicks", 20, 2, 4096); List list = new ArrayList(); list.add("extrautils2:playerchest"); - IGNORELIST = COMMON_BUILDER.comment("\r\n Disable these blocks from ever being able to connect to the network, they will be treated as a non-inventory.").define("NotallowedBlocks", - list); + IGNORELIST = COMMON_BUILDER.comment("\r\n Disable these blocks from ever being able to connect to the network, they will be treated as a non-inventory.") + .define("NotallowedBlocks", + list); EXCHANGEBUFFER = COMMON_BUILDER.comment("\r\n How many itemstacks from the network are visible to external connections through the storagenetwork:exchange. " + "Too low and not all items can pass through, too large and there will be packet/buffer overflows.") .defineInRange("exchangeBufferSize", 1024, 1, 5000); @@ -45,6 +61,14 @@ private static void initConfig() { .defineInRange("remoteMaxRange", -1, -1, Integer.MAX_VALUE / 256); RECIPEMAXTAGS = COMMON_BUILDER.comment("\r\n When matching items to recipes in the JEI + button, this is the maximum number of tags to serialize over the network when on a server. Reduce if you get errors relating to Packet Sizes being too large (Minecraft 1.12.2 had this hardcoded at 5).") .defineInRange("jeiMaximumRecipeTags", 64, 5, 128); + // + list = Arrays.asList("minecraft:golden_rail", "minecraft:ladder", "minecraft:rail", "minecraft:detector_rail", "minecraft:activator_rail", + "minecraft:double_plant", + "minecraft:waterlily"); + CABLEIGNORELIST = COMMON_BUILDER.comment("\r\n Disable these blocks from ever being able to connect to the network, they will be treated as a non-inventory.") + .define("BlacklistFacadeCableItems", list); + facadesUseCollisionBoundingBox = COMMON_BUILDER.comment("If this is true, cables with facades will also use the collision block from the block facade (ie stairs, carpet, etc). ") + .define("facadesUseCollisionBoundingBox", true); COMMON_BUILDER.pop(); COMMON_CONFIG = COMMON_BUILDER.build(); } diff --git a/src/main/java/com/lothrazar/storagenetwork/registry/PacketRegistry.java b/src/main/java/com/lothrazar/storagenetwork/registry/PacketRegistry.java index bc72588d..5f7edcb3 100644 --- a/src/main/java/com/lothrazar/storagenetwork/registry/PacketRegistry.java +++ b/src/main/java/com/lothrazar/storagenetwork/registry/PacketRegistry.java @@ -2,6 +2,7 @@ import com.lothrazar.storagenetwork.StorageNetworkMod; import com.lothrazar.storagenetwork.network.CableDataMessage; +import com.lothrazar.storagenetwork.network.CableFacadeMessage; import com.lothrazar.storagenetwork.network.CableIOMessage; import com.lothrazar.storagenetwork.network.CableLimitMessage; import com.lothrazar.storagenetwork.network.ClearRecipeMessage; @@ -46,5 +47,6 @@ public static void init() { INSTANCE.registerMessage(id++, RefreshFilterClientMessage.class, RefreshFilterClientMessage::encode, RefreshFilterClientMessage::decode, RefreshFilterClientMessage::handle); INSTANCE.registerMessage(id++, SortClientMessage.class, SortClientMessage::encode, SortClientMessage::decode, SortClientMessage::handle); INSTANCE.registerMessage(id++, KeybindCurioMessage.class, KeybindCurioMessage::encode, KeybindCurioMessage::decode, KeybindCurioMessage::handle); + INSTANCE.registerMessage(id++, CableFacadeMessage.class, CableFacadeMessage::encode, CableFacadeMessage::decode, CableFacadeMessage::handle); } } diff --git a/src/main/java/com/lothrazar/storagenetwork/registry/SsnEvents.java b/src/main/java/com/lothrazar/storagenetwork/registry/SsnEvents.java index 207a6d42..1c1e5fe1 100644 --- a/src/main/java/com/lothrazar/storagenetwork/registry/SsnEvents.java +++ b/src/main/java/com/lothrazar/storagenetwork/registry/SsnEvents.java @@ -1,7 +1,20 @@ package com.lothrazar.storagenetwork.registry; +import com.lothrazar.storagenetwork.StorageNetworkMod; +import com.lothrazar.storagenetwork.block.cable.TileCable; import com.lothrazar.storagenetwork.item.ItemBuilder; +import com.lothrazar.storagenetwork.network.CableFacadeMessage; import com.lothrazar.storagenetwork.network.KeybindCurioMessage; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtUtils; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; import net.minecraftforge.client.event.InputEvent; import net.minecraftforge.event.entity.player.EntityItemPickupEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; @@ -17,6 +30,37 @@ public void onEntityItemPickupEvent(EntityItemPickupEvent event) { @SubscribeEvent public void onHit(PlayerInteractEvent.LeftClickBlock event) { ItemBuilder.onLeftClickBlock(event); + + Level level = event.getLevel(); + if (!level.isClientSide) { + return; + } + Player player = event.getEntity(); + ItemStack held = player.getItemInHand(event.getHand()); + TileCable cable = TileCable.getTileCable(level, event.getPos()); + if (cable != null) { + if (held.isEmpty()) { + PacketRegistry.INSTANCE.sendToServer(new CableFacadeMessage(event.getPos(), true)); + } + else { + Block block = Block.byItem(held.getItem()); + if (block == null || block == Blocks.AIR) { + StorageNetworkMod.log("no block"); + return; + } + if (!ConfigRegistry.isFacadeAllowed(held)) { + StorageNetworkMod.log("not allowed as a facade from config file"); + return; + } + BlockHitResult bhr = (BlockHitResult) player.pick(5, 1, false); + BlockPlaceContext context = new BlockPlaceContext(player, event.getHand(), held, bhr); + + BlockState facadeState = null; + facadeState = block.getStateForPlacement(context); + CompoundTag tags = NbtUtils.writeBlockState(facadeState); + PacketRegistry.INSTANCE.sendToServer(new CableFacadeMessage(event.getPos(), tags)); + } + } } @SubscribeEvent @@ -26,4 +70,5 @@ public void onKeyInput(InputEvent.Key event) { PacketRegistry.INSTANCE.sendToServer(new KeybindCurioMessage()); } } + } diff --git a/src/main/java/com/lothrazar/storagenetwork/registry/SsnRegistry.java b/src/main/java/com/lothrazar/storagenetwork/registry/SsnRegistry.java index 075e9d55..659f32fc 100644 --- a/src/main/java/com/lothrazar/storagenetwork/registry/SsnRegistry.java +++ b/src/main/java/com/lothrazar/storagenetwork/registry/SsnRegistry.java @@ -111,8 +111,11 @@ public static class Tiles { public static final RegistryObject> EXPORT_KABEL = TILES.register("export_kabel", () -> BlockEntityType.Builder.of(TileCableExport::new, Blocks.EXPORT_KABEL.get()).build(null)); public static final RegistryObject> EXCHANGE = TILES.register("exchange", () -> BlockEntityType.Builder.of(TileExchange::new, Blocks.EXCHANGE.get()).build(null)); public static final RegistryObject> COLLECTOR = TILES.register("collector", () -> BlockEntityType.Builder.of(TileCollection::new, Blocks.COLLECTOR.get()).build(null)); + + } + @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public static class Menus {