daily pastebin goal
4%
SHARE
TWEET

TileEntityTinySpawner

a guest Oct 20th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.tmtravlr.mapgadgets.blocks;
  2.  
  3. import java.util.UUID;
  4.  
  5. import com.tmtravlr.mapgadgets.ConfigLoader;
  6.  
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.EntityList;
  9. import net.minecraft.entity.EntityLiving;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.util.EntitySelectors;
  14. import net.minecraft.util.ITickable;
  15. import net.minecraft.util.ResourceLocation;
  16. import net.minecraft.util.math.BlockPos;
  17. import net.minecraft.world.chunk.storage.AnvilChunkLoader;
  18.  
  19. /**
  20.  * Tile entity which spawns a single mob and disappears
  21.  * @author Rebeca Rey (Tmtravlr)
  22.  * @since January 2018
  23.  */
  24. public class TileEntityTinySpawner extends TileEntity implements ITickable {
  25.  
  26.     private NBTTagCompound entityTag = new NBTTagCompound();
  27.    
  28.     @Override
  29.     public void readFromNBT(NBTTagCompound compound) {
  30.         super.readFromNBT(compound);
  31.         this.entityTag = compound.getCompoundTag("EntityTag");
  32.     }
  33.  
  34.     @Override
  35.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  36.         super.writeToNBT(compound);
  37.         compound.setTag("EntityTag", entityTag);
  38.         return compound;
  39.     }
  40.  
  41.     @Override
  42.     public void update() {
  43.         if (!this.world.isRemote && this.isNonCreativePlayerInRange((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D, ConfigLoader.tinySpawnerSpawnRadius)) {
  44.             this.turnBackIntoEntity();
  45.         }
  46.     }
  47.  
  48.     @Override
  49.     public boolean onlyOpsCanSetNbt() {
  50.         return true;
  51.     }
  52.    
  53.     public void setEntityTag(NBTTagCompound entityTag) {
  54.         this.entityTag = entityTag;
  55.     }
  56.    
  57.     public void turnBackIntoEntity() {
  58.         ResourceLocation entityId = new ResourceLocation(this.entityTag.getString("id"));
  59.        
  60.         Entity entity = AnvilChunkLoader.readWorldEntityPos(this.entityTag, world, this.pos.getX() + 0.5, this.pos.getY(), this.pos.getZ() + 0.5, true);
  61.        
  62.         this.entityTag = new NBTTagCompound();
  63.         this.world.setBlockToAir(this.pos);
  64.     }
  65.    
  66.     private boolean isNonCreativePlayerInRange(double x, double y, double z, double range) {
  67.         for (int i = 0; i < this.world.playerEntities.size(); ++i) {
  68.             EntityPlayer player = this.world.playerEntities.get(i);
  69.  
  70.             if (!(player.isCreative() || player.isSpectator())) {
  71.                 double playerDistance = player.getDistanceSq(x, y, z);
  72.  
  73.                 if (range < 0 || playerDistance < range * range) {
  74.                     return true;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         return false;
  80.     }
  81.    
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top