Converted CasketPlugin to CasketListener

This commit is contained in:
Oven Bread
2023-03-28 13:42:10 +00:00
committed by Ryan
parent 374ad2f2e5
commit 9f85619334
2 changed files with 43 additions and 78 deletions
@@ -0,0 +1,43 @@
package content.global.handlers.item
import core.api.*
import core.api.utils.WeightBasedTable
import core.api.utils.WeightedItem
import core.game.interaction.IntType
import core.game.interaction.InteractionListener
import core.game.node.item.Item
import core.tools.StringUtils
import org.rs09.consts.Items
import java.util.*
import kotlin.collections.ArrayList
class CasketListener : InteractionListener {
companion object {
val loot = WeightBasedTable.create(
WeightedItem(Items.COINS_995, 20, 640, 55.0, false),
WeightedItem(Items.UNCUT_SAPPHIRE_1623, 1, 1, 32.0, false),
WeightedItem(Items.UNCUT_EMERALD_1621, 1, 1, 16.0, false),
WeightedItem(Items.UNCUT_RUBY_1619, 1, 1, 9.0, false),
WeightedItem(Items.UNCUT_DIAMOND_1617, 1, 1, 2.0, false),
WeightedItem(Items.COSMIC_TALISMAN_1454, 1, 1, 8.0, false),
WeightedItem(Items.LOOP_HALF_OF_A_KEY_987, 1, 1, 1.0, false),
WeightedItem(Items.TOOTH_HALF_OF_A_KEY_985, 1, 1, 1.0, false)
)
}
override fun defineListeners() {
on(Items.CASKET_405, IntType.ITEM, "open"){ player, node ->
val casket = node.asItem()
if(removeItem(player, casket, Container.INVENTORY)) {
val finalLoot : ArrayList<Item> = loot.roll()
finalLoot.forEach { player.inventory.add(it) }
player.dialogueInterpreter.sendItemMessage(finalLoot[0],
"You open the casket. Inside you find " +
(if (finalLoot[0].amount > 1) "some" else if (StringUtils.isPlusN(finalLoot[0].name)) "an" else "a") +
" " + finalLoot[0].name.lowercase(Locale.getDefault()) + ".")
}
return@on true
}
}
}
@@ -1,78 +0,0 @@
package content.global.handlers.item;
import java.util.ArrayList;
import java.util.List;
import static core.api.ContentAPIKt.*;
import core.cache.def.impl.ItemDefinition;
import core.game.interaction.OptionHandler;
import core.game.node.Node;
import core.game.node.entity.player.Player;
import core.game.node.item.ChanceItem;
import core.game.node.item.Item;
import core.plugin.Initializable;
import core.plugin.Plugin;
import core.tools.RandomFunction;
import core.tools.StringUtils;
import org.rs09.consts.Items;
import core.api.utils.WeightBasedTable;
import core.api.utils.WeightedItem;
/**
* Represents the casket handling plugin.
* @author 'Vexia
* @version 1.0
*/
@Initializable
public final class CasketPlugin extends OptionHandler {
/**
* Represents the casket rewards.
*/
private WeightBasedTable table = WeightBasedTable.create(
new WeightedItem(Items.COINS_995, 20, 640, 55, false),
new WeightedItem(Items.UNCUT_SAPPHIRE_1623, 1, 1, 32, false),
new WeightedItem(Items.UNCUT_EMERALD_1621, 1, 1, 16, false),
new WeightedItem(Items.UNCUT_RUBY_1619, 1, 1, 9, false),
new WeightedItem(Items.UNCUT_DIAMOND_1617, 1, 1, 2, false),
new WeightedItem(Items.COSMIC_TALISMAN_1454, 1, 1, 8, false),
new WeightedItem(Items.LOOP_HALF_OF_A_KEY_987, 1, 1, 1, false),
new WeightedItem(Items.TOOTH_HALF_OF_A_KEY_985, 1, 1, 1, false)
);
@Override
public Plugin<Object> newInstance(Object arg) throws Throwable {
ItemDefinition.forId(405).getHandlers().put("option:open", this);
return this;
}
@Override
public boolean handle(Player player, Node node, String option) {
final Item reward = table.roll(player).get(0);
player.getInventory().remove((Item) node);
player.getDialogueInterpreter().sendItemMessage(reward, "You open the casket. Inside you find " + (reward.getAmount() > 1 ? "some" : (StringUtils.isPlusN(reward.getName()) ? "an" : "a")) + " " + reward.getName().toLowerCase() + ".");
addItemOrDrop(player, reward.getId(), reward.getAmount());
return true;
}
/**
* Gets the chance item from the array.
* @param items the items.
* @return the chance item.
*/
private ChanceItem getChanceItem(ChanceItem[] items) {
final int chance = RandomFunction.random(100);
final List<ChanceItem> chances = new ArrayList<>(20);
for (ChanceItem c : items) {
if (chance > c.getChanceRate()) {
chances.add(c);
}
}
return chances.size() == 0 ? items[0] : chances.get(RandomFunction.random(chances.size()));
}
@Override
public boolean isWalk() {
return false;
}
}