Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Modem peripherals toggling #510

Open
wants to merge 1 commit into
base: mc-1.15.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,25 @@ protected void detachPeripheral( String name )
{
m_modem.detachPeripheral( name );
}

protected boolean enableSharing()
{
m_peripheral.attach( world, getPos(), getDirection() );
if( !m_peripheral.hasPeripheral() ) return false;

m_node.updatePeripherals( m_peripheral.toMap() );
updateBlockState();
return true;
}

protected void disableSharing()
{
m_peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
updateBlockState();
}
}

private boolean m_peripheralAccessAllowed;
private final WiredModemLocalPeripheral m_peripheral = new WiredModemLocalPeripheral( this::refreshPeripheral );

private boolean m_destroyed = false;
Expand Down Expand Up @@ -236,7 +252,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
{
super.onNeighbourTileEntityChange( neighbour );
if( !world.isRemote && m_peripheralAccessAllowed )
if( !world.isRemote && m_cable.isSharingPeripherals() )
{
Direction facing = getDirection();
if( getPos().offset( facing ).equals( neighbour ) ) refreshPeripheral();
Expand All @@ -261,7 +277,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
if( getWorld().isRemote ) return ActionResultType.SUCCESS;

String oldName = m_peripheral.getConnectedName();
togglePeripheralAccess();
m_cable.trySetSharingPeripherals( !m_cable.isSharingPeripherals() );
String newName = m_peripheral.getConnectedName();
if( !Objects.equal( newName, oldName ) )
{
Expand All @@ -284,15 +300,15 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac
public void read( @Nonnull CompoundNBT nbt )
{
super.read( nbt );
m_peripheralAccessAllowed = nbt.getBoolean( NBT_PERIPHERAL_ENABLED );
m_cable.forceSetSharingPeripherals( nbt.getBoolean( NBT_PERIPHERAL_ENABLED ) );
m_peripheral.read( nbt, "" );
}

@Nonnull
@Override
public CompoundNBT write( CompoundNBT nbt )
{
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_cable.isSharingPeripherals() );
m_peripheral.write( nbt, "" );
return super.write( nbt );
}
Expand All @@ -302,7 +318,7 @@ private void updateBlockState()
BlockState state = getBlockState();
CableModemVariant oldVariant = state.get( BlockCable.MODEM );
CableModemVariant newVariant = CableModemVariant
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_peripheralAccessAllowed );
.from( oldVariant.getFacing(), m_modem.getModemState().isOpen(), m_cable.isSharingPeripherals() );

if( oldVariant != newVariant )
{
Expand Down Expand Up @@ -331,7 +347,7 @@ public void blockTick()
m_connectionsFormed = true;

connectionsChanged();
if( m_peripheralAccessAllowed )
if( m_cable.isSharingPeripherals() )
{
m_peripheral.attach( world, pos, modemDirection );
updateConnectedPeripherals();
Expand Down Expand Up @@ -378,44 +394,23 @@ void modemChanged()

// If we can no longer attach peripherals, then detach any
// which may have existed
if( !canAttachPeripheral() && m_peripheralAccessAllowed )
if( !canAttachPeripheral() && m_cable.isSharingPeripherals() )
{
m_peripheralAccessAllowed = false;
m_cable.forceSetSharingPeripherals( false );
m_peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
markDirty();
updateBlockState();
}
}

private void togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
m_peripheral.attach( world, getPos(), getDirection() );
if( !m_peripheral.hasPeripheral() ) return;

m_peripheralAccessAllowed = true;
m_node.updatePeripherals( m_peripheral.toMap() );
}
else
{
m_peripheral.detach();

m_peripheralAccessAllowed = false;
m_node.updatePeripherals( Collections.emptyMap() );
}

updateBlockState();
}

private void updateConnectedPeripherals()
{
Map<String, IPeripheral> peripherals = m_peripheral.toMap();
if( peripherals.isEmpty() )
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
m_cable.forceSetSharingPeripherals( false );
updateBlockState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,38 @@ public Vec3d getPosition()
BlockPos pos = m_entity.getPos();
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}

protected boolean enableSharing()
{
boolean hasAny = false;
for( Direction facing : DirectionUtil.FACINGS )
{
WiredModemLocalPeripheral peripheral = m_entity.m_peripherals[facing.ordinal()];
peripheral.attach( m_entity.world, m_entity.getPos(), facing );
hasAny |= peripheral.hasPeripheral();
}

if( !hasAny ) return false;

m_entity.m_node.updatePeripherals( m_entity.getConnectedPeripherals() );

m_entity.updateBlockState();

return true;
}

protected void disableSharing()
{
for( WiredModemLocalPeripheral peripheral : m_entity.m_peripherals ) peripheral.detach();
m_entity.m_node.updatePeripherals( Collections.emptyMap() );

m_entity.updateBlockState();
}
}

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

private boolean m_peripheralAccessAllowed = false;
private final WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];

private boolean m_destroyed = false;
Expand Down Expand Up @@ -169,7 +195,7 @@ public void onNeighbourChange( @Nonnull BlockPos neighbour )
@Override
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
{
if( !world.isRemote && m_peripheralAccessAllowed )
if( !world.isRemote && m_element.isSharingPeripherals() )
{
for( Direction facing : DirectionUtil.FACINGS )
{
Expand All @@ -195,7 +221,7 @@ public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTrac

// On server, we interacted if a peripheral was found
Set<String> oldPeriphNames = getConnectedPeripheralNames();
togglePeripheralAccess();
m_element.trySetSharingPeripherals( !m_element.isSharingPeripherals() );
Set<String> periphNames = getConnectedPeripheralNames();

if( !Objects.equal( periphNames, oldPeriphNames ) )
Expand Down Expand Up @@ -228,23 +254,23 @@ private static void sendPeripheralChanges( PlayerEntity player, String kind, Col
public void read( @Nonnull CompoundNBT nbt )
{
super.read( nbt );
m_peripheralAccessAllowed = nbt.getBoolean( NBT_PERIPHERAL_ENABLED );
m_element.forceSetSharingPeripherals( nbt.getBoolean( NBT_PERIPHERAL_ENABLED ) );
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].read( nbt, Integer.toString( i ) );
}

@Nonnull
@Override
public CompoundNBT write( CompoundNBT nbt )
{
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_peripheralAccessAllowed );
nbt.putBoolean( NBT_PERIPHERAL_ENABLED, m_element.isSharingPeripherals() );
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].write( nbt, Integer.toString( i ) );
return super.write( nbt );
}

private void updateBlockState()
{
BlockState state = getBlockState();
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_peripheralAccessAllowed;
boolean modemOn = m_modemState.isOpen(), peripheralOn = m_element.isSharingPeripherals();
if( state.get( MODEM_ON ) == modemOn && state.get( PERIPHERAL_ON ) == peripheralOn ) return;

getWorld().setBlockState( getPos(), state.with( MODEM_ON, modemOn ).with( PERIPHERAL_ON, peripheralOn ) );
Expand All @@ -269,7 +295,7 @@ public void blockTick()
m_connectionsFormed = true;

connectionsChanged();
if( m_peripheralAccessAllowed )
if( m_element.isSharingPeripherals() )
{
for( Direction facing : DirectionUtil.FACINGS )
{
Expand Down Expand Up @@ -299,37 +325,9 @@ private void connectionsChanged()
}
}

private void togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
boolean hasAny = false;
for( Direction facing : DirectionUtil.FACINGS )
{
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
peripheral.attach( world, getPos(), facing );
hasAny |= peripheral.hasPeripheral();
}

if( !hasAny ) return;

m_peripheralAccessAllowed = true;
m_node.updatePeripherals( getConnectedPeripherals() );
}
else
{
m_peripheralAccessAllowed = false;

for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
}

updateBlockState();
}

private Set<String> getConnectedPeripheralNames()
{
if( !m_peripheralAccessAllowed ) return Collections.emptySet();
if( !m_element.isSharingPeripherals() ) return Collections.emptySet();

Set<String> peripherals = new HashSet<>( 6 );
for( WiredModemLocalPeripheral peripheral : m_peripherals )
Expand All @@ -342,7 +340,7 @@ private Set<String> getConnectedPeripheralNames()

private Map<String, IPeripheral> getConnectedPeripherals()
{
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
if( !m_element.isSharingPeripherals() ) return Collections.emptyMap();

Map<String, IPeripheral> peripherals = new HashMap<>( 6 );
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.extendMap( peripherals );
Expand All @@ -355,8 +353,7 @@ private void updateConnectedPeripherals()
if( peripherals.isEmpty() )
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
updateBlockState();
if( !m_element.trySetSharingPeripherals( false ) ) updateBlockState();
}

m_node.updatePeripherals( peripherals );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class WiredModemElement implements IWiredElement
{
private final IWiredNode node = new WiredNode( this );
private final Map<String, IPeripheral> remotePeripherals = new HashMap<>();
private boolean sharingPeripherals = false;

@Nonnull
@Override
Expand Down Expand Up @@ -61,4 +62,37 @@ public Map<String, IPeripheral> getRemotePeripherals()
protected abstract void attachPeripheral( String name, IPeripheral peripheral );

protected abstract void detachPeripheral( String name );

public boolean isSharingPeripherals()
{
return sharingPeripherals;
}

public void forceSetSharingPeripherals( boolean enabled )
{
// doesn't update Tile
sharingPeripherals = enabled;
}

public boolean trySetSharingPeripherals( boolean enabled )
{
// updates Tile if necessary
if( sharingPeripherals == enabled ) return true;
sharingPeripherals = enabled;

if( sharingPeripherals )
{
sharingPeripherals = enableSharing();
}
else
{
disableSharing();
}

return sharingPeripherals == enabled;
}

protected abstract boolean enableSharing();

protected abstract void disableSharing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender
{
private final WiredModemElement modem;

private final Map<IComputerAccess, ConcurrentMap<String, RemotePeripheralWrapper>> peripheralWrappers = new HashMap<>( 1 );

public WiredModemPeripheral( ModemState state, WiredModemElement modem )
Expand Down Expand Up @@ -195,6 +194,40 @@ public final Object[] getNameLocal()
return local == null ? null : new Object[] { local };
}

/**
* Returns current state of modem
* whether it shares locally connected peripherals to the network as named remote peripherals.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
*
* @return If the modem shares peripherals to the network.
* @cc.treturn boolean If the modem shares peripherals to the network.
*/
@LuaFunction
public final boolean isSharingPeripherals()
{
return modem.isSharingPeripherals();
}

/**
* Attempt to change peripheral sharing state of modem.
* That's what players can do by right-clicking the modem tile.
*
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless}
* returns false before calling it.</blockquote>
*
* @param enabled New state.
* @return Whether the setting of new state was successful.
* @cc.tparam boolean enabled New state.
* @cc.treturn boolean Whether the setting of new state was successful.
*/
@LuaFunction( mainThread = true )
public final boolean setSharingPeripherals( boolean enabled )
{
return modem.trySetSharingPeripherals( enabled );
}

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