Skip to content

Commit d735ade

Browse files
committed
Implement FieldProjectionSize enum, use it for field logic and rendering
1 parent 786f2db commit d735ade

File tree

4 files changed

+129
-25
lines changed

4 files changed

+129
-25
lines changed

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

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

33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
5+
import com.robotgryphon.compactcrafting.core.FieldProjectionSize;
56
import com.robotgryphon.compactcrafting.core.Registration;
7+
import com.robotgryphon.compactcrafting.crafting.CraftingHelper;
68
import net.minecraft.block.BlockState;
79
import net.minecraft.entity.item.ItemEntity;
810
import net.minecraft.entity.player.PlayerEntity;
@@ -22,7 +24,9 @@
2224
import java.util.List;
2325
import java.util.Optional;
2426

25-
public class FieldProjectorTile extends TileEntity implements ITickableTileEntity {
27+
public class FieldProjectorTile extends TileEntity implements ITickableTileEntity {
28+
29+
private FieldProjectionSize projectionSize = FieldProjectionSize.MEDIUM;
2630

2731
public FieldProjectorTile() {
2832
super(Registration.FIELD_PROJECTOR_TILE.get());
@@ -34,7 +38,7 @@ public Optional<BlockPos> getCenter() {
3438

3539

3640
Optional<BlockPos> location = getOppositeProjector();
37-
if(location.isPresent()) {
41+
if (location.isPresent()) {
3842
BlockPos opp = location.get();
3943
int centerOffset = opp.manhattanDistance(pos);
4044

@@ -48,21 +52,22 @@ public Optional<BlockPos> getCenter() {
4852

4953
/**
5054
* Gets the location of the opposite field projector.
55+
*
5156
* @return
5257
*/
5358
public Optional<BlockPos> getOppositeProjector() {
5459
BlockState bs = this.getBlockState();
5560
Direction facing = bs.get(FieldProjectorBlock.FACING);
5661

57-
for(int i = 1; i <= 13; i++) {
62+
for (int i = 1; i <= 13; i++) {
5863
// check block exists in offset position
5964
BlockPos offsetInDirection = pos.offset(facing, i);
6065
BlockState stateInPos = world.getBlockState(offsetInDirection);
6166

62-
if(stateInPos.getBlock() instanceof FieldProjectorBlock) {
67+
if (stateInPos.getBlock() instanceof FieldProjectorBlock) {
6368
// We have a field projector
6469
Direction otherFpDirection = stateInPos.get(FieldProjectorBlock.FACING);
65-
if(otherFpDirection == facing.getOpposite())
70+
if (otherFpDirection == facing.getOpposite())
6671
return Optional.of(offsetInDirection);
6772
}
6873
}
@@ -84,49 +89,51 @@ public boolean isMainProjector() {
8489
return side == Direction.NORTH;
8590
}
8691

92+
public FieldProjectionSize getProjectionSize() {
93+
// TODO: Make this different depending on the spacing of the field projectors
94+
return FieldProjectionSize.MEDIUM;
95+
}
96+
8797
@Override
8898
public void tick() {
89-
if(!isMainProjector())
99+
if (!isMainProjector())
90100
return;
91101

92-
Optional<BlockPos> center = getCenter();
93-
if(center.isPresent()) {
94-
BlockPos c = center.get();
95-
world.addParticle(ParticleTypes.SMOKE, c.getX() + 0.5f, c.getY() + 0.5f, c.getZ() + 0.5f, 0.0D, 0.0D, 0.0D);
96-
}
97-
102+
// TODO: Check crafting state of projection field, update that state instead of doing manual work here
98103
searchForCatalyst();
99104
}
100105

101106
private void searchForCatalyst() {
102107
Optional<BlockPos> center = getCenter();
103108

104109
// No center area - no other projector present
105-
if(!center.isPresent())
110+
if (!center.isPresent())
106111
return;
107112

108113
BlockPos centerPos = center.get();
109-
AxisAlignedBB itemSuckBounds = new AxisAlignedBB(centerPos).grow(2);
114+
FieldProjectionSize size = getProjectionSize();
115+
AxisAlignedBB itemSuckBounds = new AxisAlignedBB(centerPos).grow(size.getSize());
110116

111117
try {
112118
List<ItemEntity> itemsInRange = world.getEntitiesWithinAABB(ItemEntity.class, itemSuckBounds);
113119
collectItems(centerPos, itemsInRange);
114-
}
115-
116-
catch(NullPointerException npe) {
120+
} catch (NullPointerException npe) {
117121

118122
}
119123
}
120124

121125
private void collectItems(BlockPos center, List<ItemEntity> itemsInRange) {
122126
itemsInRange.forEach(item -> {
123-
if(item.getItem().getItem() == Items.ENDER_PEARL) {
124-
PlayerEntity closestPlayer = world.getClosestPlayer(center.getX(), center.getY(), center.getZ(), 5, false);
127+
if (item.getItem().getItem() == Items.ENDER_PEARL) {
128+
129+
boolean consumedCatalyst = CraftingHelper.consumeCatalystItem(item, 1);
125130

126-
item.remove(false);
131+
if(consumedCatalyst) {
132+
PlayerEntity closestPlayer = world.getClosestPlayer(center.getX(), center.getY(), center.getZ(), 5, false);
127133

128-
ItemStack output = new ItemStack(Items.DIAMOND, 1);
129-
closestPlayer.addItemStackToInventory(output);
134+
ItemStack output = new ItemStack(Items.DIAMOND, 1);
135+
closestPlayer.addItemStackToInventory(output);
136+
}
130137
}
131138
});
132139
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock;
66
import com.robotgryphon.compactcrafting.blocks.tiles.FieldProjectorTile;
77
import com.robotgryphon.compactcrafting.core.Constants;
8+
import com.robotgryphon.compactcrafting.core.FieldProjectionSize;
89
import net.minecraft.block.BlockState;
910
import net.minecraft.client.Minecraft;
1011
import net.minecraft.client.renderer.*;
@@ -58,13 +59,16 @@ public void render(FieldProjectorTile tile, float partialTicks, MatrixStack matr
5859
Optional<BlockPos> c = tile.getCenter();
5960
if (c.isPresent()) {
6061
BlockPos center = c.get();
61-
AxisAlignedBB cube = new AxisAlignedBB(center).grow(2);
62+
FieldProjectionSize fieldProjectionSize = tile.getProjectionSize();
63+
int fieldSize = fieldProjectionSize.getSize();
64+
65+
AxisAlignedBB cube = new AxisAlignedBB(center).grow(fieldSize);
6266

6367
renderFaces(tile, matrixStack, buffers, cube, 0);
6468

6569
if (tile.isMainProjector()) {
66-
drawScanLines(tile, matrixStack, buffers, cube, 2);
67-
renderProjectionCube(tile, matrixStack, buffers, cube, 2);
70+
drawScanLines(tile, matrixStack, buffers, cube, fieldSize);
71+
renderProjectionCube(tile, matrixStack, buffers, cube, fieldSize);
6872
}
6973
}
7074
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.robotgryphon.compactcrafting.core;
2+
3+
public enum FieldProjectionSize {
4+
/**
5+
* 3x3x3 Crafting Field Size (Magnitude 1)
6+
*/
7+
SMALL(1, "small"),
8+
9+
/**
10+
* 5x5x5 Crafting Field Size (Magnitude 2)
11+
*/
12+
MEDIUM(2, "medium"),
13+
14+
/**
15+
* 7x7x7 Crafting Field Size (Magnitude 3)
16+
*/
17+
LARGE(3, "large");
18+
19+
private int size;
20+
private String name;
21+
FieldProjectionSize(int size, String name) {
22+
this.size = size;
23+
this.name = name;
24+
}
25+
26+
public int getSize() {
27+
return this.size;
28+
}
29+
30+
public String getName() {
31+
return this.name;
32+
}
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.robotgryphon.compactcrafting.crafting;
2+
3+
import net.minecraft.entity.item.ItemEntity;
4+
import net.minecraft.item.ItemStack;
5+
import net.minecraft.particles.ParticleTypes;
6+
import net.minecraft.util.math.AxisAlignedBB;
7+
import net.minecraft.util.math.BlockPos;
8+
import net.minecraft.world.IWorld;
9+
import net.minecraft.world.IWorldWriter;
10+
import net.minecraft.world.World;
11+
import net.minecraft.world.server.ServerWorld;
12+
13+
public abstract class CraftingHelper {
14+
15+
public static void deleteCraftingBlocks(IWorld world, AxisAlignedBB area) {
16+
// Remove blocks from the world
17+
BlockPos.getAllInBox(area)
18+
.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);
23+
});
24+
}
25+
26+
/**
27+
* Consumes a number of items from a stack in the world.
28+
*
29+
* @param item The item entity (stack) to remove items from.
30+
* @param count The number of items to remove.
31+
* @return True if there was enough items to remove; false otherwise.
32+
*/
33+
public static boolean consumeCatalystItem(ItemEntity item, int count) {
34+
ItemStack stack = item.getItem();
35+
36+
// Not enough items (tm?)
37+
if(stack.getCount() < count)
38+
return false;
39+
40+
// Delete the item entity; stack had exactly enough
41+
if(stack.getCount() == count) {
42+
item.remove();
43+
return true;
44+
}
45+
46+
// Remove items from stack and clone the entity
47+
stack.setCount(stack.getCount() - count);
48+
item.setItem(stack.copy());
49+
return true;
50+
}
51+
52+
// public static void setCraftingHologram(ServerWorld world, BlockPos center) {
53+
// // Create recipe hologram
54+
// world.setBlockState(center, Blockss.craftingHologram.getDefaultState());
55+
// TileEntityCraftingHologram teHologram = (TileEntityCraftingHologram) world.getTileEntity(center);
56+
// if(teHologram != null) {
57+
// teHologram.setRecipe(multiblockRecipe);
58+
// }
59+
// }
60+
}

0 commit comments

Comments
 (0)