Skip to content

Commit

Permalink
clean(command): replace deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Aug 24, 2023
1 parent a7c6439 commit 5412858
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) {

var boxPlayer = playerMap.get(player);

var context = 1 < args.length ? SharedStockListCommand.createContextFromArguments(Arrays.copyOfRange(args, 1, args.length)) : new SharedStockListCommand.Context();
sender.sendMessage(SharedStockListCommand.createStockList(boxPlayer.getCurrentStockHolder(), context));
sender.sendMessage(SharedStockListCommand.createStockList(
boxPlayer.getCurrentStockHolder(),
1 < args.length ? Arrays.copyOfRange(args, 1, args.length) : null
));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) {
return;
}

var context = 3 < args.length ? SharedStockListCommand.createContextFromArguments(Arrays.copyOfRange(args, 3, args.length)) : new SharedStockListCommand.Context();
sender.sendMessage(SharedStockListCommand.createStockList(targetStockHolder, context));
sender.sendMessage(SharedStockListCommand.createStockList(
targetStockHolder,
3 < args.length ? Arrays.copyOfRange(args, 3, args.length) : null
));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.okocraft.box.feature.command.message;

import it.unimi.dsi.fastutil.objects.ObjectIntPair;
import net.kyori.adventure.text.Component;
import net.okocraft.box.api.message.argument.DoubleArgument;
import net.okocraft.box.api.message.argument.TripleArgument;
import net.okocraft.box.api.model.stock.StockData;
import net.okocraft.box.api.model.item.BoxItem;

import static net.okocraft.box.api.message.Components.aquaItemName;
import static net.okocraft.box.api.message.Components.aquaText;
Expand All @@ -20,10 +21,10 @@ public final class SharedMessage {
aquaText(target), aquaText(page), aquaText(maxPage)
);

public static final DoubleArgument<Integer, StockData> STOCK_LIST_ITEM_AMOUNT =
(num, stockData) -> grayTranslatable(
public static final DoubleArgument<Integer, ObjectIntPair<BoxItem>> STOCK_LIST_ITEM_AMOUNT =
(num, itemAmountPair) -> grayTranslatable(
"box.command.shared.stock-list.amount",
grayText(num), aquaItemName(stockData.item()), aquaText(stockData.amount())
grayText(num), aquaItemName(itemAmountPair.left()), aquaText(itemAmountPair.rightInt())
);

public static final Component NO_STOCK_FOUND = redTranslatable("box.command.shared.stock-list.no-stock-found");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.okocraft.box.feature.command.shared;

import it.unimi.dsi.fastutil.ints.IntComparator;
import it.unimi.dsi.fastutil.ints.IntComparators;
import it.unimi.dsi.fastutil.objects.ObjectIntPair;
import net.kyori.adventure.text.Component;
import net.okocraft.box.api.model.stock.StockData;
import net.okocraft.box.api.model.item.BoxItem;
import net.okocraft.box.api.model.stock.StockHolder;
import net.okocraft.box.api.util.TabCompleter;
import net.okocraft.box.feature.command.message.SharedMessage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -37,48 +41,43 @@ public class SharedStockListCommand {
ARGUMENT_TYPES = List.copyOf(ARGUMENT_MAP.keySet());
}

public static @NotNull Context createContextFromArguments(@NotNull String @NotNull [] args) {
var context = new Context();

ArgumentType type = null;

for (var arg : args) {
if (type != null) {
type.getContextConsumer().accept(arg, context);
type = null;
} else {
type = ARGUMENT_MAP.get(arg.toLowerCase(Locale.ENGLISH));
}
}
public static @NotNull Component createStockList(@NotNull StockHolder stockHolder, @NotNull String @Nullable [] args) {
var context = args != null ? createContextFromArguments(args) : new Context();

return context;
}
var sorter = context.sorter;
var filter = context.filter != null ? createFilter(context.filter) : null;

public static @NotNull Component createStockList(@NotNull StockHolder stockHolder, @NotNull Context context) {
var sorter = context.getSorter();
var filter = context.getFilter();
Stream<BoxItem> stream = stockHolder.getStockedItems().stream();
Collection<ObjectIntPair<BoxItem>> stockDataCollection;

var stockDataStream = stockHolder.stockDataStream();

if (sorter != null || (filter != null && !filter.isEmpty())) {

if (sorter != null) {
stockDataStream = stockDataStream.sorted(sorter);
}
if (filter != null) {
stream = stream.filter(filter);
}

if (filter != null && !filter.isEmpty()) {
stockDataStream = stockDataStream.filter(createFilter(filter));
if (sorter != null) {
if (sorter instanceof Sorter.ByName byName) {
stockDataCollection =
stream.sorted(byName)
.map(item -> ObjectIntPair.of(item, stockHolder.getAmount(item)))
.toList();
} else if (sorter instanceof Sorter.ByAmount byAmount) {
stockDataCollection =
stream.map(item -> ObjectIntPair.of(item, stockHolder.getAmount(item)))
.sorted((p1, p2) -> byAmount.compare(p1.rightInt(), p2.rightInt()))
.toList();
} else {
throw new UnsupportedOperationException("Unknown sorter: " + sorter);
}
} else {
stockDataCollection = stream.map(item -> ObjectIntPair.of(item, stockHolder.getAmount(item))).toList();
}

var stockDataCollection = stockDataStream.toList();

if (stockDataCollection.isEmpty()) {
return SharedMessage.NO_STOCK_FOUND;
}

int maxPage = stockDataCollection.size() / 8 + 1;
int page = Math.max(Math.min(context.getPage(), maxPage), 1);
int page = Math.max(Math.min(context.page, maxPage), 1);

int start = (page - 1) * 8;
var counter = new AtomicInteger(start);
Expand Down Expand Up @@ -108,101 +107,79 @@ public class SharedStockListCommand {
}
}

private static @NotNull Predicate<StockData> createFilter(@NotNull String arg) {
private static @NotNull Context createContextFromArguments(@NotNull String @NotNull [] args) {
var context = new Context();

ArgumentType type = null;

for (var arg : args) {
if (type != null) {
type.getContextConsumer().accept(arg, context);
type = null;
} else {
type = ARGUMENT_MAP.get(arg.toLowerCase(Locale.ENGLISH));
}
}

return context;
}

private static @NotNull Predicate<BoxItem> createFilter(@NotNull String arg) {
boolean startsWith = arg.endsWith("*");
boolean endsWith = arg.startsWith("*");

if (startsWith && endsWith) {
var filter = arg.substring(1, arg.length() - 1).toUpperCase(Locale.ROOT);
return stockData -> contains(stockData, filter);
return item -> contains(item, filter);
}

if (startsWith) {
var filter = arg.substring(0, arg.length() - 1).toUpperCase(Locale.ROOT);
return stockData -> startsWith(stockData, filter);
return item -> startsWith(item, filter);
}

if (endsWith) {
var filter = arg.substring(1).toUpperCase(Locale.ROOT);
return stockData -> endsWith(stockData, filter);
return item -> endsWith(item, filter);
}

var filter = arg.toUpperCase(Locale.ROOT);
return stockData -> contains(stockData, filter);
return item -> contains(item, filter);
}

private static boolean startsWith(@NotNull StockData stockData, @NotNull String filter) {
return stockData.item().getPlainName().startsWith(filter);
private static boolean startsWith(@NotNull BoxItem item, @NotNull String filter) {
return item.getPlainName().startsWith(filter);
}

private static boolean endsWith(@NotNull StockData stockData, @NotNull String filter) {
return stockData.item().getPlainName().endsWith(filter);
private static boolean endsWith(@NotNull BoxItem item, @NotNull String filter) {
return item.getPlainName().endsWith(filter);
}

private static boolean contains(@NotNull StockData stockData, @NotNull String filter) {
return stockData.item().getPlainName().contains(filter);
private static boolean contains(@NotNull BoxItem item, @NotNull String filter) {
return item.getPlainName().contains(filter);
}

public static final class Context {

private static final Comparator<StockData> NAME_ASC = Comparator.comparing(stock -> stock.item().getPlainName());
private static final Comparator<StockData> NAME_DESC = NAME_ASC.reversed();
private static final Comparator<StockData> AMOUNT_ASC = Comparator.comparing(StockData::amount);
private static final Comparator<StockData> AMOUNT_DESC = AMOUNT_ASC.reversed();

private Comparator<StockData> sorter;
private static final class Context {
private Sorter sorter;
private int page;
private String filter;

public Context() {
}

public @Nullable Comparator<StockData> getSorter() {
return sorter;
}

public int getPage() {
return page;
}

private @Nullable String getFilter() {
return filter;
}

private void setSorter(Comparator<StockData> sorter) {
this.sorter = sorter;
}

private void setPage(int page) {
this.page = page;
}

private void setFilter(String filter) {
this.filter = filter;
}
}

private enum ArgumentType {
SORTER(
"sorter", "s",
(arg, context) -> context.setSorter(switch (arg.toLowerCase(Locale.ROOT)) {
case "na", "name-asc" -> Context.NAME_ASC;
case "nd", "name-desc" -> Context.NAME_DESC;
case "aa", "amount-asc" -> Context.AMOUNT_ASC;
case "ad", "amount-desc" -> Context.AMOUNT_DESC;
default -> null;
}),
(arg, context) -> context.sorter = Sorter.get(arg.toLowerCase(Locale.ENGLISH)),
arg -> Stream.of("na", "name-asc", "nd", "name-desc", "aa", "amount-asc", "ad", "amount-desc")
.filter(sorter -> sorter.startsWith(arg.toLowerCase(Locale.ENGLISH)))
.toList()
),
PAGE("page", "p", (arg, context) -> {
try {
context.setPage(Integer.parseInt(arg));
context.page = Integer.parseInt(arg);
} catch (NumberFormatException ignored) {
}
}, arg -> Collections.emptyList()),
FILTER("filter", "f", (arg, context) -> context.setFilter(arg), TabCompleter::itemNames);
FILTER("filter", "f", (arg, context) -> context.filter = arg, TabCompleter::itemNames);

private final String longArg;
private final String shortArg;
Expand Down Expand Up @@ -233,4 +210,52 @@ private enum ArgumentType {
return tabCompleter;
}
}

public sealed interface Sorter permits Sorter.ByName, Sorter.ByAmount {

ByName NAME_ASC = new ByName(false);
ByName NAME_DESC = new ByName(true);
ByAmount AMOUNT_ASC = new ByAmount(false);
ByAmount AMOUNT_DESC = new ByAmount(true);

static @Nullable Sorter get(@NotNull String name) {
return switch (name) {
case "na", "name-asc" -> NAME_ASC;
case "nd", "name-desc" -> NAME_DESC;
case "aa", "amount-asc" -> AMOUNT_ASC;
case "ad", "amount-desc" -> AMOUNT_DESC;
default -> null;
};
}

final class ByName implements Comparator<BoxItem>, Sorter {

private final Comparator<String> comparator;

private ByName(boolean reserved) {
Comparator<String> comparator = String::compareTo;
this.comparator = reserved ? comparator.reversed() : comparator;
}

@Override
public int compare(BoxItem item, BoxItem other) {
return comparator.compare(item.getPlainName(), other.getPlainName());
}

}

final class ByAmount implements IntComparator, Sorter {

private final IntComparator comparator;

private ByAmount(boolean reserved) {
this.comparator = reserved ? IntComparators.OPPOSITE_COMPARATOR : IntComparators.NATURAL_COMPARATOR;
}

@Override
public int compare(int k1, int k2) {
return comparator.compare(k1, k2);
}
}
}
}

0 comments on commit 5412858

Please sign in to comment.