Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Api] Expand Flag Api #8

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void init(WorldEditSlimefun plugin) {
completions.registerStaticCompletion("slimefun_blocks", Utils.SLIMEFUN_BLOCKS);
completions.registerAsyncCompletion("command_flags", context -> {
List<String> args = new ArrayList<>(Arrays.asList(context.getContextValueByName(String[].class, "commandFlags")));
List<String> availableFlags = new ArrayList<>(CommandFlags.FLAG_TYPES.keySet());
List<String> availableFlags = new ArrayList<>(CommandFlags.getFlagTypes().keySet());
availableFlags.removeAll(args);

if (args.isEmpty()) {
Expand All @@ -63,8 +63,8 @@ public static void init(WorldEditSlimefun plugin) {
}

String lastArg = args.get(args.size() - 1);
if (CommandFlags.FLAG_TYPES.containsKey(lastArg)) {
return CommandFlags.FLAG_TYPES.get(lastArg).getTabSuggestions(context);
if (CommandFlags.getFlagTypes().containsKey(lastArg)) {
return CommandFlags.getFlagTypes().get(lastArg).getTabSuggestions(context);
}

if (args.size() % 2 == 0) {
Expand Down Expand Up @@ -140,7 +140,7 @@ public void paste(Player player, @Default("INVALID") String sfId, String[] comma
});

for (CommandFlag<?> flag : flags) {
flag.apply(flags, sfItem, block);
flag.apply(player, flags, sfItem, block);
}
});
long time = System.currentTimeMillis() - start;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.aikar.commands.BukkitCommandCompletionContext;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.List;
Expand All @@ -19,7 +20,7 @@ public T getValue() {
return value;
}

public abstract void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block);
public abstract void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block);
public abstract boolean canApply(SlimefunItem sfItem);
public abstract Collection<String> getTabSuggestions(BukkitCommandCompletionContext context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,48 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class CommandFlags {
public static final Map<String, CommandFlag<?>> FLAG_TYPES = Map.of(
private static final Map<String, CommandFlag<?>> FLAG_TYPES = new HashMap<>(Map.of(
"--energy", new EnergyFlag(),
"--inputs", new InputsFlag(),
"--refill_inputs_task", new RefillInputsFlag(),
"--void_outputs_task", new VoidOutputsFlag(),
"--task_timeout", new TimeoutFlag()
);
));

/**
* Takes a type and value and converts it into a matching {@link CommandFlag}
* @param type The type of flag to get, formatted as "--type"
* @param value The value of the flag to get, format depends on the type provided
* @return the matching {@link CommandFlag} or null if one could not be found
*/
public static @Nullable CommandFlag<?> getFlag(@Nonnull String type, @Nonnull String value) {
CommandFlag<?> flag = FLAG_TYPES.get(type);
if (flag != null) {
return flag.ofValue(value);
}
return null;
}

public static List<CommandFlag<?>> getFlags(List<String> args) {
/**
* Translates the provided args into {@link CommandFlag CommandFlags}
* @param args The args to convert, formatted as: {"--energy", "true", "--task_timeout", "10s"}
* @return The command flags from the given arguments, empty if arguments are invalid
*/
public static @Nonnull List<CommandFlag<?>> getFlags(@Nonnull List<String> args) {
List<CommandFlag<?>> flags = new ArrayList<>();
for (int i = 0; i < args.size(); i++) {
String arg = args.get(i);
Expand All @@ -46,17 +69,30 @@ public static List<CommandFlag<?>> getFlags(List<String> args) {
return flags;
}

public static CommandFlag<?> getFlag(String type, String value) {
CommandFlag<?> flag = FLAG_TYPES.get(type);
if (flag != null) {
return flag.ofValue(value);
/**
* Attempts to register the flag type, fails if there is already a flag for the given flag type
* @param type A {@link String} flag type in the format of "--type"
* @param flag The {@link CommandFlag} for the given flag type
* @return If the flag type was successfully registered
*/
public static boolean addFlagType(@Nonnull String type, @Nonnull CommandFlag<?> flag) {
if (FLAG_TYPES.containsKey(type)) {
return false;
}
return null;
FLAG_TYPES.put(type, flag);
return true;
}

/**
* @return An unmodifiable copy of {@link CommandFlags#FLAG_TYPES}
*/
public static @Nonnull Map<String, CommandFlag<?>> getFlagTypes() {
return Map.copyOf(FLAG_TYPES);
}

public static class EnergyFlag extends CommandFlag<Boolean> {
@Override
public void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
public void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
BlockStorage.addBlockInfo(block, "energy-charge", String.valueOf(Integer.MAX_VALUE), false);
}

Expand All @@ -79,7 +115,7 @@ public EnergyFlag ofValue(String value) {
public static class InputsFlag extends CommandFlag<List<ItemStack>> {

@Override
public void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
public void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
BlockMenu menu = BlockStorage.getInventory(block);
int[] slots = menu.getPreset().getSlotsAccessedByItemTransport(ItemTransportFlow.INSERT);
for (ItemStack input : this.value) {
Expand Down Expand Up @@ -180,7 +216,7 @@ public static class RefillInputsFlag extends CommandFlag<Boolean> {
private RefillInputsTask task;

@Override
public void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
public void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
if (task == null) {
task = new RefillInputsTask(sfItem);

Expand Down Expand Up @@ -217,7 +253,7 @@ public static class VoidOutputsFlag extends CommandFlag<Boolean> {
private VoidOutputsTask task;

@Override
public void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
public void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
if (task == null) {
task = new VoidOutputsTask(sfItem);

Expand Down Expand Up @@ -250,7 +286,7 @@ public VoidOutputsFlag ofValue(String value) {
public static class TimeoutFlag extends CommandFlag<Integer> {

@Override
public void apply(List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {
public void apply(Player player, List<CommandFlag<?>> flags, SlimefunItem sfItem, Block block) {

}

Expand Down
Loading