From 16a3ea3875054cc2d0a00371a5c4987c234b8bb7 Mon Sep 17 00:00:00 2001 From: Chicken Bones Date: Thu, 6 Jun 2013 17:25:24 +1000 Subject: [PATCH] Added some more redstone utilities and made sure masks line up Fixed smoke particles on redstone torches (bugged in vanilla) Added functions for world and coordinates on parts to clean code Added more placement and world interaction events Properly distinguished converted tiles Fixed tick scheduler to remain in better sync with vanilla --- .../microblock/MicroblockPlacement.scala | 5 +- codechicken/multipart/IRedstonePart.scala | 127 ++++++++++++------ .../multipart/MultipartGenerator.scala | 18 +-- codechicken/multipart/TMultiPart.scala | 21 ++- codechicken/multipart/TickScheduler.scala | 99 ++++++++------ codechicken/multipart/TileMultipart.scala | 44 ++++-- .../multipart/handler/PacketHandlers.scala | 3 +- .../multipart/minecraft/ButtonPart.java | 8 +- .../multipart/minecraft/LeverPart.java | 6 +- .../multipart/minecraft/McMetaPart.java | 11 +- .../multipart/minecraft/McMultipartCPH.java | 33 +++++ .../multipart/minecraft/McMultipartSPH.java | 6 + .../multipart/minecraft/McSidedMetaPart.java | 7 +- .../minecraft/MinecraftMultipartMod.java | 5 + .../minecraft/RedstoneTorchPart.java | 36 ++--- .../multipart/minecraft/TorchPart.java | 8 +- 16 files changed, 273 insertions(+), 164 deletions(-) create mode 100644 codechicken/multipart/minecraft/McMultipartCPH.java diff --git a/codechicken/microblock/MicroblockPlacement.scala b/codechicken/microblock/MicroblockPlacement.scala index 5b051b6..ac53c64 100644 --- a/codechicken/microblock/MicroblockPlacement.scala +++ b/codechicken/microblock/MicroblockPlacement.scala @@ -74,7 +74,8 @@ class MicroblockPlacement(val world:World, val player:EntityPlayer, val hit:Movi val mcrClass = pp.microClass val pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ) val vhit = new Vector3(hit.hitVec).add(-pos.x, -pos.y, -pos.z) - val htile = TileMultipartObj.getOrConvertTile(world, pos) + val gtile = TileMultipartObj.getOrConvertTile2(world, pos) + val htile = gtile._1 val slot = pp.placementGrid.getHitSlot(vhit, hit.sideHit) val oslot = pp.opposite(slot, hit.sideHit) @@ -82,7 +83,7 @@ class MicroblockPlacement(val world:World, val player:EntityPlayer, val hit:Movi val useOppMod = pp.sneakOpposite(slot, hit.sideHit) val oppMod = player.isControlDown val internal = d < 1 && htile != null - val doExpand = internal && htile.loaded && !player.isSneaking && !(oppMod && useOppMod) && pp.expand(slot, hit.sideHit) + val doExpand = internal && !gtile._2 && !player.isSneaking && !(oppMod && useOppMod) && pp.expand(slot, hit.sideHit) val side = hit.sideHit def apply():ExecutablePlacement = diff --git a/codechicken/multipart/IRedstonePart.scala b/codechicken/multipart/IRedstonePart.scala index 1f863da..427b8bc 100644 --- a/codechicken/multipart/IRedstonePart.scala +++ b/codechicken/multipart/IRedstonePart.scala @@ -5,6 +5,7 @@ import net.minecraft.block.Block import net.minecraft.block.BlockRedstoneWire import net.minecraft.world.IBlockAccess import net.minecraft.util.Direction +import PartMap._ trait IRedstonePart { @@ -23,7 +24,6 @@ trait IFaceRedstonePart extends IRedstonePart trait TRedstoneTile extends TileMultipart with IRedstoneConnector { - import PartMap._ import RedstoneInteractions._ override def strongPowerLevel(side:Int):Int = @@ -48,7 +48,7 @@ trait TRedstoneTile extends TileMultipart with IRedstoneConnector var i = 0 while(i < 4) { - if(!blocksRedstone(edgeBetween(side, rotateSide(side, i)))) + if(!blocksRedstone(edgeBetween(side, rotateSide(side&6, i)))) m|=1< - if((connectionMask(p, side)&mask) > 0) - { - val l = p.asInstanceOf[IRedstonePart].weakPowerLevel(side) - if(l > max) max = l - }) - return max - } + weakPowerLevel(side, otherConnectionMask(worldObj, xCoord, yCoord, zCoord, side, true)) override def canConnectRedstone(side:Int):Boolean = { val vside = vanillaToSide(side) - return (getConnectionMap(vside) & otherConnectionMap(worldObj, xCoord, yCoord, zCoord, vside, false)) > 0 + return (getConnectionMask(vside) & otherConnectionMask(worldObj, xCoord, yCoord, zCoord, vside, false)) > 0 } - def getConnectionMap(side:Int):Int = + def getConnectionMask(side:Int):Int = { val mask = openConnections(side) var res = 0 @@ -85,6 +75,78 @@ trait TRedstoneTile extends TileMultipart with IRedstoneConnector return res } + def weakPowerLevel(side:Int, mask:Int):Int = + { + val tmask = openConnections(side)&mask + var max = 0 + partList.foreach(p => + if((connectionMask(p, side)&tmask) > 0) + { + val l = p.asInstanceOf[IRedstonePart].weakPowerLevel(side) + if(l > max) max = l + }) + return max + } +} + +/** + * All connection masks are a 5 bit map. + * The lowest 4 bits correspond to the connection toward the face specfied rotateSide(side&6, b) where b is the bit index from lowest to highest. + * Bit 5 corresponds to a connection opposite side. + */ +trait IRedstoneConnector +{ + def getConnectionMask(side:Int):Int + def weakPowerLevel(side:Int, mask:Int):Int +} + +trait IRedstoneConnectorBlock +{ + def getConnectionMask(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Int + def weakPowerLevel(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, mask:Int):Int +} + +object RedstoneInteractions +{ + import net.minecraft.util.Facing._ + + val vanillaSideMap = Array(-2, -1, 0, 2, 3, 1) + val sideVanillaMap = Array(1, 2, 5, 3, 4) + + def getWeakPowerTo(p:TMultiPart, side:Int):Int = + { + val tile = p.tile + return getWeakPowerTo(tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, side, + tile.asInstanceOf[TRedstoneTile].openConnections(side)&connectionMask(p, side)) + } + + def getWeakPowerTo(world:World, x:Int, y:Int, z:Int, side:Int, mask:Int):Int = + getWeakPower(world, x+offsetsXForSide(side), y+offsetsYForSide(side), z+offsetsZForSide(side), side^1, mask) + + def getWeakPower(world:World, x:Int, y:Int, z:Int, side:Int, mask:Int):Int = + { + val tile = world.getBlockTileEntity(x, y, z) + if(tile.isInstanceOf[IRedstoneConnector]) + return tile.asInstanceOf[IRedstoneConnector].weakPowerLevel(side, mask) + + val block = Block.blocksList(world.getBlockId(x, y, z)) + if(block == null) + return 0 + + if(block.isInstanceOf[IRedstoneConnectorBlock]) + return block.asInstanceOf[IRedstoneConnectorBlock].weakPowerLevel(world, x, y, z, side, mask) + + val vmask = vanillaConnectionMask(block, world, x, y, z, side, true) + if((vmask&mask) > 0) + return world.getIndirectPowerLevelTo(x, y, z, side^1) + return 0 + } + + def vanillaToSide(vside:Int) = sideVanillaMap(vside+1) + + def otherConnectionMask(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, power:Boolean):Int = + getConnectionMask(world, x+offsetsXForSide(side), y+offsetsYForSide(side), z+offsetsZForSide(side), side^1, power) + def connectionMask(p:TMultiPart, side:Int):Int = { if(p.isInstanceOf[IRedstonePart] && p.asInstanceOf[IRedstonePart].canConnectRedstone(side)) @@ -95,47 +157,28 @@ trait TRedstoneTile extends TileMultipart with IRedstoneConnector } return 0 } -} - -trait IRedstoneConnector -{ - def getConnectionMap(side:Int):Int -} - -trait IRedstoneConnectorBlock -{ - def getConnectionMap(x:Int, y:Int, z:Int, side:Int):Int -} - -object RedstoneInteractions -{ - import net.minecraft.util.Facing._ - - val vanillaSideMap = Array(-2, -1, 0, 2, 3, 1) - val sideVanillaMap = Array(1, 2, 5, 3, 4) - - def vanillaToSide(vside:Int) = sideVanillaMap(vside+1) - - def otherConnectionMap(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, power:Boolean):Int = - getConnectionMap(world, x+offsetsXForSide(side), y+offsetsYForSide(side), z+offsetsZForSide(side), side^1, power) /** * @param power If true, don't test canConnectRedstone on blocks, just get a power transmission mask rather than a visual connection */ - def getConnectionMap(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, power:Boolean):Int = + def getConnectionMask(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, power:Boolean):Int = { val tile = world.getBlockTileEntity(x, y, z) if(tile.isInstanceOf[IRedstoneConnector]) - return tile.asInstanceOf[IRedstoneConnector].getConnectionMap(side) + return tile.asInstanceOf[IRedstoneConnector].getConnectionMask(side) val block = Block.blocksList(world.getBlockId(x, y, z)) if(block == null) return 0 if(block.isInstanceOf[IRedstoneConnectorBlock]) - return block.asInstanceOf[IRedstoneConnectorBlock].getConnectionMap(x, y, z, side) + return block.asInstanceOf[IRedstoneConnectorBlock].getConnectionMask(world, x, y, z, side) - //start vanilla interactions + return vanillaConnectionMask(block, world, x, y, z, side, power) + } + + def vanillaConnectionMask(block:Block, world:IBlockAccess, x:Int, y:Int, z:Int, side:Int, power:Boolean):Int = + { if(side == 0)//vanilla doesn't handle side 0 { if(power) diff --git a/codechicken/multipart/MultipartGenerator.scala b/codechicken/multipart/MultipartGenerator.scala index d8f7f87..366247a 100644 --- a/codechicken/multipart/MultipartGenerator.scala +++ b/codechicken/multipart/MultipartGenerator.scala @@ -189,17 +189,18 @@ object MultipartGenerator */ private[multipart] def addPart(world:World, pos:BlockCoord, part:TMultiPart):TileMultipart = { - var tile = TileMultipartObj.getOrConvertTile(world, pos) - + var loaded = TileMultipartObj.getOrConvertTile2(world, pos) var partTraits = traitsForPart(part, world.isRemote) + val tile = loaded._1 var ntile = tile - if(tile != null) + if(ntile != null) { - val converted = !tile.loaded - if(converted)//perform client conversion + if(loaded._2)//perform client conversion { world.setBlock(pos.x, pos.y, pos.z, MultipartProxy.block.blockID, 0, 1) + world.setBlockTileEntity(pos.x, pos.y, pos.z, ntile) PacketCustom.sendToChunk(new Packet53BlockChange(pos.x, pos.y, pos.z, world), world, pos.x>>4, pos.z>>4) + ntile.partList(0).onConverted() ntile.writeAddPart(ntile.partList(0)) } @@ -211,13 +212,6 @@ object MultipartGenerator world.setBlockTileEntity(pos.x, pos.y, pos.z, ntile) ntile.loadFrom(tile) } - else if(converted) - { - ntile.validate() - world.setBlockTileEntity(pos.x, pos.y, pos.z, ntile) - } - if(converted) - ntile.partList(0).onConverted() } else { diff --git a/codechicken/multipart/TMultiPart.scala b/codechicken/multipart/TMultiPart.scala index 6257dc4..21f23e5 100644 --- a/codechicken/multipart/TMultiPart.scala +++ b/codechicken/multipart/TMultiPart.scala @@ -30,12 +30,14 @@ abstract class TMultiPart var tile:TileMultipart = _ def getTile():TileEntity = tile + def world() = tile.worldObj + def x = tile.xCoord + def y = tile.yCoord + def z = tile.zCoord def getType:String def bind(t:TileMultipart) { - if(tile != null && t == null) - onRemoved() tile = t } @@ -61,17 +63,24 @@ abstract class TMultiPart @SideOnly(Side.CLIENT) def drawBreaking(renderBlocks:RenderBlocks){} - def read(packet:MCDataInput) = readDesc(packet) + def read(packet:MCDataInput) + { + readDesc(packet) + tile.markRender() + } def readDesc(packet:MCDataInput){} def writeDesc(packet:MCDataOutput){} def save(tag:NBTTagCompound){} def load(tag:NBTTagCompound){} - def onWorldJoin(world:World){onPartChanged()} def onPartChanged(){} def onNeighbourChanged(){} - def onAdded(){} - def onRemoved(){} + def onAdded() = onWorldJoin() + def onRemoved() = onWorldSeparate() + def onChunkLoad() = onWorldJoin() + def onChunkUnload() = onWorldSeparate() + def onWorldSeparate(){} + def onWorldJoin(){} def onConverted() = onAdded() def doesTick = true diff --git a/codechicken/multipart/TickScheduler.scala b/codechicken/multipart/TickScheduler.scala index 758f202..1061368 100644 --- a/codechicken/multipart/TickScheduler.scala +++ b/codechicken/multipart/TickScheduler.scala @@ -15,62 +15,73 @@ import scala.collection.mutable.HashSet object TickScheduler extends WorldExtensionInstantiator { - class PartTickEntry(val part:TMultiPart, var ticks:Int, var random:Boolean) + class PartTickEntry(val part:TMultiPart, var time:Long, var random:Boolean) { def this(part:TMultiPart, ticks:Int) = this(part, ticks, false) } - class WorldTickScheduler(world:World) extends WorldExtension(world) + private class WorldTickScheduler(world$:World) extends WorldExtension(world$) { var tickChunks = HashSet[ChunkTickScheduler]() - - def scheduleTick(part:TMultiPart, ticks:Int) - { - getChunkExtension(part.tile.xCoord>>4, part.tile.zCoord>>4).asInstanceOf[ChunkTickScheduler].scheduleTick(part, ticks) - } - - override def preTick() - { - if(!tickChunks.isEmpty) - tickChunks = tickChunks.filter(_.processTicks()) - } - } - - def createWorldExtension(world:World) = new WorldTickScheduler(world) - - class ChunkTickScheduler(chunk:Chunk, world:WorldTickScheduler) extends ChunkExtension(chunk, world) - { - import codechicken.multipart.handler.MultipartProxy._ - - var tickList = ListBuffer[PartTickEntry]() private var processing = false private val pending = ListBuffer[PartTickEntry]() - def scheduleTick(part:TMultiPart, ticks:Int):Unit = scheduleTick(part, ticks, false) - def scheduleTick(part:TMultiPart, ticks:Int, random:Boolean) { if(processing) - { - pending+=new PartTickEntry(part, ticks, random) - return - } + pending+=new PartTickEntry(part, world.getWorldTime()+ticks, random) + else + _scheduleTick(part, world.getWorldTime()+ticks, random) + } + + def _scheduleTick(part:TMultiPart, time:Long, random:Boolean) + { + if(part.tile != null) + getChunkExtension(part.tile.xCoord>>4, part.tile.zCoord>>4) + .asInstanceOf[ChunkTickScheduler].scheduleTick(part, time, random) + } + + override def preTick() + { + processing = true + } + + override def postTick() + { + if(!tickChunks.isEmpty) + tickChunks = tickChunks.filter(_.processTicks()) + processing = false + pending.foreach(e => _scheduleTick(e.part, e.time, e.random)) + pending.clear() + } + } + + def createWorldExtension(world:World):WorldExtension = new WorldTickScheduler(world) + + private class ChunkTickScheduler(chunk$:Chunk, world:WorldTickScheduler) extends ChunkExtension(chunk$, world) + { + import codechicken.multipart.handler.MultipartProxy._ + + var tickList = ListBuffer[PartTickEntry]() + + def scheduleTick(part:TMultiPart, time:Long, random:Boolean) + { val it = tickList.iterator while(it.hasNext) { val e = it.next if(e.part == part) { - if(ticks > e.ticks && random == e.random || e.random && !random) + if(e.random && !random) { - e.ticks = ticks + e.time = time e.random = random } return } } - tickList+=new PartTickEntry(part, ticks, random) + tickList+=new PartTickEntry(part, time, random) if(tickList.size == 1) world.tickChunks+=this } @@ -79,18 +90,14 @@ object TickScheduler extends WorldExtensionInstantiator def processTicks():Boolean = { - processing = true tickList = tickList.filter(processTick(_)) - processing = false - pending.foreach(e => scheduleTick(e.part, e.ticks, e.random)) - pending.clear() return !tickList.isEmpty } def processTick(e:PartTickEntry):Boolean = { - e.ticks-=1 - if(e.ticks == 0) + val time = world.world.getWorldTime + if(e.time <= time) { if(e.part.tile != null) { @@ -101,7 +108,7 @@ object TickScheduler extends WorldExtensionInstantiator if(e.part.isInstanceOf[IRandomUpdateTick]) { - e.ticks = nextRandomTick + e.time = time+nextRandomTick e.random = true return true } @@ -121,7 +128,7 @@ object TickScheduler extends WorldExtensionInstantiator val tag = new NBTTagCompound tag.setShort("pos", indexInChunk(new BlockCoord(part.tile)).toShort) tag.setByte("i", part.tile.partList.indexOf(part).toByte) - tag.setInteger("ticks", e.ticks) + tag.setLong("time", e.time) tagList.appendTag(tag) } } @@ -143,7 +150,7 @@ object TickScheduler extends WorldExtensionInstantiator val pos = indexInChunk(cc, tag.getShort("pos")) val tile = chunk.chunkTileEntityMap.get(new ChunkPosition(pos.x, pos.y, pos.z)) if(tile.isInstanceOf[TileMultipart]) - tickList+=new PartTickEntry(tile.asInstanceOf[TileMultipart].partList(tag.getByte("i")), tag.getInteger("ticks")) + tickList+=new PartTickEntry(tile.asInstanceOf[TileMultipart].partList(tag.getByte("i")), tag.getLong("time"), false) } } @@ -154,9 +161,13 @@ object TickScheduler extends WorldExtensionInstantiator { val t = it.next if(t.isInstanceOf[TileMultipart]) - t.asInstanceOf[TileMultipart].partList.foreach(p => + { + val tmp = t.asInstanceOf[TileMultipart] + tmp.onChunkLoad() + tmp.partList.foreach(p => if(p.isInstanceOf[IRandomUpdateTick]) - scheduleTick(p, nextRandomTick, true)) + world.scheduleTick(p, nextRandomTick, true)) + } } if(!tickList.isEmpty) @@ -170,10 +181,10 @@ object TickScheduler extends WorldExtensionInstantiator } } - def createChunkExtension(chunk:Chunk, world:WorldExtension) = new ChunkTickScheduler(chunk, world.asInstanceOf[WorldTickScheduler]) + def createChunkExtension(chunk:Chunk, world:WorldExtension):ChunkExtension = new ChunkTickScheduler(chunk, world.asInstanceOf[WorldTickScheduler]) def scheduleTick(part:TMultiPart, ticks:Int) { - getExtension(part.tile.worldObj).asInstanceOf[WorldTickScheduler].scheduleTick(part, ticks) + getExtension(part.tile.worldObj).asInstanceOf[WorldTickScheduler].scheduleTick(part, ticks, false) } } \ No newline at end of file diff --git a/codechicken/multipart/TileMultipart.scala b/codechicken/multipart/TileMultipart.scala index ea4cfc6..9673475 100644 --- a/codechicken/multipart/TileMultipart.scala +++ b/codechicken/multipart/TileMultipart.scala @@ -68,13 +68,30 @@ trait TileMultipart extends TileEntity TileMultipartObj.finishOperation(this) } - override def setWorldObj(world:World) + override def onChunkUnload() { - super.setWorldObj(world) - partList.foreach(_.onWorldJoin(world)) + partList.foreach(_.onChunkUnload()) } - def loaded() = !isInvalid + def onChunkLoad() + { + partList.foreach(_.onChunkLoad()) + } + + override def validate() + { + val wasInvalid = isInvalid() + super.validate() + if(wasInvalid) + partList.foreach(_.onWorldJoin()) + } + + override def invalidate() + { + super.invalidate() + if(worldObj != null) + partList.foreach(_.onWorldSeparate()) + } def notifyPartChange() { @@ -226,7 +243,7 @@ trait TileMultipart extends TileEntity partMap(i) = null partRemoved(part, r) - part.bind(null) + part.onRemoved() if(partList.isEmpty) { @@ -487,11 +504,13 @@ object TileMultipartObj def queueAddition(world:World, pos:BlockCoord, part:TMultiPart):Boolean = operationSync.queueAddition(world, pos, part) - def getOrConvertTile(world:World, pos:BlockCoord):TileMultipart = + def getOrConvertTile(world:World, pos:BlockCoord) = getOrConvertTile2(world, pos)._1 + + def getOrConvertTile2(world:World, pos:BlockCoord):(TileMultipart, Boolean) = { val t = world.getBlockTileEntity(pos.x, pos.y, pos.z) if(t.isInstanceOf[TileMultipart]) - return t.asInstanceOf[TileMultipart] + return (t.asInstanceOf[TileMultipart], false) val id = world.getBlockId(pos.x, pos.y, pos.z) val p = MultiPartRegistry.convertBlock(world, pos, id) @@ -501,11 +520,18 @@ object TileMultipartObj t.xCoord = pos.x t.yCoord = pos.y t.zCoord = pos.z - t.invalidate() t.setWorldObj(world) t.addPart_do(p) - return t + return (t, true) } + return (null, false) + } + + def getTile(world:World, pos:BlockCoord):TileMultipart = + { + val t = world.getBlockTileEntity(pos.x, pos.y, pos.z) + if(t.isInstanceOf[TileMultipart]) + return t.asInstanceOf[TileMultipart] return null } diff --git a/codechicken/multipart/handler/PacketHandlers.scala b/codechicken/multipart/handler/PacketHandlers.scala index 8b9644c..b5c9626 100644 --- a/codechicken/multipart/handler/PacketHandlers.scala +++ b/codechicken/multipart/handler/PacketHandlers.scala @@ -62,12 +62,11 @@ object MultipartCPH extends IClientPacketHandler { val pos = packet.readCoord var i = packet.readUnsignedByte - do + while(i < 255) { TileMultipartObj.handlePacket(pos, world, i, packet) i = packet.readUnsignedByte } - while(i < 255) } } } diff --git a/codechicken/multipart/minecraft/ButtonPart.java b/codechicken/multipart/minecraft/ButtonPart.java index d1aed91..96100a2 100644 --- a/codechicken/multipart/minecraft/ButtonPart.java +++ b/codechicken/multipart/minecraft/ButtonPart.java @@ -100,7 +100,7 @@ public class ButtonPart extends McSidedMetaPart implements IRedstonePart if(pressed()) return false; - if(!getTile().worldObj.isRemote) + if(!world().isRemote) toggle(); return true; @@ -121,7 +121,7 @@ public class ButtonPart extends McSidedMetaPart implements IRedstonePart @Override public void onEntityCollision(Entity entity) { - if(!pressed() && !getTile().worldObj.isRemote && entity instanceof EntityArrow) + if(!pressed() && !world().isRemote && entity instanceof EntityArrow) updateState(); } @@ -129,7 +129,7 @@ public class ButtonPart extends McSidedMetaPart implements IRedstonePart { boolean in = !pressed(); meta^=8; - getTile().worldObj.playSoundEffect(getTile().xCoord + 0.5, getTile().yCoord + 0.5, getTile().zCoord + 0.5, "random.click", 0.3F, in ? 0.6F : 0.5F); + world().playSoundEffect(x() + 0.5, y() + 0.5, z() + 0.5, "random.click", 0.3F, in ? 0.6F : 0.5F); if(in) TickScheduler.scheduleTick(this, delay()); @@ -141,7 +141,7 @@ public class ButtonPart extends McSidedMetaPart implements IRedstonePart private void updateState() { - boolean arrows = sensitive() && !getTile().worldObj.getEntitiesWithinAABB(EntityArrow.class, + boolean arrows = sensitive() && !world().getEntitiesWithinAABB(EntityArrow.class, getBounds().add(Vector3.fromTileEntity(getTile())).toAABB()).isEmpty(); boolean pressed = pressed(); diff --git a/codechicken/multipart/minecraft/LeverPart.java b/codechicken/multipart/minecraft/LeverPart.java index d0c1eb4..74ae9b4 100644 --- a/codechicken/multipart/minecraft/LeverPart.java +++ b/codechicken/multipart/minecraft/LeverPart.java @@ -90,11 +90,11 @@ public class LeverPart extends McSidedMetaPart implements IRedstonePart @Override public boolean activate(EntityPlayer player, MovingObjectPosition part, ItemStack item) { - World world = getTile().worldObj; + World world = world(); if(world.isRemote) return true; - world.playSoundEffect(getTile().xCoord + 0.5, getTile().yCoord + 0.5, getTile().zCoord + 0.5, "random.click", 0.3F, !active() ? 0.6F : 0.5F); + world.playSoundEffect(x() + 0.5, y() + 0.5, z() + 0.5, "random.click", 0.3F, !active() ? 0.6F : 0.5F); meta ^= 8; sendDescUpdate(); tile().notifyPartChange(); @@ -108,7 +108,7 @@ public class LeverPart extends McSidedMetaPart implements IRedstonePart { IBlockAccess actual = renderBlocks.blockAccess; renderBlocks.blockAccess = new PartMetaAccess(this); - renderBlocks.renderBlockLever(lever, getTile().xCoord, getTile().yCoord, getTile().zCoord); + renderBlocks.renderBlockLever(lever, x(), y(), z()); renderBlocks.blockAccess = actual; } diff --git a/codechicken/multipart/minecraft/McMetaPart.java b/codechicken/multipart/minecraft/McMetaPart.java index 5382c54..de26741 100644 --- a/codechicken/multipart/minecraft/McMetaPart.java +++ b/codechicken/multipart/minecraft/McMetaPart.java @@ -46,17 +46,10 @@ public abstract class McMetaPart extends McBlockPart implements IPartMeta meta = packet.readByte(); } - @Override - public void read(MCDataInput packet) - { - readDesc(packet); - tile().markRender(); - } - @Override public World getWorld() { - return getTile().worldObj; + return world(); } @Override @@ -86,6 +79,6 @@ public abstract class McMetaPart extends McBlockPart implements IPartMeta @Override public void renderStatic(Vector3 pos, LazyLightMatrix olm, int pass) { - new RenderBlocks(new PartMetaAccess(this)).renderBlockByRenderType(getBlock(), getTile().xCoord, getTile().yCoord, getTile().zCoord); + new RenderBlocks(new PartMetaAccess(this)).renderBlockByRenderType(getBlock(), x(), y(), z()); } } diff --git a/codechicken/multipart/minecraft/McMultipartCPH.java b/codechicken/multipart/minecraft/McMultipartCPH.java new file mode 100644 index 0000000..636f52d --- /dev/null +++ b/codechicken/multipart/minecraft/McMultipartCPH.java @@ -0,0 +1,33 @@ +package codechicken.multipart.minecraft; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.NetClientHandler; +import net.minecraft.client.multiplayer.WorldClient; +import codechicken.core.packet.PacketCustom; +import codechicken.core.packet.PacketCustom.IClientPacketHandler; +import codechicken.core.vec.BlockCoord; + +public class McMultipartCPH implements IClientPacketHandler +{ + public static Object channel = MinecraftMultipartMod.instance; + + @Override + public void handlePacket(PacketCustom packet, NetClientHandler nethandler, Minecraft mc) + { + switch(packet.getType()) + { + case 1: + spawnBurnoutSmoke(mc.theWorld, packet.readCoord()); + break; + } + } + + private void spawnBurnoutSmoke(WorldClient world, BlockCoord pos) + { + for(int l = 0; l < 5; l++) + world.spawnParticle("smoke", + pos.x+world.rand.nextDouble()*0.6+0.2, + pos.y+world.rand.nextDouble()*0.6+0.2, + pos.z+world.rand.nextDouble()*0.6+0.2, 0, 0, 0); + } +} diff --git a/codechicken/multipart/minecraft/McMultipartSPH.java b/codechicken/multipart/minecraft/McMultipartSPH.java index 2a17ed2..2a97198 100644 --- a/codechicken/multipart/minecraft/McMultipartSPH.java +++ b/codechicken/multipart/minecraft/McMultipartSPH.java @@ -2,6 +2,7 @@ package codechicken.multipart.minecraft; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.NetServerHandler; +import net.minecraft.world.World; import codechicken.core.packet.PacketCustom; import codechicken.core.packet.PacketCustom.IServerPacketHandler; @@ -19,4 +20,9 @@ public class McMultipartSPH implements IServerPacketHandler break; } } + + public static void spawnBurnoutSmoke(World world, int x, int y, int z) + { + new PacketCustom(channel, 1).writeCoord(x, y, z).sendToChunk(world, x>>4, z>>4); + } } diff --git a/codechicken/multipart/minecraft/McSidedMetaPart.java b/codechicken/multipart/minecraft/McSidedMetaPart.java index eb23511..dd7f02d 100644 --- a/codechicken/multipart/minecraft/McSidedMetaPart.java +++ b/codechicken/multipart/minecraft/McSidedMetaPart.java @@ -1,7 +1,6 @@ package codechicken.multipart.minecraft; import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import codechicken.core.vec.BlockCoord; import codechicken.core.vec.Vector3; @@ -22,14 +21,14 @@ public abstract class McSidedMetaPart extends McMetaPart public void onNeighbourChanged() { - if(!getTile().worldObj.isRemote) + if(!world().isRemote) dropIfCantStay(); } public boolean dropIfCantStay() { BlockCoord pos = new BlockCoord(getTile()).offset(sideForMeta(meta)); - if(!getTile().worldObj.isBlockSolidOnSide(pos.x, pos.y, pos.z, ForgeDirection.getOrientation(sideForMeta(meta)^1))) + if(!world().isBlockSolidOnSide(pos.x, pos.y, pos.z, ForgeDirection.getOrientation(sideForMeta(meta)^1))) { drop(); return true; @@ -40,6 +39,6 @@ public abstract class McSidedMetaPart extends McMetaPart public void drop() { tile().remPart(this); - TileMultipartObj.dropItem(new ItemStack(getBlock()), getTile().worldObj, Vector3.fromTileEntityCenter(getTile())); + TileMultipartObj.dropItem(new ItemStack(getBlock()), world(), Vector3.fromTileEntityCenter(getTile())); } } diff --git a/codechicken/multipart/minecraft/MinecraftMultipartMod.java b/codechicken/multipart/minecraft/MinecraftMultipartMod.java index 03e09dd..de1be51 100644 --- a/codechicken/multipart/minecraft/MinecraftMultipartMod.java +++ b/codechicken/multipart/minecraft/MinecraftMultipartMod.java @@ -3,11 +3,13 @@ package codechicken.multipart.minecraft; import net.minecraftforge.common.MinecraftForge; import codechicken.core.packet.PacketCustom; import codechicken.core.packet.PacketCustom.CustomTinyPacketHandler; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.Mod.PreInit; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; +import cpw.mods.fml.relauncher.Side; @Mod(name="Minecraft Multipart", version="1.0.0.0", useMetadata = false, modid = "McMultipart", acceptedMinecraftVersions="[1.5.2]") @NetworkMod(clientSideRequired = true, serverSideRequired = true, tinyPacketHandler=CustomTinyPacketHandler.class) @@ -22,5 +24,8 @@ public class MinecraftMultipartMod new Content().init(); MinecraftForge.EVENT_BUS.register(new EventHandler()); PacketCustom.assignHandler(this, new McMultipartSPH()); + if(FMLCommonHandler.instance().getSide() == Side.CLIENT) + PacketCustom.assignHandler(this, new McMultipartCPH()); + } } diff --git a/codechicken/multipart/minecraft/RedstoneTorchPart.java b/codechicken/multipart/minecraft/RedstoneTorchPart.java index e74c089..35d2571 100644 --- a/codechicken/multipart/minecraft/RedstoneTorchPart.java +++ b/codechicken/multipart/minecraft/RedstoneTorchPart.java @@ -10,11 +10,9 @@ import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; import codechicken.core.vec.BlockCoord; import codechicken.core.vec.Cuboid6; -import codechicken.core.vec.Vector3; import codechicken.multipart.IRandomUpdateTick; import codechicken.multipart.IRedstonePart; - -import static net.minecraft.util.Facing.*; +import codechicken.multipart.RedstoneInteractions; public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRandomUpdateTick { @@ -89,13 +87,13 @@ public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRand if(!active()) return; - double d0 = getTile().xCoord + 0.5 + (random.nextFloat() - 0.5) * 0.2; - double d1 = getTile().yCoord + 0.7 + (random.nextFloat() - 0.5) * 0.2; - double d2 = getTile().zCoord + 0.5 + (random.nextFloat() - 0.5) * 0.2; + double d0 = x() + 0.5 + (random.nextFloat() - 0.5) * 0.2; + double d1 = y() + 0.7 + (random.nextFloat() - 0.5) * 0.2; + double d2 = z() + 0.5 + (random.nextFloat() - 0.5) * 0.2; double d3 = 0.22D; double d4 = 0.27D; - World world = getTile().worldObj; + World world = world(); int m = meta&7; if (m == 1) world.spawnParticle("reddust", d0 - d4, d1 + d3, d2, 0, 0, 0); @@ -118,7 +116,7 @@ public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRand @Override public void onNeighbourChanged() { - if(!getTile().worldObj.isRemote) + if(!world().isRemote) { if(!dropIfCantStay() && isBeingPowered() == active()) scheduleTick(2); @@ -128,8 +126,7 @@ public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRand public boolean isBeingPowered() { int side = metaSideMap[meta&7]; - return getTile().worldObj.getIndirectPowerOutput( - getTile().xCoord+offsetsXForSide[side], getTile().yCoord+offsetsYForSide[side], getTile().zCoord+offsetsZForSide[side], side); + return RedstoneInteractions.getWeakPowerTo(this, side) > 0; } @Override @@ -141,19 +138,18 @@ public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRand public void randomUpdate() { - if(!active() && !isBeingPowered()) - scheduledTick(); + scheduledTick(); } private boolean burnedOut(boolean add) { - long time = getTile().worldObj.getWorldTime(); + long time = world().getWorldTime(); while(burnout != null && burnout.timeout <= time) burnout = burnout.next; if(add) { - BurnoutEntry e = new BurnoutEntry(getTile().worldObj.getWorldTime()+60); + BurnoutEntry e = new BurnoutEntry(world().getWorldTime()+60); if(burnout == null) burnout = e; else @@ -184,16 +180,10 @@ public class RedstoneTorchPart extends TorchPart implements IRedstonePart, IRand { if(burnedOut(true)) { - Vector3 pos = Vector3.fromTileEntityCenter(getTile()); - World world = getTile().worldObj; + World world = world(); Random rand = world.rand; - world.playSoundEffect(pos.x+0.5, pos.y+0.5, pos.z+0.5, "random.fizz", 0.5F, 2.6F + (rand.nextFloat() - rand.nextFloat()) * 0.8F); - - for (int l = 0; l < 5; ++l) - getTile().worldObj.spawnParticle("smoke", - pos.x + rand.nextDouble() * 0.6 + 0.2, - pos.y + rand.nextDouble() * 0.6 + 0.2, - pos.z + rand.nextDouble() * 0.6 + 0.2, 0, 0, 0); + world.playSoundEffect(x()+0.5, y()+0.5, z()+0.5, "random.fizz", 0.5F, 2.6F + (rand.nextFloat() - rand.nextFloat()) * 0.8F); + McMultipartSPH.spawnBurnoutSmoke(world, x(), y(), z()); } } else if(burnedOut(false)) diff --git a/codechicken/multipart/minecraft/TorchPart.java b/codechicken/multipart/minecraft/TorchPart.java index ba27292..0a9be25 100644 --- a/codechicken/multipart/minecraft/TorchPart.java +++ b/codechicken/multipart/minecraft/TorchPart.java @@ -79,13 +79,13 @@ public class TorchPart extends McSidedMetaPart implements IRandomDisplayTick @Override public void randomDisplayTick(Random random) { - double d0 = getTile().xCoord + 0.5; - double d1 = getTile().yCoord + 0.7; - double d2 = getTile().zCoord + 0.5; + double d0 = x() + 0.5; + double d1 = y() + 0.7; + double d2 = z() + 0.5; double d3 = 0.22D; double d4 = 0.27D; - World world = getTile().worldObj; + World world = world(); if (meta == 1) { world.spawnParticle("smoke", d0 - d4, d1 + d3, d2, 0, 0, 0);