From e10901dcc7a379a9aaff24d30395dfef494bd7b5 Mon Sep 17 00:00:00 2001 From: RecursivePineapple Date: Mon, 30 Dec 2024 16:54:51 -0500 Subject: [PATCH] architecturecraft rotating --- .../ArchitectureCraftAnalysisResult.java | 48 +++++++++++--- .../common/compat/BlockPropertyRegistry.java | 62 +++++++++++++++++++ .../common/compat/Orientation.java | 4 ++ .../compat/OrientationBlockProperty.java | 5 ++ .../items/manipulator/PendingBlock.java | 22 ++++++- .../common/utils/MMUtils.java | 16 ++++- 6 files changed, 145 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/ArchitectureCraftAnalysisResult.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/ArchitectureCraftAnalysisResult.java index 738ba45..dbee801 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/ArchitectureCraftAnalysisResult.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/ArchitectureCraftAnalysisResult.java @@ -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) { @@ -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))); @@ -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); } @@ -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 + + "]"; + } } diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/BlockPropertyRegistry.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/BlockPropertyRegistry.java index cef4054..444490e 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/BlockPropertyRegistry.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/BlockPropertyRegistry.java @@ -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; @@ -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 @@ -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); diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/Orientation.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/Orientation.java index b156379..456ae59 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/Orientation.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/Orientation.java @@ -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), @@ -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); } } diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/OrientationBlockProperty.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/OrientationBlockProperty.java index f3e0185..4292ae5 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/OrientationBlockProperty.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/compat/OrientationBlockProperty.java @@ -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); } diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/PendingBlock.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/PendingBlock.java index c051bf4..2c4e4d4 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/PendingBlock.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/PendingBlock.java @@ -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; @@ -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; @@ -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) { @@ -457,8 +473,9 @@ public static Comparator 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 { @@ -470,6 +487,7 @@ public static enum CopyableProperties { ROTATION, MODE, TEXT, + ORIENTATION, ; public static final ImmutableList VALUES = ImmutableList.copyOf(values()); diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/utils/MMUtils.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/utils/MMUtils.java index 904d506..4fc00e5 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/utils/MMUtils.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/utils/MMUtils.java @@ -560,12 +560,24 @@ public static ForgeDirection nullIfUnknown(ForgeDirection dir) { return dir == ForgeDirection.UNKNOWN ? null : dir; } + public static 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 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 getIndexSafe(List 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); } /**