From 686789acfd21d41994ce96110814e2492ebea5fd Mon Sep 17 00:00:00 2001 From: Chicken Bones Date: Wed, 4 Sep 2013 12:56:11 +1000 Subject: [PATCH] Added neighbor tile change events to multiparts Put an operation sync around the onMoved event --- codechicken/multipart/BlockMultipart.scala | 12 ++++- .../multipart/INeighborTileChange.scala | 7 +++ .../multipart/MultipartGenerator.scala | 3 +- codechicken/multipart/TileMultipart.scala | 12 ++++- codechicken/multipart/handler/Proxies.scala | 1 + .../scalatraits/TTileChangeTile.scala | 44 +++++++++++++++++++ 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 codechicken/multipart/INeighborTileChange.scala create mode 100644 codechicken/multipart/scalatraits/TTileChangeTile.scala diff --git a/codechicken/multipart/BlockMultipart.scala b/codechicken/multipart/BlockMultipart.scala index e233225..6ae64d9 100644 --- a/codechicken/multipart/BlockMultipart.scala +++ b/codechicken/multipart/BlockMultipart.scala @@ -28,6 +28,7 @@ import codechicken.lib.render.TextureUtils import net.minecraft.world.IBlockAccess import codechicken.lib.raytracer.ExtendedMOP import scala.collection.JavaConversions._ +import codechicken.multipart.scalatraits.TTileChangeTile object BlockMultipart { @@ -76,7 +77,7 @@ trait BlockMultipart extends Block { val tile = getTile(world, x, y, z) if(tile != null) - tile.onNeighborBlockChange(world, x, y, z, id) + tile.onNeighborBlockChange() } override def collisionRayTrace(world:World, x:Int, y:Int, z:Int, start:Vec3, end:Vec3):MovingObjectPosition = @@ -277,6 +278,15 @@ trait BlockMultipart extends Block tile.onEntityCollision(entity) } + override def onNeighborTileChange(world:World, x:Int, y:Int, z:Int, tileX:Int, tileY:Int, tileZ:Int) + { + val tile = getTile(world, x, y, z) + if(tile != null) + tile.onNeighborTileChange(tileX, tileY, tileZ) + } + + override def weakTileChanges() = true + override def canProvidePower = true } diff --git a/codechicken/multipart/INeighborTileChange.scala b/codechicken/multipart/INeighborTileChange.scala new file mode 100644 index 0000000..5805cb7 --- /dev/null +++ b/codechicken/multipart/INeighborTileChange.scala @@ -0,0 +1,7 @@ +package codechicken.multipart + +trait INeighborTileChange { + def weakTileChanges():Boolean + + def onNeighborTileChanged(side:Int, weak:Boolean) +} \ No newline at end of file diff --git a/codechicken/multipart/MultipartGenerator.scala b/codechicken/multipart/MultipartGenerator.scala index 83f9f0b..e4c62b2 100644 --- a/codechicken/multipart/MultipartGenerator.scala +++ b/codechicken/multipart/MultipartGenerator.scala @@ -41,7 +41,8 @@ object MultipartGenerator return superClasses } - ret = heirachy(part.getClass).flatMap(c => interfaceTraitMap_c.get(c.getName)).distinct + val interfaceTraitMap = if(client) interfaceTraitMap_c else interfaceTraitMap_s + ret = heirachy(part.getClass).flatMap(c => interfaceTraitMap.get(c.getName)).distinct if(client) partTraitMap_c = partTraitMap_c+(part.getClass -> ret) else diff --git a/codechicken/multipart/TileMultipart.scala b/codechicken/multipart/TileMultipart.scala index 2600b1d..c631823 100644 --- a/codechicken/multipart/TileMultipart.scala +++ b/codechicken/multipart/TileMultipart.scala @@ -106,8 +106,11 @@ class TileMultipart extends TileEntity { val wasInvalid = isInvalid super.validate() - if(wasInvalid) + if(wasInvalid) { + TileMultipart.startOperation(this) partList.foreach(_.onMoved()) + TileMultipart.finishOperation(this) + } } private[multipart] def prepareSwap() @@ -163,13 +166,18 @@ class TileMultipart extends TileEntity TileMultipart.finishOperation(this) } - def onNeighborBlockChange(world:World, x:Int, y:Int, z:Int, id:Int) + def onNeighborBlockChange() { TileMultipart.startOperation(this) partList.foreach(_.onNeighborChanged()) TileMultipart.finishOperation(this) } + /** + * Blank implementation, overriden by TTileChangeTile + */ + def onNeighborTileChange(tileX:Int, tileY:Int, tileZ:Int) {} + def getLightValue() = partList.foldLeft(0)((l, p) => Math.max(l, p.getLightValue)) def markDirty() diff --git a/codechicken/multipart/handler/Proxies.scala b/codechicken/multipart/handler/Proxies.scala index 9d89f91..85e00c4 100644 --- a/codechicken/multipart/handler/Proxies.scala +++ b/codechicken/multipart/handler/Proxies.scala @@ -40,6 +40,7 @@ class MultipartProxy_serverImpl MultipartGenerator.registerTrait("codechicken.multipart.JPartialOcclusion", "codechicken.multipart.scalatraits.TPartialOcclusionTile") MultipartGenerator.registerTrait("codechicken.multipart.IRedstonePart", "codechicken.multipart.scalatraits.TRedstoneTile") MultipartGenerator.registerTrait("codechicken.multipart.IRandomDisplayTick", "codechicken.multipart.scalatraits.TRandomDisplayTickTile", null) + MultipartGenerator.registerTrait("codechicken.multipart.INeighborTileChange", null, "codechicken.multipart.scalatraits.TTileChangeTile") } def postInit() diff --git a/codechicken/multipart/scalatraits/TTileChangeTile.scala b/codechicken/multipart/scalatraits/TTileChangeTile.scala new file mode 100644 index 0000000..ec08d02 --- /dev/null +++ b/codechicken/multipart/scalatraits/TTileChangeTile.scala @@ -0,0 +1,44 @@ +package codechicken.multipart.scalatraits + +import codechicken.multipart.TMultiPart +import codechicken.multipart.INeighborTileChange +import codechicken.multipart.TileMultipart +import codechicken.lib.vec.BlockCoord + +trait TTileChangeTile extends TileMultipart { + var weakTileChanges = false + + override def partAdded(part:TMultiPart) + { + super.partAdded(part) + if(part.isInstanceOf[INeighborTileChange]) + weakTileChanges|=part.asInstanceOf[INeighborTileChange].weakTileChanges + } + + override def partRemoved(part:TMultiPart, p:Int) { + super.partRemoved(part, p) + weakTileChanges = partList.find{p => + p.isInstanceOf[INeighborTileChange] && p.asInstanceOf[INeighborTileChange].weakTileChanges + }.isDefined + } + + override def onNeighborTileChange(tileX:Int, tileY:Int, tileZ:Int) + { + super.onNeighborTileChange(tileX, tileY, tileZ) + val offset = new BlockCoord(tileX, tileY, tileZ).sub(xCoord, yCoord, zCoord) + val diff = offset.absSum + val side = offset.toSide + + if(side < 0 || diff <= 0 || diff > 2) + return + + val weak = diff == 2 + if(weak && !weakTileChanges) + return + + TileMultipart.startOperation(this) + for(p@(_p: INeighborTileChange) <- partList.iterator) + p.onNeighborTileChanged(side, weak) + TileMultipart.finishOperation(this) + } +} \ No newline at end of file