Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Structure implements Runnable{
- public ArrayList<BufferedBlock> default_state_blocks = new ArrayList<BufferedBlock>();
- public EnumFacing default_facing = EnumFacing.SOUTH;
- public float neededRoom = 0.75f;
- public float neededFlatness = 0.75f;
- public HashMap<EnumFacing,ArrayList<BufferedBlock>> rotations = new HashMap<EnumFacing,ArrayList<BufferedBlock>>();
- String name;
- public Structure(String name){
- this.name = name;
- }
- public void run(){
- try{
- load();
- }
- catch(IOException e){
- e.printStackTrace();
- }
- }
- public void load() throws IOException{
- InputStream fis = getClass().getResourceAsStream(new StringBuilder().append("/assets/minecraft/structures/").append(name).toString());
- InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
- BufferedReader br = new BufferedReader(isr);
- String line;
- while ((line = br.readLine()) != null) {
- String[] words = line.split(" ");
- int i = Integer.parseInt(words[0]);
- int j = Integer.parseInt(words[1]);
- int k = Integer.parseInt(words[2]);
- Block block = Block.getBlockFromName(words[3]);
- int meta = Integer.parseInt(words[4]);
- default_state_blocks.add(new BufferedBlock(i,j,k,block,meta));
- }
- loadRotations();
- }
- public void loadRotations(){
- for(int i = 0;i<EnumFacing.HORIZONTALS.length;i++){
- EnumFacing facing = EnumFacing.HORIZONTALS[i];
- rotations.put(facing,rotate(getAngle(facing)));
- }
- }
- public ArrayList<BufferedBlock> rotate(float rot){
- ArrayList<BufferedBlock> blocks = new ArrayList<BufferedBlock>();
- for(int block = 0;block < default_state_blocks.size();block++){
- BufferedBlock bufferedBlock = default_state_blocks.get(block).copy();
- IBlockState state = bufferedBlock.block.getStateFromMeta(bufferedBlock.meta);
- IProperty propertyDirection = hasProperty(state,PropertyDirection.class);
- if(propertyDirection != null){
- EnumFacing facing = (EnumFacing)(state.getValue(propertyDirection));
- if(facing != EnumFacing.UP && facing != EnumFacing.DOWN){
- for(int i = 0;i < (int)Math.abs(rot/90);i++){
- facing = facing.rotateY();
- }
- if((int)Math.abs(rot/90) % 2 != 0){
- facing = facing.getOpposite();
- }
- }
- bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(propertyDirection,facing));
- }
- IProperty propertyAxis = hasProperty(state,PropertyEnum.class);
- if(propertyAxis != null && propertyAxis.getValueClass() == EnumFacing.Axis.class){
- PropertyEnum<EnumFacing.Axis> AXIS = (PropertyEnum<EnumFacing.Axis>)(propertyAxis);
- EnumFacing.Axis axis = state.getValue(AXIS);
- for(int i = 0;i < (int)Math.abs(rot/90);i++){
- if(axis == EnumFacing.Axis.X){
- axis = EnumFacing.Axis.Z;
- }
- else if(axis == EnumFacing.Axis.Z){
- axis = EnumFacing.Axis.X;
- }
- }
- bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(AXIS,axis));
- }
- if(propertyAxis != null && propertyAxis.getValueClass() == BlockLog.EnumAxis.class){
- PropertyEnum<BlockLog.EnumAxis> AXIS = (PropertyEnum<BlockLog.EnumAxis>)(propertyAxis);
- BlockLog.EnumAxis axis = state.getValue(AXIS);
- for(int i = 0;i < (int)Math.abs(rot/90);i++){
- if(axis == BlockLog.EnumAxis.X){
- axis = BlockLog.EnumAxis.Z;
- }
- else if(axis == BlockLog.EnumAxis.Z){
- axis = BlockLog.EnumAxis.X;
- }
- }
- bufferedBlock.meta = bufferedBlock.block.getMetaFromState(state.withProperty(AXIS,axis));
- }
- blocks.add(bufferedBlock.rotate(rot));
- }
- return blocks;
- }
- public <T> IProperty hasProperty(IBlockState state, Class<T> type) {
- for(IProperty property : state.getPropertyNames()){
- //System.out.println("Expected class " + type + "Founded class" + property.getClass());
- if(property.getClass() == type){
- return property;
- }
- }
- return null;
- }
- public float getAngle(EnumFacing facing){
- switch(facing){
- case SOUTH : { return 0;}
- case WEST : {return 90;}
- case NORTH : {return 180;}
- case EAST : {return 270;}
- }
- return 0;
- }
- public float testForRoom(World world,BlockPos pos,EnumFacing facing){
- ArrayList<BufferedBlock> blocks = getArrayByFacing(facing);
- int replacable = 0;
- for(int block = 0;block < blocks.size();block++){
- BufferedBlock bufferedBlock = blocks.get(block);
- if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isReplaceable()){
- replacable++;
- }
- }
- return (float)replacable/blocks.size();
- }
- public float testForRoom(World world,BlockPos pos,ArrayList<BufferedBlock> blocks){
- int replacable = 0;
- for(int block = 0;block < blocks.size();block++){
- BufferedBlock bufferedBlock = blocks.get(block);
- if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isReplaceable()){
- replacable++;
- }
- }
- return (float)replacable/blocks.size();
- }
- public float testForFlatness(World world,BlockPos pos,ArrayList<BufferedBlock> blocks){
- float flat = 0;
- int pedestalSize = 0;
- for(int block = 0;block < blocks.size();block++){
- BufferedBlock bufferedBlock = blocks.get(block);
- if(bufferedBlock.y == 0){
- pedestalSize++;
- if(world.getBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).getBlock().getMaterial().isSolid()){
- flat++;
- }else{
- //TO-DO
- }
- }
- }
- return (float)flat/pedestalSize;
- }
- public void generate(World world,BlockPos pos,EnumFacing facing){
- ArrayList<BufferedBlock> blocks = getArrayByFacing(facing);
- if(testForRoom(world,pos,blocks) >= neededRoom){
- if(testForFlatness(world,pos,blocks) >= neededFlatness){
- for(int block = 0;block < blocks.size();block++){
- BufferedBlock bufferedBlock = blocks.get(block);
- world.setBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z),bufferedBlock.block.getStateFromMeta(bufferedBlock.meta));
- if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) != null && bufferedBlock.nbt != null){
- NBTTagCompound defaultNBT = new NBTTagCompound();
- NBTTagCompound nbt = bufferedBlock.nbt;
- world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).writeToNBT(defaultNBT);
- nbt.setString("id",defaultNBT.getString("id"));
- nbt.setInteger("x",defaultNBT.getInteger("x"));
- nbt.setInteger("y",defaultNBT.getInteger("y"));
- nbt.setInteger("z",defaultNBT.getInteger("z"));
- world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).readFromNBT(nbt);
- }
- }
- }
- }
- }
- public void generate(World world,BlockPos pos,float rot){
- ArrayList<BufferedBlock> blocks = rotate(rot);
- for(int block = 0;block < blocks.size();block++){
- BufferedBlock bufferedBlock = blocks.get(block);
- world.setBlockState(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z),bufferedBlock.block.getStateFromMeta(bufferedBlock.meta));
- if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) != null && bufferedBlock.nbt != null){
- NBTTagCompound defaultNBT = new NBTTagCompound();
- NBTTagCompound nbt = bufferedBlock.nbt;
- world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).writeToNBT(defaultNBT);
- nbt.setString("id",defaultNBT.getString("id"));
- nbt.setInteger("x",defaultNBT.getInteger("x"));
- nbt.setInteger("y",defaultNBT.getInteger("y"));
- nbt.setInteger("z",defaultNBT.getInteger("z"));
- world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)).readFromNBT(nbt);
- if(bufferedBlock.lootTable != null){
- LootTable table = MainMod.lootTableUtils.getByName(bufferedBlock.lootTable);
- if(table != null){
- if(world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)) instanceof IInventory){
- IInventory inv = ((IInventory)world.getTileEntity(pos.add(bufferedBlock.x,bufferedBlock.y,bufferedBlock.z)));
- inv.clear();
- table.fillInventory(inv);
- }
- }
- }
- }
- }
- }
- public ArrayList<BufferedBlock> getArrayByFacing(EnumFacing facing){
- for (Map.Entry<EnumFacing,ArrayList<BufferedBlock>> entry : rotations.entrySet())
- {
- EnumFacing f = entry.getKey();
- if(facing==f){
- return (ArrayList<BufferedBlock>)(entry.getValue());
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement