From f4bb53ec5ec7716a945fc98b5ed611ce5db53b02 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 30 Mar 2021 15:44:31 +0200 Subject: [PATCH] simplified aura generation code --- .../naturesaura/blocks/BlockProjectileGenerator.java | 8 ++------ .../blocks/tiles/TileEntityAnimalGenerator.java | 8 ++------ .../blocks/tiles/TileEntityAuraBloom.java | 3 +-- .../blocks/tiles/TileEntityChorusGenerator.java | 7 +------ .../blocks/tiles/TileEntityEndFlower.java | 6 +----- .../blocks/tiles/TileEntityFireworkGenerator.java | 8 +++----- .../blocks/tiles/TileEntityFlowerGenerator.java | 11 ++++------- .../naturesaura/blocks/tiles/TileEntityImpl.java | 12 ++++++++++-- .../blocks/tiles/TileEntityMossGenerator.java | 8 ++------ .../blocks/tiles/TileEntityOakGenerator.java | 7 ++----- .../blocks/tiles/TileEntityPotionGenerator.java | 7 ++----- .../blocks/tiles/TileEntitySlimeSplitGenerator.java | 7 ++----- 12 files changed, 32 insertions(+), 60 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/BlockProjectileGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/BlockProjectileGenerator.java index 1de6294a..75a87673 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/BlockProjectileGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/BlockProjectileGenerator.java @@ -88,13 +88,9 @@ public class BlockProjectileGenerator extends BlockContainerImpl implements ITES Integer amount = NaturesAuraAPI.PROJECTILE_GENERATIONS.get(entity.getType()); if (amount == null || amount <= 0) return; - if (!generator.canGenerateRightNow(35, amount)) + if (!generator.canGenerateRightNow(amount)) return; - - while (amount > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(entity.world, pos, 35, pos); - amount -= IAuraChunk.getAuraChunk(entity.world, spot).storeAura(spot, amount); - } + generator.generateAura(amount); PacketHandler.sendToAllAround(entity.world, pos, 32, new PacketParticles((float) entity.getPosX(), (float) entity.getPosY(), (float) entity.getPosZ(), PacketParticles.Type.PROJECTILE_GEN, pos.getX(), pos.getY(), pos.getZ())); diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAnimalGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAnimalGenerator.java index c30e09d9..ee23295a 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAnimalGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAnimalGenerator.java @@ -24,12 +24,8 @@ public class TileEntityAnimalGenerator extends TileEntityImpl implements ITickab return; int remain = this.amountToRelease; - if (this.canGenerateRightNow(35, remain)) { - while (remain > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); - remain -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, remain); - } - + if (this.canGenerateRightNow(remain)) { + this.generateAura(remain); PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.ANIMAL_GEN_CREATE)); } diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAuraBloom.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAuraBloom.java index 329df437..69639b5b 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAuraBloom.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityAuraBloom.java @@ -22,8 +22,7 @@ public class TileEntityAuraBloom extends TileEntityImpl implements ITickableTile public void tick() { if (this.world.isRemote || !this.justGenerated) return; - IAuraChunk chunk = IAuraChunk.getAuraChunk(this.world, this.pos); - chunk.storeAura(this.pos, 150000); + this.generateAura(150000); this.justGenerated = false; } diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityChorusGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityChorusGenerator.java index dca3f5e8..859478cf 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityChorusGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityChorusGenerator.java @@ -46,12 +46,7 @@ public class TileEntityChorusGenerator extends TileEntityImpl implements ITickab this.world.removeBlock(pos, false); this.world.playSound(null, this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5, SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT, SoundCategory.BLOCKS, 0.5F, 1F); - - int aura = this.auraPerBlock; - while (aura > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); - aura -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, aura); - } + this.generateAura(this.auraPerBlock); } @Override diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEndFlower.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEndFlower.java index 9b4a93a7..60487fe0 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEndFlower.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEndFlower.java @@ -78,11 +78,7 @@ public class TileEntityEndFlower extends TileEntityImpl implements ITickableTile } else { int toDrain = Math.min(5000, this.container.getStoredAura()); this.container.drainAura(toDrain, false); - - while (toDrain > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 30, this.pos); - toDrain -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, toDrain); - } + this.generateAura(toDrain); if (this.container.getStoredAura() <= 0) { this.world.setBlockState(this.pos, Blocks.DEAD_BUSH.getDefaultState()); diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFireworkGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFireworkGenerator.java index 8e4d070d..54400291 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFireworkGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFireworkGenerator.java @@ -98,7 +98,7 @@ public class TileEntityFireworkGenerator extends TileEntityImpl implements ITick if (generateFactor > 0) { int toAdd = MathHelper.ceil(generateFactor * 10000F); - if (this.canGenerateRightNow(35, toAdd)) { + if (this.canGenerateRightNow(toAdd)) { this.toRelease = toAdd; this.releaseTimer = 15 * flightTime + 40; } @@ -121,10 +121,8 @@ public class TileEntityFireworkGenerator extends TileEntityImpl implements ITick if (this.releaseTimer > 0) { this.releaseTimer--; if (this.releaseTimer <= 0) { - while (this.toRelease > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); - this.toRelease -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, this.toRelease); - } + this.generateAura(this.toRelease); + this.toRelease = 0; PacketHandler.sendToAllLoaded(this.world, this.pos, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.FLOWER_GEN_AURA_CREATION)); diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java index 48a0b9da..373a1a5f 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFlowerGenerator.java @@ -58,14 +58,11 @@ public class TileEntityFlowerGenerator extends TileEntityImpl implements ITickab int addAmount = 25000; int toAdd = Math.max(0, addAmount - curr.getValue() * 100); if (toAdd > 0) { - if (IAuraType.forWorld(this.world).isSimilar(NaturesAuraAPI.TYPE_OVERWORLD) && this.canGenerateRightNow(30, toAdd)) { - int remain = toAdd; - while (remain > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 30, this.pos); - remain -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, remain); - } - } else + if (IAuraType.forWorld(this.world).isSimilar(NaturesAuraAPI.TYPE_OVERWORLD) && this.canGenerateRightNow(toAdd)) { + this.generateAura(toAdd); + } else { toAdd = 0; + } } for (Map.Entry entry : this.consumedRecently.entrySet()) { diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityImpl.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityImpl.java index 3ab5d76c..0542a354 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityImpl.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityImpl.java @@ -14,6 +14,7 @@ import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.Direction; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.server.ServerWorld; import net.minecraftforge.common.capabilities.Capability; @@ -168,13 +169,13 @@ public class TileEntityImpl extends TileEntity { } } - public boolean canGenerateRightNow(int range, int toAdd) { + public boolean canGenerateRightNow(int toAdd) { if (this.wantsLimitRemover()) { BlockState below = this.world.getBlockState(this.pos.down()); if (below.getBlock() == ModBlocks.GENERATOR_LIMIT_REMOVER) return true; } - int aura = IAuraChunk.getAuraInArea(this.world, this.pos, range); + int aura = IAuraChunk.getAuraInArea(this.world, this.pos, 35); return aura + toAdd <= IAuraChunk.DEFAULT_AURA * 2; } @@ -182,6 +183,13 @@ public class TileEntityImpl extends TileEntity { return false; } + public void generateAura(int amount) { + while (amount > 0) { + BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); + amount -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, amount); + } + } + public enum SaveType { TILE, SYNC, diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityMossGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityMossGenerator.java index add2219e..465381fa 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityMossGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityMossGenerator.java @@ -51,12 +51,8 @@ public class TileEntityMossGenerator extends TileEntityImpl implements ITickable BlockState result = NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.inverse().get(state); int toAdd = 7000; - if (this.canGenerateRightNow(35, toAdd)) { - while (toAdd > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); - toAdd -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, toAdd); - } - + if (this.canGenerateRightNow(toAdd)) { + this.generateAura(toAdd); PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(offset.getX(), offset.getY(), offset.getZ(), PacketParticles.Type.MOSS_GENERATOR)); } diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityOakGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityOakGenerator.java index e144b677..7d14e1a5 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityOakGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityOakGenerator.java @@ -25,12 +25,9 @@ public class TileEntityOakGenerator extends TileEntityImpl implements ITickableT BlockPos pos = this.scheduledBigTrees.remove(); if (this.world.getBlockState(pos).getBlock().getTags().contains(BlockTags.LOGS.getName())) { int toAdd = 100000; - boolean canGen = this.canGenerateRightNow(25, toAdd); + boolean canGen = this.canGenerateRightNow(toAdd); if (canGen) - while (toAdd > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 25, this.pos); - toAdd -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, toAdd); - } + this.generateAura(toAdd); PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles( this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.OAK_GENERATOR, diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityPotionGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityPotionGenerator.java index ece54974..8001b9a6 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityPotionGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityPotionGenerator.java @@ -45,12 +45,9 @@ public class TileEntityPotionGenerator extends TileEntityImpl implements ITickab } int toAdd = (effect.getAmplifier() * 7 + 1) * (effect.getDuration() / 25) * 100; - boolean canGen = this.canGenerateRightNow(30, toAdd); + boolean canGen = this.canGenerateRightNow(toAdd); if (canGen) - while (toAdd > 0) { - BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 30, this.pos); - toAdd -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, toAdd); - } + this.generateAura(toAdd); PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles( this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.POTION_GEN, diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntitySlimeSplitGenerator.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntitySlimeSplitGenerator.java index dcf8d72f..491aa93d 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntitySlimeSplitGenerator.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntitySlimeSplitGenerator.java @@ -25,11 +25,8 @@ public class TileEntitySlimeSplitGenerator extends TileEntityImpl implements ITi return; if (this.generationTimer > 0) { int amount = this.amountToRelease * 10; - if (this.canGenerateRightNow(35, amount)) { - while (amount > 0) { - BlockPos pos = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos); - amount -= IAuraChunk.getAuraChunk(this.world, pos).storeAura(pos, amount); - } + if (this.canGenerateRightNow(amount)) { + this.generateAura(amount); PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.SLIME_SPLIT_GEN_CREATE, this.color)); } this.generationTimer -= 10;