001package com.mrivanplays.commandworker.core.argument; 002 003import java.util.ArrayList; 004import java.util.Collections; 005import java.util.List; 006import java.util.Objects; 007import org.jetbrains.annotations.NotNull; 008 009/** 010 * Represents literal argument. This is a wrapper for brigadier's LiteralArgumentBuilder, wrapped 011 * for easier use. 012 */ 013public final class LiteralArgument implements Argument { 014 015 /** 016 * Creates a new {@link LiteralArgument} 017 * 018 * @param name completion value you see ingame. 019 * @return literal argument 020 */ 021 @NotNull 022 public static LiteralArgument literal(@NotNull String name) { 023 return new LiteralArgument(name); 024 } 025 026 private final String name; 027 private List<Argument> children; 028 029 private boolean shouldExecuteCommand = false; 030 031 private LiteralArgument(@NotNull String name) { 032 this.name = Objects.requireNonNull(name, "name"); 033 this.children = new ArrayList<>(); 034 } 035 036 @Override 037 public @NotNull String getName() { 038 return name; 039 } 040 041 /** 042 * Adds a child to this argument. 043 * 044 * @param other child 045 * @return this instance for chaining 046 */ 047 @NotNull 048 public LiteralArgument then(@NotNull Argument other) { 049 Objects.requireNonNull(other, "other"); 050 children.add(other); 051 return this; 052 } 053 054 @NotNull 055 public LiteralArgument then(@NotNull Argument... other) { 056 Objects.requireNonNull(other, "other"); 057 // We're not using Collection#addAll because this way we save memory 058 //noinspection ManualArrayToCollectionCopy 059 for (Argument arg : other) { 060 //noinspection UseBulkOperation 061 children.add(arg); 062 } 063 return this; 064 } 065 066 @NotNull 067 public LiteralArgument then(@NotNull Iterable<Argument> iterable) { 068 Objects.requireNonNull(iterable, "iterable"); 069 for (Argument arg : iterable) { 070 children.add(arg); 071 } 072 return this; 073 } 074 075 /** 076 * @return this instance for chaining 077 * @see Argument#shouldExecuteCommand() 078 */ 079 @NotNull 080 public LiteralArgument markShouldExecuteCommand() { 081 this.shouldExecuteCommand = true; 082 return this; 083 } 084 085 @Override 086 public boolean shouldExecuteCommand() { 087 return shouldExecuteCommand; 088 } 089 090 @Override 091 public @NotNull List<Argument> getChildren() { 092 return Collections.unmodifiableList(children); 093 } 094 095 @Override 096 public String toString() { 097 return "LiteralArgument(name=" 098 + name 099 + ", shouldExecuteCommand=" 100 + shouldExecuteCommand 101 + ", children=" 102 + children 103 + ")"; 104 } 105}