1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-24 04:16:58 +00:00

Allow copying peripheral names from wired modems when attaching/detaching

This is implemented in a rather ugly way: we register a client command
(/computercraft_copy) which updates the clipboard, and run that via a
click handler on the chat message.

This hopefully makes wired modems a little easier to use. We'll see.
This commit is contained in:
SquidDev 2018-11-29 12:21:14 +00:00
parent 7e6eb62504
commit 3e6f6467af
5 changed files with 111 additions and 22 deletions

View File

@ -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

View File

@ -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 <text>";
}
@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;
}
}

View File

@ -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;

View File

@ -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<String> oldPeriphName = getConnectedPeripheralNames();
Set<String> oldPeriphNames = getConnectedPeripheralNames();
togglePeripheralAccess();
Set<String> periphName = getConnectedPeripheralNames();
Set<String> periphNames = getConnectedPeripheralNames();
if( !Objects.equal( periphName, oldPeriphName ) )
if( !Objects.equal( periphNames, oldPeriphNames ) )
{
if( !oldPeriphName.isEmpty() )
{
List<String> 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<String> 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<String> peripherals )
{
if( peripherals.isEmpty() ) return;
List<String> 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 )
{

View File

@ -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