Class ItemTransformEvent

java.lang.Object
net.risingworld.api.events.Event
net.risingworld.api.events.world.ItemTransformEvent
All Implemented Interfaces:
Cancellable

public final class ItemTransformEvent extends Event implements Cancellable
Called when a world item transforms, i.e when the item id changes. Usually this happens when cooking food or smelting ores (so this event is triggered when the item changes its state from raw to cooked, or if the ore is smelted into an ingot etc)
Example: If ore gets crushed by grinder, randomly turn it into a gold ingot
1@EventMethod
2public void onItemTransform(ItemTransformEvent e) {
3 //Gets the item that is about to be processed
4 WorldItem item = e.getItem();
5
6 //Check if item is ore
7 if (item.getDefinition().type == Items.Type.Ore) {
8 //Check if item was crushed. Optionally you could also check the object definition
9 //to find out what object crushed the item exactly, but we don't need that here
10 if (e.getProcessingType() == Items.ItemInfoType.Crush) {
11 //Now get a random value. We want a chance of 10% to turn this item
12 //into a gold ingot. We use the MathUtils for that
13 if (Utils.MathUtils.nextRandomInt(0, 10) == 1) {
14 //Get the gold ingot item def (to retrieve the id)
15 Items.ItemDefinition itemDef = Definitions.getItemDefinition("goldingot");
16
17 //Set the new item type id
18 e.setNewTypeID(itemDef.id);
19 }
20 }
21 }
22}

Example: Notify furnace owner when ore is smelted
1@EventMethod
2public void onOreSmelted(ItemTransformEvent e) {
3 //Gets the item that is about to be smelted
4 WorldItem item = e.getItem();
5
6 //Check if item is ore
7 if (item.getDefinition().type == Items.Type.Ore) {
8 //Check if item was smelted. Optionally you could also check the object definition
9 //to find out what object crushed the item exactly, but we don't need that here
10 if (e.getProcessingType() == Items.ItemInfoType.Smelt) {
11 //Get the furnace (meta object) that smelted the item
12 MetaObject metaObject = e.getMetaObject();
13
14 //The meta object could be null, so check that
15 if (metaObject != null) {
16 //We want to get the player dbID from the object.
17 //This is the dbID of the player who originally placed the object
18 int playerDbID = metaObject.getRelatedObject().getPlayerDbID();
19
20 //Check if the dbID is identical to the one of our player
21 if (playerDbID == player.getDbID()) {
22 //Show a green status message to the player
23 player.showStatusMessage("<color=green>Ore was smelted successfully!</color>", 2);
24 }
25 }
26 }
27 }
28}
  • Method Details

    • getItem

      public WorldItem getItem()
      Gets the world item that's about to transform.
      Returns:
      the world item that's about to transform.
    • getNewTypeID

      public short getNewTypeID()
      Gets the new target item type ID. This means this item will transform to this item (e.g if getItem() returns a raw steak, getNewTypeID() would usually return the type ID of a cooked steak etc).
      Returns:
      the new type ID.
    • getNewStack

      public int getNewStack()
      Gets the new stack size of the item.
      Returns:
      the new stack size (amount of items).
    • setNewTypeID

      public void setNewTypeID(short newTypeID)
      Overrides the target item type ID.
      Parameters:
      newTypeID - the new type ID.
    • setNewStack

      public void setNewStack(int newStack)
      Overrides the stack size of the item.
      Parameters:
      newStack - the new stack size (amount of items).
    • getObjectID

      public long getObjectID()
      Gets the unique global ID of the meta object which processsed this item (e.g the furnace ID). Returns -1 if the transform wasn't triggered by a meta object (furnace, grinder etc).
      Returns:
      the meta object ID.
    • getObjectTypeID

      public short getObjectTypeID()
      Gets the type ID of the meta object.
      Returns:
      the meta object type ID.
    • getTrigger

      public ItemTransformEvent.Trigger getTrigger()
      Gets the trigger of the item transformation, i.e what caused the item to transform.
      Returns:
      the trigger of the item transformation.
    • getMetaObject

      public MetaObject getMetaObject()
      Gets the meta object that was involved in this process, i.e the object that processed the items. For example, this is a furnace which smelted ores, or a grinder which crushed items etc. Only if getTrigger() is Trigger.MetaObject, else null is returned.
      Returns:
      the object that was involved in this event (e.g furnace, grinder, grill etc), or null if no object was involved.
    • getObjectInfoDefinition

      public Objects.ObjectInfoDefinition getObjectInfoDefinition()
      Gets the definition of the related meta object (which processes this item, e.g the furnace, grinder etc). It provides more information about how this item was processed and about the meta object.
      Returns:
      the related meta object definition.
    • getProcessingType

      public Items.ItemInfoType getProcessingType()
      Gets the processing type (related to the meta object processing this item), i.e how the item was processed. If the item was not processed by an object, null is returned.
      Returns:
      the processing type (resulting into the item transformation), or null if the item transform was not related to an object.
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Cancellable
      Determines if the event is cancelled. If an event is cancelled, it will no longer be executed, but other plugins will still receive the event.

      Please note: If the event is threaded, cancellation has no effect, i.e the event will still be executed.
      Specified by:
      isCancelled in interface Cancellable
      Returns:
      true if the event is cancelled, or false if not.
    • setCancelled

      public void setCancelled(boolean cancel)
      Description copied from interface: Cancellable
      Cancels this event. This means it will no longer be executed, but other plugins will still receive the event.

      Specified by:
      setCancelled in interface Cancellable
      Parameters:
      cancel - set to true if you want to cancel this event.
      Example: Cancel "PlayerEnterAreaEvent", i.e prevent player from entering an area
      1//Listener class
      2public class PlayerListener implements Listener{
      3 @EventMethod
      4 public void onEnterArea(PlayerEnterAreaEvent evt){
      5 //Now the player will not be able to enter the area, i.e.
      6 //he will be teleported back to his old position (outside the area)
      7 evt.setCancelled(true);
      8 }
      9}