Skip to content

Commit

Permalink
Move multiblock helpers to common
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 6, 2024
1 parent 65aa4bc commit 394e3be
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.cyclops.cyclopscore.algorithm.Dimension;
import org.cyclops.cyclopscore.helper.LocationHelpers;
import org.cyclops.cyclopscore.helper.ILocationHelpers;
import org.cyclops.cyclopscore.helper.IModHelpers;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -99,7 +100,7 @@ protected Component isValidLocation(LevelReader world, BlockPos location, IValid
return error;
}
return contains ? null : Component.translatable("multiblock.cyclopscore.error.invalidBlock",
LocationHelpers.toCompactString(location), Component.translatable(block.getDescriptionId()));
IModHelpers.get().getLocationHelpers().toCompactString(location), Component.translatable(block.getDescriptionId()));
}

protected Component isValidLocation(LevelReader world, BlockPos location, BlockPos excludeLocation) {
Expand All @@ -120,15 +121,16 @@ protected boolean isAir(LevelReader world, BlockPos location) {
* @return The found border.
*/
protected BlockPos navigateToBorder(LevelReader world, BlockPos startLocation, int dimension, int direction, BlockPos excludeLocation) {
BlockPos loopLocation = LocationHelpers.copyLocation(startLocation);
ILocationHelpers locationHelpers = IModHelpers.get().getLocationHelpers();
BlockPos loopLocation = locationHelpers.copyLocation(startLocation);

// Loop until we find a non-valid location.
while(isValidLocation(world, loopLocation, excludeLocation) == null) {
loopLocation = LocationHelpers.addToDimension(loopLocation, dimension, direction);
loopLocation = locationHelpers.addToDimension(loopLocation, dimension, direction);
}

// Because we went one increment too far.
loopLocation = LocationHelpers.addToDimension(loopLocation, dimension, -direction);
loopLocation = locationHelpers.addToDimension(loopLocation, dimension, -direction);

return loopLocation;
}
Expand Down Expand Up @@ -158,15 +160,15 @@ protected BlockPos navigateToBorder(LevelReader world, BlockPos startLocation, i
* @return The corner location.
*/
protected BlockPos navigateToCorner(LevelReader world, BlockPos startLocation, int[] dimensions, boolean max, BlockPos excludeLocation) {
BlockPos navigateLocation = LocationHelpers.copyLocation(startLocation);
BlockPos navigateLocation = IModHelpers.get().getLocationHelpers().copyLocation(startLocation);
for(int dimension : dimensions) {
navigateLocation = navigateToBorder(world, navigateLocation, dimension, max, excludeLocation);
}
return navigateLocation;
}

protected boolean isEdge(LevelReader world, int[][] dimensionEgdes, BlockPos location) {
int[] c = LocationHelpers.toArray(location);
int[] c = IModHelpers.get().getLocationHelpers().toArray(location);
for (int i = 0; i < dimensionEgdes.length; i++) {
for (int j = 0; j < dimensionEgdes[i].length; j++) {
if(dimensionEgdes[i][j] == c[i]) {
Expand Down Expand Up @@ -216,7 +218,7 @@ protected boolean coordinateRecursion(LevelReader world, int[][] dimensionEgdes,
protected boolean coordinateRecursion(LevelReader world, int[][] dimensionEgdes, int[] accumulatedCoordinates,
BlockPosAction locationAction) {
if(accumulatedCoordinates.length == dimensionEgdes.length) { // Leaf of recursion
BlockPos location = LocationHelpers.fromArray(accumulatedCoordinates);
BlockPos location = IModHelpers.get().getLocationHelpers().fromArray(accumulatedCoordinates);
if(!locationAction.run(world, location)) {
return false;
}
Expand Down Expand Up @@ -355,6 +357,8 @@ public DetectionResult detect(LevelReader world, BlockPos startLocation, BlockPo
* size of 0 in each dimension.
*/
public DetectionResult detect(LevelReader world, BlockPos startLocation, BlockPos excludeLocation, IValidationAction action, boolean changeState) {
ILocationHelpers locationHelpers = IModHelpers.get().getLocationHelpers();

// Next to the origin, we only need one corner for each dimension,
// we can easily derive if the structure is valid with these 4 corners.
Component error;
Expand All @@ -381,13 +385,13 @@ public DetectionResult detect(LevelReader world, BlockPos startLocation, BlockPo
// we'll use this in the validation of the cube.
int[] distances = new int[corners.length];
int[][] dimensionEgdes = new int[corners.length][2]; // [dimension][start | stop]
int[] cOriginCorner = LocationHelpers.toArray(originCorner);
int[] cOriginCorner = locationHelpers.toArray(originCorner);
for(int i = 0; i < corners.length; i++) {
int[] cCorner = LocationHelpers.toArray(corners[i]);
int[] cCorner = locationHelpers.toArray(corners[i]);

// Distance measurement
Vec3i sizeDifference = LocationHelpers.subtract(corners[i], originCorner);
distances[i] = LocationHelpers.toArray(sizeDifference)[i];
Vec3i sizeDifference = locationHelpers.subtract(corners[i], originCorner);
distances[i] = locationHelpers.toArray(sizeDifference)[i];

// Start and stop coordinate measurement.
int addIndex = 0;
Expand All @@ -403,7 +407,7 @@ public DetectionResult detect(LevelReader world, BlockPos startLocation, BlockPo
return new DetectionResult(error);
}

Vec3i size = LocationHelpers.fromArray(distances);
Vec3i size = locationHelpers.fromArray(distances);
for(ISizeValidator validator : getSizeValidators()) {
// Check if the size iis valid.
// If it is invalid we immediately return a null-size, but if we are in invalidation-mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.core.Vec3i;
import net.minecraft.network.chat.Component;
import org.cyclops.cyclopscore.helper.LocationHelpers;
import org.cyclops.cyclopscore.helper.IModHelpers;

/**
* A validator to check if all sides have an equal length.
Expand All @@ -15,6 +15,6 @@ public Component isSizeValid(Vec3i size) {
return null;
}
return Component.translatable("multiblock.cyclopscore.error.size.cube",
LocationHelpers.toCompactString(size.offset(1, 1, 1)));
IModHelpers.get().getLocationHelpers().toCompactString(size.offset(1, 1, 1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.cyclops.cyclopscore.block.multi;

import net.minecraft.core.Vec3i;
import net.minecraft.network.chat.Component;
import org.cyclops.cyclopscore.helper.IModHelpers;

/**
* Multiblock detection result.
* The `error` field is only available when the size is null, so if the structure was invalid.
*
* @author rubensworks
*/
public class DetectionResult {

private final Vec3i size;
private final Component error;

public DetectionResult(Vec3i size) {
this.size = size;
this.error = null;
}

public DetectionResult(Component error) {
this.size = IModHelpers.get().getLocationHelpers().copyLocation(Vec3i.ZERO);
this.error = error;
}

public DetectionResult(String error) {
this(Component.literal(error));
}

public Vec3i getSize() {
return this.size;
}

public Component getError() {
return this.error;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof DetectionResult)) return false;
final DetectionResult other = (DetectionResult) o;
if (!other.canEqual((Object) this)) return false;
final Object this$size = this.getSize();
final Object other$size = other.getSize();
if (this$size == null ? other$size != null : !this$size.equals(other$size)) return false;
final Object this$error = this.getError();
final Object other$error = other.getError();
if (this$error == null ? other$error != null : !this$error.equals(other$error)) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof DetectionResult;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $size = this.getSize();
result = result * PRIME + ($size == null ? 43 : $size.hashCode());
final Object $error = this.getError();
result = result * PRIME + ($error == null ? 43 : $error.hashCode());
return result;
}

public String toString() {
return "DetectionResult(size=" + this.getSize() + ", error=" + this.getError() + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.cyclops.cyclopscore.block.multi;

import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Block;

/**
* An exact block count validator.
*
* @author rubensworks
*/
public class ExactBlockCountValidator implements IBlockCountValidator {

private final int exactCount;

public ExactBlockCountValidator(int exactCount) {
this.exactCount = exactCount;
}

@Override
public Component isValid(int count, boolean structureComplete, Block block) {
if (!structureComplete || count == getExactCount()) {
return null;
}
return Component.translatable("multiblock.cyclopscore.error.blockCount.exact",
getExactCount(), Component.translatable(block.getDescriptionId()), count);
}

public int getExactCount() {
return this.exactCount;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof ExactBlockCountValidator)) return false;
final ExactBlockCountValidator other = (ExactBlockCountValidator) o;
if (!other.canEqual((Object) this)) return false;
if (this.getExactCount() != other.getExactCount()) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof ExactBlockCountValidator;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getExactCount();
return result;
}

public String toString() {
return "ExactBlockCountValidator(exactCount=" + this.getExactCount() + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.cyclops.cyclopscore.block.multi;

import net.minecraft.core.Vec3i;
import net.minecraft.network.chat.Component;
import org.cyclops.cyclopscore.helper.IModHelpers;

/**
* An exact size validator.
*
* @author rubensworks
*/
public class ExactSizeValidator implements ISizeValidator {

private final Vec3i exactSize;

public ExactSizeValidator(Vec3i exactSize) {
this.exactSize = exactSize;
}

@Override
public Component isSizeValid(Vec3i size) {
if (SizeValidators.compareVec3i(size, getExactSize()) == 0) {
return null;
}
return Component.translatable("multiblock.cyclopscore.error.size.exact",
IModHelpers.get().getLocationHelpers().toCompactString(size.offset(1, 1, 1)), IModHelpers.get().getLocationHelpers().toCompactString(getExactSize().offset(1, 1, 1)));
}

public Vec3i getExactSize() {
return this.exactSize;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof ExactSizeValidator)) return false;
final ExactSizeValidator other = (ExactSizeValidator) o;
if (!other.canEqual((Object) this)) return false;
final Object this$exactSize = this.getExactSize();
final Object other$exactSize = other.getExactSize();
if (this$exactSize == null ? other$exactSize != null : !this$exactSize.equals(other$exactSize)) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof ExactSizeValidator;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $exactSize = this.getExactSize();
result = result * PRIME + ($exactSize == null ? 43 : $exactSize.hashCode());
return result;
}

public String toString() {
return "ExactSizeValidator(exactSize=" + this.getExactSize() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.minecraft.core.Vec3i;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.LevelReader;
import org.cyclops.cyclopscore.helper.LocationHelpers;
import org.cyclops.cyclopscore.helper.IModHelpers;

import java.util.List;

Expand Down Expand Up @@ -52,7 +52,7 @@ protected Component validateLocationInStructure(LevelReader world, int[][] dimen
if (!isAir(world, location)) {
//System.out.println("No air at " + location);
return Component.translatable("multiblock.cyclopscore.error.hollow.air",
LocationHelpers.toCompactString(location));
IModHelpers.get().getLocationHelpers().toCompactString(location));
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.cyclops.cyclopscore.block.multi;

import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Block;

/**
* A maximum block count validator.
*
* @author rubensworks
*/
public class MaximumBlockCountValidator implements IBlockCountValidator {

private final int maximumCount;

public MaximumBlockCountValidator(int maximumCount) {
this.maximumCount = maximumCount;
}

@Override
public Component isValid(int count, boolean structureComplete, Block block) {
if (count <= getMaximumCount()) {
return null;
}
return Component.translatable("multiblock.cyclopscore.error.blockCount.max",
getMaximumCount(), Component.translatable(block.getDescriptionId()), count);
}

public int getMaximumCount() {
return this.maximumCount;
}

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof MaximumBlockCountValidator)) return false;
final MaximumBlockCountValidator other = (MaximumBlockCountValidator) o;
if (!other.canEqual((Object) this)) return false;
if (this.getMaximumCount() != other.getMaximumCount()) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof MaximumBlockCountValidator;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getMaximumCount();
return result;
}

public String toString() {
return "MaximumBlockCountValidator(maximumCount=" + this.getMaximumCount() + ")";
}
}
Loading

0 comments on commit 394e3be

Please sign in to comment.