mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-12 03:00:30 +00:00
Move our message model to be closer to Forge's
It's rather embarassing that it's been restructured _again_, but I think this is a nice middle-ground. The previous implementation was written mostly for Fabric, which doesn't always map perfectly to Forge. - Move the message identifier into the registration phrase. It's not really a property of the message itself, rather a property of the registry, so better suited there. - Move message handling into the message itself. Honestly, it was just ending up being rather messy mixing the logic in two places. This also means we can drop some proxy methods, as it's easier to have conditionally loaded methods. - Move network registry into a dedicated class, as that's what we're doing for everything else.
This commit is contained in:
parent
e33f852baa
commit
ce0685c31f
@ -43,6 +43,7 @@ import dan200.computercraft.shared.media.items.ItemDiskExpanded;
|
||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.common.ItemPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
|
||||
@ -70,7 +71,6 @@ import dan200.computercraft.shared.util.IoUtil;
|
||||
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
||||
import dan200.computercraft.shared.wired.WiredNode;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@ -85,9 +85,6 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.SidedProxy;
|
||||
import net.minecraftforge.fml.common.event.*;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -242,9 +239,6 @@ public class ComputerCraft
|
||||
public static final ClientComputerRegistry clientComputerRegistry = new ClientComputerRegistry();
|
||||
public static final ServerComputerRegistry serverComputerRegistry = new ServerComputerRegistry();
|
||||
|
||||
// Networking
|
||||
public static SimpleNetworkWrapper networkWrapper;
|
||||
|
||||
// Creative
|
||||
public static CreativeTabMain mainCreativeTab;
|
||||
|
||||
@ -279,7 +273,7 @@ public class ComputerCraft
|
||||
Config.load( event.getSuggestedConfigurationFile() );
|
||||
|
||||
// Setup network
|
||||
networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel( ComputerCraft.MOD_ID );
|
||||
NetworkHandler.setup();
|
||||
|
||||
proxy.preInit();
|
||||
turtleProxy.preInit();
|
||||
@ -387,26 +381,6 @@ public class ComputerCraft
|
||||
return new File( getBaseDir(), "resourcepacks" );
|
||||
}
|
||||
|
||||
public static void sendToPlayer( EntityPlayer player, IMessage packet )
|
||||
{
|
||||
networkWrapper.sendTo( packet, (EntityPlayerMP) player );
|
||||
}
|
||||
|
||||
public static void sendToAllPlayers( IMessage packet )
|
||||
{
|
||||
networkWrapper.sendToAll( packet );
|
||||
}
|
||||
|
||||
public static void sendToServer( IMessage packet )
|
||||
{
|
||||
networkWrapper.sendToServer( packet );
|
||||
}
|
||||
|
||||
public static void sendToAllAround( IMessage packet, NetworkRegistry.TargetPoint point )
|
||||
{
|
||||
networkWrapper.sendToAllAround( packet, point );
|
||||
}
|
||||
|
||||
public static boolean canPlayerUseCommands( EntityPlayer player )
|
||||
{
|
||||
MinecraftServer server = player.getServer();
|
||||
|
@ -7,11 +7,9 @@
|
||||
package dan200.computercraft.client.proxy;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.ClientTableFormatter;
|
||||
import dan200.computercraft.client.render.TileEntityCableRenderer;
|
||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
||||
import dan200.computercraft.shared.command.CommandCopy;
|
||||
import dan200.computercraft.shared.command.text.TableBuilder;
|
||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
||||
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
|
||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||
@ -21,8 +19,6 @@ import dan200.computercraft.shared.util.Colour;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.color.IItemColor;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.client.ClientCommandHandler;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
@ -81,20 +77,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
ClientRegistry.bindTileEntitySpecialRenderer( TileCable.class, new TileEntityCableRenderer() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playRecordClient( BlockPos pos, SoundEvent record, String info )
|
||||
{
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
mc.world.playRecord( pos, record );
|
||||
if( info != null ) mc.ingameGUI.setRecordPlayingMessage( info );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showTableClient( TableBuilder table )
|
||||
{
|
||||
ClientTableFormatter.INSTANCE.display( table );
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
||||
public static class ForgeHandlers
|
||||
{
|
||||
|
@ -6,8 +6,8 @@
|
||||
|
||||
package dan200.computercraft.shared.command.text;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.command.CommandUtils;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.client.ChatTableClientMessage;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
@ -120,7 +120,7 @@ public class TableBuilder
|
||||
if( CommandUtils.isPlayer( source ) )
|
||||
{
|
||||
trim( 18 );
|
||||
ComputerCraft.sendToPlayer( (EntityPlayerMP) source, new ChatTableClientMessage( this ) );
|
||||
NetworkHandler.sendToPlayer( (EntityPlayerMP) source, new ChatTableClientMessage( this ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -7,9 +7,9 @@
|
||||
package dan200.computercraft.shared.computer.core;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.common.ClientTerminal;
|
||||
import dan200.computercraft.shared.computer.blocks.ComputerState;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.server.ComputerActionServerMessage;
|
||||
import dan200.computercraft.shared.network.server.QueueEventServerMessage;
|
||||
import dan200.computercraft.shared.network.server.RequestComputerMessage;
|
||||
@ -53,7 +53,7 @@ public class ClientComputer extends ClientTerminal implements IComputer
|
||||
public void requestState()
|
||||
{
|
||||
// Request state from server
|
||||
ComputerCraft.sendToServer( new RequestComputerMessage( getInstanceID() ) );
|
||||
NetworkHandler.sendToServer( new RequestComputerMessage( getInstanceID() ) );
|
||||
}
|
||||
|
||||
// IComputer
|
||||
@ -94,28 +94,28 @@ public class ClientComputer extends ClientTerminal implements IComputer
|
||||
public void turnOn()
|
||||
{
|
||||
// Send turnOn to server
|
||||
ComputerCraft.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.TURN_ON ) );
|
||||
NetworkHandler.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.TURN_ON ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown()
|
||||
{
|
||||
// Send shutdown to server
|
||||
ComputerCraft.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.SHUTDOWN ) );
|
||||
NetworkHandler.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.SHUTDOWN ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reboot()
|
||||
{
|
||||
// Send reboot to server
|
||||
ComputerCraft.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.REBOOT ) );
|
||||
NetworkHandler.sendToServer( new ComputerActionServerMessage( m_instanceID, ComputerActionServerMessage.Action.REBOOT ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueEvent( String event, Object[] arguments )
|
||||
{
|
||||
// Send event to server
|
||||
ComputerCraft.sendToServer( new QueueEventServerMessage( m_instanceID, event, arguments ) );
|
||||
NetworkHandler.sendToServer( new QueueEventServerMessage( m_instanceID, event, arguments ) );
|
||||
}
|
||||
|
||||
public void setState( ComputerState state, NBTTagCompound userData )
|
||||
|
@ -16,6 +16,7 @@ import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.IComputerEnvironment;
|
||||
import dan200.computercraft.shared.common.ServerTerminal;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.client.ComputerDataClientMessage;
|
||||
import dan200.computercraft.shared.network.client.ComputerDeletedClientMessage;
|
||||
import dan200.computercraft.shared.network.client.ComputerTerminalClientMessage;
|
||||
@ -163,7 +164,7 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput
|
||||
if( hasOutputChanged() || force )
|
||||
{
|
||||
// Send computer state to all clients
|
||||
ComputerCraft.sendToAllPlayers( createComputerPacket() );
|
||||
NetworkHandler.sendToAllPlayers( createComputerPacket() );
|
||||
}
|
||||
|
||||
if( hasTerminalChanged() || force )
|
||||
@ -178,7 +179,7 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput
|
||||
{
|
||||
if( isInteracting( player ) )
|
||||
{
|
||||
ComputerCraft.sendToPlayer( player, packet );
|
||||
NetworkHandler.sendToPlayer( player, packet );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -188,19 +189,19 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput
|
||||
public void sendComputerState( EntityPlayer player )
|
||||
{
|
||||
// Send state to client
|
||||
ComputerCraft.sendToPlayer( player, createComputerPacket() );
|
||||
NetworkHandler.sendToPlayer( player, createComputerPacket() );
|
||||
}
|
||||
|
||||
public void sendTerminalState( EntityPlayer player )
|
||||
{
|
||||
// Send terminal state to client
|
||||
ComputerCraft.sendToPlayer( player, createTerminalPacket() );
|
||||
NetworkHandler.sendToPlayer( player, createTerminalPacket() );
|
||||
}
|
||||
|
||||
public void broadcastDelete()
|
||||
{
|
||||
// Send deletion to client
|
||||
ComputerCraft.sendToAllPlayers( new ComputerDeletedClientMessage( getInstanceID() ) );
|
||||
NetworkHandler.sendToAllPlayers( new ComputerDeletedClientMessage( getInstanceID() ) );
|
||||
}
|
||||
|
||||
public IWritableMount getRootMount()
|
||||
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.network;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.network.client.*;
|
||||
import dan200.computercraft.shared.network.server.ComputerActionServerMessage;
|
||||
import dan200.computercraft.shared.network.server.QueueEventServerMessage;
|
||||
import dan200.computercraft.shared.network.server.RequestComputerMessage;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.IThreadListener;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public final class NetworkHandler
|
||||
{
|
||||
public static SimpleNetworkWrapper network;
|
||||
|
||||
private NetworkHandler()
|
||||
{
|
||||
}
|
||||
|
||||
private static final int COMPUTER_ACTION_SERVER_MESSAGE = 0;
|
||||
private static final int QUEUE_EVENT_SERVER_MESSAGE = 1;
|
||||
private static final int REQUEST_COMPUTER_SERVER_MESSAGE = 2;
|
||||
|
||||
private static final int CHAT_TABLE_CLIENT_MESSAGE = 10;
|
||||
private static final int COMPUTER_DATA_CLIENT_MESSAGE = 11;
|
||||
private static final int COMPUTER_DELETED_CLIENT_MESSAGE = 12;
|
||||
private static final int COMPUTER_TERMINAL_CLIENT_MESSAGE = 13;
|
||||
private static final int PLAY_RECORD_CLIENT_MESSAGE = 14;
|
||||
|
||||
public static void setup()
|
||||
{
|
||||
network = NetworkRegistry.INSTANCE.newSimpleChannel( ComputerCraft.MOD_ID );
|
||||
|
||||
// Server messages
|
||||
registerMainThread( NetworkHandler.COMPUTER_ACTION_SERVER_MESSAGE, Side.SERVER, ComputerActionServerMessage::new );
|
||||
registerMainThread( NetworkHandler.QUEUE_EVENT_SERVER_MESSAGE, Side.SERVER, QueueEventServerMessage::new );
|
||||
registerMainThread( NetworkHandler.REQUEST_COMPUTER_SERVER_MESSAGE, Side.SERVER, RequestComputerMessage::new );
|
||||
|
||||
// Client messages
|
||||
registerMainThread( NetworkHandler.PLAY_RECORD_CLIENT_MESSAGE, Side.CLIENT, PlayRecordClientMessage::new );
|
||||
registerMainThread( NetworkHandler.COMPUTER_DATA_CLIENT_MESSAGE, Side.CLIENT, ComputerDataClientMessage::new );
|
||||
registerMainThread( NetworkHandler.COMPUTER_TERMINAL_CLIENT_MESSAGE, Side.CLIENT, ComputerTerminalClientMessage::new );
|
||||
registerMainThread( NetworkHandler.COMPUTER_DELETED_CLIENT_MESSAGE, Side.CLIENT, ComputerDeletedClientMessage::new );
|
||||
registerMainThread( NetworkHandler.CHAT_TABLE_CLIENT_MESSAGE, Side.CLIENT, ChatTableClientMessage::new );
|
||||
}
|
||||
|
||||
public static void sendToPlayer( EntityPlayer player, IMessage packet )
|
||||
{
|
||||
network.sendTo( packet, (EntityPlayerMP) player );
|
||||
}
|
||||
|
||||
public static void sendToAllPlayers( IMessage packet )
|
||||
{
|
||||
network.sendToAll( packet );
|
||||
}
|
||||
|
||||
public static void sendToServer( IMessage packet )
|
||||
{
|
||||
network.sendToServer( packet );
|
||||
}
|
||||
|
||||
public static void sendToAllAround( IMessage packet, NetworkRegistry.TargetPoint point )
|
||||
{
|
||||
network.sendToAllAround( packet, point );
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
*
|
||||
* @param id The identifier for this packet type
|
||||
* @param side The side to register this packet handler under
|
||||
* @param factory The factory for this type of packet.
|
||||
*/
|
||||
private static <T extends NetworkMessage> void registerMainThread( int id, Side side, Supplier<T> factory )
|
||||
{
|
||||
network.registerMessage( MAIN_THREAD_HANDLER, factory.get().getClass(), id, side );
|
||||
}
|
||||
|
||||
private static final IMessageHandler<NetworkMessage, IMessage> MAIN_THREAD_HANDLER = ( packet, context ) -> {
|
||||
IThreadListener listener = context.side == Side.CLIENT ? Minecraft.getMinecraft() : context.getServerHandler().player.server;
|
||||
if( listener.isCallingFromMinecraftThread() )
|
||||
{
|
||||
packet.handle( context );
|
||||
}
|
||||
else
|
||||
{
|
||||
listener.addScheduledTask( () -> packet.handle( context ) );
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}
|
@ -6,18 +6,12 @@
|
||||
|
||||
package dan200.computercraft.shared.network;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.IThreadListener;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The base interface for any message which will be sent to the client or server.
|
||||
@ -27,13 +21,6 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
public interface NetworkMessage extends IMessage
|
||||
{
|
||||
/**
|
||||
* The unique identifier for this packet type
|
||||
*
|
||||
* @return This packet type's identifier
|
||||
*/
|
||||
int getId();
|
||||
|
||||
/**
|
||||
* Write this packet to a buffer.
|
||||
*
|
||||
@ -52,6 +39,13 @@ public interface NetworkMessage extends IMessage
|
||||
*/
|
||||
void fromBytes( @Nonnull PacketBuffer buf );
|
||||
|
||||
/**
|
||||
* Handle this {@link NetworkMessage}.
|
||||
*
|
||||
* @param context The context with which to handle this message
|
||||
*/
|
||||
void handle( MessageContext context );
|
||||
|
||||
@Override
|
||||
default void fromBytes( ByteBuf buf )
|
||||
{
|
||||
@ -63,56 +57,4 @@ public interface NetworkMessage extends IMessage
|
||||
{
|
||||
toBytes( new PacketBuffer( buf ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a packet, and a thread-safe handler for it.
|
||||
*
|
||||
* @param side The side to register this packet handler under
|
||||
* @param factory The factory for this type of packet.
|
||||
* @param handler The handler for this type of packet. Note, this may be called on any thread,
|
||||
* and so should be thread-safe.
|
||||
*/
|
||||
@SuppressWarnings( "unchecked" )
|
||||
static <T extends NetworkMessage> void register(
|
||||
Side side,
|
||||
Supplier<T> factory,
|
||||
BiConsumer<MessageContext, T> handler
|
||||
)
|
||||
{
|
||||
T instance = factory.get();
|
||||
ComputerCraft.networkWrapper.registerMessage( ( packet, ctx ) -> {
|
||||
handler.accept( ctx, (T) packet );
|
||||
return null;
|
||||
}, instance.getClass(), instance.getId(), side );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
*
|
||||
* @param side The side to register this packet handler under
|
||||
* @param factory The factory for this type of packet.
|
||||
* @param handler The handler for this type of packet. This will be called on the "main"
|
||||
* thread (either client or server).
|
||||
*/
|
||||
@SuppressWarnings( "unchecked" )
|
||||
static <T extends NetworkMessage> void registerMainThread(
|
||||
Side side,
|
||||
Supplier<T> factory,
|
||||
BiConsumer<MessageContext, T> handler
|
||||
)
|
||||
{
|
||||
T instance = factory.get();
|
||||
ComputerCraft.networkWrapper.registerMessage( ( packet, ctx ) -> {
|
||||
IThreadListener listener = side == Side.CLIENT ? Minecraft.getMinecraft() : ctx.getServerHandler().player.server;
|
||||
if( listener.isCallingFromMinecraftThread() )
|
||||
{
|
||||
handler.accept( ctx, (T) packet );
|
||||
}
|
||||
else
|
||||
{
|
||||
listener.addScheduledTask( () -> handler.accept( ctx, (T) packet ) );
|
||||
}
|
||||
return null;
|
||||
}, instance.getClass(), instance.getId(), side );
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.network;
|
||||
|
||||
public final class NetworkMessages
|
||||
{
|
||||
private NetworkMessages()
|
||||
{
|
||||
}
|
||||
|
||||
public static final int COMPUTER_ACTION_SERVER_MESSAGE = 0;
|
||||
public static final int QUEUE_EVENT_SERVER_MESSAGE = 1;
|
||||
public static final int REQUEST_COMPUTER_SERVER_MESSAGE = 2;
|
||||
|
||||
public static final int CHAT_TABLE_CLIENT_MESSAGE = 10;
|
||||
public static final int COMPUTER_DATA_CLIENT_MESSAGE = 11;
|
||||
public static final int COMPUTER_DELETED_CLIENT_MESSAGE = 12;
|
||||
public static final int COMPUTER_TERMINAL_CLIENT_MESSAGE = 13;
|
||||
public static final int PLAY_RECORD_CLIENT_MESSAGE = 14;
|
||||
}
|
@ -6,12 +6,15 @@
|
||||
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.client.ClientTableFormatter;
|
||||
import dan200.computercraft.shared.command.text.TableBuilder;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -29,17 +32,6 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.CHAT_TABLE_CLIENT_MESSAGE;
|
||||
}
|
||||
|
||||
public TableBuilder getTable()
|
||||
{
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -84,4 +76,11 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
}
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SideOnly( Side.CLIENT )
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
ClientTableFormatter.INSTANCE.display( table );
|
||||
}
|
||||
}
|
||||
|
@ -10,11 +10,8 @@ import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A packet, which performs an action on a {@link ClientComputer}.
|
||||
@ -58,12 +55,4 @@ public abstract class ComputerClientMessage implements NetworkMessage
|
||||
}
|
||||
return computer;
|
||||
}
|
||||
|
||||
public static <T extends ComputerClientMessage> void register( Supplier<T> factory, BiConsumer<ClientComputer, T> handler )
|
||||
{
|
||||
NetworkMessage.registerMainThread( Side.CLIENT, factory, ( context, packet ) -> {
|
||||
ClientComputer computer = packet.getComputer();
|
||||
if( computer != null ) handler.accept( computer, packet );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.shared.computer.blocks.ComputerState;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -34,22 +34,6 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.COMPUTER_DATA_CLIENT_MESSAGE;
|
||||
}
|
||||
|
||||
public ComputerState getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public NBTTagCompound getUserData()
|
||||
{
|
||||
return userData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -65,4 +49,10 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
state = buf.readEnumValue( ComputerState.class );
|
||||
userData = NBTUtil.readCompoundTag( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
getComputer().setState( state, userData );
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,8 @@
|
||||
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
{
|
||||
@ -20,8 +21,8 @@ public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
return NetworkMessages.COMPUTER_DELETED_CLIENT_MESSAGE;
|
||||
ComputerCraft.clientComputerRegistry.remove( getInstanceId() );
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@
|
||||
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -27,17 +27,6 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.COMPUTER_TERMINAL_CLIENT_MESSAGE;
|
||||
}
|
||||
|
||||
public NBTTagCompound getTag()
|
||||
{
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -51,4 +40,10 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
super.fromBytes( buf );
|
||||
tag = NBTUtil.readCompoundTag( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
getComputer().readDescription( tag );
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,13 @@
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -43,27 +46,6 @@ public class PlayRecordClientMessage implements NetworkMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.PLAY_RECORD_CLIENT_MESSAGE;
|
||||
}
|
||||
|
||||
public BlockPos getPos()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public SoundEvent getSoundEvent()
|
||||
{
|
||||
return soundEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -90,4 +72,13 @@ public class PlayRecordClientMessage implements NetworkMessage
|
||||
soundEvent = SoundEvent.REGISTRY.getObjectById( buf.readInt() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SideOnly( Side.CLIENT )
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
mc.world.playRecord( pos, soundEvent );
|
||||
if( name != null ) mc.ingameGUI.setRecordPlayingMessage( name );
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@ -25,12 +25,6 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.COMPUTER_ACTION_SERVER_MESSAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -45,9 +39,21 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
action = buf.readEnumValue( Action.class );
|
||||
}
|
||||
|
||||
public Action getAction()
|
||||
@Override
|
||||
protected void handle( ServerComputer computer )
|
||||
{
|
||||
return action;
|
||||
switch( action )
|
||||
{
|
||||
case TURN_ON:
|
||||
computer.turnOn();
|
||||
break;
|
||||
case REBOOT:
|
||||
computer.reboot();
|
||||
break;
|
||||
case SHUTDOWN:
|
||||
computer.shutdown();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Action
|
||||
|
@ -12,11 +12,8 @@ import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A packet, which performs an action on a {@link ServerComputer}.
|
||||
@ -61,11 +58,12 @@ public abstract class ComputerServerMessage implements NetworkMessage
|
||||
return computer;
|
||||
}
|
||||
|
||||
public static <T extends ComputerServerMessage> void register( Supplier<T> factory, BiConsumer<ServerComputer, T> handler )
|
||||
@Override
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
NetworkMessage.registerMainThread( Side.SERVER, factory, ( context, packet ) -> {
|
||||
ServerComputer computer = packet.getComputer( context );
|
||||
if( computer != null ) handler.accept( computer, packet );
|
||||
} );
|
||||
ServerComputer computer = getComputer( context );
|
||||
if( computer != null ) handle( computer );
|
||||
}
|
||||
|
||||
protected abstract void handle( ServerComputer computer );
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
@ -36,24 +36,6 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.QUEUE_EVENT_SERVER_MESSAGE;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public String getEvent()
|
||||
{
|
||||
return event;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object[] getArgs()
|
||||
{
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -71,4 +53,10 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
NBTTagCompound args = NBTUtil.readCompoundTag( buf );
|
||||
this.args = args == null ? null : NBTUtil.decodeObjects( args );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( ServerComputer computer )
|
||||
{
|
||||
computer.queueEvent( event, args );
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,11 @@
|
||||
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import dan200.computercraft.shared.network.NetworkMessages;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -25,17 +27,6 @@ public class RequestComputerMessage implements NetworkMessage
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return NetworkMessages.REQUEST_COMPUTER_SERVER_MESSAGE;
|
||||
}
|
||||
|
||||
public int getInstance()
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
@ -47,4 +38,11 @@ public class RequestComputerMessage implements NetworkMessage
|
||||
{
|
||||
instance = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
|
||||
if( computer != null ) computer.sendComputerState( context.getServerHandler().player );
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import dan200.computercraft.api.pocket.IPocketAccess;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
@ -186,7 +187,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
||||
EntityPlayerMP player = (EntityPlayerMP) m_entity;
|
||||
if( player.connection != null && !isInteracting( player ) )
|
||||
{
|
||||
ComputerCraft.sendToPlayer( player, createTerminalPacket() );
|
||||
NetworkHandler.sendToPlayer( player, createTerminalPacket() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,12 +22,6 @@ import dan200.computercraft.shared.integration.charset.IntegrationCharset;
|
||||
import dan200.computercraft.shared.media.common.DefaultMediaProvider;
|
||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import dan200.computercraft.shared.network.client.*;
|
||||
import dan200.computercraft.shared.network.server.ComputerActionServerMessage;
|
||||
import dan200.computercraft.shared.network.server.ComputerServerMessage;
|
||||
import dan200.computercraft.shared.network.server.QueueEventServerMessage;
|
||||
import dan200.computercraft.shared.network.server.RequestComputerMessage;
|
||||
import dan200.computercraft.shared.peripheral.commandblock.CommandBlockPeripheralProvider;
|
||||
import dan200.computercraft.shared.peripheral.common.DefaultPeripheralProvider;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.ContainerDiskDrive;
|
||||
@ -77,7 +71,6 @@ public class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
{
|
||||
registerProviders();
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler( ComputerCraft.instance, new GuiHandler() );
|
||||
registerNetwork();
|
||||
|
||||
Fixes.register( FMLCommonHandler.instance().getDataFixer() );
|
||||
if( Loader.isModLoaded( ModCharset.MODID ) ) IntegrationCharset.register();
|
||||
@ -109,51 +102,6 @@ public class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
CapabilityWiredElement.register();
|
||||
}
|
||||
|
||||
private void registerNetwork()
|
||||
{
|
||||
// Server messages
|
||||
|
||||
ComputerServerMessage.register( ComputerActionServerMessage::new, ( computer, packet ) -> {
|
||||
switch( packet.getAction() )
|
||||
{
|
||||
case TURN_ON:
|
||||
computer.turnOn();
|
||||
break;
|
||||
case REBOOT:
|
||||
computer.reboot();
|
||||
break;
|
||||
case SHUTDOWN:
|
||||
computer.shutdown();
|
||||
break;
|
||||
}
|
||||
} );
|
||||
|
||||
ComputerServerMessage.register( QueueEventServerMessage::new, ( computer, packet ) ->
|
||||
computer.queueEvent( packet.getEvent(), packet.getArgs() ) );
|
||||
|
||||
NetworkMessage.registerMainThread( Side.SERVER, RequestComputerMessage::new, ( context, packet ) -> {
|
||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( packet.getInstance() );
|
||||
if( computer != null ) computer.sendComputerState( context.getServerHandler().player );
|
||||
} );
|
||||
|
||||
// Client messages
|
||||
|
||||
NetworkMessage.registerMainThread( Side.CLIENT, PlayRecordClientMessage::new, ( computer, packet ) ->
|
||||
playRecordClient( packet.getPos(), packet.getSoundEvent(), packet.getName() ) );
|
||||
|
||||
ComputerClientMessage.register( ComputerDataClientMessage::new, ( computer, packet ) ->
|
||||
computer.setState( packet.getState(), packet.getUserData() ) );
|
||||
|
||||
ComputerClientMessage.register( ComputerTerminalClientMessage::new, ( computer, packet ) ->
|
||||
computer.readDescription( packet.getTag() ) );
|
||||
|
||||
NetworkMessage.registerMainThread( Side.CLIENT, ComputerDeletedClientMessage::new, ( context, packet ) ->
|
||||
ComputerCraft.clientComputerRegistry.remove( packet.getInstanceId() ) );
|
||||
|
||||
NetworkMessage.registerMainThread( Side.CLIENT, ChatTableClientMessage::new, ( context, packet ) ->
|
||||
showTableClient( packet.getTable() ) );
|
||||
}
|
||||
|
||||
public class GuiHandler implements IGuiHandler
|
||||
{
|
||||
private GuiHandler()
|
||||
|
@ -6,10 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.proxy;
|
||||
|
||||
import dan200.computercraft.shared.command.text.TableBuilder;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
public interface IComputerCraftProxy
|
||||
{
|
||||
@ -18,12 +15,4 @@ public interface IComputerCraftProxy
|
||||
void init();
|
||||
|
||||
void initServer( MinecraftServer server );
|
||||
|
||||
default void playRecordClient( BlockPos pos, SoundEvent record, String info )
|
||||
{
|
||||
}
|
||||
|
||||
default void showTableClient( TableBuilder table )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.util;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.client.PlayRecordClientMessage;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemRecord;
|
||||
@ -26,7 +26,7 @@ public class RecordUtil
|
||||
IMessage packet = record != null ? new PlayRecordClientMessage( pos, record, recordInfo ) : new PlayRecordClientMessage( pos );
|
||||
|
||||
NetworkRegistry.TargetPoint point = new NetworkRegistry.TargetPoint( world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64 );
|
||||
ComputerCraft.sendToAllAround( packet, point );
|
||||
NetworkHandler.sendToAllAround( packet, point );
|
||||
}
|
||||
|
||||
public static String getRecordInfo( @Nonnull ItemStack recordStack )
|
||||
|
Loading…
Reference in New Issue
Block a user