Skip to content

Commit

Permalink
architecturecraft rotating
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursivePineapple committed Dec 30, 2024
1 parent afbb19d commit e10901d
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class ArchitectureCraftAnalysisResult implements ITileAnalysisIntegration {

public int shape;
public byte side, turn;
public PortableItemStack material, cladding;

public static ArchitectureCraftAnalysisResult analyze(IBlockAnalysisContext context, TileEntity te) {
Expand All @@ -29,8 +28,6 @@ public static ArchitectureCraftAnalysisResult analyze(IBlockAnalysisContext cont
ArchitectureCraftAnalysisResult result = new ArchitectureCraftAnalysisResult();

result.shape = tileShape.shape.id;
result.side = tileShape.side;
result.turn = tileShape.turn;

result.material = new PortableItemStack(new ItemStack(tileShape.baseBlockState.getBlock(), 0, BlockCompatUtils.getMetaFromBlockState(tileShape.baseBlockState)));

Expand All @@ -46,9 +43,6 @@ public boolean apply(IBlockApplyContext ctx) {
TileEntity te = ctx.getTileEntity();

if (te instanceof TileShape tileShape) {
tileShape.setSide(side);
tileShape.setTurn(turn);

if (tileShape.secondaryBlockState != null) {
removeCladding(ctx, tileShape, false);
}
Expand Down Expand Up @@ -158,11 +152,49 @@ public ArchitectureCraftAnalysisResult clone() {
ArchitectureCraftAnalysisResult dup = new ArchitectureCraftAnalysisResult();

dup.shape = shape;
dup.side = side;
dup.turn = turn;
dup.material = material == null ? null : material.clone();
dup.cladding = cladding == null ? null : cladding.clone();

return dup;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + shape;
result = prime * result + ((material == null) ? 0 : material.hashCode());
result = prime * result + ((cladding == null) ? 0 : cladding.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArchitectureCraftAnalysisResult other = (ArchitectureCraftAnalysisResult) obj;
if (shape != other.shape)
return false;
if (material == null) {
if (other.material != null)
return false;
} else if (!material.equals(other.material))
return false;
if (cladding == null) {
if (other.cladding != null)
return false;
} else if (!cladding.equals(other.cladding))
return false;
return true;
}

@Override
public String toString() {
return "ArchitectureCraftAnalysisResult [shape=" + shape + ", material=" + material + ", cladding=" + cladding
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.recursive_pineapple.matter_manipulator.common.utils.Mods;
import com.recursive_pineapple.matter_manipulator.common.utils.Mods.Names;

import gcewing.architecture.common.tile.TileArchitecture;
import ic2.api.tile.IWrenchable;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
Expand Down Expand Up @@ -172,6 +173,7 @@ public static void init() {

if (Mods.StorageDrawers.isModLoaded()) initStorageDrawers();
if (Mods.IndustrialCraft2.isModLoaded()) initIC2();
if (Mods.ArchitectureCraft.isModLoaded()) initArch();
}

//#region Vanilla
Expand Down Expand Up @@ -724,6 +726,66 @@ public void setValue(World world, int x, int y, int z, ForgeDirection value) {

//#endregion

//#region Architecturecraft

private static void initArch() {

final ForgeDirection[][] FORWARDS = {
{SOUTH, EAST, NORTH, WEST}, // down = DOWN
{NORTH, EAST, SOUTH, WEST}, // down = UP
{DOWN, EAST, UP, WEST}, // down = NORTH
{DOWN, WEST, UP, EAST}, // down = SOUTH
{DOWN, NORTH, UP, SOUTH}, // down = WEST
{DOWN, SOUTH, UP, NORTH}, // down = EAST
};

registerTileEntityInterfaceProperty(
TileArchitecture.class,
new OrientationBlockProperty() {
@Override
public String getName() { return "orientation"; }

@Override
public Orientation getValue(World world, int x, int y, int z) {
if (!(world.getTileEntity(x, y, z) instanceof TileArchitecture tile)) return Orientation.NONE;

return Orientation.getOrientation(
ForgeDirection.getOrientation(tile.side),
MMUtils.getIndexSafe(MMUtils.getIndexSafe(FORWARDS, tile.side), tile.turn));
}

@Override
public void setValue(World world, int x, int y, int z, Orientation value) {
if (!(world.getTileEntity(x, y, z) instanceof TileArchitecture tile)) return;

if (value == null || value == Orientation.NONE || value.a == value.b || value.a.getOpposite() == value.b) value = Orientation.DOWN_NORTH;

int index = MMUtils.indexOf(MMUtils.getIndexSafe(FORWARDS, value.a.ordinal()), value.b);

if (index != -1) {
tile.turn = (byte) index;
tile.side = (byte) value.a.ordinal();
} else {
for (int side = 0; side < FORWARDS.length; side++) {
index = MMUtils.indexOf(FORWARDS[side], value);

if (index != -1) {
tile.side = (byte) side;
tile.turn = (byte) index;
break;
}
}
}

tile.markDirty();
world.markBlockForUpdate(x, y, z);
}
}
);
}

//#endregion

public static DirectionBlockProperty methodIntDirectionTile(Class<?> clazz, String getterName, String setterName) {
try {
Method getter = clazz.getDeclaredMethod(getterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraftforge.common.util.ForgeDirection;

public enum Orientation {
NONE(UNKNOWN, UNKNOWN),
DOWN_DOWN(DOWN, DOWN),
UP_DOWN(UP, DOWN),
NORTH_DOWN(NORTH, DOWN),
Expand Down Expand Up @@ -60,6 +61,9 @@ private Orientation(ForgeDirection a, ForgeDirection b) {
}

public static Orientation getOrientation(ForgeDirection a, ForgeDirection b) {
if (a == null) a = ForgeDirection.UNKNOWN;
if (b == null) b = ForgeDirection.UNKNOWN;

return ORIENTATIONS.get(a).get(b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ default Orientation parse(String text) throws Exception {
throw new Exception("illegal orientation: '" + text + "'");
}

@Override
default String stringify(Orientation value) {
return value.name().toLowerCase();
}

public static interface O2M {
int getMeta(Orientation dir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.joml.Vector3f;
Expand All @@ -17,6 +18,7 @@
import com.recursive_pineapple.matter_manipulator.common.building.TileAnalysisResult;
import com.recursive_pineapple.matter_manipulator.common.compat.BlockProperty;
import com.recursive_pineapple.matter_manipulator.common.compat.BlockPropertyRegistry;
import com.recursive_pineapple.matter_manipulator.common.compat.Orientation;
import com.recursive_pineapple.matter_manipulator.common.utils.LazyBlock;
import com.recursive_pineapple.matter_manipulator.common.utils.MMUtils;
import com.recursive_pineapple.matter_manipulator.common.utils.Mods;
Expand Down Expand Up @@ -252,6 +254,20 @@ public void transform(Transform transform) {
MMMod.LOG.error("could not transform rotation", e);
}
}

if (properties.containsKey(CopyableProperties.ORIENTATION)) {
try {
Orientation o = Orientation.valueOf(properties.get(CopyableProperties.ORIENTATION).toUpperCase());

o = Orientation.getOrientation(
transform.apply(o.a),
transform.apply(o.b));

properties.put(CopyableProperties.ORIENTATION, o.name().toLowerCase());
} catch (Exception e) {
MMMod.LOG.error("could not transform orientation", e);
}
}
}

if (tileData != null) {
Expand Down Expand Up @@ -457,8 +473,9 @@ public static Comparator<PendingBlock> getComparator() {
int chunkX = b.x >> 4;
int chunkZ = b.z >> 4;

return chunkX | (chunkZ << 32);
});
return (long) chunkX | (long) (chunkZ << 32);
})
.thenComparing(b -> Objects.hashCode(b.tileData));
}

public static enum CopyableProperties {
Expand All @@ -470,6 +487,7 @@ public static enum CopyableProperties {
ROTATION,
MODE,
TEXT,
ORIENTATION,
;

public static final ImmutableList<CopyableProperties> VALUES = ImmutableList.copyOf(values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,24 @@ public static ForgeDirection nullIfUnknown(ForgeDirection dir) {
return dir == ForgeDirection.UNKNOWN ? null : dir;
}

public static <T> int indexOf(T[] array, T value) {
int l = array.length;

for (int i = 0; i < l; i++) {
if (array[i] == value) {
return i;
}
}

return -1;
}

public static <T> T getIndexSafe(T[] array, int index) {
return index < 0 || index >= array.length ? null : array[index];
return array == null || index < 0 || index >= array.length ? null : array[index];
}

public static <T> T getIndexSafe(List<T> list, int index) {
return index < 0 || index >= list.size() ? null : list.get(index);
return list == null || index < 0 || index >= list.size() ? null : list.get(index);
}

/**
Expand Down

0 comments on commit e10901d

Please sign in to comment.