Advertisement
Guest User

Untitled

a guest
Jun 15th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. package net.minecraftforge.fluids;
  2.  
  3. import com.gamerforea.eventhelper.util.EventUtils;
  4. import java.util.Random;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.world.IBlockAccess;
  10. import net.minecraft.world.World;
  11. import net.minecraftforge.common.util.ForgeDirection;
  12.  
  13. /**
  14. * This is a fluid block implementation which emulates vanilla Minecraft fluid
  15. * behavior.
  16. *
  17. * It is highly recommended that you use/extend this class for "classic" fluid
  18. * blocks.
  19. *
  20. * @author King Lemming
  21. *
  22. */
  23. public class BlockFluidClassic extends BlockFluidBase {
  24.  
  25. protected boolean[] isOptimalFlowDirection = new boolean[4];
  26. protected int[] flowCost = new int[4];
  27.  
  28. protected FluidStack stack;
  29.  
  30. public BlockFluidClassic(Fluid fluid, Material material) {
  31. super(fluid, material);
  32. stack = new FluidStack(fluid, FluidContainerRegistry.BUCKET_VOLUME);
  33. }
  34.  
  35. public BlockFluidClassic setFluidStack(FluidStack stack) {
  36. this.stack = stack;
  37. return this;
  38. }
  39.  
  40. public BlockFluidClassic setFluidStackAmount(int amount) {
  41. this.stack.amount = amount;
  42. return this;
  43. }
  44.  
  45. @Override
  46. public int getQuantaValue(IBlockAccess world, int x, int y, int z) {
  47. if (world.getBlock(x, y, z) == Blocks.air) {
  48. return 0;
  49. }
  50.  
  51. if (world.getBlock(x, y, z) != this) {
  52. return -1;
  53. }
  54.  
  55. int quantaRemaining = quantaPerBlock - world.getBlockMetadata(x, y, z);
  56. return quantaRemaining;
  57. }
  58.  
  59. /**
  60. * Returns whether this block is collideable based on the arguments passed
  61. * in
  62. *
  63. * @param par1 block metaData
  64. * @param par2 whether the player right-clicked while holding a boat
  65. */
  66. @Override
  67. public boolean canCollideCheck(int meta, boolean fullHit) {
  68. return fullHit && meta == 0;
  69. }
  70.  
  71. @Override
  72. public int getMaxRenderHeightMeta() {
  73. return 0;
  74. }
  75.  
  76. @Override
  77. public int getLightValue(IBlockAccess world, int x, int y, int z) {
  78. if (maxScaledLight == 0) {
  79. return super.getLightValue(world, x, y, z);
  80. }
  81. int data = quantaPerBlock - world.getBlockMetadata(x, y, z) - 1;
  82. return (int) (data / quantaPerBlockFloat * maxScaledLight);
  83. }
  84.  
  85. /**
  86. * Ticks the block if it's been scheduled
  87. */
  88. @Override
  89. public void updateTick(World world, int x, int y, int z, Random rand) {
  90. int quantaRemaining = quantaPerBlock - world.getBlockMetadata(x, y, z);
  91. int expQuanta = -101;
  92.  
  93. // check adjacent block levels if non-source
  94. if (quantaRemaining < quantaPerBlock) {
  95. int y2 = y - densityDir;
  96.  
  97. if (world.getBlock(x, y2, z) == this
  98. || world.getBlock(x - 1, y2, z) == this
  99. || world.getBlock(x + 1, y2, z) == this
  100. || world.getBlock(x, y2, z - 1) == this
  101. || world.getBlock(x, y2, z + 1) == this) {
  102. expQuanta = quantaPerBlock - 1;
  103. } else {
  104. int maxQuanta = -100;
  105. maxQuanta = getLargerQuanta(world, x - 1, y, z, maxQuanta);
  106. maxQuanta = getLargerQuanta(world, x + 1, y, z, maxQuanta);
  107. maxQuanta = getLargerQuanta(world, x, y, z - 1, maxQuanta);
  108. maxQuanta = getLargerQuanta(world, x, y, z + 1, maxQuanta);
  109.  
  110. expQuanta = maxQuanta - 1;
  111. }
  112.  
  113. // decay calculation
  114. if (expQuanta != quantaRemaining) {
  115. quantaRemaining = expQuanta;
  116.  
  117. if (expQuanta <= 0) {
  118. world.setBlock(x, y, z, Blocks.air);
  119. } else {
  120. world.setBlockMetadataWithNotify(x, y, z, quantaPerBlock - expQuanta, 3);
  121. world.scheduleBlockUpdate(x, y, z, this, tickRate);
  122. world.notifyBlocksOfNeighborChange(x, y, z, this);
  123. }
  124. }
  125. } // This is a "source" block, set meta to zero, and send a server only update
  126. else if (quantaRemaining >= quantaPerBlock) {
  127. world.setBlockMetadataWithNotify(x, y, z, 0, 2);
  128. }
  129.  
  130. // Flow vertically if possible
  131. if (canDisplace(world, x, y + densityDir, z)) {
  132. /** Code start **/
  133. ForgeDirection dir = densityDir < 0 ? ForgeDirection.DOWN : ForgeDirection.UP;
  134. if (EventUtils.cantFromTo(world, x, y, z, dir)) {
  135. return;
  136. }
  137. /** Code end **/
  138. flowIntoBlock(world, x, y + densityDir, z, 1);
  139. return;
  140. }
  141.  
  142. // Flow outward if possible
  143. int flowMeta = quantaPerBlock - quantaRemaining + 1;
  144. if (flowMeta >= quantaPerBlock) {
  145. return;
  146. }
  147.  
  148. if (isSourceBlock(world, x, y, z) || !isFlowingVertically(world, x, y, z)) {
  149. if (world.getBlock(x, y - densityDir, z) == this) {
  150. flowMeta = 1;
  151. }
  152. boolean flowTo[] = getOptimalFlowDirections(world, x, y, z);
  153.  
  154. /** Code start **/
  155. ForgeDirection[] dirs = new ForgeDirection[]{ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.EAST, ForgeDirection.WEST};
  156.  
  157. for (int i = 0; i < 4; i++) {
  158. if (flowTo[i]) {
  159. if(!EventUtils.cantFromTo(world, x, y, z, dirs[i])) {
  160. continue;
  161. }
  162. switch(i) {
  163. case 0:
  164. flowIntoBlock(world, x - 1, y, z, flowMeta);
  165. break;
  166. case 1:
  167. flowIntoBlock(world, x + 1, y, z, flowMeta);
  168. break;
  169. case 2:
  170. flowIntoBlock(world, x, y, z - 1, flowMeta);
  171. break;
  172. case 3:
  173. flowIntoBlock(world, x, y, z + 1, flowMeta);
  174. break;
  175. }
  176. }
  177. }
  178.  
  179. /** Code end **/
  180.  
  181. /** if (flowTo[0]) {
  182. flowIntoBlock(world, x - 1, y, z, flowMeta);
  183. }
  184. if (flowTo[1]) {
  185. flowIntoBlock(world, x + 1, y, z, flowMeta);
  186. }
  187. if (flowTo[2]) {
  188. flowIntoBlock(world, x, y, z - 1, flowMeta);
  189. }
  190. if (flowTo[3]) {
  191. flowIntoBlock(world, x, y, z + 1, flowMeta);
  192. } **/
  193. }
  194. }
  195.  
  196. public boolean isFlowingVertically(IBlockAccess world, int x, int y, int z) {
  197. return world.getBlock(x, y + densityDir, z) == this
  198. || (world.getBlock(x, y, z) == this && canFlowInto(world, x, y + densityDir, z));
  199. }
  200.  
  201. public boolean isSourceBlock(IBlockAccess world, int x, int y, int z) {
  202. return world.getBlock(x, y, z) == this && world.getBlockMetadata(x, y, z) == 0;
  203. }
  204.  
  205. protected boolean[] getOptimalFlowDirections(World world, int x, int y, int z) {
  206. for (int side = 0; side < 4; side++) {
  207. flowCost[side] = 1000;
  208.  
  209. int x2 = x;
  210. int y2 = y;
  211. int z2 = z;
  212.  
  213. switch (side) {
  214. case 0:
  215. --x2;
  216. break;
  217. case 1:
  218. ++x2;
  219. break;
  220. case 2:
  221. --z2;
  222. break;
  223. case 3:
  224. ++z2;
  225. break;
  226. }
  227.  
  228. if (!canFlowInto(world, x2, y2, z2) || isSourceBlock(world, x2, y2, z2)) {
  229. continue;
  230. }
  231.  
  232. if (canFlowInto(world, x2, y2 + densityDir, z2)) {
  233. flowCost[side] = 0;
  234. } else {
  235. flowCost[side] = calculateFlowCost(world, x2, y2, z2, 1, side);
  236. }
  237. }
  238.  
  239. int min = flowCost[0];
  240. for (int side = 1; side < 4; side++) {
  241. if (flowCost[side] < min) {
  242. min = flowCost[side];
  243. }
  244. }
  245. for (int side = 0; side < 4; side++) {
  246. isOptimalFlowDirection[side] = flowCost[side] == min;
  247. }
  248. return isOptimalFlowDirection;
  249. }
  250.  
  251. protected int calculateFlowCost(World world, int x, int y, int z, int recurseDepth, int side) {
  252. int cost = 1000;
  253. for (int adjSide = 0; adjSide < 4; adjSide++) {
  254. if ((adjSide == 0 && side == 1)
  255. || (adjSide == 1 && side == 0)
  256. || (adjSide == 2 && side == 3)
  257. || (adjSide == 3 && side == 2)) {
  258. continue;
  259. }
  260.  
  261. int x2 = x;
  262. int y2 = y;
  263. int z2 = z;
  264.  
  265. switch (adjSide) {
  266. case 0:
  267. --x2;
  268. break;
  269. case 1:
  270. ++x2;
  271. break;
  272. case 2:
  273. --z2;
  274. break;
  275. case 3:
  276. ++z2;
  277. break;
  278. }
  279.  
  280. if (!canFlowInto(world, x2, y2, z2) || isSourceBlock(world, x2, y2, z2)) {
  281. continue;
  282. }
  283.  
  284. if (canFlowInto(world, x2, y2 + densityDir, z2)) {
  285. return recurseDepth;
  286. }
  287.  
  288. if (recurseDepth >= 4) {
  289. continue;
  290. }
  291.  
  292. int min = calculateFlowCost(world, x2, y2, z2, recurseDepth + 1, adjSide);
  293. if (min < cost) {
  294. cost = min;
  295. }
  296. }
  297. return cost;
  298. }
  299.  
  300. protected void flowIntoBlock(World world, int x, int y, int z, int meta) {
  301. if (meta < 0) {
  302. return;
  303. }
  304. if (displaceIfPossible(world, x, y, z)) {
  305. world.setBlock(x, y, z, this, meta, 3);
  306. }
  307. }
  308.  
  309. protected boolean canFlowInto(IBlockAccess world, int x, int y, int z) {
  310. if (world.getBlock(x, y, z).isAir(world, x, y, z)) {
  311. return true;
  312. }
  313.  
  314. Block block = world.getBlock(x, y, z);
  315. if (block == this) {
  316. return true;
  317. }
  318.  
  319. if (displacements.containsKey(block)) {
  320. return displacements.get(block);
  321. }
  322.  
  323. Material material = block.getMaterial();
  324. if (material.blocksMovement()
  325. || material == Material.water
  326. || material == Material.lava
  327. || material == Material.portal) {
  328. return false;
  329. }
  330.  
  331. int density = getDensity(world, x, y, z);
  332. if (density == Integer.MAX_VALUE) {
  333. return true;
  334. }
  335.  
  336. if (this.density > density) {
  337. return true;
  338. } else {
  339. return false;
  340. }
  341. }
  342.  
  343. protected int getLargerQuanta(IBlockAccess world, int x, int y, int z, int compare) {
  344. int quantaRemaining = getQuantaValue(world, x, y, z);
  345. if (quantaRemaining <= 0) {
  346. return compare;
  347. }
  348. return quantaRemaining >= compare ? quantaRemaining : compare;
  349. }
  350.  
  351. /* IFluidBlock */
  352. @Override
  353. public FluidStack drain(World world, int x, int y, int z, boolean doDrain) {
  354. if (!isSourceBlock(world, x, y, z)) {
  355. return null;
  356. }
  357.  
  358. if (doDrain) {
  359. world.setBlock(x, y, z, Blocks.air);
  360. }
  361.  
  362. return stack.copy();
  363. }
  364.  
  365. @Override
  366. public boolean canDrain(World world, int x, int y, int z) {
  367. return isSourceBlock(world, x, y, z);
  368. }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement