From daa6c09b5265f828e5dd008807d1e3adf1dedd56 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 28 Jan 2025 21:34:11 +0900 Subject: [PATCH] more on itemfileref --- .../modulebasegame/gameitems/ItemFileRef.kt | 41 +++++++++++++++++++ .../torvald/terrarum/savegame/VirtualDisk.kt | 3 ++ 2 files changed, 44 insertions(+) diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt index 5f88100dc..b52d99bf9 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt @@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.gameitems import com.badlogic.gdx.Gdx import net.torvald.terrarum.App import net.torvald.terrarum.ModMgr +import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID import java.io.File @@ -60,6 +61,19 @@ open class ItemFileRef(originalID: ItemID) : GameItem(originalID) { */ open var mediumIdentifier = "" + /** + * How this item should look like on inventory/in world. Used when creation of subclass is not possible. + */ + open var morphItem = "" + + /** + * Fully-qualified classname. Class to be called when this item is used by hitting "interaction" key. + * Used when creation of subclass is not possible. + * + * If specified, the class must implement FileRefItemPrimaryUseHandler + */ + open var useItemHandler = "" + override var baseMass = 1.0 override var baseToolSize: Double? = null @@ -77,4 +91,31 @@ open class ItemFileRef(originalID: ItemID) : GameItem(originalID) { File(App.saveSharedDir + "/$refPath") else ModMgr.getFile(refModuleName, refPath) + + @Transient private var classCache: FileRefItemPrimaryUseHandler? = null + + override fun startPrimaryUse(actor: ActorWithBody, delta: Float): Long { + return if (useItemHandler.isNotBlank()) { + try { + if (classCache == null) { + val newClass = Class.forName(useItemHandler) + val newClassConstructor = newClass.getConstructor(/* no args defined */) + val newClassInstance = newClassConstructor.newInstance(/* no args defined */) + classCache = (newClassInstance as FileRefItemPrimaryUseHandler) + } + classCache!!.use(this) + } + catch (e: Throwable) { + e.printStackTrace() + super.startPrimaryUse(actor, delta) + } + } + else super.startPrimaryUse(actor, delta) + } +} + +interface FileRefItemPrimaryUseHandler { + /** If this item must be consumed, return 1; if this item must not be consumed, return 0; if this item + * was failed to be used (for some reason), return -1. */ + fun use(item: ItemFileRef): Long } \ No newline at end of file diff --git a/src/net/torvald/terrarum/savegame/VirtualDisk.kt b/src/net/torvald/terrarum/savegame/VirtualDisk.kt index 0f747adcc..bc1e43dd0 100644 --- a/src/net/torvald/terrarum/savegame/VirtualDisk.kt +++ b/src/net/torvald/terrarum/savegame/VirtualDisk.kt @@ -317,6 +317,8 @@ object VDFileID { const val BODYPART_TO_ENTRY_MAP = -1025L const val BODYPARTGLOW_TO_ENTRY_MAP = -1026L const val BODYPARTEMISSIVE_TO_ENTRY_MAP = -1027L + + const val BLOB_REDEEMED_CODES = -2147483648L } fun diskIDtoReadableFilename(id: EntryID, saveKind: Int?): String = when (id) { @@ -354,6 +356,7 @@ fun diskIDtoReadableFilename(id: EntryID, saveKind: Int?): String = when (id) { VDFileID.BODYPART_TO_ENTRY_MAP -> "bodypart-to-entry.map" VDFileID.BODYPARTGLOW_TO_ENTRY_MAP -> "bodypartglow-to-entry.map" VDFileID.BODYPARTEMISSIVE_TO_ENTRY_MAP -> "bodypartemsv-to-entry.map" + VDFileID.BLOB_REDEEMED_CODES -> "blob: redeemed codes" in 1..65535 -> if (saveKind == PLAYER_DATA) "bodypart #$id.tga.gz"