1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-04 23:53:01 +00:00

Make monitors non-ticking

- Convert terminals from a polling-based system to a more event-driven
   one: they now accept an onChanged callback, which marks the parent as
   dirty.
 - Schedule ticks when monitors are marked as dirty.
 - Add several missing @Overrides. This has nothing to do with the rest
   of the changes, but I'm bad at good git practice.
This commit is contained in:
SquidDev
2019-01-20 15:35:48 +00:00
parent 8a7e651c99
commit 83b01d35eb
13 changed files with 137 additions and 102 deletions

View File

@@ -16,6 +16,7 @@ public interface ILuaAPI extends dan200.computercraft.api.lua.ILuaAPI
{ {
void advance( double v ); void advance( double v );
@Override
default void update() default void update()
{ {
advance( 0.05 ); advance( 0.05 );

View File

@@ -26,12 +26,14 @@ public class ResourceQueue<T extends Resource<T>> extends ResourceGroup<T>
{ {
} }
@Override
public synchronized void shutdown() public synchronized void shutdown()
{ {
super.shutdown(); super.shutdown();
pending.clear(); pending.clear();
} }
@Override
public synchronized boolean queue( Supplier<T> resource ) public synchronized boolean queue( Supplier<T> resource )
{ {
if( !active ) return false; if( !active ) return false;
@@ -40,6 +42,7 @@ public class ResourceQueue<T extends Resource<T>> extends ResourceGroup<T>
return true; return true;
} }
@Override
public synchronized void release( T resource ) public synchronized void release( T resource )
{ {
super.release( resource ); super.release( resource );

View File

@@ -239,6 +239,7 @@ public class HttpRequest extends Resource<HttpRequest>
if( tryClose() ) environment.queueEvent( SUCCESS_EVENT, new Object[] { address, object } ); if( tryClose() ) environment.queueEvent( SUCCESS_EVENT, new Object[] { address, object } );
} }
@Override
protected void dispose() protected void dispose()
{ {
super.dispose(); super.dispose();

View File

@@ -29,11 +29,18 @@ public class Terminal
private final Palette m_palette; private final Palette m_palette;
private boolean m_changed; private boolean m_changed;
private final Runnable onChanged;
public Terminal( int width, int height ) public Terminal( int width, int height )
{
this( width, height, null );
}
public Terminal( int width, int height, Runnable changedCallback )
{ {
m_width = width; m_width = width;
m_height = height; m_height = height;
this.onChanged = changedCallback;
m_cursorColour = 0; m_cursorColour = 0;
m_cursorBackgroundColour = 15; m_cursorBackgroundColour = 15;
@@ -65,7 +72,7 @@ public class Terminal
m_cursorY = 0; m_cursorY = 0;
m_cursorBlink = false; m_cursorBlink = false;
clear(); clear();
m_changed = true; setChanged();
m_palette.resetColours(); m_palette.resetColours();
} }
@@ -122,7 +129,7 @@ public class Terminal
m_backgroundColour[i].write( oldBackgroundColour[i] ); m_backgroundColour[i].write( oldBackgroundColour[i] );
} }
} }
m_changed = true; setChanged();
} }
public void setCursorPos( int x, int y ) public void setCursorPos( int x, int y )
@@ -131,7 +138,7 @@ public class Terminal
{ {
m_cursorX = x; m_cursorX = x;
m_cursorY = y; m_cursorY = y;
m_changed = true; setChanged();
} }
} }
@@ -140,7 +147,7 @@ public class Terminal
if( m_cursorBlink != blink ) if( m_cursorBlink != blink )
{ {
m_cursorBlink = blink; m_cursorBlink = blink;
m_changed = true; setChanged();
} }
} }
@@ -149,7 +156,7 @@ public class Terminal
if( m_cursorColour != colour ) if( m_cursorColour != colour )
{ {
m_cursorColour = colour; m_cursorColour = colour;
m_changed = true; setChanged();
} }
} }
@@ -158,7 +165,7 @@ public class Terminal
if( m_cursorBackgroundColour != colour ) if( m_cursorBackgroundColour != colour )
{ {
m_cursorBackgroundColour = colour; m_cursorBackgroundColour = colour;
m_changed = true; setChanged();
} }
} }
@@ -201,7 +208,7 @@ public class Terminal
m_text[y].write( text, x ); m_text[y].write( text, x );
m_textColour[y].write( textColour, x ); m_textColour[y].write( textColour, x );
m_backgroundColour[y].write( backgroundColour, x ); m_backgroundColour[y].write( backgroundColour, x );
m_changed = true; setChanged();
} }
} }
@@ -214,7 +221,7 @@ public class Terminal
m_text[y].write( text, x ); m_text[y].write( text, x );
m_textColour[y].fill( base16.charAt( m_cursorColour ), x, x + text.length() ); m_textColour[y].fill( base16.charAt( m_cursorColour ), x, x + text.length() );
m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ), x, x + text.length() ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ), x, x + text.length() );
m_changed = true; setChanged();
} }
} }
@@ -244,7 +251,7 @@ public class Terminal
m_text = newText; m_text = newText;
m_textColour = newTextColour; m_textColour = newTextColour;
m_backgroundColour = newBackgroundColour; m_backgroundColour = newBackgroundColour;
m_changed = true; setChanged();
} }
} }
@@ -256,7 +263,7 @@ public class Terminal
m_textColour[y].fill( base16.charAt( m_cursorColour ) ); m_textColour[y].fill( base16.charAt( m_cursorColour ) );
m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) );
} }
m_changed = true; setChanged();
} }
public synchronized void clearLine() public synchronized void clearLine()
@@ -267,7 +274,7 @@ public class Terminal
m_text[y].fill( ' ' ); m_text[y].fill( ' ' );
m_textColour[y].fill( base16.charAt( m_cursorColour ) ); m_textColour[y].fill( base16.charAt( m_cursorColour ) );
m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) );
m_changed = true; setChanged();
} }
} }
@@ -285,7 +292,7 @@ public class Terminal
m_text[y].write( text ); m_text[y].write( text );
m_textColour[y].write( textColour ); m_textColour[y].write( textColour );
m_backgroundColour[y].write( backgroundColour ); m_backgroundColour[y].write( backgroundColour );
m_changed = true; setChanged();
} }
public synchronized TextBuffer getTextColourLine( int y ) public synchronized TextBuffer getTextColourLine( int y )
@@ -306,17 +313,23 @@ public class Terminal
return null; return null;
} }
public boolean getChanged() /**
* @deprecated All {@code *Changed()} methods are deprecated: one should pass in a callback
* instead.
*/
@Deprecated
public final boolean getChanged()
{ {
return m_changed; return m_changed;
} }
public void setChanged() public final void setChanged()
{ {
m_changed = true; m_changed = true;
if( onChanged != null ) onChanged.run();
} }
public void clearChanged() public final void clearChanged()
{ {
m_changed = false; m_changed = false;
} }
@@ -371,6 +384,6 @@ public class Terminal
{ {
m_palette.readFromNBT( nbt ); m_palette.readFromNBT( nbt );
} }
m_changed = true; setChanged();
} }
} }

View File

@@ -22,19 +22,14 @@ public class ClientTerminal implements ITerminal
m_terminalChanged = false; m_terminalChanged = false;
} }
public void update()
{
if( m_terminal != null )
{
m_terminalChanged |= m_terminal.getChanged();
m_terminal.clearChanged();
}
}
public boolean pollTerminalChanged() public boolean pollTerminalChanged()
{ {
boolean changed = m_terminalChanged; boolean changed = m_terminalChanged;
m_terminalChanged = false; m_terminalChanged = false;
Terminal terminal = m_terminal;
if( terminal != null ) terminal.clearChanged();
return changed; return changed;
} }
@@ -71,7 +66,7 @@ public class ClientTerminal implements ITerminal
{ {
if( m_terminal == null ) if( m_terminal == null )
{ {
m_terminal = new Terminal( width, height ); m_terminal = new Terminal( width, height, () -> m_terminalChanged = true );
m_terminalChanged = true; m_terminalChanged = true;
} }
else else

View File

@@ -9,35 +9,33 @@ package dan200.computercraft.shared.common;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import java.util.concurrent.atomic.AtomicBoolean;
public class ServerTerminal implements ITerminal public class ServerTerminal implements ITerminal
{ {
private final boolean m_colour; private final boolean m_colour;
private Terminal m_terminal; private Terminal m_terminal;
private boolean m_terminalChanged; private final AtomicBoolean m_terminalChanged = new AtomicBoolean( false );
private boolean m_terminalChangedLastFrame; private boolean m_terminalChangedLastFrame = false;
public ServerTerminal( boolean colour ) public ServerTerminal( boolean colour )
{ {
m_colour = colour; m_colour = colour;
m_terminal = null; m_terminal = null;
m_terminalChanged = false;
m_terminalChangedLastFrame = false;
} }
public ServerTerminal( boolean colour, int terminalWidth, int terminalHeight ) public ServerTerminal( boolean colour, int terminalWidth, int terminalHeight )
{ {
m_colour = colour; m_colour = colour;
m_terminal = new Terminal( terminalWidth, terminalHeight ); m_terminal = new Terminal( terminalWidth, terminalHeight, this::markTerminalChanged );
m_terminalChanged = false;
m_terminalChangedLastFrame = false;
} }
public void resize( int width, int height ) protected void resize( int width, int height )
{ {
if( m_terminal == null ) if( m_terminal == null )
{ {
m_terminal = new Terminal( width, height ); m_terminal = new Terminal( width, height, this::markTerminalChanged );
m_terminalChanged = true; markTerminalChanged();
} }
else else
{ {
@@ -50,23 +48,21 @@ public class ServerTerminal implements ITerminal
if( m_terminal != null ) if( m_terminal != null )
{ {
m_terminal = null; m_terminal = null;
m_terminalChanged = true; markTerminalChanged();
} }
} }
protected void markTerminalChanged() protected void markTerminalChanged()
{ {
m_terminalChanged = true; m_terminalChanged.set( true );
} }
public void update() public void update()
{ {
m_terminalChangedLastFrame = m_terminalChanged || (m_terminal != null && m_terminal.getChanged()); Terminal terminal = m_terminal;
if( m_terminal != null ) if( terminal != null ) terminal.clearChanged();
{
m_terminal.clearChanged(); m_terminalChangedLastFrame = m_terminalChanged.getAndSet( false );
}
m_terminalChanged = false;
} }
public boolean hasTerminalChanged() public boolean hasTerminalChanged()

View File

@@ -32,10 +32,8 @@ public class ClientComputer extends ClientTerminal implements IComputer
m_instanceID = instanceID; m_instanceID = instanceID;
} }
@Override
public void update() public void update()
{ {
super.update();
m_changedLastFrame = m_changed; m_changedLastFrame = m_changed;
m_changed = false; m_changed = false;
} }

View File

@@ -7,7 +7,7 @@
package dan200.computercraft.shared.peripheral.common; package dan200.computercraft.shared.peripheral.common;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.common.BlockDirectional; import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
@@ -39,7 +39,7 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class BlockPeripheral extends BlockDirectional public class BlockPeripheral extends BlockGeneric
{ {
public static class Properties public static class Properties
{ {
@@ -466,11 +466,12 @@ public class BlockPeripheral extends BlockDirectional
case DiskDrive: case DiskDrive:
case Printer: case Printer:
{ {
if( stack.hasDisplayName() && tile instanceof ITilePeripheral ) EnumFacing dir = DirectionUtil.fromEntityRot( player );
if( stack.hasDisplayName() && tile instanceof TilePeripheralBase )
{ {
ITilePeripheral peripheral = (ITilePeripheral) tile; TilePeripheralBase peripheral = (TilePeripheralBase) tile;
peripheral.setLabel( stack.getDisplayName() ); peripheral.setLabel( stack.getDisplayName() );
peripheral.setDirection( DirectionUtil.fromEntityRot( player ) ); peripheral.setDirection( dir );
} }
break; break;
} }
@@ -558,14 +559,18 @@ public class BlockPeripheral extends BlockDirectional
@Override @Override
@Deprecated @Deprecated
@Nonnull @Nonnull
public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess world, BlockPos pos )
{ {
if( getPeripheralType( state ) == PeripheralType.WirelessModem ) if( getPeripheralType( state ) == PeripheralType.WirelessModem )
{ {
return ModemBounds.getBounds( getDirection( source, pos ) ); TileEntity tile = world.getTileEntity( pos );
if( tile instanceof TileWirelessModem )
{
return ModemBounds.getBounds( ((TileWirelessModem) tile).getDirection() );
}
} }
return super.getBoundingBox( state, source, pos ); return super.getBoundingBox( state, world, pos );
} }
@Override @Override

View File

@@ -6,13 +6,12 @@
package dan200.computercraft.shared.peripheral.common; package dan200.computercraft.shared.peripheral.common;
import dan200.computercraft.shared.common.IDirectionalTile;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
/** /**
* The tile for {@link BlockPeripheral}. * The tile for {@link BlockPeripheral}.
*/ */
public interface ITilePeripheral extends IDirectionalTile public interface ITilePeripheral
{ {
PeripheralType getPeripheralType(); PeripheralType getPeripheralType();

View File

@@ -73,6 +73,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe
return null; return null;
} }
@Override
public void setLabel( String label ) public void setLabel( String label )
{ {
m_label = label; m_label = label;

View File

@@ -8,12 +8,16 @@ package dan200.computercraft.shared.peripheral.monitor;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.common.ServerTerminal; import dan200.computercraft.shared.common.ServerTerminal;
import dan200.computercraft.shared.util.TickScheduler;
import java.util.concurrent.atomic.AtomicBoolean;
public class ServerMonitor extends ServerTerminal public class ServerMonitor extends ServerTerminal
{ {
private final TileMonitor origin; private final TileMonitor origin;
private int textScale = 2; private int textScale = 2;
private boolean resized; private final AtomicBoolean resized = new AtomicBoolean( false );
private final AtomicBoolean changed = new AtomicBoolean( false );
public ServerMonitor( boolean colour, TileMonitor origin ) public ServerMonitor( boolean colour, TileMonitor origin )
{ {
@@ -41,10 +45,28 @@ public class ServerMonitor extends ServerTerminal
if( oldWidth != termWidth || oldHeight != termHeight ) if( oldWidth != termWidth || oldHeight != termHeight )
{ {
getTerminal().clear(); getTerminal().clear();
resized = true; resized.set( true );
markChanged();
} }
} }
@Override
protected void markTerminalChanged()
{
super.markTerminalChanged();
markChanged();
}
private void markChanged()
{
if( !changed.getAndSet( true ) ) TickScheduler.schedule( origin );
}
protected void clearChanged()
{
changed.set( false );
}
public int getTextScale() public int getTextScale()
{ {
return textScale; return textScale;
@@ -57,14 +79,14 @@ public class ServerMonitor extends ServerTerminal
rebuild(); rebuild();
} }
public synchronized boolean pollResized() public boolean pollResized()
{ {
if( resized ) return resized.getAndSet( false );
{ }
resized = false;
return true;
}
return false; public boolean pollTerminalChanged()
{
update();
return hasTerminalChanged();
} }
} }

View File

@@ -11,15 +11,20 @@ import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.common.ServerTerminal; import dan200.computercraft.shared.common.ServerTerminal;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.BlockPeripheral; import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.peripheral.common.IPeripheralTile;
import dan200.computercraft.shared.peripheral.common.ITilePeripheral;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
@@ -28,7 +33,7 @@ import javax.annotation.Nonnull;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class TileMonitor extends TilePeripheralBase public class TileMonitor extends TileGeneric implements ITilePeripheral, IPeripheralTile
{ {
// Statics // Statics
@@ -78,6 +83,7 @@ public class TileMonitor extends TilePeripheralBase
super.onLoad(); super.onLoad();
m_advanced = getBlockState().getValue( BlockPeripheral.Properties.VARIANT ) m_advanced = getBlockState().getValue( BlockPeripheral.Properties.VARIANT )
.getPeripheralType() == PeripheralType.AdvancedMonitor; .getPeripheralType() == PeripheralType.AdvancedMonitor;
world.scheduleUpdate( getPos(), getBlockType(), 0 );
} }
@Override @Override
@@ -146,44 +152,32 @@ public class TileMonitor extends TilePeripheralBase
} }
@Override @Override
public void update() public void updateTick()
{ {
super.update(); if( m_xIndex != 0 || m_yIndex != 0 || m_serverMonitor == null ) return;
if( !getWorld().isRemote ) m_serverMonitor.clearChanged();
if( m_serverMonitor.pollResized() )
{ {
if( m_xIndex == 0 && m_yIndex == 0 && m_serverMonitor != null ) for( int x = 0; x < m_width; x++ )
{ {
if( m_serverMonitor.pollResized() ) for( int y = 0; y < m_height; y++ )
{ {
for( int x = 0; x < m_width; x++ ) TileMonitor monitor = getNeighbour( x, y );
{ if( monitor == null ) continue;
for( int y = 0; y < m_height; y++ )
{
TileMonitor monitor = getNeighbour( x, y );
if( monitor == null ) continue;
for( IComputerAccess computer : monitor.m_computers ) for( IComputerAccess computer : monitor.m_computers )
{ {
computer.queueEvent( "monitor_resize", new Object[] { computer.queueEvent( "monitor_resize", new Object[] {
computer.getAttachmentName() computer.getAttachmentName()
} ); } );
}
}
} }
} }
}
}
m_serverMonitor.update(); if( m_serverMonitor.pollTerminalChanged() ) updateBlock();
if( m_serverMonitor.hasTerminalChanged() ) updateBlock();
}
}
else
{
if( m_xIndex == 0 && m_yIndex == 0 && m_clientMonitor != null )
{
m_clientMonitor.update();
}
}
} }
// IPeripheralTile implementation // IPeripheralTile implementation
@@ -197,6 +191,12 @@ public class TileMonitor extends TilePeripheralBase
return m_peripheral; return m_peripheral;
} }
@Override
public PeripheralType getPeripheralType()
{
return m_advanced ? PeripheralType.AdvancedMonitor : PeripheralType.Monitor;
}
public ServerMonitor getCachedServerMonitor() public ServerMonitor getCachedServerMonitor()
{ {
return m_serverMonitor; return m_serverMonitor;
@@ -319,7 +319,6 @@ public class TileMonitor extends TilePeripheralBase
} }
// Sizing and placement stuff // Sizing and placement stuff
@Override
public EnumFacing getDirection() public EnumFacing getDirection()
{ {
int dir = getDir() % 6; int dir = getDir() % 6;
@@ -444,7 +443,7 @@ public class TileMonitor extends TilePeripheralBase
return getSimilarMonitorAt( pos.offset( right, xOffset ).offset( down, yOffset ) ); return getSimilarMonitorAt( pos.offset( right, xOffset ).offset( down, yOffset ) );
} }
public TileMonitor getOrigin() private TileMonitor getOrigin()
{ {
return getNeighbour( 0, 0 ); return getNeighbour( 0, 0 );
} }
@@ -752,15 +751,16 @@ public class TileMonitor extends TilePeripheralBase
{ {
case Monitor: case Monitor:
case AdvancedMonitor: case AdvancedMonitor:
{
return false; return false;
}
default: default:
{
return true; return true;
}
} }
} }
} }
@Override
public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative )
{
if( !creative ) drops.add( PeripheralItemFactory.create( this ) );
}
} }

View File

@@ -177,6 +177,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
} }
} }
@Override
public void broadcastState( boolean force ) public void broadcastState( boolean force )
{ {
super.broadcastState( force ); super.broadcastState( force );