Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

fix: regenerate addressess and update merge state when gate position changes #310

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,7 @@ public void updateMembersMergeStatus(World world, BlockPos basePos, EnumFacing b
for (BlockPos pos : getChevronBlocks())
updateMemberMergeStatus(world, pos, basePos, baseFacing, shouldBeMerged);
}

public void updateMembersBasePos(IBlockAccess blockAccess, BlockPos basePos, EnumFacing baseFacing) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ public void updateMembersBasePos(IBlockAccess blockAccess, BlockPos basePos, Enu
for (BlockPos pos : getChevronBlocks())
updateMemberBasePos(blockAccess, pos.rotate(FacingToRotation.get(baseFacing)).add(basePos), basePos, baseFacing);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public void setGateAddress(SymbolTypeEnum symbolType, StargateAddress stargateAd

markDirty();
}

public StargateAddressDynamic getDialedAddress() {
return dialedAddress;
}
Expand Down Expand Up @@ -661,28 +661,8 @@ public void onLoad() {
network = StargateNetwork.get(world);

targetPoint = new TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 512);
Random random = new Random(pos.hashCode() * 31 + world.provider.getDimension());

for (SymbolTypeEnum symbolType : SymbolTypeEnum.values()) {

StargatePos stargatePos;

if (gateAddressMap.get(symbolType) == null) {
StargateAddress address = new StargateAddress(symbolType);
address.generate(random);

stargatePos = new StargatePos(world.provider.getDimension(), pos, address);
network.addStargate(address, stargatePos);
gateAddressMap.put(symbolType, address);
// Aunis.info(address.toString());
}

else {
stargatePos = new StargatePos(world.provider.getDimension(), pos, gateAddressMap.get(symbolType));
}

gatePosMap.put(symbolType, stargatePos);
}
generateAddresses(false);

if (stargateState.engaged()) {
verifyConnection();
Expand All @@ -693,6 +673,21 @@ public void onLoad() {
AunisPacketHandler.INSTANCE.sendToServer(new StateUpdateRequestToServer(pos, StateTypeEnum.RENDERER_STATE));
}
}

public void generateAddresses(boolean reset) {
Random random = new Random(pos.hashCode() * 31 + world.provider.getDimension());

for (SymbolTypeEnum symbolType : SymbolTypeEnum.values()) {
StargateAddress address = getStargateAddress(symbolType);

if (gateAddressMap.get(symbolType) == null || reset) {
address = new StargateAddress(symbolType);
address.generate(random);
}

if (address != null) this.setGateAddress(symbolType, address);
}
}

private boolean addedToNetwork;

Expand Down Expand Up @@ -1031,6 +1026,16 @@ public final boolean isMerged() {
* @param facing Facing of the base block.
*/
public final void updateMergeState(boolean shouldBeMerged, EnumFacing facing) {
// If the gate has already been merged, there is no need to merge it again.
// However, the gate position might have changed in that case, so we should
// update the members base position.
if (this.isMerged == shouldBeMerged) {
if (shouldBeMerged) {
getMergeHelper().updateMembersBasePos(world, pos, facing);
}
return;
}

if (!shouldBeMerged) {
if (isMerged)
onGateBroken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ protected void onGateMerged() {
updateBeamers();
}

protected abstract boolean onGateMergeRequested();

// ------------------------------------------------------------------------
// Loading and ticking
Expand All @@ -176,16 +177,28 @@ public void onLoad() {
super.onLoad();

if (!world.isRemote) {
lastPos = pos;
updateBeamers();
updatePowerTier();
}
}

private BlockPos lastPos = BlockPos.ORIGIN;

@Override
public void update() {
super.update();

if (!world.isRemote) {
if (!lastPos.equals(pos)) {
lastPos = pos;
generateAddresses(!hasUpgrade(StargateClassicBaseTile.StargateUpgradeEnum.CHEVRON_UPGRADE));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if there's no chevron upgrade stargate will reset address after chunk will be unloaded and loaded again? Because lastPos field will be null. Custom stargate address will not work without chevron upgrade

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you recommend to check whether the tile has moved?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, does it actually matter? The address will stay the same, since the generator is using the position as seed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slava mentions custom Stargate addresses, so someone using commands won't be happy with those changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe save a boolean to NBT like addressOverriddenByCommand or just fixedAddress with a docstring indicating it handles address persistence when used with commands?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that command. I've added lastPos assignment in onLoad, and I think it works now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check that? With command and with Warp drive or other gate-moving mods?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that. I think now code inside !lastPos.equals(pos) condition will not be called at all. Because position will be set in onLoad method after TileEntity creation and it's not possible that this position will not be equal to the current gate position

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm thinking. onLoad executes before update and whole effort of making this mod WarpDrive compatible is worthless.


if (isMerged()) {
updateMergeState(onGateMergeRequested(), facing);
}
}

if (givePageTask != null) {
if (givePageTask.update(world.getTotalWorldTime())) {
givePageTask = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ protected void onGateMerged() {
markDirty();
}
}

@Override
protected boolean onGateMergeRequested() {
if (stargateSize != AunisConfig.stargateSize) {
StargateMilkyWayMergeHelper.INSTANCE.convertToPattern(world, pos, facing, stargateSize, AunisConfig.stargateSize);
stargateSize = AunisConfig.stargateSize;
}

return StargateMilkyWayMergeHelper.INSTANCE.checkBlocks(world, pos, facing);
}

@Override
public StargateAbstractMergeHelper getMergeHelper() {
Expand Down Expand Up @@ -302,28 +312,6 @@ public BlockPos getGateCenterPos() {
return pos.offset(EnumFacing.UP, 4);
}

private boolean firstTick = true;

@Override
public void update() {
super.update();

if (!world.isRemote) {
if (firstTick) {
firstTick = false;

// Doing this in onLoad causes ConcurrentModificationException
if (stargateSize != AunisConfig.stargateSize && isMerged()) {
StargateMilkyWayMergeHelper.INSTANCE.convertToPattern(world, pos, facing, stargateSize, AunisConfig.stargateSize);
updateMergeState(StargateMilkyWayMergeHelper.INSTANCE.checkBlocks(world, pos, facing), facing);

stargateSize = AunisConfig.stargateSize;
markDirty();
}
}
}
}

public static final EnumSet<BiomeOverlayEnum> SUPPORTED_OVERLAYS = EnumSet.of(
BiomeOverlayEnum.NORMAL,
BiomeOverlayEnum.FROST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ public StargateAbstractMergeHelper getMergeHelper() {
return StargateUniverseMergeHelper.INSTANCE;
}

@Override
protected boolean onGateMergeRequested() {
return StargateUniverseMergeHelper.INSTANCE.checkBlocks(world, pos, facing);
}

// --------------------------------------------------------------------------------
// Renderer states
Expand All @@ -386,4 +390,4 @@ public StargateUniverseRendererState getRendererStateClient() {
public int getSupportedCapacitors() {
return AunisConfig.powerConfig.universeCapacitors;
}
}
}