Skip to content

Commit 9b2501c

Browse files
committed
Re-enable basic crafting check, start handling field block logic
1 parent 347772f commit 9b2501c

File tree

5 files changed

+123
-14
lines changed

5 files changed

+123
-14
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.robotgryphon.compactcrafting.blocks.tiles;
22

33
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
4+
import com.robotgryphon.compactcrafting.core.BlockUpdateType;
45
import com.robotgryphon.compactcrafting.field.FieldProjection;
56
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
67
import com.robotgryphon.compactcrafting.core.Registration;
@@ -111,11 +112,7 @@ public void tick() {
111112
return;
112113
}
113114

114-
if (fieldCheckTimeout > 0) {
115-
fieldCheckTimeout--;
116-
} else {
117-
doFieldCheck();
118-
}
115+
tickCrafting();
119116
}
120117

121118
/**
@@ -143,15 +140,20 @@ private void tickCrafting() {
143140
Optional<AxisAlignedBB> fieldBounds = fp.getBounds();
144141

145142
List<ItemEntity> enderPearls = getCatalystsInField(fieldBounds.get(), Items.ENDER_PEARL);
146-
147-
// We dropped an ender pearl in - are there any blocks in the field?
148-
// If so, we'll need to check the recipes -- but for now we just use it to
149-
// not delete the item if there's nothing in here
150-
if (CraftingHelper.hasBlocksInField(world, fieldBounds.get())) {
151-
CraftingHelper.deleteCraftingBlocks(world, fieldBounds.get());
152-
CraftingHelper.consumeCatalystItem(enderPearls.get(0), 1);
153-
154-
this.isCrafting = false;
143+
if(enderPearls.size() > 0) {
144+
// We dropped an ender pearl in - are there any blocks in the field?
145+
// If so, we'll need to check the recipes -- but for now we just use it to
146+
// not delete the item if there's nothing in here
147+
if (CraftingHelper.hasBlocksInField(world, fieldBounds.get())) {
148+
this.isCrafting = true;
149+
150+
if(!world.isRemote()) {
151+
CraftingHelper.deleteCraftingBlocks(world, fieldBounds.get());
152+
CraftingHelper.consumeCatalystItem(enderPearls.get(0), 1);
153+
}
154+
155+
this.isCrafting = false;
156+
}
155157
}
156158
}
157159

@@ -202,4 +204,15 @@ public AxisAlignedBB getRenderBoundingBox() {
202204
public Optional<FieldProjection> getField() {
203205
return this.field;
204206
}
207+
208+
/**
209+
* Called whenever a nearby block is changed near the field.
210+
* @param pos The position a block was updated at.
211+
*/
212+
public void handleNearbyBlockUpdate(BlockPos pos, BlockUpdateType updateType) {
213+
if(updateType == BlockUpdateType.UNKNOWN)
214+
return;
215+
216+
doFieldCheck();
217+
}
205218
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.robotgryphon.compactcrafting.core;
2+
3+
public enum BlockUpdateType {
4+
PLACE,
5+
REMOVE,
6+
UNKNOWN
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.robotgryphon.compactcrafting.events;
2+
3+
import com.robotgryphon.compactcrafting.CompactCrafting;
4+
import com.robotgryphon.compactcrafting.core.Registration;
5+
import com.robotgryphon.compactcrafting.field.FieldHelper;
6+
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
7+
import net.minecraft.block.BlockState;
8+
import net.minecraft.block.Blocks;
9+
import net.minecraft.util.math.AxisAlignedBB;
10+
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.world.IWorld;
12+
import net.minecraftforge.api.distmarker.Dist;
13+
import net.minecraftforge.eventbus.api.SubscribeEvent;
14+
import net.minecraftforge.fml.common.Mod;
15+
16+
import java.util.Set;
17+
import java.util.stream.Collectors;
18+
19+
@Mod.EventBusSubscriber(modid = CompactCrafting.MOD_ID)
20+
public class EventHandler {
21+
22+
@SubscribeEvent
23+
public static void onBlockPlaced(final net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent blockPlaced) {
24+
// Check if block is in or around a projector field
25+
26+
IWorld world = blockPlaced.getWorld();
27+
BlockPos pos = blockPlaced.getPos();
28+
29+
// We don't care about client worlds RN
30+
if(world.isRemote())
31+
return;
32+
33+
// Send the event position over to the field helper, so any nearby projectors can be notified
34+
FieldHelper.checkBlockPlacement(world, pos);
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.robotgryphon.compactcrafting.field;
2+
3+
import com.robotgryphon.compactcrafting.CompactCrafting;
4+
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
5+
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
6+
import com.robotgryphon.compactcrafting.core.BlockUpdateType;
7+
import com.robotgryphon.compactcrafting.core.Registration;
8+
import net.minecraft.block.BlockState;
9+
import net.minecraft.util.math.AxisAlignedBB;
10+
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.world.IWorld;
12+
import net.minecraft.world.gen.blockstateprovider.BlockStateProviderType;
13+
14+
import java.util.Set;
15+
import java.util.stream.Collectors;
16+
17+
/**
18+
* Provides utilities to help with projector field management.
19+
*/
20+
public abstract class FieldHelper {
21+
public static void checkBlockPlacement(IWorld world, BlockPos pos) {
22+
// Scan all blocks in the maximum field projection range (trying to find projectors)
23+
AxisAlignedBB scanBlocks = new AxisAlignedBB(pos).grow(FieldProjectionSize.maximum().getOffset());
24+
25+
Set<BlockPos> projectors = BlockPos.getAllInBox(scanBlocks)
26+
.filter(p -> !world.isAirBlock(p))
27+
.filter(p -> {
28+
BlockState bs = world.getBlockState(p);
29+
return bs.getBlock() instanceof FieldProjectorBlock;
30+
})
31+
.collect(Collectors.toSet());
32+
33+
if(!projectors.isEmpty()) {
34+
// We have projectors in the surrounding area, notify them to check their fields
35+
for(BlockPos projectorPos : projectors) {
36+
BlockState state = world.getBlockState(projectorPos);
37+
FieldProjectorTile tile = (FieldProjectorTile) world.getTileEntity(projectorPos);
38+
39+
// Not a field projector tile. Somehow.
40+
if(tile == null) {
41+
CompactCrafting.LOGGER.warn("Warning: Got a projector block but there was no field projector TE on the same position. Position: " + projectorPos.getCoordinatesAsString());
42+
continue;
43+
}
44+
45+
tile.handleNearbyBlockUpdate(pos, BlockUpdateType.PLACE);
46+
}
47+
}
48+
}
49+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ public int getSize() {
4242
public String getName() {
4343
return this.name;
4444
}
45+
46+
public static FieldProjectionSize maximum() {
47+
return LARGE;
48+
}
4549
}

0 commit comments

Comments
 (0)