Guest User

Untitled

a guest
Aug 16th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. package net.madcrazydrumma.skyrimcraft.world;
  2.  
  3. import net.madcrazydrumma.skyrimcraft.block.BlockHandler;
  4. import net.madcrazydrumma.skyrimcraft.util.ModInfo;
  5. import net.minecraft.block.*;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.block.state.IBlockState;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.tileentity.TileEntityChest;
  10. import net.minecraft.util.ResourceLocation;
  11. import net.minecraft.util.Rotation;
  12. import net.minecraft.util.math.BlockPos;
  13. import net.minecraft.world.World;
  14. import net.minecraft.world.gen.feature.WorldGenerator;
  15. import net.minecraftforge.fml.common.registry.GameRegistry;
  16.  
  17. import java.util.Random;
  18.  
  19. public class AnvilHutGenerator extends WorldGenerator
  20. {
  21. public static final ResourceLocation chestLoot = new ResourceLocation(ModInfo.MODID, "loot_tables/anvilHutLoot.json");
  22.  
  23. private final int[][] andesitePos = new int[][]{
  24. //Floor
  25. {0, -1, 0}, {1, -1, 0}, {2, -1, 0}, {3, -1, 0}, {4, -1, 0}, {5, -1, 0}, {6, -1, 0}, {7, -1, 0}, {8, -1, 0},
  26. {0, -1, 1}, {1, -1, 1}, {2, -1, 1}, {3, -1, 1}, {4, -1, 1}, {5, -1, 1}, {6, -1, 1}, {7, -1, 1}, {8, -1, 1},
  27. {0, -1, 2}, {1, -1, 2}, {2, -1, 2}, {3, -1, 2}, {4, -1, 2}, {5, -1, 2}, {6, -1, 2}, {7, -1, 2}, {8, -1, 2},
  28. {0, -1, 3}, {1, -1, 3}, {2, -1, 3}, {3, -1, 3}, {4, -1, 3}, {5, -1, 3}, {6, -1, 3}, {7, -1, 3}, {8, -1, 3},
  29. {0, -1, 4}, {1, -1, 4}, {2, -1, 4}, {3, -1, 4}, {4, -1, 4}, {5, -1, 4}, {6, -1, 4}, {7, -1, 4}, {8, -1, 4},
  30. {0, -1, 5}, {1, -1, 5}, {2, -1, 5}, {3, -1, 5}, {4, -1, 5}, {5, -1, 5}, {6, -1, 5}, {7, -1, 5}, {8, -1, 5},
  31. {0, -1, 6}, {1, -1, 6}, {2, -1, 6}, {3, -1, 6}, {4, -1, 6}, {5, -1, 6}, {6, -1, 6}, {7, -1, 6}, {8, -1, 6},
  32. {0, -1, 7}, {1, -1, 7}, {2, -1, 7}, {3, -1, 7}, {4, -1, 7}, {5, -1, 7}, {6, -1, 7}, {7, -1, 7}, {8, -1, 7},
  33. {0, -1, 8}, {1, -1, 8}, {2, -1, 8}, {3, -1, 8}, {4, -1, 8}, {5, -1, 8}, {6, -1, 8}, {7, -1, 8}, {8, -1, 8},
  34. {0, -1, 9}, {1, -1, 9}, {2, -1, 9}, {3, -1, 9}, {4, -1, 9}, {5, -1, 9}, {6, -1, 9}, {7, -1, 9}, {8, -1, 9},
  35. {0, -1, 10}, {1, -1, 10}, {2, -1, 10}, {3, -1, 10}, {4, -1, 10}, {5, -1, 10}, {6, -1, 10}, {7, -1, 10}, {8, -1, 10},
  36. {0, -1, 11}, {1, -1, 11}, {2, -1, 11}, {3, -1, 11}, {4, -1, 11}, {5, -1, 11}, {6, -1, 11}, {7, -1, 11}, {8, -1, 11},
  37. {0, -1, 12}, {1, -1, 12}, {2, -1, 12}, {3, -1, 12}, {4, -1, 12}, {5, -1, 12}, {6, -1, 12}, {7, -1, 12}, {8, -1, 12}
  38. };
  39.  
  40. private final int[][] oakLogsPos = new int[][] {
  41. //Corners
  42. {1, 0, 1}, {7, 0, 1}, {1, 0, 11}, {7, 0, 11},
  43. {1, 1, 1}, {7, 1, 1}, {1, 1, 11}, {7, 1, 11},
  44. {1, 2, 1}, {7, 2, 1}, {1, 2, 11}, {7, 2, 11},
  45. //Middle
  46. {1, 0, 6}, {7, 0, 6},
  47. {1, 1, 6}, {7, 1, 6},
  48. {1, 2, 6}, {7, 2, 6}
  49. };
  50.  
  51. private final int[][] planksPos = new int[][] {
  52. {1, 3, 1}, {2, 3, 1}, {3, 3, 1}, {4, 3, 1}, {5, 3, 1}, {6, 3, 1}, {7, 3, 1},
  53. {1, 3, 2}, {2, 3, 2}, {3, 3, 2}, {4, 3, 2}, {5, 3, 2}, {6, 3, 2}, {7, 3, 2},
  54. {1, 3, 3}, {2, 3, 3}, {3, 3, 3}, {4, 3, 3}, {5, 3, 3}, {6, 3, 3}, {7, 3, 3},
  55. {1, 3, 4}, {2, 3, 4}, {3, 3, 4}, {4, 3, 4}, {5, 3, 4}, {6, 3, 4}, {7, 3, 4},
  56. {1, 3, 5}, {2, 3, 5}, {3, 3, 5}, {4, 3, 5}, {5, 3, 5}, {6, 3, 5}, {7, 3, 5},
  57. {1, 3, 6}, {2, 3, 6}, {3, 3, 6}, {4, 3, 6}, {5, 3, 6}, {6, 3, 6}, {7, 3, 6},
  58. {1, 3, 7}, {2, 3, 7}, {3, 3, 7}, {4, 3, 7}, {5, 3, 7}, {6, 3, 7}, {7, 3, 7},
  59. {1, 3, 8}, {2, 3, 8}, {3, 3, 8}, {4, 3, 8}, {5, 3, 8}, {6, 3, 8}, {7, 3, 8},
  60. {1, 3, 9}, {2, 3, 9}, {3, 3, 9}, {4, 3, 9}, {5, 3, 9}, {6, 3, 9}, {7, 3, 9},
  61. {1, 3, 10}, {2, 3, 10}, {3, 3, 10}, {4, 3, 10}, {5, 3, 10}, {6, 3, 10}, {7, 3, 10},
  62. {1, 3, 11}, {2, 3, 11}, {3, 3, 11}, {4, 3, 11}, {5, 3, 11}, {6, 3, 11}, {7, 3, 11}
  63. };
  64.  
  65. private final int[][] fencePos = new int[][] {
  66. /* ENTRANCES */
  67. {2, 0, 1}, {2, 0, 11},
  68. {3, 0, 1}, {3, 0, 11},
  69. // 4 is entrance?
  70. {5, 0, 1}, {5, 0, 11},
  71. {6, 0, 1}, {6, 0, 11},
  72. /* END ENTRANCES */
  73.  
  74. // EDGES
  75. {1, 0, 2}, {1, 0, 3}, {1, 0, 4}, {1, 0, 5}, /*6 is middle so dont place fence*/ {1, 0, 7}, {1, 0, 8}, {1, 0, 9}, {1, 0, 10},
  76. {7, 0, 2}, {7, 0, 3}, {7, 0, 4}, {7, 0, 5}, /*6 is middle so dont place fence*/ {7, 0, 7}, {7, 0, 8}, {7, 0, 9}, {7, 0, 10},
  77.  
  78. //Random
  79. {2, 0, 5}, {3, 0, 5}, {5, 0, 7}, {6, 0, 7}
  80. };
  81. private final int[][] fenceGatePos = new int[][] {
  82. {4, 0, 1}, {4, 0, 11}
  83. };
  84.  
  85. private final int[] chestPos = new int[] {2, 0, 4};
  86.  
  87. private final int[][] anvilPos = new int[][] {
  88. {5, 0, 6}, {6, 0, 6}
  89. };
  90.  
  91. private final int[][] torchPos = new int[][] {
  92.  
  93. };
  94.  
  95. public AnvilHutGenerator() {
  96. }
  97.  
  98. @Override
  99. public boolean generate(World worldIn, Random rand, BlockPos corner) {
  100. if(canSpawnHere(worldIn, corner)) {
  101. IBlockState andesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE);
  102. IBlockState oakLogs = Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.OAK);
  103. IBlockState planks = Blocks.PLANKS.getDefaultState().withProperty(BlockPlanks.VARIANT, BlockPlanks.EnumType.OAK);
  104. IBlockState fence = Blocks.OAK_FENCE.getDefaultState();
  105. IBlockState fenceGate = Blocks.OAK_FENCE_GATE.getDefaultState();
  106.  
  107. IBlockState chest = Blocks.CHEST.getDefaultState().withRotation(Rotation.CLOCKWISE_90);
  108. IBlockState anvil = BlockHandler.anvil.getDefaultState().withRotation(Rotation.CLOCKWISE_90);
  109.  
  110. //Build layers from arrays
  111. buildLayer(worldIn, corner, andesitePos, andesite);
  112. buildLayer(worldIn, corner, oakLogsPos, oakLogs);
  113. buildLayer(worldIn, corner, fencePos, fence);
  114. buildLayer(worldIn, corner, fenceGatePos, fenceGate);
  115. buildLayer(worldIn, corner, anvilPos, anvil);
  116. buildLayer(worldIn, corner, planksPos, planks);
  117.  
  118. //Place other features LAST (chests, torches, etc.)
  119.  
  120. placeBlock(worldIn, corner, chestPos, chest);
  121. BlockPos actualPos = corner.add(chestPos[0], chestPos[1], chestPos[2]);
  122. TileEntityChest chestTE = (TileEntityChest)worldIn.getTileEntity(actualPos);
  123. if(chestTE != null)
  124. chestTE.setLootTable(chestLoot, actualPos.toLong());
  125.  
  126. return true;
  127. }
  128.  
  129. return false;
  130. }
  131.  
  132. // use an int[][] to place a lot of one block at once
  133. private void buildLayer(World world, BlockPos frontLeftCorner, int[][] blockPositions, IBlockState toPlace) {
  134. // iterate through the entire int[][]
  135. for(int[] coord : blockPositions) {
  136. placeBlock(world, frontLeftCorner, coord[0], coord[1], coord[2], toPlace);
  137. }
  138. }
  139.  
  140. /** Helper Method **/
  141. private void placeBlock(World world, BlockPos frontLeftCorner, int[] offsets, IBlockState toPlace) {
  142. placeBlock(world, frontLeftCorner, offsets[0], offsets[1], offsets[2], toPlace);
  143. }
  144.  
  145. /** Places a block using corner position and offsets **/
  146. private void placeBlock(World world, BlockPos frontLeftCorner, int offsetX, int offsetY, int offsetZ, IBlockState toPlace) {
  147. // figure out where that block is relative to the corner
  148. BlockPos placePos = frontLeftCorner.add(offsetX, offsetY, offsetZ);
  149. world.setBlockState(placePos, toPlace, 2);
  150. }
  151.  
  152. private boolean canSpawnHere(World world, BlockPos posAboveGround) {
  153. // check all the corners to see which ones are replaceable
  154. boolean corner1Air = canReplace(world, posAboveGround);
  155. boolean corner2Air = canReplace(world, posAboveGround.add(12, 0, 0)); //Blocks away from origin
  156. boolean corner4Air = canReplace(world, posAboveGround.add(0, 0, 8));
  157. boolean corner3Air = canReplace(world, posAboveGround.add(12, 0, 8));
  158.  
  159. // if Y > 20 and all corners pass the test, it's okay to spawn the structure
  160. return posAboveGround.getY() > 20 && corner1Air && corner2Air && corner3Air && corner4Air;
  161. }
  162.  
  163. private boolean canReplace(World world, BlockPos pos) {
  164. Block at = world.getBlockState(pos).getBlock();
  165. Material material = at.getDefaultState().getMaterial();
  166. // we think it's replaceable if it's air / liquid / snow, plants, or leaves
  167. return material.isReplaceable() || material == Material.PLANTS || material == Material.LEAVES;
  168. }
  169. }
Add Comment
Please, Sign In to add comment