ContentAPI improvements, ported some more content to using ContentAPI

This commit is contained in:
Ceikry
2021-06-22 10:27:28 -05:00
parent 7b47cf267f
commit 9986fb5b61
7 changed files with 188 additions and 60 deletions
+7
View File
@@ -0,0 +1,7 @@
package api
enum class Container {
INVENTORY,
BANK,
EQUIPMENT
}
+142 -11
View File
@@ -11,9 +11,9 @@ import core.game.node.entity.impl.Animator
import core.game.node.entity.impl.Projectile
import core.game.node.entity.npc.NPC
import core.game.node.entity.player.Player
import core.game.node.entity.player.link.TeleportManager
import core.game.node.entity.player.link.audio.Audio
import core.game.node.entity.player.link.emote.Emotes
import core.game.node.entity.skill.Skills
import core.game.node.entity.skill.gather.SkillingTool
import core.game.node.item.GroundItem
import core.game.node.item.GroundItemManager
@@ -23,7 +23,7 @@ import core.game.world.map.Location
import core.game.world.map.RegionManager
import core.game.world.map.path.Pathfinder
import core.game.world.update.flag.context.Animation
import rs09.ServerConstants
import core.game.world.update.flag.context.Graphics
import rs09.game.content.dialogue.DialogueFile
import rs09.game.system.SystemLogger
import rs09.game.world.GameWorld
@@ -115,14 +115,21 @@ object ContentAPI {
* Remove an item from a player's inventory
* @param player the player whose inventory to remove the item from
* @param item the ID or Item object to remove from the player's inventory
* @param container the Container to remove the items from. An enum exists for this in the api package called Container. Ex: api.Container.BANK
*/
@JvmStatic
fun <T> removeItem(player: Player, item: T): Boolean {
fun <T> removeItem(player: Player, item: T, container: Container): Boolean {
item ?: return false
when (item) {
is Item -> return player.inventory.remove(item)
is Int -> return player.inventory.remove(Item(item))
else -> SystemLogger.logErr("Attempted to pass a non-item and non-int to removeItem")
val it = when (item) {
is Item -> item
is Int -> Item(item)
else -> throw IllegalStateException("Invalid value passed for item")
}
when(container){
Container.INVENTORY -> player.inventory.remove(it)
Container.BANK -> player.bank.remove(it)
Container.EQUIPMENT -> player.equipment.remove(it)
}
return false
}
@@ -426,15 +433,21 @@ object ContentAPI {
/**
* Plays an animation on the entity
* @param entity the entity to animate
* @param anim the animation to play
* @param anim the animation to play, can be an ID or an Animation object.
* @param forced whether or not to force the animation (usually not necessary)
*/
@JvmStatic
fun animate(entity: Entity, anim: Animation, forced: Boolean = false) {
fun <T> animate(entity: Entity, anim: T, forced: Boolean = false) {
val animation = when(anim){
is Int -> Animation(anim)
is Animation -> anim
else -> throw IllegalStateException("Invalid value passed for anim")
}
if (forced) {
entity.animator.forceAnimation(anim)
entity.animator.forceAnimation(animation)
} else {
entity.animator.animate(anim)
entity.animator.animate(animation)
}
}
@@ -702,4 +715,122 @@ object ContentAPI {
else -> SystemLogger.logErr("Attempt to set the charge of invalid type: ${node.javaClass.simpleName}")
}
}
/**
* Gets the used option in the context of an interaction.
* @param player the player to get the used option for.
* @return the option the player used
*/
@JvmStatic
fun getUsedOption(player: Player): String {
return player.getAttribute("interact:option","INVALID")
}
/**
* Used to play both an Animation and Graphics object simultaneously.
* @param entity the entity to perform this on
* @param anim the Animation object to use, can also be an ID.
* @param gfx the Graphics object to use, can also be an ID.
*/
@JvmStatic
fun <A,G> visualize(entity: Entity, anim: A, gfx: G){
val animation = when(anim){
is Int -> Animation(anim)
is Animation -> anim
else -> throw IllegalStateException("Invalid parameter passed for animation.")
}
val graphics = when(gfx){
is Int -> Graphics(gfx)
is Graphics -> gfx
else -> throw IllegalStateException("Invalid parameter passed for graphics.")
}
entity.visualize(animation,graphics)
}
/**
* Used to submit a pulse to the GameWorld's Pulser.
* @param pulse the Pulse object to submit
*/
@JvmStatic
fun submitWorldPulse(pulse: Pulse){
GameWorld.Pulser.submit(pulse)
}
/**
* Teleports or "instantly moves" an entity to a given Location object.
* @param entity the entity to move
* @param loc the Location object to move them to
* @param type the teleport type to use (defaults to instant). An enum exists as TeleportManager.TeleportType.
*/
@JvmStatic
fun teleport(entity: Entity, loc: Location, type: TeleportManager.TeleportType = TeleportManager.TeleportType.INSTANT){
entity.teleporter.send(loc,type)
}
/**
* Sets the dynamic or "temporary" (restores) level of a skill.
* @param entity the entity to set the level for
* @param skill the Skill to set. A Skills enum exists that can be used. Ex: Skills.STRENGTH
* @param level the level to set the skill to
*/
@JvmStatic
fun setTempLevel(entity: Entity, skill: Int, level: Int){
entity.skills.setLevel(skill, level)
}
/**
* Gets the static (unchanging/max) level of an entity's skill
* @param entity the entity to get the level for
* @param skill the Skill to get the level of. A Skills enum exists that can be used. Ex: Skills.STRENGTH
* @return the static level of the skill
*/
@JvmStatic
fun getStatLevel(entity: Entity, skill: Int): Int {
return entity.skills.getStaticLevel(skill)
}
/**
* Gets the dynamic (boostable/debuffable/restoring) level of an entity's skill
* @param entity the entity to get the level for
* @param skill the Skill to get the level of. A Skills enum exists that can be used. Ex: Skills.STRENGTH
* @return the dynamic level of the skill
*/
@JvmStatic
fun getDynLevel(entity: Entity, skill: Int): Int {
return entity.skills.getLevel(skill)
}
/**
* Adjusts (buffs/debuffs) the given Skill by the amount given.
* @param entity the entity to adjust the skill for
* @param skill the Skill to adjust. A Skills enum exists that can be used. Ex: Skills.STRENGTH
* @param amount the amount to adjust the skill by. Ex-Buff: 5, Ex-Debuff: -5
*/
@JvmStatic
fun adjustLevel(entity: Entity, skill: Int, amount: Int){
entity.skills.setLevel(skill, entity.skills.getStaticLevel(skill) + amount)
}
/**
* Remove all of a given item from the given container
* @param player the player to remove the item from
* @param item the item to remove. Can be an Item object or an ID.
* @param container the Container to remove the item from. An enum exists for this called Container. Ex: Container.BANK
*/
@JvmStatic
fun <T> removeAll(player: Player, item: T, container: Container){
val it = when(item){
is Item -> item.id
is Int -> item
else -> throw IllegalStateException("Invalid value passed as item")
}
when(container){
Container.EQUIPMENT -> player.equipment.remove(Item(it, amountInEquipment(player, it)))
Container.BANK -> player.bank.remove(Item(it, amountInBank(player, it)))
Container.INVENTORY -> player.inventory.remove(Item(it, amountInInventory(player, it)))
}
}
}