Skip to content

Commit

Permalink
Merge branch 'master' into mc-1.14.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Jun 15, 2020
2 parents b9b8121 + c0f3ca8 commit 9134f24
Show file tree
Hide file tree
Showing 26 changed files with 563 additions and 118 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Mod properties
mod_version=1.88.0
mod_version=1.89.0

# Minecraft properties (update mods.toml when changing)
mc_version=1.14.4
Expand Down
1 change: 1 addition & 0 deletions src/main/java/dan200/computercraft/ComputerCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public final class ComputerCraft
public static int modem_highAltitudeRangeDuringStorm = 384;
public static int maxNotesPerTick = 8;
public static MonitorRenderer monitorRenderer = MonitorRenderer.BEST;
public static long monitorBandwidth = 1_000_000;

public static boolean turtlesNeedFuel = true;
public static int turtleFuelLimit = 20000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
import dan200.computercraft.shared.util.Palette;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;

Expand All @@ -25,12 +24,8 @@ class MonitorTextureBufferShader
{
static final int TEXTURE_INDEX = GL13.GL_TEXTURE3;

private static final FloatBuffer MATRIX_BUFFER = BufferUtils.createFloatBuffer( 16 );
private static final FloatBuffer PALETTE_BUFFER = BufferUtils.createFloatBuffer( 16 * 3 );

private static int uniformMv;
private static int uniformP;

private static int uniformFont;
private static int uniformWidth;
private static int uniformHeight;
Expand All @@ -43,16 +38,6 @@ class MonitorTextureBufferShader

static void setupUniform( int width, int height, Palette palette, boolean greyscale )
{
MATRIX_BUFFER.rewind();
GL11.glGetFloatv( GL11.GL_MODELVIEW_MATRIX, MATRIX_BUFFER );
MATRIX_BUFFER.rewind();
GLX.glUniformMatrix4( uniformMv, false, MATRIX_BUFFER );

MATRIX_BUFFER.rewind();
GL11.glGetFloatv( GL11.GL_PROJECTION_MATRIX, MATRIX_BUFFER );
MATRIX_BUFFER.rewind();
GLX.glUniformMatrix4( uniformP, false, MATRIX_BUFFER );

GLX.glUniform1i( uniformWidth, width );
GLX.glUniform1i( uniformHeight, height );

Expand Down Expand Up @@ -121,9 +106,6 @@ private static boolean load()

if( !ok ) return false;

uniformMv = getUniformLocation( program, "u_mv" );
uniformP = getUniformLocation( program, "u_p" );

uniformFont = getUniformLocation( program, "u_font" );
uniformWidth = getUniformLocation( program, "u_width" );
uniformHeight = getUniformLocation( program, "u_height" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public class TileEntityMonitorRenderer extends TileEntityRenderer<TileMonitor>
{
private static final float MARGIN = (float) (TileMonitor.RENDER_MARGIN * 1.1);
private static ByteBuffer tboContents;

@Override
public void render( @Nonnull TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
Expand Down Expand Up @@ -161,7 +162,14 @@ private static void renderTerminal( ClientMonitor monitor, float xMargin, float

if( redraw )
{
ByteBuffer monitorBuffer = GLAllocation.createDirectByteBuffer( width * height * 3 );
int size = width * height * 3;
if( tboContents == null || tboContents.capacity() < size )
{
tboContents = GLAllocation.createDirectByteBuffer( size );
}

ByteBuffer monitorBuffer = tboContents;
monitorBuffer.position( 0 );
for( int y = 0; y < height; y++ )
{
TextBuffer text = terminal.getLine( y ), textColour = terminal.getTextColourLine( y ), background = terminal.getBackgroundColourLine( y );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ protected final void close()
{
m_open = false;

Closeable closeable = m_closable;
if( closeable != null )
{
IoUtil.closeQuietly( closeable );
m_closable = null;
}
IoUtil.closeQuietly( m_closable );
m_closable = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean queue( Consumer<T> task )

protected static <T extends Closeable> T closeCloseable( T closeable )
{
if( closeable != null ) IoUtil.closeQuietly( closeable );
IoUtil.closeQuietly( closeable );
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected void dispose()

WeakReference<WebsocketHandle> websocketHandleRef = websocketHandle;
WebsocketHandle websocketHandle = websocketHandleRef == null ? null : websocketHandleRef.get();
if( websocketHandle != null ) IoUtil.closeQuietly( websocketHandle );
IoUtil.closeQuietly( websocketHandle );
this.websocketHandle = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ private void cleanup()
Reference<?> ref;
while( (ref = m_openFileQueue.poll()) != null )
{
Closeable file = m_openFiles.remove( ref );
if( file != null ) IoUtil.closeQuietly( file );
IoUtil.closeQuietly( m_openFiles.remove( ref ) );
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dan200/computercraft/shared/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public final class Config
private static final ConfigValue<Integer> modemRangeDuringStorm;
private static final ConfigValue<Integer> modemHighAltitudeRangeDuringStorm;
private static final ConfigValue<Integer> maxNotesPerTick;
private static final ConfigValue<Integer> monitorBandwidth;

private static final ConfigValue<Boolean> turtlesNeedFuel;
private static final ConfigValue<Integer> turtleFuelLimit;
Expand Down Expand Up @@ -230,6 +231,16 @@ private Config() {}
.comment( "Maximum amount of notes a speaker can play at once" )
.defineInRange( "max_notes_per_tick", ComputerCraft.maxNotesPerTick, 1, Integer.MAX_VALUE );

monitorBandwidth = builder
.comment( "The limit to how much monitor data can be sent *per tick*. Note:\n" +
" - Bandwidth is measured before compression, so the data sent to the client is smaller.\n" +
" - This ignores the number of players a packet is sent to. Updating a monitor for one player consumes " +
"the same bandwidth limit as sending to 20.\n" +
" - A full sized monitor sends ~25kb of data. So the default (1MB) allows for ~40 monitors to be updated " +
"in a single tick. \n" +
"Set to 0 to disable." )
.defineInRange( "monitor_bandwidth", (int) ComputerCraft.monitorBandwidth, 0, Integer.MAX_VALUE );

builder.pop();
}

Expand Down Expand Up @@ -317,6 +328,7 @@ public static void sync()
ComputerCraft.modem_highAltitudeRange = modemHighAltitudeRange.get();
ComputerCraft.modem_rangeDuringStorm = modemRangeDuringStorm.get();
ComputerCraft.modem_highAltitudeRangeDuringStorm = modemHighAltitudeRangeDuringStorm.get();
ComputerCraft.monitorBandwidth = monitorBandwidth.get();

// Turtles
ComputerCraft.turtlesNeedFuel = turtlesNeedFuel.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package dan200.computercraft.shared.common;

import dan200.computercraft.core.terminal.Terminal;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import dan200.computercraft.shared.network.client.TerminalState;

public class ClientTerminal implements ITerminal
{
Expand Down Expand Up @@ -48,14 +46,13 @@ public boolean isColour()
return m_colour;
}

public void readDescription( CompoundNBT nbt )
public void read( TerminalState state )
{
m_colour = nbt.getBoolean( "colour" );
if( nbt.contains( "terminal" ) )
m_colour = state.colour;
if( state.hasTerminal() )
{
CompoundNBT terminal = nbt.getCompound( "terminal" );
resizeTerminal( terminal.getInt( "term_width" ), terminal.getInt( "term_height" ) );
m_terminal.read( new PacketBuffer( Unpooled.wrappedBuffer( terminal.getByteArray( "term_contents" ) ) ) );
resizeTerminal( state.width, state.height );
state.apply( m_terminal );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
*/
package dan200.computercraft.shared.common;

import dan200.computercraft.ComputerCraft;
import dan200.computercraft.core.terminal.Terminal;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import dan200.computercraft.shared.network.client.TerminalState;

import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -73,8 +69,6 @@ public boolean hasTerminalChanged()
return m_terminalChangedLastFrame;
}

// ITerminal implementation

@Override
public Terminal getTerminal()
{
Expand All @@ -87,29 +81,8 @@ public boolean isColour()
return m_colour;
}

public void writeDescription( CompoundNBT nbt )
public TerminalState write()
{
nbt.putBoolean( "colour", m_colour );
if( m_terminal != null )
{
// We have a 10 byte header (2 integer positions, then blinking and current colours), followed by the
// contents and palette.
// Yes, this serialisation code is terrible, but we need to serialise to NBT in order to work with monitors
// (or rather tile entity serialisation).
final int length = 10 + (2 * m_terminal.getWidth() * m_terminal.getHeight()) + (16 * 3);
ByteBuf buffer = Unpooled.buffer( length );
m_terminal.write( new PacketBuffer( buffer ) );

if( buffer.writableBytes() != 0 )
{
ComputerCraft.log.warn( "Should have written {} bytes, but have {} ({} remaining).", length, buffer.writerIndex(), buffer.writableBytes() );
}

CompoundNBT terminal = new CompoundNBT();
terminal.putInt( "term_width", m_terminal.getWidth() );
terminal.putInt( "term_height", m_terminal.getHeight() );
terminal.putByteArray( "term_contents", buffer.array() );
nbt.put( "terminal", terminal );
}
return new TerminalState( m_colour, m_terminal );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ private NetworkMessage createComputerPacket()

protected NetworkMessage createTerminalPacket()
{
CompoundNBT tagCompound = new CompoundNBT();
writeDescription( tagCompound );
return new ComputerTerminalClientMessage( getInstanceID(), tagCompound );
return new ComputerTerminalClientMessage( getInstanceID(), write() );
}

public void broadcastState( boolean force )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
import net.minecraftforge.fml.server.ServerLifecycleHooks;

Expand Down Expand Up @@ -52,6 +54,7 @@ public static void setup()
registerMainThread( 12, ComputerDeletedClientMessage::new );
registerMainThread( 13, ComputerTerminalClientMessage::new );
registerMainThread( 14, PlayRecordClientMessage.class, PlayRecordClientMessage::new );
registerMainThread( 15, MonitorClientMessage.class, MonitorClientMessage::new );
}

public static void sendToPlayer( PlayerEntity player, NetworkMessage packet )
Expand All @@ -74,15 +77,13 @@ public static void sendToServer( NetworkMessage packet )

public static void sendToAllAround( NetworkMessage packet, World world, Vec3d pos, double range )
{
for( ServerPlayerEntity player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers() )
{
if( player.getEntityWorld() != world ) continue;
PacketDistributor.TargetPoint target = new PacketDistributor.TargetPoint( pos.x, pos.y, pos.z, range, world.getDimension().getType() );
network.send( PacketDistributor.NEAR.with( () -> target ), packet );
}

double x = pos.x - player.posX;
double y = pos.y - player.posY;
double z = pos.z - player.posZ;
if( x * x + y * y + z * z < range * range ) sendToPlayer( player, packet );
}
public static void sendToAllTracking( NetworkMessage packet, Chunk chunk )
{
network.send( PacketDistributor.TRACKING_CHUNK.with( () -> chunk ), packet );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
*/
package dan200.computercraft.shared.network.client;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

import javax.annotation.Nonnull;

public class ComputerTerminalClientMessage extends ComputerClientMessage
{
private CompoundNBT tag;
private TerminalState state;

public ComputerTerminalClientMessage( int instanceId, CompoundNBT tag )
public ComputerTerminalClientMessage( int instanceId, TerminalState state )
{
super( instanceId );
this.tag = tag;
this.state = state;
}

public ComputerTerminalClientMessage()
Expand All @@ -29,19 +28,19 @@ public ComputerTerminalClientMessage()
public void toBytes( @Nonnull PacketBuffer buf )
{
super.toBytes( buf );
buf.writeCompoundTag( tag ); // TODO: Do we need to compress this?
state.write( buf );
}

@Override
public void fromBytes( @Nonnull PacketBuffer buf )
{
super.fromBytes( buf );
tag = buf.readCompoundTag();
state = new TerminalState( buf );
}

@Override
public void handle( NetworkEvent.Context context )
{
getComputer().readDescription( tag );
getComputer().read( state );
}
}
Loading

0 comments on commit 9134f24

Please sign in to comment.