Skip to content

Commit 5cf63a4

Browse files
committed
Recipes: Basic recipe output/catalyst checking
Recipes do not currently respect the blocks in the field when determining matching state.
1 parent f3fd562 commit 5cf63a4

File tree

11 files changed

+182
-71
lines changed

11 files changed

+182
-71
lines changed

src/main/java/com/robotgryphon/compactcrafting/blocks/FieldProjectorBlock.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.robotgryphon.compactcrafting.blocks;
22

3-
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
4-
import com.robotgryphon.compactcrafting.core.Registration;
53
import com.robotgryphon.compactcrafting.field.ProjectorHelper;
64
//import mcjty.theoneprobe.api.IProbeHitData;
75
//import mcjty.theoneprobe.api.IProbeInfo;

src/main/java/com/robotgryphon/compactcrafting/blocks/tiles/FieldProjectorTile.java renamed to src/main/java/com/robotgryphon/compactcrafting/blocks/FieldProjectorTile.java

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.robotgryphon.compactcrafting.blocks.tiles;
1+
package com.robotgryphon.compactcrafting.blocks;
22

33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
@@ -19,6 +19,7 @@
1919
import net.minecraft.util.RegistryKey;
2020
import net.minecraft.util.math.AxisAlignedBB;
2121
import net.minecraft.util.math.BlockPos;
22+
import net.minecraft.world.World;
2223
import net.minecraft.world.server.ServerWorld;
2324
import net.minecraftforge.fml.RegistryObject;
2425
import net.minecraftforge.registries.ForgeRegistries;
@@ -143,61 +144,71 @@ private void beginCraft() {
143144
this.isCrafting = true;
144145
}
145146

146-
private void tickCrafting() {
147-
if(this.field.isPresent()) {
147+
/**
148+
* Scans the field and attempts to match a recipe that's placed in it.
149+
*/
150+
private void doRecipeScan() {
151+
// Only the primary projector needs to worry about the recipe scan
152+
if(!isMainProjector())
153+
return;
154+
155+
if (this.field.isPresent()) {
148156
FieldProjection fp = this.field.get();
149-
Optional<AxisAlignedBB> fb = fp.getBounds();
157+
AxisAlignedBB fieldBounds = fp.getBounds();
150158

151159
Collection<RegistryObject<MiniaturizationRecipe>> entries = Registration.MINIATURIZATION_RECIPES.getEntries();
152-
if(entries.isEmpty())
160+
if (entries.isEmpty())
153161
return;
154162

155163
FieldProjection field = this.field.get();
156164
FieldProjectionSize fieldSize = field.getFieldSize();
157-
AxisAlignedBB fieldBounds = fb.get();
158165

159-
Set<MiniaturizationRecipe> matchedRecipes = entries
166+
Stream<MiniaturizationRecipe> matchedRecipes = entries
160167
.stream()
161168
.map(RegistryObject::get)
162-
.filter(recipe -> recipe.matches(world, fieldSize, fieldBounds))
163-
.collect(Collectors.toSet());
169+
.filter(recipe -> recipe.matches(world, fieldSize, fieldBounds));
164170

165-
if(matchedRecipes.size() != 1) {
166-
CompactCrafting.LOGGER.debug("More than one recipe matched result, this shouldn't happen. Check yo configs.");
167-
return;
168-
}
169-
170-
for(MiniaturizationRecipe matchedRecipe : matchedRecipes) {
171-
List<ItemEntity> catalystEntities = getCatalystsInField(fieldBounds, matchedRecipe.catalyst);
172-
if (catalystEntities.size() > 0) {
173-
// We dropped a catalyst item in - are there any blocks in the field?
174-
// If so, we'll need to check the recipes -- but for now we just use it to
175-
// not delete the item if there's nothing in here
176-
if (CraftingHelper.hasBlocksInField(world, fieldBounds)) {
177-
this.isCrafting = true;
178-
179-
if (!world.isRemote()) {
180-
CraftingHelper.deleteCraftingBlocks(world, fieldBounds);
181-
CraftingHelper.consumeCatalystItem(catalystEntities.get(0), 1);
182-
183-
Set<ItemEntity> collect = Arrays.stream(matchedRecipe.getOutputs())
184-
.map(is -> new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), is))
185-
.collect(Collectors.toSet());
171+
Optional<MiniaturizationRecipe> matched = matchedRecipes.findFirst();
172+
matched.ifPresent(miniaturizationRecipe -> this.currentRecipe = miniaturizationRecipe);
173+
}
174+
}
186175

187-
for(ItemEntity ie : collect) {
188-
world.addEntity(ie);
189-
}
176+
private void tickCrafting() {
177+
// We don't have a recipe -- get out early
178+
if (this.currentRecipe == null)
179+
return;
190180

181+
if (this.field.isPresent()) {
182+
FieldProjection fp = this.field.get();
183+
AxisAlignedBB fieldBounds = fp.getBounds();
184+
List<ItemEntity> catalystEntities = getCatalystsInField(fieldBounds, currentRecipe.catalyst);
185+
if (catalystEntities.size() > 0) {
186+
// We dropped a catalyst item in - are there any blocks in the field?
187+
// If so, we'll need to check the recipes -- but for now we just use it to
188+
// not delete the item if there's nothing in here
189+
if (CraftingHelper.hasBlocksInField(world, fieldBounds)) {
190+
this.isCrafting = true;
191+
192+
if (!world.isRemote()) {
193+
CraftingHelper.deleteCraftingBlocks(world, fieldBounds);
194+
CraftingHelper.consumeCatalystItem(catalystEntities.get(0), 1);
195+
196+
BlockPos fieldCenter = field.get().getCenterPosition();
197+
for (ItemStack is : currentRecipe.getOutputs()) {
198+
ItemEntity itemEntity = new ItemEntity(world, fieldCenter.getX() + 0.5f, fieldCenter.getY() + 0.5f, fieldCenter.getZ() + 0.5f, is);
199+
((ServerWorld) world).addEntity(itemEntity);
191200
}
192201

193-
this.isCrafting = false;
194202
}
203+
204+
// We aren't crafting any more - recipe complete, reset for next one
205+
this.isCrafting = false;
206+
this.currentRecipe = null;
195207
}
196208
}
197209
}
198210
}
199211

200-
201212
private List<ItemEntity> getCatalystsInField(AxisAlignedBB fieldBounds, Item itemFilter) {
202213
List<ItemEntity> itemsInRange = world.getEntitiesWithinAABB(ItemEntity.class, fieldBounds);
203214
return itemsInRange.stream()
@@ -232,10 +243,7 @@ public AxisAlignedBB getRenderBoundingBox() {
232243
// Otherwise just use the super implementation
233244
if (this.field.isPresent()) {
234245
FieldProjection fp = this.field.get();
235-
if (!fp.getBounds().isPresent())
236-
return super.getRenderBoundingBox();
237-
238-
return fp.getBounds().get().grow(10);
246+
return fp.getBounds().grow(10);
239247
}
240248

241249
return super.getRenderBoundingBox();
@@ -247,12 +255,24 @@ public Optional<FieldProjection> getField() {
247255

248256
/**
249257
* Called whenever a nearby block is changed near the field.
258+
*
250259
* @param pos The position a block was updated at.
251260
*/
252261
public void handleNearbyBlockUpdate(BlockPos pos, BlockUpdateType updateType) {
253-
if(updateType == BlockUpdateType.UNKNOWN)
262+
if (updateType == BlockUpdateType.UNKNOWN)
254263
return;
255264

256-
doFieldCheck();
265+
// Is there a current projection field that's active?
266+
if(this.field.isPresent()) {
267+
AxisAlignedBB fieldBounds = this.field.get().getBounds();
268+
269+
// Is the block update INSIDE the current field?
270+
boolean blockInField = fieldBounds
271+
.contains(pos.getX() + 0.5f, pos.getY() + 0.5f, pos.getZ() + 0.5f);
272+
273+
// Recipe update
274+
if(blockInField)
275+
doRecipeScan();
276+
}
257277
}
258278
}

src/main/java/com/robotgryphon/compactcrafting/client/render/FieldProjectorRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.mojang.blaze3d.matrix.MatrixStack;
44
import com.mojang.blaze3d.vertex.IVertexBuilder;
55
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
6-
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
6+
import com.robotgryphon.compactcrafting.blocks.FieldProjectorTile;
77
import com.robotgryphon.compactcrafting.core.Constants;
88
import com.robotgryphon.compactcrafting.field.FieldProjection;
99
import net.minecraft.block.BlockState;
@@ -67,7 +67,7 @@ public void render(FieldProjectorTile tile, float partialTicks, MatrixStack matr
6767
BlockPos center = fp.getCenterPosition();
6868
int fieldSize = fp.getFieldSize().getSize();
6969

70-
AxisAlignedBB cube = fp.getBounds().get();
70+
AxisAlignedBB cube = fp.getBounds();
7171

7272
renderFaces(tile, matrixStack, buffers, cube, 0);
7373

src/main/java/com/robotgryphon/compactcrafting/core/Registration.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
5-
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
5+
import com.robotgryphon.compactcrafting.blocks.FieldProjectorTile;
66
import com.robotgryphon.compactcrafting.items.FieldProjectorItem;
77
import com.robotgryphon.compactcrafting.recipes.IRecipeLayer;
88
import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe;
99
import com.robotgryphon.compactcrafting.recipes.SingleComponentRecipeLayer;
1010
import net.minecraft.block.Block;
11+
import net.minecraft.block.Blocks;
1112
import net.minecraft.block.material.Material;
1213
import net.minecraft.item.Item;
14+
import net.minecraft.item.ItemStack;
1315
import net.minecraft.item.Items;
1416
import net.minecraft.tileentity.TileEntityType;
1517
import net.minecraft.util.math.AxisAlignedBB;
1618
import net.minecraft.util.math.BlockPos;
1719
import net.minecraftforge.common.ToolType;
1820
import net.minecraftforge.eventbus.api.IEventBus;
1921
import net.minecraftforge.fml.RegistryObject;
20-
import net.minecraftforge.fml.common.registry.GameRegistry;
2122
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
2223
import net.minecraftforge.registries.*;
24+
import org.lwjgl.system.CallbackI;
2325

2426
import java.util.function.Supplier;
25-
import java.util.stream.Stream;
2627

2728
import static com.robotgryphon.compactcrafting.CompactCrafting.MOD_ID;
2829

@@ -55,7 +56,7 @@ public class Registration {
5556
// ================================================================================================================
5657
public static final RegistryObject<Block> FIELD_PROJECTOR_BLOCK = BLOCKS.register("field_projector", () ->
5758
new FieldProjectorBlock(Block.Properties.create(Material.IRON)
58-
.hardnessAndResistance(8, 20)
59+
.hardnessAndResistance(8, 20)
5960
));
6061

6162
// ================================================================================================================
@@ -78,11 +79,18 @@ public class Registration {
7879
public static final RegistryObject<MiniaturizationRecipe> SIMPLE_RECIPE = MINIATURIZATION_RECIPES.register("simple", () ->
7980
{
8081
MiniaturizationRecipe rec = new MiniaturizationRecipe();
81-
rec.layers = new IRecipeLayer[] {
82-
new SingleComponentRecipeLayer(Items.IRON_BLOCK, new AxisAlignedBB(BlockPos.ZERO))
82+
rec.layers = new IRecipeLayer[]{
83+
new SingleComponentRecipeLayer("I", new BlockPos[0]),
84+
new SingleComponentRecipeLayer("R", new BlockPos[0])
8385
};
8486

8587
rec.catalyst = Items.REDSTONE;
88+
rec.outputs = new ItemStack[]{
89+
new ItemStack(Items.CRYING_OBSIDIAN, 1)
90+
};
91+
92+
rec.addComponent("I", Blocks.IRON_BLOCK.getDefaultState());
93+
rec.addComponent("R", Blocks.REDSTONE_WIRE.getDefaultState());
8694

8795
return rec;
8896
});
@@ -99,7 +107,7 @@ public static void init() {
99107

100108
String nanoIsLazy = "miniaturization_recipes";
101109
MINIATURIZATION_RECIPES.makeRegistry(nanoIsLazy, () -> new RegistryBuilder<MiniaturizationRecipe>()
102-
.tagFolder(nanoIsLazy));
110+
.tagFolder(nanoIsLazy));
103111

104112
MINIATURIZATION_RECIPES.register(eventBus);
105113
}

src/main/java/com/robotgryphon/compactcrafting/crafting/CraftingHelper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import net.minecraft.util.math.AxisAlignedBB;
77
import net.minecraft.util.math.BlockPos;
88
import net.minecraft.world.IWorld;
9-
import net.minecraft.world.IWorldWriter;
10-
import net.minecraft.world.World;
11-
import net.minecraft.world.server.ServerWorld;
9+
10+
import java.util.Comparator;
11+
import java.util.List;
12+
import java.util.stream.Collectors;
1213

1314
public abstract class CraftingHelper {
1415

@@ -22,6 +23,8 @@ public static void deleteCraftingBlocks(IWorld world, AxisAlignedBB area) {
2223
// Remove blocks from the world
2324
BlockPos.getAllInBox(area)
2425
.filter(pos -> !world.isAirBlock(pos))
26+
.map(BlockPos::toImmutable)
27+
.sorted(Comparator.comparingInt(BlockPos::getY).reversed())
2528
.forEach(blockPos -> {
2629
world.destroyBlock(blockPos, false);
2730
world.addParticle(ParticleTypes.LARGE_SMOKE,

src/main/java/com/robotgryphon/compactcrafting/field/FieldHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
5-
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
5+
import com.robotgryphon.compactcrafting.blocks.FieldProjectorTile;
66
import com.robotgryphon.compactcrafting.core.BlockUpdateType;
77
import net.minecraft.block.BlockState;
88
import net.minecraft.util.math.AxisAlignedBB;
@@ -24,7 +24,6 @@ public static void checkBlockPlacement(IWorld world, BlockPos pos) {
2424
.toArray(BlockPos[]::new);
2525

2626
for (BlockPos p : potentials) {
27-
BlockState bs = world.getBlockState(p);
2827
FieldProjectorTile tile = (FieldProjectorTile) world.getTileEntity(p);
2928

3029
CompactCrafting.LOGGER.debug("Got a block placed near a projector: " + p.getCoordinatesAsString());
@@ -35,7 +34,7 @@ public static void checkBlockPlacement(IWorld world, BlockPos pos) {
3534
continue;
3635
}
3736

38-
tile.handleNearbyBlockUpdate(p, BlockUpdateType.PLACE);
37+
tile.handleNearbyBlockUpdate(pos, BlockUpdateType.PLACE);
3938
}
4039
}
4140

src/main/java/com/robotgryphon/compactcrafting/field/FieldProjection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public BlockPos getCenterPosition() {
2828
return center;
2929
}
3030

31-
public Optional<AxisAlignedBB> getBounds() {
31+
public AxisAlignedBB getBounds() {
3232
FieldProjectionSize size = this.size;
3333
BlockPos center = getCenterPosition();
3434
AxisAlignedBB bounds = new AxisAlignedBB(center).grow(size.getSize());
3535

36-
return Optional.of(bounds);
36+
return bounds;
3737
}
3838

3939
/**

src/main/java/com/robotgryphon/compactcrafting/recipes/IRecipeLayer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.robotgryphon.compactcrafting.recipes;
22

3-
import net.minecraft.block.BlockState;
43
import net.minecraft.util.math.AxisAlignedBB;
4+
import net.minecraft.util.math.vector.Vector3i;
55
import net.minecraft.world.IWorldReader;
66

77
import java.util.Map;
@@ -19,7 +19,17 @@ public interface IRecipeLayer {
1919
*/
2020
boolean matchesFieldLayer(IWorldReader world, AxisAlignedBB fieldLayer);
2121

22-
Map<String, BlockState> getComponents();
22+
int getVolume();
23+
24+
Vector3i getDimensions();
25+
26+
/**
27+
* Relative offset from center; if BlockPos.ZERO, center of this is assumed to be the center
28+
* of the layer.
29+
*
30+
* @return
31+
*/
32+
Vector3i getRelativeOffset();
2333

2434
Map<String, Integer> getComponentTotals();
2535
}

0 commit comments

Comments
 (0)