Vos plus belles optimisations de code !
-
Tout est dans le titre, venez poster ici vos plus belles optimisations de code
Je commence :
code non optimisé, 184 lignes :
private boolean canSpawn(World world, BlockPos pos) { boolean b = true; /* blocks d'air - couche 0 */ if(world.isAirBlock(pos) && world.isAirBlock(pos.add(0, 0, 1)) && world.isAirBlock(pos.add(0, 0, 2)) && world.isAirBlock(pos.add(0, 0, 3)) && world.isAirBlock(pos.add(1, 0, 0)) && world.isAirBlock(pos.add(1, 0, 1)) && world.isAirBlock(pos.add(1, 0, 2)) && world.isAirBlock(pos.add(1, 0, 3)) && world.isAirBlock(pos.add(2, 0, 0)) && world.isAirBlock(pos.add(2, 0, 1)) && world.isAirBlock(pos.add(2, 0, 2)) && world.isAirBlock(pos.add(2, 0, 3)) && world.isAirBlock(pos.add(3, 0, 0)) && world.isAirBlock(pos.add(3, 0, 1)) && world.isAirBlock(pos.add(3, 0, 2)) && world.isAirBlock(pos.add(3, 0, 3)) && world.isAirBlock(pos.add(4, 0, 0)) && world.isAirBlock(pos.add(4, 0, 1)) && world.isAirBlock(pos.add(4, 0, 2)) && world.isAirBlock(pos.add(4, 0, 3)) && world.isAirBlock(pos.add(5, 0, 0)) && world.isAirBlock(pos.add(5, 0, 1)) && world.isAirBlock(pos.add(5, 0, 2)) && world.isAirBlock(pos.add(5, 0, 3)) && world.isAirBlock(pos.add(6, 0, 0)) && world.isAirBlock(pos.add(6, 0, 1)) && world.isAirBlock(pos.add(6, 0, 2)) && world.isAirBlock(pos.add(6, 0, 3)) /* blocks d'air - couche 1 */ && world.isAirBlock(pos.add(0, 1, 0)) && world.isAirBlock(pos.add(0, 1, 1)) && world.isAirBlock(pos.add(0, 1, 2)) && world.isAirBlock(pos.add(0, 1, 3)) && world.isAirBlock(pos.add(1, 1, 0)) && world.isAirBlock(pos.add(1, 1, 1)) && world.isAirBlock(pos.add(1, 1, 2)) && world.isAirBlock(pos.add(1, 1, 3)) && world.isAirBlock(pos.add(2, 1, 0)) && world.isAirBlock(pos.add(2, 1, 1)) && world.isAirBlock(pos.add(2, 1, 2)) && world.isAirBlock(pos.add(2, 1, 3)) && world.isAirBlock(pos.add(3, 1, 0)) && world.isAirBlock(pos.add(3, 1, 1)) && world.isAirBlock(pos.add(3, 1, 2)) && world.isAirBlock(pos.add(3, 1, 3)) && world.isAirBlock(pos.add(4, 1, 0)) && world.isAirBlock(pos.add(4, 1, 1)) && world.isAirBlock(pos.add(4, 1, 2)) && world.isAirBlock(pos.add(4, 1, 3)) && world.isAirBlock(pos.add(5, 1, 0)) && world.isAirBlock(pos.add(5, 1, 1)) && world.isAirBlock(pos.add(5, 1, 2)) && world.isAirBlock(pos.add(5, 1, 3)) && world.isAirBlock(pos.add(6, 1, 0)) && world.isAirBlock(pos.add(6, 1, 1)) && world.isAirBlock(pos.add(6, 1, 2)) && world.isAirBlock(pos.add(6, 1, 3)) /* blocks d'air - couche 2 */ && world.isAirBlock(pos.add(0, 2, 0)) && world.isAirBlock(pos.add(0, 2, 1)) && world.isAirBlock(pos.add(0, 2, 2)) && world.isAirBlock(pos.add(0, 2, 3)) && world.isAirBlock(pos.add(1, 2, 0)) && world.isAirBlock(pos.add(1, 2, 1)) && world.isAirBlock(pos.add(1, 2, 2)) && world.isAirBlock(pos.add(1, 2, 3)) && world.isAirBlock(pos.add(2, 2, 0)) && world.isAirBlock(pos.add(2, 2, 1)) && world.isAirBlock(pos.add(2, 2, 2)) && world.isAirBlock(pos.add(2, 2, 3)) && world.isAirBlock(pos.add(3, 2, 0)) && world.isAirBlock(pos.add(3, 2, 1)) && world.isAirBlock(pos.add(3, 2, 2)) && world.isAirBlock(pos.add(3, 2, 3)) && world.isAirBlock(pos.add(4, 2, 0)) && world.isAirBlock(pos.add(4, 2, 1)) && world.isAirBlock(pos.add(4, 2, 2)) && world.isAirBlock(pos.add(4, 2, 3)) && world.isAirBlock(pos.add(5, 2, 0)) && world.isAirBlock(pos.add(5, 2, 1)) && world.isAirBlock(pos.add(5, 2, 2)) && world.isAirBlock(pos.add(5, 2, 3)) && world.isAirBlock(pos.add(6, 2, 0)) && world.isAirBlock(pos.add(6, 2, 1)) && world.isAirBlock(pos.add(6, 2, 2)) && world.isAirBlock(pos.add(6, 2, 3)) /* blocks de lave - couche -1 */ && world.getBlockState(pos.add(0, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -1, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -1, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -1, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -1, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -1, 3)) == Blocks.lava.getDefaultState() /*Blocks de lave - couche -2 */ && world.getBlockState(pos.add(0, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -2, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -2, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -2, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -2, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -2, 3)) == Blocks.lava.getDefaultState() /* Blocks de lave - courche -3 */ && world.getBlockState(pos.add(0, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(0, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(1, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(2, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(3, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(4, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(5, -3, 3)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -3, 0)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -3, 1)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -3, 2)) == Blocks.lava.getDefaultState() && world.getBlockState(pos.add(6, -3, 3)) == Blocks.lava.getDefaultState()) return true; return false; }
code optimisé, 11 lignes (sans compacter les accolades parce que les gens qui font ça sont des dégueulasses) :
private boolean canSpawn(World world, BlockPos pos) { for(int i = 0; i <= 6; i++) { for(int j = 0; j <= 2; j++) { for(int k = 0; k <= 3; k++) { if(!world.isAirBlock(pos.add(i, j, k)) || !(world.getBlockState(pos.add(i, -(j+1), k)) == Blocks.lava.getDefaultState())) return false; } } } return true; }
-
Je ne compactais pas les accolades car je trouvais ça dégueulasse mais maintenant je le fais et je préfère tu gagne beaucoup de place quand il commence à y avoir beaucoup de boucles et de fonctions
-
@‘BrokenSwing’:
Je ne compactais pas les accolades car je trouvais ça dégueulasse mais maintenant je le fais et je préfère tu gagne beaucoup de place quand il commence à y avoir beaucoup de boucles et de fonctions
Perso je ne m’y repère plus si les fins d’accolades sont pas alignés au début des fonctions mais je peux comprendre
-
Pour quelqu’un une fois j’avais optimisé un labyrinthe : à la base il se générer toujours pareil et il y avait plusieurs milliers de lignes de code, voici comment je l’ai optimisée :
package fr.scarex.st18.gen.structure; import java.util.Random; import net.minecraft.block.BlockChest; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.monster.EntityZombie; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityChest; import net.minecraft.tileentity.TileEntityMobSpawner; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.gen.feature.WorldGenerator; import fr.scarex.st18.ST18; public class ZombieDongeon extends WorldGenerator { public static IBlockState state = Blocks.mossy_cobblestone.getDefaultState(); public static IBlockState state1 = Blocks.cobblestone.getDefaultState(); @Override public boolean generate(World world, Random rand, BlockPos pos) { if (pos.getY() > 4 && pos.getY() < 30 && world.getBiomeGenForCoords(pos) == BiomeGenBase.plains && rand.nextInt(40) == 0) { generateAll(world, pos.getX(), pos.getY(), pos.getZ(), rand); return true; } return false; } public static void generateFloorAndCeiling(World world, int x, int y, int z) { /* ====================Generating floor==================== */ for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { world.setBlockState(new BlockPos(x + i, y, z + j), state); } } /* ====================Generating roof==================== */ for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { world.setBlockState(new BlockPos(x + i, y + 5, z + j), state); } } } public static void generateForHall(World world, int x, int y, int z) { /* ====================Generating floor==================== */ for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { world.setBlockState(new BlockPos(x + i, y, z + j), state); } } /* ====================Generating roof==================== */ for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { world.setBlockState(new BlockPos(x + i, y + 5, z + j), state); } } } public static void generateCorridor(World world, int x, int y, int z) { /* ====================Generating walls==================== */ for (int i = 0; i < 5; i++) { world.setBlockState(new BlockPos(x + i, y, z), state1); world.setBlockState(new BlockPos(x + i, y, z + 4), state1); } /* ====================Clearing interior==================== */ for (int i = 0; i < 5; i++) { for (int j = 1; j <= 3; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } } public static void generate1(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate2(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate3(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate4(World world, int x, int y, int z) { for (int i = 0; i < 6; i += 1) { if (i % 2 == 0) { for (int j = 0; j <= 5; j += 3) { world.setBlockState(new BlockPos(x + i, y, z + j), state1); world.setBlockState(new BlockPos(x + i, y, z + j + 1), state1); } world.setBlockToAir(new BlockPos(x + i, y, z + 2)); } else { for (int j = 0; j < 6; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } } } public static void generate5(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate6(World world, int x, int y, int z) { for (int i = 0; i < 5; i++) { if (i % 2 == 0) { for (int j = 0; j < 5; j++) { if (j % 2 == 0) world.setBlockState(new BlockPos(x + i, y, z + j), state1); else world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } else { for (int j = 0; j < 5; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } } } public static void generate7(World world, int x, int y, int z) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } world.setBlockState(new BlockPos(x, y, z), state1); world.setBlockState(new BlockPos(x + 4, y, z), state1); world.setBlockState(new BlockPos(x, y, z + 4), state1); world.setBlockState(new BlockPos(x + 4, y, z + 4), state1); } public static void generate8(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate9(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); for (int k = 0; k < 5; k++) { world.setBlockToAir(new BlockPos(x + i, y, j + k)); } i++; j = z; for (int k = 0; k < 5; k++) { world.setBlockState(new BlockPos(x + i, y, j + k), state1); } i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); for (int k = 0; k < 5; k++) { world.setBlockToAir(new BlockPos(x + i, y, j + k)); } i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate10(World world, int x, int y, int z) { int i = 0; int j = z; for (int k = 0; k < 5; k++) { world.setBlockState(new BlockPos(x + i, y, z + k), state1); } i++; j = z; for (int k = 0; k < 4; k++) { world.setBlockToAir(new BlockPos(x + i, y, z + k)); } world.setBlockState(new BlockPos(x + i, y, z + 4), state1); i++; j = z; for (int k = 0; k < 3; k++) { world.setBlockState(new BlockPos(x + i, y, z + k), state1); } world.setBlockToAir(new BlockPos(x + i, y, z + 3)); world.setBlockState(new BlockPos(x + i, y, z + 4), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, z), state1); for (int k = 1; k < 4; k++) { world.setBlockToAir(new BlockPos(x + i, y, z + k)); } world.setBlockState(new BlockPos(x + i, y, z + 4), state1); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); for (int k = 2; k < 5; k++) { world.setBlockState(new BlockPos(x + i, y, z + k), state1); } } public static void generate11(World world, int x, int y, int z) { int i = 0; int j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; for (int k = 0; k < 5; k++) { world.setBlockToAir(new BlockPos(x + i, y, j + k)); } i++; j = z; world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j)); i++; j = z; world.setBlockState(new BlockPos(x + i, y, j++), state1); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockToAir(new BlockPos(x + i, y, j++)); world.setBlockState(new BlockPos(x + i, y, j), state1); } public static void generate12(World world, int x, int y, int z) { for (int i = 0; i < 5; i++) { world.setBlockState(new BlockPos(x + i, y, z), state1); world.setBlockState(new BlockPos(x, y, z + i), state1); } for (int i = 1; i < 5; i++) { world.setBlockToAir(new BlockPos(x + i, y, z + 1)); world.setBlockToAir(new BlockPos(x + 1, y, z + i)); } world.setBlockState(new BlockPos(x + 2, y, z + 2), state1); for (int i = 4; i > 1; i–) { world.setBlockState(new BlockPos(x + i, y, z + 4), state1); world.setBlockState(new BlockPos(x + 4, y, z + i), state1); } for (int i = 3; i > 1; i--) { world.setBlockToAir(new BlockPos(x + i, y, z + 3)); world.setBlockToAir(new BlockPos(x + 3, y, z + i)); } } public static void generate13(World world, int x, int y, int z) { int i = 0; for (i = 0; i < 5; i++) { world.setBlockState(new BlockPos(x + i, y, z), state1); } world.setBlockState(new BlockPos(x, y, z + 1), state1); for (i = 1; i < 5; i++) { world.setBlockToAir(new BlockPos(x + i, y, z + 1)); } world.setBlockState(new BlockPos(x, y, z + 2), state1); world.setBlockToAir(new BlockPos(x + 1, y, z + 2)); world.setBlockToAir(new BlockPos(x, y, z + 3)); world.setBlockToAir(new BlockPos(x + 1, y, z + 3)); for (i = 0; i < 3; i++) { world.setBlockState(new BlockPos(x + i, y, z + 4), state1); } world.setBlockState(new BlockPos(x + 2, y, z + 3), state1); world.setBlockState(new BlockPos(x + 2, y, z + 2), state1); for (; i < 5; i++) { world.setBlockState(new BlockPos(x + i, y, z + 2), state1); } world.setBlockState(new BlockPos(x + 4, y, z + 4), state1); world.setBlockToAir(new BlockPos(x + 4, y, z + 3)); world.setBlockToAir(new BlockPos(x + 3, y, z + 3)); world.setBlockToAir(new BlockPos(x + 3, y, z + 4)); } public static void generateAngle1(World world, int x, int y, int z, byte b) { for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } for (int i = 0; i < 5; i++) { world.setBlockState(new BlockPos(x + i, y, b == 0 || b == 1 ? z : z + 4), state1); world.setBlockState(new BlockPos(b == 0 || b == 2 ? x + 4 : x, y, z + i), state1); } for (int i = 1; i < 4; i++) { world.setBlockToAir(new BlockPos(b == 0 || b == 2 ? x : x + 4, y, z + i)); world.setBlockToAir(new BlockPos(x + i, y, b == 0 || b == 1 ? z + 4 : z)); } world.setBlockState(new BlockPos(b == 0 || b == 2 ? x : x + 4, y, b == 0 || b == 1 ? z + 4 : z), state1); } public static void generateHallBoss(World world, int x, int y, int z) { for (int i = 4; i < 18; i += 5) { world.setBlockState(new BlockPos(x + i, y, z), state1); world.setBlockState(new BlockPos(x + i, y, z + 19), state1); world.setBlockState(new BlockPos(x, y, z + i), state1); world.setBlockState(new BlockPos(x + 19, y, z + i), state1); world.setBlockState(new BlockPos(x + i + 1, y, z), state1); world.setBlockState(new BlockPos(x + i + 1, y, z + 19), state1); world.setBlockState(new BlockPos(x, y, z + i + 1), state1); world.setBlockState(new BlockPos(x + 19, y, z + i + 1), state1); } for (int i = 1; i < 19; i++) { for (int j = 1; j < 19; j++) { world.setBlockToAir(new BlockPos(x + i, y, z + j)); } } for (int i = 1; i < 19; i += 5) { for (int j = 0; j < 3; j++) { world.setBlockToAir(new BlockPos(x + i + j, y, z)); world.setBlockToAir(new BlockPos(x + i + j, y, z + 19)); world.setBlockToAir(new BlockPos(x, y, z + i + j)); world.setBlockToAir(new BlockPos(x + 19, y, z + i + j)); } } world.setBlockState(new BlockPos(x, y, z), state1); world.setBlockState(new BlockPos(x + 19, y, z), state1); world.setBlockState(new BlockPos(x, y, z + 19), state1); world.setBlockState(new BlockPos(x + 19, y, z + 19), state1); } public static void generateRandomRoom(World world, int x, int y, int z, Random rand) { generateFloorAndCeiling(world, x, y, z); int i = rand.nextInt(14); for (int j = 1; j < 5; j++) { switch (i) { case 0: generateCorridor(world, x, y + j, z); break; case 1: generate1(world, x, y + j, z); break; case 2: generate2(world, x, y + j, z); break; case 3: generate3(world, x, y + j, z); break; case 4: generate4(world, x, y + j, z); break; case 5: generate5(world, x, y + j, z); break; case 6: generate6(world, x, y + j, z); break; case 7: generate7(world, x, y + j, z); break; case 8: generate8(world, x, y + j, z); break; case 9: generate9(world, x, y + j, z); break; case 10: generate10(world, x, y + j, z); break; case 11: generate11(world, x, y + j, z); break; case 12: generate12(world, x, y + j, z); break; case 13: generate13(world, x, y + j, z); break; } } } public static void generateAll(World world, int x, int y, int z, Random rand) { int i = 4 + rand.nextInt(5); int j = i + 7; int i1 = 4 + rand.nextInt(5); int j1 = i1 + 7; for (int k = 0; k <= j; k++) { for (int k1 = 0; k1 <= j1; k1++) { if (k == i && k1 == i1) { ZombieDongeon.generateForHall(world, x + k * 5, y, z + k1 * 5); for (int l = 1; l < 5; l++) { ZombieDongeon.generateHallBoss(world, x + k * 5, y + l, z + k1 * 5); } BlockPos pos = new BlockPos(x + k * 5 + 9, y + 1, z + k1 * 5 + 1); world.setBlockState(pos, Blocks.chest.getDefaultState().withProperty(BlockChest.FACING, EnumFacing.SOUTH)); world.setBlockState(pos.east(), Blocks.chest.getDefaultState().withProperty(BlockChest.FACING, EnumFacing.SOUTH)); int r = 4 + EnumZombieDongeonRandomItem.rand.nextInt(4); for (int t = 0; t < r; t++) { ((TileEntityChest) world.getTileEntity(pos)).setInventorySlotContents(t, EnumZombieDongeonRandomItem.getRandomitem()); } EntityZombie zombie = new EntityZombie(world); zombie.setCustomNameTag("Patrick"); zombie.setLocationAndAngles(x + k * 5 + 10, y + 1, z + k1 * 5 + 10, 0.0F, 0.0F); world.spawnEntityInWorld(zombie); k1 += 3; } else { if ((k == 0 && k1 == 0) || (k == 0 && k1 == j1) || (k == j && k1 == j1) || (k == j && k1 == 0)) { ZombieDongeon.generateFloorAndCeiling(world, x + k * 5, y, z + k1 * 5); for (int l = 1; l < 5; l++) { ZombieDongeon.generateAngle1(world, x + k * 5, y + l, z + k1 * 5, (byte) ((k == j && k1 == 0) ? 0 : ((k == 0 && k1 == 0) ? 1 : ((k == j && k1 == j1) ? 2 : 3)))); } } else { if (k1 == i1 && (k > i && k < i + 4)) k1 += 4; ZombieDongeon.generateRandomRoom(world, x + k * 5, y, z + k1 * 5, rand); } if (rand.nextInt(10) == 0) { BlockPos pos = new BlockPos(x + k * 5 + rand.nextInt(5), y + 1, z + k1 * 5 + rand.nextInt(5)); world.setBlockState(pos, Blocks.mob_spawner.getDefaultState()); TileEntity tile = world.getTileEntity(pos); if (tile instanceof TileEntityMobSpawner) ((TileEntityMobSpawner) tile).getSpawnerBaseLogic().setEntityName("Zombie"); } } } } } public static enum EnumZombieDongeonRandomItem { DIAMOND(3, new ItemStack(Items.diamond), 1, 3), POTATO(10, new ItemStack(Items.potato), 10, 14), PICKAXE(3, new ItemStack(Items.diamond_pickaxe), 1, 0), APPLE(10, new ItemStack(Items.apple), 1, 10), IRON(5, new ItemStack(Items.iron_ingot), 1, 15), FLESH(5, new ItemStack(Items.rotten_flesh), 1, 15), CHESTPLATE(3, new ItemStack(Items.chainmail_chestplate), 1, 1), GOLD(5, new ItemStack(Items.gold_ingot), 1, 10), SADDLE(10, new ItemStack(Items.saddle), 1, 0), NAMETAG(10, new ItemStack(Items.name_tag), 1, 6); public static final Random rand = new Random(); private int weight; private ItemStack stack; private int minChance; private int maxChance; private EnumZombieDongeonRandomItem(int weight, ItemStack stack, int minChance, int maxChance) { this.weight = weight; this.stack = stack; this.minChance = minChance; this.maxChance = maxChance; } public static ItemStack getRandomitem() { int i = rand.nextInt(62); // 62 ici est la somme, tu peux la calculer ou bien la calculer toi-même avant de compiler int j = -1; int k = 0; while (k < i) { k += values()[++j].weight; } ItemStack stack = values()[j].stack.copy(); stack.stackSize = values()[j].maxChance > 0 ? values()[j].minChance + rand.nextInt(values()[j].maxChance) : 1; return stack; } } }
-
Je viens de découvrir un truc en lisant ton code, j’utilisais toujours
world.setBlockState(pos, Blocks.air.getDefaultState());
au lieu de
world.setBlockToAir(pos);
Je vais modifier tout ça
-
Je pense que ma plus belle optimisation… Ça à été quand j’ai découvert la fonction static… pas ‘public static quelque chose’ non non, juste static…
static { // do something }
Avant ça j’utilisais toujours une fonction qui checké chaque valeurs et initialisait au besoin avant d’utiliser une méthode static (surtout quand j’utilisais des Map ou des List qui sont supposés être partiellement pré-initialisé) ^^’
unknowClass.getConstructor().newInstance();
M’aurais permis de virer une grosse partie de ifelse, si seulement ce dernier avais été disponible en PHP --’
p.s : à partir de combien de jours/mois/années vous considérez cela comme un nécropost ici ?
-
@‘règlement’:
Ne déterrez pas un sujet datant d’un mois pour des raisons inutiles, n’ayant pas de lien directe avec le sujet, ou n’enrichissant pas la discussion du sujet.
On a passé le mois mais la raison n’est pas inutile donc c’est bon.
(raison inutile = poster un message presque vide du genre : “cool ce truc”, “ah moi aussi j’ai fait ça” ou répondre à une question / donner une solution à un problème alors que celui qui a posé la question n’est plus actif sur le forum).