Skip to content

Commit

Permalink
fixed race condition in CC / air cannon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Oct 15, 2024
1 parent 4c21b6d commit 1b11589
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import javax.annotation.Nullable;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;

import static me.desht.pneumaticcraft.common.util.PneumaticCraftUtils.xlate;

Expand Down Expand Up @@ -134,6 +135,8 @@ public class AirCannonBlockEntity extends AbstractAirHandlingBlockEntity
public boolean insertingInventoryHasSpace = true;
private boolean gpsSlotChanged = true;
private FakePlayer fakePlayer = null;
private final AtomicBoolean firePending = new AtomicBoolean(false);
private final AtomicBoolean lastFireOK = new AtomicBoolean(false);

private static final int INVENTORY_SIZE = 2;
private static final int CANNON_SLOT = 0;
Expand Down Expand Up @@ -175,6 +178,10 @@ public void tickCommonPre() {
public void tickServer() {
super.tickServer();

if (firePending.get()) {
lastFireOK.set(fire());
}

updateTrackedItems();
updateTrackedTNT();

Expand Down Expand Up @@ -570,7 +577,7 @@ public void onNeighborBlockUpdate(BlockPos fromPos) {
super.onNeighborBlockUpdate(fromPos);
boolean isPowered = rsController.getCurrentRedstonePower() > 0;
if (isPowered && !wasPowered && rsController.shouldRun()) {
fire();
lastFireOK.set(fire());
}
}

Expand All @@ -593,8 +600,14 @@ private boolean inventoryCanCarry() {
});
}

private synchronized boolean fire() {
private synchronized boolean requestFire() {
firePending.set(true);
return true;
}

private boolean fire() {
Entity launchedEntity = getCloseEntityIfUpgraded();
firePending.set(false);
if (getPressure() >= PneumaticValues.MIN_PRESSURE_AIR_CANNON && (launchedEntity != null || !itemHandler.getStackInSlot(CANNON_SLOT).isEmpty())) {
float force = getForce();
//noinspection SuspiciousNameCombination
Expand Down Expand Up @@ -694,7 +707,16 @@ public Object[] call(Object[] args) {
public Object[] call(Object[] args) {
requireNoArgs(args);
// returns true if the fire succeeded.
return new Object[]{fire()};
return new Object[]{ requestFire() };
}
});

registry.registerLuaMethod(new LuaMethod("firedOK") {
@Override
public Object[] call(Object[] args) {
requireNoArgs(args);
// returns true if the fire succeeded.
return new Object[]{ lastFireOK.get() };
}
});

Expand Down

0 comments on commit 1b11589

Please sign in to comment.