From ea8f497575a7d463dffa84df3754e78e02930778 Mon Sep 17 00:00:00 2001 From: Vasco Lavos Date: Mon, 24 Mar 2014 15:37:43 +0000 Subject: [PATCH] The Mana Spreader now takes a list of all positions and blocks visited by the scan beam and checks against that before doing another simulation, this is a much needed performance change, hopefully server owners won't hate me anymore :) --- .../common/block/tile/TileSpreader.java | 66 ++++++++++--------- .../common/entity/EntityManaBurst.java | 43 ++++++++++++ 2 files changed, 78 insertions(+), 31 deletions(-) diff --git a/MODSRC/vazkii/botania/common/block/tile/TileSpreader.java b/MODSRC/vazkii/botania/common/block/tile/TileSpreader.java index 18d33cde8..6e98204c8 100644 --- a/MODSRC/vazkii/botania/common/block/tile/TileSpreader.java +++ b/MODSRC/vazkii/botania/common/block/tile/TileSpreader.java @@ -11,6 +11,8 @@ */ package vazkii.botania.common.block.tile; +import java.util.List; + import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.entity.RenderItem; @@ -40,6 +42,7 @@ import vazkii.botania.client.core.handler.HUDHandler; import vazkii.botania.common.block.ModBlocks; import vazkii.botania.common.core.helper.Vector3; import vazkii.botania.common.entity.EntityManaBurst; +import vazkii.botania.common.entity.EntityManaBurst.PositionProperties; import vazkii.botania.common.lib.LibBlockNames; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; @@ -59,9 +62,12 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector IManaReceiver receiver = null; + public boolean canShootBurst = true; public int lastBurstDeathTick = -1; public int burstParticleTick = 0; + List lastTentativeBurst; + @Override public boolean isFull() { return mana >= MAX_MANA; @@ -111,6 +117,9 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector redstone = true; } + if(needsNewBurstSimulation()) + checkForReceiver(); + if(!redstone) tryShootBurst(); } @@ -132,14 +141,6 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector if(cmp.hasKey(TAG_KNOWN_MANA)) knownMana = cmp.getInteger(TAG_KNOWN_MANA); - - if(worldObj != null && worldObj.isRemote) { - EntityManaBurst fakeBurst = getBurst(true); - TileEntity receiver = fakeBurst.getCollidedTile(true); - if(receiver != null && receiver instanceof IManaReceiver) - this.receiver = (IManaReceiver) receiver; - else this.receiver = null; - } } @Override @@ -187,47 +188,50 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector this.receiver = (IManaReceiver) receiver; else this.receiver = null; + checkForReceiver(); PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId); } } } - public boolean canShootBurst = true; + private boolean needsNewBurstSimulation() { + if(lastTentativeBurst == null) + return true; + + for(PositionProperties props : lastTentativeBurst) + if(!props.contentsEqual(worldObj)) + return true; + + return false; + } public void tryShootBurst() { - if(receiver == null) { - EntityManaBurst fakeBurst = getBurst(true); - TileEntity receiver = fakeBurst.getCollidedTile(true); - if(receiver != null && receiver instanceof IManaReceiver) - this.receiver = (IManaReceiver) receiver; - } - if(receiver != null) { - forceCheckReceiver(); - - if(receiver != null) { - if(canShootBurst && receiver.canRecieveManaFromBursts() && !receiver.isFull()) { - EntityManaBurst burst = getBurst(false); - if(burst != null) { - if(!worldObj.isRemote) { - mana -= burst.getStartingMana(); - worldObj.spawnEntityInWorld(burst); - } - - canShootBurst = false; - PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId); + if(canShootBurst && receiver.canRecieveManaFromBursts() && !receiver.isFull()) { + EntityManaBurst burst = getBurst(false); + if(burst != null) { + if(!worldObj.isRemote) { + mana -= burst.getStartingMana(); + worldObj.spawnEntityInWorld(burst); } + + canShootBurst = false; + PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId); } } } } - public void forceCheckReceiver() { + public void checkForReceiver() { + System.out.println("check"); EntityManaBurst fakeBurst = getBurst(true); + fakeBurst.setScanBeam(); TileEntity receiver = fakeBurst.getCollidedTile(true); + if(receiver != null && receiver instanceof IManaReceiver) this.receiver = (IManaReceiver) receiver; else this.receiver = null; + lastTentativeBurst = fakeBurst.propsList; } public EntityManaBurst getBurst(boolean fake) { @@ -362,7 +366,7 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector @Override public void onInventoryChanged() { - forceCheckReceiver(); + checkForReceiver(); PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId); } } diff --git a/MODSRC/vazkii/botania/common/entity/EntityManaBurst.java b/MODSRC/vazkii/botania/common/entity/EntityManaBurst.java index 43f40667e..fe4ff0012 100644 --- a/MODSRC/vazkii/botania/common/entity/EntityManaBurst.java +++ b/MODSRC/vazkii/botania/common/entity/EntityManaBurst.java @@ -63,6 +63,9 @@ public class EntityManaBurst extends EntityThrowable implements IManaBurst { List alreadyCollidedAt = new ArrayList(); boolean fullManaLastTick = true; + + boolean scanBeam = false; + public List propsList = new ArrayList(); public EntityManaBurst(World world) { super(world); @@ -356,6 +359,17 @@ public class EntityManaBurst extends EntityThrowable implements IManaBurst { setMotion(motionX, motionY, motionZ); fullManaLastTick = getMana() == getStartingMana(); + + if(scanBeam) { + PositionProperties props = new PositionProperties(this); + if(propsList.isEmpty()) + propsList.add(props); + else { + PositionProperties lastProps = propsList.get(propsList.size() - 1); + if(!props.coordsEqual(lastProps)) + propsList.add(props); + } + } } @Override @@ -553,6 +567,10 @@ public class EntityManaBurst extends EntityThrowable implements IManaBurst { public boolean isFake() { return fake; } + + public void setScanBeam() { + scanBeam = true; + } @Override public int getColor() { @@ -696,4 +714,29 @@ public class EntityManaBurst extends EntityThrowable implements IManaBurst { if(tile != null && tile instanceof TileSpreader) ((TileSpreader) tile).lastBurstDeathTick = ticksExisted; } + + public static class PositionProperties { + + public final ChunkCoordinates coords; + public final int id; int meta; + + public PositionProperties(Entity entity) { + int x = (int) entity.posX; + int y = (int) entity.posY; + int z = (int) entity.posZ; + coords = new ChunkCoordinates(x, y, z); + id = entity.worldObj.getBlockId(x, y, z); + meta = entity.worldObj.getBlockMetadata(x, y, z); + } + + public boolean coordsEqual(PositionProperties props) { + return coords.equals(props.coords); + } + + public boolean contentsEqual(World world) { + int id = world.getBlockId(coords.posX, coords.posY, coords.posZ); + int meta = world.getBlockMetadata(coords.posX, coords.posY, coords.posZ); + return id == this.id && meta == this.meta; + } + } }