From 5542f088f9c2076e674948cd0ded73b361006a19 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 6 Mar 2021 14:23:41 -0500 Subject: [PATCH] Add programatic model for interface block --- .../phycon/PhysicalConnectivityClient.kt | 4 + .../phycon/client/PhyModelProvider.kt | 25 +++ .../phycon/client/model/InterfaceModel.kt | 158 ++++++++++++++++++ .../phycon/blockstates/network_interface.json | 155 +---------------- .../block/interface_cable_corner_3.json | 22 --- 5 files changed, 189 insertions(+), 175 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/phycon/client/PhyModelProvider.kt create mode 100644 src/main/kotlin/net/shadowfacts/phycon/client/model/InterfaceModel.kt delete mode 100644 src/main/resources/assets/phycon/models/block/interface_cable_corner_3.json diff --git a/src/main/kotlin/net/shadowfacts/phycon/PhysicalConnectivityClient.kt b/src/main/kotlin/net/shadowfacts/phycon/PhysicalConnectivityClient.kt index 4a5f391..54f97dd 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/PhysicalConnectivityClient.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/PhysicalConnectivityClient.kt @@ -1,12 +1,14 @@ package net.shadowfacts.phycon import net.fabricmc.api.ClientModInitializer +import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry import net.shadowfacts.phycon.block.inserter.InserterScreen import net.shadowfacts.phycon.block.redstone_emitter.RedstoneEmitterScreen import net.shadowfacts.phycon.init.PhyScreens import net.shadowfacts.phycon.block.terminal.TerminalScreen +import net.shadowfacts.phycon.client.PhyModelProvider import net.shadowfacts.phycon.networking.ClientReceiver import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems @@ -16,6 +18,8 @@ import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems object PhysicalConnectivityClient: ClientModInitializer { override fun onInitializeClient() { + ModelLoadingRegistry.INSTANCE.registerResourceProvider(::PhyModelProvider) + ScreenRegistry.register(PhyScreens.TERMINAL, ::TerminalScreen) ScreenRegistry.register(PhyScreens.INSERTER, ::InserterScreen) ScreenRegistry.register(PhyScreens.REDSTONE_EMITTER, ::RedstoneEmitterScreen) diff --git a/src/main/kotlin/net/shadowfacts/phycon/client/PhyModelProvider.kt b/src/main/kotlin/net/shadowfacts/phycon/client/PhyModelProvider.kt new file mode 100644 index 0000000..ba8efcc --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/client/PhyModelProvider.kt @@ -0,0 +1,25 @@ +package net.shadowfacts.phycon.client + +import net.fabricmc.fabric.api.client.model.ModelProviderContext +import net.fabricmc.fabric.api.client.model.ModelResourceProvider +import net.minecraft.client.render.model.UnbakedModel +import net.minecraft.resource.ResourceManager +import net.minecraft.util.Identifier +import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.client.model.InterfaceModel + +/** + * @author shadowfacts + */ +class PhyModelProvider(resourceManager: ResourceManager) : ModelResourceProvider { + companion object { + val INTERFACE = Identifier(PhysicalConnectivity.MODID, "block/network_interface") + } + + override fun loadModelResource(resourceId: Identifier, context: ModelProviderContext): UnbakedModel? { + return when (resourceId) { + INTERFACE -> InterfaceModel + else -> null + } + } +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/client/model/InterfaceModel.kt b/src/main/kotlin/net/shadowfacts/phycon/client/model/InterfaceModel.kt new file mode 100644 index 0000000..cdd03c7 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/client/model/InterfaceModel.kt @@ -0,0 +1,158 @@ +package net.shadowfacts.phycon.client.model + +import net.minecraft.block.BlockState +import net.minecraft.client.render.model.* +import net.minecraft.client.texture.Sprite +import net.minecraft.client.util.SpriteIdentifier +import net.minecraft.util.Identifier +import net.minecraft.util.math.Direction +import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.block.FaceDeviceBlock +import java.util.Random +import java.util.function.Function + +/** + * @author shadowfacts + */ +object InterfaceModel: UnbakedModel, BakedModel { + + private val interfaceSideID = Identifier(PhysicalConnectivity.MODID, "block/interface_side") + private val interfaceCableStraightID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_straight") + private val interfaceCableCornerID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner") + private val interfaceCableCorner2ID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner_2") + private var interfaceSides = Array(6) { null } + private var interfaceCableStraight = Array(6) { null } + private var interfaceCableCorner = mutableMapOf() + private var interfaceCableCorner2 = mutableMapOf() + + override fun getModelDependencies(): Collection { + return listOf( + interfaceSideID, + interfaceCableStraightID, + interfaceCableCornerID, + interfaceCableCorner2ID, + ) + } + + override fun getTextureDependencies( + unbakedModelGetter: Function, + unresolvedTextureReferences: MutableSet> + ): Collection { + return listOf() + } + + override fun bake(loader: ModelLoader, textureGetter: Function, rotationContainer: ModelBakeSettings, modelId: Identifier): BakedModel { + listOf( + ModelRotation.X0_Y0, + ModelRotation.X180_Y0, + ModelRotation.X270_Y0, + ModelRotation.X90_Y0, + ModelRotation.X90_Y90, + ModelRotation.X90_Y270, + ).forEachIndexed { i, rot -> + interfaceSides[i] = loader.bake(interfaceSideID, rot) + interfaceCableStraight[i] = loader.bake(interfaceCableStraightID, rot) + } + + mapOf( + interfaceCableCorner to interfaceCableCornerID to ModelRotation.values().toList(), + interfaceCableCorner2 to interfaceCableCorner2ID to listOf( + ModelRotation.X0_Y0, + ModelRotation.X0_Y90, + ModelRotation.X0_Y180, + ModelRotation.X0_Y270, + ModelRotation.X180_Y0, + ModelRotation.X180_Y90, + ModelRotation.X180_Y180, + ModelRotation.X180_Y270, + ), + ).forEach { (k, rotations) -> + val (map, id) = k + map.clear() + rotations.forEach { rot -> + val model = loader.bake(id, rot) + if (model == null) map.remove(rot) + else map[rot] = model + } + } + + return this + } + + override fun getQuads(state: BlockState?, face: Direction?, random: Random): List { + if (state == null) return listOf() + val facing = state[FaceDeviceBlock.FACING] + val connection = state[FaceDeviceBlock.CABLE_CONNECTION] + + val sideQuads = interfaceSides[facing.ordinal]?.getQuads(state, face, random) ?: listOf() + val cableQuads = if (connection == facing.opposite) { + interfaceCableStraight[facing.ordinal]?.getQuads(state, face, random) ?: listOf() + } else { + val model = when (facing) { + Direction.DOWN -> when (connection) { + Direction.NORTH -> interfaceCableCorner[ModelRotation.X0_Y0] + Direction.EAST -> interfaceCableCorner[ModelRotation.X0_Y90] + Direction.SOUTH -> interfaceCableCorner[ModelRotation.X0_Y180] + Direction.WEST -> interfaceCableCorner[ModelRotation.X0_Y270] + else -> null + } + Direction.UP -> when (connection) { + Direction.NORTH -> interfaceCableCorner[ModelRotation.X180_Y180] + Direction.EAST -> interfaceCableCorner[ModelRotation.X180_Y270] + Direction.SOUTH -> interfaceCableCorner[ModelRotation.X180_Y0] + Direction.WEST -> interfaceCableCorner[ModelRotation.X180_Y90] + else -> null + } + Direction.NORTH -> when (connection) { + Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y0] + Direction.EAST -> interfaceCableCorner2[ModelRotation.X180_Y180] + Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y180] + Direction.WEST -> interfaceCableCorner2[ModelRotation.X0_Y0] + else -> null + } + Direction.SOUTH -> when (connection) { + Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y180] + Direction.WEST -> interfaceCableCorner2[ModelRotation.X180_Y0] + Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y0] + Direction.EAST -> interfaceCableCorner2[ModelRotation.X0_Y180] + else -> null + } + Direction.WEST -> when (connection) { + Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y270] + Direction.NORTH -> interfaceCableCorner2[ModelRotation.X180_Y90] + Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y90] + Direction.SOUTH -> interfaceCableCorner2[ModelRotation.X0_Y270] + else -> null + } + Direction.EAST -> when (connection) { + Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y90] + Direction.SOUTH -> interfaceCableCorner2[ModelRotation.X180_Y270] + Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y270] + Direction.NORTH -> interfaceCableCorner2[ModelRotation.X0_Y90] + else -> null + } + else -> null + } + model?.getQuads(state, face, random) ?: listOf() + } + + return sideQuads + cableQuads + } + + override fun useAmbientOcclusion() = true + + override fun hasDepth() = false + + override fun isSideLit() = false + + override fun isBuiltin() = false + + override fun getSprite(): Sprite { + return interfaceSides.first()!!.sprite + } + + override fun getTransformation() = null + + override fun getOverrides() = null + +} diff --git a/src/main/resources/assets/phycon/blockstates/network_interface.json b/src/main/resources/assets/phycon/blockstates/network_interface.json index 6d851e4..8c4e294 100644 --- a/src/main/resources/assets/phycon/blockstates/network_interface.json +++ b/src/main/resources/assets/phycon/blockstates/network_interface.json @@ -1,159 +1,8 @@ { "multipart": [ { - "apply": { "model": "phycon:block/cable_center" } - }, - { - "when": { "facing": "down" }, - "apply": { "model": "phycon:block/interface_side" } - }, - { - "when": { "facing": "up" }, - "apply": { "model": "phycon:block/interface_side", "x": 180 } - }, - { - "when": { "facing": "north" }, - "apply": { "model": "phycon:block/interface_side", "x": 270 } - }, - { - "when": { "facing": "south" }, - "apply": { "model": "phycon:block/interface_side", "x": 90 } - }, - { - "when": { "facing": "west" }, - "apply": { "model": "phycon:block/interface_side", "x": 90, "y": 90 } - }, - { - "when": { "facing": "east" }, - "apply": { "model": "phycon:block/interface_side", "x": 90, "y": 270 } - }, - - { - "when": { "cable_connection": "up", "facing": "down" }, - "apply": { "model": "phycon:block/interface_cable_straight" } - }, - { - "when": { "cable_connection": "down", "facing": "up" }, - "apply": { "model": "phycon:block/interface_cable_straight", "x": 180 } - }, - { - "when": { "cable_connection": "north", "facing": "south" }, - "apply": { "model": "phycon:block/interface_cable_straight", "x": 90 } - }, - { - "when": { "cable_connection": "south", "facing": "north" }, - "apply": { "model": "phycon:block/interface_cable_straight", "x": 270 } - }, - { - "when": { "cable_connection": "west", "facing": "east" }, - "apply": { "model": "phycon:block/interface_cable_straight", "x": 90, "y": 270 } - }, - { - "when": { "cable_connection": "east", "facing": "west" }, - "apply": { "model": "phycon:block/interface_cable_straight", "x": 90, "y": 90 } - }, - - { - "when": {"cable_connection": "north", "facing": "down"}, - "apply": { "model": "phycon:block/interface_cable_corner" } - }, - { - "when": {"cable_connection": "east", "facing": "down"}, - "apply": { "model": "phycon:block/interface_cable_corner", "y": 90 } - }, - { - "when": {"cable_connection": "south", "facing": "down"}, - "apply": { "model": "phycon:block/interface_cable_corner", "y": 180 } - }, - { - "when": {"cable_connection": "west", "facing": "down"}, - "apply": { "model": "phycon:block/interface_cable_corner", "y": 270 } - }, - - { - "when": {"cable_connection": "north", "facing": "up"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 180 } - }, - { - "when": {"cable_connection": "east", "facing": "up"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 270 } - }, - { - "when": {"cable_connection": "south", "facing": "up"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 180 } - }, - { - "when": {"cable_connection": "west", "facing": "up"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 90 } - }, - - { - "when": {"cable_connection": "down", "facing": "north"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 180 } - }, - { - "when": {"cable_connection": "up", "facing": "north"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 270 } - }, - { - "when": {"cable_connection": "west", "facing": "north"}, - "apply": { "model": "phycon:block/interface_cable_corner_2" } - }, - { - "when": {"cable_connection": "east", "facing": "north"}, - "apply": { "model": "phycon:block/interface_cable_corner_2", "x": 180, "y": 180 } - }, - - { - "when": {"cable_connection": "down", "facing": "south"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 90 } - }, - { - "when": {"cable_connection": "up", "facing": "south"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 180 } - }, - { - "when": {"cable_connection": "west", "facing": "south"}, - "apply": { "model": "phycon:block/interface_cable_corner_3" } - }, - { - "when": {"cable_connection": "east", "facing": "south"}, - "apply": { "model": "phycon:block/interface_cable_corner_3", "x": 180, "y": 180 } - }, - - { - - "when": {"cable_connection": "down", "facing": "west"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 90 } - }, - { - "when": {"cable_connection": "up", "facing": "west"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 270 } - }, - { - "when": {"cable_connection": "north", "facing": "west"}, - "apply": { "model": "phycon:block/interface_cable_corner_3", "y": 90 } - }, - { - "when": {"cable_connection": "south", "facing": "west"}, - "apply": { "model": "phycon:block/interface_cable_corner_3", "x": 180, "y": 270 } - }, - - { - "when": {"cable_connection": "down", "facing": "east"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 270 } - }, - { - "when": {"cable_connection": "up", "facing": "east"}, - "apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 90 } - }, - { - "when": {"cable_connection": "north", "facing": "east"}, - "apply": { "model": "phycon:block/interface_cable_corner_2", "y": 90 } - }, - { - "when": {"cable_connection": "south", "facing": "east"}, - "apply": { "model": "phycon:block/interface_cable_corner_2", "x": 180, "y": 270 } + "_comment": "see InterfaceModel", + "apply": { "model": "phycon:block/network_interface" } } ] } diff --git a/src/main/resources/assets/phycon/models/block/interface_cable_corner_3.json b/src/main/resources/assets/phycon/models/block/interface_cable_corner_3.json deleted file mode 100644 index 2dc43b1..0000000 --- a/src/main/resources/assets/phycon/models/block/interface_cable_corner_3.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "textures": { - "straight": "phycon:block/cable_straight_rotated", - "cap": "phycon:block/cable_cap_end", - "corner_down": "phycon:block/interface_cable_corner_l_up", - "corner_up": "phycon:block/interface_cable_corner_r" - }, - "elements": [ - { - "from": [0, 6, 6], - "to": [10, 10, 10], - "faces": { - "down": {"texture": "#corner_down"}, - "up": {"texture": "#corner_up"}, - "north": {"texture": "#straight"}, - "south": {"texture": "#straight"}, - "west": {"texture": "#cap", "cullface": "west"}, - "east": {"texture": "#straight"} - } - } - ] -} \ No newline at end of file