Skip to content

Commit

Permalink
Tracking previous dimension in nanomachine controller to robustly han…
Browse files Browse the repository at this point in the history
…dle dimension changes to update wireless network association, fixes #1535.

Added `api.Network.leaveWirelessNetwork(WirelessEndpoint endpoint, int dimension)` to API.
  • Loading branch information
fnuecke committed Nov 21, 2015
1 parent eb80740 commit f19dd57
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/li/cil/oc/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
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";

// ----------------------------------------------------------------------- //

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/li/cil/oc/api/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ public static void leaveWirelessNetwork(final WirelessEndpoint endpoint) {
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/>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/li/cil/oc/api/detail/NetworkAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
Expand Down
25 changes: 19 additions & 6 deletions src/main/scala/li/cil/oc/common/nanomachines/ControllerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/li/cil/oc/server/network/Network.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/li/cil/oc/server/network/WirelessNetwork.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f19dd57

Please sign in to comment.