Advertisement
Nuparu00

7 Days to Mine [A4] - Structure.class

Jul 10th, 2017
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.28 KB | None | 0 0
  1. public class Structure implements Runnable{
  2.  
  3. public ArrayList<BufferedBlock> default_state_blocks = new ArrayList<BufferedBlock>();
  4. public EnumFacing default_facing = EnumFacing.SOUTH;
  5.  
  6. public float neededRoom = 0.75f;
  7. public float neededFlatness = 0.75f;
  8.  
  9. public HashMap<EnumFacing,ArrayList<BufferedBlock>> rotations = new HashMap<EnumFacing,ArrayList<BufferedBlock>>();
  10.  
  11. String name;
  12. public Structure(String name){
  13. this.name = name;  
  14. }
  15. public void run(){
  16. try{
  17.     load();
  18. }
  19. catch(IOException e){
  20.    
  21.      e.printStackTrace();  
  22. }
  23. }
  24. public void load() throws IOException{
  25. InputStream fis = getClass().getResourceAsStream(new StringBuilder().append("/assets/minecraft/structures/").append(name).toString());
  26. InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
  27. BufferedReader br = new BufferedReader(isr);
  28. String line;
  29.  while ((line = br.readLine()) != null) {
  30.                 String[] words = line.split(" ");
  31.                 int i = Integer.parseInt(words[0]);
  32.                 int j = Integer.parseInt(words[1]);
  33.                 int k = Integer.parseInt(words[2]);
  34.  
  35.                 Block block = Block.getBlockFromName(words[3]);
  36.                 int meta = Integer.parseInt(words[4]);
  37.                 default_state_blocks.add(new BufferedBlock(i,j,k,block,meta));
  38.  
  39.            
  40.  
  41.                
  42.             }
  43. loadRotations();
  44.  
  45. }
  46.  
  47. public void loadRotations(){
  48. for(int i = 0;i<EnumFacing.HORIZONTALS.length;i++){
  49. EnumFacing facing = EnumFacing.HORIZONTALS[i];
  50. rotations.put(facing,rotate(getAngle(facing)));
  51. }
  52. }
  53.  
  54. public ArrayList<BufferedBlock> rotate(float rot){
  55. ArrayList<BufferedBlock> blocks = new ArrayList<BufferedBlock>();
  56.  
  57. for(int block = 0;block < default_state_blocks.size();block++){
  58. BufferedBlock bufferedBlock = default_state_blocks.get(block).copy();
  59. IBlockState state = bufferedBlock.block.getStateFromMeta(bufferedBlock.meta);
  60. IProperty propertyDirection = hasProperty(state,PropertyDirection.class);
  61. if(propertyDirection != null){
  62. EnumFacing facing = (EnumFacing)(state.getValue(propertyDirection));
  63. if(facing != EnumFacing.UP && facing != EnumFacing.DOWN){
  64. for(int i = 0;i < (int)Math.abs(rot/90);i++){
  65. facing = facing.rotateY();
  66. }
  67. if((int)Math.abs(rot/90) % 2 != 0){
  68. facing = facing.getOpposite();
  69. }
  70. }
  71. bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(propertyDirection,facing));
  72. }
  73.  
  74. IProperty propertyAxis = hasProperty(state,PropertyEnum.class);
  75. if(propertyAxis != null && propertyAxis.getValueClass() == EnumFacing.Axis.class){
  76. PropertyEnum<EnumFacing.Axis> AXIS = (PropertyEnum<EnumFacing.Axis>)(propertyAxis);
  77. EnumFacing.Axis axis = state.getValue(AXIS);
  78.  
  79. for(int i = 0;i < (int)Math.abs(rot/90);i++){
  80. if(axis == EnumFacing.Axis.X){
  81.     axis = EnumFacing.Axis.Z;
  82. }
  83. else if(axis == EnumFacing.Axis.Z){
  84.     axis = EnumFacing.Axis.X;
  85. }
  86. }
  87. bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(AXIS,axis));
  88. }
  89.  
  90. if(propertyAxis != null && propertyAxis.getValueClass() == BlockLog.EnumAxis.class){
  91. PropertyEnum<BlockLog.EnumAxis> AXIS = (PropertyEnum<BlockLog.EnumAxis>)(propertyAxis);
  92. BlockLog.EnumAxis axis = state.getValue(AXIS);
  93.  
  94. for(int i = 0;i < (int)Math.abs(rot/90);i++){
  95. if(axis == BlockLog.EnumAxis.X){
  96.     axis = BlockLog.EnumAxis.Z;
  97. }
  98. else if(axis == BlockLog.EnumAxis.Z){
  99.     axis = BlockLog.EnumAxis.X;
  100. }
  101. }
  102.  
  103. bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(AXIS,axis));
  104. }
  105.  
  106.  
  107. blocks.add(bufferedBlock.rotate(rot));
  108. }
  109. return blocks;
  110. }
  111. public <T> IProperty hasProperty(IBlockState state, Class<T> type) {
  112.     for(IProperty property : state.getPropertyNames()){
  113.         //System.out.println("Expected class " + type + "Founded class" + property.getClass());
  114.         if(property.getClass() == type){
  115.             return property;
  116.         }
  117.     }
  118.     return null;
  119. }
  120.  
  121.  
  122. public float getAngle(EnumFacing facing){
  123. switch(facing){
  124. case SOUTH : { return 0;}
  125. case WEST : {return 90;}
  126. case NORTH : {return 180;}
  127. case EAST : {return 270;}
  128. }
  129. return 0;
  130. }
  131. public float testForRoom(World world,BlockPos pos,EnumFacing facing){
  132. ArrayList<BufferedBlock> blocks = getArrayByFacing(facing);
  133. int replacable = 0;
  134. for(int block = 0;block < blocks.size();block++){
  135. BufferedBlock bufferedBlock = blocks.get(block);
  136. if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isReplaceable()){
  137.     replacable++;
  138. }
  139.  
  140. }
  141.  
  142.  
  143. return (float)replacable/blocks.size();
  144. }
  145. public float testForRoom(World world,BlockPos pos,ArrayList<BufferedBlock> blocks){
  146. int replacable = 0;
  147. for(int block = 0;block < blocks.size();block++){
  148. BufferedBlock bufferedBlock = blocks.get(block);
  149. if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isReplaceable()){
  150.     replacable++;
  151. }
  152.  
  153. }
  154.  
  155.  
  156. return (float)replacable/blocks.size();
  157. }
  158.  
  159. public float testForFlatness(World world,BlockPos pos,ArrayList<BufferedBlock> blocks){
  160. float flat = 0;
  161. int pedestalSize = 0;
  162. for(int block = 0;block < blocks.size();block++){
  163. BufferedBlock bufferedBlock = blocks.get(block);
  164. if(bufferedBlock.y == 0){
  165. pedestalSize++;
  166. if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isSolid()){
  167. flat++;
  168. }else{
  169. //TO-DO
  170. }
  171.  
  172. }
  173. }
  174. return (float)flat/pedestalSize;
  175. }
  176.  
  177. public void generate(World world,BlockPos pos,EnumFacing facing){
  178.  
  179. ArrayList<BufferedBlock> blocks = getArrayByFacing(facing);
  180. if(testForRoom(world,pos,blocks) >= neededRoom){
  181.     if(testForFlatness(world,pos,blocks) >= neededFlatness){
  182. for(int block = 0;block < blocks.size();block++){
  183. BufferedBlock bufferedBlock = blocks.get(block);
  184. world.setBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z),bufferedBlock.block.getStateFromMeta(bufferedBlock.meta));
  185. if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) != null && bufferedBlock.nbt != null){
  186.     NBTTagCompound defaultNBT = new NBTTagCompound();
  187.     NBTTagCompound nbt = bufferedBlock.nbt;
  188.     world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).writeToNBT(defaultNBT);
  189.     nbt.setString("id",defaultNBT.getString("id"));
  190.     nbt.setInteger("x",defaultNBT.getInteger("x"));
  191.     nbt.setInteger("y",defaultNBT.getInteger("y"));
  192.     nbt.setInteger("z",defaultNBT.getInteger("z"));
  193.     world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).readFromNBT(nbt);
  194.                
  195. }
  196. }
  197. }
  198. }
  199. }
  200.  
  201. public void generate(World world,BlockPos pos,float rot){
  202.  
  203. ArrayList<BufferedBlock> blocks = rotate(rot);
  204.  
  205. for(int block = 0;block < blocks.size();block++){
  206. BufferedBlock bufferedBlock = blocks.get(block);
  207. world.setBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z),bufferedBlock.block.getStateFromMeta(bufferedBlock.meta));
  208. if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) != null && bufferedBlock.nbt != null){
  209.     NBTTagCompound defaultNBT = new NBTTagCompound();
  210.     NBTTagCompound nbt = bufferedBlock.nbt;
  211.     world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).writeToNBT(defaultNBT);
  212.     nbt.setString("id",defaultNBT.getString("id"));
  213.     nbt.setInteger("x",defaultNBT.getInteger("x"));
  214.     nbt.setInteger("y",defaultNBT.getInteger("y"));
  215.     nbt.setInteger("z",defaultNBT.getInteger("z"));
  216.     world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).readFromNBT(nbt);
  217.                     if(bufferedBlock.lootTable != null){
  218.                    
  219.                     LootTable table = MainMod.lootTableUtils.getByName(bufferedBlock.lootTable);
  220.  
  221.                    
  222.                     if(table != null){
  223.                        
  224.                         if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) instanceof IInventory){
  225.                             IInventory inv = ((IInventory)world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)));
  226.                             inv.clear();
  227.                             table.fillInventory(inv);
  228.                            
  229.                            
  230.                         }
  231.                     }
  232.                 }
  233.                
  234. }
  235. }
  236.  
  237. }
  238.  
  239. public ArrayList<BufferedBlock> getArrayByFacing(EnumFacing facing){
  240. for (Map.Entry<EnumFacing,ArrayList<BufferedBlock>> entry : rotations.entrySet())
  241.         {
  242.             EnumFacing f = entry.getKey();
  243.             if(facing==f){
  244.                 return (ArrayList<BufferedBlock>)(entry.getValue());
  245.             }
  246.         }
  247. return null;
  248. }
  249.  
  250.  
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement