66import net .minecraft .util .math .BlockPos ;
77import net .minecraft .world .IWorldReader ;
88
9- import java .util .Optional ;
9+ import java .util .* ;
1010import java .util .stream .Stream ;
1111
1212/**
@@ -36,20 +36,58 @@ public static Optional<BlockPos> getCenterForSize(IWorldReader world, BlockPos i
3636 return Optional .of (center );
3737 }
3838
39- public static Optional <BlockPos > getOppositeForSize (IWorldReader world , BlockPos initial , FieldProjectionSize size ) {
40- Optional <BlockPos > center = getCenterForSize (world , initial , size );
41- if (!center .isPresent ())
42- return Optional .empty ();
39+ public static Optional <BlockPos > getCenterForSize (BlockPos initial , Direction facing , FieldProjectionSize size ) {
40+ BlockPos center = initial .offset (facing , size .getProjectorDistance () + 1 );
41+ return Optional .of (center );
42+ }
43+
44+ public static Optional <BlockPos > getOppositePositionForSize (BlockPos initial , Direction direction , FieldProjectionSize size ) {
45+ BlockPos center = initial .offset (direction , size .getProjectorDistance () + 1 );
46+ BlockPos opp = center .offset (direction , size .getProjectorDistance () + 1 );
47+
48+ return Optional .of (opp );
49+ }
50+
51+ public static Optional <BlockPos > getOppositePositionForSize (IWorldReader world , BlockPos initial , FieldProjectionSize size ) {
52+ Optional <Direction > facing = FieldProjectorBlock .getDirection (world , initial );
4353
44- Optional < Direction > direction = FieldProjectorBlock . getDirection ( world , initial );
45- if (! direction .isPresent ())
54+ // Initial wasn't a valid field projector, can't get direction to look in
55+ if (! facing .isPresent ())
4656 return Optional .empty ();
4757
48- BlockPos projectorLocationForDirection = getProjectorLocationForDirection (world , center .get (), direction .get (), size );
49- return Optional .of (projectorLocationForDirection );
58+ Direction fieldDirection = facing .get ();
59+ return getOppositePositionForSize (initial , fieldDirection , size );
60+ }
61+
62+ public static Optional <FieldProjectionSize > getClosestOppositeSize (IWorldReader world , BlockPos initial ) {
63+ for (FieldProjectionSize size : FieldProjectionSize .values ()) {
64+ if (hasProjectorOpposite (world , initial , size )) {
65+ return Optional .of (size );
66+ }
67+ }
68+
69+ return Optional .empty ();
70+ }
71+
72+ public static Optional <FieldProjectionSize > getClosestOppositeSize (IWorldReader world , BlockPos initial , Direction look ) {
73+ for (FieldProjectionSize size : FieldProjectionSize .values ()) {
74+ if (hasProjectorOpposite (world , initial , look , size )) {
75+ return Optional .of (size );
76+ }
77+ }
78+
79+ return Optional .empty ();
80+ }
81+
82+ public static Set <BlockPos > getProjectorLocationsForAxis (BlockPos center , Direction .Axis axis , FieldProjectionSize size ) {
83+ Direction posdir = Direction .getFacingFromAxis (Direction .AxisDirection .POSITIVE , axis );
84+ BlockPos posLocation = ProjectorHelper .getProjectorLocationForDirection (center , posdir , size );
85+ BlockPos negLocation = ProjectorHelper .getProjectorLocationForDirection (center , posdir .getOpposite (), size );
86+
87+ return new HashSet <>(Arrays .asList (posLocation , negLocation ));
5088 }
5189
52- public static BlockPos getProjectorLocationForDirection (IWorldReader world , BlockPos center , Direction direction , FieldProjectionSize size ) {
90+ public static BlockPos getProjectorLocationForDirection (BlockPos center , Direction direction , FieldProjectionSize size ) {
5391 BlockPos location = center .offset (direction , size .getProjectorDistance () + 1 );
5492 return location ;
5593 }
@@ -63,8 +101,8 @@ public static BlockPos getProjectorLocationForDirection(IWorldReader world, Bloc
63101 * @param size The field size to check.
64102 * @return
65103 */
66- public static boolean hasValidProjectorInDirection (IWorldReader world , BlockPos center , Direction direction , FieldProjectionSize size ) {
67- BlockPos location = getProjectorLocationForDirection (world , center , direction , size );
104+ public static boolean hasProjectorInDirection (IWorldReader world , BlockPos center , Direction direction , FieldProjectionSize size ) {
105+ BlockPos location = getProjectorLocationForDirection (center , direction , size );
68106 BlockState state = world .getBlockState (location );
69107
70108 if (state .getBlock () instanceof FieldProjectorBlock ) {
@@ -75,6 +113,20 @@ public static boolean hasValidProjectorInDirection(IWorldReader world, BlockPos
75113 return false ;
76114 }
77115
116+ public static boolean hasProjectorOpposite (IWorldReader world , BlockPos initial , FieldProjectionSize size ) {
117+ Optional <BlockPos > opp = getOppositePositionForSize (world , initial , size );
118+ return opp
119+ .map (possible -> world .getBlockState (possible ).getBlock () instanceof FieldProjectorBlock )
120+ .orElse (false );
121+ }
122+
123+ public static boolean hasProjectorOpposite (IWorldReader world , BlockPos initial , Direction look , FieldProjectionSize size ) {
124+ Optional <BlockPos > opp = getOppositePositionForSize (initial , look , size );
125+ return opp
126+ .map (possible -> world .getBlockState (possible ).getBlock () instanceof FieldProjectorBlock )
127+ .orElse (false );
128+ }
129+
78130 /**
79131 * Checks an axis to see if, given a field size, there are two valid projectors.
80132 *
@@ -91,12 +143,12 @@ public static boolean checkAxisForValidProjectors(IWorldReader world, BlockPos c
91143 Direction checkDirection = Direction .getFacingFromAxisDirection (primaryAxis , Direction .AxisDirection .POSITIVE );
92144
93145 // Do we have a valid projector in the first direction?
94- boolean posValid = hasValidProjectorInDirection (world , center , checkDirection , fieldSize );
146+ boolean posValid = hasProjectorInDirection (world , center , checkDirection , fieldSize );
95147 if (!posValid )
96148 return false ;
97149
98150 // What about the negative direction?
99- boolean negValid = hasValidProjectorInDirection (world , center , checkDirection .getOpposite (), fieldSize );
151+ boolean negValid = hasProjectorInDirection (world , center , checkDirection .getOpposite (), fieldSize );
100152 if (!negValid )
101153 return false ;
102154
@@ -124,10 +176,12 @@ public static boolean checkProjectorsValid(IWorldReader world, BlockPos center,
124176
125177 public static Stream <BlockPos > getValidOppositePositions (IWorldReader world , BlockPos initial ) {
126178 Stream <BlockPos > validOpposites = Stream .of (FieldProjectionSize .values ())
127- .map (s -> getOppositeForSize (world , initial , s ))
179+ .map (s -> getOppositePositionForSize (world , initial , s ))
128180 .filter (Optional ::isPresent )
129181 .map (Optional ::get );
130182
131183 return validOpposites ;
132184 }
185+
186+
133187}
0 commit comments