From 8af7cddcb1060d9b9413e7ca9d4dbf6c16cb9480 Mon Sep 17 00:00:00 2001 From: Chicken-Bones Date: Wed, 17 Dec 2014 23:44:32 +1000 Subject: [PATCH] Moved the onChunkLoad hook to CCL's event handler Changed IRandomUpdateTick to be called from onWorldJoin --- build/build.properties | 4 +- .../multipart/IRandomUpdateTick.scala | 11 +++++- src/codechicken/multipart/TMultiPart.scala | 4 +- src/codechicken/multipart/TickScheduler.scala | 39 +++++-------------- src/codechicken/multipart/TileMultipart.scala | 11 ++---- .../multipart/handler/proxies.scala | 3 +- .../minecraft/RedstoneTorchPart.java | 12 ++++-- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/build/build.properties b/build/build.properties index ed4e5eb..b5d1add 100644 --- a/build/build.properties +++ b/build/build.properties @@ -1,4 +1,4 @@ mc_version=1.7.10 forge_version=10.13.1.1217 -ccl_version=1.1.1.88 -mod_version=1.1.0 +ccl_version=1.1.1.109 +mod_version=1.1.1 diff --git a/src/codechicken/multipart/IRandomUpdateTick.scala b/src/codechicken/multipart/IRandomUpdateTick.scala index f689dce..221d1e7 100644 --- a/src/codechicken/multipart/IRandomUpdateTick.scala +++ b/src/codechicken/multipart/IRandomUpdateTick.scala @@ -1,12 +1,19 @@ package codechicken.multipart /** - * Interface for parts with random update ticks. + * Interface for parts with random update ticks. Used in conjuction with TickScheduler */ -trait IRandomUpdateTick +trait TRandomUpdateTick extends TMultiPart { /** * Called on random update. Random ticks are between 800 and 1600 ticks from their last scheduled/random tick */ def randomUpdate() + + /** + * If implementing interface in java, be sure to implement this method yourself + */ + override def onWorldJoin() { + TickScheduler.loadRandomTick(this) + } } \ No newline at end of file diff --git a/src/codechicken/multipart/TMultiPart.scala b/src/codechicken/multipart/TMultiPart.scala index b4c3020..9761766 100644 --- a/src/codechicken/multipart/TMultiPart.scala +++ b/src/codechicken/multipart/TMultiPart.scala @@ -235,11 +235,11 @@ abstract class TMultiPart */ def onChunkUnload() = onWorldSeparate() /** - * Called when this part separates from the world (due to removal, chunk unload or other). Use this to sync with external data structures. + * Called when this part separates from the world (due to removal, chunk unload or other). Use this to sync with external data structures. Called on both client and server */ def onWorldSeparate(){} /** - * Called when this part separates from the world (due to removal, chunk unload or other). Use this to sync with external data structures. + * Called when this part joins the world (due to placement, chunkload or frame move etc). Use this to sync with external data structures. Called on both client and server */ def onWorldJoin(){} /** diff --git a/src/codechicken/multipart/TickScheduler.scala b/src/codechicken/multipart/TickScheduler.scala index 61b7487..5a11021 100644 --- a/src/codechicken/multipart/TickScheduler.scala +++ b/src/codechicken/multipart/TickScheduler.scala @@ -5,7 +5,7 @@ import codechicken.lib.world.WorldExtension import codechicken.lib.world.ChunkExtension import net.minecraft.world.chunk.Chunk import net.minecraft.world.World -import net.minecraft.nbt.{NBTBase, NBTTagCompound, NBTTagList, CompressedStreamTools} +import net.minecraft.nbt.{NBTTagCompound, NBTTagList, CompressedStreamTools} import scala.collection.mutable.ListBuffer import codechicken.lib.vec.BlockCoord import net.minecraft.world.ChunkCoordIntPair @@ -15,10 +15,8 @@ import java.io.DataOutputStream import net.minecraftforge.common.DimensionManager import java.io.File import java.io.FileOutputStream -import java.io.DataInputStream import java.io.FileInputStream import net.minecraft.world.storage.SaveHandler -import java.util.ArrayList /** * Used for scheduling delayed callbacks to parts. @@ -55,7 +53,7 @@ object TickScheduler extends WorldExtensionInstantiator .asInstanceOf[ChunkTickScheduler].scheduleTick(part, time, random) } - def loadRandom(part:TMultiPart) = scheduleTick(part, nextRandomTick, true) + def loadRandom(part:TRandomUpdateTick) = scheduleTick(part, nextRandomTick, true) override def preTick() { @@ -175,12 +173,12 @@ object TickScheduler extends WorldExtensionInstantiator { if(e.part.tile != null) { - if(e.random && e.part.isInstanceOf[IRandomUpdateTick]) - e.part.asInstanceOf[IRandomUpdateTick].randomUpdate() + if(e.random && e.part.isInstanceOf[TRandomUpdateTick]) + e.part.asInstanceOf[TRandomUpdateTick].randomUpdate() else e.part.scheduledTick() - if(e.part.isInstanceOf[IRandomUpdateTick]) + if(e.part.isInstanceOf[TRandomUpdateTick]) { e.time = schedTime+nextRandomTick e.random = true @@ -228,26 +226,6 @@ object TickScheduler extends WorldExtensionInstantiator } } - override def load() - { - val it = new ArrayList(chunk.chunkTileEntityMap.values).iterator - while(it.hasNext) - { - val t = it.next - if(t.isInstanceOf[TileMultipart]) - { - val tmp = t.asInstanceOf[TileMultipart] - tmp.onChunkLoad() - tmp.partList.foreach(p => - if(p.isInstanceOf[IRandomUpdateTick]) - world.scheduleTick(p, nextRandomTick, true)) - } - } - - if(!tickList.isEmpty) - world.tickChunks+=this - } - override def unload() { if(!tickList.isEmpty) @@ -256,8 +234,11 @@ object TickScheduler extends WorldExtensionInstantiator } def createChunkExtension(chunk:Chunk, world:WorldExtension):ChunkExtension = new ChunkTickScheduler(chunk, world.asInstanceOf[WorldTickScheduler]) - - private[multipart] def loadRandomTick(part:TMultiPart) + + /** + * Start random ticking for a part. Should be called from TMultiPart.onWorldJoin + */ + def loadRandomTick(part:TRandomUpdateTick) { getExtension(part.tile.getWorldObj).asInstanceOf[WorldTickScheduler].loadRandom(part) } diff --git a/src/codechicken/multipart/TileMultipart.scala b/src/codechicken/multipart/TileMultipart.scala index bf0ad83..d277542 100644 --- a/src/codechicken/multipart/TileMultipart.scala +++ b/src/codechicken/multipart/TileMultipart.scala @@ -20,8 +20,9 @@ import java.util.Collection import codechicken.lib.raytracer.ExtendedMOP import net.minecraft.util.Vec3 import java.lang.Iterable +import codechicken.lib.world.IChunkLoadTile -class TileMultipart extends TileEntity +class TileMultipart extends TileEntity with IChunkLoadTile { /** * List of parts in this tile space @@ -86,7 +87,7 @@ class TileMultipart extends TileEntity operate(_.onChunkUnload()) } - def onChunkLoad() + override def onChunkLoad() { operate(_.onChunkLoad()) } @@ -312,11 +313,7 @@ class TileMultipart extends TileEntity * Called when a part is added (placement) * Provided for trait overrides, do not call externally. */ - def partAdded(part:TMultiPart) - { - if(part.isInstanceOf[IRandomUpdateTick]) - TickScheduler.loadRandomTick(part) - } + def partAdded(part:TMultiPart){} /** * Removes part from this tile. Note that due to the operation sync, the part may not be removed until the call stack has been passed to all other parts in the space. diff --git a/src/codechicken/multipart/handler/proxies.scala b/src/codechicken/multipart/handler/proxies.scala index 9413893..0689f18 100644 --- a/src/codechicken/multipart/handler/proxies.scala +++ b/src/codechicken/multipart/handler/proxies.scala @@ -12,7 +12,7 @@ import cpw.mods.fml.relauncher.Side import codechicken.lib.packet.PacketCustom import net.minecraft.world.ChunkCoordIntPair import codechicken.lib.vec.BlockCoord -import codechicken.lib.world.WorldExtensionManager +import codechicken.lib.world.{TileChunkLoadHook, WorldExtensionManager} import cpw.mods.fml.common.FMLCommonHandler import cpw.mods.fml.common.registry.GameRegistry import net.minecraft.block.Block @@ -53,6 +53,7 @@ class MultipartProxy_serverImpl PacketCustom.assignHandshakeHandler(MultipartSPH.registryChannel, MultipartSPH) WorldExtensionManager.registerWorldExtension(TickScheduler) + TileChunkLoadHook.init() MultipartCompatiblity.load() } diff --git a/src/codechicken/multipart/minecraft/RedstoneTorchPart.java b/src/codechicken/multipart/minecraft/RedstoneTorchPart.java index b53365a..3db772d 100644 --- a/src/codechicken/multipart/minecraft/RedstoneTorchPart.java +++ b/src/codechicken/multipart/minecraft/RedstoneTorchPart.java @@ -2,6 +2,7 @@ package codechicken.multipart.minecraft; import java.util.Random; +import codechicken.multipart.TickScheduler; import net.minecraft.block.Block; import net.minecraft.block.BlockRedstoneTorch; import net.minecraft.init.Blocks; @@ -12,10 +13,10 @@ import net.minecraftforge.common.util.ForgeDirection; import codechicken.lib.vec.BlockCoord; import codechicken.lib.vec.Cuboid6; import codechicken.multipart.IFaceRedstonePart; -import codechicken.multipart.IRandomUpdateTick; +import codechicken.multipart.TRandomUpdateTick; import codechicken.multipart.RedstoneInteractions; -public class RedstoneTorchPart extends TorchPart implements IFaceRedstonePart, IRandomUpdateTick +public class RedstoneTorchPart extends TorchPart implements IFaceRedstonePart, TRandomUpdateTick { public static BlockRedstoneTorch torchActive = (BlockRedstoneTorch) Blocks.redstone_torch; public static BlockRedstoneTorch torchIdle = (BlockRedstoneTorch) Blocks.unlit_redstone_torch; @@ -142,7 +143,12 @@ public class RedstoneTorchPart extends TorchPart implements IFaceRedstonePart, I { scheduledTick(); } - + + @Override + public void onWorldJoin() { + TickScheduler.loadRandomTick(this); + } + private boolean burnedOut(boolean add) { long time = world().getTotalWorldTime();