forked from unimatrix/web
Offer native Exchange listing, price controls and withdrawal
This commit is contained in:
parent
f3686fb5e4
commit
a09b6e107b
2 changed files with 52 additions and 18 deletions
|
|
@ -47,7 +47,7 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
private static final int ARCHIVE_COLUMN_LINES = 7;
|
||||
private static final int ARCHIVE_COLUMN_WIDTH = 177;
|
||||
private static final int PREVIOUS = -1, NEXT = -2, EMPTY = -3;
|
||||
private static final int CHECKOUT_BACK = 45, CHECKOUT_BUY = 49, CHECKOUT_CONTROLS = 2;
|
||||
private static final int CHECKOUT_BACK = 45, CHECKOUT_PRIMARY = 49, CHECKOUT_CONTROLS = 2;
|
||||
private final Map<UUID, Scene> scenes = new HashMap<>();
|
||||
private final JavaPlugin plugin;
|
||||
// This range is private to each connection and reused on reopen to bound the client map cache.
|
||||
|
|
@ -246,18 +246,13 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
|
||||
private static Page checkoutPage(Document document, int index) {
|
||||
var controls = document.cards().stream()
|
||||
.filter(card -> card.slot() != CHECKOUT_BACK && card.slot() != CHECKOUT_BUY)
|
||||
.sorted(Comparator.comparingInt(card -> switch (card.slot()) {
|
||||
case 30 -> 0;
|
||||
case 32 -> 1;
|
||||
case 31 -> 2;
|
||||
default -> 3;
|
||||
})).toList();
|
||||
.filter(card -> card.slot() != CHECKOUT_BACK && card.slot() != CHECKOUT_PRIMARY)
|
||||
.sorted(Comparator.comparingInt(card -> exchangeControlOrder(document.title(), card.slot()))).toList();
|
||||
int start = index * CHECKOUT_CONTROLS;
|
||||
int end = Math.min(start + CHECKOUT_CONTROLS, controls.size());
|
||||
var visible = new ArrayList<>(controls.subList(start, end));
|
||||
while (visible.size() < CHECKOUT_CONTROLS) visible.add(new Card(EMPTY, "", List.of(), false));
|
||||
for (int slot : new int[]{CHECKOUT_BACK,CHECKOUT_BUY}) {
|
||||
for (int slot : new int[]{CHECKOUT_BACK,CHECKOUT_PRIMARY}) {
|
||||
visible.add(document.cards().stream().filter(card -> card.slot() == slot).findFirst().orElseThrow());
|
||||
}
|
||||
if (controls.size() > CHECKOUT_CONTROLS) {
|
||||
|
|
@ -268,6 +263,31 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
return new Page(index, document.style(), List.copyOf(visible));
|
||||
}
|
||||
|
||||
private static int exchangeControlOrder(String title, int slot) {
|
||||
return switch (title) {
|
||||
case "Exchange · Add item" -> switch (slot) {
|
||||
case 13 -> 0;
|
||||
case 40 -> 1;
|
||||
case 30 -> 2;
|
||||
case 32 -> 3;
|
||||
default -> 4;
|
||||
};
|
||||
case "Exchange · Price" -> switch (slot) {
|
||||
case 13 -> 0;
|
||||
case 53 -> 1;
|
||||
case 27 -> 2;
|
||||
case 32 -> 3;
|
||||
default -> 4;
|
||||
};
|
||||
default -> switch (slot) {
|
||||
case 30 -> 0;
|
||||
case 32 -> 1;
|
||||
case 31 -> 2;
|
||||
default -> 3;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
private void paint(Scene scene, boolean initial) {
|
||||
BufferedImage art = artwork(scene);
|
||||
scene.buttons.clear();
|
||||
|
|
@ -462,7 +482,7 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
draw.setColor(card.actionable() ? palette.tile : new Color(55, 63, 66));
|
||||
boolean bottom = row == LAST_ROW;
|
||||
draw.fillRect(x, y + 3, 128, bottom ? LAST_CARD_HEIGHT : 122);
|
||||
if (card.slot() >= 0 && drawStyledCard(draw, scene.document.style(), card, palette, x, y)) continue;
|
||||
if (card.slot() >= 0 && drawStyledCard(draw, scene.document, card, palette, x, y)) continue;
|
||||
draw.setColor(Color.WHITE);
|
||||
draw.setFont(CARD_TITLE);
|
||||
writeWrapped(draw, card.title(), x + 7, y + 24, 114, bottom ? 2 : 3);
|
||||
|
|
@ -505,7 +525,7 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
if (!wrap(graphics, styledTitle(document), 500, 1).complete) return false;
|
||||
if (document.style() == Style.EXCHANGE_CHECKOUT) {
|
||||
if (document.description().isBlank() || !hasCheckoutAction(document, CHECKOUT_BACK) ||
|
||||
!hasCheckoutAction(document, CHECKOUT_BUY)) return false;
|
||||
!hasCheckoutAction(document, CHECKOUT_PRIMARY)) return false;
|
||||
graphics.setFont(CHECKOUT_TERMS);
|
||||
var terms = checkoutTerms(graphics, document.description());
|
||||
if (terms.isEmpty() || terms.size() > 9) return false;
|
||||
|
|
@ -534,7 +554,7 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
!fitsFullCardTitle(graphics, document.style(), card)) return false;
|
||||
continue;
|
||||
}
|
||||
if (usesFullCardLore(document.style())) {
|
||||
if (usesFullCardLore(document.style()) || saleControls(document, card)) {
|
||||
if (!fitsFullCardTitle(graphics, document.style(), card)) return false;
|
||||
graphics.setFont(compactCardLore(document.style()) ? COMPACT_CARD_LORE : FULL_CARD_LORE);
|
||||
if (!fullCardLore(graphics, document.style(), card).complete) return false;
|
||||
|
|
@ -577,7 +597,7 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
}
|
||||
|
||||
private static boolean compactCardTitle(Style style) {
|
||||
return style == Style.CODEX || style == Style.GUILD || compactCardLore(style);
|
||||
return style == Style.CODEX || style == Style.GUILD || style == Style.EXCHANGE_CHECKOUT || compactCardLore(style);
|
||||
}
|
||||
|
||||
private static boolean compactCardLore(Style style) {
|
||||
|
|
@ -621,7 +641,18 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
}
|
||||
|
||||
private static String styledTitle(Document document) {
|
||||
return document.style() == Style.EXCHANGE_CHECKOUT ? "Exchange checkout" : document.title();
|
||||
return switch (document.title()) {
|
||||
case "Exchange · Purchase details" -> "Exchange checkout";
|
||||
case "Exchange · Add item" -> "Create listing";
|
||||
case "Exchange · Price" -> "Listing price";
|
||||
case "Exchange · Withdraw listing" -> "Withdraw listing";
|
||||
default -> document.title();
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean saleControls(Document document, Card card) {
|
||||
return document.style() == Style.EXCHANGE_CHECKOUT && card.actionable() &&
|
||||
!document.title().equals("Exchange · Purchase details");
|
||||
}
|
||||
|
||||
private static boolean hasCheckoutAction(Document document, int slot) {
|
||||
|
|
@ -728,11 +759,12 @@ final class PaperDecisionsScene implements DecisionsViewTransport, Listener, Aut
|
|||
}
|
||||
}
|
||||
|
||||
private boolean drawStyledCard(java.awt.Graphics2D draw, Style style, Card card, Palette palette, int x, int y) {
|
||||
private boolean drawStyledCard(java.awt.Graphics2D draw, Document document, Card card, Palette palette, int x, int y) {
|
||||
Style style = document.style();
|
||||
if (style == Style.AUTO) return false;
|
||||
String art = nativeArt(card.material());
|
||||
draw.setColor(palette.accent);
|
||||
if (usesFullCardLore(style)) {
|
||||
if (usesFullCardLore(style) || saleControls(document, card)) {
|
||||
if (style == Style.ORGANISATION) draw.fillRect(x, y + 3, 4, 122);
|
||||
draw.drawLine(x + 7, y + 48, x + 121, y + 48);
|
||||
draw.setColor(Color.WHITE);
|
||||
|
|
|
|||
|
|
@ -121,7 +121,8 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
if(BedrockForms.isBedrock(p)){s.gate.expire();BedrockForms.show(plugin,p,s.title,s.description,s.choices);return;}
|
||||
var style=switch(s.title){
|
||||
case "Exchange · Buy","Exchange · Your listings","Exchange · Kits" -> DecisionsViewTransport.Style.EXCHANGE;
|
||||
case "Exchange · Purchase details" -> DecisionsViewTransport.Style.EXCHANGE_CHECKOUT;
|
||||
case "Exchange · Purchase details","Exchange · Add item","Exchange · Price","Exchange · Withdraw listing" ->
|
||||
DecisionsViewTransport.Style.EXCHANGE_CHECKOUT;
|
||||
default -> null;
|
||||
};
|
||||
if(style!=null&&NativeMenus.show(plugin,p,"development.fullscreen-exchange-viewers",s.title,s.description,s.inventory,s.actions,s.gate,style,null))return;
|
||||
|
|
@ -229,7 +230,8 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
if(reopen)kitPurchase(player,purchase);
|
||||
});
|
||||
}
|
||||
private void withdraw(Player p,JsonObject row){var s=panel(p,"Exchange · Withdraw listing");try{s.inventory.setItem(13,bridge.exchangeIcon(row));}catch(Exception bad){p.sendMessage("This stored item cannot be decoded.");return;}button(s,45,Material.ARROW,"Back","Your listings",()->list(p,true,0));button(s,49,Material.CHEST,"Withdraw "+row.get("quantity").getAsInt()+" items","Return all unsold stock to your inventory",()->withdrawListing(p,Long.parseLong(row.get("id").getAsString()),true));show(p,s);}
|
||||
private void withdraw(Player p,JsonObject row){var s=panel(p,"Exchange · Withdraw listing");s.description=row.get("title").getAsString()+"\n"+
|
||||
row.get("quantity").getAsInt()+" unsold\n"+money(row.get("priceMinor").getAsLong())+" each";try{s.inventory.setItem(13,bridge.exchangeIcon(row));}catch(Exception bad){p.sendMessage("This stored item cannot be decoded.");return;}button(s,45,Material.ARROW,"Back","Your listings",()->list(p,true,0));button(s,49,Material.CHEST,"Withdraw "+row.get("quantity").getAsInt()+" items","Return all unsold stock to your inventory",()->withdrawListing(p,Long.parseLong(row.get("id").getAsString()),true));show(p,s);}
|
||||
private void withdrawListing(Player player,long listing,boolean reopen){
|
||||
UUID id=UUID.randomUUID();var query=action("prepare_cancel");query.addProperty("requestId",id.toString());
|
||||
request(player,query,permit->{
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff