Advertisement
Creepinson

Untitled

Jun 17th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. package me.creepinson.entity;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockFalling;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.block.state.IBlockState;
  9. import net.minecraft.crash.CrashReportCategory;
  10. import net.minecraft.entity.EntityCreature;
  11. import net.minecraft.entity.MoverType;
  12. import net.minecraft.entity.SharedMonsterAttributes;
  13. import net.minecraft.entity.ai.EntityAILookIdle;
  14. import net.minecraft.entity.ai.EntityAIPanic;
  15. import net.minecraft.entity.ai.EntityAISwimming;
  16. import net.minecraft.entity.ai.EntityAIWander;
  17. import net.minecraft.init.Blocks;
  18. import net.minecraft.init.Items;
  19. import net.minecraft.item.Item;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.nbt.NBTTagCompound;
  22. import net.minecraft.network.datasync.DataParameter;
  23. import net.minecraft.network.datasync.DataSerializers;
  24. import net.minecraft.network.datasync.EntityDataManager;
  25. import net.minecraft.util.DamageSource;
  26. import net.minecraft.util.ResourceLocation;
  27. import net.minecraft.util.datafix.DataFixer;
  28. import net.minecraft.util.math.BlockPos;
  29. import net.minecraft.world.World;
  30. import net.minecraftforge.fml.relauncher.Side;
  31. import net.minecraftforge.fml.relauncher.SideOnly;
  32.  
  33. public class EntityMovingBlock extends EntityCreature
  34. {
  35. private IBlockState fallTile;
  36. public NBTTagCompound tileEntityData;
  37. protected static final DataParameter<BlockPos> ORIGIN = EntityDataManager.<BlockPos>createKey(EntityMovingBlock.class, DataSerializers.BLOCK_POS);
  38. World world = null;
  39.  
  40.  
  41. public EntityMovingBlock(World worldIn)
  42. {
  43. super(worldIn);
  44. this.world = worldIn;
  45.  
  46. }
  47.  
  48. public EntityMovingBlock(World worldIn, double x, double y, double z, IBlockState fallingBlockState)
  49. {
  50. super(worldIn);
  51. this.world = worldIn;
  52. this.fallTile = fallingBlockState;
  53. this.setSize(0.98F, 0.98F);
  54. this.setPosition(x, y, z);
  55. this.motionX = 0.0D;
  56. this.motionY = 0.0D;
  57. this.motionZ = 0.0D;
  58. this.prevPosX = x;
  59. this.prevPosY = y;
  60. this.prevPosZ = z;
  61.  
  62. experienceValue = 3;
  63.  
  64. setNoAI(!true);
  65. this.tasks.addTask(2, new EntityAISwimming(this));
  66. this.tasks.addTask(9, new EntityAIWander(this, 1.0D));
  67. this.tasks.addTask(6, new EntityAILookIdle(this));
  68. this.tasks.addTask(7, new EntityAIWander(this, 0.8D));
  69. this.tasks.addTask(10, new EntityAIPanic(this, 1.2D));
  70.  
  71. }
  72.  
  73. protected void applyEntityAttributes() {
  74. super.applyEntityAttributes();
  75. this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.15D);
  76. this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(10D);
  77. if (this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE) != null)
  78. this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(2.0);
  79. }
  80. @Override
  81. protected Item getDropItem() {
  82. return new ItemStack(this.fallTile.getBlock()).getItem();
  83. }
  84.  
  85.  
  86. /**
  87. * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
  88. * prevent them from trampling crops
  89. */
  90. protected boolean canTriggerWalking()
  91. {
  92. return false;
  93. }
  94.  
  95. protected void entityInit()
  96. {
  97.  
  98. }
  99.  
  100. /**
  101. * Returns true if other Entities should be prevented from moving through this Entity.
  102. */
  103. public boolean canBeCollidedWith()
  104. {
  105. return !this.isDead;
  106. }
  107.  
  108. /**
  109. * Called to update the entity's position/logic.
  110. */
  111. public void onUpdate()
  112. {
  113. Block block = this.fallTile.getBlock();
  114.  
  115. if (this.fallTile.getMaterial() == Material.AIR)
  116. {
  117. this.setDead();
  118. }
  119. else
  120. {
  121. this.prevPosX = this.posX;
  122. this.prevPosY = this.posY;
  123. this.prevPosZ = this.posZ;
  124.  
  125.  
  126. BlockPos blockpos = new BlockPos(this);
  127.  
  128. if (this.world.getBlockState(blockpos).getBlock() == block)
  129. {
  130. this.world.setBlockToAir(blockpos);
  131. }
  132. else if (!this.world.isRemote)
  133. {
  134. this.setDead();
  135. return;
  136. }
  137. }
  138.  
  139.  
  140. this.move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);
  141. this.motionX *= 0.9800000190734863D;
  142. this.motionY *= 0.9800000190734863D;
  143. this.motionZ *= 0.9800000190734863D;
  144.  
  145. if (!this.world.isRemote)
  146. {
  147. BlockPos blockpos1 = new BlockPos(this);
  148.  
  149. if (this.onGround)
  150. {
  151. IBlockState iblockstate = this.world.getBlockState(blockpos1);
  152.  
  153. if (this.world.isAirBlock(new BlockPos(this.posX, this.posY - 0.009999999776482582D, this.posZ))) //Forge: Don't indent below.
  154. if (BlockFalling.canFallThrough(this.world.getBlockState(new BlockPos(this.posX, this.posY - 0.009999999776482582D, this.posZ))))
  155. {
  156. this.onGround = false;
  157. return;
  158. }
  159.  
  160. this.motionX *= 0.699999988079071D;
  161. this.motionZ *= 0.699999988079071D;
  162. this.motionY *= -0.5D;
  163.  
  164. if (iblockstate.getBlock() != Blocks.PISTON_EXTENSION)
  165. {
  166. this.setDead();
  167.  
  168.  
  169. }
  170.  
  171. }
  172.  
  173. }
  174. }
  175.  
  176.  
  177.  
  178.  
  179. public static void registerFixesFallingBlock(DataFixer fixer)
  180. {
  181. }
  182.  
  183. /**
  184. * (abstract) Protected helper method to write subclass entity data to NBT.
  185. */
  186. public void writeEntityToNBT(NBTTagCompound compound)
  187. {
  188. Block block = this.fallTile != null ? this.fallTile.getBlock() : Blocks.AIR;
  189. ResourceLocation resourcelocation = (ResourceLocation)Block.REGISTRY.getNameForObject(block);
  190. compound.setString("Block", resourcelocation == null ? "" : resourcelocation.toString());
  191. compound.setByte("Data", (byte)block.getMetaFromState(this.fallTile));
  192.  
  193.  
  194. if (this.tileEntityData != null)
  195. {
  196. compound.setTag("TileEntityData", this.tileEntityData);
  197. }
  198. }
  199.  
  200. /**
  201. * (abstract) Protected helper method to read subclass entity data from NBT.
  202. */
  203. public void readEntityFromNBT(NBTTagCompound compound)
  204. {
  205. int i = compound.getByte("Data") & 255;
  206.  
  207. if (compound.hasKey("Block", 8))
  208. {
  209. this.fallTile = Block.getBlockFromName(compound.getString("Block")).getStateFromMeta(i);
  210. }
  211. else if (compound.hasKey("TileID", 99))
  212. {
  213. this.fallTile = Block.getBlockById(compound.getInteger("TileID")).getStateFromMeta(i);
  214. }
  215. else
  216. {
  217. this.fallTile = Block.getBlockById(compound.getByte("Tile") & 255).getStateFromMeta(i);
  218. }
  219. Block block = this.fallTile.getBlock();
  220. if (block == null || block.getDefaultState().getMaterial() == Material.AIR)
  221. {
  222. this.fallTile = Blocks.SAND.getDefaultState();
  223. }
  224. }
  225.  
  226.  
  227.  
  228. public void addEntityCrashInfo(CrashReportCategory category)
  229. {
  230. super.addEntityCrashInfo(category);
  231.  
  232. if (this.fallTile != null)
  233. {
  234. Block block = this.fallTile.getBlock();
  235. category.addCrashSection("Immitating block ID", Integer.valueOf(Block.getIdFromBlock(block)));
  236. category.addCrashSection("Immitating block data", Integer.valueOf(block.getMetaFromState(this.fallTile)));
  237. }
  238. }
  239.  
  240. @SideOnly(Side.CLIENT)
  241. public World getWorldObj()
  242. {
  243. return this.world;
  244. }
  245.  
  246. /**
  247. * Return whether this entity should be rendered as on fire.
  248. */
  249. @SideOnly(Side.CLIENT)
  250. public boolean canRenderOnFire()
  251. {
  252. return false;
  253. }
  254.  
  255. @Nullable
  256. public IBlockState getBlock()
  257. {
  258. return this.fallTile;
  259. }
  260.  
  261. public boolean ignoreItemEntityData()
  262. {
  263. return true;
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement