Skip to content

Commit

Permalink
Modem peripherals toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
neumond committed Jul 27, 2020
1 parent cd879b0 commit 4e11f98
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.*;

import static dan200.computercraft.shared.Capabilities.CAPABILITY_PERIPHERAL;
Expand Down Expand Up @@ -90,12 +91,22 @@ public Vec3d getPosition()
BlockPos pos = m_entity.getPos();
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}

@Override
public void togglePeripheralAccess()
{
if( !m_entity.requestToTogglePeripherals.getAndSet( true ) )
{
TickScheduler.schedule( m_entity );
}
}
}

private final WiredModemPeripheral[] modems = new WiredModemPeripheral[6];
private final SidedCaps<IPeripheral> modemCaps = SidedCaps.ofNonNull( this::getPeripheral );

private boolean m_peripheralAccessAllowed = false;
public AtomicBoolean requestToTogglePeripherals = new AtomicBoolean( false );
private final WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];

private boolean m_destroyed = false;
Expand Down Expand Up @@ -196,6 +207,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
// On server, we interacted if a peripheral was found
Set<String> oldPeriphNames = getConnectedPeripheralNames();
togglePeripheralAccess();
updateBlockState();
Set<String> periphNames = getConnectedPeripheralNames();

if( !Objects.equal( periphNames, oldPeriphNames ) )
Expand Down Expand Up @@ -262,7 +274,14 @@ public void blockTick()
{
if( getWorld().isRemote ) return;

if( m_modemState.pollChanged() ) updateBlockState();
boolean needUpdate = false;

if( requestToTogglePeripherals.getAndSet( false ) )
{
if( togglePeripheralAccess() ) needUpdate = true;
}
if( m_modemState.pollChanged() ) needUpdate = true;
if( needUpdate ) updateBlockState();

if( !m_connectionsFormed )
{
Expand Down Expand Up @@ -299,7 +318,7 @@ private void connectionsChanged()
}
}

private void togglePeripheralAccess()
private boolean togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
Expand All @@ -311,7 +330,7 @@ private void togglePeripheralAccess()
hasAny |= peripheral.hasPeripheral();
}

if( !hasAny ) return;
if( !hasAny ) return false;

m_peripheralAccessAllowed = true;
m_node.updatePeripherals( getConnectedPeripherals() );
Expand All @@ -323,8 +342,7 @@ private void togglePeripheralAccess()
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
}

updateBlockState();
return true;
}

private Set<String> getConnectedPeripheralNames()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public Map<String, IPeripheral> getRemotePeripherals()
protected abstract void attachPeripheral( String name, IPeripheral peripheral );

protected abstract void detachPeripheral( String name );

public void togglePeripheralAccess()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public final Object[] getNameLocal()
return local == null ? null : new Object[] { local };
}

@LuaFunction
public final void togglePeripherals()
{
modem.togglePeripheralAccess();
}

@Override
public void attach( @Nonnull IComputerAccess computer )
{
Expand Down

0 comments on commit 4e11f98

Please sign in to comment.