Skip to content

Commit

Permalink
Port to Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
opekope2 committed Dec 1, 2024
1 parent 8ef5a86 commit 0a90105
Show file tree
Hide file tree
Showing 28 changed files with 233 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

package opekope2.avm_staff.api

import dev.architectury.registry.registries.RegistrySupplier
import net.minecraft.block.Block
import net.minecraft.item.Item
import net.minecraft.particle.SimpleParticleType
import net.minecraftforge.registries.RegistryObject
import opekope2.avm_staff.api.IStaffModPlatform.Instance
import opekope2.avm_staff.api.item.CrownItem
import opekope2.avm_staff.api.item.StaffItem
Expand All @@ -38,7 +38,7 @@ interface IStaffModPlatform {
*
* @param settings The item settings to pass to the constructor
*/
fun staffItem(settings: Item.Settings, repairIngredient: RegistrySupplier<Item>?): StaffItem
fun staffItem(settings: Item.Settings, repairIngredient: RegistryObject<Item>?): StaffItem

/**
* Creates an item, which is rendered like a staff.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package opekope2.avm_staff.api.entity

import dev.architectury.extensions.network.EntitySpawnExtension
import dev.architectury.networking.NetworkManager
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.block.*
import net.minecraft.block.piston.PistonBehavior
import net.minecraft.client.option.GraphicsMode
Expand All @@ -32,14 +28,11 @@ import net.minecraft.entity.data.DataTracker
import net.minecraft.entity.projectile.ProjectileUtil
import net.minecraft.nbt.NbtCompound
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.listener.ClientPlayPacketListener
import net.minecraft.network.packet.Packet
import net.minecraft.particle.ParticleEffect
import net.minecraft.particle.ParticleType
import net.minecraft.registry.Registries
import net.minecraft.registry.RegistryKey
import net.minecraft.registry.RegistryKeys
import net.minecraft.server.network.EntityTrackerEntry
import net.minecraft.state.property.Properties.LIT
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.hit.HitResult
Expand All @@ -49,6 +42,9 @@ import net.minecraft.util.math.Vec3d
import net.minecraft.world.RaycastContext
import net.minecraft.world.World
import net.minecraft.world.event.GameEvent
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.entity.IEntityAdditionalSpawnData
import opekope2.avm_staff.content.EntityTypes
import opekope2.avm_staff.content.ParticleTypes
import opekope2.avm_staff.util.*
Expand All @@ -57,7 +53,7 @@ import java.util.*
/**
* Technical entity representing a part of a flame of a campfire staff.
*/
class CampfireFlameEntity : Entity, EntitySpawnExtension {
class CampfireFlameEntity : Entity, IEntityAdditionalSpawnData {
private var currentRelativeRight: Vec3d = Vec3d.ZERO
private var currentRelativeUp: Vec3d = Vec3d.ZERO

Expand Down Expand Up @@ -151,7 +147,7 @@ class CampfireFlameEntity : Entity, EntitySpawnExtension {
if (!world.isClient && age >= parameters.stepResolution) discard()
}

@Environment(EnvType.CLIENT)
@OnlyIn(Dist.CLIENT)
private fun spawnParticle(particleEffect: ParticleEffect, start: Vec3d, end: Vec3d) {
val particleOffset = (end - start) * random.nextDouble() +
currentRelativeRight * ((random.nextDouble() * 2 - 1) / rayResolution) +
Expand Down Expand Up @@ -184,7 +180,7 @@ class CampfireFlameEntity : Entity, EntitySpawnExtension {
}
}

@Environment(EnvType.CLIENT)
@OnlyIn(Dist.CLIENT)
private fun tickRayClient(start: Vec3d, end: Vec3d): HitResult.Type {
val blockHit = raycastBlock(start, end)
val entityHit = raycastEntity(start, end, false)
Expand Down Expand Up @@ -306,15 +302,12 @@ class CampfireFlameEntity : Entity, EntitySpawnExtension {

override fun getPistonBehavior() = PistonBehavior.IGNORE

override fun createSpawnPacket(entityTrackerEntry: EntityTrackerEntry): Packet<ClientPlayPacketListener> =
NetworkManager.createAddEntityPacket(this, entityTrackerEntry)

override fun saveAdditionalSpawnData(buf: PacketByteBuf) {
override fun writeSpawnData(buf: PacketByteBuf) {
parameters.write(buf)
buf.writeVarInt(shooter.id)
}

override fun loadAdditionalSpawnData(buf: PacketByteBuf) {
override fun readSpawnData(buf: PacketByteBuf) {
parameters = Parameters(buf)
shooter = world.getEntityById(buf.readVarInt()) as LivingEntity
}
Expand Down Expand Up @@ -392,7 +385,7 @@ class CampfireFlameEntity : Entity, EntitySpawnExtension {
get() = this == HitResult.Type.BLOCK

private val flameParticleRayResolution: Int
@Environment(EnvType.CLIENT)
@OnlyIn(Dist.CLIENT)
get() = when (clientOptions.graphicsMode.value) {
GraphicsMode.FABULOUS -> 6
GraphicsMode.FANCY -> 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package opekope2.avm_staff.api.item

import dev.architectury.registry.registries.RegistrySupplier
import net.minecraft.component.DataComponentTypes
import net.minecraft.entity.Entity
import net.minecraft.entity.EquipmentSlot
Expand All @@ -36,6 +35,7 @@ import net.minecraft.util.TypedActionResult
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
import net.minecraft.world.World
import net.minecraftforge.registries.RegistryObject
import opekope2.avm_staff.api.staff.StaffHandler
import opekope2.avm_staff.util.*

Expand All @@ -44,7 +44,7 @@ import opekope2.avm_staff.util.*
* Implementing loader-specific interfaces is highly recommended when extending the class to pass loader-specific
* functionality to [StaffHandler].
*/
abstract class StaffItem(settings: Settings, private val repairIngredientSupplier: RegistrySupplier<Item>?) :
abstract class StaffItem(settings: Settings, private val repairIngredientSupplier: RegistryObject<Item>?) :
Item(settings) {
override fun canRepair(stack: ItemStack, ingredient: ItemStack) =
repairIngredientSupplier != null && ingredient.isOf(repairIngredientSupplier.get())
Expand Down
8 changes: 4 additions & 4 deletions StaffMod/src/main/kotlin/opekope2/avm_staff/content/Blocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

package opekope2.avm_staff.content

import dev.architectury.registry.registries.RegistrySupplier
import net.minecraft.block.AbstractBlock
import net.minecraft.block.Block
import net.minecraft.block.enums.NoteBlockInstrument
import net.minecraft.block.piston.PistonBehavior
import net.minecraft.registry.RegistryKeys
import net.minecraft.sound.BlockSoundGroup
import net.minecraftforge.registries.ForgeRegistries
import net.minecraftforge.registries.RegistryObject
import opekope2.avm_staff.api.block.CrownBlock
import opekope2.avm_staff.api.block.WallCrownBlock
import opekope2.avm_staff.util.MOD_ID
Expand All @@ -33,9 +33,9 @@ import opekope2.avm_staff.util.RegistryUtil
/**
* Blocks added by AVM Staffs mod.
*/
object Blocks : RegistryUtil<Block>(MOD_ID, RegistryKeys.BLOCK) {
object Blocks : RegistryUtil<Block>(MOD_ID, ForgeRegistries.BLOCKS) {
private fun settings() = AbstractBlock.Settings.create()
private fun settings(block: RegistrySupplier<out Block>) = AbstractBlock.Settings.copy(block.get())
private fun settings(block: RegistryObject<out Block>) = AbstractBlock.Settings.copy(block.get())

/**
* Block registered as `avm_staff:crown_of_king_orange`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package opekope2.avm_staff.content

import net.minecraft.entity.EntityType
import net.minecraft.entity.SpawnGroup
import net.minecraft.registry.RegistryKeys
import net.minecraft.util.Identifier
import net.minecraftforge.registries.ForgeRegistries
import opekope2.avm_staff.api.entity.CakeEntity
import opekope2.avm_staff.api.entity.CampfireFlameEntity
import opekope2.avm_staff.api.entity.ImpactTntEntity
Expand All @@ -33,7 +33,7 @@ import kotlin.math.max
/**
* Entity types added by AVM Staffs mod.
*/
object EntityTypes : RegistryUtil<EntityType<*>>(MOD_ID, RegistryKeys.ENTITY_TYPE) {
object EntityTypes : RegistryUtil<EntityType<*>>(MOD_ID, ForgeRegistries.ENTITY_TYPES) {
/**
* Entity registered as `avm_staff:cake`
*/
Expand Down
25 changes: 18 additions & 7 deletions StaffMod/src/main/kotlin/opekope2/avm_staff/content/ItemGroups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

package opekope2.avm_staff.content

import dev.architectury.registry.CreativeTabRegistry
import dev.architectury.registry.registries.RegistrySupplier
import net.minecraft.item.ItemGroup
import net.minecraft.registry.RegistryKeys
import net.minecraft.text.Text
import net.minecraftforge.registries.RegistryObject
import opekope2.avm_staff.util.MOD_ID
import opekope2.avm_staff.util.RegistryUtil
import opekope2.avm_staff.util.mutableItemStackInStaff
Expand All @@ -35,12 +34,24 @@ object ItemGroups : RegistryUtil<ItemGroup>(MOD_ID, RegistryKeys.ITEM_GROUP) {
* Item group containing items added by Staff Mod.
*/
@JvmField
val AVM_STAFF_MOD_ITEMS: RegistrySupplier<ItemGroup> = ItemGroups.register("${MOD_ID}_items") {
CreativeTabRegistry.create(Text.translatable("itemGroup.${MOD_ID}_items")) {
Items.royalStaff.defaultStack.apply {
mutableItemStackInStaff = net.minecraft.item.Items.COMMAND_BLOCK.defaultStack
val AVM_STAFF_MOD_ITEMS: RegistryObject<ItemGroup> = ItemGroups.register("${MOD_ID}_items") {
ItemGroup.builder()
.displayName(Text.translatable("itemGroup.${MOD_ID}_items"))
.icon {
Items.royalStaff.defaultStack.apply {
mutableItemStackInStaff = net.minecraft.item.Items.COMMAND_BLOCK.defaultStack
}
}
}
.entries { _, entries ->
entries.add(Items.faintStaffRod)
entries.add(Items.faintRoyalStaffHead)
entries.add(Items.faintRoyalStaff)
entries.add(Items.royalStaff)
entries.add(Items.royalStaffIngredient)
entries.add(Items.crownOfKingOrange)
entries.add(Items.staffInfusionSmithingTemplate)
}
.build()
}

/**
Expand Down
23 changes: 8 additions & 15 deletions StaffMod/src/main/kotlin/opekope2/avm_staff/content/Items.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

package opekope2.avm_staff.content

import dev.architectury.registry.CreativeTabRegistry
import net.minecraft.component.DataComponentTypes
import net.minecraft.component.type.ToolComponent
import net.minecraft.item.Item
import net.minecraft.item.SmithingTemplateItem
import net.minecraft.registry.RegistryKeys
import net.minecraft.text.Text
import net.minecraft.util.Rarity
import net.minecraftforge.registries.ForgeRegistries
import opekope2.avm_staff.api.IStaffModPlatform
import opekope2.avm_staff.api.item.CrownItem
import opekope2.avm_staff.api.item.StaffItem
Expand All @@ -39,7 +39,7 @@ import opekope2.avm_staff.util.TagKeyUtil
/**
* Items added by AVM Staffs mod.
*/
object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
object Items : RegistryUtil<Item>(MOD_ID, ForgeRegistries.ITEMS) {
private fun settings() = Item.Settings()

/**
Expand All @@ -50,7 +50,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
IStaffModPlatform.crownItem(
Blocks.CROWN_OF_KING_ORANGE.get(),
Blocks.WALL_CROWN_OF_KING_ORANGE.get(),
settings().maxCount(1).rarity(Rarity.UNCOMMON).`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS)
settings().maxCount(1).rarity(Rarity.UNCOMMON)
)
}

Expand All @@ -67,7 +67,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
@JvmField
val FAINT_ROYAL_STAFF = register("faint_royal_staff") {
IStaffModPlatform.itemWithStaffRenderer(
settings().maxCount(1).rarity(Rarity.RARE).`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS)
settings().maxCount(1).rarity(Rarity.RARE)
)
}

Expand All @@ -83,7 +83,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
*/
@JvmField
val FAINT_ROYAL_STAFF_HEAD = register("faint_royal_staff_head") {
Item(settings().maxCount(16).rarity(Rarity.RARE).`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS))
Item(settings().maxCount(16).rarity(Rarity.RARE))
}

/**
Expand All @@ -98,7 +98,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
*/
@JvmField
val FAINT_STAFF_ROD = register("faint_staff_rod") {
Item(settings().`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS))
Item(settings())
}

/**
Expand All @@ -115,8 +115,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
val ROYAL_STAFF = register("royal_staff") {
IStaffModPlatform.staffItem(
settings().maxCount(1).rarity(Rarity.EPIC).attributeModifiers(StaffHandler.Fallback.ATTRIBUTE_MODIFIERS)
.maxDamage(5179).component(DataComponentTypes.TOOL, ToolComponent(listOf(), 1f, 1))
.`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS),
.maxDamage(5179).component(DataComponentTypes.TOOL, ToolComponent(listOf(), 1f, 1)),
ROYAL_STAFF_INGREDIENT
)
}
Expand All @@ -133,7 +132,7 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
*/
@JvmField
val ROYAL_STAFF_INGREDIENT = register("royal_staff_ingredient") {
Item(settings().`arch$tab`(ItemGroups.AVM_STAFF_MOD_ITEMS))
Item(settings())
}

/**
Expand Down Expand Up @@ -168,12 +167,6 @@ object Items : RegistryUtil<Item>(MOD_ID, RegistryKeys.ITEM) {
@JvmName("staffInfusionSmithingTemplate")
get() = STAFF_INFUSION_SMITHING_TEMPLATE.get()

override fun register() {
super.register()
// Because SmithingTemplateItem doesn't take Item.Settings in its constructor
CreativeTabRegistry.append(ItemGroups.AVM_STAFF_MOD_ITEMS, STAFF_INFUSION_SMITHING_TEMPLATE)
}

/**
* Item tags added by AVM Staffs mod.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ package opekope2.avm_staff.content
import net.minecraft.client.particle.ParticleManager
import net.minecraft.particle.ParticleType
import net.minecraft.particle.SimpleParticleType
import net.minecraft.registry.RegistryKeys
import net.minecraftforge.registries.ForgeRegistries
import opekope2.avm_staff.api.IStaffModPlatform
import opekope2.avm_staff.util.MOD_ID
import opekope2.avm_staff.util.RegistryUtil

/**
* Particle types added by AVM Staffs mod.
*/
object ParticleTypes : RegistryUtil<ParticleType<*>>(MOD_ID, RegistryKeys.PARTICLE_TYPE) {
object ParticleTypes : RegistryUtil<ParticleType<*>>(MOD_ID, ForgeRegistries.PARTICLE_TYPES) {
/**
* Particle registered as `avm_staff:flame`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

package opekope2.avm_staff.content

import net.minecraft.registry.RegistryKeys
import net.minecraft.sound.SoundEvent
import net.minecraftforge.registries.ForgeRegistries
import opekope2.avm_staff.util.MOD_ID
import opekope2.avm_staff.util.RegistryUtil

/**
* Sound events added by AVM Staffs mod.
*/
object SoundEvents : RegistryUtil<SoundEvent>(MOD_ID, RegistryKeys.SOUND_EVENT) {
object SoundEvents : RegistryUtil<SoundEvent>(MOD_ID, ForgeRegistries.SOUND_EVENTS) {
/**
* Sound event registered as `avm_staff:entity.cake.splash`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@

package opekope2.avm_staff.internal.event_handler

import dev.architectury.event.events.client.ClientTickEvent
import dev.architectury.event.events.common.InteractionEvent
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.util.Hand
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickEmpty
import opekope2.avm_staff.api.item.StaffItem
import opekope2.avm_staff.internal.networking.c2s.play.AttackC2SPacket
import thedarkcolour.kotlinforforge.forge.FORGE_BUS

@Environment(EnvType.CLIENT)
object ClientEventHandlers : InteractionEvent.ClientLeftClickAir {
@OnlyIn(Dist.CLIENT)
object ClientEventHandlers {
init {
ClientTickEvent.CLIENT_POST.register(KeyBindingHandler)
InteractionEvent.CLIENT_LEFT_CLICK_AIR.register(this)
FORGE_BUS.addListener(::click)
}

override fun click(player: PlayerEntity, hand: Hand) {
private fun click(event: LeftClickEmpty) {
val player = event.entity
val hand = event.hand
val staffStack = player.getStackInHand(hand)
val staffItem = staffStack.item as? StaffItem ?: return

Expand Down
Loading

0 comments on commit 0a90105

Please sign in to comment.