This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
_CR_'s profile picture

Getting peripherals from TileEntities at world loading

Started by _CR_, 17 June 2014 - 08:02 PM
_CR_ #1
Posted 17 June 2014 - 10:02 PM
I'm the developer of CRMod, that adds chunkloader modules for turtles. To load the chunks when the world loads I have to make a callback that from the forge's tickets loads the needed chunks: I get the ticket, the tickets has some coordinates and from those I get the location to load. I have to check for the existence of a turtle with the chunky module before loading the chunks and I also should store the ticket in the peripheral's object. The problem is that since CC1.6 getting to this object isn't easy, when I get the tile entity with world.getBlockTileEntity i get an object of class TileTurtleExpanded that isn't in the apis so I use reflection to get the method getAccess and obtain an ITurtleAccess. after doing so I should call getPeripheral, but it always return null (not only for my peripheral, also, for example, for the wireless modem). It only does this here in the callback, inside the update method of the peripheral it works fine. In CC1.58 it worked also in the callback. There are workarounds: using getUpgrade to verify there is the chunky module, loading the chunks and releasing the ticket, at the first update the turtle sees it hasn't a ticket and creates a new one. It works but it shouldn't, the doc of ForgeChunkManager.releaseTicket says releaseTicket should unload the chunks, if it really does it the update method wouldn't be called, so that's not a great solution.
Is there any way to get to the peripheral? and maybe getting to the ITurtleAccess in a better way?
_CR_ #2
Posted 18 June 2014 - 02:59 PM
There's also another problem. I need to unload the chunks when the turtle is destroyed, the only way I found is doing it in the detach method but it has two problems: the peripheral gets attached only when the player opens the terminal of the turtle and, more important, the detach methods gets called also when the world is shutting down and in this situation the unforceChunks method results in a null pointer exception, probably because the world is already partially closed. Any ideas about this? maybe a way to know if the world is shutting down?
_CR_ #3
Posted 19 June 2014 - 12:08 PM
I solved the last problem. I created a method in the main mod class that is executed at the event FMLServerStoppingEvent, it sets a boolean that says the server is stopping, the detach method doesn't unload the chunks if this boolean is set.
Also the module loads the chunks only if attached (and it gets attached the first time the player opens the terminal)
Edited on 19 June 2014 - 10:13 AM
_CR_ #4
Posted 19 June 2014 - 10:06 PM
I improved the workaround and eliminated the risks but I still have to use reflection to get to the ITurtleAccess objects
Engineer #5
Posted 24 June 2014 - 01:09 AM
I improved the workaround and eliminated the risks but I still have to use reflection to get to the ITurtleAccess objects
You never can really get to those without reflection outside CC API calls
theoriginalbit #6
Posted 24 June 2014 - 01:45 AM
You never can really get to those without reflection outside CC API calls
well the ITurtleAccess used to be provided in the call method for the upgrade.