forked from VazkiiMods/Botania
-
Notifications
You must be signed in to change notification settings - Fork 43
Add Botania Multiblocks to StructureLib #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dream-Master
merged 13 commits into
GTNewHorizons:master
from
sisyphussy:multiblock-integration
Sep 3, 2025
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
889110f
Add botania Multiblocks to StructureLib
sisyphussy f84b6b5
Merge branch 'GTNewHorizons:master' into multiblock-integration
sisyphussy bb8ef6e
Replace dirt with grass in ManaEnchanter
sisyphussy 5c998da
Comment Cleanup
sisyphussy 1984a64
Update dependencies.gradle
sisyphussy cf21372
Fix Multiblock Projection
sisyphussy f7d8a25
Added StructureLib check
sisyphussy de776c1
Removed redundant import-statement
sisyphussy f532370
update deps + error handling
sisyphussy 22daa1a
Merge branch 'master' into multiblock-integration
Dream-Master f703c33
Revert hashCode function + updateDeps
sisyphussy 099e83b
Merge branch 'multiblock-integration' of https://github.com/sisyphuss…
sisyphussy 9404051
Merge branch 'master' into multiblock-integration
sisyphussy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
src/main/java/vazkii/botania/api/lexicon/multiblock/compat/MultiblockCompatRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package vazkii.botania.api.lexicon.multiblock.compat; | ||
|
||
import com.gtnewhorizon.structurelib.alignment.constructable.IMultiblockInfoContainer; | ||
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing; | ||
import com.gtnewhorizon.structurelib.structure.IStructureDefinition; | ||
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment; | ||
import com.gtnewhorizon.structurelib.structure.StructureDefinition; | ||
import com.gtnewhorizon.structurelib.structure.StructureUtility; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.tileentity.TileEntity; | ||
import net.minecraft.util.ChunkCoordinates; | ||
import vazkii.botania.api.lexicon.multiblock.Multiblock; | ||
import vazkii.botania.api.lexicon.multiblock.component.MultiblockComponent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* Automatically register any Botania Multiblock | ||
*/ | ||
public class MultiblockCompatRegistry { | ||
|
||
public static <T extends TileEntity> void registerMultiblock( | ||
Multiblock mb, Class<T> controllerTileClass, | ||
Block controllerBlock, | ||
MultiblockComponent... extra | ||
) { | ||
List<MultiblockComponent> components = mb.getComponents(); | ||
if (extra.length != 0) { | ||
components = new ArrayList<>(components); | ||
Collections.addAll(components, extra); | ||
} | ||
|
||
Map<Integer, RawStructureData> structureDataMap = new HashMap<>(); | ||
int min_x = Integer.MAX_VALUE, max_x = Integer.MIN_VALUE; | ||
int min_y = Integer.MAX_VALUE, max_y = Integer.MIN_VALUE; | ||
int min_z = Integer.MAX_VALUE, max_z = Integer.MIN_VALUE; | ||
|
||
char ch = 'a'; | ||
for (MultiblockComponent component : components) { | ||
int hash = getHash(component.getBlock(), component.getMeta()); | ||
ChunkCoordinates pos = component.getRelativePosition(); | ||
RawStructureData structureData = structureDataMap.get(hash); | ||
if (structureData == null) { | ||
List<ChunkCoordinates> coordinates = new ArrayList<>(); | ||
structureData = new RawStructureData( | ||
coordinates, | ||
component.getBlock(), | ||
component.getMeta(), | ||
ch++ | ||
); | ||
structureDataMap.put(hash, structureData); | ||
} | ||
structureData.getPositions().add(pos); | ||
|
||
min_x = Math.min(min_x, pos.posX); max_x = Math.max(max_x, pos.posX); | ||
min_y = Math.min(min_y, pos.posY); max_y = Math.max(max_y, pos.posY); | ||
min_z = Math.min(min_z, pos.posZ); max_z = Math.max(max_z, pos.posZ); | ||
} | ||
|
||
int x_size = max_x - min_x + 1; | ||
int y_size = max_y - min_y + 1; | ||
int z_size = max_z - min_z + 1; | ||
|
||
//Data is saved in [z][y][x] format | ||
String[][] stringData = new String[z_size][y_size]; | ||
|
||
char[] buffer = new char[x_size]; | ||
Arrays.fill(buffer, ' '); | ||
//Since Strings and their content are immutable, this is fine. | ||
String filledWithSpaces = new String(buffer); | ||
for (int z = 0; z < z_size; z++) { | ||
for (int y = 0; y < y_size; y++) { | ||
stringData[z][y] = filledWithSpaces; | ||
} | ||
} | ||
|
||
int controller_x = 0, controller_y = 0, controller_z = 0; | ||
StringBuilder builder = new StringBuilder(x_size); | ||
for (RawStructureData data : structureDataMap.values()) { | ||
for (ChunkCoordinates pos : data.getPositions()) { | ||
int x = pos.posX - min_x; | ||
int z = pos.posZ - min_z; | ||
//Data has to get flipped | ||
int y = (stringData[z].length - 1) - (pos.posY - min_y); | ||
|
||
if (data.getBlock() == controllerBlock) { | ||
controller_x = x; | ||
controller_y = y; | ||
controller_z = z; | ||
} | ||
|
||
String s = stringData[z][y]; | ||
builder.append(s); | ||
builder.setCharAt(x, data.getBlockIdentifier()); | ||
stringData[z][y] = builder.toString(); | ||
builder.setLength(0); | ||
} | ||
} | ||
|
||
StructureDefinition.Builder<? super T> structureBuilder = IStructureDefinition.builder() | ||
.addShape("main", stringData); | ||
|
||
for (RawStructureData data : structureDataMap.values()) { | ||
structureBuilder.addElement( | ||
data.getBlockIdentifier(), | ||
StructureUtility.ofBlock( | ||
data.getBlock(), | ||
data.getMetadata() | ||
) | ||
); | ||
} | ||
|
||
IMultiblockInfoContainer.registerTileClass( | ||
controllerTileClass, | ||
new BotaniaMultiblockInfoContainer<>( | ||
structureBuilder.build(), | ||
controller_x, | ||
controller_y, | ||
controller_z | ||
) | ||
); | ||
} | ||
|
||
private static int getHash(Block block, int metadata) { | ||
return block.hashCode() * 31 + metadata; | ||
} | ||
|
||
|
||
private static class BotaniaMultiblockInfoContainer<T extends TileEntity> implements IMultiblockInfoContainer<T> { | ||
|
||
private final IStructureDefinition<? super T> structure; | ||
private final int x_offset, y_offset, z_offset; | ||
|
||
public BotaniaMultiblockInfoContainer( | ||
IStructureDefinition<? super T> structure, | ||
int x_offset, int y_offset, int z_offset | ||
) { | ||
this.structure = structure; | ||
this.x_offset = x_offset; | ||
this.y_offset = y_offset; | ||
this.z_offset = z_offset; | ||
} | ||
|
||
@Override | ||
public void construct(ItemStack stackSize, boolean hintsOnly, T tileEntity, ExtendedFacing aSide) { | ||
structure.buildOrHints( | ||
tileEntity, | ||
stackSize, | ||
"main", | ||
tileEntity.getWorldObj(), | ||
aSide, | ||
tileEntity.xCoord, | ||
tileEntity.yCoord, | ||
tileEntity.zCoord, | ||
x_offset, | ||
y_offset, | ||
z_offset, | ||
hintsOnly | ||
); | ||
} | ||
|
||
@Override | ||
public int survivalConstruct(ItemStack stackSize, int elementBudge, ISurvivalBuildEnvironment env, T tileEntity, ExtendedFacing aSide) { | ||
return structure.survivalBuild( | ||
tileEntity, | ||
stackSize, | ||
"main", | ||
tileEntity.getWorldObj(), | ||
aSide, | ||
tileEntity.xCoord, | ||
tileEntity.yCoord, | ||
tileEntity.zCoord, | ||
x_offset, | ||
y_offset, | ||
z_offset, | ||
elementBudge, | ||
env, | ||
false | ||
); | ||
} | ||
|
||
@Override | ||
public String[] getDescription(ItemStack stackSize) { | ||
return new String[0]; | ||
} | ||
} | ||
|
||
private static class RawStructureData { | ||
private final List<ChunkCoordinates> positions; | ||
private final char identifier; | ||
private final Block block; | ||
private final int metadata; | ||
|
||
public RawStructureData(List<ChunkCoordinates> positions, Block block, int metadata, char identifier) { | ||
this.positions = positions; | ||
this.identifier = identifier; | ||
this.block = block; | ||
this.metadata = metadata; | ||
} | ||
|
||
public Block getBlock() { | ||
return block; | ||
} | ||
|
||
public int getMetadata() { | ||
return metadata; | ||
} | ||
|
||
public List<ChunkCoordinates> getPositions() { | ||
return positions; | ||
} | ||
|
||
public char getBlockIdentifier() { | ||
return identifier; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.