Skip to content

Instantly share code, notes, and snippets.

@izzyaxel
Last active June 3, 2018 19:04
Show Gist options
  • Save izzyaxel/12f5898399866e871566 to your computer and use it in GitHub Desktop.
Save izzyaxel/12f5898399866e871566 to your computer and use it in GitHub Desktop.
The TIleEntity for a 'black hole' type of block
public class TEBlackHole extends TileEntity
{
//I use constant variables for this sort of thing
final int PULLRADIUS = 20;
final int KILLRADIUS = 3;
//Should updateEntity() be called?
@Override
public boolean canUpdate()
{
return true;
}
@Override
public void updateEntity()
{
//Using entity and checking for types you don't want allows for finer control, in this case all projectiles get sucked in too, so if a player were getting pulled, they couldn't even ender pearl out.
List<Entity> allAround = this.worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(this.xCoord - this.PULLRADIUS, this.yCoord - this.PULLRADIUS, this.zCoord - this.PULLRADIUS, this.xCoord + this.PULLRADIUS, this.yCoord + this.PULLRADIUS, this.zCoord + this.PULLRADIUS));
for(Entity ent : allAround)
{
//Don't pull players, just to be able to test this thing lol
//And certainly don't do anything to the Dragon or Ender Crystals, probably the Wither too...and there may be others I'm not thinking of right now.
if(!(ent instanceof EntityPlayer) && !(ent instanceof EntityDragon) && !(ent instanceof EntityEnderCrystal) && !(ent instanceof EntityWither))
{
//The number you divide by is the magnitude, lower numbers will pull faster, higher will pull slower
//You want to add 0.5 to the TileEntity's position because the default position of a block is a corner
//You can substitute different formulae in here to get different types of pull movement, such as falloff in pull strength
//Use += instead to pull faster and faster on each tick. You'll probably want to increase the magnitude number, 15 seems to be the highest you'd want. This will also make entities overshoot the center and get pulled back in over and over
ent.motionX = ((this.xCoord + 0.5d) - ent.posX) / 5;
ent.motionY = ((this.yCoord + 0.5d) - ent.posY) / 5;
ent.motionZ = ((this.zCoord + 0.5d) - ent.posZ) / 5;
ent.velocityChanged = true;
}
}
//Optional, anything inside this box will take constant damage, and projectiles/dropped items will be destroyed
List<Entity> killBox = this.worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(this.xCoord - this.KILLRADIUS, this.yCoord - this.KILLRADIUS, this.zCoord - this.KILLRADIUS, this.xCoord + this.KILLRADIUS, this.yCoord + this.KILLRADIUS, this.zCoord + this.KILLRADIUS));
for(Entity ent : killBox)
{
//Again, it'd be rather hard to test without excluding players...
if(!(ent instanceof EntityPlayer))
{
//Destroy any non-living entity that gets too close
if(!(ent instanceof EntityLiving))
{
this.worldObj.removeEntity(ent);
}
//Damage anything in this radius constantly, could use removeEntity here too if you don't want the EntityLiving dying effects
ent.attackEntityFrom(DamageSource.outOfWorld, 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment