Skip to content

Commit

Permalink
Migrate staff block handlers to the new model loading system
Browse files Browse the repository at this point in the history
  • Loading branch information
opekope2 committed Feb 15, 2024
1 parent 853550e commit 938cd8a
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,19 @@ import net.minecraft.entity.attribute.EntityAttribute
import net.minecraft.entity.attribute.EntityAttributeModifier
import net.minecraft.entity.attribute.EntityAttributes
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.BlockItem
import net.minecraft.item.ItemStack
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import net.minecraft.world.World
import net.minecraft.world.WorldEvents
import opekope2.avm_staff.api.item.IDisablesShield
import opekope2.avm_staff.api.item.StaffItemHandler
import opekope2.avm_staff.api.item.model.ReloadableSingleBakedModelProvider
import opekope2.avm_staff.util.*
import opekope2.avm_staff.util.attackDamage
import opekope2.avm_staff.util.equipTime
import opekope2.avm_staff.util.itemInStaff
import java.util.*

class AnvilHandler(anvilItem: BlockItem, private val damagedStackFactory: () -> ItemStack?) : StaffItemHandler(),
IDisablesShield {
override val itemModelProvider = ReloadableSingleBakedModelProvider {
anvilItem.block.defaultState.getTransformedModel(TRANSFORM_INTO_STAFF)
}

class AnvilHandler(private val damagedStackFactory: () -> ItemStack?) : StaffItemHandler(), IDisablesShield {
override fun attackEntity(
staffStack: ItemStack,
world: World,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import com.google.common.collect.Multimap
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.client.render.block.entity.BellBlockEntityRenderer
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.render.model.BakedQuad
import net.minecraft.client.render.model.BasicBakedModel
import net.minecraft.client.render.model.*
import net.minecraft.client.render.model.json.ModelOverrideList
import net.minecraft.client.render.model.json.ModelTransformation
import net.minecraft.client.render.model.json.Transformation
import net.minecraft.client.texture.Sprite
import net.minecraft.client.util.SpriteIdentifier
import net.minecraft.entity.Entity
import net.minecraft.entity.EquipmentSlot
import net.minecraft.entity.LivingEntity
Expand All @@ -42,19 +41,23 @@ import net.minecraft.sound.SoundCategory
import net.minecraft.sound.SoundEvents
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import net.minecraft.util.Identifier
import net.minecraft.util.TypedActionResult
import net.minecraft.util.math.Direction
import net.minecraft.world.World
import opekope2.avm_staff.api.item.StaffItemHandler
import opekope2.avm_staff.api.item.model.IReloadableBakedModelProvider
import opekope2.avm_staff.api.item.model.IStaffItemBakedModel
import opekope2.avm_staff.api.item.model.IStaffItemUnbakedModel
import opekope2.avm_staff.api.item.model.StaffItemBakedModel
import opekope2.avm_staff.util.attackDamage
import opekope2.avm_staff.util.attackSpeed
import opekope2.avm_staff.util.getBakedQuads
import opekope2.avm_staff.util.transform
import org.joml.Vector3f
import java.util.function.Function
import java.util.function.Supplier

class BellBlockHandler : StaffItemHandler() {
override val itemModelProvider: IReloadableBakedModelProvider = ReloadableBellModelProvider()

override fun use(
staffStack: ItemStack,
world: World,
Expand Down Expand Up @@ -94,18 +97,23 @@ class BellBlockHandler : StaffItemHandler() {
}

@Environment(EnvType.CLIENT)
private class ReloadableBellModelProvider : IReloadableBakedModelProvider {
private lateinit var model: BakedModel

private val bellSprite: Sprite
get() = BellBlockEntityRenderer.BELL_BODY_TEXTURE.sprite
private class BellUnbakedModel : IStaffItemUnbakedModel {
override fun getModelDependencies() = setOf<Identifier>()

override fun getModel(staffStack: ItemStack): BakedModel = model
override fun setParents(modelLoader: Function<Identifier, UnbakedModel>?) {
}

override fun reload() {
override fun bake(
baker: Baker,
textureGetter: Function<SpriteIdentifier, Sprite>,
rotationContainer: ModelBakeSettings,
modelId: Identifier,
transformation: Transformation
): IStaffItemBakedModel {
val bellSprite = textureGetter.apply(BellBlockEntityRenderer.BELL_BODY_TEXTURE)
val bellModel = BellBlockEntityRenderer.getTexturedModelData().createModel().getChild("bell_body")
val quads = bellModel.getBakedQuads(bellSprite, transformation)
model = BasicBakedModel(
val quads = bellModel.getBakedQuads(bellSprite, bellTransformation)
val baked = BasicBakedModel(
quads,
createEmptyFaceQuads(),
true,
Expand All @@ -115,13 +123,15 @@ class BellBlockHandler : StaffItemHandler() {
ModelTransformation.NONE,
ModelOverrideList.EMPTY
)

return StaffItemBakedModel(baked.transform(null, transformation, textureGetter))
}

private companion object {
private val transformation = Transformation(
private val bellTransformation = Transformation(
Vector3f(),
Vector3f((9f - 7f) / 9f / 2f, (22f - 3f) / 16f, (9f - 7f) / 9f / 2f),
Vector3f(7f / 9f)
Vector3f(-3.5f / 9f, -4f / 9f, -3.5f / 9f),
Vector3f(16f / 9f)
)

private fun createEmptyFaceQuads(): Map<Direction, List<BakedQuad>> = mapOf(
Expand All @@ -135,12 +145,16 @@ class BellBlockHandler : StaffItemHandler() {
}
}

private companion object {
companion object {
private val ATTRIBUTE_MODIFIERS = ImmutableMultimap.of(
EntityAttributes.GENERIC_ATTACK_DAMAGE,
attackDamage(8.0),
EntityAttributes.GENERIC_ATTACK_SPEED,
attackSpeed(1.5)
)

val modelSupplierFactory: Supplier<Supplier<out IStaffItemUnbakedModel>> = Supplier {
Supplier(::BellUnbakedModel)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package opekope2.avm_staff.internal.staff_item_handler

import net.minecraft.block.Blocks
import net.minecraft.entity.LivingEntity
import net.minecraft.item.BoneMealItem
import net.minecraft.item.ItemStack
Expand All @@ -30,15 +29,8 @@ import net.minecraft.world.World
import net.minecraft.world.WorldEvents
import net.minecraft.world.event.GameEvent
import opekope2.avm_staff.api.item.StaffItemHandler
import opekope2.avm_staff.api.item.model.ReloadableSingleBakedModelProvider
import opekope2.avm_staff.util.TRANSFORM_INTO_STAFF
import opekope2.avm_staff.util.getTransformedModel

class BoneBlockHandler : StaffItemHandler() {
override val itemModelProvider = ReloadableSingleBakedModelProvider {
Blocks.BONE_BLOCK.defaultState.getTransformedModel(TRANSFORM_INTO_STAFF)
}

override fun useOnBlock(
staffStack: ItemStack,
world: World,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ import com.google.common.collect.Multimap
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.block.AbstractFurnaceBlock
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.client.MinecraftClient
import net.minecraft.client.render.block.BlockModels
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.render.model.Baker
import net.minecraft.client.render.model.ModelBakeSettings
import net.minecraft.client.render.model.UnbakedModel
import net.minecraft.client.render.model.json.Transformation
import net.minecraft.client.texture.Sprite
import net.minecraft.client.util.SpriteIdentifier
import net.minecraft.entity.EquipmentSlot
import net.minecraft.entity.ItemEntity
import net.minecraft.entity.LivingEntity
Expand All @@ -34,7 +42,6 @@ import net.minecraft.entity.attribute.EntityAttributeModifier
import net.minecraft.entity.attribute.EntityAttributes
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.inventory.SingleStackInventory
import net.minecraft.item.BlockItem
import net.minecraft.item.ItemStack
import net.minecraft.particle.ParticleTypes
import net.minecraft.recipe.AbstractCookingRecipe
Expand All @@ -43,27 +50,25 @@ import net.minecraft.server.world.ServerWorld
import net.minecraft.sound.SoundCategory
import net.minecraft.sound.SoundEvent
import net.minecraft.util.Hand
import net.minecraft.util.Identifier
import net.minecraft.util.TypedActionResult
import net.minecraft.util.math.Box
import net.minecraft.world.World
import opekope2.avm_staff.api.item.StaffItemHandler
import opekope2.avm_staff.api.item.model.IReloadableBakedModelProvider
import opekope2.avm_staff.api.item.model.IStaffItemBakedModel
import opekope2.avm_staff.api.item.model.IStaffItemUnbakedModel
import opekope2.avm_staff.mixin.IAbstractFurnaceBlockEntityMixin
import opekope2.avm_staff.util.*
import java.util.function.Function
import java.util.function.Supplier
import kotlin.jvm.optionals.getOrNull

class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
furnaceItem: BlockItem,
private val recipeType: RecipeType<TRecipe>,
private val smeltSound: SoundEvent
) : StaffItemHandler() {
override val maxUseTime = 72000

override val itemModelProvider: IReloadableBakedModelProvider = ReloadableFurnaceModelProvider(
furnaceItem.block.defaultState.with(AbstractFurnaceBlock.LIT, true),
furnaceItem.block.defaultState
)

override fun use(
staffStack: ItemStack,
world: World,
Expand Down Expand Up @@ -117,7 +122,7 @@ class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
user: LivingEntity,
world: World
): ItemEntity? {
val smeltingPosition = user.eyePos + user.rotationVector.normalize() * 1.75
val smeltingPosition = user.approximateStaffItemPosition
val items = world.getEntitiesByClass(ItemEntity::class.java, SMELTING_VOLUME.offset(smeltingPosition)) { true }
val closest = items.minByOrNull { (smeltingPosition - it.pos).lengthSquared() }
return closest
Expand Down Expand Up @@ -177,20 +182,41 @@ class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
}

@Environment(EnvType.CLIENT)
private class ReloadableFurnaceModelProvider(private val litState: BlockState, private val unlitState: BlockState) :
IReloadableBakedModelProvider {
private lateinit var litModel: BakedModel
private lateinit var unlitModel: BakedModel
private class FurnaceUnbakedModel(private val unlitState: BlockState, private val litState: BlockState) :
IStaffItemUnbakedModel {
private val litStateId = BlockModels.getModelId(litState)
private val unlitStateId = BlockModels.getModelId(unlitState)
private val dependencies = setOf(litStateId, unlitStateId)

override fun getModelDependencies() = dependencies

override fun setParents(modelLoader: Function<Identifier, UnbakedModel>?) {
}

override fun bake(
baker: Baker,
textureGetter: Function<SpriteIdentifier, Sprite>,
rotationContainer: ModelBakeSettings,
modelId: Identifier,
transformation: Transformation
): IStaffItemBakedModel? {
val unlitModel = baker.bake(unlitStateId, rotationContainer) ?: return null
val litModel = baker.bake(litStateId, rotationContainer) ?: return null

return FurnaceBakedModel(
unlitModel.transform(unlitState, transformation, textureGetter),
litModel.transform(litState, transformation, textureGetter)
)
}
}

@Environment(EnvType.CLIENT)
private class FurnaceBakedModel(private val unlitModel: BakedModel, private val litModel: BakedModel) :
BakedModel by unlitModel, IStaffItemBakedModel {
override fun getModel(staffStack: ItemStack): BakedModel {
return if (staffStack.nbt?.getBoolean(LIT_KEY) == true) litModel
else unlitModel
}

override fun reload() {
litModel = litState.getTransformedModel(TRANSFORM_INTO_STAFF)
unlitModel = unlitState.getTransformedModel(TRANSFORM_INTO_STAFF)
}
}

private class ItemEntityInventory(private val itemEntity: ItemEntity) : SingleStackInventory {
Expand All @@ -205,7 +231,7 @@ class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
override fun asBlockEntity(): Nothing = throw UnsupportedOperationException()
}

private companion object {
companion object {
private const val LIT_KEY = "Lit"
private const val BURN_TIME_KEY = "BurnTime"
private val SMELTING_VOLUME = Box(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5).contract(0.25 / 2)
Expand All @@ -215,5 +241,14 @@ class FurnaceHandler<TRecipe : AbstractCookingRecipe>(
EntityAttributes.GENERIC_ATTACK_SPEED,
attackSpeed(2.0)
)

fun getModelSupplierFactory(furnaceBlock: Block): Supplier<Supplier<out IStaffItemUnbakedModel>> = Supplier {
Supplier {
FurnaceUnbakedModel(
furnaceBlock.defaultState,
furnaceBlock.defaultState.with(AbstractFurnaceBlock.LIT, true)
)
}
}
}
}
Loading

0 comments on commit 938cd8a

Please sign in to comment.