forked from unimatrix/web
799 lines
39 KiB
Java
799 lines
39 KiB
Java
package com.dunerealms.fullscreen;
|
|
|
|
import com.dunerealms.gui.DecisionsViewTransport;
|
|
import java.util.function.IntConsumer;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Font;
|
|
import java.awt.BasicStroke;
|
|
import java.awt.image.BufferedImage;
|
|
import java.util.*;
|
|
import io.netty.buffer.Unpooled;
|
|
import io.netty.channel.*;
|
|
import net.minecraft.core.*;
|
|
import net.minecraft.core.component.DataComponents;
|
|
import net.minecraft.network.FriendlyByteBuf;
|
|
import net.minecraft.network.protocol.game.*;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.decoration.*;
|
|
import net.minecraft.world.item.Items;
|
|
import net.minecraft.world.level.saveddata.maps.*;
|
|
import net.minecraft.world.scores.DisplaySlot;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
|
import org.bukkit.craftbukkit.scoreboard.CraftScoreboard;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.*;
|
|
import org.bukkit.event.player.*;
|
|
import org.bukkit.map.MapPalette;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
final class PaperDecisionsScene implements DecisionsViewTransport, Listener, AutoCloseable {
|
|
private static final String INPUT = "dune_fullscreen_decisions";
|
|
private static final int COLS = 11, ROWS = 9;
|
|
private static final int STANDARD_PAGE = 4, THREE_COLUMN_PAGE = 6;
|
|
private static final int LAST_ROW = 5, LAST_CARD_HEIGHT = 64;
|
|
private static final Font CARD_TITLE = new Font(Font.SANS_SERIF, Font.BOLD, 17);
|
|
private static final Font CARD_LORE = new Font(Font.SANS_SERIF, Font.PLAIN, 13);
|
|
private static final int JOURNAL_LORE_LINES = 5;
|
|
private static final Font JOURNAL_LORE = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
|
|
private static final Font CHECKOUT_TERMS = new Font(Font.SANS_SERIF, Font.BOLD, 15);
|
|
private static final int PREVIOUS = -1, NEXT = -2, EMPTY = -3;
|
|
private static final int CHECKOUT_BACK = 45, CHECKOUT_BUY = 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.
|
|
private static final int FIRST_SCENE_MAP = 2_000_000_000;
|
|
|
|
PaperDecisionsScene(JavaPlugin plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
private record Tile(MapId map, BufferedImage normal, BufferedImage focused) {}
|
|
private record Page(int index, Style style, List<Card> cards) {}
|
|
private record Roleplay(String heading, String contact, Card portrait, Card world, Card summary) {}
|
|
|
|
private static final class Scene {
|
|
final Player viewer;
|
|
final Location original;
|
|
final boolean gravity;
|
|
final Document document;
|
|
final Roleplay roleplay;
|
|
final IntConsumer select;
|
|
final Runnable dismiss;
|
|
final List<Entity> entities = new ArrayList<>();
|
|
final Map<Integer, Tile> buttons = new HashMap<>();
|
|
Integer focus;
|
|
volatile Page page;
|
|
int mountId;
|
|
boolean presentationMasked;
|
|
volatile boolean closed;
|
|
boolean positioning;
|
|
|
|
Scene(Player viewer, Document document, Roleplay roleplay, IntConsumer select, Runnable dismiss) {
|
|
this.viewer = viewer;
|
|
this.document = document;
|
|
this.roleplay = roleplay;
|
|
this.select = select;
|
|
this.dismiss = dismiss;
|
|
original = viewer.getLocation();
|
|
gravity = viewer.hasGravity();
|
|
page = page(this.document, 0);
|
|
}
|
|
}
|
|
|
|
@Override public void close() {
|
|
List.copyOf(scenes.values()).forEach(scene -> finish(scene, true, true));
|
|
}
|
|
|
|
@Override public boolean open(Player viewer, Document document, IntConsumer select, Runnable dismiss) {
|
|
if (!(viewer instanceof CraftPlayer player) || !viewer.isOnline() || viewer.isDead() ||
|
|
viewer.isInsideVehicle() || scenes.containsKey(viewer.getUniqueId()) ||
|
|
!canPresent(document)) return false;
|
|
var pipeline = player.getHandle().connection.connection.channel.pipeline();
|
|
if (pipeline.get("packet_handler") == null || pipeline.get(INPUT) != null) return false;
|
|
|
|
Roleplay roleplay = roleplay(document);
|
|
Document visibleDocument = presentable(document, roleplay);
|
|
if (roleplay != null && !fitsRoleplay(visibleDocument, roleplay)) return false;
|
|
if (visibleDocument.style() != Style.AUTO && !fitsStyled(visibleDocument)) return false;
|
|
Scene scene = new Scene(viewer, visibleDocument, roleplay, select, dismiss);
|
|
var origin = scene.original;
|
|
var viewpoint = new Location(origin.getWorld(), origin.getBlockX() + .5, origin.getBlockY() + 6,
|
|
origin.getBlockZ() + .5, 0, 0);
|
|
if (!viewpoint.getBlock().isPassable() || !viewpoint.clone().add(0, 1, 0).getBlock().isPassable()) return false;
|
|
scenes.put(viewer.getUniqueId(), scene);
|
|
try {
|
|
viewer.setGravity(false);
|
|
scene.positioning = true;
|
|
if (!viewer.teleport(viewpoint) || !viewer.getLocation().equals(viewpoint)) {
|
|
finish(scene, false, false);
|
|
return false;
|
|
}
|
|
scene.positioning = false;
|
|
show(scene);
|
|
Bukkit.getScheduler().runTaskLater(plugin, () -> finish(scene, true, true), 2400);
|
|
return true;
|
|
} catch (RuntimeException | LinkageError error) {
|
|
plugin.getLogger().warning("Fullscreen Decisions could not open: " + error.getClass().getSimpleName());
|
|
finish(scene, true, false);
|
|
return false;
|
|
}
|
|
}
|
|
private void show(Scene scene) {
|
|
var handle = ((CraftPlayer) scene.viewer).getHandle();
|
|
var level = handle.level();
|
|
var origin = scene.original;
|
|
paint(scene, true);
|
|
for (int row = 0; row < ROWS; row++) {
|
|
for (int col = 0; col < COLS; col++) {
|
|
var pos = new BlockPos(origin.getBlockX() + 5 - col, origin.getBlockY() + 11 - row, origin.getBlockZ() + 2);
|
|
var frame = new GlowItemFrame(level, pos, Direction.NORTH);
|
|
frame.setInvisible(true);
|
|
var map = new MapId(FIRST_SCENE_MAP + row * COLS + col);
|
|
var item = new net.minecraft.world.item.ItemStack(Items.FILLED_MAP);
|
|
item.set(DataComponents.MAP_ID, map);
|
|
frame.setItem(item, false, false);
|
|
add(scene, frame, Direction.NORTH.get3DDataValue());
|
|
}
|
|
}
|
|
|
|
var mount = new ArmorStand(level, handle.getX(), handle.getY() - 1.4, handle.getZ());
|
|
var camera = new ArmorStand(level, handle.getX(), handle.getY(), handle.getZ());
|
|
for (var stand : List.of(mount, camera)) {
|
|
stand.setInvisible(true);
|
|
stand.setNoGravity(true);
|
|
add(scene, stand, 0);
|
|
}
|
|
scene.mountId = mount.getId();
|
|
handle.connection.send(passengers(scene.mountId, handle.getId()));
|
|
handle.connection.send(new ClientboundSetCameraPacket(camera));
|
|
scene.presentationMasked = true;
|
|
maskHands(handle, true);
|
|
sidebar(scene.viewer, true);
|
|
handle.connection.send(new ClientboundSetActionBarTextPacket(net.minecraft.network.chat.Component.empty()));
|
|
var channel = handle.connection.connection.channel;
|
|
var ids = scene.entities.stream().map(Entity::getId).collect(java.util.stream.Collectors.toUnmodifiableSet());
|
|
channel.pipeline().addBefore("packet_handler", INPUT, new ChannelInboundHandlerAdapter() {
|
|
private Integer focus;
|
|
private Page previous;
|
|
@Override public void channelRead(ChannelHandlerContext context, Object input) throws Exception {
|
|
if (!scene.closed) {
|
|
Page page = scene.page;
|
|
if (page != previous) {
|
|
previous = page;
|
|
focus = null;
|
|
}
|
|
if (input instanceof ServerboundMovePlayerPacket move && move.hasRotation()) {
|
|
Integer target = target(page, move.getYRot(0), move.getXRot(0));
|
|
if (!Objects.equals(target, focus)) {
|
|
focus = target;
|
|
Bukkit.getScheduler().runTask(plugin, () -> { if (scene.page == page) focus(scene, target); });
|
|
}
|
|
}
|
|
if (input instanceof ServerboundInteractPacket use && ids.contains(use.entityId())) {
|
|
Integer target = focus;
|
|
if (use.hand() == net.minecraft.world.InteractionHand.MAIN_HAND)
|
|
Bukkit.getScheduler().runTask(plugin, () -> { if (scene.page == page) select(scene, target); });
|
|
return;
|
|
}
|
|
}
|
|
super.channelRead(context, input);
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void sidebar(Player viewer, boolean masked) {
|
|
var scoreboard = ((CraftScoreboard) viewer.getScoreboard()).getHandle();
|
|
var connection = ((CraftPlayer) viewer).getHandle().connection;
|
|
for (DisplaySlot slot : DisplaySlot.values()) {
|
|
if (slot != DisplaySlot.SIDEBAR && !slot.name().startsWith("TEAM_")) continue;
|
|
var objective = scoreboard.getDisplayObjective(slot);
|
|
if (objective != null) connection.send(new ClientboundSetDisplayObjectivePacket(slot, masked ? null : objective));
|
|
}
|
|
}
|
|
|
|
private static boolean canPresent(Document document) {
|
|
var cards = document.cards();
|
|
return !cards.isEmpty() && cards.size() <= 54 &&
|
|
cards.stream().allMatch(card -> card.slot() >= 0) &&
|
|
cards.stream().map(Card::slot).distinct().count() == cards.size();
|
|
}
|
|
|
|
private static Page page(Document document, int index) {
|
|
if (document.style() == Style.EXCHANGE_CHECKOUT) return checkoutPage(document, index);
|
|
var cards = document.cards();
|
|
int capacity = capacity(document.style());
|
|
if (cards.size() <= capacity) return new Page(0, document.style(), cards);
|
|
int start = index * capacity;
|
|
int end = Math.min(start + capacity, cards.size());
|
|
var visible = new ArrayList<>(cards.subList(start, end));
|
|
while (visible.size() < capacity) visible.add(new Card(EMPTY, "", List.of(), false));
|
|
String pageNumber = "Page " + (index + 1) + " / " + ((cards.size() + capacity - 1) / capacity);
|
|
visible.add(new Card(PREVIOUS, "Previous", List.of(pageNumber), index > 0));
|
|
visible.add(new Card(NEXT, "Next", List.of(pageNumber), end < cards.size()));
|
|
return new Page(index, document.style(), List.copyOf(visible));
|
|
}
|
|
|
|
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();
|
|
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}) {
|
|
visible.add(document.cards().stream().filter(card -> card.slot() == slot).findFirst().orElseThrow());
|
|
}
|
|
if (controls.size() > CHECKOUT_CONTROLS) {
|
|
String number = "Page " + (index + 1) + " / " + ((controls.size() + CHECKOUT_CONTROLS - 1) / CHECKOUT_CONTROLS);
|
|
visible.add(new Card(PREVIOUS, "Previous", List.of(number), index > 0));
|
|
visible.add(new Card(NEXT, "Next", List.of(number), end < controls.size()));
|
|
}
|
|
return new Page(index, document.style(), List.copyOf(visible));
|
|
}
|
|
|
|
private void paint(Scene scene, boolean initial) {
|
|
BufferedImage art = artwork(scene);
|
|
scene.buttons.clear();
|
|
scene.focus = null;
|
|
for (int row = 0; row < ROWS; row++) {
|
|
for (int col = 0; col < COLS; col++) {
|
|
if (!initial && (row < 3 || row > 5 || col < 4 || col > 6 || (col == 5 && capacity(scene.page.style) == STANDARD_PAGE))) continue;
|
|
var map = new MapId(FIRST_SCENE_MAP + row * COLS + col);
|
|
var image = art.getSubimage(col * 128, row * 128, 128, 128);
|
|
var card = cardAt(scene.page, row, col);
|
|
var tile = new Tile(map, image, card == null || !card.actionable() ? image : focused(image, row == LAST_ROW ? LAST_CARD_HEIGHT : 122));
|
|
if (card != null && card.actionable()) scene.buttons.put(card.slot(), tile);
|
|
pixels(scene, tile, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void maskHands(net.minecraft.server.level.ServerPlayer player, boolean masked) {
|
|
var data = player.getEntityData().packAll();
|
|
if (data != null) {
|
|
for (var value : data) {
|
|
if (value.id() != 0 || !(value.value() instanceof Byte flags)) continue;
|
|
var display = new net.minecraft.network.syncher.SynchedEntityData.DataValue<>(
|
|
value.id(), net.minecraft.network.syncher.EntityDataSerializers.BYTE,
|
|
masked ? (byte) (flags | 0x20) : flags);
|
|
player.connection.send(new ClientboundSetEntityDataPacket(player.getId(), List.of(display)));
|
|
break;
|
|
}
|
|
}
|
|
int offhand = net.minecraft.world.entity.player.Inventory.SLOT_OFFHAND;
|
|
for (int slot = 0; slot < 9; slot++) sendHandSlot(player, slot, masked);
|
|
sendHandSlot(player, offhand, masked);
|
|
}
|
|
|
|
private static void sendHandSlot(net.minecraft.server.level.ServerPlayer player, int slot, boolean masked) {
|
|
var item = masked ? net.minecraft.world.item.ItemStack.EMPTY : player.getInventory().getItem(slot).copy();
|
|
player.connection.send(new ClientboundSetPlayerInventoryPacket(slot, item));
|
|
}
|
|
|
|
private void add(Scene scene, Entity entity, int extra) {
|
|
var connection = ((CraftPlayer) scene.viewer).getHandle().connection;
|
|
scene.entities.add(entity);
|
|
if (entity instanceof ItemFrame) {
|
|
connection.send(new ClientboundAddEntityPacket(entity, extra, entity.blockPosition()));
|
|
} else {
|
|
connection.send(new ClientboundAddEntityPacket(entity.getId(), entity.getUUID(), entity.getX(), entity.getY(),
|
|
entity.getZ(), entity.getXRot(), entity.getYRot(), entity.getType(), extra,
|
|
net.minecraft.world.phys.Vec3.ZERO, entity.getYHeadRot()));
|
|
}
|
|
var data = entity.getEntityData().getNonDefaultValues();
|
|
if (data != null) connection.send(new ClientboundSetEntityDataPacket(entity.getId(), data));
|
|
}
|
|
|
|
private ClientboundSetPassengersPacket passengers(int mount, int... riders) {
|
|
var buffer = new FriendlyByteBuf(Unpooled.buffer());
|
|
try {
|
|
buffer.writeVarInt(mount);
|
|
buffer.writeVarIntArray(riders);
|
|
return ClientboundSetPassengersPacket.STREAM_CODEC.decode(buffer);
|
|
} finally {
|
|
buffer.release();
|
|
}
|
|
}
|
|
|
|
private void pixels(Scene scene, Tile tile, boolean focused) {
|
|
byte[] image = MapPalette.imageToBytes(focused ? tile.focused : tile.normal);
|
|
var patch = new MapItemSavedData.MapPatch(0, 0, 128, 128, image);
|
|
((CraftPlayer) scene.viewer).getHandle().connection.send(
|
|
new ClientboundMapItemDataPacket(tile.map, (byte) 0, true, List.of(), patch));
|
|
}
|
|
|
|
private static int capacity(Style style) {
|
|
return style == Style.EXCHANGE || style == Style.HOUSE ? THREE_COLUMN_PAGE : STANDARD_PAGE;
|
|
}
|
|
|
|
private static Card cardAt(Page page, int row, int col) {
|
|
if (row < 3 || row > LAST_ROW || col < 4 || col > 6) return null;
|
|
if (col == 5 && (capacity(page.style) == STANDARD_PAGE || row == LAST_ROW)) return null;
|
|
int cap = capacity(page.style);
|
|
int index = row == LAST_ROW ? cap + (col == 4 ? 0 : 1) :
|
|
(row - 3) * (cap / 2) + (cap == THREE_COLUMN_PAGE ? col - 4 : (col - 4) / 2);
|
|
return index < page.cards.size() ? page.cards.get(index) : null;
|
|
}
|
|
|
|
private static Integer target(Page page, float yaw, float pitch) {
|
|
if (!Float.isFinite(yaw) || !Float.isFinite(pitch) || Math.abs(yaw) > 45 || Math.abs(pitch) > 40) return null;
|
|
double x = -Math.tan(Math.toRadians(yaw)) * 2.46;
|
|
double y = Math.tan(Math.toRadians(pitch)) * 2.46;
|
|
if (Math.abs(x) > 1.5 || y > 1.02) return null;
|
|
Card card = cardAt(page, 4 + (int) Math.round(y), 5 - (int) Math.round(x));
|
|
return card != null && card.actionable() ? card.slot() : null;
|
|
}
|
|
|
|
private void focus(Scene scene, Integer slot) {
|
|
if (scene.closed || Objects.equals(scene.focus, slot)) return;
|
|
scene.focus = slot;
|
|
scene.buttons.forEach((action, tile) -> pixels(scene, tile, action.equals(slot)));
|
|
}
|
|
|
|
private void select(Scene scene, Integer slot) {
|
|
if (scene.closed || slot == null || !scene.buttons.containsKey(slot)) return;
|
|
if (slot == PREVIOUS || slot == NEXT) {
|
|
scene.page = page(scene.document, scene.page.index + (slot == NEXT ? 1 : -1));
|
|
paint(scene, false);
|
|
return;
|
|
}
|
|
finish(scene, true, false);
|
|
scene.select.accept(slot);
|
|
}
|
|
|
|
@EventHandler public void swap(PlayerSwapHandItemsEvent event) {
|
|
var scene = scenes.get(event.getPlayer().getUniqueId());
|
|
if (scene == null) return;
|
|
event.setCancelled(true);
|
|
finish(scene, true, true);
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
public void teleport(PlayerTeleportEvent event) {
|
|
interrupt(event.getPlayer());
|
|
}
|
|
|
|
@EventHandler public void changedWorld(PlayerChangedWorldEvent event) {
|
|
interrupt(event.getPlayer());
|
|
}
|
|
|
|
@EventHandler public void death(org.bukkit.event.entity.PlayerDeathEvent event) {
|
|
interrupt(event.getEntity());
|
|
}
|
|
|
|
@EventHandler public void quit(PlayerQuitEvent event) {
|
|
var scene = scenes.get(event.getPlayer().getUniqueId());
|
|
if (scene != null) finish(scene, true, true);
|
|
}
|
|
|
|
private void interrupt(Player viewer) {
|
|
var scene = scenes.get(viewer.getUniqueId());
|
|
if (scene != null && !scene.positioning) finish(scene, false, true);
|
|
}
|
|
|
|
private void finish(Scene scene, boolean restoreLocation, boolean dismiss) {
|
|
if (scene.closed) return;
|
|
scene.closed = true;
|
|
scenes.remove(scene.viewer.getUniqueId(), scene);
|
|
Player viewer = scene.viewer;
|
|
var handle = ((CraftPlayer) viewer).getHandle();
|
|
var connection = handle.connection;
|
|
var pipeline = connection.connection.channel.pipeline();
|
|
try {
|
|
if (pipeline.get(INPUT) != null) pipeline.remove(INPUT);
|
|
if (viewer.isOnline() && !scene.entities.isEmpty()) {
|
|
if (scene.presentationMasked) {
|
|
maskHands(handle, false);
|
|
sidebar(viewer, false);
|
|
}
|
|
connection.send(new ClientboundSetCameraPacket(handle));
|
|
if (scene.mountId != 0) connection.send(passengers(scene.mountId));
|
|
connection.send(new ClientboundRemoveEntitiesPacket(scene.entities.stream().mapToInt(Entity::getId).toArray()));
|
|
}
|
|
} finally {
|
|
viewer.setGravity(scene.gravity);
|
|
if (restoreLocation) viewer.teleport(scene.original);
|
|
if (dismiss) scene.dismiss.run();
|
|
}
|
|
}
|
|
|
|
private BufferedImage artwork(Scene scene) {
|
|
var image = new BufferedImage(COLS * 128, ROWS * 128, BufferedImage.TYPE_INT_RGB);
|
|
var draw = image.createGraphics();
|
|
try {
|
|
Palette palette = palette(scene);
|
|
draw.setColor(palette.background);
|
|
draw.fillRect(0, 0, image.getWidth(), image.getHeight());
|
|
draw.setColor(palette.accent);
|
|
draw.setStroke(new BasicStroke(7));
|
|
draw.drawRect(445, 335, 518, 374);
|
|
draw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 38));
|
|
String heading = scene.roleplay != null ? scene.roleplay.heading :
|
|
scene.document.style() == Style.AUTO ? "YOUR DECISIONS" : styledTitle(scene.document);
|
|
int headlineWidth = draw.getFontMetrics().stringWidth(heading);
|
|
draw.drawString(heading, (image.getWidth() - headlineWidth) / 2, 372);
|
|
if (scene.roleplay != null) drawRoleplay(draw, scene);
|
|
else if (scene.document.style() != Style.AUTO) drawStyledIdentity(draw, scene, palette);
|
|
for (int row = 3; row <= LAST_ROW; row++) for (int col = 4; col <= 6; col++) {
|
|
Card card = cardAt(scene.page, row, col);
|
|
if (card == null || card.slot() == EMPTY) continue;
|
|
int x = col * 128;
|
|
int y = row * 128;
|
|
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;
|
|
draw.setColor(Color.WHITE);
|
|
draw.setFont(CARD_TITLE);
|
|
writeWrapped(draw, card.title(), x + 7, y + 24, 114, bottom ? 2 : 3);
|
|
String lore = displayLore(scene.roleplay, card);
|
|
if (!lore.isBlank()) {
|
|
draw.setFont(CARD_LORE);
|
|
writeWrapped(draw, lore, x + 7, y + (card.slot() < 0 ? 45 : bottom ? 64 : 78), 114, bottom ? 2 : 3);
|
|
}
|
|
}
|
|
} finally {
|
|
draw.dispose();
|
|
}
|
|
return image;
|
|
}
|
|
|
|
private record Palette(Color background, Color tile, Color accent) {}
|
|
|
|
private static Palette palette(Scene scene) {
|
|
return switch (scene.document.style()) {
|
|
case ORGANISATION -> new Palette(new Color(16, 31, 37), new Color(29, 75, 79), new Color(142, 228, 211));
|
|
case EXCHANGE, EXCHANGE_CHECKOUT -> new Palette(new Color(42, 28, 18), new Color(91, 62, 36), new Color(245, 208, 139));
|
|
case JOURNAL -> new Palette(new Color(30, 40, 31), new Color(61, 80, 51), new Color(235, 212, 154));
|
|
case HOUSE -> new Palette(new Color(34, 26, 39), new Color(72, 52, 79), new Color(244, 211, 152));
|
|
case AUTO -> scene.roleplay == null
|
|
? new Palette(new Color(25, 40, 46), new Color(52, 93, 107), new Color(237, 199, 105))
|
|
: new Palette(new Color(9, 33, 24), new Color(38, 80, 61), new Color(237, 199, 105));
|
|
};
|
|
}
|
|
|
|
private static boolean fitsStyled(Document document) {
|
|
var graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
|
|
try {
|
|
graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 38));
|
|
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;
|
|
graphics.setFont(CHECKOUT_TERMS);
|
|
var terms = checkoutTerms(graphics, document.description());
|
|
if (terms.isEmpty() || terms.size() > 9) return false;
|
|
}
|
|
graphics.setFont(CARD_TITLE);
|
|
if (document.identity() != null && !wrap(graphics, document.identity().mentat(), 112, 2).complete) return false;
|
|
for (Card card : document.cards()) {
|
|
boolean seal = document.style() == Style.HOUSE && nativeArt(card.material()) != null;
|
|
graphics.setFont(CARD_TITLE);
|
|
if (document.style() == Style.JOURNAL) {
|
|
if (!wrap(graphics, card.title(), 114, 2).complete) return false;
|
|
graphics.setFont(JOURNAL_LORE);
|
|
if (!journalLore(graphics, card).complete) return false;
|
|
continue;
|
|
}
|
|
if (!wrap(graphics, card.title(), 114, seal ? 2 : 3).complete) return false;
|
|
graphics.setFont(CARD_LORE);
|
|
if (!wrap(graphics, styledLore(document.style(), card), 114, seal ? 2 : 3).complete) return false;
|
|
}
|
|
return true;
|
|
} finally {
|
|
graphics.dispose();
|
|
}
|
|
}
|
|
|
|
private static String styledTitle(Document document) {
|
|
return document.style() == Style.EXCHANGE_CHECKOUT ? "Exchange checkout" : document.title();
|
|
}
|
|
|
|
private static boolean hasCheckoutAction(Document document, int slot) {
|
|
return document.cards().stream().anyMatch(card -> card.slot() == slot && card.actionable());
|
|
}
|
|
|
|
private static List<String> checkoutTerms(java.awt.Graphics2D graphics, String description) {
|
|
var lines = new ArrayList<String>();
|
|
for (String term : description.lines().toList()) {
|
|
var wrapped = wrap(graphics, term, 106, 9);
|
|
if (!wrapped.complete) return List.of();
|
|
lines.addAll(wrapped.lines);
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
private static Wrapped journalLore(java.awt.Graphics2D draw, Card card) {
|
|
var lines = new ArrayList<String>();
|
|
for (String entry : card.lore()) {
|
|
var wrapped = wrap(draw, entry, 114, JOURNAL_LORE_LINES - lines.size());
|
|
lines.addAll(wrapped.lines());
|
|
if (!wrapped.complete()) return new Wrapped(lines, false);
|
|
}
|
|
return new Wrapped(lines, true);
|
|
}
|
|
|
|
private static String styledLore(Style style, Card card) {
|
|
if (card.lore().isEmpty()) return "";
|
|
if (style == Style.HOUSE && card.material().startsWith("house_"))
|
|
return card.lore().getFirst().equals("Your current House") ? card.lore().getFirst() : "";
|
|
if (style == Style.EXCHANGE_CHECKOUT && !card.actionable()) return String.join(" ", card.lore());
|
|
if (style == Style.EXCHANGE || style == Style.EXCHANGE_CHECKOUT) {
|
|
// The listing renderer appends the server price after the stored item lore.
|
|
for (int index = card.lore().size() - 1; index >= 0; index--) {
|
|
String line = card.lore().get(index);
|
|
if (line.endsWith(" DM each") || line.endsWith(" DM")) return line;
|
|
}
|
|
}
|
|
return card.lore().getFirst();
|
|
}
|
|
|
|
private void drawStyledIdentity(java.awt.Graphics2D draw, Scene scene, Palette palette) {
|
|
var document = scene.document;
|
|
var identity = document.identity();
|
|
if (document.style() == Style.EXCHANGE) {
|
|
drawResource(draw, "/mcgui/native/exchange.png", 454, 343, 40, 31);
|
|
draw.setColor(palette.accent);
|
|
for (int row = 3; row <= 4; row++) draw.fillRect(510, row * 128 + 124, 388, 4);
|
|
return;
|
|
}
|
|
if (document.style() == Style.HOUSE) {
|
|
drawResource(draw, "/mcgui/dunerealms-season-logo-v2.png", 454, 343, 65, 31);
|
|
return;
|
|
}
|
|
draw.setColor(palette.background.darker());
|
|
draw.fillRect(644, 389, 120, 232);
|
|
switch (document.style()) {
|
|
case JOURNAL -> {
|
|
drawResource(draw, "/mcgui/dunerealms-season-logo-v2.png", 650, 449, 108, 96);
|
|
draw.setColor(palette.accent);
|
|
draw.drawLine(650, 446, 758, 446);
|
|
draw.drawLine(650, 549, 758, 549);
|
|
}
|
|
case EXCHANGE_CHECKOUT -> {
|
|
draw.setColor(Color.WHITE);
|
|
draw.setFont(CHECKOUT_TERMS);
|
|
int y = 420;
|
|
for (String line : checkoutTerms(draw, document.description())) {
|
|
draw.drawString(line, 651, y);
|
|
y += draw.getFontMetrics().getHeight();
|
|
}
|
|
}
|
|
case ORGANISATION -> {
|
|
if (identity != null) {
|
|
BufferedImage mark = organisationLogo(identity.logo());
|
|
if (mark != null) drawImage(draw, mark, 660, 397, 88, 74);
|
|
if (identity.portrait() >= 0 && identity.portrait() <= 7)
|
|
drawSceneArt(draw, "npc_avatar_" + identity.portrait(), 662, 477, 84, 72);
|
|
draw.setColor(palette.accent);
|
|
draw.setFont(CARD_LORE);
|
|
writeWrapped(draw, identity.mentat().isBlank() ? "" : "Mentat", 649, 567, 110, 1);
|
|
draw.setFont(CARD_TITLE);
|
|
writeWrapped(draw, identity.mentat(), 649, 589, 110, 2);
|
|
}
|
|
}
|
|
|
|
default -> {}
|
|
}
|
|
}
|
|
|
|
private boolean drawStyledCard(java.awt.Graphics2D draw, Style style, Card card, Palette palette, int x, int y) {
|
|
if (style == Style.AUTO) return false;
|
|
String art = nativeArt(card.material());
|
|
draw.setColor(palette.accent);
|
|
if (style == Style.JOURNAL) {
|
|
draw.drawLine(x + 7, y + 48, x + 121, y + 48);
|
|
draw.setColor(Color.WHITE);
|
|
draw.setFont(CARD_TITLE);
|
|
writeWrapped(draw, card.title(), x + 7, y + 21, 114, 2);
|
|
draw.setColor(palette.accent);
|
|
draw.setFont(JOURNAL_LORE);
|
|
int lineY = y + 64;
|
|
for (String line : journalLore(draw, card).lines()) {
|
|
draw.drawString(line, x + 7, lineY);
|
|
lineY += draw.getFontMetrics().getHeight();
|
|
}
|
|
return true;
|
|
} else if (style == Style.ORGANISATION) {
|
|
draw.fillRect(x, y + 3, 4, 122);
|
|
draw.drawLine(x + 7, y + 69, x + 119, y + 69);
|
|
} else if (style == Style.EXCHANGE || style == Style.EXCHANGE_CHECKOUT) {
|
|
draw.fillRect(x, y + 70, 128, 5);
|
|
draw.fillRect(x, y + 120, 128, 5);
|
|
} else if (art != null) {
|
|
drawResource(draw, art, x + 39, y + 7, 50, 48);
|
|
draw.setFont(CARD_TITLE);
|
|
writeWrapped(draw, card.title(), x + 7, y + 71, 114, 2);
|
|
draw.setFont(CARD_LORE);
|
|
writeWrapped(draw, styledLore(style, card), x + 7, y + 104, 114, 2);
|
|
return true;
|
|
}
|
|
draw.setColor(Color.WHITE);
|
|
draw.setFont(CARD_TITLE);
|
|
writeWrapped(draw, card.title(), x + 7, y + 24, 114, 3);
|
|
draw.setColor(palette.accent);
|
|
draw.setFont(CARD_LORE);
|
|
writeWrapped(draw, styledLore(style, card), x + 7, y + 87, 114, 3);
|
|
return true;
|
|
}
|
|
|
|
private static String nativeArt(String key) {
|
|
if (key.matches("mentat_(atreides|corrino|fremen|harkonnen|ordos)"))
|
|
return "/mcgui/native/" + key.replace('_', '-') + "-v1.png";
|
|
return switch (key) {
|
|
case "house_atreides" -> "/mcgui/native/house-aerdrecht-v1.png";
|
|
case "house_corrino" -> "/mcgui/native/house-korenholt-v1.png";
|
|
case "house_fremen" -> "/mcgui/native/house-vrijduin-v1.png";
|
|
case "house_harkonnen" -> "/mcgui/native/house-harkveen-v1.png";
|
|
case "house_ordos" -> "/mcgui/native/house-oordwijk-v1.png";
|
|
default -> null;
|
|
};
|
|
}
|
|
|
|
private static BufferedImage organisationLogo(String value) {
|
|
if (value.length() > 110000 || value.isBlank()) return null;
|
|
try (var input = javax.imageio.ImageIO.createImageInputStream(
|
|
new java.io.ByteArrayInputStream(Base64.getDecoder().decode(value)))) {
|
|
var readers = javax.imageio.ImageIO.getImageReaders(input);
|
|
if (!readers.hasNext()) return null;
|
|
var reader = readers.next();
|
|
try {
|
|
reader.setInput(input, true, true);
|
|
if (!reader.getFormatName().equalsIgnoreCase("png") ||
|
|
reader.getWidth(0) > 512 || reader.getHeight(0) > 512) return null;
|
|
return reader.read(0);
|
|
} finally {
|
|
reader.dispose();
|
|
}
|
|
} catch (IllegalArgumentException | java.io.IOException error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static Roleplay roleplay(Document document) {
|
|
String[] title = document.title().split(" · ", 2);
|
|
if (title.length != 2) return null;
|
|
String heading = switch (title[0]) {
|
|
case "NPC dashboard" -> "YOUR MISSIONS";
|
|
case "NPC next quest" -> "YOUR NEXT MISSION";
|
|
case "NPC turn-in" -> document.cards().stream().anyMatch(card ->
|
|
card.actionable() && (card.title().equals("Claim verified reward") || card.title().equals("Next quest")))
|
|
? "MISSION COMPLETE" : "MISSION TURN-IN";
|
|
default -> null;
|
|
};
|
|
if (heading == null) return null;
|
|
var portrait = document.cards().stream().filter(card -> card.material().matches("npc_avatar_[0-7]")).findFirst().orElse(null);
|
|
var world = document.cards().stream().filter(card -> card.material().matches("npc_world_[a-z_]+")).findFirst().orElse(null);
|
|
var summary = document.description().isBlank() ? document.cards().stream()
|
|
.filter(card -> card.material().equals("npc_summary")).findFirst().orElse(null) : null;
|
|
return new Roleplay(heading, portrait == null ? title[1] : portrait.title(), portrait, world, summary);
|
|
}
|
|
|
|
private static Document presentable(Document document, Roleplay roleplay) {
|
|
if (roleplay == null) return document;
|
|
var cards = document.cards().stream()
|
|
.filter(card -> card.actionable() || card != roleplay.portrait && card != roleplay.world && card != roleplay.summary)
|
|
.toList();
|
|
return new Document(document.title(), document.description(), cards, document.style(), document.identity());
|
|
}
|
|
|
|
private static String displayLore(Roleplay roleplay, Card card) {
|
|
if (card.lore().isEmpty()) return "";
|
|
if (roleplay == null || card.slot() < 0) return card.lore().getFirst();
|
|
if (!card.actionable()) return String.join(" ", card.lore());
|
|
return roleplay.heading.equals("YOUR MISSIONS") && card.slot() >= 9 && card.slot() < 45
|
|
? card.lore().getFirst() : "";
|
|
}
|
|
|
|
private static boolean fitsRoleplay(Document document, Roleplay roleplay) {
|
|
var graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
|
|
try {
|
|
graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
|
|
if (!wrap(graphics, roleplay.contact, 112, 2).complete) return false;
|
|
if (!fitsCards(graphics, document, roleplay)) return false;
|
|
graphics.setFont(CARD_LORE);
|
|
String summary = roleplay.summary == null ? document.description() : String.join(" ", roleplay.summary.lore());
|
|
return wrap(graphics, summary, 112, 6).complete;
|
|
} finally {
|
|
graphics.dispose();
|
|
}
|
|
}
|
|
|
|
private static boolean fitsCards(java.awt.Graphics2D graphics, Document document, Roleplay roleplay) {
|
|
for (Card card : document.cards()) {
|
|
graphics.setFont(CARD_TITLE);
|
|
if (!wrap(graphics, card.title(), 114, 3).complete) return false;
|
|
graphics.setFont(CARD_LORE);
|
|
if (!wrap(graphics, displayLore(roleplay, card), 114, 3).complete) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void drawRoleplay(java.awt.Graphics2D draw, Scene scene) {
|
|
var role = scene.roleplay;
|
|
draw.setColor(new Color(23, 56, 40));
|
|
draw.fillRect(644, 389, 120, 232);
|
|
String asset = role.portrait == null ? "npc_avatar_7" : role.portrait.material();
|
|
drawSceneArt(draw, asset, 650, 395, 108, 83);
|
|
draw.setColor(new Color(250, 226, 169));
|
|
draw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
|
|
writeWrapped(draw, role.contact, 648, 498, 112, 2);
|
|
draw.setColor(Color.WHITE);
|
|
draw.setFont(CARD_LORE);
|
|
String summary = role.summary == null ? scene.document.description() : String.join(" ", role.summary.lore());
|
|
if (summary.isBlank() && role.world != null) drawSceneArt(draw, role.world.material(), 650, 518, 108, 78);
|
|
else writeWrapped(draw, summary, 648, 532, 112, 6);
|
|
}
|
|
|
|
private void drawSceneArt(java.awt.Graphics2D draw, String asset, int x, int y, int width, int height) {
|
|
drawResource(draw, "/mcgui-npc-avatars/" + asset + ".png", x, y, width, height);
|
|
}
|
|
|
|
private void drawResource(java.awt.Graphics2D draw, String resource, int x, int y, int width, int height) {
|
|
if (resource == null) return;
|
|
try (var stream = DecisionsViewTransport.class.getResourceAsStream(resource)) {
|
|
if (stream == null) return;
|
|
var image = javax.imageio.ImageIO.read(stream);
|
|
if (image == null) return;
|
|
drawImage(draw, image, x, y, width, height);
|
|
} catch (java.io.IOException error) {
|
|
plugin.getLogger().warning("Could not render fullscreen artwork: " + resource);
|
|
}
|
|
}
|
|
|
|
private static void drawImage(java.awt.Graphics2D draw, BufferedImage image, int x, int y, int width, int height) {
|
|
double scale = Math.min((double) width / image.getWidth(), (double) height / image.getHeight());
|
|
int w = (int) Math.round(image.getWidth() * scale), h = (int) Math.round(image.getHeight() * scale);
|
|
draw.drawImage(image, x + (width - w) / 2, y + (height - h) / 2, w, h, null);
|
|
}
|
|
|
|
private record Wrapped(List<String> lines, boolean complete) {}
|
|
|
|
private static void writeWrapped(java.awt.Graphics2D draw, String text, int x, int y, int width, int lines) {
|
|
int lineHeight = draw.getFontMetrics().getHeight();
|
|
for (String line : wrap(draw, text, width, lines).lines()) {
|
|
draw.drawString(line, x, y);
|
|
y += lineHeight;
|
|
}
|
|
}
|
|
|
|
private static Wrapped wrap(java.awt.Graphics2D draw, String text, int width, int lines) {
|
|
var metrics = draw.getFontMetrics();
|
|
var result = new ArrayList<String>();
|
|
String remaining = text.strip().replaceAll("\\s+", " ");
|
|
while (result.size() < lines && !remaining.isEmpty()) {
|
|
int end = remaining.length();
|
|
while (end > 1 && metrics.stringWidth(remaining.substring(0, end)) > width) end--;
|
|
if (end < remaining.length()) {
|
|
int wordBreak = remaining.lastIndexOf(' ', end);
|
|
if (wordBreak > 0) end = wordBreak;
|
|
}
|
|
result.add(remaining.substring(0, end));
|
|
remaining = remaining.substring(end).stripLeading();
|
|
}
|
|
return new Wrapped(List.copyOf(result), remaining.isEmpty());
|
|
}
|
|
|
|
private BufferedImage focused(BufferedImage normal, int height) {
|
|
var image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB);
|
|
var draw = image.createGraphics();
|
|
try {
|
|
draw.drawImage(normal, 0, 0, null);
|
|
draw.setColor(new Color(250, 224, 152));
|
|
draw.setStroke(new BasicStroke(8));
|
|
draw.drawRect(5, 5, 117, height - 5);
|
|
} finally {
|
|
draw.dispose();
|
|
}
|
|
return image;
|
|
}
|
|
}
|

Staff