From 65f58913a41e3f98bada4ea4e7807caccac67d15 Mon Sep 17 00:00:00 2001 From: shartte Date: Thu, 10 Sep 2020 22:34:16 +0200 Subject: [PATCH] Make players assume ownership of networks when they place security stations onto unsecured ones (#4714) * Fixes #4712: When a security station is placed onto an unsecured network, the placer assumes ownership of the entire network. Otherwise the contiguous network would not necessarily reconnect in the same way when the chunk is reloaded due to differing player-ids throughout the network. In addition, changes to the node's owner were not being persisted due to the host never being marked as dirty. * Fix formatting --- .../appeng/api/networking/GridNotification.java | 5 +++++ .../java/appeng/api/networking/IGridBlock.java | 3 ++- src/main/java/appeng/me/GridNode.java | 3 ++- src/main/java/appeng/me/cache/SecurityCache.java | 16 ++++++++++++++-- .../java/appeng/me/helpers/AENetworkProxy.java | 6 +++++- .../java/appeng/me/helpers/IGridProxyable.java | 3 +++ 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/api/java/appeng/api/networking/GridNotification.java b/src/api/java/appeng/api/networking/GridNotification.java index 48c7a29b0..b172f9dd1 100644 --- a/src/api/java/appeng/api/networking/GridNotification.java +++ b/src/api/java/appeng/api/networking/GridNotification.java @@ -28,4 +28,9 @@ public enum GridNotification { * the visible connections for this node have changed, useful for cable. */ CONNECTIONS_CHANGED, + + /** + * the owner of the grid node has changed, and the node needs to be re-saved + */ + OWNER_CHANGED } diff --git a/src/api/java/appeng/api/networking/IGridBlock.java b/src/api/java/appeng/api/networking/IGridBlock.java index 93fd3f37e..3a86fe2eb 100644 --- a/src/api/java/appeng/api/networking/IGridBlock.java +++ b/src/api/java/appeng/api/networking/IGridBlock.java @@ -87,7 +87,8 @@ public interface IGridBlock { AEColor getGridColor(); /** - * Notifies your IGridBlock that changes were made to your connections + * Called by the {@link IGridNode} to notify its {@link IGridBlock} about + * events. */ void onGridNotification(@Nonnull GridNotification notification); diff --git a/src/main/java/appeng/me/GridNode.java b/src/main/java/appeng/me/GridNode.java index 8a9699a04..2e0412502 100644 --- a/src/main/java/appeng/me/GridNode.java +++ b/src/main/java/appeng/me/GridNode.java @@ -327,8 +327,9 @@ public class GridNode implements IGridNode, IPathItem { @Override public void setPlayerID(final int playerID) { - if (playerID >= 0) { + if (playerID >= 0 && this.playerID != playerID) { this.playerID = playerID; + gridProxy.onGridNotification(GridNotification.OWNER_CHANGED); } } diff --git a/src/main/java/appeng/me/cache/SecurityCache.java b/src/main/java/appeng/me/cache/SecurityCache.java index 423372570..6a3bbfbe7 100644 --- a/src/main/java/appeng/me/cache/SecurityCache.java +++ b/src/main/java/appeng/me/cache/SecurityCache.java @@ -81,8 +81,16 @@ public class SecurityCache implements ISecurityGrid { private void updateSecurityKey() { final long lastCode = this.securityKey; + /** + * Placing a security station will propagate the security station's owner to all + * connected grid nodes to prevent the network from not reforming due to + * different owners later. + */ + int newOwner = -1; if (this.securityProvider.size() == 1) { - this.securityKey = this.securityProvider.get(0).getSecurityKey(); + ISecurityProvider securityProvider = this.securityProvider.get(0); + this.securityKey = securityProvider.getSecurityKey(); + newOwner = securityProvider.getOwner(); } else { this.securityKey = -1; } @@ -90,7 +98,11 @@ public class SecurityCache implements ISecurityGrid { if (lastCode != this.securityKey) { this.getGrid().postEvent(new MENetworkSecurityChange()); for (final IGridNode n : this.getGrid().getNodes()) { - ((GridNode) n).setLastSecurityKey(this.securityKey); + GridNode gridNode = (GridNode) n; + gridNode.setLastSecurityKey(this.securityKey); + if (gridNode.getPlayerID() != newOwner) { + gridNode.setPlayerID(newOwner); + } } } } diff --git a/src/main/java/appeng/me/helpers/AENetworkProxy.java b/src/main/java/appeng/me/helpers/AENetworkProxy.java index 57f7608a5..5617ef435 100644 --- a/src/main/java/appeng/me/helpers/AENetworkProxy.java +++ b/src/main/java/appeng/me/helpers/AENetworkProxy.java @@ -167,7 +167,6 @@ public class AENetworkProxy implements IGridBlock { * short cut! * * @return grid of node - * * @throws GridAccessException of node or grid is null */ public IGrid getGrid() throws GridAccessException { @@ -280,6 +279,11 @@ public class AENetworkProxy implements IGridBlock { @Override public void onGridNotification(final GridNotification notification) { + if (notification == GridNotification.OWNER_CHANGED) { + gp.saveChanges(); + return; + } + if (this.gp instanceof CablePart) { ((CablePart) this.gp).markForUpdate(); } diff --git a/src/main/java/appeng/me/helpers/IGridProxyable.java b/src/main/java/appeng/me/helpers/IGridProxyable.java index 4f47bdb91..30638a075 100644 --- a/src/main/java/appeng/me/helpers/IGridProxyable.java +++ b/src/main/java/appeng/me/helpers/IGridProxyable.java @@ -28,4 +28,7 @@ public interface IGridProxyable extends IGridHost { DimensionalCoord getLocation(); void gridChanged(); + + void saveChanges(); + }