Class PlayerHitVegetationEvent

All Implemented Interfaces:
Cancellable

public final class PlayerHitVegetationEvent extends PlayerVegetationEvent
Called when a player hits a plant (e.g a tree, bush, flowers etc).
Example: Randomly spawn an apple when player hits a tree
1@EventMethod
2public void onVegetationHit(PlayerVegetationHitEvent event){
3 //First we need the plant definition to find out if the plant is a fully grown tree
4 Plants.PlantDefinition plantDef = event.getPlantDefinition();
5
6 //Check if plant is a tree and if it cannot grow (so it's an adult tree)
7 if(plantDef.type == Plants.Type.FruitTree && !plantDef.cangrow){
8 //We want a 50% chance that an apple spawns. To get a random value efficiently,
9 //you can use the Utils.MathUtils class. For a 50% chance, just get a random boolean:
10 if(Utils.MathUtils.nextRandomBoolean()){
11 //You can use ItemDefinitions to get the internal type id of an apple
12 Items.ItemDefinition itemDef = Definitions.getItemDefinition("apple");
13
14 //Spawn "physical" apple at the "hit position" (where the pickaxe hits the tree)
15 World.spawnItem(itemDef.getID(), 0, 1, event.getHitPosition(), Quaternion.IDENTITY, true);
16 }
17 }
18}
See Also:
  • Method Details

    • getDamage

      public short getDamage()
      Gets the amount of damage the player inflicts by hitting the plant.
      Returns:
      the amount of damage dealt with this hit.
    • setDamage

      public void setDamage(short damage)
      Sets the amount of damage the player should inflict with this hit.
      Parameters:
      damage - the new amount of damage.
    • getHitPosition

      public Vector3f getHitPosition()
      Gets the world coordinates where the plant was hit exactly.
      Returns:
      coordinates where the plant was hit.