From 98c6ed4469df8c1681fc0ec63e30baaa16fc83eb Mon Sep 17 00:00:00 2001 From: Alexander Anishin Date: Wed, 31 May 2023 10:09:36 +0300 Subject: [PATCH] Cleaned up Particle Generator code --- .../common/blocks/ParticleGenerator.java | 210 ++++++++---------- .../tileentities/TileParticleGenerator.java | 123 +++++----- 2 files changed, 153 insertions(+), 180 deletions(-) diff --git a/src/main/java/com/brandon3055/draconicevolution/common/blocks/ParticleGenerator.java b/src/main/java/com/brandon3055/draconicevolution/common/blocks/ParticleGenerator.java index d87d7d81..498669fb 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/blocks/ParticleGenerator.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/blocks/ParticleGenerator.java @@ -11,6 +11,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; import com.brandon3055.draconicevolution.DraconicEvolution; import com.brandon3055.draconicevolution.client.gui.GuiHandler; @@ -45,33 +46,34 @@ public class ParticleGenerator extends BlockDE { } @Override - public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float p_149727_7_, - float p_149727_8_, float p_149727_9_) { - if (world.getBlockMetadata(x, y, z) == 1) return false; - + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float subX, + float subY, float subZ) { + if (world.getBlockMetadata(x, y, z) == 1) { + return false; + } if (player.getHeldItem() != null && player.getHeldItem().getItem() == Items.paper) { TileEntity tile = world.getTileEntity(x, y, z); - TileParticleGenerator gen = (tile != null && tile instanceof TileParticleGenerator) - ? (TileParticleGenerator) tile - : null; - ItemStack stack = player.getHeldItem(); - if (gen != null && stack.hasTagCompound() && stack.getTagCompound().hasKey("particles_enabled")) { - gen.setBlockNBT(stack.getTagCompound()); - return true; + if (tile instanceof TileParticleGenerator) { + TileParticleGenerator generator = (TileParticleGenerator) tile; + ItemStack stack = player.getHeldItem(); + if (stack.hasTagCompound() && stack.getTagCompound().hasKey("particles_enabled")) { + generator.setBlockNBT(stack.getTagCompound()); + return true; + } } } if (player.isSneaking()) { - if (activateEnergyStorageCore(world, x, y, z, player)) return true; - TileEntity tile = world.getTileEntity(x, y, z); - TileParticleGenerator gen = (tile != null && tile instanceof TileParticleGenerator) - ? (TileParticleGenerator) tile - : null; - if (gen != null) { - gen.toggleInverted(); + if (activateEnergyStorageCore(world, x, y, z, player)) { + return true; } - } else + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileParticleGenerator) { + ((TileParticleGenerator) tile).toggleInverted(); + } + } else { FMLNetworkHandler.openGui(player, DraconicEvolution.instance, GuiHandler.GUIID_PARTICLEGEN, world, x, y, z); + } return true; } @@ -84,11 +86,8 @@ public class ParticleGenerator extends BlockDE { @Override public void onNeighborBlockChange(final World world, final int x, final int y, final int z, final Block block) { TileEntity tile = world.getTileEntity(x, y, z); - TileParticleGenerator gen = (tile != null && tile instanceof TileParticleGenerator) - ? (TileParticleGenerator) tile - : null; - if (gen != null) { - gen.signal = world.isBlockIndirectlyGettingPowered(x, y, z); + if (tile instanceof TileParticleGenerator) { + ((TileParticleGenerator) tile).signal = world.isBlockIndirectlyGettingPowered(x, y, z); world.markBlockForUpdate(x, y, z); } } @@ -100,42 +99,43 @@ public class ParticleGenerator extends BlockDE { @SideOnly(Side.CLIENT) @Override - public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int meta) { - if (world.isRemote) { - Random rand = world.rand; - float modifier = 0.1F; - float SCALE = 1; - double spawnX = x + 0.5D; - double spawnY = y + 0.5D; - double spawnZ = z + 0.5D; + public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int metadata) { + if (!world.isRemote) { + return; + } + Random rand = world.rand; + float modifier = 0.1F; + float scale = 1; + double spawnX = x + 0.5D; + double spawnY = y + 0.5D; + double spawnZ = z + 0.5D; - for (int i = 0; i < 100; i++) { - float MX = modifier - ((2f * modifier) * rand.nextFloat()); - float MY = modifier - ((2f * modifier) * rand.nextFloat()); - float MZ = modifier - ((2f * modifier) * rand.nextFloat()); + for (int i = 0; i < 100; i++) { + float motionX = modifier - ((2f * modifier) * rand.nextFloat()); + float motionY = modifier - ((2f * modifier) * rand.nextFloat()); + float motionZ = modifier - ((2f * modifier) * rand.nextFloat()); - { - ParticleCustom particle = new ParticleCustom( - world, - spawnX, - spawnY, - spawnZ, - MX, - MY, - MZ, - SCALE, - false, - 1); - particle.red = rand.nextInt(255); - particle.green = rand.nextInt(255); - particle.blue = rand.nextInt(255); - particle.maxAge = rand.nextInt(10); - particle.fadeTime = 20; - particle.fadeLength = 20; - particle.gravity = 0F; + { + ParticleCustom particle = new ParticleCustom( + world, + spawnX, + spawnY, + spawnZ, + motionX, + motionY, + motionZ, + scale, + false, + 1); + particle.red = rand.nextInt(255); + particle.green = rand.nextInt(255); + particle.blue = rand.nextInt(255); + particle.maxAge = rand.nextInt(10); + particle.fadeTime = 20; + particle.fadeLength = 20; + particle.gravity = 0F; - ParticleHandler.spawnCustomParticle(particle); - } + ParticleHandler.spawnCustomParticle(particle); } } } @@ -146,8 +146,7 @@ public class ParticleGenerator extends BlockDE { } @Override - public boolean shouldSideBeRendered(IBlockAccess p_149646_1_, int p_149646_2_, int p_149646_3_, int p_149646_4_, - int p_149646_5_) { + public boolean shouldSideBeRendered(IBlockAccess worldIn, int x, int y, int z, int side) { return false; } @@ -157,7 +156,7 @@ public class ParticleGenerator extends BlockDE { } @Override - public boolean hasTileEntity(int meta) { + public boolean hasTileEntity(int metadata) { return true; } @@ -167,71 +166,46 @@ public class ParticleGenerator extends BlockDE { } private boolean activateEnergyStorageCore(World world, int x, int y, int z, EntityPlayer player) { - for (int x1 = x - 11; x1 <= x + 11; x1++) { - if (world.getBlock(x1, y, z) == ModBlocks.energyStorageCore) { - TileEnergyStorageCore tile = (world.getTileEntity(x1, y, z) != null - && world.getTileEntity(x1, y, z) instanceof TileEnergyStorageCore) - ? (TileEnergyStorageCore) world.getTileEntity(x1, y, z) - : null; - if (tile != null && !tile.isOnline()) { - if (player.capabilities.isCreativeMode) { - if (!tile.creativeActivate()) { - if (world.isRemote) player.addChatComponentMessage( - new ChatComponentTranslation("msg.energyStorageCoreUTA.txt")); - return false; - } - } else { - if (!tile.tryActivate()) { - if (world.isRemote) player.addChatComponentMessage( - new ChatComponentTranslation("msg.energyStorageCoreUTA.txt")); - return false; - } - } - return true; - } - } - } - - for (int z1 = z - 11; z1 <= z + 11; z1++) { - if (world.getBlock(x, y, z1) == ModBlocks.energyStorageCore) { - TileEnergyStorageCore tile = (world.getTileEntity(x, y, z1) != null - && world.getTileEntity(x, y, z1) instanceof TileEnergyStorageCore) - ? (TileEnergyStorageCore) world.getTileEntity(x, y, z1) - : null; - if (tile != null && !tile.isOnline()) { - if (player.capabilities.isCreativeMode) { - if (!tile.creativeActivate()) { - if (world.isRemote) player.addChatComponentMessage( - new ChatComponentTranslation("msg.energyStorageCoreUTA.txt")); - return false; - } - } else { - if (!tile.tryActivate()) { - if (world.isRemote) player.addChatComponentMessage( - new ChatComponentTranslation("msg.energyStorageCoreUTA.txt")); - return false; - } - } - return true; + final ForgeDirection[] horizontalDirections = new ForgeDirection[] { ForgeDirection.EAST, ForgeDirection.SOUTH, + ForgeDirection.WEST, ForgeDirection.NORTH }; + for (ForgeDirection direction : horizontalDirections) { + for (int distance = 1; distance <= TileEnergyStorageCore.STABILIZER_SEARCH_DISTANCE; distance++) { + int coreX = x + direction.offsetX * distance; + int coreZ = z + direction.offsetZ * distance; + if (world.getBlock(coreX, y, coreZ) == ModBlocks.energyStorageCore) { + return tryActivateEnergyStorageCore(world, coreX, y, coreZ, player); } } } return false; } - @Override - public void breakBlock(World world, int x, int y, int z, Block p_149749_5_, int meta) { - if (meta == 1) { - TileEntity tile = world.getTileEntity(x, y, z); - TileParticleGenerator gen = (tile != null && tile instanceof TileParticleGenerator) - ? (TileParticleGenerator) tile - : null; - if (gen != null && gen.getMaster() != null) { - world.setBlockMetadataWithNotify(x, y, z, 0, 2); - // LogHelper.info("deActivate"); - gen.getMaster().isStructureStillValid(true); + private boolean tryActivateEnergyStorageCore(World world, int coreX, int coreY, int coreZ, EntityPlayer player) { + TileEntity tile = world.getTileEntity(coreX, coreY, coreZ); + if (tile instanceof TileEnergyStorageCore) { + TileEnergyStorageCore core = (TileEnergyStorageCore) tile; + if (core.tryActivate(player.capabilities.isCreativeMode)) { + return true; + } + if (world.isRemote) { + player.addChatComponentMessage(new ChatComponentTranslation("msg.energyStorageCoreUTA.txt")); } } - super.breakBlock(world, x, y, z, p_149749_5_, meta); + return false; + } + + @Override + public void breakBlock(World world, int x, int y, int z, Block blockBroken, int metadata) { + if (metadata == 1) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TileParticleGenerator) { + TileParticleGenerator generator = (TileParticleGenerator) tile; + if (generator.getMaster() != null) { + world.setBlockMetadataWithNotify(x, y, z, 0, 2); + generator.getMaster().isStructureStillValid(true); + } + } + } + super.breakBlock(world, x, y, z, blockBroken, metadata); } } diff --git a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TileParticleGenerator.java b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TileParticleGenerator.java index 85590e48..085096fc 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TileParticleGenerator.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TileParticleGenerator.java @@ -85,76 +85,81 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { @SideOnly(Side.CLIENT) @Override public void updateEntity() { - if (!worldObj.isRemote) return; + if (!worldObj.isRemote) { + return; + } rotation += 0.5F; if (stabalizerMode) { - spawnStabilizerParticle(); + return; } - if (stabalizerMode) return; - if (signal && !inverted) active = true; - else if (!signal && inverted) active = true; - else active = false; + active = signal != inverted; - if (tick >= spawn_rate && active && particles_enabled) { - tick = 0; + if (tick < spawn_rate || !active || !particles_enabled) { + tick++; + return; + } - Random rand = worldObj.rand; + tick = 0; + Random rand = worldObj.rand; - float MX = motion_x + (random_motion_x * rand.nextFloat()); - float MY = motion_y + (random_motion_y * rand.nextFloat()); - float MZ = motion_z + (random_motion_z * rand.nextFloat()); - float SCALE = scale + (random_scale * rand.nextFloat()); - double spawnX = xCoord + spawn_x + (random_spawn_x * rand.nextFloat()); - double spawnY = yCoord + spawn_y + (random_spawn_y * rand.nextFloat()); - double spawnZ = zCoord + spawn_z + (random_spawn_z * rand.nextFloat()); + float motionX = motion_x + (random_motion_x * rand.nextFloat()); + float motionY = motion_y + (random_motion_y * rand.nextFloat()); + float motionZ = motion_z + (random_motion_z * rand.nextFloat()); + float scale = this.scale + (random_scale * rand.nextFloat()); + double spawnX = xCoord + spawn_x + (random_spawn_x * rand.nextFloat()); + double spawnY = yCoord + spawn_y + (random_spawn_y * rand.nextFloat()); + double spawnZ = zCoord + spawn_z + (random_spawn_z * rand.nextFloat()); - ParticleCustom particle = new ParticleCustom( - worldObj, - spawnX + 0.5, - spawnY + 0.5, - spawnZ + 0.5, - MX, - MY, - MZ, - SCALE, - collide, - this.selected_particle); - particle.red = this.red + rand.nextInt(random_red + 1); - particle.green = this.green + rand.nextInt(random_green + 1); - particle.blue = this.blue + rand.nextInt(random_blue + 1); - particle.maxAge = this.life + rand.nextInt(random_life + 1); - particle.fadeTime = this.fade; - particle.fadeLength = this.fade; - particle.gravity = this.gravity; + ParticleCustom particle = new ParticleCustom( + worldObj, + spawnX + 0.5, + spawnY + 0.5, + spawnZ + 0.5, + motionX, + motionY, + motionZ, + scale, + collide, + this.selected_particle); + particle.red = this.red + rand.nextInt(random_red + 1); + particle.green = this.green + rand.nextInt(random_green + 1); + particle.blue = this.blue + rand.nextInt(random_blue + 1); + particle.maxAge = this.life + rand.nextInt(random_life + 1); + particle.fadeTime = this.fade; + particle.fadeLength = this.fade; + particle.gravity = this.gravity; - ParticleHandler.spawnCustomParticle(particle, 256); + ParticleHandler.spawnCustomParticle(particle, 256); - } else tick++; } @SideOnly(Side.CLIENT) private void spawnStabilizerParticle() { - if (getMaster() == null || worldObj.getTotalWorldTime() % (20) != 1) return; + TileEnergyStorageCore core = getMaster(); + if (core == null || worldObj.getTotalWorldTime() % 20 != 1) { + return; + } double x = xCoord + 0.5; double y = yCoord + 0.5; double z = zCoord + 0.5; - int direction = 0; + int direction; - if (getMaster().xCoord > xCoord) direction = 0; - else if (getMaster().xCoord < xCoord) direction = 1; - else if (getMaster().zCoord > zCoord) direction = 2; - else if (getMaster().zCoord < zCoord) direction = 3; + if (core.xCoord != xCoord) { + direction = core.xCoord > xCoord ? 0 : 1; + } else { + direction = core.zCoord > zCoord ? 2 : 3; + } Particles.EnergyBeamParticle particle = new Particles.EnergyBeamParticle( worldObj, x, y, z, - getMaster().xCoord + 0.5, - getMaster().zCoord + 0.5, + core.xCoord + 0.5, + core.zCoord + 0.5, direction, false); Particles.EnergyBeamParticle particle2 = new Particles.EnergyBeamParticle( @@ -162,8 +167,8 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { x, y, z, - getMaster().xCoord + 0.5, - getMaster().zCoord + 0.5, + core.xCoord + 0.5, + core.zCoord + 0.5, direction, true); ParticleHandler.spawnCustomParticle(particle, 60); @@ -205,21 +210,12 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { } public TileEnergyStorageCore getMaster() { - if (master == null) return null; - TileEnergyStorageCore tile = (worldObj.getTileEntity(master.getXCoord(), master.getYCoord(), master.getZCoord()) - != null - && worldObj.getTileEntity( - master.getXCoord(), - master.getYCoord(), - master.getZCoord()) instanceof TileEnergyStorageCore) - ? (TileEnergyStorageCore) worldObj - .getTileEntity(master.getXCoord(), master.getYCoord(), master.getZCoord()) - : null; - return tile; + TileEntity tile = master.getTileEntity(worldObj); + return tile instanceof TileEnergyStorageCore ? (TileEnergyStorageCore) tile : null; } public void setMaster(TileLocation master) { - this.master = master; + this.master = master != null ? master : new TileLocation(); } @Override @@ -345,8 +341,11 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { @Override public Object[] callMethod(String method, Object... args) { if (method.startsWith("setGeneratorProperty")) { - if (args.length != 2) return new Object[] { false }; - else if (!(args[0] instanceof String)) return new Object[] { false }; + if (args.length != 2) { + return new Object[] { false }; + } else if (!(args[0] instanceof String)) { + return new Object[] { false }; + } /* Particles */ if (args[0].equals("particles_enabled") && args[1] instanceof Boolean) { @@ -399,7 +398,7 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { fade = limit(((Double) args[1]).intValue(), 0, 100); } else if (args[0].equals("spawn_rate") && args[1] instanceof Double) { spawn_rate = limit(((Double) args[1]).intValue(), 1, 200); - } else if (args[0].equals("collide") && args[1] instanceof Double) { + } else if (args[0].equals("collide") && args[1] instanceof Boolean) { collide = (Boolean) args[1]; } else if (args[0].equals("selected_particle") && args[1] instanceof Double) { selected_particle = limit(((Double) args[1]).intValue(), 1, selected_max); @@ -434,7 +433,7 @@ public class TileParticleGenerator extends TileEntity implements IDEPeripheral { worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); return new Object[] { true }; } else if (method.startsWith("getGeneratorState")) { - Map map = new HashMap(); + Map map = new HashMap<>(); /* Particles */ map.put("particles_enabled", particles_enabled);