mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-19 05:32:55 +00:00
Keep track of the current input state on the server
- We send special packets for key and mouse events, which are then processed by the container's InputState. - InputState keeps track of currently held keys and mouse buttons. - When closing the container, we queue key_up/mouse_up events for any pending buttons.
This commit is contained in:
parent
bd28955c8e
commit
7a7951ae68
@ -31,23 +31,23 @@ public class WidgetTerminal extends Widget
|
||||
|
||||
private final IComputerContainer m_computer;
|
||||
|
||||
private float m_terminateTimer;
|
||||
private float m_rebootTimer;
|
||||
private float m_shutdownTimer;
|
||||
private float m_terminateTimer = 0.0f;
|
||||
private float m_rebootTimer = 0.0f;
|
||||
private float m_shutdownTimer = 0.0f;
|
||||
|
||||
private int m_lastClickButton;
|
||||
private int m_lastClickX;
|
||||
private int m_lastClickY;
|
||||
private int m_lastClickButton = -1;
|
||||
private int m_lastClickX = -1;
|
||||
private int m_lastClickY = -1;
|
||||
|
||||
private boolean m_focus;
|
||||
private boolean m_allowFocusLoss;
|
||||
private boolean m_focus = false;
|
||||
private boolean m_allowFocusLoss = true;
|
||||
|
||||
private int m_leftMargin;
|
||||
private int m_rightMargin;
|
||||
private int m_topMargin;
|
||||
private int m_bottomMargin;
|
||||
|
||||
private ArrayList<Integer> m_keysDown;
|
||||
private final ArrayList<Integer> m_keysDown = new ArrayList<>();
|
||||
|
||||
public WidgetTerminal( int x, int y, int termWidth, int termHeight, IComputerContainer computer, int leftMargin, int rightMargin, int topMargin, int bottomMargin )
|
||||
{
|
||||
@ -58,23 +58,11 @@ public class WidgetTerminal extends Widget
|
||||
);
|
||||
|
||||
m_computer = computer;
|
||||
m_terminateTimer = 0.0f;
|
||||
m_rebootTimer = 0.0f;
|
||||
m_shutdownTimer = 0.0f;
|
||||
|
||||
m_lastClickButton = -1;
|
||||
m_lastClickX = -1;
|
||||
m_lastClickY = -1;
|
||||
|
||||
m_focus = false;
|
||||
m_allowFocusLoss = true;
|
||||
|
||||
m_leftMargin = leftMargin;
|
||||
m_rightMargin = rightMargin;
|
||||
m_topMargin = topMargin;
|
||||
m_bottomMargin = bottomMargin;
|
||||
|
||||
m_keysDown = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setAllowFocusLoss( boolean allowFocusLoss )
|
||||
@ -122,9 +110,7 @@ public class WidgetTerminal extends Widget
|
||||
}
|
||||
|
||||
// Queue the "paste" event
|
||||
queueEvent( "paste", new Object[] {
|
||||
clipboard
|
||||
} );
|
||||
queueEvent( "paste", new Object[] { clipboard } );
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@ -143,18 +129,15 @@ public class WidgetTerminal extends Widget
|
||||
}
|
||||
|
||||
// Queue the "key" event
|
||||
queueEvent( "key", new Object[] {
|
||||
key, repeat
|
||||
} );
|
||||
IComputer computer = m_computer.getComputer();
|
||||
if( computer != null ) computer.keyDown( key, repeat );
|
||||
handled = true;
|
||||
}
|
||||
|
||||
if( (ch >= 32 && ch <= 126) || (ch >= 160 && ch <= 255) ) // printable chars in byte range
|
||||
{
|
||||
// Queue the "char" event
|
||||
queueEvent( "char", new Object[] {
|
||||
Character.toString( ch )
|
||||
} );
|
||||
queueEvent( "char", new Object[] { Character.toString( ch ) } );
|
||||
handled = true;
|
||||
}
|
||||
|
||||
@ -189,9 +172,7 @@ public class WidgetTerminal extends Widget
|
||||
charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
|
||||
charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
|
||||
|
||||
computer.queueEvent( "mouse_click", new Object[] {
|
||||
button + 1, charX + 1, charY + 1
|
||||
} );
|
||||
computer.mouseClick( button + 1, charX + 1, charY + 1 );
|
||||
|
||||
m_lastClickButton = button;
|
||||
m_lastClickX = charX;
|
||||
@ -222,9 +203,8 @@ public class WidgetTerminal extends Widget
|
||||
if( m_focus )
|
||||
{
|
||||
// Queue the "key_up" event
|
||||
queueEvent( "key_up", new Object[] {
|
||||
key
|
||||
} );
|
||||
IComputer computer = m_computer.getComputer();
|
||||
if( computer != null ) computer.keyUp( key );
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
@ -251,12 +231,7 @@ public class WidgetTerminal extends Widget
|
||||
|
||||
if( m_lastClickButton >= 0 && !Mouse.isButtonDown( m_lastClickButton ) )
|
||||
{
|
||||
if( m_focus )
|
||||
{
|
||||
computer.queueEvent( "mouse_up", new Object[] {
|
||||
m_lastClickButton + 1, charX + 1, charY + 1
|
||||
} );
|
||||
}
|
||||
if( m_focus ) computer.mouseUp( m_lastClickButton + 1, charX + 1, charY + 1 );
|
||||
m_lastClickButton = -1;
|
||||
}
|
||||
|
||||
@ -270,22 +245,16 @@ public class WidgetTerminal extends Widget
|
||||
{
|
||||
if( wheelChange < 0 )
|
||||
{
|
||||
computer.queueEvent( "mouse_scroll", new Object[] {
|
||||
1, charX + 1, charY + 1
|
||||
} );
|
||||
computer.mouseScroll( 1, charX + 1, charY + 1 );
|
||||
}
|
||||
else if( wheelChange > 0 )
|
||||
{
|
||||
computer.queueEvent( "mouse_scroll", new Object[] {
|
||||
-1, charX + 1, charY + 1
|
||||
} );
|
||||
computer.mouseScroll( -1, charX + 1, charY + 1 );
|
||||
}
|
||||
|
||||
if( m_lastClickButton >= 0 && (charX != m_lastClickX || charY != m_lastClickY) )
|
||||
{
|
||||
computer.queueEvent( "mouse_drag", new Object[] {
|
||||
m_lastClickButton + 1, charX + 1, charY + 1
|
||||
} );
|
||||
computer.mouseDrag( m_lastClickButton + 1, charX + 1, charY + 1 );
|
||||
m_lastClickX = charX;
|
||||
m_lastClickY = charY;
|
||||
}
|
||||
@ -455,18 +424,12 @@ public class WidgetTerminal extends Widget
|
||||
private void queueEvent( String event )
|
||||
{
|
||||
IComputer computer = m_computer.getComputer();
|
||||
if( computer != null )
|
||||
{
|
||||
computer.queueEvent( event );
|
||||
}
|
||||
if( computer != null ) computer.queueEvent( event );
|
||||
}
|
||||
|
||||
private void queueEvent( String event, Object[] args )
|
||||
{
|
||||
IComputer computer = m_computer.getComputer();
|
||||
if( computer != null )
|
||||
{
|
||||
computer.queueEvent( event, args );
|
||||
}
|
||||
if( computer != null ) computer.queueEvent( event, args );
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,7 @@
|
||||
package dan200.computercraft.shared.command;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.computer.core.*;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@ -22,6 +19,7 @@ import javax.annotation.Nullable;
|
||||
public class ContainerViewComputer extends Container implements IContainerComputer
|
||||
{
|
||||
private final IComputer computer;
|
||||
private final InputState input = new InputState( this );
|
||||
|
||||
public ContainerViewComputer( IComputer computer )
|
||||
{
|
||||
@ -67,4 +65,18 @@ public class ContainerViewComputer extends Container implements IContainerComput
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputState getInput()
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed( EntityPlayer player )
|
||||
{
|
||||
super.onContainerClosed( player );
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@ import com.google.common.base.Objects;
|
||||
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;
|
||||
import dan200.computercraft.shared.network.server.*;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class ClientComputer extends ClientTerminal implements IComputer
|
||||
@ -116,6 +114,42 @@ public class ClientComputer extends ClientTerminal implements IComputer
|
||||
NetworkHandler.sendToServer( new QueueEventServerMessage( m_instanceID, event, arguments ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyDown( int key, boolean repeat )
|
||||
{
|
||||
NetworkHandler.sendToServer( new KeyEventServerMessage( m_instanceID, repeat ? KeyEventServerMessage.TYPE_REPEAT : KeyEventServerMessage.TYPE_DOWN, key ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyUp( int key )
|
||||
{
|
||||
NetworkHandler.sendToServer( new KeyEventServerMessage( m_instanceID, KeyEventServerMessage.TYPE_UP, key ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick( int button, int x, int y )
|
||||
{
|
||||
NetworkHandler.sendToServer( new MouseEventServerMessage( m_instanceID, MouseEventServerMessage.TYPE_CLICK, button, x, y ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp( int button, int x, int y )
|
||||
{
|
||||
NetworkHandler.sendToServer( new MouseEventServerMessage( m_instanceID, MouseEventServerMessage.TYPE_UP, button, x, y ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDrag( int button, int x, int y )
|
||||
{
|
||||
NetworkHandler.sendToServer( new MouseEventServerMessage( m_instanceID, MouseEventServerMessage.TYPE_DRAG, button, x, y ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseScroll( int direction, int x, int y )
|
||||
{
|
||||
NetworkHandler.sendToServer( new MouseEventServerMessage( m_instanceID, MouseEventServerMessage.TYPE_SCROLL, direction, x, y ) );
|
||||
}
|
||||
|
||||
public void setState( ComputerState state, NBTTagCompound userData )
|
||||
{
|
||||
boolean oldOn = m_on;
|
||||
|
@ -9,7 +9,7 @@ package dan200.computercraft.shared.computer.core;
|
||||
import dan200.computercraft.shared.common.ITerminal;
|
||||
import dan200.computercraft.shared.computer.blocks.ComputerState;
|
||||
|
||||
public interface IComputer extends ITerminal
|
||||
public interface IComputer extends ITerminal, InputHandler
|
||||
{
|
||||
int getInstanceID();
|
||||
|
||||
@ -29,13 +29,14 @@ public interface IComputer extends ITerminal
|
||||
|
||||
void reboot();
|
||||
|
||||
@Override
|
||||
void queueEvent( String event, Object[] arguments );
|
||||
|
||||
default void queueEvent( String event )
|
||||
{
|
||||
queueEvent( event, null );
|
||||
}
|
||||
|
||||
void queueEvent( String event, Object[] arguments );
|
||||
|
||||
default ComputerState getState()
|
||||
{
|
||||
if( !isOn() ) return ComputerState.Off;
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.computer.core;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@ -23,4 +24,15 @@ public interface IContainerComputer
|
||||
*/
|
||||
@Nullable
|
||||
IComputer getComputer();
|
||||
|
||||
/**
|
||||
* Get the input controller for this container.
|
||||
*
|
||||
* @return This container's input.
|
||||
*/
|
||||
@Nonnull
|
||||
default InputState getInput()
|
||||
{
|
||||
return new InputState( this );
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.computer.core;
|
||||
|
||||
/**
|
||||
* Receives some input and forwards it to a computer
|
||||
*
|
||||
* @see InputState
|
||||
* @see IComputer
|
||||
*/
|
||||
public interface InputHandler
|
||||
{
|
||||
void queueEvent( String event, Object[] arguments );
|
||||
|
||||
default void keyDown( int key, boolean repeat )
|
||||
{
|
||||
queueEvent( "key", new Object[] { key, repeat } );
|
||||
}
|
||||
|
||||
default void keyUp( int key )
|
||||
{
|
||||
queueEvent( "key_up", new Object[] { key } );
|
||||
}
|
||||
|
||||
default void mouseClick( int button, int x, int y )
|
||||
{
|
||||
queueEvent( "mouse_click", new Object[] { button, x, y } );
|
||||
}
|
||||
|
||||
default void mouseUp( int button, int x, int y )
|
||||
{
|
||||
queueEvent( "mouse_up", new Object[] { button, x, y } );
|
||||
}
|
||||
|
||||
default void mouseDrag( int button, int x, int y )
|
||||
{
|
||||
queueEvent( "mouse_drag", new Object[] { button, x, y } );
|
||||
}
|
||||
|
||||
default void mouseScroll( int direction, int x, int y )
|
||||
{
|
||||
queueEvent( "mouse_scroll", new Object[] { direction, x, y } );
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.computer.core;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntIterator;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntSet;
|
||||
|
||||
/**
|
||||
* An {@link InputHandler} which keeps track of the current key and mouse state, and releases them when the container
|
||||
* is closed.
|
||||
*/
|
||||
public class InputState implements InputHandler
|
||||
{
|
||||
private final IContainerComputer owner;
|
||||
private final IntSet keysDown = new IntOpenHashSet( 4 );
|
||||
|
||||
private int lastMouseX;
|
||||
private int lastMouseY;
|
||||
private int lastMouseDown = -1;
|
||||
|
||||
public InputState( IContainerComputer owner )
|
||||
{
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueEvent( String event, Object[] arguments )
|
||||
{
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.queueEvent( event, arguments );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyDown( int key, boolean repeat )
|
||||
{
|
||||
keysDown.add( key );
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.keyDown( key, repeat );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyUp( int key )
|
||||
{
|
||||
keysDown.remove( key );
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.keyUp( key );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick( int button, int x, int y )
|
||||
{
|
||||
lastMouseX = x;
|
||||
lastMouseY = y;
|
||||
lastMouseDown = button;
|
||||
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.mouseClick( button, x, y );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp( int button, int x, int y )
|
||||
{
|
||||
lastMouseX = x;
|
||||
lastMouseY = y;
|
||||
lastMouseDown = -1;
|
||||
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.mouseUp( button, x, y );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDrag( int button, int x, int y )
|
||||
{
|
||||
lastMouseX = x;
|
||||
lastMouseY = y;
|
||||
lastMouseDown = button;
|
||||
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.mouseDrag( button, x, y );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseScroll( int direction, int x, int y )
|
||||
{
|
||||
lastMouseX = x;
|
||||
lastMouseY = y;
|
||||
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null ) computer.mouseScroll( direction, x, y );
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
IComputer computer = owner.getComputer();
|
||||
if( computer != null )
|
||||
{
|
||||
IntIterator keys = keysDown.iterator();
|
||||
while( keys.hasNext() ) computer.keyUp( keys.nextInt() );
|
||||
|
||||
if( lastMouseDown != -1 ) computer.mouseUp( lastMouseDown, lastMouseX, lastMouseY );
|
||||
}
|
||||
|
||||
keysDown.clear();
|
||||
lastMouseDown = -1;
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ServerComputer extends ServerTerminal implements IComputer, IComputerEnvironment
|
||||
@ -367,14 +368,19 @@ public class ServerComputer extends ServerTerminal implements IComputer, IComput
|
||||
return ComputerCraftAPI.createUniqueNumberedSaveDir( m_world, "computer" );
|
||||
}
|
||||
|
||||
public boolean isInteracting( EntityPlayer player )
|
||||
@Nullable
|
||||
public IContainerComputer getContainer( EntityPlayer player )
|
||||
{
|
||||
if( player == null ) return false;
|
||||
if( player == null ) return null;
|
||||
|
||||
Container container = player.openContainer;
|
||||
if( !(container instanceof IContainerComputer) ) return false;
|
||||
if( !(container instanceof IContainerComputer) ) return null;
|
||||
|
||||
IComputer computer = ((IContainerComputer) container).getComputer();
|
||||
return computer == this;
|
||||
return (IContainerComputer) container;
|
||||
}
|
||||
|
||||
protected boolean isInteracting( EntityPlayer player )
|
||||
{
|
||||
return getContainer( player ) != null;
|
||||
}
|
||||
}
|
||||
|
@ -9,32 +9,47 @@ package dan200.computercraft.shared.computer.inventory;
|
||||
import dan200.computercraft.shared.computer.blocks.TileComputer;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ContainerComputer extends Container
|
||||
implements IContainerComputer
|
||||
public class ContainerComputer extends Container implements IContainerComputer
|
||||
{
|
||||
private TileComputer m_computer;
|
||||
private final TileComputer computer;
|
||||
private final InputState input = new InputState( this );
|
||||
|
||||
public ContainerComputer( TileComputer computer )
|
||||
{
|
||||
m_computer = computer;
|
||||
this.computer = computer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith( @Nonnull EntityPlayer player )
|
||||
{
|
||||
return m_computer.isUseableByPlayer( player );
|
||||
return computer.isUseableByPlayer( player );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IComputer getComputer()
|
||||
{
|
||||
return m_computer.getServerComputer();
|
||||
return computer.getServerComputer();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputState getInput()
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed( EntityPlayer player )
|
||||
{
|
||||
super.onContainerClosed( player );
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ 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 dan200.computercraft.shared.network.server.*;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
@ -34,6 +32,8 @@ public final class 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 KEY_EVENT_SERVER_MESSAGE = 3;
|
||||
private static final int MOUSE_EVENT_SERVER_MESSAGE = 4;
|
||||
|
||||
private static final int CHAT_TABLE_CLIENT_MESSAGE = 10;
|
||||
private static final int COMPUTER_DATA_CLIENT_MESSAGE = 11;
|
||||
@ -49,6 +49,8 @@ public final class NetworkHandler
|
||||
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 );
|
||||
registerMainThread( NetworkHandler.KEY_EVENT_SERVER_MESSAGE, Side.SERVER, KeyEventServerMessage::new );
|
||||
registerMainThread( NetworkHandler.MOUSE_EVENT_SERVER_MESSAGE, Side.SERVER, MouseEventServerMessage::new );
|
||||
|
||||
// Client messages
|
||||
registerMainThread( NetworkHandler.PLAY_RECORD_CLIENT_MESSAGE, Side.CLIENT, PlayRecordClientMessage::new );
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
@ -40,7 +41,7 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( ServerComputer computer )
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
switch( action )
|
||||
{
|
||||
|
@ -7,9 +7,9 @@
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
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;
|
||||
|
||||
@ -46,24 +46,17 @@ public abstract class ComputerServerMessage implements NetworkMessage
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
public ServerComputer getComputer( MessageContext context )
|
||||
{
|
||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instanceId );
|
||||
if( computer == null ) return null;
|
||||
|
||||
// Verify the player is interacting with a computer.
|
||||
EntityPlayer player = context.getServerHandler().player;
|
||||
if( player == null || !computer.isInteracting( player ) ) return null;
|
||||
|
||||
return computer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( MessageContext context )
|
||||
{
|
||||
ServerComputer computer = getComputer( context );
|
||||
if( computer != null ) handle( computer );
|
||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instanceId );
|
||||
if( computer == null ) return;
|
||||
|
||||
IContainerComputer container = computer.getContainer( context.getServerHandler().player );
|
||||
if( container == null ) return;
|
||||
|
||||
handle( computer, container );
|
||||
}
|
||||
|
||||
protected abstract void handle( ServerComputer computer );
|
||||
protected abstract void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container );
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class KeyEventServerMessage extends ComputerServerMessage
|
||||
{
|
||||
public static final int TYPE_DOWN = 0;
|
||||
public static final int TYPE_REPEAT = 1;
|
||||
public static final int TYPE_UP = 2;
|
||||
|
||||
private int type;
|
||||
private int key;
|
||||
|
||||
public KeyEventServerMessage( int instanceId, int type, int key )
|
||||
{
|
||||
super( instanceId );
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public KeyEventServerMessage()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.toBytes( buf );
|
||||
buf.writeByte( type );
|
||||
buf.writeVarInt( key );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
key = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
InputState input = container.getInput();
|
||||
if( type == TYPE_UP )
|
||||
{
|
||||
input.keyUp( key );
|
||||
}
|
||||
else
|
||||
{
|
||||
input.keyDown( key, type == TYPE_REPEAT );
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class MouseEventServerMessage extends ComputerServerMessage
|
||||
{
|
||||
public static final int TYPE_CLICK = 0;
|
||||
public static final int TYPE_DRAG = 1;
|
||||
public static final int TYPE_UP = 2;
|
||||
public static final int TYPE_SCROLL = 3;
|
||||
|
||||
private int type;
|
||||
private int x;
|
||||
private int y;
|
||||
private int arg;
|
||||
|
||||
public MouseEventServerMessage( int instanceId, int type, int arg, int x, int y )
|
||||
{
|
||||
super( instanceId );
|
||||
this.type = type;
|
||||
this.arg = arg;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public MouseEventServerMessage()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.toBytes( buf );
|
||||
buf.writeByte( type );
|
||||
buf.writeVarInt( arg );
|
||||
buf.writeVarInt( x );
|
||||
buf.writeVarInt( y );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
arg = buf.readVarInt();
|
||||
x = buf.readVarInt();
|
||||
y = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
InputState input = container.getInput();
|
||||
switch( type )
|
||||
{
|
||||
case TYPE_CLICK:
|
||||
input.mouseClick( arg, x, y );
|
||||
break;
|
||||
case TYPE_DRAG:
|
||||
input.mouseDrag( arg, x, y );
|
||||
break;
|
||||
case TYPE_UP:
|
||||
input.mouseUp( arg, x, y );
|
||||
break;
|
||||
case TYPE_SCROLL:
|
||||
input.mouseScroll( arg, x, y );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
|
||||
package dan200.computercraft.shared.network.server;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.util.NBTUtil;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@ -55,7 +56,7 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( ServerComputer computer )
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
computer.queueEvent( event, args );
|
||||
}
|
||||
|
@ -8,17 +8,20 @@ package dan200.computercraft.shared.pocket.inventory;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumHand;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ContainerPocketComputer extends ContainerHeldItem
|
||||
implements IContainerComputer
|
||||
public class ContainerPocketComputer extends ContainerHeldItem implements IContainerComputer
|
||||
{
|
||||
private final InputState input = new InputState( this );
|
||||
|
||||
public ContainerPocketComputer( EntityPlayer player, EnumHand hand )
|
||||
{
|
||||
super( player, hand );
|
||||
@ -38,4 +41,18 @@ public class ContainerPocketComputer extends ContainerHeldItem
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputState getInput()
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed( EntityPlayer player )
|
||||
{
|
||||
super.onContainerClosed( player );
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ package dan200.computercraft.shared.turtle.inventory;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.IContainerComputer;
|
||||
import dan200.computercraft.shared.computer.core.InputState;
|
||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||
import dan200.computercraft.shared.turtle.core.TurtleBrain;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@ -21,16 +22,16 @@ import net.minecraft.item.ItemStack;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ContainerTurtle extends Container
|
||||
implements IContainerComputer
|
||||
public class ContainerTurtle extends Container implements IContainerComputer
|
||||
{
|
||||
private static final int PROGRESS_ID_SELECTED_SLOT = 0;
|
||||
|
||||
public final int m_playerInvStartY;
|
||||
public final int m_turtleInvStartX;
|
||||
|
||||
protected ITurtleAccess m_turtle;
|
||||
private final ITurtleAccess m_turtle;
|
||||
private IComputer m_computer;
|
||||
private final InputState input = new InputState( this );
|
||||
private int m_selectedSlot;
|
||||
|
||||
protected ContainerTurtle( IInventory playerInventory, ITurtleAccess turtle, int playerInvStartY, int turtleInvStartX )
|
||||
@ -199,4 +200,18 @@ public class ContainerTurtle extends Container
|
||||
{
|
||||
return m_computer;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputState getInput()
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed( EntityPlayer player )
|
||||
{
|
||||
super.onContainerClosed( player );
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user