Skip to content

Commit

Permalink
Add two options named CommandBefore and CommandAfter for submitting a…
Browse files Browse the repository at this point in the history
…ctions which ignore repetitions
  • Loading branch information
ustc-zzzz committed Jun 17, 2019
1 parent 8eff08a commit b269d80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions resources/assets/virtualchest/examples/example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ aliases {
}
PrimaryShiftAction {
Command = "cost-item: 9; cost: -8"
CommandAfter = "tell: Wheats are sold successfully. You have %economy_bal_format% now."
CommandBefore = "tell: You have %economy_bal_format% now. Now start selling wheats to the server."
HandheldItem {
ItemType = "minecraft:wheat"
Count = 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ public class VirtualChestActionDispatcher
{
private static final DataQuery KEEP_INVENTORY_OPEN = DataQuery.of("KeepInventoryOpen");
private static final DataQuery HANDHELD_ITEM = DataQuery.of("HandheldItem");

private static final DataQuery COMMAND = DataQuery.of("Command");
private static final DataQuery COMMAND_AFTER = DataQuery.of("CommandAfter");
private static final DataQuery COMMAND_BEFORE = DataQuery.of("CommandBefore");

private static final char SEQUENCE_SPLITTER = ';';

private final ImmutableList<VirtualChestHandheldItem> handheldItem;
private final ImmutableList<Boolean> keepInventoryOpen;

private final ImmutableList<List<String>> commands;
private final ImmutableList<List<String>> commandsAfter;
private final ImmutableList<List<String>> commandsBefore;

private final ImmutableList<DataContainer> views;

Expand All @@ -54,23 +60,32 @@ public VirtualChestActionDispatcher(List<DataView> views)

ImmutableList.Builder<VirtualChestHandheldItem> handheldItemBuilder = ImmutableList.builder();
ImmutableList.Builder<Boolean> keepInventoryOpenBuilder = ImmutableList.builder();

ImmutableList.Builder<List<String>> commandsBuilder = ImmutableList.builder();
ImmutableList.Builder<List<String>> commandsAfterBuilder = ImmutableList.builder();
ImmutableList.Builder<List<String>> commandsBeforeBuilder = ImmutableList.builder();

ImmutableList.Builder<DataContainer> dataContainerBuilder = ImmutableList.builder();

for (DataView view : views)
{
handheldItemBuilder.add(this.parseHandheldItem(view));
commandsBuilder.add(parseCommand(view.getString(COMMAND).orElse("")));
keepInventoryOpenBuilder.add(view.getBoolean(KEEP_INVENTORY_OPEN).orElse(false));

commandsBuilder.add(parseCommand(view.getString(COMMAND).orElse("")));
commandsAfterBuilder.add(parseCommand(view.getString(COMMAND_AFTER).orElse("")));
commandsBeforeBuilder.add(parseCommand(view.getString(COMMAND_BEFORE).orElse("")));

dataContainerBuilder.add(view.copy());
}

this.commands = commandsBuilder.build();
this.handheldItem = handheldItemBuilder.build();
this.keepInventoryOpen = keepInventoryOpenBuilder.build();

this.commands = commandsBuilder.build();
this.commandsAfter = commandsAfterBuilder.build();
this.commandsBefore = commandsBeforeBuilder.build();

this.views = dataContainerBuilder.build();
}

Expand Down Expand Up @@ -105,10 +120,15 @@ public Tuple<Boolean, CompletableFuture<CommandResult>> runCommand(VirtualChestP
if (size >= itemTemplate.getCount()) // it's enough! do action now
{
int rep = itemTemplate.getRepetition(size);
List<String> commands = this.commands.get(i);
VirtualChestActions actions = plugin.getVirtualChestActions();
ClassToInstanceMap<Context> map = getContextMap(actionUUID, player, itemTemplate);

List<String> commands = this.commands.get(i);
List<String> commandsAfter = this.commandsAfter.get(i);
List<String> commandsBefore = this.commandsBefore.get(i);

Stream<String> stream = IntStream.rangeClosed(0, rep).boxed().flatMap(o -> commands.stream());
stream = Stream.concat(Stream.concat(commandsBefore.stream(), stream), commandsAfter.stream());
return Tuple.of(this.keepInventoryOpen.get(i), actions.submitCommands(player, stream, map, record));
}
areSearchingInventory[i] = false; // otherwise do not search inventory for it in step 2
Expand Down Expand Up @@ -149,10 +169,16 @@ public Tuple<Boolean, CompletableFuture<CommandResult>> runCommand(VirtualChestP
if (size >= itemTemplate.getCount()) // it's enough! do action now
{
int rep = itemTemplate.getRepetition(size);
List<String> commands1 = this.commands.get(i);
VirtualChestActions actions = plugin.getVirtualChestActions();
ClassToInstanceMap<Context> map = getContextMap(actionUUID, player, itemTemplate);
Stream<String> stream = IntStream.rangeClosed(0, rep).boxed().flatMap(o -> commands1.stream());

List<String> commands = this.commands.get(i);
List<String> commandsAfter = this.commandsAfter.get(i);
List<String> commandsBefore = this.commandsBefore.get(i);

Stream<String> stream = IntStream.rangeClosed(0, rep).boxed().flatMap(o -> commands.stream());
stream = Stream.concat(Stream.concat(commandsBefore.stream(), stream), commandsAfter.stream());

return Tuple.of(this.keepInventoryOpen.get(i), actions.submitCommands(player, stream, map, record));
}
}
Expand Down

0 comments on commit b269d80

Please sign in to comment.