Skip to content

Commit 898e69d

Browse files
committed
Improve field catalyst detection, fix issue with field rendering
- Improves catalyst detection that the field does not spawn particles if it's an air block - Updates catalyst code that field will not consume catalyst if no blocks are in field - Slightly improves catalyst count logic - Changes projector rendering to increase field of view and center on the crafting field, instead of the projectors themselves
1 parent d735ade commit 898e69d

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

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

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import net.minecraft.util.Direction;
1919
import net.minecraft.util.math.AxisAlignedBB;
2020
import net.minecraft.util.math.BlockPos;
21+
import net.minecraft.util.math.vector.Vector3d;
2122
import net.minecraft.util.math.vector.Vector3f;
23+
import net.minecraft.util.math.vector.Vector3i;
2224
import net.minecraftforge.items.ItemHandlerHelper;
2325

2426
import java.util.List;
2527
import java.util.Optional;
28+
import java.util.stream.Collectors;
2629

2730
public class FieldProjectorTile extends TileEntity implements ITickableTileEntity {
2831

@@ -99,42 +102,75 @@ public void tick() {
99102
if (!isMainProjector())
100103
return;
101104

102-
// TODO: Check crafting state of projection field, update that state instead of doing manual work here
103-
searchForCatalyst();
105+
Optional<AxisAlignedBB> fieldBounds = getFieldBounds();
106+
if(fieldBounds.isPresent()) {
107+
// TODO: Check crafting state of projection field, update that state instead of doing manual work here
108+
boolean hasCatalyst = hasCatalystInBounds(fieldBounds.get(), Items.ENDER_PEARL);
109+
110+
if(hasCatalyst) {
111+
List<ItemEntity> enderPearls = getCatalystsInField(fieldBounds.get(), Items.ENDER_PEARL);
112+
113+
// We dropped an ender pearl in - are there any blocks in the field?
114+
// If so, we'll need to check the recipes -- but for now we just use it to
115+
// not delete the item if there's nothing in here
116+
if(CraftingHelper.hasBlocksInField(world, fieldBounds.get())) {
117+
CraftingHelper.consumeCatalystItem(enderPearls.get(0), 1);
118+
CraftingHelper.deleteCraftingBlocks(world, fieldBounds.get());
119+
}
120+
}
121+
}
104122
}
105123

106-
private void searchForCatalyst() {
124+
private Optional<AxisAlignedBB> getFieldBounds() {
107125
Optional<BlockPos> center = getCenter();
108126

109127
// No center area - no other projector present
110128
if (!center.isPresent())
111-
return;
129+
return Optional.empty();
112130

113131
BlockPos centerPos = center.get();
114132
FieldProjectionSize size = getProjectionSize();
115-
AxisAlignedBB itemSuckBounds = new AxisAlignedBB(centerPos).grow(size.getSize());
133+
AxisAlignedBB bounds = new AxisAlignedBB(centerPos).grow(size.getSize());
116134

135+
return Optional.of(bounds);
136+
}
137+
138+
private List<ItemEntity> getCatalystsInField(AxisAlignedBB fieldBounds, Item itemFilter) {
139+
List<ItemEntity> itemsInRange = world.getEntitiesWithinAABB(ItemEntity.class, fieldBounds);
140+
return itemsInRange.stream()
141+
.filter(ise -> ise.getItem().getItem() == itemFilter)
142+
.collect(Collectors.toList());
143+
}
144+
145+
private boolean hasCatalystInBounds(AxisAlignedBB bounds, Item itemFilter) {
117146
try {
118-
List<ItemEntity> itemsInRange = world.getEntitiesWithinAABB(ItemEntity.class, itemSuckBounds);
119-
collectItems(centerPos, itemsInRange);
147+
List<ItemEntity> itemsInRange = getCatalystsInField(bounds, itemFilter);
148+
int matchedCatalysts = collectItems(itemsInRange);
149+
150+
return matchedCatalysts > 0;
120151
} catch (NullPointerException npe) {
121152

122153
}
123-
}
124154

125-
private void collectItems(BlockPos center, List<ItemEntity> itemsInRange) {
126-
itemsInRange.forEach(item -> {
127-
if (item.getItem().getItem() == Items.ENDER_PEARL) {
155+
return false;
156+
}
128157

129-
boolean consumedCatalyst = CraftingHelper.consumeCatalystItem(item, 1);
158+
private int collectItems(List<ItemEntity> itemsInRange) {
159+
return itemsInRange.stream()
160+
// .filter(ise -> ise.getItem().getItem() == item)
161+
.map(ItemEntity::getItem)
162+
.map(ItemStack::getCount)
163+
.mapToInt(Integer::intValue)
164+
.sum();
165+
}
130166

131-
if(consumedCatalyst) {
132-
PlayerEntity closestPlayer = world.getClosestPlayer(center.getX(), center.getY(), center.getZ(), 5, false);
167+
@Override
168+
public AxisAlignedBB getRenderBoundingBox() {
169+
Optional<AxisAlignedBB> field = this.getFieldBounds();
170+
if(field.isPresent()) {
171+
return field.get().grow(10);
172+
}
133173

134-
ItemStack output = new ItemStack(Items.DIAMOND, 1);
135-
closestPlayer.addItemStackToInventory(output);
136-
}
137-
}
138-
});
174+
return super.getRenderBoundingBox();
139175
}
140176
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212

1313
public abstract class CraftingHelper {
1414

15+
public static boolean hasBlocksInField(IWorld world, AxisAlignedBB area) {
16+
// Remove blocks from the world
17+
return BlockPos.getAllInBox(area)
18+
.anyMatch(p -> !world.isAirBlock(p));
19+
}
20+
1521
public static void deleteCraftingBlocks(IWorld world, AxisAlignedBB area) {
1622
// Remove blocks from the world
1723
BlockPos.getAllInBox(area)
24+
.filter(pos -> !world.isAirBlock(pos))
1825
.forEach(blockPos -> {
19-
world.destroyBlock(blockPos, false);
20-
world.addParticle(ParticleTypes.LARGE_SMOKE,
21-
blockPos.getX() + 0.5f, blockPos.getY() + 0.5f, blockPos.getZ() + 0.5f,
22-
10.0d, 0.5D, 0.5D);
26+
world.destroyBlock(blockPos, false);
27+
world.addParticle(ParticleTypes.LARGE_SMOKE,
28+
blockPos.getX() + 0.5f, blockPos.getY() + 0.5f, blockPos.getZ() + 0.5f,
29+
0d, 0.05D, 0D);
2330
});
2431
}
2532

0 commit comments

Comments
 (0)