Make Magnet Lens code better but actually worse if you're a masochist. (#3241)

* Make Magnet Lens code better but actually worse if you're a masochist.

This should preserve the peculiarities of the previous logic, while fixing the unreported bug where bursts not fired from Mana Spreaders would not magnetize block around the origin.

* It's 2020 and I still can't spell "receive".

* Wow okay my bad we like fancy curly braces here.

* Fix format violations.

* Magnet lens demagnetizes if the magnetized position is invalid.

* Re-magnetize properly, fix possible edge case
This commit is contained in:
Eutro authored and GitHub committed 2020-05-11 09:30:44 -05:00
1 parent 249681dc94
commit ff360c48aa
1 file changed
+53 -40
@@ -17,6 +17,8 @@ import vazkii.botania.api.internal.IManaBurst;
import vazkii.botania.api.mana.IManaReceiver;
import vazkii.botania.common.core.helper.Vector3;
import java.util.function.Predicate;
public class LensMagnet extends Lens {
private static final String TAG_MAGNETIZED = "botania:magnetized";
@@ -30,50 +32,61 @@ public class LensMagnet extends Lens {
boolean magnetized = entity.getPersistentData().contains(TAG_MAGNETIZED);
int range = 3;
magnetize: {
for (BlockPos pos : BlockPos.getAllInBoxMutable(basePos.add(-range, -range, -range),
basePos.add(range, range, range))) {
if (entity.world.getTileEntity(pos) instanceof IManaReceiver) {
TileEntity tile = entity.world.getTileEntity(pos);
BlockPos source = burst.getBurstSourceBlockPos();
final boolean sourceless = source.getY() == -1;
if (magnetized) {
int magX = entity.getPersistentData().getInt(TAG_MAGNETIZED_X);
int magY = entity.getPersistentData().getInt(TAG_MAGNETIZED_Y);
int magZ = entity.getPersistentData().getInt(TAG_MAGNETIZED_Z);
if (tile.getPos().getX() != magX || tile.getPos().getY() != magY || tile.getPos().getZ() != magZ) {
continue;
}
}
IManaReceiver receiver = (IManaReceiver) tile;
BlockPos srcCoords = burst.getBurstSourceBlockPos();
if (tile.getPos().distanceSq(srcCoords) > 9 && receiver.canReceiveManaFromBursts() && !receiver.isFull()) {
Vector3 burstVec = Vector3.fromEntity(entity);
Vector3 tileVec = Vector3.fromTileEntityCenter(tile).add(0, -0.1, 0);
Vector3 motionVec = new Vector3(entity.getMotion());
Vector3 normalMotionVec = motionVec.normalize();
Vector3 magnetVec = tileVec.subtract(burstVec).normalize();
Vector3 differenceVec = normalMotionVec.subtract(magnetVec).multiply(motionVec.mag() * 0.1);
Vector3 finalMotionVec = motionVec.subtract(differenceVec);
if (!magnetized) {
finalMotionVec = finalMotionVec.multiply(0.75);
entity.getPersistentData().putBoolean(TAG_MAGNETIZED, true);
entity.getPersistentData().putInt(TAG_MAGNETIZED_X, tile.getPos().getX());
entity.getPersistentData().putInt(TAG_MAGNETIZED_Y, tile.getPos().getY());
entity.getPersistentData().putInt(TAG_MAGNETIZED_Z, tile.getPos().getZ());
}
burst.setBurstMotion(finalMotionVec.x, finalMotionVec.y, finalMotionVec.z);
break magnetize;
}
}
Predicate<TileEntity> predicate = tile -> tile instanceof IManaReceiver
&& (sourceless || tile.getPos().distanceSq(source) > 9)
&& ((IManaReceiver) tile).canReceiveManaFromBursts()
&& !((IManaReceiver) tile).isFull();
TileEntity tile = null;
if (magnetized) {
tile = entity.world.getTileEntity(new BlockPos(
entity.getPersistentData().getInt(TAG_MAGNETIZED_X),
entity.getPersistentData().getInt(TAG_MAGNETIZED_Y),
entity.getPersistentData().getInt(TAG_MAGNETIZED_Z)
));
if (!predicate.test(tile)) {
tile = null;
entity.getPersistentData().remove(TAG_MAGNETIZED);
magnetized = false;
}
}
if (!magnetized) {
for (BlockPos pos : BlockPos.getAllInBoxMutable(basePos.add(-range, -range, -range),
basePos.add(range, range, range))) {
tile = entity.world.getTileEntity(pos);
if (predicate.test(tile)) {
break;
}
tile = null;
}
}
if (tile == null) {
return;
}
Vector3 burstVec = Vector3.fromEntity(entity);
Vector3 tileVec = Vector3.fromTileEntityCenter(tile).add(0, -0.1, 0);
Vector3 motionVec = new Vector3(entity.getMotion());
Vector3 normalMotionVec = motionVec.normalize();
Vector3 magnetVec = tileVec.subtract(burstVec).normalize();
Vector3 differenceVec = normalMotionVec.subtract(magnetVec).multiply(motionVec.mag() * 0.1);
Vector3 finalMotionVec = motionVec.subtract(differenceVec);
if (!magnetized) {
finalMotionVec = finalMotionVec.multiply(0.75);
entity.getPersistentData().putBoolean(TAG_MAGNETIZED, true);
entity.getPersistentData().putInt(TAG_MAGNETIZED_X, tile.getPos().getX());
entity.getPersistentData().putInt(TAG_MAGNETIZED_Y, tile.getPos().getY());
entity.getPersistentData().putInt(TAG_MAGNETIZED_Z, tile.getPos().getZ());
}
burst.setBurstMotion(finalMotionVec.x, finalMotionVec.y, finalMotionVec.z);
}
}