Advertisement
Guest User

Untitled

a guest
Oct 19th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 KB | None | 0 0
  1. package some.legacy.code;
  2.  
  3. import com.google.common.collect.Lists;
  4. import net.minecraft.server.v1_8_R3.*;
  5. import org.bukkit.Location;
  6. import org.bukkit.World;
  7. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  8. import org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory;
  9. import org.bukkit.event.entity.CreatureSpawnEvent;
  10. import org.bukkit.event.player.PlayerTeleportEvent;
  11. import org.bukkit.util.Vector;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15.  
  16. public class CustomFallingBlock extends EntityFallingBlock {
  17.  
  18. boolean ignoreGravity = true;
  19. public int data;
  20.  
  21. private IBlockData block;
  22. public int ticksLived;
  23. public boolean dropItem = true;
  24.  
  25. private boolean e;
  26. private boolean hurtEntities;
  27. private int fallHurtMax = 40;
  28. private float fallHurtAmount = 2.0F;
  29.  
  30. public NBTTagCompound tileEntityData;
  31.  
  32. public CustomFallingBlock(World world) {
  33. super(((CraftWorld)world).getHandle());
  34. this.dropItem = false;
  35. }
  36.  
  37. public CustomFallingBlock(World world, double x, double y, double z, IBlockData blockData) {
  38. super(((CraftWorld)world).getHandle(), x, y, z, blockData);
  39. }
  40.  
  41. public CustomFallingBlock(World world, double x, double y, double z, IBlockData iblockdata, int data) {
  42. super(((CraftWorld)world).getHandle());
  43.  
  44. this.block = iblockdata;
  45. this.data = data;
  46. this.k = true;
  47. this.setSize(0.98F, 0.98F);
  48. this.setPosition(x, y, z);
  49. this.motX = 0.0D;
  50. this.motY = 0.0D;
  51. this.motZ = 0.0D;
  52. this.lastX = x;
  53. this.lastY = y;
  54. this.lastZ = z;
  55. }
  56.  
  57. protected void h() {}
  58.  
  59. public boolean ad() {
  60. return !this.dead;
  61. }
  62.  
  63. protected boolean s_() {
  64. return false;
  65. }
  66.  
  67. public void t_() {
  68. Block block = this.block.getBlock();
  69.  
  70. if (block.getMaterial() == Material.AIR) {
  71. this.die();
  72. } else {
  73. this.lastX = this.locX;
  74. this.lastY = this.locY;
  75. this.lastZ = this.locZ;
  76. BlockPosition blockposition;
  77.  
  78. if (this.ticksLived++ == 0) {
  79. blockposition = new BlockPosition(this);
  80. if (this.world.getType(blockposition).getBlock() == block && !CraftEventFactory.callEntityChangeBlockEvent(this, blockposition.getX(), blockposition.getY(), blockposition.getZ(), Blocks.AIR, 0).isCancelled()) {
  81. if (!this.ignoreGravity) {
  82. this.world.setAir(blockposition);
  83. }
  84. } else if (!this.world.isClientSide) {
  85. if (!this.ignoreGravity) {
  86. this.die();
  87. }
  88. return;
  89. }
  90. }
  91. if (!this.ignoreGravity) {
  92. this.motY -= 0.03999999910593033D;
  93. }
  94. this.move(this.motX, this.motY, this.motZ);
  95. this.motX *= 0.9800000190734863D;
  96. if (!this.ignoreGravity) {
  97. this.motY *= 0.9800000190734863D;
  98. }
  99. this.motZ *= 0.9800000190734863D;
  100. if (!this.world.isClientSide) {
  101. blockposition = new BlockPosition(this);
  102. if (this.onGround) {
  103. this.motX *= 0.699999988079071D;
  104. this.motZ *= 0.699999988079071D;
  105. if (!this.ignoreGravity) {
  106. this.motY *= -0.5D;
  107. }
  108. if (this.world.getType(blockposition).getBlock() != Blocks.PISTON_EXTENSION && (!this.ignoreGravity)) {
  109. this.die();
  110. if (!this.e) {
  111. if (this.world.a(block, blockposition, true, EnumDirection.UP, (Entity) null, (ItemStack) null) && !BlockFalling.canFall(this.world, blockposition.down()) /* mimic the false conditions of setTypeIdAndData */ && blockposition.getX() >= -30000000 && blockposition.getZ() >= -30000000 && blockposition.getX() < 30000000 && blockposition.getZ() < 30000000 && blockposition.getY() >= 0 && blockposition.getY() < 256 && this.world.getType(blockposition) != this.block) {
  112. if (CraftEventFactory.callEntityChangeBlockEvent(this, blockposition.getX(), blockposition.getY(), blockposition.getZ(), this.block.getBlock(), this.block.getBlock().toLegacyData(this.block)).isCancelled()) {
  113. return;
  114. }
  115. this.world.setTypeAndData(blockposition, this.block, 3);
  116. // CraftBukkit end
  117. if (block instanceof BlockFalling) {
  118. ((BlockFalling) block).a_(this.world, blockposition);
  119. }
  120.  
  121. if (this.tileEntityData != null && block instanceof IContainer) {
  122. TileEntity tileentity = this.world.getTileEntity(blockposition);
  123.  
  124. if (tileentity != null) {
  125. NBTTagCompound nbttagcompound = new NBTTagCompound();
  126.  
  127. tileentity.b(nbttagcompound);
  128. Iterator iterator = this.tileEntityData.c().iterator();
  129.  
  130. while (iterator.hasNext()) {
  131. String s = (String) iterator.next();
  132. NBTBase nbtbase = this.tileEntityData.get(s);
  133.  
  134. if (!s.equals("x") && !s.equals("y") && !s.equals("z")) {
  135. nbttagcompound.set(s, nbtbase.clone());
  136. }
  137. }
  138.  
  139. tileentity.a(nbttagcompound);
  140. tileentity.update();
  141. }
  142. }
  143. } else if (this.dropItem && this.world.getGameRules().getBoolean("doEntityDrops")) {
  144. this.a(new ItemStack(block, 1, block.getDropData(this.block)), 0.0F);
  145. }
  146. }
  147. }
  148. } else if (((this.ticksLived > 100 && !this.world.isClientSide && (blockposition.getY() < 1 || blockposition.getY() > 256) || this.ticksLived > 600)
  149. && (!this.ignoreGravity))){
  150. if (this.dropItem && this.world.getGameRules().getBoolean("doEntityDrops")) {
  151. this.a(new ItemStack(block, 1, block.getDropData(this.block)), 0.0F);
  152. }
  153.  
  154. this.die();
  155. }
  156. }
  157.  
  158. }
  159. }
  160.  
  161. @Override
  162. public void move(double d0, double d1, double d2) {
  163. super.move(d0, d1, d2);
  164. }
  165.  
  166. public void e(float f, float f1) {
  167. Block block = this.block.getBlock();
  168.  
  169. if (this.hurtEntities) {
  170. int i = MathHelper.f(f - 1.0F);
  171.  
  172. if (i > 0) {
  173. ArrayList arraylist = Lists.newArrayList(this.world.getEntities(this, this.getBoundingBox()));
  174. boolean flag = block == Blocks.ANVIL;
  175. DamageSource damagesource = flag ? DamageSource.ANVIL : DamageSource.FALLING_BLOCK;
  176. Iterator iterator = arraylist.iterator();
  177.  
  178. while (iterator.hasNext()) {
  179. Entity entity = (Entity) iterator.next();
  180.  
  181. CraftEventFactory.entityDamage = this; // CraftBukkit
  182. entity.damageEntity(damagesource, (float) Math.min(MathHelper.d((float) i * this.fallHurtAmount), this.fallHurtMax));
  183. CraftEventFactory.entityDamage = null; // CraftBukkit
  184. }
  185.  
  186. if (flag && (double) this.random.nextFloat() < 0.05000000074505806D + (double) i * 0.05D) {
  187. int j = ((Integer) this.block.get(BlockAnvil.DAMAGE)).intValue();
  188.  
  189. ++j;
  190. if (j > 2) {
  191. this.e = true;
  192. } else {
  193. this.block = this.block.set(BlockAnvil.DAMAGE, Integer.valueOf(j));
  194. }
  195. }
  196. }
  197. }
  198. }
  199.  
  200. protected void b(NBTTagCompound nbttagcompound) {
  201. Block block = this.block != null ? this.block.getBlock() : Blocks.AIR;
  202. MinecraftKey minecraftkey = (MinecraftKey) Block.REGISTRY.c(block);
  203.  
  204. nbttagcompound.setString("Block", minecraftkey == null ? "" : minecraftkey.toString());
  205. nbttagcompound.setByte("Data", (byte) block.toLegacyData(this.block));
  206. nbttagcompound.setByte("Time", (byte) this.ticksLived);
  207. nbttagcompound.setBoolean("DropItem", this.dropItem);
  208. nbttagcompound.setBoolean("HurtEntities", this.hurtEntities);
  209. nbttagcompound.setFloat("FallHurtAmount", this.fallHurtAmount);
  210. nbttagcompound.setInt("FallHurtMax", this.fallHurtMax);
  211. if (this.tileEntityData != null) {
  212. nbttagcompound.set("TileEntityData", this.tileEntityData);
  213. }
  214.  
  215. }
  216.  
  217. protected void a(NBTTagCompound nbttagcompound) {
  218. int i = nbttagcompound.getByte("Data") & 255;
  219.  
  220. if (nbttagcompound.hasKeyOfType("Block", 8)) {
  221. this.block = Block.getByName(nbttagcompound.getString("Block")).fromLegacyData(i);
  222. } else if (nbttagcompound.hasKeyOfType("TileID", 99)) {
  223. this.block = Block.getById(nbttagcompound.getInt("TileID")).fromLegacyData(i);
  224. } else {
  225. this.block = Block.getById(nbttagcompound.getByte("Tile") & 255).fromLegacyData(i);
  226. }
  227.  
  228. this.ticksLived = nbttagcompound.getByte("Time") & 255;
  229. Block block = this.block.getBlock();
  230.  
  231. if (nbttagcompound.hasKeyOfType("HurtEntities", 99)) {
  232. this.hurtEntities = nbttagcompound.getBoolean("HurtEntities");
  233. this.fallHurtAmount = nbttagcompound.getFloat("FallHurtAmount");
  234. this.fallHurtMax = nbttagcompound.getInt("FallHurtMax");
  235. } else if (block == Blocks.ANVIL) {
  236. this.hurtEntities = true;
  237. }
  238.  
  239. if (nbttagcompound.hasKeyOfType("DropItem", 99)) {
  240. this.dropItem = nbttagcompound.getBoolean("DropItem");
  241. }
  242.  
  243. if (nbttagcompound.hasKeyOfType("TileEntityData", 10)) {
  244. this.tileEntityData = nbttagcompound.getCompound("TileEntityData");
  245. }
  246.  
  247. if (block == null || block.getMaterial() == Material.AIR) {
  248. this.block = Blocks.SAND.getBlockData();
  249. }
  250.  
  251. }
  252.  
  253. public void a(boolean flag) {
  254. this.hurtEntities = flag;
  255. }
  256.  
  257. public void appendEntityCrashDetails(CrashReportSystemDetails crashreportsystemdetails) {
  258. super.appendEntityCrashDetails(crashreportsystemdetails);
  259. if (this.block != null) {
  260. Block block = this.block.getBlock();
  261.  
  262. crashreportsystemdetails.a("Immitating block ID", (Object) Integer.valueOf(Block.getId(block)));
  263. crashreportsystemdetails.a("Immitating block data", (Object) Integer.valueOf(block.toLegacyData(this.block)));
  264. }
  265. }
  266.  
  267. public IBlockData getBlock() {
  268. return this.block;
  269. }
  270.  
  271. public int getBlockId() {
  272. return Block.getCombinedId(this.block);
  273. }
  274.  
  275. public Vector getVelocity() {
  276. return new Vector(this.motX, this.motY, this.motZ);
  277. }
  278.  
  279. public void setVelocity(Vector vel) {
  280. this.motX = vel.getX();
  281. this.motY = vel.getY();
  282. this.motZ = vel.getZ();
  283. this.velocityChanged = true;
  284. }
  285.  
  286. public Location getLocation() {
  287. return new Location(getWorldB(), this.locX, this.locY, this.locZ);
  288. }
  289.  
  290. public CraftWorld getWorldB() {
  291. return ((WorldServer)this.world).getWorld();
  292. }
  293.  
  294. public void setLocation(Location l) {
  295. this.locX = l.getX();
  296. this.locY = l.getY();
  297. this.locZ = l.getZ();
  298. }
  299.  
  300. public byte getBlockData() {
  301. return (byte)this.data;
  302. }
  303.  
  304. public void remove() {
  305. die();
  306. }
  307.  
  308. public boolean teleport(Location location) {
  309. return teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
  310. }
  311.  
  312. public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) {
  313. this.world = ((CraftWorld)location.getWorld()).getHandle();
  314. setLocation(location.getX(), location.getY(), location.getZ(),
  315. location.getYaw(), location.getPitch());
  316.  
  317. return true;
  318. }
  319.  
  320. // public CustomFallingBlock(Location location, int color) {
  321. // super(((CraftWorld)location.getWorld()).getHandle(), location.getX(), location.getY(), location.getZ(),
  322. // (new BlockCloth(Material.CLOTH).fromLegacyData(color)));
  323. // }
  324. //
  325. // public CustomFallingBlock(net.minecraft.server.v1_8_R3.World world) {
  326. // super(world);
  327. // }
  328.  
  329. /**
  330. * Spawns a falling block at location
  331. *
  332. * @param location Location to teleport the falling block to
  333. * @return Created falling block
  334. */
  335. public static Entity spawnCustomEntity(Location location) {
  336. double x = location.getBlockX() + 0.5;
  337. double y = location.getBlockY() + 1.5;
  338. double z = location.getBlockZ() + 0.5;
  339.  
  340. // IBlockData tempBlock = net.minecraft.server.v1_8_R2.Block.getByCombinedId(material.getId());
  341. // tempBlock = tempBlock.getBlock().fromLegacyData(data);
  342.  
  343. CustomFallingBlock entity = new CustomFallingBlock(location.getWorld(), x, y, z, Block.getByCombinedId(35), 5);
  344. entity.ticksLived = 1; // ticksLived
  345.  
  346. ((CraftWorld)location.getWorld()).getHandle().addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
  347.  
  348. // net.minecraft.server.v1_8_R3.Entity nmsEntity = (entity.getBukkitEntity()).getHandle();
  349. // NBTTagCompound tag = nmsEntity.getNBTTag();
  350. // if(tag == null) {
  351. // tag = new NBTTagCompound();
  352. // }
  353. // nmsEntity.c(tag);
  354. // tag.setInt("NoAI", 1);
  355. // nmsEntity.f(tag);
  356.  
  357. return entity;
  358. // setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
  359. //
  360. // ((CraftWorld)location.getWorld()).getHandle().addEntity(this, CreatureSpawnEvent.SpawnReason.CUSTOM);
  361. // return this;
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement