Skip to content

Commit 497d2bb

Browse files
committed
Add initial version of world crafting
1 parent b9a0193 commit 497d2bb

3 files changed

Lines changed: 138 additions & 2 deletions

File tree

src/main/java/org/cyclops/structurecrafting/block/BlockStructuredCrafter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.world.World;
88
import org.cyclops.cyclopscore.config.configurable.ConfigurableBlock;
99
import org.cyclops.cyclopscore.config.extendedconfig.ExtendedConfig;
10+
import org.cyclops.structurecrafting.craft.WorldCraftingMatrix;
1011

1112
/**
1213
* This block will detect neighbour block updates and will try to craft a new block/item from them.
@@ -36,8 +37,13 @@ public BlockStructuredCrafter(ExtendedConfig eConfig) {
3637
}
3738

3839
@Override
39-
public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) {
40-
System.out.println("changed"); // TODO
40+
public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock) {
41+
if(!world.isBlockPowered(pos)) {
42+
WorldCraftingMatrix[] matrices = WorldCraftingMatrix.deriveMatrices(world, pos);
43+
for (WorldCraftingMatrix matrix : matrices) {
44+
if (matrix.craft()) break;
45+
}
46+
}
4147
}
4248

4349
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.cyclops.structurecrafting.craft;
2+
3+
import net.minecraft.block.state.IBlockState;
4+
import net.minecraft.item.Item;
5+
import net.minecraft.item.ItemBlock;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.item.crafting.CraftingManager;
8+
import net.minecraft.util.BlockPos;
9+
import net.minecraft.util.EnumFacing;
10+
import net.minecraft.world.World;
11+
12+
/**
13+
* A crafting matrix represented with blockstates.
14+
* @author rubensworks
15+
*/
16+
public class WorldCraftingMatrix {
17+
18+
private static final WorldInventoryCrafting INVENTORY_CRAFTING = new WorldInventoryCrafting();
19+
20+
private final World world;
21+
private final BlockPos centerPos;
22+
private final EnumFacing.Axis axis;
23+
private final BlockPos targetPos;
24+
25+
public WorldCraftingMatrix(World world, BlockPos centerPos, EnumFacing.Axis axis, BlockPos targetPos) {
26+
this.world = world;
27+
this.centerPos = centerPos;
28+
this.axis = axis;
29+
this.targetPos = targetPos;
30+
}
31+
32+
protected BlockPos addInAxis(BlockPos pos, EnumFacing.Axis axis, int i, int j) {
33+
if(axis == EnumFacing.Axis.X) {
34+
return pos.add(0, i, j);
35+
} else if(axis == EnumFacing.Axis.Y) {
36+
return pos.add(i, 0, j);
37+
} else if(axis == EnumFacing.Axis.Z) {
38+
return pos.add(i, j, 0);
39+
}
40+
return null;
41+
}
42+
43+
public boolean craft() {
44+
if(!world.isAirBlock(targetPos)) {
45+
return false;
46+
}
47+
48+
BlockPos[] positions = new BlockPos[9];
49+
50+
// Set crafting grid
51+
for(int i = -1; i < 2; i++) {
52+
for(int j = -1; j < 2; j++) {
53+
BlockPos pos = addInAxis(centerPos, axis, i, j);
54+
IBlockState blockState = world.getBlockState(pos);
55+
56+
ItemStack itemStack = null;
57+
if(blockState != null) {
58+
Item item = blockState.getBlock().getItem(world, pos);
59+
if(item != null) {
60+
itemStack = new ItemStack(item, blockState.getBlock().getDamageValue(world, pos));
61+
}
62+
}
63+
INVENTORY_CRAFTING.setItemStack(i + 1, j + 1, itemStack);
64+
positions[(j + 1) * 3 + (i + 1)] = pos;
65+
}
66+
}
67+
68+
// Determine output
69+
ItemStack itemStack = CraftingManager.getInstance().findMatchingRecipe(INVENTORY_CRAFTING, world);
70+
if(itemStack != null && itemStack.getItem() instanceof ItemBlock) {
71+
// replace blocks depending on what the crafting results are.
72+
world.setBlockState(targetPos, ((ItemBlock) itemStack.getItem()).getBlock().getStateFromMeta(itemStack.getItemDamage()));
73+
74+
// Handle remaining container items: place blocks and drop items
75+
for(int i = 0; i < INVENTORY_CRAFTING.getSizeInventory(); i++) {
76+
ItemStack remainingStack = INVENTORY_CRAFTING.getStackInSlot(i);
77+
if(remainingStack != null && remainingStack.stackSize > 0) {
78+
if(remainingStack.getItem() instanceof ItemBlock) {
79+
world.setBlockState(positions[i],((ItemBlock) remainingStack.getItem()).getBlock().
80+
getStateFromMeta(remainingStack.getItemDamage()));
81+
} else {
82+
// TODO: drop as entity, temp
83+
}
84+
} else {
85+
world.setBlockToAir(positions[i]);
86+
}
87+
}
88+
return true;
89+
}
90+
91+
return false;
92+
}
93+
94+
public static WorldCraftingMatrix[] deriveMatrices(World world, BlockPos centerPos) {
95+
final WorldCraftingMatrix[] matrices = new WorldCraftingMatrix[6]; // Test for all faces of the center position.
96+
for(int i = 0; i < EnumFacing.values().length; i++) {
97+
EnumFacing side = EnumFacing.values()[i];
98+
matrices[i] = new WorldCraftingMatrix(world, centerPos.offset(side), side.getAxis(), centerPos.offset(side.getOpposite()));
99+
}
100+
return matrices;
101+
}
102+
103+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.cyclops.structurecrafting.craft;
2+
3+
import net.minecraft.entity.player.EntityPlayer;
4+
import net.minecraft.inventory.Container;
5+
import net.minecraft.inventory.InventoryCrafting;
6+
import net.minecraft.item.ItemStack;
7+
8+
/**
9+
* A world-based implementation of the crafting container.
10+
* @author rubensworks
11+
*/
12+
public class WorldInventoryCrafting extends InventoryCrafting {
13+
14+
public WorldInventoryCrafting() {
15+
super(new Container() {
16+
@Override
17+
public boolean canInteractWith(EntityPlayer playerIn) {
18+
return false;
19+
}
20+
}, 3, 3);
21+
}
22+
23+
public void setItemStack(int row, int col, ItemStack itemStack) {
24+
setInventorySlotContents(col * 3 + row, itemStack);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)