Skip to content

Commit

Permalink
Merge branch 'dev' into release/1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
opekope2 committed Oct 15, 2024
2 parents 66f4539 + c686e59 commit 2a01933
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 0 additions & 4 deletions FabricMod/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion NeoForgeMod/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
)
Expand All @@ -212,6 +211,7 @@ object StaffRenderer {
overlay: Int,
part: StaffRendererPartComponent
) {
val itemRenderer = MinecraftClient.getInstance().itemRenderer
val model = safeGetModel(staffStack, part)

itemRenderer.renderItem(
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +35,10 @@ internal abstract class PacketRegistrar<TPacket : IPacket>(
private val codec = PacketCodec.of(IPacket::write, packetConstructor)

fun registerReceiver(receiver: NetworkManager.NetworkReceiver<TPacket>) {
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockPos>, val rawIds: List<Int>) : IS2CPacket {
Expand Down Expand Up @@ -86,12 +85,11 @@ internal class MassDestructionS2CPacket(val positions: List<BlockPos>, 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 ->
Expand All @@ -106,24 +104,23 @@ internal class MassDestructionS2CPacket(val positions: List<BlockPos>, 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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<FirePellet>()

private val particleManager by lazy { MinecraftClient.getInstance().particleManager }

init {
TickEvent.SERVER_PRE.register(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ internal class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
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)
}
Expand Down Expand Up @@ -176,6 +177,5 @@ internal class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
.addDefault(EntityAttributes.PLAYER_ENTITY_INTERACTION_RANGE)
.addDefault(EntityAttributes.PLAYER_BLOCK_INTERACTION_RANGE)
.build()
private val particleManager by lazy { MinecraftClient.getInstance().particleManager }
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 2a01933

Please sign in to comment.