diff --git a/FabricMod/src/main/kotlin/opekope2/avm_staff/internal/fabric/StaffMod.kt b/FabricMod/src/main/kotlin/opekope2/avm_staff/internal/fabric/StaffMod.kt index 6100055a8..055fbab65 100644 --- a/FabricMod/src/main/kotlin/opekope2/avm_staff/internal/fabric/StaffMod.kt +++ b/FabricMod/src/main/kotlin/opekope2/avm_staff/internal/fabric/StaffMod.kt @@ -38,11 +38,17 @@ import opekope2.avm_staff.api.item.CrownItem import opekope2.avm_staff.api.item.StaffItem import opekope2.avm_staff.api.item.renderer.StaffRenderer import opekope2.avm_staff.internal.fabric.item.FabricStaffItem +import opekope2.avm_staff.internal.subscribeToClientEvents @Suppress("unused") object StaffMod : ModInitializer, IStaffModPlatform { override fun onInitialize() { AttackEntityCallback.EVENT.register(::dispatchStaffEntityAttack) + + if (FabricLoader.getInstance().environmentType == EnvType.CLIENT) { + // Needs to be called before client entry point because of Fabric Loader and Architectury API + subscribeToClientEvents() + } } @Suppress("UNUSED_PARAMETER") diff --git a/FabricMod/src/main/resources/fabric.mod.json b/FabricMod/src/main/resources/fabric.mod.json index 16f1d505a..c28095b3e 100644 --- a/FabricMod/src/main/resources/fabric.mod.json +++ b/FabricMod/src/main/resources/fabric.mod.json @@ -77,10 +77,6 @@ "adapter": "kotlin", "value": "opekope2.avm_staff.internal.InitializerKt::registerSmithingTableTextures" }, - { - "adapter": "kotlin", - "value": "opekope2.avm_staff.internal.InitializerKt::subscribeToClientEvents" - }, { "adapter": "kotlin", "value": "opekope2.avm_staff.internal.staff.handler.VanillaStaffHandlersKt::registerVanillaStaffItemRenderers" diff --git a/NeoForgeMod/build.gradle.kts b/NeoForgeMod/build.gradle.kts index 62f6e82fc..3aba56242 100644 --- a/NeoForgeMod/build.gradle.kts +++ b/NeoForgeMod/build.gradle.kts @@ -62,7 +62,9 @@ dependencies { common(project(":StaffMod", configuration = "namedElements")) { isTransitive = false } shadowBundle(project(":StaffMod", configuration = "transformProductionNeoForge")) { isTransitive = false } - implementation(libs.kotlinforforge.neoforge) + implementation(libs.kotlinforforge.neoforge) { + exclude("net.neoforged.fancymodloader", "loader") + } } tasks { diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/BlockStateStaffItemRenderer.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/BlockStateStaffItemRenderer.kt index 21b297ddf..4932cdf4e 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/BlockStateStaffItemRenderer.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/BlockStateStaffItemRenderer.kt @@ -49,9 +49,6 @@ class BlockStateStaffItemRenderer(blockState: BlockState) : IStaffItemRenderer { ) = renderBlockState(blockStateId, blockItem, matrices, vertexConsumers, light, overlay) companion object { - private val itemRenderer by lazy { MinecraftClient.getInstance().itemRenderer } - private val modelManager by lazy { MinecraftClient.getInstance().bakedModelManager } - /** * Renders a [BlockState]. * @@ -93,6 +90,8 @@ class BlockStateStaffItemRenderer(blockState: BlockState) : IStaffItemRenderer { light: Int, overlay: Int ) { + val itemRenderer = MinecraftClient.getInstance().itemRenderer + val modelManager = MinecraftClient.getInstance().bakedModelManager val model = modelManager.getModel(blockStateId) itemRenderer.renderItem( blockStateItem, ModelTransformationMode.NONE, false, matrices, vertexConsumers, light, overlay, model diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/StaffRenderer.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/StaffRenderer.kt index 6a02fa4c2..6da562e53 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/StaffRenderer.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/api/item/renderer/StaffRenderer.kt @@ -39,9 +39,6 @@ import kotlin.jvm.optionals.getOrNull */ @Environment(EnvType.CLIENT) object StaffRenderer { - private val bakedModelManager by lazy { MinecraftClient.getInstance().bakedModelManager } - private val itemRenderer by lazy { MinecraftClient.getInstance().itemRenderer } - /** * Renders the staff. * @@ -197,7 +194,9 @@ object StaffRenderer { if (staffItemRenderer != null) { staffItemRenderer.renderItemInStaff(staffStack, mode, matrices, vertexConsumers, light, overlay) } else { - val model = bakedModelManager.missingModel + val itemRenderer = MinecraftClient.getInstance().itemRenderer + val model = MinecraftClient.getInstance().bakedModelManager.missingModel + itemRenderer.renderItem( itemStackInStaff, ModelTransformationMode.NONE, false, matrices, vertexConsumers, light, overlay, model ) @@ -212,6 +211,7 @@ object StaffRenderer { overlay: Int, part: StaffRendererPartComponent ) { + val itemRenderer = MinecraftClient.getInstance().itemRenderer val model = safeGetModel(staffStack, part) itemRenderer.renderItem( @@ -220,12 +220,14 @@ object StaffRenderer { } private fun safeGetModel(staffStack: ItemStack, part: StaffRendererPartComponent): BakedModel { + val itemRenderer = MinecraftClient.getInstance().itemRenderer + staffStack[staffRendererPartComponentType.get()] = part val model = itemRenderer.getModel(staffStack, null, null, 0) staffStack.remove(staffRendererPartComponentType.get()) // Prevent StackOverflowError if an override is missing return if (!model.isBuiltin) model - else bakedModelManager.missingModel + else MinecraftClient.getInstance().bakedModelManager.missingModel } } diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/PacketRegistrar.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/PacketRegistrar.kt index 6af7bb2c6..77c86bf1b 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/PacketRegistrar.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/PacketRegistrar.kt @@ -19,6 +19,8 @@ package opekope2.avm_staff.internal.networking import dev.architectury.networking.NetworkManager +import dev.architectury.platform.Platform +import dev.architectury.utils.Env import net.minecraft.network.PacketByteBuf import net.minecraft.network.codec.PacketCodec import net.minecraft.network.packet.CustomPayload @@ -33,6 +35,10 @@ internal abstract class PacketRegistrar( private val codec = PacketCodec.of(IPacket::write, packetConstructor) fun registerReceiver(receiver: NetworkManager.NetworkReceiver) { - NetworkManager.registerReceiver(side, payloadId, codec, receiver) + if (side == NetworkManager.Side.S2C && Platform.getEnvironment() == Env.SERVER) { + NetworkManager.registerS2CPayloadType(payloadId, codec) + } else { + NetworkManager.registerReceiver(side, payloadId, codec, receiver) + } } } diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/s2c/play/MassDestructionS2CPacket.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/s2c/play/MassDestructionS2CPacket.kt index 87a6bca9c..0da2672bb 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/s2c/play/MassDestructionS2CPacket.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/networking/s2c/play/MassDestructionS2CPacket.kt @@ -36,7 +36,6 @@ import opekope2.avm_staff.mixin.IParticleManagerAccessor import opekope2.avm_staff.util.MOD_ID import opekope2.avm_staff.util.countingSort import java.util.function.IntSupplier -import kotlin.math.max import kotlin.math.sqrt internal class MassDestructionS2CPacket(val positions: List, val rawIds: List) : IS2CPacket { @@ -86,12 +85,11 @@ internal class MassDestructionS2CPacket(val positions: List, val rawId Identifier.of(MOD_ID, "mass_destruction"), ::MassDestructionS2CPacket ) { - private val MAX_PARTICLES = IParticleManagerAccessor.maxParticleCount() / (4 * 4 * 4) - private const val MAX_SOUNDS = 128 - const val MAX_DATA_IN_PACKET = 1024 * 1024 override fun receive(packet: MassDestructionS2CPacket, context: NetworkManager.PacketContext) { + val maxParticles = IParticleManagerAccessor.maxParticleCount() / (4 * 4 * 4) + val maxSounds = 128 val world = context.player.entityWorld val playerPos = context.player.pos val blockBrokenEvents = Array(packet.positions.size) { i -> @@ -106,24 +104,23 @@ internal class MassDestructionS2CPacket(val positions: List, val rawId } val sortedBlockBrokenEvents = countingSort(blockBrokenEvents, max) - val maxProcessableEvents = max(MAX_SOUNDS, MAX_PARTICLES) - - sortedBlockBrokenEvents.take(maxProcessableEvents).forEachIndexed { i, (pos, blockStateRawId) -> + sortedBlockBrokenEvents.take(maxSounds).forEach { (pos, blockStateRawId) -> + val blockState = Block.getStateFromRawId(blockStateRawId) + if (blockState.isAir) return@forEach + + val soundGroup = blockState.soundGroup + world.playSoundAtBlockCenter( + pos, + soundGroup.breakSound, + SoundCategory.BLOCKS, + (soundGroup.volume + 1.0f) / 2.0f, + soundGroup.pitch * 0.8f, + false + ) + } + sortedBlockBrokenEvents.take(maxParticles).asReversed().forEach { (pos, blockStateRawId) -> val blockState = Block.getStateFromRawId(blockStateRawId) - if (i < MAX_SOUNDS && !blockState.isAir) { - val soundGroup = blockState.soundGroup - world.playSoundAtBlockCenter( - pos, - soundGroup.breakSound, - SoundCategory.BLOCKS, - (soundGroup.volume + 1.0f) / 2.0f, - soundGroup.pitch * 0.8f, - false - ) - } - if (i < MAX_PARTICLES) { - world.addBlockBreakParticles(pos, blockState) - } + world.addBlockBreakParticles(pos, blockState) } } } diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/CampfireHandler.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/CampfireHandler.kt index 2f6e6da71..d72d5f272 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/CampfireHandler.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/CampfireHandler.kt @@ -115,6 +115,8 @@ internal class CampfireHandler( @Environment(EnvType.CLIENT) fun throwFlameParticles(user: LivingEntity, target: Vec3d, relativeRight: Vec3d, relativeUp: Vec3d) { val random = Random.create() + val particleManager = MinecraftClient.getInstance().particleManager + val origin = user.approximateStaffTipPosition for (i in 0..flameParticleCount) { @@ -253,18 +255,15 @@ internal class CampfireHandler( private const val FLAMETHROWER_CONE_RAYS = 16 private const val FLAMETHROWER_CONE_RAYS_TOTAL = FLAMETHROWER_CONE_RAYS * FLAMETHROWER_CONE_RAYS - private val graphicsModeOption by lazy { MinecraftClient.getInstance().options.graphicsMode } private val flameParticleCount: Int @Environment(EnvType.CLIENT) - get() = when (graphicsModeOption.value!!) { + get() = when (MinecraftClient.getInstance().options.graphicsMode.value!!) { GraphicsMode.FAST -> 4 * 4 GraphicsMode.FANCY -> 8 * 8 GraphicsMode.FABULOUS -> 16 * 16 } private val firePellets = mutableListOf() - private val particleManager by lazy { MinecraftClient.getInstance().particleManager } - init { TickEvent.SERVER_PRE.register(this) } diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/FurnaceHandler.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/FurnaceHandler.kt index 5ca9b653e..5f0acd1b2 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/FurnaceHandler.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff/handler/FurnaceHandler.kt @@ -126,6 +126,7 @@ internal class FurnaceHandler( val ry = Math.random() * 0.5 val rz = Math.random() * 0.25 - 0.25 / 2 + val particleManager = MinecraftClient.getInstance().particleManager particleManager.addParticle(ParticleTypes.FLAME, x + rx, y + ry, z + rz, 0.0, 0.0, 0.0) particleManager.addParticle(ParticleTypes.SMOKE, x + rx, y + ry, z + rz, 0.0, 0.0, 0.0) } @@ -176,6 +177,5 @@ internal class FurnaceHandler( .addDefault(EntityAttributes.PLAYER_ENTITY_INTERACTION_RANGE) .addDefault(EntityAttributes.PLAYER_BLOCK_INTERACTION_RANGE) .build() - private val particleManager by lazy { MinecraftClient.getInstance().particleManager } } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 02d4a3bd2..b10fe7ff0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] java = "21" # Don't forget to update *.mixins.json kotlin = "2.0.0" -staff-mod = "0.18.1" +staff-mod = "0.18.3" architectury-plugin = "3.4-SNAPSHOT" architectury-loom = "1.7-SNAPSHOT" yarn = "1.21+build.9"