Rewrote fruit cutting plugin as listener

This commit is contained in:
bushtail
2023-02-17 02:12:41 +00:00
committed by Ryan
parent 7f6752ba8a
commit 254089d34e
2 changed files with 87 additions and 273 deletions
@@ -0,0 +1,87 @@
package content.global.handlers.item.withitem
import core.api.*
import core.game.dialogue.DialogueFile
import core.game.interaction.IntType
import core.game.interaction.InteractionListener
import core.game.node.item.Item
import core.game.world.update.flag.context.Animation
import core.tools.START_DIALOGUE
import org.rs09.consts.Items
class FruitCuttingListener : InteractionListener {
val anim = Animation(1192) // TODO Add fruit cutting animation to ConstLib
enum class Fruit(val fruit: Int, val chunk: Int, val slice: Item) {
PINEAPPLE(Items.PINEAPPLE_2114, Items.PINEAPPLE_CHUNKS_2116, Item(Items.PINEAPPLE_RING_2118, 4)),
BANANA(Items.BANANA_1963, -1, Item(Items.SLICED_BANANA_3162)),
LEMON(Items.LEMON_2102, Items.LEMON_CHUNKS_2104, Item(Items.LEMON_SLICES_2106)),
LIME(Items.LIME_2120, Items.LIME_CHUNKS_2122, Item(Items.LIME_SLICES_2124)),
ORANGE(Items.ORANGE_2108, Items.ORANGE_CHUNKS_2110, Item(Items.ORANGE_SLICES_2112));
companion object {
val productOfChunk = values().associate { it.fruit to it.chunk }
val productOfSlice = values().associate { it.fruit to it.slice }
val cutable = intArrayOf(PINEAPPLE.fruit, BANANA.fruit, LEMON.fruit, LIME.fruit, ORANGE.fruit)
fun forChunkId(id: Int) : Int {
return productOfChunk[id]!!
}
fun forSliceId(id: Int) : Item {
return productOfSlice[id]!!
}
}
}
override fun defineListeners() {
onUseWith(IntType.ITEM, Fruit.cutable, Items.KNIFE_946) { player, used, _ ->
openDialogue(
player,
FruitCuttingDialogue(used.id, Fruit.forChunkId(used.id), Fruit.forSliceId(used.id))
)
return@onUseWith true
}
}
}
class FruitCuttingDialogue(val fruit: Int, val chunk: Int, val slice: Item) : DialogueFile() {
val anim = Animation(1192)
override fun handle(componentID: Int, buttonID: Int) {
when(stage) {
START_DIALOGUE -> {
if(chunk == -1) {
if(removeItem(player!!, fruit) && addItem(player!!, slice.id)) {
animate(player!!, anim, true)
sendMessage(player!!, "You deftly chop the ${getItemName(fruit).lowercase()} into slices.")
}
} else {
setInterfaceText(player!!, "Would you like to...", 140, 4)
sendItemOnInterface(player!!, 140, 6, chunk, 1)
sendItemOnInterface(player!!, 140, 5, slice.id, 1)
setInterfaceText(player!!, "Slice the ${getItemName(fruit).lowercase()}", 140, 2)
setInterfaceText(player!!, "Dice the ${getItemName(fruit).lowercase()}", 140, 3)
openInterface(player!!, 140)
when(buttonID) {
1 -> {
closeInterface(player!!)
if(removeItem(player!!, fruit) && addItem(player!!, slice.id, slice.amount)) {
animate(player!!, anim, true)
sendMessage(player!!, "You cut the ${getItemName(fruit).lowercase()} into slices.")
}
}
2 -> {
closeInterface(player!!)
if(removeItem(player!!, fruit) && addItem(player!!, chunk)) {
animate(player!!, anim, true)
sendMessage(player!!, "You cut the ${getItemName(fruit).lowercase()} into chunks.")
}
}
}
}
}
}
}
}
@@ -1,273 +0,0 @@
package content.global.handlers.item.withitem;
import static core.api.ContentAPIKt.*;
import core.game.component.Component;
import core.plugin.Initializable;
import kotlin.Unit;
import org.rs09.consts.Items;
import core.game.dialogue.DialoguePlugin;
import core.game.interaction.NodeUsageEvent;
import core.game.interaction.UseWithHandler;
import core.game.node.entity.player.Player;
import core.game.node.entity.player.link.RunScript;
import core.game.node.item.Item;
import core.game.world.update.flag.context.Animation;
import core.plugin.Plugin;
/**
* Represents the fruit cutting plugin.
* @author 'Vexia
* @version 1.0
*/
@Initializable
public final class FruitCuttingPlugin extends UseWithHandler {
/**
* Represents the cutting banana animation.
*/
private static final Animation ANIMATION = new Animation(1192);
/**
* Constructs a new {@code FruitCuttingPlugin} {@code Object}.
*/
public FruitCuttingPlugin() {
super(946);
}
@Override
public Plugin<Object> newInstance(Object arg) throws Throwable {
for (Fruit fruit : Fruit.values()) {
addHandler(fruit.getBase().getId(), ITEM_TYPE, this);
}
new FruitCuttingDialogue().init();
return this;
}
@Override
public boolean handle(NodeUsageEvent event) {
final Player player = event.getPlayer();
final Fruit fruit = Fruit.forBase(event.getUsedItem());
if (fruit == Fruit.BANANA || fruit == Fruit.LEMON) {
if (player.getInventory().remove(fruit.getBase())) {
player.lock(2);
player.animate(ANIMATION);
player.getInventory().add(fruit.getSliced());
player.getPacketDispatch().sendMessage("You deftly chop the "+ fruit.name().toLowerCase() + " into slices.");
}
return true;
}
player.getDialogueInterpreter().open(31237434, fruit);
return true;
}
/**
* Represents the a fruit to cut.
* @author 'Vexia
* @date 30/11/2013
*/
public enum Fruit {
PINEAPPLE(new Item(2114), new Item(2116), new Item(2118, 4)),
BANANA(new Item(1963), null, new Item(3162)),
LEMON(new Item(2102), new Item(Items.LEMON_CHUNKS_2104), new Item(2106)),
LIME(new Item(Items.LIME_2120),new Item(Items.LIME_CHUNKS_2122), new Item(Items.LIME_SLICES_2124)),
ORANGE(new Item(2108), new Item(2110), new Item(2112));
/**
* Constructs a new {@code FruitCuttingPlugin.java} {@code Object}.
* @param base the base item.
* @param diced the diced item.
* @param sliced the sliced item.
*/
Fruit(Item base, Item diced, Item sliced) {
this.base = base;
this.diced = diced;
this.sliced = sliced;
}
/**
* Represents the base item.
*/
private final Item base;
/**
* Represents the diced item.
*/
private final Item diced;
/**
* Represents the sliced item.
*/
private final Item sliced;
/**
* Gets the base.
* @return The base.
*/
public Item getBase() {
return base;
}
/**
* Gets the diced.
* @return The diced.
*/
public Item getDiced() {
return diced;
}
/**
* Gets the sliced.
* @return The sliced.
*/
public Item getSliced() {
return sliced;
}
/**
* Method used to get the fruit for the base item.
* @param item the item.
* @return the fruit.
*/
public static Fruit forBase(final Item item) {
for (Fruit fruit : values()) {
if (fruit.getBase().getId() == item.getId()) {
return fruit;
}
}
return null;
}
}
/**
* Represents the dialogue plugin used to cut fruit.
* @author 'Vexia
* @version 1.0
*/
public static final class FruitCuttingDialogue extends DialoguePlugin {
/**
* Represents the component interface.
*/
private static final Component COMPONENT = new Component(140);
/**
* Represents the fruit.
*/
private Fruit fruit;
/**
* Represents the fruit to cut.
*/
private Item item;
/**
* Represents if its a sliced cut.
*/
private boolean slice;
/**
* Constructs a new {@code FruitCuttingDialogue} {@code Object}.
*/
public FruitCuttingDialogue() {
/**
* empty.
*/
}
/**
* Constructs a new {@code FruitCuttingDialogue} {@code Object}.
* @param player the player.
*/
public FruitCuttingDialogue(Player player) {
super(player);
}
@Override
public DialoguePlugin newInstance(Player player) {
return new FruitCuttingDialogue(player);
}
@Override
public boolean open(Object... args) {
fruit = ((Fruit) args[0]);
player.getPacketDispatch().sendString("Would you like to...", 140, 4);
player.getPacketDispatch().sendItemOnInterface(fruit.getSliced().getId(), 1, 140, 5);
player.getPacketDispatch().sendItemOnInterface(fruit.getDiced().getId(), 1, 140, 6);
player.getPacketDispatch().sendString("Slice the " + fruit.getBase().getName().toLowerCase() + ".", 140, 2);
player.getPacketDispatch().sendString("Dice the " + fruit.getBase().getName().toLowerCase() + ".", 140, 3);
player.getInterfaceManager().openChatbox(COMPONENT);
stage = 0;
return true;
}
@Override
public boolean handle(int interfaceId, int buttonId) {
switch (stage) {
case 0:
player.getInterfaceManager().openChatbox(309);
player.getPacketDispatch().sendItemZoomOnInterface(1, 160, 309, 2);
slice = buttonId == 2 ? false : true;
item = buttonId == 2 ? fruit.getDiced() : fruit.getSliced();
player.getPacketDispatch().sendString("<br><br><br><br>" + item.getName(), 309, 6);
player.getPacketDispatch().sendItemZoomOnInterface(item.getId(), 175, 309, 2);
stage = 1;
break;
case 1:
int amount = 0;
switch (buttonId) {
case 6:
amount = 1;
break;
case 5:
amount = 5;
break;
case 4:
sendInputDialogue(player, false, "Enter the amount:", (value) -> {
String s = value.toString();
s = s.replace("k","000");
s = s.replace("K","000");
int val = Integer.parseInt(s);
cut(player, val, slice);
return Unit.INSTANCE;
});
return true;
case 3:
amount = player.getInventory().getAmount(fruit.getBase());
break;
}
cut(player, amount, slice);
break;
}
return true;
}
@Override
public int[] getIds() {
return new int[] { 31237434 };
}
/**
* Method used to cut a fruit.
* @param player the player.
* @param amount the amount.
* @param slice if its sliced.
*/
private final void cut(final Player player, int amount, boolean slice) {
if (amount > player.getInventory().getAmount(fruit.getBase())) {
amount = player.getInventory().getAmount(fruit.getBase());
}
end();
final Item remove = new Item(fruit.getBase().getId(), amount);
if (!player.getInventory().containsItem(remove)) {
return;
}
if (player.getInventory().remove(remove)) {
player.getPacketDispatch().sendMessage("You cut the " + fruit.name().toLowerCase() + " into " + (slice ? fruit == Fruit.PINEAPPLE ? "rings" : "slices" : "chunks") + ".");
for (int i = 0; i < amount; i++) {
player.getInventory().add(slice ? fruit.getSliced() : fruit.getDiced());
}
}
}
}
}