diff options
author | Tec <daniel112092@gmail.com> | 2020-06-23 21:07:50 +0200 |
---|---|---|
committer | Tec <daniel112092@gmail.com> | 2020-06-23 21:07:50 +0200 |
commit | 3707610dd388d8cdcb9f3a1f30de6249c65ad782 (patch) | |
tree | 0089db40cd698d060a01a1b1ea51dce45200e094 | |
parent | ca0d8f6c57f22620934d049566218d0e71f3100f (diff) | |
download | GT5-Unofficial-3707610dd388d8cdcb9f3a1f30de6249c65ad782.tar.gz GT5-Unofficial-3707610dd388d8cdcb9f3a1f30de6249c65ad782.tar.bz2 GT5-Unofficial-3707610dd388d8cdcb9f3a1f30de6249c65ad782.zip |
Cleanup code style, rename interface, unimplement default method to force user to implement it.
rework to eager evaluation of instanceof check
4 files changed, 236 insertions, 126 deletions
diff --git a/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomBlockSetting.java b/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomBlockSetting.java new file mode 100644 index 0000000000..b70e76fbac --- /dev/null +++ b/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomBlockSetting.java @@ -0,0 +1,17 @@ +package com.github.technus.tectech.mechanics.structure; + +import net.minecraft.block.Block; +import net.minecraft.world.World; + +public interface ICustomBlockSetting { + /** + * Default block setting calls {@link World#setBlock(int x, int y, int z, Block block, int meta, int updateType)} like: + * {@code world.setBlock(x,y,z,this/block,meta,2)} where updateType 2 means to update lighting and stuff + * @param world world that should be affected + * @param x x position to set + * @param y y position to set + * @param z z position to set + * @param meta required meta + */ + void setBlock(World world, int x, int y, int z, int meta); +} diff --git a/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomMetaBlock.java b/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomMetaBlock.java deleted file mode 100644 index 2c783ef268..0000000000 --- a/src/main/java/com/github/technus/tectech/mechanics/structure/ICustomMetaBlock.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.technus.tectech.mechanics.structure; - -import net.minecraft.block.Block; -import net.minecraft.world.World; - -public interface ICustomMetaBlock { - default void setBlock(World world, int x, int y, int z, int meta){ - world.setBlock(x, y, z, (Block)this, meta, 2); - } -} diff --git a/src/main/java/com/github/technus/tectech/mechanics/structure/Structure.java b/src/main/java/com/github/technus/tectech/mechanics/structure/Structure.java index 9e680b877e..4282a9e13c 100644 --- a/src/main/java/com/github/technus/tectech/mechanics/structure/Structure.java +++ b/src/main/java/com/github/technus/tectech/mechanics/structure/Structure.java @@ -16,10 +16,11 @@ import static com.github.technus.tectech.loader.TecTechConfig.DEBUG_MODE; @Deprecated public class Structure { - private Structure(){} + private Structure() { + } @SafeVarargs - public static <T> IHatchAdder<T>[] adders(IHatchAdder<T>... iHatchAdder){ + public static <T> IHatchAdder<T>[] adders(IHatchAdder<T>... iHatchAdder) { return iHatchAdder; } @@ -121,7 +122,7 @@ public class Structure { } } else if ((pointer = block - ' ') >= 0) { igt = aBaseMetaTileEntity.getIGregTechTileEntity(xyz[0], xyz[1], xyz[2]); - if (igt == null || !addingMethods[pointer].apply(metaTile,igt, casingTextures[pointer])) { + if (igt == null || !addingMethods[pointer].apply(metaTile, igt, casingTextures[pointer])) { if (worldblock != blockTypeFallback[pointer]) { if (DEBUG_MODE) { TecTech.LOGGER.info("Fallback-struct-block-error " + xyz[0] + ' ' + xyz[1] + ' ' + xyz[2] + " / " + abc[0] + ' ' + abc[1] + ' ' + abc[2] + " / " + worldblock.getUnlocalizedName() + ' ' + (blockTypeFallback[pointer] == null ? "null" : blockTypeFallback[pointer].getUnlocalizedName())); @@ -155,10 +156,10 @@ public class Structure { byte[] blockMeta,//use numbers 0-9 for casing types int horizontalOffset, int verticalOffset, int depthOffset, IGregTechTileEntity tileEntity, ExtendedFacing extendedFacing, boolean hintsOnly) { - World world=tileEntity.getWorld(); - int baseX=tileEntity.getXCoord(); - int baseY=tileEntity.getYCoord(); - int baseZ=tileEntity.getZCoord(); + World world = tileEntity.getWorld(); + int baseX = tileEntity.getXCoord(); + int baseY = tileEntity.getYCoord(); + int baseZ = tileEntity.getZCoord(); if (world == null || (!world.isRemote && hintsOnly)) { return false; } @@ -214,7 +215,7 @@ public class Structure { break; default: //check for block if ((pointer = block - '0') >= 0) { - if (world.getBlock(xyz[0], xyz[1], xyz[2]) != blockType[pointer] || blockType[pointer].getDamageValue(world,xyz[0], xyz[1], xyz[2]) != blockMeta[pointer]) { + if (world.getBlock(xyz[0], xyz[1], xyz[2]) != blockType[pointer] || blockType[pointer].getDamageValue(world, xyz[0], xyz[1], xyz[2]) != blockMeta[pointer]) { TecTech.proxy.hint_particle(world, xyz[0], xyz[1], xyz[2], blockType[pointer], blockMeta[pointer]); } } else if ((pointer = block - ' ') >= 0) { @@ -237,10 +238,11 @@ public class Structure { break; default: //check for block if ((pointer = block - '0') >= 0) { - if (blockType[pointer] instanceof ICustomMetaBlock) - ((ICustomMetaBlock)blockType[pointer]).setBlock(world,xyz[0], xyz[1], xyz[2], blockMeta[pointer]); - else + if (blockType[pointer] instanceof ICustomBlockSetting) { + ((ICustomBlockSetting) blockType[pointer]).setBlock(world, xyz[0], xyz[1], xyz[2], blockMeta[pointer]); + } else { world.setBlock(xyz[0], xyz[1], xyz[2], blockType[pointer], blockMeta[pointer], 2); + } } } } diff --git a/src/main/java/com/github/technus/tectech/mechanics/structure/StructureUtility.java b/src/main/java/com/github/technus/tectech/mechanics/structure/StructureUtility.java index 10779659dc..a9b88af2f3 100644 --- a/src/main/java/com/github/technus/tectech/mechanics/structure/StructureUtility.java +++ b/src/main/java/com/github/technus/tectech/mechanics/structure/StructureUtility.java @@ -167,7 +167,7 @@ public class StructureUtility { * @param <T> * @return */ - public static <T> IStructureElementNoPlacement<T> ofHintDeferred(Supplier<IIcon[]> icons,short[] RGBa) { + public static <T> IStructureElementNoPlacement<T> ofHintDeferred(Supplier<IIcon[]> icons, short[] RGBa) { return new IStructureElementNoPlacement<T>() { @Override public boolean check(T t, World world, int x, int y, int z) { @@ -176,7 +176,7 @@ public class StructureUtility { @Override public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle_tinted(world, x, y, z, icons.get(),RGBa); + TecTech.proxy.hint_particle_tinted(world, x, y, z, icons.get(), RGBa); return false; } }; @@ -266,7 +266,7 @@ public class StructureUtility { @Override public boolean check(T t, World world, int x, int y, int z) { Block worldBlock = world.getBlock(x, y, z); - return iBlockAdder.apply(t, worldBlock, worldBlock.getDamageValue(world,x,y,z)); + return iBlockAdder.apply(t, worldBlock, worldBlock.getDamageValue(world, x, y, z)); } @Override @@ -284,28 +284,47 @@ public class StructureUtility { if (blocsMap == null || blocsMap.isEmpty() || defaultBlock == null) { throw new IllegalArgumentException(); } - return new IStructureElement<T>() { - @Override - public boolean check(T t, World world, int x, int y, int z) { - Block worldBlock = world.getBlock(x, y, z); - return blocsMap.getOrDefault(worldBlock, -1) == worldBlock.getDamageValue(world,x,y,z); - } + if(defaultBlock instanceof ICustomBlockSetting){ + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return blocsMap.getOrDefault(worldBlock, -1) == worldBlock.getDamageValue(world, x, y, z); + } - @Override - public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { - if (defaultBlock instanceof ICustomMetaBlock) - ((ICustomMetaBlock)defaultBlock).setBlock(world, x, y, z, defaultMeta); - else + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { + ((ICustomBlockSetting) defaultBlock).setBlock(world, x, y, z, defaultMeta); + return true; + } + + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + }else { + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return blocsMap.getOrDefault(worldBlock, -1) == worldBlock.getDamageValue(world, x, y, z); + } + + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { world.setBlock(x, y, z, defaultBlock, defaultMeta, 2); - return true; - } + return true; + } - @Override - public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); - return true; - } - }; + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + } } /** @@ -320,56 +339,94 @@ public class StructureUtility { throw new IllegalArgumentException(); } } - return new IStructureElement<T>() { - @Override - public boolean check(T t, World world, int x, int y, int z) { - Block worldBlock = world.getBlock(x, y, z); - return blocsMap.getOrDefault(worldBlock, Collections.emptySet()).contains(worldBlock.getDamageValue(world, x, y, z)); - } + if(defaultBlock instanceof ICustomBlockSetting){ + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return blocsMap.getOrDefault(worldBlock, Collections.emptySet()).contains(worldBlock.getDamageValue(world, x, y, z)); + } - @Override - public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { - if (defaultBlock instanceof ICustomMetaBlock) - ((ICustomMetaBlock)defaultBlock).setBlock(world, x, y, z, defaultMeta); - else + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { + ((ICustomBlockSetting) defaultBlock).setBlock(world, x, y, z, defaultMeta); + return true; + } + + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + }else { + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return blocsMap.getOrDefault(worldBlock, Collections.emptySet()).contains(worldBlock.getDamageValue(world, x, y, z)); + } + + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { world.setBlock(x, y, z, defaultBlock, defaultMeta, 2); - return true; - } + return true; + } - @Override - public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); - return true; - } - }; + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + } } public static <T> IStructureElement<T> ofBlock(Block block, int meta, Block defaultBlock, int defaultMeta) { if (block == null || defaultBlock == null) { throw new IllegalArgumentException(); } - return new IStructureElement<T>() { - @Override - public boolean check(T t, World world, int x, int y, int z) { - Block worldBlock = world.getBlock(x, y, z); - return block == worldBlock && meta == worldBlock.getDamageValue(world, x, y, z); - } + if(block instanceof ICustomBlockSetting){ + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return block == worldBlock && meta == worldBlock.getDamageValue(world, x, y, z); + } - @Override - public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { - if (defaultBlock instanceof ICustomMetaBlock) - ((ICustomMetaBlock)defaultBlock).setBlock(world, x, y, z, defaultMeta); - else + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { + ((ICustomBlockSetting) defaultBlock).setBlock(world, x, y, z, defaultMeta); + return true; + } + + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + } else { + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return block == worldBlock && meta == worldBlock.getDamageValue(world, x, y, z); + } + + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { world.setBlock(x, y, z, defaultBlock, defaultMeta, 2); - return true; - } + return true; + } - @Override - public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); - return true; - } - }; + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + } } public static <T> IStructureElement<T> ofBlock(Block block, int meta) { @@ -384,28 +441,47 @@ public class StructureUtility { if (iBlockAdder == null || defaultBlock == null) { throw new IllegalArgumentException(); } - return new IStructureElement<T>() { - @Override - public boolean check(T t, World world, int x, int y, int z) { - Block worldBlock = world.getBlock(x, y, z); - return iBlockAdder.apply(t, worldBlock, worldBlock.getDamageValue(world, x, y, z)); - } + if(defaultBlock instanceof ICustomBlockSetting){ + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return iBlockAdder.apply(t, worldBlock, worldBlock.getDamageValue(world, x, y, z)); + } - @Override - public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { - if (defaultBlock instanceof ICustomMetaBlock) - ((ICustomMetaBlock)defaultBlock).setBlock(world, x, y, z, defaultMeta); - else + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { + ((ICustomBlockSetting) defaultBlock).setBlock(world, x, y, z, defaultMeta); + return true; + } + + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + }else { + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + Block worldBlock = world.getBlock(x, y, z); + return iBlockAdder.apply(t, worldBlock, worldBlock.getDamageValue(world, x, y, z)); + } + + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { world.setBlock(x, y, z, defaultBlock, defaultMeta, 2); - return true; - } + return true; + } - @Override - public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); - return true; - } - }; + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, defaultBlock, defaultMeta); + return true; + } + }; + } } public static <T> IStructureElement<T> ofBlockAdder(IBlockAdder<T> iBlockAdder, int dots) { @@ -462,31 +538,53 @@ public class StructureUtility { if (iHatchAdder == null || hintBlock == null) { throw new IllegalArgumentException(); } - return new IStructureElement<T>() { - @Override - public boolean check(T t, World world, int x, int y, int z) { - TileEntity tileEntity = world.getTileEntity(x, y, z); - Block worldBlock = world.getBlock(x, y, z); - return (tileEntity instanceof IGregTechTileEntity && - iHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) textureIndex)) || - (worldBlock == placeCasing && worldBlock.getDamageValue(world, x, y, z) == placeCasingMeta); - } + if(placeCasing instanceof ICustomBlockSetting){ + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + Block worldBlock = world.getBlock(x, y, z); + return (tileEntity instanceof IGregTechTileEntity && + iHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) textureIndex)) || + (worldBlock == placeCasing && worldBlock.getDamageValue(world, x, y, z) == placeCasingMeta); + } - @Override - public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { - TecTech.proxy.hint_particle(world, x, y, z, hintBlock, hintMeta); - return true; - } + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, hintBlock, hintMeta); + return true; + } - @Override - public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { - if (placeCasing instanceof ICustomMetaBlock) - ((ICustomMetaBlock)placeCasing).setBlock(world, x, y, z, placeCasingMeta); - else + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { + ((ICustomBlockSetting) placeCasing).setBlock(world, x, y, z, placeCasingMeta); + return true; + } + }; + }else { + return new IStructureElement<T>() { + @Override + public boolean check(T t, World world, int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + Block worldBlock = world.getBlock(x, y, z); + return (tileEntity instanceof IGregTechTileEntity && + iHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) textureIndex)) || + (worldBlock == placeCasing && worldBlock.getDamageValue(world, x, y, z) == placeCasingMeta); + } + + @Override + public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) { + TecTech.proxy.hint_particle(world, x, y, z, hintBlock, hintMeta); + return true; + } + + @Override + public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) { world.setBlock(x, y, z, placeCasing, placeCasingMeta, 2); - return true; - } - }; + return true; + } + }; + } } //endregion @@ -885,6 +983,7 @@ public class StructureUtility { /** * Used internally, to generate skips for structure definitions + * * @param a * @param b * @param c @@ -897,6 +996,7 @@ public class StructureUtility { /** * Used internally, to generate skips for structure definitions + * * @param step * @param <T> * @return @@ -1119,7 +1219,7 @@ public class StructureUtility { if (tileEntity == null) { Block block = w.getBlock(x, y, z); if (block != null && block != Blocks.air) { - builder.append(map.get(block.getUnlocalizedName() + '\0' + block.getDamageValue(world,x, y, z))); + builder.append(map.get(block.getUnlocalizedName() + '\0' + block.getDamageValue(world, x, y, z))); } else { builder.append(' '); } @@ -1211,14 +1311,15 @@ public class StructureUtility { /** * Transposes shape (swaps B and C axis, can be used to un-transpose transposed shape) * WARNING! Do not use on old api... + * * @param structurePiece shape (transposed shape) * @return transposed shape (untransposed shape) */ - public static String[][] transpose(String[][] structurePiece){ - String[][] shape=new String[structurePiece[0].length][structurePiece.length]; + public static String[][] transpose(String[][] structurePiece) { + String[][] shape = new String[structurePiece[0].length][structurePiece.length]; for (int i = 0; i < structurePiece.length; i++) { for (int j = 0; j < structurePiece[i].length; j++) { - shape[j][i]=structurePiece[i][j]; + shape[j][i] = structurePiece[i][j]; } } return shape; |