diff --git a/src/main/java/dan200/computercraft/client/proxy/ComputerCraftProxyClient.java b/src/main/java/dan200/computercraft/client/proxy/ComputerCraftProxyClient.java index b420f619d..370fcf283 100644 --- a/src/main/java/dan200/computercraft/client/proxy/ComputerCraftProxyClient.java +++ b/src/main/java/dan200/computercraft/client/proxy/ComputerCraftProxyClient.java @@ -9,6 +9,7 @@ package dan200.computercraft.client.proxy; import dan200.computercraft.ComputerCraft; import dan200.computercraft.client.gui.*; import dan200.computercraft.client.render.*; +import dan200.computercraft.shared.command.CommandCopy; import dan200.computercraft.shared.command.ContainerViewComputer; import dan200.computercraft.shared.computer.blocks.ComputerState; import dan200.computercraft.shared.computer.blocks.TileComputer; @@ -51,6 +52,7 @@ import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; +import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.client.event.ModelRegistryEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderHandEvent; @@ -88,6 +90,9 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon // Setup client forge handlers registerForgeHandlers(); + + // Register any client-specific commands + ClientCommandHandler.instance.registerCommand( CommandCopy.INSTANCE ); } @SubscribeEvent diff --git a/src/main/java/dan200/computercraft/shared/command/CommandCopy.java b/src/main/java/dan200/computercraft/shared/command/CommandCopy.java new file mode 100644 index 000000000..5477ac9a5 --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/command/CommandCopy.java @@ -0,0 +1,75 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.command; + +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; +import net.minecraft.util.text.TextComponentTranslation; +import net.minecraft.util.text.event.ClickEvent; +import net.minecraft.util.text.event.HoverEvent; +import net.minecraftforge.client.IClientCommand; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nonnull; + +public class CommandCopy extends CommandBase implements IClientCommand +{ + public static final CommandCopy INSTANCE = new CommandCopy(); + private static final String NAME = "computercraft_copy"; + + private CommandCopy() + { + } + + @Override + public boolean allowUsageWithoutPrefix( ICommandSender sender, String message ) + { + return false; + } + + @Nonnull + @Override + public String getName() + { + return NAME; + } + + @Nonnull + @Override + public String getUsage( @Nonnull ICommandSender sender ) + { + return "computercraft_copy "; + } + + @Override + public int getRequiredPermissionLevel() + { + return 0; + } + + @Override + @SideOnly( Side.CLIENT ) + public void execute( @Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args ) + { + String message = String.join( " ", args ); + if( !message.isEmpty() ) GuiScreen.setClipboardString( message ); + } + + public static ITextComponent createCopyText( String text ) + { + TextComponentString name = new TextComponentString( text ); + name.getStyle() + .setClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/" + NAME + " " + text ) ) + .setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new TextComponentTranslation( "gui.computercraft:tooltip.copy" ) ) ); + return name; + } +} diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java index ef62d394c..a532544c9 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileCable.java @@ -11,6 +11,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.network.wired.IWiredElement; import dan200.computercraft.api.network.wired.IWiredNode; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.shared.command.CommandCopy; import dan200.computercraft.shared.common.BlockGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.common.BlockCable; @@ -407,13 +408,15 @@ public class TileCable extends TileModemBase if( oldPeriphName != null ) { player.sendMessage( - new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_disconnected", oldPeriphName ) + new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_disconnected", + CommandCopy.createCopyText( oldPeriphName ) ) ); } if( periphName != null ) { player.sendMessage( - new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_connected", periphName ) + new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_connected", + CommandCopy.createCopyText( periphName ) ) ); } return true; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java index a80d1d900..336d5d19f 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileWiredModemFull.java @@ -11,6 +11,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.network.wired.IWiredElement; import dan200.computercraft.api.network.wired.IWiredNode; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.shared.command.CommandCopy; import dan200.computercraft.shared.peripheral.common.BlockCable; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.wired.CapabilityWiredElement; @@ -20,6 +21,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; @@ -181,29 +183,14 @@ public class TileWiredModemFull extends TilePeripheralBase if( !getWorld().isRemote ) { // On server, we interacted if a peripheral was found - Set oldPeriphName = getConnectedPeripheralNames(); + Set oldPeriphNames = getConnectedPeripheralNames(); togglePeripheralAccess(); - Set periphName = getConnectedPeripheralNames(); + Set periphNames = getConnectedPeripheralNames(); - if( !Objects.equal( periphName, oldPeriphName ) ) + if( !Objects.equal( periphNames, oldPeriphNames ) ) { - if( !oldPeriphName.isEmpty() ) - { - List names = new ArrayList<>( oldPeriphName ); - names.sort( Comparator.naturalOrder() ); - - player.sendMessage( - new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_disconnected", String.join( ", ", names ) ) - ); - } - if( !periphName.isEmpty() ) - { - List names = new ArrayList<>( periphName ); - names.sort( Comparator.naturalOrder() ); - player.sendMessage( - new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_connected", String.join( ", ", names ) ) - ); - } + sendPeripheralChanges( player, "gui.computercraft:wired_modem.peripheral_disconnected", oldPeriphNames ); + sendPeripheralChanges( player, "gui.computercraft:wired_modem.peripheral_connected", periphNames ); } return true; @@ -216,6 +203,23 @@ public class TileWiredModemFull extends TilePeripheralBase } } + private static void sendPeripheralChanges( EntityPlayer player, String kind, Collection peripherals ) + { + if( peripherals.isEmpty() ) return; + + List names = new ArrayList<>( peripherals ); + names.sort( Comparator.naturalOrder() ); + + TextComponentString base = new TextComponentString( "" ); + for( int i = 0; i < names.size(); i++ ) + { + if( i > 0 ) base.appendText( ", " ); + base.appendSibling( CommandCopy.createCopyText( names.get( i ) ) ); + } + + player.sendMessage( new TextComponentTranslation( kind, base ) ); + } + @Override public void readFromNBT( NBTTagCompound tag ) { diff --git a/src/main/resources/assets/computercraft/lang/en_us.lang b/src/main/resources/assets/computercraft/lang/en_us.lang index d5a49046e..324203b2a 100644 --- a/src/main/resources/assets/computercraft/lang/en_us.lang +++ b/src/main/resources/assets/computercraft/lang/en_us.lang @@ -42,6 +42,8 @@ upgrade.computercraft:speaker.adjective=Noisy gui.computercraft:wired_modem.peripheral_connected=Peripheral "%s" connected to network gui.computercraft:wired_modem.peripheral_disconnected=Peripheral "%s" disconnected from network +gui.computercraft:tooltip.copy=Copy to clipboard + gui.computercraft:config.http_enable=Enable HTTP API gui.computercraft:config.http_websocket_enable=Enable HTTP websockets gui.computercraft:config.http_whitelist=HTTP whitelist