Guest User

Code

a guest
Mar 24th, 2017
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.51 KB | None | 0 0
  1. package com.steelfeathers.nethersurvival.worldgen;
  2.  
  3. import java.util.Random;
  4. import com.google.common.base.Predicate;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.properties.IProperty;
  7. import net.minecraft.block.state.IBlockState;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.util.EnumFacing;
  10. import net.minecraft.util.math.BlockPos;
  11. import net.minecraft.world.World;
  12. import net.minecraft.world.gen.feature.WorldGenerator;
  13.  
  14. public class WorldGenCrystal extends WorldGenBase {
  15.    
  16.     private static boolean noState = false;
  17.     private static IBlockState state;
  18.     private static IBlockState state_up;
  19.     private static IBlockState state_down;
  20.     private static IBlockState state_north;
  21.     private static IBlockState state_south;
  22.     private static IBlockState state_east;
  23.     private static IBlockState state_west;
  24.     private static IProperty<EnumFacing> property;
  25.     private static int clusterSize = 3;
  26.     private static int clusterSpread = 5;
  27.     private static Block targetBlock;
  28.  
  29.    
  30.     public WorldGenCrystal(IBlockState state, Block targetBlock) {
  31.         this.state = this.state_up = this.state_down = this.state_north = this.state_south = this.state_east = this.state_west = state;
  32.         noState = true;
  33.         this.targetBlock = targetBlock;
  34.     }
  35.    
  36.     public WorldGenCrystal(IBlockState state, Block targetBlock, int clusterSize, int clusterSpread) {
  37.         this.state = this.state_up = this.state_down = this.state_north = this.state_south = this.state_east = this.state_west = state;
  38.         this.clusterSize = clusterSize;
  39.         this.clusterSpread = clusterSpread;
  40.         noState = true;
  41.         this.targetBlock = targetBlock;
  42.     }
  43.    
  44.     public WorldGenCrystal(IBlockState state, Block targetBlock, int clusterSize, int clusterSpread, IProperty<EnumFacing> property) {
  45.         this.state = state;
  46.         this.state_up = state.withProperty(property, EnumFacing.UP);
  47.         this.state_down = state.withProperty(property, EnumFacing.DOWN);
  48.         this.state_north = state.withProperty(property, EnumFacing.NORTH);
  49.         this.state_south = state.withProperty(property, EnumFacing.SOUTH);
  50.         this.state_east = state.withProperty(property, EnumFacing.EAST);
  51.         this.state_west = state.withProperty(property, EnumFacing.WEST);
  52.         this.property = property;
  53.         this.clusterSize = clusterSize;
  54.         this.clusterSpread = clusterSpread;
  55.         noState = false;
  56.         this.targetBlock = targetBlock;
  57.     }
  58.    
  59.  
  60.    
  61.     @Override
  62.     public boolean generate(World worldIn, Random rand, BlockPos position) {
  63.         int D = 1;  //use a directional hash to determine the combination of blocks surrounding the target position
  64.         int U = 2;
  65.         int N = 4;
  66.         int S = 8;
  67.         int W = 16;
  68.         int E = 32;
  69.         int hash = 0;
  70.         int tot = D+U+N+S+W+E;
  71.        
  72. // THIS IS THE WHERE THE ISSUE STARTS. If I comment out just the loop, so that the code only runs once, everything is fine.
  73.         for (int i = 0; i < clusterSize; ++i)
  74.         {                      
  75.             if (worldIn.isAirBlock(position))  
  76.             {
  77.                 int r = rand.nextInt(6);
  78.                
  79.                 boolean sideDown = false;
  80.                 boolean sideUp = false;
  81.                 boolean sideNorth = false;
  82.                 boolean sideEast = false;
  83.                 boolean sideWest = false;
  84.                 boolean sideSouth = false;
  85.                 IBlockState blockstate = worldIn.getBlockState(position);
  86.                
  87.                 if (worldIn.getBlockState(position.up()).getBlock() == targetBlock)
  88.                 {
  89.                     hash += U;
  90.                     sideUp = true;
  91.                 }
  92.                
  93.                 if (worldIn.getBlockState(position.down()).getBlock() == targetBlock)
  94.                 {
  95.                     hash += D;
  96.                     sideDown = true;
  97.                 }
  98.                
  99.                 if (worldIn.getBlockState(position.north()).getBlock() == targetBlock)
  100.                 {
  101.                     hash += N;
  102.                     sideNorth = true;
  103.                 }
  104.                
  105.                 if (worldIn.getBlockState(position.south()).getBlock() == targetBlock)
  106.                 {
  107.                     hash += S;
  108.                     sideSouth = true;
  109.                 }
  110.                
  111.                 if (worldIn.getBlockState(position.east()).getBlock() == targetBlock)
  112.                 {
  113.                     hash += E;
  114.                     sideEast = true;
  115.                 }
  116.                
  117.                 if (worldIn.getBlockState(position.west()).getBlock() == targetBlock)
  118.                 {
  119.                     hash += W;
  120.                     sideWest = true;
  121.                 }
  122.                
  123.                
  124.                 if (hash != tot)
  125.                 {
  126.                     if (hash == tot-D || hash == U) //all sides but down are target block, or only up side is target block
  127.                     {
  128.                         worldIn.setBlockState(position, state_down, 2);
  129.                     }
  130.                     else if (hash == tot-U || hash == D)
  131.                     {
  132.                         worldIn.setBlockState(position, state_up, 2);
  133.                     }
  134.                     else if (hash == tot-N || hash == S)
  135.                     {
  136.                         worldIn.setBlockState(position, state_north, 2);
  137.                     }
  138.                     else if (hash == tot-S || hash == N)
  139.                     {
  140.                         worldIn.setBlockState(position, state_south, 2);
  141.                     }
  142.                     else if (hash == tot-W || hash == E)
  143.                     {
  144.                         worldIn.setBlockState(position, state_west, 2);
  145.                     }
  146.                     else if (hash == tot-E || hash == W)
  147.                     {
  148.                         worldIn.setBlockState(position, state_east, 2);
  149.                     }
  150.                     else if (r == 0 && sideUp || r == 1 && sideDown || r == 2 && sideSouth ||
  151.                                     r == 3 && sideNorth || r == 4 && sideEast || r == 5 && sideWest)
  152.                     {
  153.                         if (noState)
  154.                         {
  155.                             worldIn.setBlockState(position, state, 2);
  156.                         }
  157.                         else
  158.                         {
  159.                             worldIn.setBlockState(position, state.withProperty(property, EnumFacing.getFront(r)), 2);
  160.                         }  
  161.                     }
  162.                 }
  163.                
  164.             }
  165.            
  166.             position = position.add(rand.nextInt(clusterSpread) - rand.nextInt(clusterSpread), rand.nextInt(clusterSpread) - rand.nextInt(clusterSpread), rand.nextInt(clusterSpread) - rand.nextInt(clusterSpread));
  167.            
  168.         }
  169.            
  170.  
  171.  
  172.  
  173.         return true;
  174.        
  175.        
  176.    
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment