001 package org.bukkit.event.player; 002 003 import org.bukkit.entity.CreatureType; 004 import org.bukkit.entity.Egg; 005 import org.bukkit.entity.EntityType; 006 import org.bukkit.entity.Player; 007 import org.bukkit.event.HandlerList; 008 009 /** 010 * Called when a player throws an egg and it might hatch 011 */ 012 public class PlayerEggThrowEvent extends PlayerEvent { 013 private static final HandlerList handlers = new HandlerList(); 014 private final Egg egg; 015 private boolean hatching; 016 private EntityType hatchType; 017 private byte numHatches; 018 019 public PlayerEggThrowEvent(final Player player, final Egg egg, final boolean hatching, final byte numHatches, final EntityType hatchingType) { 020 super(player); 021 this.egg = egg; 022 this.hatching = hatching; 023 this.numHatches = numHatches; 024 this.hatchType = hatchingType; 025 } 026 027 @Deprecated 028 public PlayerEggThrowEvent(Player player, Egg egg, boolean hatching, byte numHatches, CreatureType hatchingType) { 029 this(player, egg, hatching, numHatches, hatchingType.toEntityType()); 030 } 031 032 /** 033 * Gets the egg involved in this event. 034 * 035 * @return the egg involved in this event 036 */ 037 public Egg getEgg() { 038 return egg; 039 } 040 041 /** 042 * Gets whether the egg is hatching or not. Will be what the server 043 * would've done without interaction. 044 * 045 * @return boolean Whether the egg is going to hatch or not 046 */ 047 public boolean isHatching() { 048 return hatching; 049 } 050 051 /** 052 * Sets whether the egg will hatch or not. 053 * 054 * @param hatching true if you want the egg to hatch, false if you want it 055 * not to 056 */ 057 public void setHatching(boolean hatching) { 058 this.hatching = hatching; 059 } 060 061 /** 062 * Get the type of the mob being hatched (EntityType.CHICKEN by default) 063 * 064 * @return The type of the mob being hatched by the egg 065 * @deprecated In favour of {@link #getHatchingType()}. 066 */ 067 @Deprecated 068 public CreatureType getHatchType() { 069 return CreatureType.fromEntityType(hatchType); 070 } 071 072 /** 073 * Get the type of the mob being hatched (EntityType.CHICKEN by default) 074 * 075 * @return The type of the mob being hatched by the egg 076 */ 077 public EntityType getHatchingType() { 078 return hatchType; 079 } 080 081 /** 082 * Change the type of mob being hatched by the egg 083 * 084 * @param hatchType The type of the mob being hatched by the egg 085 * @deprecated In favour of {@link #setHatchingType(EntityType)}. 086 */ 087 @Deprecated 088 public void setHatchType(CreatureType hatchType) { 089 this.hatchType = hatchType.toEntityType(); 090 } 091 092 /** 093 * Change the type of mob being hatched by the egg 094 * 095 * @param hatchType The type of the mob being hatched by the egg 096 */ 097 public void setHatchingType(EntityType hatchType) { 098 if(!hatchType.isSpawnable()) throw new IllegalArgumentException("Can't spawn that entity type from an egg!"); 099 this.hatchType = hatchType; 100 } 101 102 /** 103 * Get the number of mob hatches from the egg. By default the number will 104 * be the number the server would've done 105 * <ul> 106 * <li>7/8 chance of being 0 107 * <li>31/256 ~= 1/8 chance to be 1 108 * <li>1/256 chance to be 4 109 * </ul> 110 * 111 * @return The number of mobs going to be hatched by the egg 112 */ 113 public byte getNumHatches() { 114 return numHatches; 115 } 116 117 /** 118 * Change the number of mobs coming out of the hatched egg 119 * <p> 120 * The boolean hatching will override this number. Ie. If hatching = 121 * false, this number will not matter 122 * 123 * @param numHatches The number of mobs coming out of the egg 124 */ 125 public void setNumHatches(byte numHatches) { 126 this.numHatches = numHatches; 127 } 128 129 @Override 130 public HandlerList getHandlers() { 131 return handlers; 132 } 133 134 public static HandlerList getHandlerList() { 135 return handlers; 136 } 137 }