|
1 | 1 | package com.robotgryphon.compactcrafting.blocks.tiles; |
2 | 2 |
|
| 3 | +import com.robotgryphon.compactcrafting.CompactCrafting; |
| 4 | +import com.robotgryphon.compactcrafting.blocks.FieldProjectorBlock; |
3 | 5 | import com.robotgryphon.compactcrafting.core.Registration; |
| 6 | +import net.minecraft.block.BlockState; |
| 7 | +import net.minecraft.entity.item.ItemEntity; |
| 8 | +import net.minecraft.entity.player.PlayerEntity; |
| 9 | +import net.minecraft.item.Item; |
| 10 | +import net.minecraft.item.ItemStack; |
| 11 | +import net.minecraft.item.Items; |
| 12 | +import net.minecraft.particles.ParticleTypes; |
| 13 | +import net.minecraft.tileentity.ITickableTileEntity; |
4 | 14 | import net.minecraft.tileentity.TileEntity; |
5 | 15 | import net.minecraft.tileentity.TileEntityType; |
| 16 | +import net.minecraft.util.Direction; |
| 17 | +import net.minecraft.util.math.AxisAlignedBB; |
| 18 | +import net.minecraft.util.math.BlockPos; |
| 19 | +import net.minecraft.util.math.vector.Vector3f; |
| 20 | +import net.minecraftforge.items.ItemHandlerHelper; |
6 | 21 |
|
7 | | -public class FieldProjectorTile extends TileEntity { |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +public class FieldProjectorTile extends TileEntity implements ITickableTileEntity { |
8 | 26 |
|
9 | 27 | public FieldProjectorTile() { |
10 | 28 | super(Registration.FIELD_PROJECTOR_TILE.get()); |
11 | 29 | } |
12 | 30 |
|
| 31 | + public Optional<BlockPos> getCenter() { |
| 32 | + BlockState bs = this.getBlockState(); |
| 33 | + Direction facing = bs.get(FieldProjectorBlock.FACING); |
| 34 | + |
| 35 | + |
| 36 | + Optional<BlockPos> location = getOppositeProjector(); |
| 37 | + if(location.isPresent()) { |
| 38 | + BlockPos opp = location.get(); |
| 39 | + int centerOffset = opp.manhattanDistance(pos); |
| 40 | + |
| 41 | + BlockPos center = pos.offset(facing, centerOffset / 2); |
| 42 | + return Optional.of(center); |
| 43 | + } else { |
| 44 | + // No center -- no found opposite |
| 45 | + return Optional.empty(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets the location of the opposite field projector. |
| 51 | + * @return |
| 52 | + */ |
| 53 | + public Optional<BlockPos> getOppositeProjector() { |
| 54 | + BlockState bs = this.getBlockState(); |
| 55 | + Direction facing = bs.get(FieldProjectorBlock.FACING); |
| 56 | + |
| 57 | + for(int i = 1; i <= 13; i++) { |
| 58 | + // check block exists in offset position |
| 59 | + BlockPos offsetInDirection = pos.offset(facing, i); |
| 60 | + BlockState stateInPos = world.getBlockState(offsetInDirection); |
| 61 | + |
| 62 | + if(stateInPos.getBlock() instanceof FieldProjectorBlock) { |
| 63 | + // We have a field projector |
| 64 | + Direction otherFpDirection = stateInPos.get(FieldProjectorBlock.FACING); |
| 65 | + if(otherFpDirection == facing.getOpposite()) |
| 66 | + return Optional.of(offsetInDirection); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return Optional.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + public Direction getProjectorSide() { |
| 74 | + BlockState bs = this.getBlockState(); |
| 75 | + Direction facing = bs.get(FieldProjectorBlock.FACING); |
| 76 | + |
| 77 | + return facing.getOpposite(); |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isMainProjector() { |
| 81 | + Direction side = getProjectorSide(); |
| 82 | + |
| 83 | + // We're the main projector if we're to the NORTH |
| 84 | + return side == Direction.NORTH; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void tick() { |
| 89 | + if(!isMainProjector()) |
| 90 | + return; |
| 91 | + |
| 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 | + |
| 98 | + searchForCatalyst(); |
| 99 | + } |
| 100 | + |
| 101 | + private void searchForCatalyst() { |
| 102 | + Optional<BlockPos> center = getCenter(); |
| 103 | + |
| 104 | + // No center area - no other projector present |
| 105 | + if(!center.isPresent()) |
| 106 | + return; |
| 107 | + |
| 108 | + BlockPos centerPos = center.get(); |
| 109 | + AxisAlignedBB itemSuckBounds = new AxisAlignedBB(centerPos).grow(2); |
| 110 | + |
| 111 | + try { |
| 112 | + List<ItemEntity> itemsInRange = world.getEntitiesWithinAABB(ItemEntity.class, itemSuckBounds); |
| 113 | + collectItems(centerPos, itemsInRange); |
| 114 | + } |
| 115 | + |
| 116 | + catch(NullPointerException npe) { |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void collectItems(BlockPos center, List<ItemEntity> itemsInRange) { |
| 122 | + itemsInRange.forEach(item -> { |
| 123 | + if(item.getItem().getItem() == Items.ENDER_PEARL) { |
| 124 | + PlayerEntity closestPlayer = world.getClosestPlayer(center.getX(), center.getY(), center.getZ(), 5, false); |
| 125 | + |
| 126 | + item.remove(false); |
| 127 | + |
| 128 | + ItemStack output = new ItemStack(Items.DIAMOND, 1); |
| 129 | + closestPlayer.addItemStackToInventory(output); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
13 | 133 | } |
0 commit comments