1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-05 19:42:54 +00:00
SquidDev f9e13ca67a Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.

Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:

 - Flatten everything. For instance, there are now three instances of
   BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
   more BlockPeripheral (thank heavens) - there's separate block classes
   for each peripheral type.

 - Remove pretty much all legacy code. As we're breaking world
   compatibility anyway, we can remove all the code to load worlds from
   1.4 days.
 - The command system is largely rewriten to take advantage of 1.13's
   new system. It's very fancy!

 - WidgetTerminal now uses Minecraft's "GUI listener" system.

 - BREAKING CHANGE: All the codes in keys.lua are different, due to the
   move to LWJGL 3. Hopefully this won't have too much of an impact.

   I don't want to map to the old key codes on the Java side, as there
   always ends up being small but slight inconsistencies. IMO it's
   better to make a clean break - people should be using keys rather
   than hard coding the constants anyway.

 - commands.list now allows fetching sub-commands. The ROM has already
   been updated to allow fancy usage such as commands.time.set("noon").

 - Turtles, modems and cables can be waterlogged.
2019-04-02 20:59:48 +01:00

211 lines
7.1 KiB
Java

/*
* 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.command.arguments;
import com.google.gson.JsonObject;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import net.minecraft.command.CommandSource;
import net.minecraft.command.arguments.IArgumentSerializer;
import net.minecraft.network.PacketBuffer;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static dan200.computercraft.shared.command.CommandUtils.suggest;
import static dan200.computercraft.shared.command.CommandUtils.suggestOnServer;
import static dan200.computercraft.shared.command.Exceptions.COMPUTER_ARG_NONE;
public final class ComputersArgumentType implements ArgumentType<ComputersArgumentType.ComputersSupplier>
{
private static final ComputersArgumentType MANY = new ComputersArgumentType( false );
private static final ComputersArgumentType SOME = new ComputersArgumentType( true );
private static final List<String> EXAMPLES = Arrays.asList(
"0", "#0", "@Label", "~Advanced"
);
public static ComputersArgumentType manyComputers()
{
return MANY;
}
public static ComputersArgumentType someComputers()
{
return SOME;
}
public static Collection<ServerComputer> getComputersArgument( CommandContext<CommandSource> context, String name ) throws CommandSyntaxException
{
return context.getArgument( name, ComputersSupplier.class ).unwrap( context.getSource() );
}
private final boolean requireSome;
private ComputersArgumentType( boolean requireSome )
{
this.requireSome = requireSome;
}
@Override
public ComputersSupplier parse( StringReader reader ) throws CommandSyntaxException
{
int start = reader.getCursor();
char kind = reader.peek();
ComputersSupplier computers;
if( kind == '@' )
{
reader.skip();
String label = reader.readUnquotedString();
computers = getComputers( x -> Objects.equals( label, x.getLabel() ) );
}
else if( kind == '~' )
{
reader.skip();
String family = reader.readUnquotedString();
computers = getComputers( x -> x.getFamily().name().equalsIgnoreCase( family ) );
}
else if( kind == '#' )
{
reader.skip();
int id = reader.readInt();
computers = getComputers( x -> x.getID() == id );
}
else
{
int instance = reader.readInt();
computers = s -> {
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
return computer == null ? Collections.emptyList() : Collections.singletonList( computer );
};
}
if( requireSome )
{
String selector = reader.getString().substring( start, reader.getCursor() );
return source -> {
Collection<ServerComputer> matched = computers.unwrap( source );
if( matched.isEmpty() ) throw COMPUTER_ARG_NONE.create( selector );
return matched;
};
}
else
{
return computers;
}
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions( CommandContext<S> context, SuggestionsBuilder builder )
{
String remaining = builder.getRemaining();
// We can run this one on the client, for obvious reasons.
if( remaining.startsWith( "~" ) )
{
return suggest( builder, ComputerFamily.values(), x -> "~" + x.name() );
}
// Verify we've a command source and we're running on the server
return suggestOnServer( context, builder, s -> {
if( remaining.startsWith( "@" ) )
{
suggestComputers( builder, remaining, x -> {
String label = x.getLabel();
return label == null ? null : "@" + label;
} );
}
else if( remaining.startsWith( "#" ) )
{
suggestComputers( builder, remaining, c -> "#" + c.getID() );
}
else
{
suggestComputers( builder, remaining, c -> Integer.toString( c.getInstanceID() ) );
}
return builder.buildFuture();
} );
}
@Override
public Collection<String> getExamples()
{
return EXAMPLES;
}
private static void suggestComputers( SuggestionsBuilder builder, String remaining, Function<ServerComputer, String> renderer )
{
remaining = remaining.toLowerCase( Locale.ROOT );
for( ServerComputer computer : ComputerCraft.serverComputerRegistry.getComputers() )
{
String converted = renderer.apply( computer );
if( converted != null && converted.toLowerCase( Locale.ROOT ).startsWith( remaining ) )
{
builder.suggest( converted );
}
}
}
private static ComputersSupplier getComputers( Predicate<ServerComputer> predicate )
{
return s -> Collections.unmodifiableList( ComputerCraft.serverComputerRegistry
.getComputers()
.stream()
.filter( predicate )
.collect( Collectors.toList() )
);
}
public static class Serializer implements IArgumentSerializer<ComputersArgumentType>
{
@Override
public void write( @Nonnull ComputersArgumentType arg, @Nonnull PacketBuffer buf )
{
buf.writeBoolean( arg.requireSome );
}
@Nonnull
@Override
public ComputersArgumentType read( @Nonnull PacketBuffer buf )
{
return buf.readBoolean() ? SOME : MANY;
}
@Override
public void write( @Nonnull ComputersArgumentType arg, @Nonnull JsonObject json )
{
json.addProperty( "requireSome", arg.requireSome );
}
}
@FunctionalInterface
public interface ComputersSupplier
{
Collection<ServerComputer> unwrap( CommandSource source ) throws CommandSyntaxException;
}
public static Set<ServerComputer> unwrap( CommandSource source, Collection<ComputersSupplier> suppliers ) throws CommandSyntaxException
{
Set<ServerComputer> computers = new HashSet<>();
for( ComputersSupplier supplier : suppliers ) computers.addAll( supplier.unwrap( source ) );
return computers;
}
}