36
36
import cloud .commandframework .keys .CloudKeyHolder ;
37
37
import cloud .commandframework .keys .SimpleCloudKey ;
38
38
import cloud .commandframework .permission .CommandPermission ;
39
+ import cloud .commandframework .types .tuples .Pair ;
39
40
import java .util .Collections ;
40
41
import java .util .HashMap ;
41
42
import java .util .LinkedList ;
43
+ import java .util .List ;
42
44
import java .util .Map ;
45
+ import java .util .NoSuchElementException ;
43
46
import java .util .Optional ;
44
47
import java .util .function .Function ;
45
48
import java .util .function .Supplier ;
49
+ import java .util .stream .Collectors ;
46
50
import org .apiguardian .api .API ;
47
51
import org .checkerframework .checker .nullness .qual .NonNull ;
48
52
import org .checkerframework .checker .nullness .qual .Nullable ;
56
60
public class CommandContext <C > {
57
61
58
62
private final CaptionVariableReplacementHandler captionVariableReplacementHandler ;
59
- private final Map < CommandArgument <C , ?>, ArgumentTiming > argumentTimings = new HashMap <>();
63
+ private final List < ArgumentContext <C , ?>> argumentContexts = new LinkedList <>();
60
64
private final FlagContext flagContext = FlagContext .create ();
61
65
private final Map <CloudKey <?>, Object > internalStorage = new HashMap <>();
62
66
private final C commandSender ;
@@ -603,20 +607,103 @@ public <T> T computeIfAbsent(
603
607
*
604
608
* @param argument Argument
605
609
* @return Created timing instance
610
+ *
611
+ * @deprecated This has been replaced by {@link #createArgumentContext(CommandArgument)}
606
612
*/
613
+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
614
+ @ Deprecated
607
615
public @ NonNull ArgumentTiming createTiming (final @ NonNull CommandArgument <C , ?> argument ) {
608
- final ArgumentTiming argumentTiming = new ArgumentTiming ();
609
- this .argumentTimings .put (argument , argumentTiming );
610
- return argumentTiming ;
616
+ return new ArgumentTiming ();
611
617
}
612
618
613
619
/**
614
620
* Get an immutable view of the argument timings map
615
621
*
616
622
* @return Argument timings
623
+ * @deprecated Replaced with {@link #argumentContexts()}
617
624
*/
625
+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
626
+ @ Deprecated
618
627
public @ NonNull Map <CommandArgument <@ NonNull C , @ NonNull ?>, ArgumentTiming > getArgumentTimings () {
619
- return Collections .unmodifiableMap (this .argumentTimings );
628
+ return this .argumentContexts .stream ()
629
+ .map (context -> Pair .of (
630
+ context .argument (),
631
+ new ArgumentTiming (
632
+ context .startTime (),
633
+ context .endTime (),
634
+ context .success ()
635
+ )
636
+ )
637
+ ).collect (Collectors .toMap (Pair ::getFirst , Pair ::getSecond ));
638
+ }
639
+
640
+ /**
641
+ * Create an argument context instance for the given argument
642
+ *
643
+ * @param argument the argument
644
+ * @return the created context
645
+ * @param <T> the type of the argument
646
+ * @since 1.9.0
647
+ */
648
+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
649
+ public <T > @ NonNull ArgumentContext <C , T > createArgumentContext (final @ NonNull CommandArgument <C , T > argument ) {
650
+ final ArgumentContext <C , T > argumentContext = new ArgumentContext <>(argument );
651
+ this .argumentContexts .add (argumentContext );
652
+ return argumentContext ;
653
+ }
654
+
655
+ /**
656
+ * Returns the context for the given argument
657
+ *
658
+ * @param argument the argument
659
+ * @return the context
660
+ * @param <T> the type of the argument
661
+ * @since 1.9.0
662
+ */
663
+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
664
+ @ SuppressWarnings ("unchecked" )
665
+ public <T > @ NonNull ArgumentContext <C , T > argumentContext (final @ NonNull CommandArgument <C , T > argument ) {
666
+ return this .argumentContexts .stream ().filter (context -> context .argument ().equals (argument ))
667
+ .findFirst ()
668
+ .map (context -> (ArgumentContext <C , T >) context )
669
+ .orElseThrow (NoSuchElementException ::new );
670
+ }
671
+
672
+ /**
673
+ * Returns the context for the argument at the given position
674
+ *
675
+ * @param position the position
676
+ * @return the context
677
+ * @since 1.9.0
678
+ */
679
+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
680
+ public @ NonNull ArgumentContext <C , ?> argumentContext (final int position ) {
681
+ return this .argumentContexts .get (position );
682
+ }
683
+
684
+ /**
685
+ * Return the context for the argument with the given name.
686
+ *
687
+ * @param name the name
688
+ * @return the context
689
+ * @since 1.9.0
690
+ */
691
+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
692
+ public @ NonNull ArgumentContext <C , ?> argumentContext (final String name ) {
693
+ return this .argumentContexts .stream ().filter (context -> context .argument ().getName ().equals (name ))
694
+ .findFirst ()
695
+ .orElseThrow (NoSuchElementException ::new );
696
+ }
697
+
698
+ /**
699
+ * Return an unmodifiable view of the stored argument contexts
700
+ *
701
+ * @return the contexts
702
+ * @since 1.9.0
703
+ */
704
+ @ API (status = API .Status .MAINTAINED , since = "1.9.0" )
705
+ public @ NonNull List <@ NonNull ArgumentContext <@ NonNull C , @ NonNull ?>> argumentContexts () {
706
+ return Collections .unmodifiableList (this .argumentContexts );
620
707
}
621
708
622
709
/**
@@ -680,16 +767,19 @@ public void setCurrentArgument(final @Nullable CommandArgument<C, ?> argument) {
680
767
* parsed.
681
768
* <p>
682
769
* The times are measured in nanoseconds.
770
+ *
771
+ * @deprecated Superseded by {@link ArgumentContext}
683
772
*/
684
- @ API (status = API .Status .STABLE )
773
+ @ Deprecated
774
+ @ API (status = API .Status .DEPRECATED , since = "1.9.0" )
685
775
public static final class ArgumentTiming {
686
776
687
777
private long start ;
688
778
private long end ;
689
779
private boolean success ;
690
780
691
781
/**
692
- * Created a new argument timing instance
782
+ * Creates a new argument timing instance
693
783
*
694
784
* @param start Start time (in nanoseconds)
695
785
* @param end End time (in nanoseconds)
@@ -702,7 +792,7 @@ public ArgumentTiming(final long start, final long end, final boolean success) {
702
792
}
703
793
704
794
/**
705
- * Created a new argument timing instance without an end time
795
+ * Creates a new argument timing instance without an end time
706
796
*
707
797
* @param start Start time (in nanoseconds)
708
798
*/
@@ -712,7 +802,7 @@ public ArgumentTiming(final long start) {
712
802
}
713
803
714
804
/**
715
- * Created a new argument timing instance
805
+ * Creates a new argument timing instance
716
806
*/
717
807
public ArgumentTiming () {
718
808
this (-1 , -1 , false );
0 commit comments