diff options
Diffstat (limited to 'src/main/java/com/github/technus/tectech/Util.java')
-rw-r--r-- | src/main/java/com/github/technus/tectech/Util.java | 271 |
1 files changed, 169 insertions, 102 deletions
diff --git a/src/main/java/com/github/technus/tectech/Util.java b/src/main/java/com/github/technus/tectech/Util.java index ff058d044d..ff9e7f6cb8 100644 --- a/src/main/java/com/github/technus/tectech/Util.java +++ b/src/main/java/com/github/technus/tectech/Util.java @@ -30,16 +30,19 @@ import static gregtech.api.enums.GT_Values.E; /** * Created by Tec on 21.03.2017. */ -public class Util { +public final class Util { + private Util() {} + public static String intBitsToString(int number) { - StringBuilder result = new StringBuilder(); + StringBuilder result = new StringBuilder(16); for (int i = 31; i >= 0; i--) { int mask = 1 << i; result.append((number & mask) != 0 ? "1" : "0"); - if (i % 8 == 0) - result.append(" "); + if (i % 8 == 0) { + result.append(' '); + } } result.replace(result.length() - 1, result.length(), ""); @@ -47,14 +50,15 @@ public class Util { } public static String intBitsToShortString(int number) { - StringBuilder result = new StringBuilder(); + StringBuilder result = new StringBuilder(16); for (int i = 31; i >= 0; i--) { int mask = 1 << i; result.append((number & mask) != 0 ? ":" : "."); - if (i % 8 == 0) - result.append("|"); + if (i % 8 == 0) { + result.append('|'); + } } result.replace(result.length() - 1, result.length(), ""); @@ -70,7 +74,9 @@ public class Util { IGregTechTileEntity aBaseMetaTileEntity, boolean forceCheck) { World world = aBaseMetaTileEntity.getWorld(); - if (world.isRemote) return false; + if (world.isRemote) { + return false; + } //TE Rotation byte facing = aBaseMetaTileEntity.getFrontFacing(); @@ -94,12 +100,14 @@ public class Util { b -= block; break; } if (block > '@')//characters allow to skip check a-1 skip, b-2 skips etc. + { a += block - '@'; - else if (block < '+')//used to mark THINGS + } else if (block < '+')//used to mark THINGS + { a++; - else if (block=='.') + } else if (block=='.') { a++; - else { + } else { //get x y z from rotation switch (facing) {//translation case 4: @@ -138,34 +146,42 @@ public class Util { } //that must be here since in some cases other axis (b,c) controls y - if (y < 0 || y >= 256) return false; + if (y < 0 || y >= 256) { + return false; + } //Check block if (world.blockExists(x, y, z)) {//this actually checks if the chunk is loaded at this pos switch (block) { case '-'://must be air - if (world.getBlock(x, y, z).getMaterial() != Material.air) + if (world.getBlock(x, y, z).getMaterial() != Material.air) { return false; + } break; case '+'://must not be air - if (world.getBlock(x, y, z).getMaterial() == Material.air) + if (world.getBlock(x, y, z).getMaterial() == Material.air) { return false; + } break; default: //check for block (countable) pointer = block - '0'; //countable air -> net.minecraft.block.BlockAir if (world.getBlock(x, y, z) != blockType[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Struct-block-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + " " + blockType[pointer].getUnlocalizedName()); + if (DEBUG_MODE) { + TecTech.Logger.info("Struct-block-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + ' ' + blockType[pointer].getUnlocalizedName()); + } return false; } if (world.getBlockMetadata(x, y, z) != blockMeta[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Struct-meta-id-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlockMetadata(x, y, z) + " " + blockMeta[pointer]); + if (DEBUG_MODE) { + TecTech.Logger.info("Struct-meta-id-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlockMetadata(x, y, z) + ' ' + blockMeta[pointer]); + } return false; } } - } else if (forceCheck) return false; + } else if (forceCheck) { + return false; + } a++;//block in horizontal layer } } @@ -191,7 +207,9 @@ public class Util { IGregTechTileEntity aBaseMetaTileEntity, boolean forceCheck) { World world = aBaseMetaTileEntity.getWorld(); - if (world.isRemote) return false; + if (world.isRemote) { + return false; + } //TE Rotation byte facing = aBaseMetaTileEntity.getFrontFacing(); @@ -218,12 +236,13 @@ public class Util { b -= block; break; } else if (block > '@') //characters allow to skip check a-1 skip, b-2 skips etc. + { a += block - '@'; - //else if (block < '+')//used to mark THINGS + }//else if (block < '+')//used to mark THINGS // a++; - else if (block=='.') + else if (block=='.') { a++; - else { + } else { //get x y z from rotation switch (facing) {//translation case 4: @@ -262,30 +281,36 @@ public class Util { } //that must be here since in some cases other axis (b,c) controls y - if (y < 0 || y >= 256) return false; + if (y < 0 || y >= 256) { + return false; + } //Check block if (world.blockExists(x, y, z)) {//this actually checks if the chunk is loaded at this pos switch (block) { case '-'://must be air - if (world.getBlock(x, y, z).getMaterial() != Material.air) + if (world.getBlock(x, y, z).getMaterial() != Material.air) { return false; + } break; case '+'://must not be air - if (world.getBlock(x, y, z).getMaterial() == Material.air) + if (world.getBlock(x, y, z).getMaterial() == Material.air) { return false; + } break; default://check for block (countable) if ((pointer = block - '0') >= 0) { //countable air -> net.minecraft.block.BlockAir if (world.getBlock(x, y, z) != blockType[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Struct-block-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + " " + blockType[pointer].getUnlocalizedName()); + if (DEBUG_MODE) { + TecTech.Logger.info("Struct-block-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + ' ' + blockType[pointer].getUnlocalizedName()); + } return false; } if (world.getBlockMetadata(x, y, z) != blockMeta[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Struct-meta-id-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlockMetadata(x, y, z) + " " + blockMeta[pointer]); + if (DEBUG_MODE) { + TecTech.Logger.info("Struct-meta-id-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlockMetadata(x, y, z) + ' ' + blockMeta[pointer]); + } return false; } } else if ((pointer = block - ' ') >= 0) { @@ -293,23 +318,29 @@ public class Util { try { if (igt == null || !(boolean) adder.invoke(imt, addingMethods[pointer], igt, casingTextures[pointer])) { if (world.getBlock(x, y, z) != blockTypeFallback[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Fallback-struct-block-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + " " + blockTypeFallback[pointer].getUnlocalizedName()); + if (DEBUG_MODE) { + TecTech.Logger.info("Fallback-struct-block-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlock(x, y, z).getUnlocalizedName() + ' ' + blockTypeFallback[pointer].getUnlocalizedName()); + } return false; } if (world.getBlockMetadata(x, y, z) != blockMetaFallback[pointer]) { - if (DEBUG_MODE) - TecTech.Logger.info("Fallback-Struct-meta-id-error " + x + " " + y + " " + z + " / " + a + " " + b + " " + c + " / " + world.getBlockMetadata(x, y, z) + " " + blockMetaFallback[pointer]); + if (DEBUG_MODE) { + TecTech.Logger.info("Fallback-Struct-meta-id-error " + x + ' ' + y + ' ' + z + " / " + a + ' ' + b + ' ' + c + " / " + world.getBlockMetadata(x, y, z) + ' ' + blockMetaFallback[pointer]); + } return false; } } } catch (InvocationTargetException | IllegalAccessException e) { - if (DEBUG_MODE) e.printStackTrace(); + if (DEBUG_MODE) { + e.printStackTrace(); + } return false; } } } - } else if (forceCheck) return false; + } else if (forceCheck) { + return false; + } a++;//block in horizontal layer } } @@ -337,9 +368,13 @@ public class Util { byte[] blockMeta,//use numbers 0-9 for casing types int horizontalOffset, int verticalOffset, int depthOffset, TileEntity tileEntity, int facing, boolean hintsOnly) { - if(!tileEntity.hasWorldObj()) return false; + if(!tileEntity.hasWorldObj()) { + return false; + } World world = tileEntity.getWorldObj(); - if (!world.isRemote && hintsOnly) return false; + if (!world.isRemote && hintsOnly) { + return false; + } //TE Rotation @@ -362,12 +397,14 @@ public class Util { b -= block; break; } if (block > '@')//characters allow to skip check a-1 skip, b-2 skips etc. + { a += block - '@'; - //else if (block < '+')//used to mark THINGS + }//else if (block < '+')//used to mark THINGS // a++; else if (block=='.')// this TE + { a++; - else { + } else { //get x y z from rotation switch (facing) {//translation case 4: @@ -406,7 +443,9 @@ public class Util { } //that must be here since in some cases other axis (b,c) controls y - if (y < 0 || y >= 256) return false; + if (y < 0 || y >= 256) { + return false; + } //Check block if (world.blockExists(x, y, z)) {//this actually checks if the chunk is loaded @@ -420,15 +459,18 @@ public class Util { break; default: //check for block if ((pointer = block - '0') >= 0) { - if(world.getBlock(x,y,z)!=blockType[pointer] || world.getBlockMetadata(x,y,z)!=blockMeta[pointer]) - TecTech.proxy.hint_particle(world,x, y, z, blockType[pointer], blockMeta[pointer]); + if(world.getBlock(x,y,z)!=blockType[pointer] || world.getBlockMetadata(x,y,z)!=blockMeta[pointer]) { + TecTech.proxy.hint_particle(world, x, y, z, blockType[pointer], blockMeta[pointer]); + } } else if ((pointer = block - ' ') >= 0) { - switch(pointer){ - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: - TecTech.proxy.hint_particle(world,x, y, z, TT_Container_Casings.sHintCasingsTT, pointer); break; - default:TecTech.proxy.hint_particle(world,x, y, z, TT_Container_Casings.sHintCasingsTT, 12); + if(pointer>=0 && pointer<12) { + TecTech.proxy.hint_particle(world, x, y, z, TT_Container_Casings.sHintCasingsTT, pointer); + } else { + TecTech.proxy.hint_particle(world, x, y, z, TT_Container_Casings.sHintCasingsTT, 12); } - } else TecTech.proxy.hint_particle(world,x, y, z, TT_Container_Casings.sHintCasingsTT, 15); + } else { + TecTech.proxy.hint_particle(world, x, y, z, TT_Container_Casings.sHintCasingsTT, 15); + } } }else{ switch (block) { @@ -441,13 +483,15 @@ public class Util { default: //check for block if ((pointer = block - '0') >= 0) { world.setBlock(x, y, z, blockType[pointer], blockMeta[pointer], 2); - } else if ((pointer = block - ' ') >= 0) { + } else if (block - ' ' < 0) { + world.setBlock(x, y, z, TT_Container_Casings.sHintCasingsTT, 15, 2); + } //else { //switch(pointer){ // case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: // world.setBlock(x, y, z, TT_Container_Casings.sHintCasingsTT, pointer, 2); break; // default:world.setBlock(x, y, z, TT_Container_Casings.sHintCasingsTT, 12, 2); //} - } else world.setBlock(x, y, z, TT_Container_Casings.sHintCasingsTT, 15,2); + //} } } } @@ -467,7 +511,9 @@ public class Util { //TE Rotation byte facing = aBaseMetaTileEntity.getFrontFacing(); World world = aBaseMetaTileEntity.getWorld(); - if (world.isRemote) return new String[]{"Not at Client m8"}; + if (world.isRemote) { + return new String[]{"Not at Client m8"}; + } ItemStack[] array = new ItemStack[10]; @@ -525,7 +571,9 @@ public class Util { } //that must be here since in some cases other axis (b,c) controls y - if (y < 0 || y >= 256) return new String[]{"Invalid position"}; + if (y < 0 || y >= 256) { + return new String[]{"Invalid position"}; + } //Check block Block block = world.getBlock(x, y, z); @@ -544,7 +592,9 @@ public class Util { break; } } - if (err) return new String[]{"Too much different blocks"}; + if (err) { + return new String[]{"Too much different blocks"}; + } } a++;//block in horizontal layer @@ -556,8 +606,8 @@ public class Util { List<String> output = new ArrayList<>(); - output.add("Offsets: " + horizontalOffset + " " + verticalOffset + " " + depthOffset); - output.add("Sizes: " + horizontalSize + " " + verticalSize + " " + depthSize); + output.add("Offsets: " + horizontalOffset + ' ' + verticalOffset + ' ' + depthOffset); + output.add("Sizes: " + horizontalSize + ' ' + verticalSize + ' ' + depthSize); output.add(""); output.add("ID[]: Name[]"); @@ -572,7 +622,7 @@ public class Util { output.add(""); for (int i = 0; i < array.length; i++) { if (array[i] != null) { - output.add(i + ": " + array[i].getItem().getUnlocalizedName() + " " + array[i].getItemDamage()); + output.add(i + ": " + array[i].getItem().getUnlocalizedName() + ' ' + array[i].getItemDamage()); } } output.add(""); @@ -582,10 +632,10 @@ public class Util { c = -depthOffset; for (int cz = 0; cz < depthSize; cz++) {//front to back b = verticalOffset; - String addMe = "{"; + StringBuilder addMe = new StringBuilder().append('{'); for (int by = 0; by < verticalSize; by++) {//top to bottom a = -horizontalOffset; - String line = ""; + StringBuilder line = new StringBuilder(); for (int az = 0; az < horizontalSize; az++) {//left to right //get x y z from rotation switch (facing) {//translation @@ -629,11 +679,11 @@ public class Util { int meta = world.getBlockMetadata(x, y, z); if (a == 0 && b == 0 && c == 0) { - line += '.'; + line.append('.'); } else if (block.getMaterial() == Material.air) { - line += '-'; + line.append('-'); } else if (block.hasTileEntity(meta)) { - line += '*'; + line.append('*'); } else { ItemStack stack = new ItemStack(block, 1, meta); String str = "?";//OH YEAH NPEs @@ -643,73 +693,81 @@ public class Util { break; } } - line += str; + line.append(str); } a++;//block in horizontal layer } if (ignoreAir) { - String l = ""; + StringBuilder builder = new StringBuilder(); char temp = '@'; - for (char ch : line.toCharArray()) { + for (char ch : line.toString().toCharArray()){ if (ch == '-') { temp += 1; if (temp == '~') { - l += '~'; + builder.append('~'); temp = '@'; } } else { if (temp > '@') { - l += temp; + builder.append(temp); temp = '@'; } - l += ch; + builder.append(ch); } } - while (l.length() > 0 && l.toCharArray()[l.length() - 1] == '~') - l = l.substring(0, l.length() - 1); - if (l.length() == 0) - l = "E,"; - else { - l = "\"" + l + "\","; + while (builder.length()>0 && builder.charAt(builder.length() - 1) == '~') { + builder.deleteCharAt(builder.length() - 1); + } + if (builder.length()==0) { + builder.append("E,"); + } else { + builder.insert(0,'"'); + builder.append('"').append(','); } - addMe += l; + addMe.append(builder); } else { - if (line.length() == 0) - line = "E,"; - else { - line = "\"" + line + "\","; + if (line.length()==0) { + line.append("E,"); + } else { + line.insert(0,'"'); + line.append('"').append(','); } - addMe += line; + addMe.append(line); } b--;//horizontal layer } //region less verbose - addMe=(addMe + "},").replaceAll("(E,)+(?=})",E/*Remove Empty strings at end*/); - Matcher m = matchE_.matcher(addMe); - while (m.find()) { - byte lenEE = (byte)(m.group(1).length()>>1); - addMe=addMe.replaceFirst("E,(E,)+","\"\\\\u00"+String.format("%02X", lenEE-1)+"\","); - //addMe=addMe.replaceFirst("E,(E,)+\"","\"\\\\u00"+String.format("%02X", lenEE)); + addMe.append('}').append(','); + String builtStr=addMe.toString().replaceAll("(E,)+(?=})",E/*Remove Empty strings at end*/); + Matcher matcher = matchE_.matcher(builtStr); + while (matcher.find()) { + byte lenEE = (byte)(matcher.group(1).length()>>1); + builtStr=builtStr.replaceFirst("E,(E,)+","\"\\\\u00"+String.format("%02X", lenEE-1)+"\","); + //builtStr=builtStr.replaceFirst("E,(E,)+\"","\"\\\\u00"+String.format("%02X", lenEE)); } //endregion - output.add(addMe); + output.add(builtStr); c++;//depth } output.add("}"); - return output.toArray(new String[0]); + return output.toArray(new String[output.size()]); } private static final Pattern matchE_ = Pattern.compile("(E,(E,)+)"); public static boolean isInputEqual(boolean aDecreaseStacksizeBySuccess, boolean aDontCheckStackSizes, FluidStack[] requiredFluidInputs, ItemStack[] requiredInputs, FluidStack[] givenFluidInputs, ItemStack... givenInputs) { - if (!GregTech_API.sPostloadFinished) return false; - if (requiredFluidInputs.length > 0 && givenFluidInputs == null) return false; + if (!GregTech_API.sPostloadFinished) { + return false; + } + if (requiredFluidInputs.length > 0 && givenFluidInputs == null) { + return false; + } int amt; - for (FluidStack tFluid : requiredFluidInputs) + for (FluidStack tFluid : requiredFluidInputs) { if (tFluid != null) { boolean temp = true; amt = tFluid.amount; - for (FluidStack aFluid : givenFluidInputs) + for (FluidStack aFluid : givenFluidInputs) { if (aFluid != null && aFluid.isFluidEqual(tFluid)) { if (aDontCheckStackSizes) { temp = false; @@ -721,10 +779,16 @@ public class Util { break; } } - if (temp) return false; + } + if (temp) { + return false; + } } + } - if (requiredInputs.length > 0 && givenInputs == null) return false; + if (requiredInputs.length > 0 && givenInputs == null) { + return false; + } for (ItemStack tStack : requiredInputs) { if (tStack != null) { amt = tStack.stackSize; @@ -742,7 +806,9 @@ public class Util { } } } - if (temp) return false; + if (temp) { + return false; + } } } @@ -798,7 +864,7 @@ public class Util { } public static String getUniqueIdentifier(ItemStack is) { - return GameRegistry.findUniqueIdentifierFor(is.getItem()).modId + ":" + is.getUnlocalizedName(); + return GameRegistry.findUniqueIdentifierFor(is.getItem()).modId + ':' + is.getUnlocalizedName(); } @@ -806,16 +872,16 @@ public class Util { public static final long[] V = new long[]{8L, 32L, 128L, 512L, 2048L, 8192L, 32768L, 131072L, 524288L, 2097152L, 8388608L, 33554432L, 134217728L, 536870912L, 1073741824L, Integer.MAX_VALUE-7}; public static byte getTier(long l) { - byte i = -1; + byte b = -1; do { - ++i; - if (i >= V.length) { - return i; + ++b; + if (b >= V.length) { + return b; } - } while(l > V[i]); + } while(l > V[b]); - return i; + return b; } public static String[] splitButDifferent(String string,String delimiter){ @@ -832,8 +898,9 @@ public class Util { public static String[] infoFromNBT(NBTTagCompound nbt) { String[] strings = new String[nbt.getInteger("i")]; - for (int i = 0; i < strings.length; i++) + for (int i = 0; i < strings.length; i++) { strings[i] = nbt.getString(Integer.toString(i)); + } return strings; } |