Skip to content

Commit

Permalink
Add compat for carpenter's blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gtosh4 committed Jul 16, 2023
1 parent f133ab3 commit 647e346
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {

devOnlyNonPublishable 'com.github.GTNewHorizons:ArchitectureCraft:1.8.6'
devOnlyNonPublishable 'com.github.GTNewHorizons:ForgeMultipart:1.3.4'
devOnlyNonPublishable 'com.github.GTNewHorizons:CarpentersBlocks:3.4.0-GTNH'

runtimeOnlyNonPublishable 'com.github.GTNewHorizons:NotEnoughItems:2.3.57-GTNH:dev'

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.forge.compat.ArchitectureCraftBlockTransformHook;
import com.sk89q.worldedit.forge.compat.CarpentersBlocksBlockTransformHook;
import com.sk89q.worldedit.forge.compat.ForgeMultipartCompat;
import com.sk89q.worldedit.forge.compat.ForgeMultipartExistsCompat;
import com.sk89q.worldedit.forge.compat.NoForgeMultipartCompat;
Expand Down Expand Up @@ -68,7 +69,7 @@
name = "WorldEdit",
version = "%VERSION%",
acceptableRemoteVersions = "*",
dependencies = "after:ForgeMultipart;after:ArchitectureCraft")
dependencies = "after:ForgeMultipart;after:ArchitectureCraft;after:CarpentersBlocks")
public class ForgeWorldEdit {

public static Logger logger;
Expand Down Expand Up @@ -109,6 +110,10 @@ public void preInit(FMLPreInitializationEvent event) {
ForgeWorldData.getInstance()
.addBlockTransformHook(new ArchitectureCraftBlockTransformHook());
}
if (Loader.isModLoaded("CarpentersBlocks")) {
ForgeWorldData.getInstance()
.addBlockTransformHook(new CarpentersBlocksBlockTransformHook());
}

FMLCommonHandler.instance()
.bus()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.sk89q.worldedit.forge.compat;

import static com.carpentersblocks.data.Slope.*;

import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompoundTagBuilder;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extent.transform.BlockTransformHook;
import com.sk89q.worldedit.math.transform.AffineTransform;
import com.sk89q.worldedit.math.transform.Transform;

public class CarpentersBlocksBlockTransformHook implements BlockTransformHook {

private int[][] rotationGroups = { { ID_WEDGE_SE, ID_WEDGE_NE, ID_WEDGE_NW, ID_WEDGE_SW },
{ ID_WEDGE_NEG_N, ID_WEDGE_NEG_W, ID_WEDGE_NEG_S, ID_WEDGE_NEG_E },
{ ID_WEDGE_POS_N, ID_WEDGE_POS_W, ID_WEDGE_POS_S, ID_WEDGE_POS_E },
{ ID_WEDGE_INT_NEG_SE, ID_WEDGE_INT_NEG_NE, ID_WEDGE_INT_NEG_NW, ID_WEDGE_INT_NEG_SW },
{ ID_WEDGE_INT_POS_SE, ID_WEDGE_INT_POS_NE, ID_WEDGE_INT_POS_NW, ID_WEDGE_INT_POS_SW },
{ ID_WEDGE_EXT_NEG_SE, ID_WEDGE_EXT_NEG_NE, ID_WEDGE_EXT_NEG_NW, ID_WEDGE_EXT_NEG_SW },
{ ID_WEDGE_EXT_POS_SE, ID_WEDGE_EXT_POS_NE, ID_WEDGE_EXT_POS_NW, ID_WEDGE_EXT_POS_SW },
{ ID_OBL_INT_NEG_SE, ID_OBL_INT_NEG_NE, ID_OBL_INT_NEG_NW, ID_OBL_INT_NEG_SW },
{ ID_OBL_INT_POS_SE, ID_OBL_INT_POS_NE, ID_OBL_INT_POS_NW, ID_OBL_INT_POS_SW },
{ ID_OBL_EXT_NEG_SE, ID_OBL_EXT_NEG_NE, ID_OBL_EXT_NEG_NW, ID_OBL_EXT_NEG_SW },
{ ID_OBL_EXT_POS_SE, ID_OBL_EXT_POS_NE, ID_OBL_EXT_POS_NW, ID_OBL_EXT_POS_SW }, { ID_PRISM_NEG, ID_PRISM_POS },
{ ID_PRISM_1P_POS_N, ID_PRISM_1P_POS_W, ID_PRISM_1P_POS_S, ID_PRISM_1P_POS_E },
{ ID_PRISM_2P_POS_SE, ID_PRISM_2P_POS_NE, ID_PRISM_2P_POS_NW, ID_PRISM_2P_POS_SW },
{ ID_PRISM_3P_POS_NWE, ID_PRISM_3P_POS_NSW, ID_PRISM_3P_POS_SWE, ID_PRISM_3P_POS_NSE }, { ID_PRISM_POS_4P },
{ ID_PRISM_WEDGE_POS_N, ID_PRISM_WEDGE_POS_W, ID_PRISM_WEDGE_POS_S, ID_PRISM_WEDGE_POS_E }, };

private int rotateSlot(int slot, int ticks) {
for (int type = 0; type < rotationGroups.length; type++) {
int[] rotations = rotationGroups[type];
for (int i = 0; i < rotations.length; i++) {
if (rotations[i] == slot) {
// make sure ticks is positive
ticks = ticks % rotations.length + rotations.length;
slot = rotations[(i + ticks) % rotations.length];
return slot;
}
}
}
return -1;
}

@Override
public BaseBlock transformBlock(BaseBlock block, Transform transform) {
CompoundTag nbt = block.getNbtData();
if (nbt == null) {
return block;
}
if (!"TileEntityCarpentersBlock".equals(nbt.getString("id"))) {
return block;
}
if (!(transform instanceof AffineTransform affine)) {
return block;
}
Vector rot = affine.getRotations();
if (rot.getX() > 0) {
System.out.println("X rotation is currently unsupported");
rot = rot.setX(0);
}
if (rot.getZ() > 0) {
System.out.println("Z rotation is currently unsupported");
rot = rot.setZ(0);
}
if (rot.lengthSq() == 0) {
return block;
}
CompoundTagBuilder builder = nbt.createBuilder();
int md = nbt.asInt("cbMetadata");

if (rot.getY() > 0) {
int ticks = (int) Math.round(rot.getY() / 90);
md = rotateSlot(md, ticks);
}
builder.putInt("cbMetadata", md);
return new BaseBlock(block.getId(), block.getData(), builder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ private int rotateEdgeSlot(Vector rot, int slot) {
}
if (rot.getX() < 0) {
int ticks = (int) Math.round(rot.getX() / 90);
System.out.println("X rotation currently unsupported");
}
if (rot.getZ() > 0) {
int ticks = (int) Math.round(rot.getZ() / 90);
System.out.println("Z rotation currently unsupported");
}
return slot;
}
Expand All @@ -108,11 +106,9 @@ private int rotatePostSlot(Vector rot, int slot) {
}
if (rot.getX() < 0) {
int ticks = (int) Math.round(rot.getX() / 90);
System.out.println("X rotation currently unsupported");
}
if (rot.getZ() > 0) {
int ticks = (int) Math.round(rot.getZ() / 90);
System.out.println("Z rotation currently unsupported");
}
return slot;
}
Expand All @@ -128,11 +124,9 @@ private int rotateCnrSlot(Vector rot, int slot) {
}
if (rot.getX() < 0) {
int ticks = (int) Math.round(rot.getX() / 90);
System.out.println("X rotation currently unsupported");
}
if (rot.getZ() > 0) {
int ticks = (int) Math.round(rot.getZ() / 90);
System.out.println("Z rotation currently unsupported");
}
return slot;
}
Expand All @@ -149,11 +143,9 @@ private int rotateFaceSlot(Vector rot, int slot) {
}
if (rot.getX() < 0) {
int ticks = (int) Math.round(rot.getX() / 90);
System.out.println("X rotation currently unsupported");
}
if (rot.getZ() > 0) {
int ticks = (int) Math.round(rot.getZ() / 90);
System.out.println("Z rotation currently unsupported");
}
return slot;
}
Expand All @@ -171,6 +163,14 @@ public BaseBlock transformBlock(BaseBlock block, Transform transform) {
return block;
}
Vector rot = affine.getRotations();
if (rot.getX() > 0) {
System.out.println("X rotation is currently unsupported");
rot = rot.setX(0);
}
if (rot.getZ() > 0) {
System.out.println("Z rotation is currently unsupported");
rot = rot.setZ(0);
}
if (rot.lengthSq() == 0) {
return block;
}
Expand Down

0 comments on commit 647e346

Please sign in to comment.