Files
2006Scape/2006Scape Server/plugins/plugin/commands/SpawnItem.kt
T
Dark98 67147ef466 feat(commands): migrate item, npc, spellbook, and tele to plugins and add item/npc search
Migrate legacy hardcoded administrative and utility commands from Commands.java
to modular Kotlin event subscriber plugins, introduce dedicated item and NPC search
commands, and establish a permission-tiered command event hierarchy bounded by
Revision 377 cache specifications.

### Command Infrastructure
- CommandEvent.java:
  - Remove final modifier to allow permission-specific command event inheritance.
- ModeratorCommandEvent.java, AdminCommandEvent.java, DeveloperCommandEvent.java:
  - Add strongly typed event classes matching player permission tiers:
    Moderator (rights >= 1), Admin (rights >= 2), and Developer (rights == 3).
- Commands.java:
  - Dispatch permission-tiered command events across the event bus alongside switch handling.
  - Remove legacy switch implementations for ::item, ::npc, ::spellbook, and ::tele.

### Migrated Command Plugins
- SpawnItem.kt (::spawnitem, ::item):
  - Support both numeric IDs and fuzzy name resolution (exact, prefix, and substring).
  - Add quantity shorthand parsing for thousand (k), million (m), and billion (b) multipliers.
  - Support positional 'note' and 'noted' flags at the start or end of arguments; deprecate 'cert'.
  - Restrict valid IDs to Revision 377 cache bounds (0..7955).
  - Guard confirmation message against full inventory state when addItem() returns false.
- SpawnNpc.kt (::spawnnpc, ::npc):
  - Support both numeric IDs and fuzzy name resolution.
  - Validate against Revision 377 NPC ID range (0..3851), enabling ID 0 (Hans) spawning.
  - Calculate authentic combat, hitpoints, attack, and defence values with definitions fallback.
- Spellbook.kt (::spellbook):
  - Toggle between modern and ancient spellbooks when executed without arguments.
  - Support explicit 'modern'/'normal' and 'ancient'/'ancients' subcommands.
  - Cleanly reset autocast state via player.playerAssistant.resetAutocast().
- Teleport.kt (::teleport, ::tele):
  - Support coordinate teleportation with comma-tolerant token parsing (e.g. ::teleport 3222, 3218 0).
  - Add standard destination nicknames: lumby, fally, cammy, ardy, ape, and apeatoll.
  - Trigger authentic spell animations (modern 714/715, ancient 1979), graphics (308/392), and teleport sound effects.

### New Search Commands
- SearchItem.kt (::searchitem, ::itemsearch):
  - Search items by name or ID and render results on scrollable interface 8134.
  - Optimize interface clear range (8146..8195) to preserve title and subtitle lines without redundant packets.
  - Format output consistently in @bla@ with @dre@ noted tags and @blu@ ID labels.
  - Cap query scan at Revision 377 MAX_ITEM_ID (7955).
- SearchNpc.kt (::searchnpc, ::npcsearch):
  - Search NPCs by name or ID with scrollable interface 8134 display.
  - Format output in @bla@ with @yel@ combat levels and @blu@ ID labels.
  - Cap query scan at Revision 377 MAX_NPC_ID (3851).
2026-11-15 19:29:42 +00:00

175 lines
6.4 KiB
Kotlin

package plugin.commands
import com.rs2.event.EventContext
import com.rs2.event.EventSubscriber
import com.rs2.event.SubscribesTo
import com.rs2.event.impl.DeveloperCommandEvent
import com.rs2.game.players.Player
import org.apollo.cache.def.ItemDefinition
import java.text.NumberFormat
@SubscribesTo(DeveloperCommandEvent::class)
class SpawnItem : EventSubscriber<DeveloperCommandEvent> {
companion object {
/**
* Revision 377 maximum item ID (7,956 items in cache: IDs 0..7955).
*/
private const val MAX_ITEM_ID = 7955
private fun parseAmount(str: String): Int? {
val clean = str.trim().lowercase()
if (clean.isEmpty()) return null
val multiplier = when {
clean.endsWith("b") -> 1_000_000_000L
clean.endsWith("m") -> 1_000_000L
clean.endsWith("k") -> 1_000L
else -> 1L
}
val numPart = if (multiplier > 1L) clean.dropLast(1) else clean
val num = numPart.toDoubleOrNull() ?: return null
if (num <= 0.0) return null
val total = (num * multiplier).toLong()
return total.coerceIn(1L, Int.MAX_VALUE.toLong()).toInt()
}
private fun findItemIdByName(name: String): Int {
var exact = -1
var prefix = -1
var substring = -1
val count = ItemDefinition.count().coerceAtMost(MAX_ITEM_ID + 1)
for (id in 0 until count) {
val def = ItemDefinition.lookup(id) ?: continue
val defName = def.name ?: continue
if (defName.isEmpty() || defName.equals("null", ignoreCase = true)) continue
if (defName.equals(name, ignoreCase = true)) {
if (!def.isNote) return id
if (exact == -1) exact = id
} else if (exact == -1) {
if (defName.startsWith(name, ignoreCase = true)) {
if (prefix == -1 || (ItemDefinition.lookup(prefix)?.isNote == true && !def.isNote)) {
prefix = id
}
} else if (prefix == -1 && defName.contains(name, ignoreCase = true)) {
if (substring == -1 || (ItemDefinition.lookup(substring)?.isNote == true && !def.isNote)) {
substring = id
}
}
}
}
return when {
exact != -1 -> exact
prefix != -1 -> prefix
else -> substring
}
}
}
override fun test(event: DeveloperCommandEvent): Boolean =
event.name.equals("spawnitem", ignoreCase = true) || event.name.equals("item", ignoreCase = true)
override fun subscribe(context: EventContext, player: Player?, event: DeveloperCommandEvent) {
if (player == null) return
context.breakSubscriberChain()
val arguments = event.arguments
if (arguments.isEmpty()) {
player.packetSender.sendMessage("Must specify an item id or name: ::spawnitem 995 10m, ::spawnitem coins 100k, or ::spawnitem noted gold ore 10m")
return
}
val tokens = arguments.toMutableList()
var requestedNote = false
if (tokens.isNotEmpty() && (tokens.first().equals("noted", ignoreCase = true) || tokens.first().equals("note", ignoreCase = true))) {
requestedNote = true
tokens.removeAt(0)
} else if (tokens.isNotEmpty() && (tokens.last().equals("noted", ignoreCase = true) || tokens.last().equals("note", ignoreCase = true))) {
requestedNote = true
tokens.removeAt(tokens.size - 1)
}
if (tokens.isEmpty()) {
player.packetSender.sendMessage("Must specify an item id or name: ::spawnitem noted gold ore 10m")
return
}
val firstArgId = tokens[0].toIntOrNull()
var itemId: Int
val itemAmount: Int
if (firstArgId != null) {
itemId = firstArgId
if (tokens.size >= 2) {
val parsed = parseAmount(tokens[1])
if (parsed == null) {
player.packetSender.sendMessage("Invalid amount specified: ${tokens[1]}")
return
}
itemAmount = parsed
} else {
itemAmount = 1
}
} else {
var amount = 1
var nameTokens = tokens.toList()
if (tokens.size >= 2) {
val trailingAmount = parseAmount(tokens.last())
if (trailingAmount != null) {
amount = trailingAmount
nameTokens = tokens.subList(0, tokens.size - 1)
}
}
val itemName = nameTokens.joinToString(" ")
val resolvedId = findItemIdByName(itemName)
if (resolvedId == -1) {
player.packetSender.sendMessage("No item found matching: $itemName")
return
}
itemId = resolvedId
itemAmount = amount
}
if (itemId !in 0..MAX_ITEM_ID || itemId >= ItemDefinition.count()) {
player.packetSender.sendMessage("No such item (ID: $itemId).")
return
}
var def = ItemDefinition.lookup(itemId)
val defName = def?.name
if (def == null || defName.isNullOrEmpty() || defName.equals("null", ignoreCase = true)) {
player.packetSender.sendMessage("No such item (ID: $itemId).")
return
}
if (requestedNote) {
val notedId = ItemDefinition.itemToNote(itemId)
if (notedId == itemId) {
if (!def.isNote) {
player.packetSender.sendMessage("Note: $defName cannot be noted.")
}
} else {
itemId = notedId
def = ItemDefinition.lookup(notedId) ?: def
}
}
val added = player.itemAssistant.addItem(itemId, itemAmount)
if (!added) {
return
}
if (player.isBusy) {
player.packetSender.closeAllWindows()
}
val formattedAmount = NumberFormat.getIntegerInstance().format(itemAmount)
val noteTag = if (def.isNote) " (noted)" else ""
player.packetSender.sendMessage("You have spawned $formattedAmount x $defName$noteTag (ID: $itemId).")
}
}