Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.96 KB | None | 0 0
  1. package thut.api.entity.blockentity;
  2.  
  3. import com.google.common.collect.Lists;
  4. import io.netty.buffer.ByteBuf;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.UUID;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.annotation.Nullable;
  12. import net.minecraft.block.Block;
  13. import net.minecraft.block.state.IBlockState;
  14. import net.minecraft.entity.Entity;
  15. import net.minecraft.entity.EntityLivingBase;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraft.entity.player.EntityPlayerMP;
  18. import net.minecraft.inventory.EntityEquipmentSlot;
  19. import net.minecraft.item.Item;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.nbt.NBTTagCompound;
  22. import net.minecraft.network.PacketBuffer;
  23. import net.minecraft.potion.PotionEffect;
  24. import net.minecraft.tileentity.TileEntity;
  25. import net.minecraft.util.DamageSource;
  26. import net.minecraft.util.EnumActionResult;
  27. import net.minecraft.util.EnumHand;
  28. import net.minecraft.util.EnumHandSide;
  29. import net.minecraft.util.math.AxisAlignedBB;
  30. import net.minecraft.util.math.BlockPos;
  31. import net.minecraft.util.math.Vec3d;
  32. import net.minecraft.world.World;
  33. import net.minecraftforge.common.ForgeHooks;
  34. import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
  35. import net.minecraftforge.fml.relauncher.Side;
  36. import net.minecraftforge.fml.relauncher.SideOnly;
  37. import thut.core.common.ThutCore;
  38.  
  39. public abstract class BlockEntityBase
  40. extends EntityLivingBase
  41. implements IEntityAdditionalSpawnData, IBlockEntity
  42. {
  43. public static int ACCELERATIONTICKS = 20;
  44. public BlockPos boundMin = BlockPos.ORIGIN;
  45. public BlockPos boundMax = BlockPos.ORIGIN;
  46. private BlockEntityWorld fake_world;
  47. private boolean shouldRevert = true;
  48. public double speedUp = 0.5D;
  49. public double speedDown = -0.5D;
  50. public double speedHoriz = 0.5D;
  51. public double acceleration = 0.05D;
  52. public boolean toMoveY = false;
  53. public boolean toMoveX = false;
  54. public boolean toMoveZ = false;
  55. public boolean hasPassenger = false;
  56. int n = 0;
  57. boolean first = true;
  58. Random r = new Random();
  59. public UUID owner;
  60. public List<AxisAlignedBB> blockBoxes = Lists.newArrayList();
  61. public IBlockState[][][] blocks = (IBlockState[][][])null;
  62. public TileEntity[][][] tiles = (TileEntity[][][])null;
  63. BlockEntityUpdater collider;
  64. BlockEntityInteractHandler interacter;
  65.  
  66. public BlockEntityBase(World par1World)
  67. {
  68. super(par1World);
  69. this.ignoreFrustumCheck = true;
  70. this.hurtResistantTime = 0;
  71. this.isImmuneToFire = true;
  72. }
  73.  
  74. public BlockEntityWorld getFakeWorld()
  75. {
  76. if (this.fake_world == null) {
  77. this.fake_world = new BlockEntityWorld(this, this.world);
  78. }
  79. return this.fake_world;
  80. }
  81.  
  82. public BlockEntityBase(World world, double x, double y, double z)
  83. {
  84. this(world);
  85. setPosition(x, y, z);
  86. this.r.setSeed(100L);
  87. }
  88.  
  89. public boolean attackEntityFrom(DamageSource source, float amount)
  90. {
  91. return false;
  92. }
  93.  
  94. public void knockBack(Entity entityIn, float strenght, double xRatio, double zRatio) {}
  95.  
  96. protected double getSpeed(double pos, double destPos, double speed, double speedPos, double speedNeg)
  97. {
  98. if (!getEntityWorld().isAreaLoaded(getPosition(), 8)) {
  99. return 0.0D;
  100. }
  101. double ds = speed;
  102. double dp = destPos - pos;
  103. if (dp > 0.0D)
  104. {
  105. boolean tooFast = pos + ds * (ACCELERATIONTICKS + 1) > destPos;
  106. if (!tooFast) {
  107. ds = Math.min(speedPos, ds + this.acceleration * speedPos);
  108. } else {
  109. while ((ds >= 0.0D) && (tooFast))
  110. {
  111. ds -= this.acceleration * speedPos / 10.0D;
  112. tooFast = pos + ds * (ACCELERATIONTICKS + 1) > destPos;
  113. }
  114. }
  115. return ds;
  116. }
  117. if (dp < 0.0D)
  118. {
  119. speedNeg = Math.abs(speedNeg);
  120. boolean tooFast = pos + ds * (ACCELERATIONTICKS + 1) < destPos;
  121. if (!tooFast) {
  122. ds = Math.max(-speedNeg, ds - this.acceleration * speedNeg);
  123. } else {
  124. while ((ds <= 0.0D) && (tooFast))
  125. {
  126. ds += this.acceleration * speedNeg / 10.0D;
  127. tooFast = pos + ds * (ACCELERATIONTICKS + 1) < destPos;
  128. }
  129. }
  130. return ds;
  131. }
  132. return 0.0D;
  133. }
  134.  
  135. protected abstract void accelerate();
  136.  
  137. public void applyEntityCollision(Entity entity)
  138. {
  139. if (this.collider == null)
  140. {
  141. this.collider = new BlockEntityUpdater(this);
  142. this.collider.onSetPosition();
  143. }
  144. try
  145. {
  146. this.collider.applyEntityCollision(entity);
  147. }
  148. catch (Exception e)
  149. {
  150. e.printStackTrace();
  151. }
  152. }
  153.  
  154. public boolean canBeCollidedWith()
  155. {
  156. return !this.isDead;
  157. }
  158.  
  159. public boolean canBePushed()
  160. {
  161. return true;
  162. }
  163.  
  164. public boolean canRenderOnFire()
  165. {
  166. return false;
  167. }
  168.  
  169. public void checkCollision()
  170. {
  171. int xMin = this.boundMin.getX();
  172. int zMin = this.boundMin.getZ();
  173. int xMax = this.boundMax.getX();
  174. int zMax = this.boundMax.getZ();
  175.  
  176. List<?> list = this.world.getEntitiesWithinAABBExcludingEntity(this, new AxisAlignedBB(this.posX + (xMin - 1), this.posY, this.posZ + (zMin - 1), this.posX + xMax + 1.0D, this.posY + 64.0D, this.posZ + zMax + 1.0D));
  177. if ((list != null) && (!list.isEmpty()))
  178. {
  179. if ((list.size() == 1) && (getRecursivePassengers() != null) &&
  180. (!getRecursivePassengers().isEmpty())) {
  181. return;
  182. }
  183. for (int i = 0; i < list.size(); i++)
  184. {
  185. Entity entity = (Entity)list.get(i);
  186. applyEntityCollision(entity);
  187. if (((entity instanceof EntityPlayerMP)) &&
  188. (entity.getEntityBoundingBox().grow(2.0D).intersects(getEntityBoundingBox()))) {
  189. this.hasPassenger = true;
  190. }
  191. }
  192. }
  193. }
  194.  
  195. protected abstract boolean checkAccelerationConditions();
  196.  
  197. protected abstract void doMotion();
  198.  
  199. public void resetPositionToBB()
  200. {
  201. BlockPos min = getMin();
  202. BlockPos max = getMax();
  203. float xDiff = (max.getX() - min.getX()) / 2.0F;
  204. float zDiff = (max.getZ() - min.getZ()) / 2.0F;
  205. AxisAlignedBB axisalignedbb = getEntityBoundingBox();
  206. if (xDiff % 1.0F != 0.0F) {
  207. this.posX = (axisalignedbb.minX + xDiff);
  208. } else {
  209. this.posX = ((axisalignedbb.minX + axisalignedbb.maxX) / 2.0D);
  210. }
  211. this.posY = axisalignedbb.minY;
  212. if (zDiff % 1.0F != 0.0F) {
  213. this.posZ = (axisalignedbb.minZ + zDiff);
  214. } else {
  215. this.posZ = ((axisalignedbb.minZ + axisalignedbb.maxZ) / 2.0D);
  216. }
  217. }
  218.  
  219. protected void entityInit()
  220. {
  221. super.entityInit();
  222. }
  223.  
  224. public AxisAlignedBB getBoundingBox()
  225. {
  226. return null;
  227. }
  228.  
  229. public boolean getCanSpawnHere()
  230. {
  231. return false;
  232. }
  233.  
  234. protected abstract BlockEntityInteractHandler createInteractHandler();
  235.  
  236. public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand)
  237. {
  238. if (this.interacter == null) {
  239. this.interacter = createInteractHandler();
  240. }
  241. try
  242. {
  243. return this.interacter.applyPlayerInteraction(player, vec, player.getHeldItem(hand), hand);
  244. }
  245. catch (Exception e)
  246. {
  247. ThutCore.logger.log(Level.SEVERE, "Error handling interactions for " + this, e);
  248. }
  249. return super.applyPlayerInteraction(player, vec, hand);
  250. }
  251.  
  252. public boolean processInitialInteract(EntityPlayer player, EnumHand hand)
  253. {
  254. if (this.interacter == null) {
  255. this.interacter = createInteractHandler();
  256. }
  257. return this.interacter.processInitialInteract(player, player.getHeldItem(hand), hand);
  258. }
  259.  
  260. public boolean isPotionApplicable(PotionEffect par1PotionEffect)
  261. {
  262. return false;
  263. }
  264.  
  265. protected abstract void preColliderTick();
  266.  
  267. protected abstract void onGridAlign();
  268.  
  269. public void onUpdate()
  270. {
  271. if (ForgeHooks.onLivingUpdate(this)) {
  272. return;
  273. }
  274. if (this.collider == null)
  275. {
  276. this.collider = new BlockEntityUpdater(this);
  277. this.collider.onSetPosition();
  278. }
  279. preColliderTick();
  280. this.prevPosY = this.posY;
  281. this.prevPosX = this.posX;
  282. this.prevPosZ = this.posZ;
  283. this.collider.onUpdate();
  284. accelerate();
  285. int dy = (int)(this.motionY * 16.0D);
  286. int dx = (int)(this.motionX * 16.0D);
  287. int dz = (int)(this.motionZ * 16.0D);
  288. if ((this.toMoveY) || (this.toMoveX) || (this.toMoveZ))
  289. {
  290. doMotion();
  291. }
  292. else if ((dx == dy) && (dy == dz) && (dz == 0) && (!this.world.isRemote))
  293. {
  294. BlockPos pos = getPosition();
  295. boolean update = (this.posX != pos.getX() + 0.5D) || (this.posY != Math.round(this.posY)) || (this.posZ != pos.getZ() + 0.5D);
  296. if (update) {
  297. onGridAlign();
  298. }
  299. }
  300. checkCollision();
  301. }
  302.  
  303. public void readBlocks(NBTTagCompound nbt)
  304. {
  305. if (nbt.hasKey("Blocks"))
  306. {
  307. NBTTagCompound blockTag = nbt.getCompoundTag("Blocks");
  308. int sizeX = blockTag.getInteger("BlocksLengthX");
  309. int sizeZ = blockTag.getInteger("BlocksLengthZ");
  310. int sizeY = blockTag.getInteger("BlocksLengthY");
  311. if ((sizeX == 0) || (sizeZ == 0)) {
  312. sizeX = sizeZ = nbt.getInteger("BlocksLength");
  313. }
  314. if (sizeY == 0) {
  315. sizeY = 1;
  316. }
  317. int version = blockTag.getInteger("v");
  318. this.blocks = new IBlockState[sizeX][sizeY][sizeZ];
  319. this.tiles = new TileEntity[sizeX][sizeY][sizeZ];
  320. for (int i = 0; i < sizeX; i++) {
  321. for (int k = 0; k < sizeY; k++) {
  322. for (int j = 0; j < sizeZ; j++)
  323. {
  324. int n = -1;
  325. if (blockTag.hasKey("I" + i + "," + j)) {
  326. n = blockTag.getInteger("I" + i + "," + j);
  327. } else if (blockTag.hasKey("I" + i + "," + k + "," + j)) {
  328. n = blockTag.getInteger("I" + i + "," + k + "," + j);
  329. }
  330. if (n != -1)
  331. {
  332. IBlockState state;
  333. IBlockState state;
  334. if (version == 0)
  335. {
  336. Block b = Block.getBlockFromItem(Item.getItemById(n));
  337. int meta = blockTag.getInteger("M" + i + "," + k + "," + j);
  338. state = b.getStateFromMeta(meta);
  339. }
  340. else
  341. {
  342. Block b = Block.getBlockById(n);
  343. int meta = blockTag.getInteger("M" + i + "," + k + "," + j);
  344. state = b.getStateFromMeta(meta);
  345. }
  346. this.blocks[i][k][j] = state;
  347. if (blockTag.hasKey("T" + i + "," + k + "," + j)) {
  348. try
  349. {
  350. NBTTagCompound tag = blockTag.getCompoundTag("T" + i + "," + k + "," + j);
  351. this.tiles[i][k][j] = IBlockEntity.BlockEntityFormer.makeTile(tag);
  352. }
  353. catch (Exception e)
  354. {
  355. e.printStackTrace();
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. setBlocks(this.blocks);
  363. setTiles(this.tiles);
  364. }
  365. }
  366.  
  367. public void readEntityFromNBT(NBTTagCompound nbt)
  368. {
  369. super.readEntityFromNBT(nbt);
  370. if (nbt.hasKey("bounds"))
  371. {
  372. NBTTagCompound bounds = nbt.getCompoundTag("bounds");
  373. this.boundMin = new BlockPos(bounds.getDouble("minx"), bounds.getDouble("miny"), bounds.getDouble("minz"));
  374. this.boundMax = new BlockPos(bounds.getDouble("maxx"), bounds.getDouble("maxy"), bounds.getDouble("maxz"));
  375. }
  376. readBlocks(nbt);
  377. }
  378.  
  379. public void readSpawnData(ByteBuf data)
  380. {
  381. PacketBuffer buff = new PacketBuffer(data);
  382. NBTTagCompound tag = new NBTTagCompound();
  383. try
  384. {
  385. tag = buff.readCompoundTag();
  386. readEntityFromNBT(tag);
  387. }
  388. catch (Exception e)
  389. {
  390. e.printStackTrace();
  391. }
  392. }
  393.  
  394. public void setDead()
  395. {
  396. if ((!getEntityWorld().isRemote) && (!this.isDead) && (this.addedToChunk) && (this.shouldRevert)) {
  397. IBlockEntity.BlockEntityFormer.RevertEntity(this);
  398. }
  399. super.setDead();
  400. }
  401.  
  402. public void setDropItemsWhenDead(boolean dropWhenDead)
  403. {
  404. this.shouldRevert = dropWhenDead;
  405. }
  406.  
  407. public void setPosition(double x, double y, double z)
  408. {
  409. super.setPosition(x, y, z);
  410. if (this.collider != null) {
  411. this.collider.onSetPosition();
  412. }
  413. }
  414.  
  415. public void writeBlocks(NBTTagCompound nbt)
  416. {
  417. if (this.blocks != null)
  418. {
  419. NBTTagCompound blocksTag = new NBTTagCompound();
  420. blocksTag.setInteger("BlocksLengthX", this.blocks.length);
  421. blocksTag.setInteger("BlocksLengthY", this.blocks[0].length);
  422. blocksTag.setInteger("BlocksLengthZ", this.blocks[0][0].length);
  423. blocksTag.setInteger("v", 1);
  424. int sizeX = this.blocks.length;
  425. int sizeY = this.blocks[0].length;
  426. int sizeZ = this.blocks[0][0].length;
  427. for (int i = 0; i < sizeX; i++) {
  428. for (int k = 0; k < sizeY; k++) {
  429. for (int j = 0; j < sizeZ; j++)
  430. {
  431. IBlockState b = this.blocks[i][k][j];
  432. if (b != null)
  433. {
  434. blocksTag.setInteger("I" + i + "," + k + "," + j, Block.getIdFromBlock(b.getBlock()));
  435. blocksTag.setInteger("M" + i + "," + k + "," + j, b.getBlock().getMetaFromState(b));
  436. try
  437. {
  438. if (this.tiles[i][k][j] != null)
  439. {
  440. NBTTagCompound tag = new NBTTagCompound();
  441. tag = this.tiles[i][k][j].writeToNBT(tag);
  442. blocksTag.setTag("T" + i + "," + k + "," + j, tag);
  443. }
  444. }
  445. catch (Exception e)
  446. {
  447. e.printStackTrace();
  448. }
  449. }
  450. }
  451. }
  452. }
  453. nbt.setTag("Blocks", blocksTag);
  454. }
  455. }
  456.  
  457. public void writeEntityToNBT(NBTTagCompound nbt)
  458. {
  459. super.writeEntityToNBT(nbt);
  460. NBTTagCompound vector = new NBTTagCompound();
  461. vector.setDouble("minx", this.boundMin.getX());
  462. vector.setDouble("miny", this.boundMin.getY());
  463. vector.setDouble("minz", this.boundMin.getZ());
  464. vector.setDouble("maxx", this.boundMax.getX());
  465. vector.setDouble("maxy", this.boundMax.getY());
  466. vector.setDouble("maxz", this.boundMax.getZ());
  467. nbt.setTag("bounds", vector);
  468. try
  469. {
  470. writeBlocks(nbt);
  471. }
  472. catch (Exception e)
  473. {
  474. e.printStackTrace();
  475. }
  476. }
  477.  
  478. public void writeSpawnData(ByteBuf data)
  479. {
  480. PacketBuffer buff = new PacketBuffer(data);
  481. NBTTagCompound tag = new NBTTagCompound();
  482. writeEntityToNBT(tag);
  483. buff.writeCompoundTag(tag);
  484. }
  485.  
  486. @SideOnly(Side.CLIENT)
  487. public boolean isInRangeToRenderDist(double distance)
  488. {
  489. return true;
  490. }
  491.  
  492. public boolean canPassengerSteer()
  493. {
  494. return true;
  495. }
  496.  
  497. public Iterable<ItemStack> getArmorInventoryList()
  498. {
  499. return Lists.newArrayList();
  500. }
  501.  
  502. public ItemStack getItemStackFromSlot(EntityEquipmentSlot slotIn)
  503. {
  504. return ItemStack.EMPTY;
  505. }
  506.  
  507. public void setItemStackToSlot(EntityEquipmentSlot slotIn, ItemStack stack) {}
  508.  
  509. public ItemStack getHeldItem(EnumHand hand)
  510. {
  511. return ItemStack.EMPTY;
  512. }
  513.  
  514. public void setHeldItem(EnumHand hand, @Nullable ItemStack stack) {}
  515.  
  516. public EnumHandSide getPrimaryHand()
  517. {
  518. return EnumHandSide.LEFT;
  519. }
  520.  
  521. public void setBlocks(IBlockState[][][] blocks)
  522. {
  523. this.blocks = blocks;
  524. }
  525.  
  526. public IBlockState[][][] getBlocks()
  527. {
  528. return this.blocks;
  529. }
  530.  
  531. public void setTiles(TileEntity[][][] tiles)
  532. {
  533. this.tiles = tiles;
  534. }
  535.  
  536. public TileEntity[][][] getTiles()
  537. {
  538. return this.tiles;
  539. }
  540.  
  541. public BlockPos getMin()
  542. {
  543. return this.boundMin;
  544. }
  545.  
  546. public BlockPos getMax()
  547. {
  548. return this.boundMax;
  549. }
  550.  
  551. public void setMin(BlockPos pos)
  552. {
  553. this.boundMin = pos;
  554. }
  555.  
  556. public void setMax(BlockPos pos)
  557. {
  558. this.boundMax = pos;
  559. }
  560.  
  561. public void setFakeWorld(BlockEntityWorld world)
  562. {
  563. this.fake_world = world;
  564. }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement