Skip to content

Commit

Permalink
Pulls up a bunch of methods into superclasses. Progress on #71
Browse files Browse the repository at this point in the history
The only change that would affect plugins is that getItemInHand has
been renamed to getItemStackInHand to be compatible with Player.java.
Oh and none of this is tested.
  • Loading branch information
gregcarlin committed Nov 28, 2012
1 parent 1fdeefb commit b1af07b
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 220 deletions.
62 changes: 62 additions & 0 deletions src/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,66 @@ public void readFromTag(NBTTagCompound tag) {
public NBTTagCompound getMetaTag() {
return getEntity().metadata;
}

/**
* Returns the location of this entity.
*
* @return location
*/
public Location getLocation() {
Location loc = new Location();

loc.x = getX();
loc.y = getY();
loc.z = getZ();
loc.rotX = getRotation();
loc.rotY = getPitch();
loc.dimension = getWorld().getType().getId();
return loc;
}

/**
* Spawns this entity
*/
public void spawn() {
spawn((LivingEntity) null);
}

/**
* Spawns this entity with a rider
*
* @param rider
*/
public void spawn(LivingEntity rider) {
OWorld world = entity.p;

entity.b(getX() + 0.5d, getY(), getZ() + 0.5d, getRotation(), 0f);
world.d(entity);

if (rider != null) {
OEntityLiving mob2 = rider.getEntity();

mob2.b(getX(), getY(), getZ(), getRotation(), 0f);
world.d(mob2);
mob2.a(entity);
}
}

/**
* Returns whether or not this entity is invulnerable.
*
* @return
*/
public boolean isInvulnerable() {
return getEntity().ar();
}

/**
* Sets whether or not this entity is invulnerable.
*
* @param isInvulnerable
*/
public void setInvulnerable(boolean isInvulnerable) {
entity.i = isInvulnerable;
}
}
144 changes: 141 additions & 3 deletions src/LivingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public MobSpawner getSpawner() {
}

/**
* Adds a potion Effect to the player
* Adds a potion effect to the entity.
*
* @param effect the effect to add.
*/
Expand All @@ -159,7 +159,7 @@ public void addPotionEffect(PotionEffect effect) {
}

/**
* Removes a potion Effect from player
* Removes a potion effect from entity.
*
* @param effect The potion effect to remove
*/
Expand All @@ -172,7 +172,7 @@ public void removePotionEffect(PotionEffect effect) {
}

/**
* Returns a Collection of potion effects active on the player
* Returns a Collection of potion effects active on the entity.
*
* @return List of potion effects
*/
Expand All @@ -186,4 +186,142 @@ public List<PotionEffect> getPotionEffects() {
}
return list;
}

/**
* Sets the item held by the entity.
*
* @param item
*/
public void setItemInHand(Item item) {
getEntity().b(0, item.getBaseItem());
}

/**
* Gets the item held by the entity.
*
* @return
*/
public Item getItemStackInHand() {
OItemStack stack = getEntity().bD();
return stack == null ? null : new Item(stack);
}

/**
* Sets an armor slot of the entity.
*
* @param slot
* The slot of the armor, 0 being boots and 3 being helmet
* @param armor
* The item of armor to add
*/
public void setArmorSlot(int slot, Item armor) {
if(slot >= 0 && slot <= 3) {
getEntity().b(slot + 1, armor.getBaseItem());
}
}

/**
* Gets the item in one of the entity's armor slots.
*
* @param slot
* The slot of the armor, 0 being boots and 3 being helmet
* @return
*/
public Item getArmorSlot(int slot) {
if(slot < 0 || slot > 3) {
return null;
}
OItemStack stack = getEntity().q(slot);
return stack == null ? null : new Item(stack);
}

/**
* Returns whether or not this entity will despawn naturally.
*
* @return
*/
public boolean isPersistent() {
return getEntity().bT;
}

/**
* Sets whether or not this entity will despawn naturally.
*
* @param isPersistent
*/
public void setPersistent(boolean isPersistent) {
getEntity().bT = isPersistent;
}

/**
* Returns the drop chance of an item owned by this entity.
*
* @param slot The slot of the item, 0-4: 0 is hand, 1-4 are armor slots (1=boots and 4=helmet)
* @return The drop chance, 0-1 (anything greater than 1 is guaranteed to drop)
*/
public float getDropChance(int slot) {
if(slot >= 0 && slot <= 4) {
return getEntity().bo[slot];
}
return 0;
}

/**
* Sets the drop chance of an item owned by this entity.
*
* @param slot The slot of the item, 0-4: 0 is hand, 1-4 are armor slots (1=boots and 4=helmet)
* @param chance The drop chance, 0-1 (anything greater than 1 is guaranteed to drop)
*/
public void setDropChance(int slot, float chance) {
if(slot >= 0 && slot <= 4) {
getEntity().bo[slot] = chance;
}
}

/**
* Whether or not this entity can pick up items.
*
* @return
*/
public boolean canPickUpLoot() {
return getEntity().br;
}

/**
* Sets whether or not this entity can pick up items.
*
* @param flag
*/
public void setCanPickUpLoot(boolean flag) {
getEntity().br = flag;
}

/**
* Returns whether or not this entity is currently sneaking (crouching).
*
* @return true if sneaking
*/
public boolean getSneaking() {
return getEntity().ah();
}

/**
* Force this entity to be sneaking or not
*
* @param sneaking
* true if sneaking
*/
public void setSneaking(boolean sneaking) {
getEntity().a(sneaking);
}

/**
* Damages this player, taking into account armor/enchantments/potions
*
* @param type The type of damage to deal (certain types byass armor or affect potions differently)
* @param amount The amount of damage to deal (2 = 1 heart)
*/
public void applyDamage(PluginLoader.DamageType type, int amount) {
getEntity().d(type.getDamageSource(), amount);
}
}
Loading

0 comments on commit b1af07b

Please sign in to comment.