| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.entity.Entity;
|
| 6 | import net.minecraft.entity.EntityList;
|
| 7 | import net.minecraft.entity.EntityLiving;
|
| 8 | import net.minecraft.entity.EntityLivingBase;
|
| 9 | import net.minecraft.entity.IEntityLivingData;
|
| 10 | import net.minecraft.entity.item.EntityMinecart;
|
| 11 | import net.minecraft.nbt.NBTBase;
|
| 12 | import net.minecraft.nbt.NBTTagCompound;
|
| 13 | import net.minecraft.nbt.NBTTagList;
|
| 14 | import net.minecraft.util.AxisAlignedBB;
|
| 15 | import net.minecraft.util.BlockPos;
|
| 16 | import net.minecraft.util.EnumParticleTypes;
|
| 17 | import net.minecraft.util.StringUtils;
|
| 18 | import net.minecraft.util.WeightedRandom;
|
| 19 | import net.minecraft.world.World;
|
| 20 |
|
| 21 | public abstract class MobSpawnerBaseLogic
|
| 22 | {
|
| 23 | /** The delay to spawn. */
|
| 24 | private int spawnDelay = 20;
|
| 25 | private String mobID = "Pig";
|
| 26 | private final List<MobSpawnerBaseLogic.WeightedRandomMinecart> minecartToSpawn = Lists.<MobSpawnerBaseLogic.WeightedRandomMinecart>newArrayList();
|
| 27 | private MobSpawnerBaseLogic.WeightedRandomMinecart randomEntity;
|
| 28 |
|
| 29 | /** The rotation of the mob inside the mob spawner */
|
| 30 | private double mobRotation;
|
| 31 |
|
| 32 | /** the previous rotation of the mob inside the mob spawner */
|
| 33 | private double prevMobRotation;
|
| 34 | private int minSpawnDelay = 200;
|
| 35 | private int maxSpawnDelay = 800;
|
| 36 | private int spawnCount = 4;
|
| 37 |
|
| 38 | /** Cached instance of the entity to render inside the spawner. */
|
| 39 | private Entity cachedEntity;
|
| 40 | private int maxNearbyEntities = 6;
|
| 41 |
|
| 42 | /** The distance from which a player activates the spawner. */
|
| 43 | private int activatingRangeFromPlayer = 16;
|
| 44 |
|
| 45 | /** The range coefficient for spawning entities around. */
|
| 46 | private int spawnRange = 4;
|
| 47 |
|
| 48 | /**
|
| 49 | * Gets the entity name that should be spawned.
|
| 50 | */
|
| 51 | private String getEntityNameToSpawn()
|
| 52 | {
|
| 53 | if (this.getRandomEntity() == null)
|
| 54 | {
|
| 55 | if (this.mobID != null && this.mobID.equals("Minecart"))
|
| 56 | {
|
| 57 | this.mobID = "MinecartRideable";
|
| 58 | }
|
| 59 |
|
| 60 | return this.mobID;
|
| 61 | }
|
| 62 | else
|
| 63 | {
|
| 64 | return this.getRandomEntity().entityType;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public void setEntityName(String name)
|
| 69 | {
|
| 70 | this.mobID = name;
|
| 71 | }
|
| 72 |
|
| 73 | /**
|
| 74 | * Returns true if there's a player close enough to this mob spawner to activate it.
|
| 75 | */
|
| 76 | private boolean isActivated()
|
| 77 | {
|
| 78 | BlockPos blockpos = this.getSpawnerPosition();
|
| 79 | return this.getSpawnerWorld().isAnyPlayerWithinRangeAt((double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 0.5D, (double)blockpos.getZ() + 0.5D, (double)this.activatingRangeFromPlayer);
|
| 80 | }
|
| 81 |
|
| 82 | public void updateSpawner()
|
| 83 | {
|
| 84 | if (this.isActivated())
|
| 85 | {
|
| 86 | BlockPos blockpos = this.getSpawnerPosition();
|
| 87 |
|
| 88 | if (this.getSpawnerWorld().isRemote)
|
| 89 | {
|
| 90 | double d3 = (double)((float)blockpos.getX() + this.getSpawnerWorld().rand.nextFloat());
|
| 91 | double d4 = (double)((float)blockpos.getY() + this.getSpawnerWorld().rand.nextFloat());
|
| 92 | double d5 = (double)((float)blockpos.getZ() + this.getSpawnerWorld().rand.nextFloat());
|
| 93 | this.getSpawnerWorld().spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d3, d4, d5, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 94 | this.getSpawnerWorld().spawnParticle(EnumParticleTypes.FLAME, d3, d4, d5, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 95 |
|
| 96 | if (this.spawnDelay > 0)
|
| 97 | {
|
| 98 | --this.spawnDelay;
|
| 99 | }
|
| 100 |
|
| 101 | this.prevMobRotation = this.mobRotation;
|
| 102 | this.mobRotation = (this.mobRotation + (double)(1000.0F / ((float)this.spawnDelay + 200.0F))) % 360.0D;
|
| 103 | }
|
| 104 | else
|
| 105 | {
|
| 106 | if (this.spawnDelay == -1)
|
| 107 | {
|
| 108 | this.resetTimer();
|
| 109 | }
|
| 110 |
|
| 111 | if (this.spawnDelay > 0)
|
| 112 | {
|
| 113 | --this.spawnDelay;
|
| 114 | return;
|
| 115 | }
|
| 116 |
|
| 117 | boolean flag = false;
|
| 118 |
|
| 119 | for (int i = 0; i < this.spawnCount; ++i)
|
| 120 | {
|
| 121 | Entity entity = EntityList.createEntityByName(this.getEntityNameToSpawn(), this.getSpawnerWorld());
|
| 122 |
|
| 123 | if (entity == null)
|
| 124 | {
|
| 125 | return;
|
| 126 | }
|
| 127 |
|
| 128 | int j = this.getSpawnerWorld().getEntitiesWithinAABB(entity.getClass(), (new AxisAlignedBB((double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ(), (double)(blockpos.getX() + 1), (double)(blockpos.getY() + 1), (double)(blockpos.getZ() + 1))).expand((double)this.spawnRange, (double)this.spawnRange, (double)this.spawnRange)).size();
|
| 129 |
|
| 130 | if (j >= this.maxNearbyEntities)
|
| 131 | {
|
| 132 | this.resetTimer();
|
| 133 | return;
|
| 134 | }
|
| 135 |
|
| 136 | double d0 = (double)blockpos.getX() + (this.getSpawnerWorld().rand.nextDouble() - this.getSpawnerWorld().rand.nextDouble()) * (double)this.spawnRange + 0.5D;
|
| 137 | double d1 = (double)(blockpos.getY() + this.getSpawnerWorld().rand.nextInt(3) - 1);
|
| 138 | double d2 = (double)blockpos.getZ() + (this.getSpawnerWorld().rand.nextDouble() - this.getSpawnerWorld().rand.nextDouble()) * (double)this.spawnRange + 0.5D;
|
| 139 | EntityLiving entityliving = entity instanceof EntityLiving ? (EntityLiving)entity : null;
|
| 140 | entity.setLocationAndAngles(d0, d1, d2, this.getSpawnerWorld().rand.nextFloat() * 360.0F, 0.0F);
|
| 141 |
|
| 142 | if (entityliving == null || entityliving.getCanSpawnHere() && entityliving.isNotColliding())
|
| 143 | {
|
| 144 | this.spawnNewEntity(entity, true);
|
| 145 | this.getSpawnerWorld().playAuxSFX(2004, blockpos, 0);
|
| 146 |
|
| 147 | if (entityliving != null)
|
| 148 | {
|
| 149 | entityliving.spawnExplosionParticle();
|
| 150 | }
|
| 151 |
|
| 152 | flag = true;
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | if (flag)
|
| 157 | {
|
| 158 | this.resetTimer();
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private Entity spawnNewEntity(Entity entityIn, boolean spawn)
|
| 165 | {
|
| 166 | if (this.getRandomEntity() != null)
|
| 167 | {
|
| 168 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 169 | entityIn.writeToNBTOptional(nbttagcompound);
|
| 170 |
|
| 171 | for (String s : this.getRandomEntity().nbtData.getKeySet())
|
| 172 | {
|
| 173 | NBTBase nbtbase = this.getRandomEntity().nbtData.getTag(s);
|
| 174 | nbttagcompound.setTag(s, nbtbase.copy());
|
| 175 | }
|
| 176 |
|
| 177 | entityIn.readFromNBT(nbttagcompound);
|
| 178 |
|
| 179 | if (entityIn.worldObj != null && spawn)
|
| 180 | {
|
| 181 | entityIn.worldObj.spawnEntityInWorld(entityIn);
|
| 182 | }
|
| 183 |
|
| 184 | NBTTagCompound nbttagcompound2;
|
| 185 |
|
| 186 | for (Entity entity = entityIn; nbttagcompound.hasKey("Riding", 10); nbttagcompound = nbttagcompound2)
|
| 187 | {
|
| 188 | nbttagcompound2 = nbttagcompound.getCompoundTag("Riding");
|
| 189 | Entity entity1 = EntityList.createEntityByName(nbttagcompound2.getString("id"), entityIn.worldObj);
|
| 190 |
|
| 191 | if (entity1 != null)
|
| 192 | {
|
| 193 | NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
| 194 | entity1.writeToNBTOptional(nbttagcompound1);
|
| 195 |
|
| 196 | for (String s1 : nbttagcompound2.getKeySet())
|
| 197 | {
|
| 198 | NBTBase nbtbase1 = nbttagcompound2.getTag(s1);
|
| 199 | nbttagcompound1.setTag(s1, nbtbase1.copy());
|
| 200 | }
|
| 201 |
|
| 202 | entity1.readFromNBT(nbttagcompound1);
|
| 203 | entity1.setLocationAndAngles(entity.posX, entity.posY, entity.posZ, entity.rotationYaw, entity.rotationPitch);
|
| 204 |
|
| 205 | if (entityIn.worldObj != null && spawn)
|
| 206 | {
|
| 207 | entityIn.worldObj.spawnEntityInWorld(entity1);
|
| 208 | }
|
| 209 |
|
| 210 | entity.mountEntity(entity1);
|
| 211 | }
|
| 212 |
|
| 213 | entity = entity1;
|
| 214 | }
|
| 215 | }
|
| 216 | else if (entityIn instanceof EntityLivingBase && entityIn.worldObj != null && spawn)
|
| 217 | {
|
| 218 | if (entityIn instanceof EntityLiving)
|
| 219 | {
|
| 220 | ((EntityLiving)entityIn).onInitialSpawn(entityIn.worldObj.getDifficultyForLocation(new BlockPos(entityIn)), (IEntityLivingData)null);
|
| 221 | }
|
| 222 |
|
| 223 | entityIn.worldObj.spawnEntityInWorld(entityIn);
|
| 224 | }
|
| 225 |
|
| 226 | return entityIn;
|
| 227 | }
|
| 228 |
|
| 229 | private void resetTimer()
|
| 230 | {
|
| 231 | if (this.maxSpawnDelay <= this.minSpawnDelay)
|
| 232 | {
|
| 233 | this.spawnDelay = this.minSpawnDelay;
|
| 234 | }
|
| 235 | else
|
| 236 | {
|
| 237 | int i = this.maxSpawnDelay - this.minSpawnDelay;
|
| 238 | this.spawnDelay = this.minSpawnDelay + this.getSpawnerWorld().rand.nextInt(i);
|
| 239 | }
|
| 240 |
|
| 241 | if (this.minecartToSpawn.size() > 0)
|
| 242 | {
|
| 243 | this.setRandomEntity((MobSpawnerBaseLogic.WeightedRandomMinecart)WeightedRandom.getRandomItem(this.getSpawnerWorld().rand, this.minecartToSpawn));
|
| 244 | }
|
| 245 |
|
| 246 | this.func_98267_a(1);
|
| 247 | }
|
| 248 |
|
| 249 | public void readFromNBT(NBTTagCompound nbt)
|
| 250 | {
|
| 251 | this.mobID = nbt.getString("EntityId");
|
| 252 | this.spawnDelay = nbt.getShort("Delay");
|
| 253 | this.minecartToSpawn.clear();
|
| 254 |
|
| 255 | if (nbt.hasKey("SpawnPotentials", 9))
|
| 256 | {
|
| 257 | NBTTagList nbttaglist = nbt.getTagList("SpawnPotentials", 10);
|
| 258 |
|
| 259 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 260 | {
|
| 261 | this.minecartToSpawn.add(new MobSpawnerBaseLogic.WeightedRandomMinecart(nbttaglist.getCompoundTagAt(i)));
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | if (nbt.hasKey("SpawnData", 10))
|
| 266 | {
|
| 267 | this.setRandomEntity(new MobSpawnerBaseLogic.WeightedRandomMinecart(nbt.getCompoundTag("SpawnData"), this.mobID));
|
| 268 | }
|
| 269 | else
|
| 270 | {
|
| 271 | this.setRandomEntity((MobSpawnerBaseLogic.WeightedRandomMinecart)null);
|
| 272 | }
|
| 273 |
|
| 274 | if (nbt.hasKey("MinSpawnDelay", 99))
|
| 275 | {
|
| 276 | this.minSpawnDelay = nbt.getShort("MinSpawnDelay");
|
| 277 | this.maxSpawnDelay = nbt.getShort("MaxSpawnDelay");
|
| 278 | this.spawnCount = nbt.getShort("SpawnCount");
|
| 279 | }
|
| 280 |
|
| 281 | if (nbt.hasKey("MaxNearbyEntities", 99))
|
| 282 | {
|
| 283 | this.maxNearbyEntities = nbt.getShort("MaxNearbyEntities");
|
| 284 | this.activatingRangeFromPlayer = nbt.getShort("RequiredPlayerRange");
|
| 285 | }
|
| 286 |
|
| 287 | if (nbt.hasKey("SpawnRange", 99))
|
| 288 | {
|
| 289 | this.spawnRange = nbt.getShort("SpawnRange");
|
| 290 | }
|
| 291 |
|
| 292 | if (this.getSpawnerWorld() != null)
|
| 293 | {
|
| 294 | this.cachedEntity = null;
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | public void writeToNBT(NBTTagCompound nbt)
|
| 299 | {
|
| 300 | String s = this.getEntityNameToSpawn();
|
| 301 |
|
| 302 | if (!StringUtils.isNullOrEmpty(s))
|
| 303 | {
|
| 304 | nbt.setString("EntityId", s);
|
| 305 | nbt.setShort("Delay", (short)this.spawnDelay);
|
| 306 | nbt.setShort("MinSpawnDelay", (short)this.minSpawnDelay);
|
| 307 | nbt.setShort("MaxSpawnDelay", (short)this.maxSpawnDelay);
|
| 308 | nbt.setShort("SpawnCount", (short)this.spawnCount);
|
| 309 | nbt.setShort("MaxNearbyEntities", (short)this.maxNearbyEntities);
|
| 310 | nbt.setShort("RequiredPlayerRange", (short)this.activatingRangeFromPlayer);
|
| 311 | nbt.setShort("SpawnRange", (short)this.spawnRange);
|
| 312 |
|
| 313 | if (this.getRandomEntity() != null)
|
| 314 | {
|
| 315 | nbt.setTag("SpawnData", this.getRandomEntity().nbtData.copy());
|
| 316 | }
|
| 317 |
|
| 318 | if (this.getRandomEntity() != null || this.minecartToSpawn.size() > 0)
|
| 319 | {
|
| 320 | NBTTagList nbttaglist = new NBTTagList();
|
| 321 |
|
| 322 | if (this.minecartToSpawn.size() > 0)
|
| 323 | {
|
| 324 | for (MobSpawnerBaseLogic.WeightedRandomMinecart mobspawnerbaselogic$weightedrandomminecart : this.minecartToSpawn)
|
| 325 | {
|
| 326 | nbttaglist.appendTag(mobspawnerbaselogic$weightedrandomminecart.toNBT());
|
| 327 | }
|
| 328 | }
|
| 329 | else
|
| 330 | {
|
| 331 | nbttaglist.appendTag(this.getRandomEntity().toNBT());
|
| 332 | }
|
| 333 |
|
| 334 | nbt.setTag("SpawnPotentials", nbttaglist);
|
| 335 | }
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | public Entity func_180612_a(World worldIn)
|
| 340 | {
|
| 341 | if (this.cachedEntity == null)
|
| 342 | {
|
| 343 | Entity entity = EntityList.createEntityByName(this.getEntityNameToSpawn(), worldIn);
|
| 344 |
|
| 345 | if (entity != null)
|
| 346 | {
|
| 347 | entity = this.spawnNewEntity(entity, false);
|
| 348 | this.cachedEntity = entity;
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | return this.cachedEntity;
|
| 353 | }
|
| 354 |
|
| 355 | /**
|
| 356 | * Sets the delay to minDelay if parameter given is 1, else return false.
|
| 357 | */
|
| 358 | public boolean setDelayToMin(int delay)
|
| 359 | {
|
| 360 | if (delay == 1 && this.getSpawnerWorld().isRemote)
|
| 361 | {
|
| 362 | this.spawnDelay = this.minSpawnDelay;
|
| 363 | return true;
|
| 364 | }
|
| 365 | else
|
| 366 | {
|
| 367 | return false;
|
| 368 | }
|
| 369 | }
|
| 370 |
|
| 371 | private MobSpawnerBaseLogic.WeightedRandomMinecart getRandomEntity()
|
| 372 | {
|
| 373 | return this.randomEntity;
|
| 374 | }
|
| 375 |
|
| 376 | public void setRandomEntity(MobSpawnerBaseLogic.WeightedRandomMinecart p_98277_1_)
|
| 377 | {
|
| 378 | this.randomEntity = p_98277_1_;
|
| 379 | }
|
| 380 |
|
| 381 | public abstract void func_98267_a(int id);
|
| 382 |
|
| 383 | public abstract World getSpawnerWorld();
|
| 384 |
|
| 385 | public abstract BlockPos getSpawnerPosition();
|
| 386 |
|
| 387 | public double getMobRotation()
|
| 388 | {
|
| 389 | return this.mobRotation;
|
| 390 | }
|
| 391 |
|
| 392 | public double getPrevMobRotation()
|
| 393 | {
|
| 394 | return this.prevMobRotation;
|
| 395 | }
|
| 396 |
|
| 397 | public class WeightedRandomMinecart extends WeightedRandom.Item
|
| 398 | {
|
| 399 | private final NBTTagCompound nbtData;
|
| 400 | private final String entityType;
|
| 401 |
|
| 402 | public WeightedRandomMinecart(NBTTagCompound tagCompound)
|
| 403 | {
|
| 404 | this(tagCompound.getCompoundTag("Properties"), tagCompound.getString("Type"), tagCompound.getInteger("Weight"));
|
| 405 | }
|
| 406 |
|
| 407 | public WeightedRandomMinecart(NBTTagCompound tagCompound, String type)
|
| 408 | {
|
| 409 | this(tagCompound, type, 1);
|
| 410 | }
|
| 411 |
|
| 412 | private WeightedRandomMinecart(NBTTagCompound tagCompound, String type, int weight)
|
| 413 | {
|
| 414 | super(weight);
|
| 415 |
|
| 416 | if (type.equals("Minecart"))
|
| 417 | {
|
| 418 | if (tagCompound != null)
|
| 419 | {
|
| 420 | type = EntityMinecart.EnumMinecartType.byNetworkID(tagCompound.getInteger("Type")).getName();
|
| 421 | }
|
| 422 | else
|
| 423 | {
|
| 424 | type = "MinecartRideable";
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | this.nbtData = tagCompound;
|
| 429 | this.entityType = type;
|
| 430 | }
|
| 431 |
|
| 432 | public NBTTagCompound toNBT()
|
| 433 | {
|
| 434 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 435 | nbttagcompound.setTag("Properties", this.nbtData);
|
| 436 | nbttagcompound.setString("Type", this.entityType);
|
| 437 | nbttagcompound.setInteger("Weight", this.itemWeight);
|
| 438 | return nbttagcompound;
|
| 439 | }
|
| 440 | }
|
| 441 | }
|
| 442 |
|