Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 478 lines 17 kB view raw
1package net.minecraft.server; 2 3import org.bukkit.craftbukkit.entity.CraftEntity; 4import org.bukkit.craftbukkit.event.CraftEventFactory; 5import org.bukkit.event.entity.EntityDamageByEntityEvent; 6import org.bukkit.event.entity.EntityDamageEvent; 7import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; 8import org.bukkit.event.entity.EntityTargetEvent; 9 10import java.util.Iterator; 11import java.util.List; 12 13// CraftBukkit start 14// CraftBukkit end 15 16public class EntityWolf extends EntityAnimal { 17 18 private boolean a = false; 19 private float b; 20 private float c; 21 private boolean f; 22 private boolean g; 23 private float h; 24 private float i; 25 26 public EntityWolf(World world) { 27 super(world); 28 this.texture = "/mob/wolf.png"; 29 this.b(0.8F, 0.8F); 30 this.aE = 1.1F; 31 this.health = 8; 32 } 33 34 protected void b() { 35 super.b(); 36 this.datawatcher.a(16, Byte.valueOf((byte) 0)); 37 this.datawatcher.a(17, ""); 38 this.datawatcher.a(18, new Integer(this.health)); 39 } 40 41 protected boolean n() { 42 return false; 43 } 44 45 public void b(NBTTagCompound nbttagcompound) { 46 super.b(nbttagcompound); 47 nbttagcompound.a("Angry", this.isAngry()); 48 nbttagcompound.a("Sitting", this.isSitting()); 49 if (this.getOwnerName() == null) { 50 nbttagcompound.setString("Owner", ""); 51 } else { 52 nbttagcompound.setString("Owner", this.getOwnerName()); 53 } 54 } 55 56 public void a(NBTTagCompound nbttagcompound) { 57 super.a(nbttagcompound); 58 this.setAngry(nbttagcompound.m("Angry")); 59 this.setSitting(nbttagcompound.m("Sitting")); 60 String s = nbttagcompound.getString("Owner"); 61 62 if (s.length() > 0) { 63 this.setOwnerName(s); 64 this.setTamed(true); 65 } 66 } 67 68 protected boolean h_() { 69 return !this.isTamed(); 70 } 71 72 protected String g() { 73 return this.isAngry() ? "mob.wolf.growl" : (this.random.nextInt(3) == 0 ? (this.isTamed() && this.datawatcher.b(18) < 10 ? "mob.wolf.whine" : "mob.wolf.panting") : "mob.wolf.bark"); 74 } 75 76 protected String h() { 77 return "mob.wolf.hurt"; 78 } 79 80 protected String i() { 81 return "mob.wolf.death"; 82 } 83 84 protected float k() { 85 return 0.4F; 86 } 87 88 protected int j() { 89 return -1; 90 } 91 92 protected void c_() { 93 super.c_(); 94 if (!this.e && !this.C() && this.isTamed() && this.vehicle == null) { 95 EntityHuman entityhuman = this.world.a(this.getOwnerName()); 96 97 if (entityhuman != null) { 98 float f = entityhuman.f(this); 99 100 if (f > 5.0F) { 101 this.c(entityhuman, f); 102 } 103 } else if (!this.ad()) { 104 this.setSitting(true); 105 } 106 } else if (this.target == null && !this.C() && !this.isTamed() && this.world.random.nextInt(100) == 0) { 107 List list = this.world.a(EntitySheep.class, AxisAlignedBB.b(this.locX, this.locY, this.locZ, this.locX + 1.0D, this.locY + 1.0D, this.locZ + 1.0D).b(16.0D, 4.0D, 16.0D)); 108 109 if (!list.isEmpty()) { 110 // CraftBukkit start 111 Entity entity = (Entity) list.get(this.world.random.nextInt(list.size())); 112 org.bukkit.entity.Entity bukkitTarget = entity == null ? null : entity.getBukkitEntity(); 113 114 EntityTargetEvent event = new EntityTargetEvent(this.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.RANDOM_TARGET); 115 this.world.getServer().getPluginManager().callEvent(event); 116 117 if (!event.isCancelled() || event.getTarget() != null) { 118 this.setTarget(entity); 119 } 120 // CraftBukkit end 121 } 122 } 123 124 if (this.ad()) { 125 this.setSitting(false); 126 } 127 128 if (!this.world.isStatic) { 129 this.datawatcher.watch(18, Integer.valueOf(this.health)); 130 } 131 } 132 133 public void v() { 134 super.v(); 135 this.a = false; 136 if (this.V() && !this.C() && !this.isAngry()) { 137 Entity entity = this.W(); 138 139 if (entity instanceof EntityHuman) { 140 EntityHuman entityhuman = (EntityHuman) entity; 141 ItemStack itemstack = entityhuman.inventory.getItemInHand(); 142 143 if (itemstack != null) { 144 if (!this.isTamed() && itemstack.id == Item.BONE.id) { 145 this.a = true; 146 } else if (this.isTamed() && Item.byId[itemstack.id] instanceof ItemFood) { 147 this.a = ((ItemFood) Item.byId[itemstack.id]).l(); 148 } 149 } 150 } 151 } 152 153 if (!this.Y && this.f && !this.g && !this.C() && this.onGround) { 154 this.g = true; 155 this.h = 0.0F; 156 this.i = 0.0F; 157 this.world.a(this, (byte) 8); 158 } 159 } 160 161 public void m_() { 162 super.m_(); 163 this.c = this.b; 164 if (this.a) { 165 this.b += (1.0F - this.b) * 0.4F; 166 } else { 167 this.b += (0.0F - this.b) * 0.4F; 168 } 169 170 if (this.a) { 171 this.aF = 10; 172 } 173 174 if (this.ac()) { 175 this.f = true; 176 this.g = false; 177 this.h = 0.0F; 178 this.i = 0.0F; 179 } else if ((this.f || this.g) && this.g) { 180 if (this.h == 0.0F) { 181 this.world.makeSound(this, "mob.wolf.shake", this.k(), (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F); 182 } 183 184 this.i = this.h; 185 this.h += 0.05F; 186 if (this.i >= 2.0F) { 187 this.f = false; 188 this.g = false; 189 this.i = 0.0F; 190 this.h = 0.0F; 191 } 192 193 if (this.h > 0.4F) { 194 float f = (float) this.boundingBox.b; 195 int i = (int) (MathHelper.sin((this.h - 0.4F) * 3.1415927F) * 7.0F); 196 197 for (int j = 0; j < i; ++j) { 198 float f1 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length * 0.5F; 199 float f2 = (this.random.nextFloat() * 2.0F - 1.0F) * this.length * 0.5F; 200 201 this.world.a("splash", this.locX + (double) f1, (double) (f + 0.8F), this.locZ + (double) f2, this.motX, this.motY, this.motZ); 202 } 203 } 204 } 205 } 206 207 public float t() { 208 return this.width * 0.8F; 209 } 210 211 protected int u() { 212 return this.isSitting() ? 20 : super.u(); 213 } 214 215 private void c(Entity entity, float f) { 216 PathEntity pathentity = this.world.findPath(this, entity, 16.0F); 217 218 if (pathentity == null && f > 12.0F) { 219 int i = MathHelper.floor(entity.locX) - 2; 220 int j = MathHelper.floor(entity.locZ) - 2; 221 int k = MathHelper.floor(entity.boundingBox.b); 222 223 for (int l = 0; l <= 4; ++l) { 224 for (int i1 = 0; i1 <= 4; ++i1) { 225 if ((l < 1 || i1 < 1 || l > 3 || i1 > 3) && this.world.e(i + l, k - 1, j + i1) && !this.world.e(i + l, k, j + i1) && !this.world.e(i + l, k + 1, j + i1)) { 226 this.setPositionRotation((double) ((float) (i + l) + 0.5F), (double) k, (double) ((float) (j + i1) + 0.5F), this.yaw, this.pitch); 227 return; 228 } 229 } 230 } 231 } else { 232 this.setPathEntity(pathentity); 233 } 234 } 235 236 protected boolean w() { 237 return this.isSitting() || this.g; 238 } 239 240 public boolean damageEntity(Entity entity, int i) { 241 this.setSitting(false); 242 if (entity != null && !(entity instanceof EntityHuman) && !(entity instanceof EntityArrow)) { 243 i = (i + 1) / 2; 244 } 245 246 if (!super.damageEntity((Entity) entity, i)) { 247 return false; 248 } else { 249 if (!this.isTamed() && !this.isAngry()) { 250 if (entity instanceof EntityHuman) { 251 // CraftBukkit start 252 org.bukkit.entity.Entity bukkitTarget = entity == null ? null : entity.getBukkitEntity(); 253 254 EntityTargetEvent event = new EntityTargetEvent(this.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.TARGET_ATTACKED_ENTITY); 255 this.world.getServer().getPluginManager().callEvent(event); 256 257 if (!event.isCancelled()) { 258 if (event.getTarget() == null) { 259 this.target = null; 260 } else { 261 this.setAngry(true); 262 this.target = ((CraftEntity) event.getTarget()).getHandle(); 263 } 264 } 265 // CraftBukkit end 266 } 267 268 if (entity instanceof EntityArrow && ((EntityArrow) entity).shooter != null) { 269 entity = ((EntityArrow) entity).shooter; 270 } 271 272 if (entity instanceof EntityLiving) { 273 List list = this.world.a(EntityWolf.class, AxisAlignedBB.b(this.locX, this.locY, this.locZ, this.locX + 1.0D, this.locY + 1.0D, this.locZ + 1.0D).b(16.0D, 4.0D, 16.0D)); 274 Iterator iterator = list.iterator(); 275 276 while (iterator.hasNext()) { 277 Entity entity1 = (Entity) iterator.next(); 278 EntityWolf entitywolf = (EntityWolf) entity1; 279 280 if (!entitywolf.isTamed() && entitywolf.target == null) { 281 // CraftBukkit start 282 org.bukkit.entity.Entity bukkitTarget = entity == null ? null : entity.getBukkitEntity(); 283 284 EntityTargetEvent event = new EntityTargetEvent(this.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.TARGET_ATTACKED_ENTITY); 285 this.world.getServer().getPluginManager().callEvent(event); 286 287 if (!event.isCancelled()) { 288 if (event.getTarget() == null) { 289 this.target = null; 290 } else { 291 entitywolf.target = (Entity) entity; 292 if (entity instanceof EntityHuman) { 293 entitywolf.setAngry(true); 294 } 295 } 296 } 297 // CraftBukkit end 298 } 299 } 300 } 301 } else if (entity != this && entity != null) { 302 if (this.isTamed() && entity instanceof EntityHuman && ((EntityHuman) entity).name.equalsIgnoreCase(this.getOwnerName())) { 303 return true; 304 } 305 306 this.target = (Entity) entity; 307 } 308 309 return true; 310 } 311 } 312 313 protected Entity findTarget() { 314 return this.isAngry() ? this.world.findNearbyPlayer(this, 16.0D) : null; 315 } 316 317 protected void a(Entity entity, float f) { 318 if (f > 2.0F && f < 6.0F && this.random.nextInt(10) == 0) { 319 if (this.onGround) { 320 double d0 = entity.locX - this.locX; 321 double d1 = entity.locZ - this.locZ; 322 float f1 = MathHelper.a(d0 * d0 + d1 * d1); 323 324 this.motX = d0 / (double) f1 * 0.5D * 0.800000011920929D + this.motX * 0.20000000298023224D; 325 this.motZ = d1 / (double) f1 * 0.5D * 0.800000011920929D + this.motZ * 0.20000000298023224D; 326 this.motY = 0.4000000059604645D; 327 } 328 } else if ((double) f < 1.5D && entity.boundingBox.e > this.boundingBox.b && entity.boundingBox.b < this.boundingBox.e) { 329 this.attackTicks = 20; 330 byte b0 = 2; 331 332 if (this.isTamed()) { 333 b0 = 4; 334 } 335 // CraftBukkit start 336 org.bukkit.entity.Entity damager = this.getBukkitEntity(); 337 org.bukkit.entity.Entity damagee = entity == null ? null : entity.getBukkitEntity(); 338 339 EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK, b0); 340 this.world.getServer().getPluginManager().callEvent(event); 341 342 if (event.isCancelled()) { 343 return; 344 } 345 // CraftBukkit end 346 347 entity.damageEntity(this, b0); 348 } 349 } 350 351 public boolean a(EntityHuman entityhuman) { 352 ItemStack itemstack = entityhuman.inventory.getItemInHand(); 353 354 if (!this.isTamed()) { 355 if (itemstack != null && itemstack.id == Item.BONE.id && !this.isAngry()) { 356 --itemstack.count; 357 if (itemstack.count <= 0) { 358 entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, (ItemStack) null); 359 } 360 361 if (!this.world.isStatic) { 362 // CraftBukkit - added event call and isCancelled check. 363 if (this.random.nextInt(3) == 0 && !CraftEventFactory.callEntityTameEvent(this, entityhuman).isCancelled()) { 364 // CraftBukkit end 365 this.setTamed(true); 366 this.setPathEntity((PathEntity) null); 367 this.setSitting(true); 368 this.health = 20; 369 this.setOwnerName(entityhuman.name); 370 this.a(true); 371 this.world.a(this, (byte) 7); 372 } else { 373 this.a(false); 374 this.world.a(this, (byte) 6); 375 } 376 } 377 378 return true; 379 } 380 } else { 381 if (itemstack != null && Item.byId[itemstack.id] instanceof ItemFood) { 382 ItemFood itemfood = (ItemFood) Item.byId[itemstack.id]; 383 384 if (itemfood.l() && this.datawatcher.b(18) < 20) { 385 --itemstack.count; 386 if (itemstack.count <= 0) { 387 entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, (ItemStack) null); 388 } 389 390 this.b(((ItemFood) Item.PORK).k(), RegainReason.EATING); 391 return true; 392 } 393 } 394 395 if (entityhuman.name.equalsIgnoreCase(this.getOwnerName())) { 396 if (!this.world.isStatic) { 397 this.setSitting(!this.isSitting()); 398 this.aC = false; 399 this.setPathEntity((PathEntity) null); 400 } 401 402 return true; 403 } 404 } 405 406 return false; 407 } 408 409 void a(boolean flag) { 410 String s = "heart"; 411 412 if (!flag) { 413 s = "smoke"; 414 } 415 416 for (int i = 0; i < 7; ++i) { 417 double d0 = this.random.nextGaussian() * 0.02D; 418 double d1 = this.random.nextGaussian() * 0.02D; 419 double d2 = this.random.nextGaussian() * 0.02D; 420 421 this.world.a(s, this.locX + (double) (this.random.nextFloat() * this.length * 2.0F) - (double) this.length, this.locY + 0.5D + (double) (this.random.nextFloat() * this.width), this.locZ + (double) (this.random.nextFloat() * this.length * 2.0F) - (double) this.length, d0, d1, d2); 422 } 423 } 424 425 public int l() { 426 return 8; 427 } 428 429 public String getOwnerName() { 430 return this.datawatcher.c(17); 431 } 432 433 public void setOwnerName(String s) { 434 this.datawatcher.watch(17, s); 435 } 436 437 public boolean isSitting() { 438 return (this.datawatcher.a(16) & 1) != 0; 439 } 440 441 public void setSitting(boolean flag) { 442 byte b0 = this.datawatcher.a(16); 443 444 if (flag) { 445 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 | 1))); 446 } else { 447 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 & -2))); 448 } 449 } 450 451 public boolean isAngry() { 452 return (this.datawatcher.a(16) & 2) != 0; 453 } 454 455 public void setAngry(boolean flag) { 456 byte b0 = this.datawatcher.a(16); 457 458 if (flag) { 459 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 | 2))); 460 } else { 461 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 & -3))); 462 } 463 } 464 465 public boolean isTamed() { 466 return (this.datawatcher.a(16) & 4) != 0; 467 } 468 469 public void setTamed(boolean flag) { 470 byte b0 = this.datawatcher.a(16); 471 472 if (flag) { 473 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 | 4))); 474 } else { 475 this.datawatcher.watch(16, Byte.valueOf((byte) (b0 & -5))); 476 } 477 } 478}