Tracking previous dimension in nanomachine controller to robustly handle dimension changes to update wireless network association, fixes #1535.

Added `api.Network.leaveWirelessNetwork(WirelessEndpoint endpoint, int dimension)` to API.
This commit is contained in:
Florian Nücke 2015-11-21 12:47:05 +01:00
parent eb8074097e
commit f19dd57a0c
6 changed files with 60 additions and 7 deletions

View File

@ -18,7 +18,7 @@ import li.cil.oc.api.detail.NetworkAPI;
*/
public class API {
public static final String ID_OWNER = "OpenComputers|Core";
public static final String VERSION = "5.6.3";
public static final String VERSION = "5.6.4";
// ----------------------------------------------------------------------- //

View File

@ -114,6 +114,22 @@ public final class Network {
API.network.leaveWirelessNetwork(endpoint);
}
/**
* Removes a wireless endpoint from the wireless network of a specific dimension.
* <p/>
* This may be useful if the dimension of an endpoint changed and you can only
* react to that change (e.g. a player changing dimensions).
* <p/>
* Calling this for an endpoint that was not added before does nothing.
*
* @param endpoint the endpoint to remove from the wireless network.
* @param dimension the dimension with the wireless network to remove the endpoint from.
*/
public static void leaveWirelessNetwork(final WirelessEndpoint endpoint, final int dimension) {
if (API.network != null)
API.network.leaveWirelessNetwork(endpoint, dimension);
}
/**
* Sends a packet via the wireless network.
* <p/>

View File

@ -80,6 +80,19 @@ public interface NetworkAPI {
*/
void leaveWirelessNetwork(WirelessEndpoint endpoint);
/**
* Removes a wireless endpoint from the wireless network of a specific dimension.
* <p/>
* This may be useful if the dimension of an endpoint changed and you can only
* react to that change (e.g. a player changing dimensions).
* <p/>
* Calling this for an endpoint that was not added before does nothing.
*
* @param endpoint the endpoint to remove from the wireless network.
* @param dimension the dimension with the wireless network to remove the endpoint from.
*/
void leaveWirelessNetwork(WirelessEndpoint endpoint, int dimension);
/**
* Sends a packet via the wireless network.
* <p/>

View File

@ -5,6 +5,9 @@ import java.util.UUID
import com.google.common.base.Charsets
import com.google.common.base.Strings
import li.cil.oc.Constants
import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.nanomachines.Behavior
import li.cil.oc.api.nanomachines.Controller
import li.cil.oc.api.nanomachines.DisableReason
@ -13,13 +16,10 @@ import li.cil.oc.api.network.WirelessEndpoint
import li.cil.oc.common.item.data.NanomachineData
import li.cil.oc.integration.util.DamageSourceWithRandomCause
import li.cil.oc.server.PacketSender
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.util.BlockPosition
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.util.InventoryUtils
import li.cil.oc.util.PlayerUtils
import li.cil.oc.Constants
import li.cil.oc.Settings
import li.cil.oc.api
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.nbt.NBTTagCompound
@ -33,6 +33,7 @@ import scala.collection.mutable
class ControllerImpl(val player: EntityPlayer) extends Controller with WirelessEndpoint {
if (isServer) api.Network.joinWirelessNetwork(this)
var previousDimension = player.worldObj.provider.dimensionId
lazy val CommandRange = Settings.get.nanomachinesCommandRange * Settings.get.nanomachinesCommandRange
final val FullSyncInterval = 20 * 60
@ -232,11 +233,23 @@ class ControllerImpl(val player: EntityPlayer) extends Controller with WirelessE
if (commandDelay > 0) {
commandDelay -= 1
if (commandDelay == 0) {
queuedCommand.foreach(_())
queuedCommand.foreach(_ ())
queuedCommand = None
}
}
api.Network.updateWirelessNetwork(this)
// Handle dimension changes, the robust way (because when logging in,
// load is called while the world is still set to the overworld, but
// no dimension change event is fired if the player actually logged
// out in another dimension... yay)
if (player.worldObj.provider.dimensionId != previousDimension) {
api.Network.leaveWirelessNetwork(this, previousDimension)
api.Network.joinWirelessNetwork(this)
previousDimension = player.worldObj.provider.dimensionId
}
else {
api.Network.updateWirelessNetwork(this)
}
}
var hasPower = getLocalBuffer > 0 || Settings.get.ignorePower

View File

@ -527,6 +527,10 @@ object Network extends api.detail.NetworkAPI {
WirelessNetwork.remove(endpoint)
}
override def leaveWirelessNetwork(endpoint: WirelessEndpoint, dimension: Int) {
WirelessNetwork.remove(endpoint, dimension)
}
// ----------------------------------------------------------------------- //
override def sendWirelessPacket(source: WirelessEndpoint, strength: Double, packet: network.Packet) {

View File

@ -59,6 +59,13 @@ object WirelessNetwork {
}
}
def remove(endpoint: WirelessEndpoint, dimension: Int) = {
dimensions.get(dimension) match {
case Some(set) => set.remove(endpoint)
case _ => false
}
}
def remove(endpoint: WirelessEndpoint) = {
dimensions.get(dimension(endpoint)) match {
case Some(set) => set.remove(endpoint)