From ff360c48aa149e9af17620293b5b1ed3b726765f Mon Sep 17 00:00:00 2001 From: Eutro <11538216+eutropius225@users.noreply.github.com> Date: Mon, 11 May 2020 15:30:44 +0100 Subject: [PATCH] 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 --- .../botania/common/item/lens/LensMagnet.java | 93 +++++++++++-------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/src/main/java/vazkii/botania/common/item/lens/LensMagnet.java b/src/main/java/vazkii/botania/common/item/lens/LensMagnet.java index ec0742c04..f8de80755 100644 --- a/src/main/java/vazkii/botania/common/item/lens/LensMagnet.java +++ b/src/main/java/vazkii/botania/common/item/lens/LensMagnet.java @@ -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 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); } }