Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -27,25 +27,25 @@

//ToDo Well, idk if getRingBlocks() should be relative... Maybe return absolute positions too. But not sure
public abstract class StargateAbstractMergeHelper {

/**
* @return {@link List} of {@link BlockPos} pointing to ring blocks. Positions are relative
*/
@Nonnull
public abstract List<BlockPos> getRingBlocks();

/**
* @return {@link List} of {@link BlockPos} pointing to chevron blocks. Positions are relative
*/
@Nonnull
public abstract List<BlockPos> getChevronBlocks();

private BlockPos topBlock = null;
private BlockPos topBlock = null;

/**
* For Classic gate returns top chevron position (relative).
* For Orlin's gate returns top ring position (no chevrons).
*
*
* @return Gate's top block.
*/
public BlockPos getTopBlock() {
Expand All @@ -59,139 +59,147 @@ public BlockPos getTopBlock() {

return topBlock;
}

/**
* @return {@link List} of {@link BlockPos} pointing to absent blocks of variant given. Positions are absolute.
*/
@Nonnull
public List<BlockPos> getAbsentBlockPositions(IBlockAccess world, BlockPos basePos, EnumFacing facing, EnumMemberVariant variant) {
List<BlockPos> blocks = null;

switch (variant) {
case CHEVRON:
blocks = getChevronBlocks();
break;

case RING:
blocks = getRingBlocks();
break;
}

return blocks.stream()
.map(pos -> pos.rotate(FacingToRotation.get(facing)).add(basePos))
.filter(pos -> !matchMember(world.getBlockState(pos)))
.collect(Collectors.toList());
.map(pos -> pos.rotate(FacingToRotation.get(facing)).add(basePos))
.filter(pos -> !matchMember(world.getBlockState(pos)))
.collect(Collectors.toList());
}

@Nullable
public abstract EnumMemberVariant getMemberVariantFromItemStack(ItemStack stack);

/**
* @return Max box where to search for the base.
*/
public abstract AunisAxisAlignedBB getBaseSearchBox();

/**
* @param state State of the block for the check.
*
* @return True if the {@link IBlockState} represents the Base block, false otherwise.
*/
public abstract boolean matchBase(IBlockState state);

/**
* @param state State of the block for the check.
*
* @return True if the {@link IBlockState} represents the Member block, false otherwise.
*/
public abstract boolean matchMember(IBlockState state);

/**
* @return Member block.
*/
public abstract StargateAbstractMemberBlock getMemberBlock();

/**
* Method searches for a {@link StargateMilkyWayBaseBlock} within {@link this#BASE_SEARCH_BOX}
* and returns it's {@link TileEntity}.
*
*
* @param blockAccess Usually {@link World}.
* @param memberPos Starting position.
* @param facing Facing of the member blocks.
* @param memberPos Starting position.
* @param facing Facing of the member blocks.
*
* @return {@link StargateMilkyWayBaseTile} if found, {@code null} otherwise.
*/
@Nullable
public StargateAbstractBaseTile findBaseTile(IBlockAccess blockAccess, BlockPos memberPos, EnumFacing facing) {
AunisAxisAlignedBB globalBox = getBaseSearchBox().rotate(facing).offset(memberPos);

for (MutableBlockPos pos : BlockPos.getAllInBoxMutable(globalBox.getMinBlockPos(), globalBox.getMaxBlockPos())) {
if (matchBase(blockAccess.getBlockState(pos))) {
return (StargateAbstractBaseTile) blockAccess.getTileEntity(pos.toImmutable());
}
}

return null;
}

/**
* Check the given {@link BlockPos} for the {@link StargateMilkyWayMemberBlock},
* it's correct variant and facing.
*
*
* @param blockAccess Usually {@link World}.
* @param pos {@link BlockPos} to be checked.
* @param facing Expected {@link EnumFacing}.
* @param variant Expected {@link EnumMemberVariant}.
* @param pos {@link BlockPos} to be checked.
* @param facing Expected {@link EnumFacing}.
* @param variant Expected {@link EnumMemberVariant}.
*
* @return {@code true} if the block matches given parameters, {@code false} otherwise.
*/
protected abstract boolean checkMemberBlock(IBlockAccess blockAccess, BlockPos pos, EnumFacing facing, EnumMemberVariant variant);

/**
* Called on block placement. Checks the found base block
* for other Stargate blocks and returns the result.
*
*
* @param blockAccess Usually {@link World}.
* @param basePos Found {@link StargateMilkyWayBaseBlock}.
* @param baseFacing Current base facing.
* @param basePos Found {@link StargateMilkyWayBaseBlock}.
* @param baseFacing Current base facing.
*
* @return {@code true} if the structure matches, {@code false} otherwise.
*/
public boolean checkBlocks(IBlockAccess blockAccess, BlockPos basePos, EnumFacing baseFacing) {
if (AunisConfig.debugConfig.checkGateMerge) {
public boolean checkBlocks(IBlockAccess blockAccess, BlockPos basePos, EnumFacing baseFacing) {
if (AunisConfig.debugConfig.checkGateMerge) {
for (BlockPos pos : getRingBlocks()) {
if (!checkMemberBlock(blockAccess, pos.rotate(FacingToRotation.get(baseFacing)).add(basePos), baseFacing, EnumMemberVariant.RING))
return false;
}

for (BlockPos pos : getChevronBlocks()) {
if (!checkMemberBlock(blockAccess, pos.rotate(FacingToRotation.get(baseFacing)).add(basePos), baseFacing, EnumMemberVariant.CHEVRON))
return false;
}
}

return true;
}

/**
* Updates merge status of the given block. Block is internally
* checked by {@link this#MEMBER_MATCHER}.
*
* @param world {@link World} instance.
* @param checkPos Position of the currently checked {@link StargateMilkyWayMemberBlock}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of the base block
*
* @param world {@link World} instance.
* @param checkPos Position of the currently checked {@link StargateMilkyWayMemberBlock}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of the base block
* @param shouldBeMerged {@code true} if the structure is merging, false otherwise.
*/
protected abstract void updateMemberMergeStatus(World world, BlockPos checkPos, BlockPos basePos, EnumFacing baseFacing, boolean shouldBeMerged);

/**
* Updates merge status of the Stargate.
*
* @param world {@link World} instance.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
*
* @param world {@link World} instance.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
* @param shouldBeMerged {@code true} if the structure is merging, false otherwise.
*/
public void updateMembersMergeStatus(World world, BlockPos basePos, EnumFacing baseFacing, boolean shouldBeMerged) {
for (BlockPos pos : getRingBlocks())
updateMemberMergeStatus(world, pos, basePos, baseFacing, shouldBeMerged);

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 @@ -16,72 +16,73 @@
import net.minecraft.world.World;

public abstract class StargateClassicMergeHelper extends StargateAbstractMergeHelper {

protected boolean checkMemberBlock(IBlockAccess blockAccess, BlockPos pos, EnumFacing facing, EnumMemberVariant variant) {
IBlockState state = blockAccess.getBlockState(pos);

return matchMember(state) &&
state.getValue(AunisProps.FACING_HORIZONTAL) == facing &&
state.getValue(AunisProps.MEMBER_VARIANT) == variant;
state.getValue(AunisProps.FACING_HORIZONTAL) == facing &&
state.getValue(AunisProps.MEMBER_VARIANT) == variant;
}

protected void updateMemberMergeStatus(World world, BlockPos checkPos, BlockPos basePos, EnumFacing baseFacing, boolean shouldBeMerged) {
checkPos = checkPos.rotate(FacingToRotation.get(baseFacing)).add(basePos);
IBlockState state = world.getBlockState(checkPos);
if (matchMember(state)) {

if (matchMember(state)) {
StargateClassicMemberTile memberTile = (StargateClassicMemberTile) world.getTileEntity(checkPos);

if ((shouldBeMerged && !memberTile.isMerged()) || (memberTile.isMerged() && memberTile.getBasePos().equals(basePos))) {

ItemStack camoStack = memberTile.getCamoItemStack();
if (camoStack != null) {
InventoryHelper.spawnItemStack(world, checkPos.getX(), checkPos.getY(), checkPos.getZ(), camoStack);
}

if (memberTile.getCamoState() != null) {
memberTile.setCamoState(null);
}

// This also sets merge status
memberTile.setBasePos(shouldBeMerged ? basePos : null);

world.setBlockState(checkPos, state.withProperty(AunisProps.RENDER_BLOCK, !shouldBeMerged), 3);
}
}
}

/**
* Updates the {@link StargateMilkyWayBaseBlock} position of the
* {@link StargateMilkyWayMemberTile}.
*
*
* @param blockAccess Usually {@link World}.
* @param pos Position of the currently updated {@link StargateMilkyWayMemberBlock}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
* @param pos Position of the currently updated {@link StargateMilkyWayMemberBlock}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
*/
private void updateMemberBasePos(IBlockAccess blockAccess, BlockPos pos, BlockPos basePos, EnumFacing baseFacing) {
IBlockState state = blockAccess.getBlockState(pos);

if (matchMember(state)) {
if (matchMember(state)) {
StargateClassicMemberTile memberTile = (StargateClassicMemberTile) blockAccess.getTileEntity(pos);

memberTile.setBasePos(basePos);
}
}

/**
* Updates all {@link StargateMilkyWayMemberTile} to contain
* correct {@link StargateMilkyWayBaseBlock} position.
*
*
* @param blockAccess Usually {@link World}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
* @param basePos Position of {@link StargateMilkyWayBaseBlock} the tiles should be linked to.
* @param baseFacing Facing of {@link StargateMilkyWayBaseBlock}.
*/
@Override
public void updateMembersBasePos(IBlockAccess blockAccess, BlockPos basePos, EnumFacing baseFacing) {
for (BlockPos pos : getRingBlocks())
updateMemberBasePos(blockAccess, pos.rotate(FacingToRotation.get(baseFacing)).add(basePos), basePos, baseFacing);

for (BlockPos pos : getChevronBlocks())
updateMemberBasePos(blockAccess, pos.rotate(FacingToRotation.get(baseFacing)).add(basePos), basePos, baseFacing);
}
Expand Down
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);
}

this.setGateAddress(symbolType, address);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Address might be null

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Also there is so much code stripped of this method compared to the original code from onLoad

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah beacause it was basically setGateAddress repeated.

}
}

private boolean addedToNetwork;

Expand Down Expand Up @@ -1043,6 +1038,15 @@ public final void updateMergeState(boolean shouldBeMerged, EnumFacing facing) {
else {
onGateMerged();
}

// If the gate has already been merged, there is no need to merge it again.
// However, when the gate position changes, the members base pos should be updated as well.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

when the gate position changes
But it will be updated every time when updateMergeState will be called with (shouldBeMerged && this.isMerged) == true. Should be fine anyway? :I
Also you can move this stuff higher probably (above if (!shouldBeMerged) condition, line 1029)

Copy link
Copy Markdown
Author

@sentialx sentialx May 17, 2021

Choose a reason for hiding this comment

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

But it will be updated every time when updateMergeState will be called with (shouldBeMerged && this.isMerged) == true. Should be fine anyway? :I

If that's the case, this means that the gate changed its position. There is no other code path that could trigger the if statement.

if (this.isMerged == shouldBeMerged) {
if (shouldBeMerged) {
getMergeHelper().updateMembersBasePos(world, pos, facing);
}
return;
}

this.isMerged = shouldBeMerged;
IBlockState actualState = world.getBlockState(pos);
Expand Down
Loading