|
18 | 18 | import net.minecraft.util.Direction; |
19 | 19 | import net.minecraft.util.math.AxisAlignedBB; |
20 | 20 | import net.minecraft.util.math.BlockPos; |
| 21 | +import net.minecraft.util.math.vector.Vector3d; |
21 | 22 | import net.minecraft.util.math.vector.Vector3f; |
| 23 | +import net.minecraft.util.math.vector.Vector3i; |
22 | 24 | import net.minecraftforge.items.ItemHandlerHelper; |
23 | 25 |
|
24 | 26 | import java.util.List; |
25 | 27 | import java.util.Optional; |
| 28 | +import java.util.stream.Collectors; |
26 | 29 |
|
27 | 30 | public class FieldProjectorTile extends TileEntity implements ITickableTileEntity { |
28 | 31 |
|
@@ -99,42 +102,75 @@ public void tick() { |
99 | 102 | if (!isMainProjector()) |
100 | 103 | return; |
101 | 104 |
|
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 | + } |
104 | 122 | } |
105 | 123 |
|
106 | | - private void searchForCatalyst() { |
| 124 | + private Optional<AxisAlignedBB> getFieldBounds() { |
107 | 125 | Optional<BlockPos> center = getCenter(); |
108 | 126 |
|
109 | 127 | // No center area - no other projector present |
110 | 128 | if (!center.isPresent()) |
111 | | - return; |
| 129 | + return Optional.empty(); |
112 | 130 |
|
113 | 131 | BlockPos centerPos = center.get(); |
114 | 132 | FieldProjectionSize size = getProjectionSize(); |
115 | | - AxisAlignedBB itemSuckBounds = new AxisAlignedBB(centerPos).grow(size.getSize()); |
| 133 | + AxisAlignedBB bounds = new AxisAlignedBB(centerPos).grow(size.getSize()); |
116 | 134 |
|
| 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) { |
117 | 146 | 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; |
120 | 151 | } catch (NullPointerException npe) { |
121 | 152 |
|
122 | 153 | } |
123 | | - } |
124 | 154 |
|
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 | + } |
128 | 157 |
|
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 | + } |
130 | 166 |
|
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 | + } |
133 | 173 |
|
134 | | - ItemStack output = new ItemStack(Items.DIAMOND, 1); |
135 | | - closestPlayer.addItemStackToInventory(output); |
136 | | - } |
137 | | - } |
138 | | - }); |
| 174 | + return super.getRenderBoundingBox(); |
139 | 175 | } |
140 | 176 | } |
0 commit comments