| 1 | package com.jellypudding.offlineclient.modules.render;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.ClientTickEvent;
|
| 5 | import com.jellypudding.offlineclient.gui.PeekScreen;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.render.BannerPreview;
|
| 9 | import com.jellypudding.offlineclient.render.BookPreview;
|
| 10 | import com.jellypudding.offlineclient.render.ContainerPreview;
|
| 11 | import com.jellypudding.offlineclient.render.EntityPreview;
|
| 12 | import com.jellypudding.offlineclient.render.MapPreview;
|
| 13 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 14 | import com.jellypudding.offlineclient.setting.EnumSetting;
|
| 15 | import com.jellypudding.offlineclient.setting.KeybindSetting;
|
| 16 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 17 | import com.jellypudding.offlineclient.util.ColorUtil;
|
| 18 | import net.minecraft.ChatFormatting;
|
| 19 | import net.minecraft.client.gui.screens.Screen;
|
| 20 | import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
| 21 | import net.minecraft.client.gui.screens.inventory.BookViewScreen;
|
| 22 | import net.minecraft.client.gui.screens.inventory.ContainerScreen;
|
| 23 | import net.minecraft.client.gui.screens.inventory.InventoryScreen;
|
| 24 | import net.minecraft.client.gui.screens.inventory.tooltip.ClientBundleTooltip;
|
| 25 | import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
|
| 26 | import net.minecraft.client.input.KeyEvent;
|
| 27 | import net.minecraft.client.input.MouseButtonEvent;
|
| 28 | import net.minecraft.client.renderer.state.MapRenderState;
|
| 29 | import net.minecraft.core.HolderSet;
|
| 30 | import net.minecraft.core.NonNullList;
|
| 31 | import net.minecraft.core.component.DataComponents;
|
| 32 | import net.minecraft.nbt.NbtOps;
|
| 33 | import net.minecraft.nbt.Tag;
|
| 34 | import net.minecraft.network.chat.Component;
|
| 35 | import net.minecraft.network.chat.MutableComponent;
|
| 36 | import net.minecraft.world.Container;
|
| 37 | import net.minecraft.world.effect.MobEffectInstance;
|
| 38 | import net.minecraft.world.effect.MobEffectUtil;
|
| 39 | import net.minecraft.world.entity.Bucketable;
|
| 40 | import net.minecraft.world.entity.Entity;
|
| 41 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 42 | import net.minecraft.world.entity.LivingEntity;
|
| 43 | import net.minecraft.world.food.FoodProperties;
|
| 44 | import net.minecraft.world.inventory.ChestMenu;
|
| 45 | import net.minecraft.world.item.BannerItem;
|
| 46 | import net.minecraft.world.item.BundleItem;
|
| 47 | import net.minecraft.world.item.DyeColor;
|
| 48 | import net.minecraft.world.item.Item;
|
| 49 | import net.minecraft.world.item.ItemStack;
|
| 50 | import net.minecraft.world.item.ItemStackTemplate;
|
| 51 | import net.minecraft.world.item.Items;
|
| 52 | import net.minecraft.world.item.MapItem;
|
| 53 | import net.minecraft.world.item.MobBucketItem;
|
| 54 | import net.minecraft.world.item.component.BundleContents;
|
| 55 | import net.minecraft.world.item.component.Consumable;
|
| 56 | import net.minecraft.world.item.component.CustomData;
|
| 57 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 58 | import net.minecraft.world.item.component.SuspiciousStewEffects;
|
| 59 | import net.minecraft.world.item.component.WritableBookContent;
|
| 60 | import net.minecraft.world.item.component.WrittenBookContent;
|
| 61 | import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
|
| 62 | import net.minecraft.world.level.block.Block;
|
| 63 | import net.minecraft.world.level.block.Blocks;
|
| 64 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 65 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 66 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 67 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 68 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 69 | import org.lwjgl.glfw.GLFW;
|
| 70 |
|
| 71 | import java.util.ArrayList;
|
| 72 | import java.util.Comparator;
|
| 73 | import java.util.LinkedHashMap;
|
| 74 | import java.util.List;
|
| 75 | import java.util.Locale;
|
| 76 | import java.util.Map;
|
| 77 | import java.util.Optional;
|
| 78 | import java.util.function.Consumer;
|
| 79 |
|
| 80 | // GuiGraphicsExtractorMixin remembers the hovered stack and asks for the extra
|
| 81 | // tooltip rows. The container and tooltip display mixins ask what to hide.
|
| 82 | public final class BetterTooltips extends Module {
|
| 83 |
|
| 84 | public enum When { KEY_HELD, ALWAYS }
|
| 85 |
|
| 86 | public enum OpenWith { MIDDLE_CLICK, KEY }
|
| 87 |
|
| 88 | public enum SizeUnit { BYTES, KILOBYTES, MEGABYTES, AUTO }
|
| 89 |
|
| 90 | private static final int SHULKER_SLOTS = 27;
|
| 91 | private static final int ENDER_CHEST_SLOTS = 27;
|
| 92 | private static final int COLUMNS = 9;
|
| 93 | private static final int COMPACT_ROWS = 5;
|
| 94 | private static final int KILOBYTE = 1024;
|
| 95 | private static final int MEGABYTE = KILOBYTE * KILOBYTE;
|
| 96 | private static final int ENDER_TINT = 0xC0003232;
|
| 97 | private static final int PREVIEW_ALPHA = 0xC0;
|
| 98 | private static final float TINT_SHADE = 0.45f;
|
| 99 |
|
| 100 | private final EnumSetting<When> show = new EnumSetting<>("Show previews",
|
| 101 | "When the picture previews appear in a tooltip.", When.KEY_HELD)
|
| 102 | .describe(When.KEY_HELD, "Only whilst the preview key is held.")
|
| 103 | .describe(When.ALWAYS, "On every tooltip.");
|
| 104 | private final KeybindSetting previewKey = new KeybindSetting("Preview key",
|
| 105 | "The key to hold for a preview.", GLFW.GLFW_KEY_LEFT_ALT)
|
| 106 | .under(show, When.KEY_HELD);
|
| 107 | private final BoolSetting containers = new BoolSetting("Containers",
|
| 108 | "Hover a shulker box in any inventory to see what is packed inside it.", true);
|
| 109 | private final BoolSetting compactList = new BoolSetting("Compact list",
|
| 110 | "Replace the vanilla shulker item lines with the five biggest stacks by count.", true)
|
| 111 | .under(containers);
|
| 112 | private final BoolSetting enderChest = new BoolSetting("Ender chest",
|
| 113 | "Hover an ender chest item to see what yours held when you last opened one.", true);
|
| 114 | private final BoolSetting maps = new BoolSetting("Maps",
|
| 115 | "Hover a filled map to see the map itself.", true);
|
| 116 | private final NumberSetting mapSize = new NumberSetting("Map size",
|
| 117 | "How big the map preview is drawn.", 128, 64, 256, 8, " px").min(16).max(512)
|
| 118 | .under(maps);
|
| 119 | private final BoolSetting books = new BoolSetting("Books",
|
| 120 | "Hover a book to read its first page.", true);
|
| 121 | private final BoolSetting banners = new BoolSetting("Banners",
|
| 122 | "Hover a banner or pattern or shield to see the flag it makes.", true);
|
| 123 | private final BoolSetting buckets = new BoolSetting("Bucket mobs",
|
| 124 | "Hover a bucket with a fish or axolotl inside to see the creature.", true);
|
| 125 | private final BoolSetting bundles = new BoolSetting("Bundles",
|
| 126 | "Hover a bundle to see every item inside it rather than the vanilla few.", true);
|
| 127 | private final BoolSetting foodInfo = new BoolSetting("Food info",
|
| 128 | "Add a line with the hunger and saturation of food.", true);
|
| 129 | private final BoolSetting effects = new BoolSetting("Effects",
|
| 130 | "List the potion effects a food or stew gives with their length.", true);
|
| 131 | private final BoolSetting byteSize = new BoolSetting("Byte size",
|
| 132 | "Add a line with how many bytes the item takes up.", true);
|
| 133 | private final EnumSetting<SizeUnit> sizeUnit = new EnumSetting<>("Size unit",
|
| 134 | "The unit the byte size is written in.", SizeUnit.AUTO)
|
| 135 | .describe(SizeUnit.AUTO, "Whichever unit fits the number best.")
|
| 136 | .under(byteSize);
|
| 137 | private final BoolSetting hiddenTooltips = new BoolSetting("Show hidden tooltips",
|
| 138 | "Show a tooltip even when the item asks for it to be hidden.", false);
|
| 139 | private final BoolSetting hiddenLines = new BoolSetting("Show hidden lines",
|
| 140 | "Show lines the item hides such as enchantments and lore.", false);
|
| 141 | private final BoolSetting openContents = new BoolSetting("Open contents",
|
| 142 | "Click a container or book in an inventory to open a window showing what is inside.", true);
|
| 143 | private final EnumSetting<OpenWith> openWith = new EnumSetting<>("Open with",
|
| 144 | "What opens the window.", OpenWith.MIDDLE_CLICK)
|
| 145 | .describe(OpenWith.MIDDLE_CLICK, "The middle mouse button.")
|
| 146 | .describe(OpenWith.KEY, "The key picked below.")
|
| 147 | .under(openContents);
|
| 148 | private final KeybindSetting openKey = new KeybindSetting("Open key",
|
| 149 | "The key that opens the window.", GLFW.GLFW_KEY_P)
|
| 150 | .under(openWith, OpenWith.KEY);
|
| 151 | private final BoolSetting pauseInCreative = new BoolSetting("Pause in creative",
|
| 152 | "Leave the click alone in creative so middle click still clones the item.", true)
|
| 153 | .under(openContents);
|
| 154 |
|
| 155 | // What the ender chest held the last time it was open this session.
|
| 156 | private final List<ItemStack> remembered = new ArrayList<>();
|
| 157 | private ItemStack hovered = ItemStack.EMPTY;
|
| 158 |
|
| 159 | public BetterTooltips() {
|
| 160 | super("BetterTooltips", "Previews and extra facts in item tooltips.", Category.RENDER);
|
| 161 | addSettings(show, previewKey, containers, compactList, enderChest, maps, mapSize, books, banners,
|
| 162 | buckets, bundles, foodInfo, effects, byteSize, sizeUnit, hiddenTooltips, hiddenLines,
|
| 163 | openContents, openWith, openKey, pauseInCreative);
|
| 164 | searchTags("tooltip", "shulker", "preview", "peek");
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected void onDisable() {
|
| 169 | hovered = ItemStack.EMPTY;
|
| 170 | // The copy belongs to the server it was taken on.
|
| 171 | remembered.clear();
|
| 172 | }
|
| 173 |
|
| 174 | // Called from the mixin whilst the game builds an item tooltip.
|
| 175 | public void setHovered(ItemStack stack) {
|
| 176 | hovered = stack;
|
| 177 | }
|
| 178 |
|
| 179 | @Subscribe
|
| 180 | private void onClientTick(ClientTickEvent event) {
|
| 181 | if (mc.level == null) {
|
| 182 | remembered.clear();
|
| 183 | return;
|
| 184 | }
|
| 185 | if (!enderChest.isOn() || !(mc.gui.screen() instanceof ContainerScreen screen)) {
|
| 186 | return;
|
| 187 | }
|
| 188 | if (!screen.getTitle().getString().equals(Blocks.ENDER_CHEST.getName().getString())) {
|
| 189 | return;
|
| 190 | }
|
| 191 | ChestMenu menu = screen.getMenu();
|
| 192 | Container container = menu.getContainer();
|
| 193 | if (container.getContainerSize() < ENDER_CHEST_SLOTS) {
|
| 194 | return;
|
| 195 | }
|
| 196 | remembered.clear();
|
| 197 | for (int i = 0; i < ENDER_CHEST_SLOTS; i++) {
|
| 198 | remembered.add(container.getItem(i).copy());
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | // The tooltip rows with our lines and preview worked in. The hovered stack is used up.
|
| 203 | public List<ClientTooltipComponent> decorate(List<ClientTooltipComponent> rows) {
|
| 204 | ItemStack stack = hovered;
|
| 205 | hovered = ItemStack.EMPTY;
|
| 206 | if (!isEnabled() || stack.isEmpty()) {
|
| 207 | return rows;
|
| 208 | }
|
| 209 | List<ClientTooltipComponent> out = new ArrayList<>(rows);
|
| 210 | // An empty tooltip is one the item hides and it only ever gets the hint.
|
| 211 | boolean hidden = rows.isEmpty();
|
| 212 | if (!hidden) {
|
| 213 | int at = Math.min(1, out.size());
|
| 214 | for (Component line : startLines(stack)) {
|
| 215 | out.add(at++, text(line));
|
| 216 | }
|
| 217 | Component size = byteSizeLine(stack);
|
| 218 | if (size != null) {
|
| 219 | out.add(text(size));
|
| 220 | }
|
| 221 | }
|
| 222 | if (previewing()) {
|
| 223 | ClientTooltipComponent preview = preview(stack);
|
| 224 | if (preview != null) {
|
| 225 | if (stack.getItem() instanceof BundleItem) {
|
| 226 | out.removeIf(row -> row instanceof ClientBundleTooltip);
|
| 227 | }
|
| 228 | out.add(preview);
|
| 229 | }
|
| 230 | } else if (previewable(stack)) {
|
| 231 | if (!hidden) {
|
| 232 | out.add(text(Component.empty()));
|
| 233 | }
|
| 234 | out.add(text(Component.literal("Hold ")
|
| 235 | .append(Component.literal(previewKey.getKeyName()).withStyle(ChatFormatting.YELLOW))
|
| 236 | .append(" to preview")));
|
| 237 | }
|
| 238 | return out;
|
| 239 | }
|
| 240 |
|
| 241 | private static ClientTooltipComponent text(Component line) {
|
| 242 | return ClientTooltipComponent.create(line.getVisualOrderText());
|
| 243 | }
|
| 244 |
|
| 245 | private boolean previewing() {
|
| 246 | return show.is(When.ALWAYS) || previewKey.isHeld();
|
| 247 | }
|
| 248 |
|
| 249 | // True whilst a preview is up. The vanilla shulker item lines can then go.
|
| 250 | public boolean skipsContainerLines() {
|
| 251 | return isEnabled() && containers.isOn() && previewing();
|
| 252 | }
|
| 253 |
|
| 254 | public boolean compactsContainerLines() {
|
| 255 | return isEnabled() && containers.isOn() && compactList.isOn();
|
| 256 | }
|
| 257 |
|
| 258 | public boolean showsHiddenTooltips() {
|
| 259 | return isEnabled() && hiddenTooltips.isOn();
|
| 260 | }
|
| 261 |
|
| 262 | public boolean showsHiddenLines() {
|
| 263 | return isEnabled() && hiddenLines.isOn();
|
| 264 | }
|
| 265 |
|
| 266 | // The five biggest item types by count and how many more kinds there are.
|
| 267 | public void compactLines(List<Optional<ItemStackTemplate>> slots, Consumer<Component> out) {
|
| 268 | Map<Item, Integer> counts = new LinkedHashMap<>();
|
| 269 | for (Optional<ItemStackTemplate> slot : slots) {
|
| 270 | if (slot.isEmpty() || slot.get().count() == 0) {
|
| 271 | continue;
|
| 272 | }
|
| 273 | counts.merge(slot.get().item().value(), slot.get().count(), Integer::sum);
|
| 274 | }
|
| 275 | counts.entrySet().stream()
|
| 276 | .sorted(Comparator.comparingInt((Map.Entry<Item, Integer> entry) -> entry.getValue()).reversed())
|
| 277 | .limit(COMPACT_ROWS)
|
| 278 | .forEach(entry -> out.accept(entry.getKey().getName(new ItemStack(entry.getKey())).copy()
|
| 279 | .append(Component.literal(" x" + entry.getValue()).withStyle(ChatFormatting.GRAY))));
|
| 280 | if (counts.size() > COMPACT_ROWS) {
|
| 281 | out.accept(Component.translatable("item.container.more_items", counts.size() - COMPACT_ROWS)
|
| 282 | .withStyle(ChatFormatting.ITALIC));
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | // Lines that go right under the item name.
|
| 287 | private List<Component> startLines(ItemStack stack) {
|
| 288 | List<Component> lines = new ArrayList<>();
|
| 289 | if (effects.isOn()) {
|
| 290 | for (MobEffectInstance effect : effectsOf(stack)) {
|
| 291 | lines.add(effectLine(effect));
|
| 292 | }
|
| 293 | }
|
| 294 | FoodProperties food = stack.get(DataComponents.FOOD);
|
| 295 | if (foodInfo.isOn() && food != null) {
|
| 296 | lines.add(Component.literal(String.format(Locale.ROOT, "Hunger %d and saturation %.1f",
|
| 297 | food.nutrition(), food.saturation())).withStyle(ChatFormatting.GRAY));
|
| 298 | }
|
| 299 | return lines;
|
| 300 | }
|
| 301 |
|
| 302 | private static List<MobEffectInstance> effectsOf(ItemStack stack) {
|
| 303 | List<MobEffectInstance> found = new ArrayList<>();
|
| 304 | SuspiciousStewEffects stew = stack.get(DataComponents.SUSPICIOUS_STEW_EFFECTS);
|
| 305 | if (stew != null) {
|
| 306 | for (SuspiciousStewEffects.Entry entry : stew.effects()) {
|
| 307 | found.add(entry.createEffectInstance());
|
| 308 | }
|
| 309 | return found;
|
| 310 | }
|
| 311 | Consumable consumable = stack.get(DataComponents.CONSUMABLE);
|
| 312 | if (consumable == null) {
|
| 313 | return found;
|
| 314 | }
|
| 315 | consumable.onConsumeEffects().forEach(effect -> {
|
| 316 | if (effect instanceof ApplyStatusEffectsConsumeEffect apply) {
|
| 317 | found.addAll(apply.effects());
|
| 318 | }
|
| 319 | });
|
| 320 | return found;
|
| 321 | }
|
| 322 |
|
| 323 | // Blue for a helpful effect and red for a harmful one.
|
| 324 | private Component effectLine(MobEffectInstance effect) {
|
| 325 | float tickRate = mc.level == null ? 20 : mc.level.tickRateManager().tickrate();
|
| 326 | MutableComponent line = Component.translatable(effect.getDescriptionId());
|
| 327 | if (effect.getAmplifier() != 0) {
|
| 328 | line.append(" " + (effect.getAmplifier() + 1));
|
| 329 | }
|
| 330 | line.append(" (").append(MobEffectUtil.formatDuration(effect, 1, tickRate)).append(")");
|
| 331 | return line.withStyle(effect.getEffect().value().isBeneficial() ? ChatFormatting.BLUE : ChatFormatting.RED);
|
| 332 | }
|
| 333 |
|
| 334 | private Component byteSizeLine(ItemStack stack) {
|
| 335 | if (!byteSize.isOn() || mc.player == null) {
|
| 336 | return null;
|
| 337 | }
|
| 338 | Optional<Tag> tag = ItemStack.CODEC
|
| 339 | .encodeStart(mc.player.registryAccess().createSerializationContext(NbtOps.INSTANCE), stack)
|
| 340 | .result();
|
| 341 | if (tag.isEmpty()) {
|
| 342 | return Component.literal("Size unknown").withStyle(ChatFormatting.RED);
|
| 343 | }
|
| 344 | return Component.literal(sizeText(tag.get().sizeInBytes())).withStyle(ChatFormatting.DARK_GRAY);
|
| 345 | }
|
| 346 |
|
| 347 | private String sizeText(int bytes) {
|
| 348 | SizeUnit unit = sizeUnit.getValue();
|
| 349 | if (unit == SizeUnit.AUTO) {
|
| 350 | unit = bytes >= MEGABYTE ? SizeUnit.MEGABYTES : bytes >= KILOBYTE ? SizeUnit.KILOBYTES : SizeUnit.BYTES;
|
| 351 | }
|
| 352 | return switch (unit) {
|
| 353 | case BYTES, AUTO -> bytes + " bytes";
|
| 354 | case KILOBYTES -> String.format(Locale.ROOT, "%.2f kB", bytes / (float) KILOBYTE);
|
| 355 | case MEGABYTES -> String.format(Locale.ROOT, "%.4f MB", bytes / (float) MEGABYTE);
|
| 356 | };
|
| 357 | }
|
| 358 |
|
| 359 | // True for anything that would get a preview whilst the key is held.
|
| 360 | private boolean previewable(ItemStack stack) {
|
| 361 | Item item = stack.getItem();
|
| 362 | if (containers.isOn() && hasContents(stack)) {
|
| 363 | return true;
|
| 364 | }
|
| 365 | if (enderChest.isOn() && item == Items.ENDER_CHEST) {
|
| 366 | return true;
|
| 367 | }
|
| 368 | if (maps.isOn() && item == Items.FILLED_MAP) {
|
| 369 | return true;
|
| 370 | }
|
| 371 | if (books.isOn() && (item == Items.WRITABLE_BOOK || item == Items.WRITTEN_BOOK)) {
|
| 372 | return true;
|
| 373 | }
|
| 374 | if (banners.isOn() && isBannerLike(stack)) {
|
| 375 | return true;
|
| 376 | }
|
| 377 | if (buckets.isOn() && item instanceof MobBucketItem) {
|
| 378 | return true;
|
| 379 | }
|
| 380 | return bundles.isOn() && item instanceof BundleItem && hasBundleItems(stack);
|
| 381 | }
|
| 382 |
|
| 383 | private static boolean hasContents(ItemStack stack) {
|
| 384 | ItemContainerContents contents = stack.get(DataComponents.CONTAINER);
|
| 385 | return contents != null && contents.nonEmptyItems().iterator().hasNext();
|
| 386 | }
|
| 387 |
|
| 388 | private static boolean hasBundleItems(ItemStack stack) {
|
| 389 | BundleContents contents = stack.get(DataComponents.BUNDLE_CONTENTS);
|
| 390 | return contents != null && !contents.isEmpty();
|
| 391 | }
|
| 392 |
|
| 393 | private static boolean isBannerLike(ItemStack stack) {
|
| 394 | if (stack.getItem() instanceof BannerItem || stack.has(DataComponents.PROVIDES_BANNER_PATTERNS)) {
|
| 395 | return true;
|
| 396 | }
|
| 397 | return stack.getItem() == Items.SHIELD
|
| 398 | && !stack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY).layers().isEmpty();
|
| 399 | }
|
| 400 |
|
| 401 | // Null when there is nothing worth previewing.
|
| 402 | private ClientTooltipComponent preview(ItemStack stack) {
|
| 403 | Item item = stack.getItem();
|
| 404 | if (containers.isOn() && hasContents(stack)) {
|
| 405 | return new ContainerPreview(contentsOf(stack), COLUMNS, tintOf(stack));
|
| 406 | }
|
| 407 | if (enderChest.isOn() && item == Items.ENDER_CHEST) {
|
| 408 | return remembered.isEmpty()
|
| 409 | ? text(Component.literal("Unknown inventory.").withStyle(ChatFormatting.DARK_RED))
|
| 410 | : new ContainerPreview(new ArrayList<>(remembered), COLUMNS, ENDER_TINT);
|
| 411 | }
|
| 412 | if (maps.isOn() && item == Items.FILLED_MAP) {
|
| 413 | return mapPreview(stack);
|
| 414 | }
|
| 415 | if (books.isOn() && (item == Items.WRITABLE_BOOK || item == Items.WRITTEN_BOOK)) {
|
| 416 | return bookPreview(stack);
|
| 417 | }
|
| 418 | if (banners.isOn() && isBannerLike(stack)) {
|
| 419 | return bannerPreview(stack);
|
| 420 | }
|
| 421 | if (buckets.isOn() && item instanceof MobBucketItem bucket) {
|
| 422 | return bucketPreview(stack, bucket);
|
| 423 | }
|
| 424 | if (bundles.isOn() && item instanceof BundleItem && hasBundleItems(stack)) {
|
| 425 | return new ContainerPreview(stack.get(DataComponents.BUNDLE_CONTENTS).itemCopyStream().toList(), COLUMNS);
|
| 426 | }
|
| 427 | return null;
|
| 428 | }
|
| 429 |
|
| 430 | // What your ender chest held when you last opened it. Empty until then.
|
| 431 | public List<ItemStack> rememberedEnderChest() {
|
| 432 | return List.copyOf(remembered);
|
| 433 | }
|
| 434 |
|
| 435 | public static List<ItemStack> contentsOf(ItemStack stack) {
|
| 436 | NonNullList<ItemStack> items = NonNullList.withSize(SHULKER_SLOTS, ItemStack.EMPTY);
|
| 437 | stack.get(DataComponents.CONTAINER).copyInto(items);
|
| 438 | return items;
|
| 439 | }
|
| 440 |
|
| 441 | // A shulker box previews on a dark shade of its own dye.
|
| 442 | public static int tintOf(ItemStack stack) {
|
| 443 | if (!(Block.byItem(stack.getItem()) instanceof ShulkerBoxBlock box) || box.getColor() == null) {
|
| 444 | return ColorUtil.withAlpha(0x100010, PREVIEW_ALPHA);
|
| 445 | }
|
| 446 | int dye = box.getColor().getTextureDiffuseColor();
|
| 447 | int shaded = ColorUtil.lerp(0xFF000000, dye | 0xFF000000, TINT_SHADE);
|
| 448 | return ColorUtil.withAlpha(shaded, PREVIEW_ALPHA);
|
| 449 | }
|
| 450 |
|
| 451 | private ClientTooltipComponent mapPreview(ItemStack stack) {
|
| 452 | MapId id = stack.get(DataComponents.MAP_ID);
|
| 453 | if (id == null || mc.level == null) {
|
| 454 | return null;
|
| 455 | }
|
| 456 | MapItemSavedData data = MapItem.getSavedData(id, mc.level);
|
| 457 | if (data == null) {
|
| 458 | return null;
|
| 459 | }
|
| 460 | MapRenderState state = new MapRenderState();
|
| 461 | mc.getMapRenderer().extractRenderState(id, data, state);
|
| 462 | return new MapPreview(state, mapSize.getInt());
|
| 463 | }
|
| 464 |
|
| 465 | private static ClientTooltipComponent bookPreview(ItemStack stack) {
|
| 466 | Component first = null;
|
| 467 | int pages = 0;
|
| 468 | WritableBookContent writable = stack.get(DataComponents.WRITABLE_BOOK_CONTENT);
|
| 469 | WrittenBookContent written = stack.get(DataComponents.WRITTEN_BOOK_CONTENT);
|
| 470 | if (writable != null && !writable.pages().isEmpty()) {
|
| 471 | first = Component.literal(writable.pages().getFirst().get(false));
|
| 472 | pages = writable.pages().size();
|
| 473 | } else if (written != null && !written.pages().isEmpty()) {
|
| 474 | first = written.pages().getFirst().get(false);
|
| 475 | pages = written.pages().size();
|
| 476 | }
|
| 477 | if (first == null) {
|
| 478 | return null;
|
| 479 | }
|
| 480 | String count = pages == 1 ? " (1 page)" : " (" + pages + " pages)";
|
| 481 | return new BookPreview(first.copy().append(Component.literal(count).withStyle(ChatFormatting.GRAY)));
|
| 482 | }
|
| 483 |
|
| 484 | private static ClientTooltipComponent bannerPreview(ItemStack stack) {
|
| 485 | BannerPatternLayers layers = stack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 486 | if (stack.getItem() instanceof BannerItem banner) {
|
| 487 | return new BannerPreview(banner.getColor(), layers);
|
| 488 | }
|
| 489 | HolderSet<BannerPattern> provided = stack.get(DataComponents.PROVIDES_BANNER_PATTERNS);
|
| 490 | if (provided != null) {
|
| 491 | BannerPatternLayers sample = provided.size() == 0 ? BannerPatternLayers.EMPTY
|
| 492 | : new BannerPatternLayers.Builder().add(provided.get(0), DyeColor.WHITE).build();
|
| 493 | return new BannerPreview(DyeColor.GRAY, sample);
|
| 494 | }
|
| 495 | return new BannerPreview(stack.getOrDefault(DataComponents.BASE_COLOR, DyeColor.WHITE), layers);
|
| 496 | }
|
| 497 |
|
| 498 | private ClientTooltipComponent bucketPreview(ItemStack stack, MobBucketItem bucket) {
|
| 499 | CustomData data = stack.get(DataComponents.BUCKET_ENTITY_DATA);
|
| 500 | if (data == null || mc.level == null) {
|
| 501 | return null;
|
| 502 | }
|
| 503 | Entity entity = bucket.type.create(mc.level, EntitySpawnReason.BUCKET);
|
| 504 | if (!(entity instanceof LivingEntity living)) {
|
| 505 | return null;
|
| 506 | }
|
| 507 | living.applyComponentsFromItemStack(stack);
|
| 508 | if (living instanceof Bucketable bucketable) {
|
| 509 | bucketable.loadFromBucketTag(data.copyTag());
|
| 510 | }
|
| 511 | // A fish that thinks it is on land flops about.
|
| 512 | living.wasTouchingWater = true;
|
| 513 | return new EntityPreview(living);
|
| 514 | }
|
| 515 |
|
| 516 | // True for the click or key the player picked to open a window.
|
| 517 | public boolean wantsToOpen(MouseButtonEvent event) {
|
| 518 | return canOpen() && openWith.is(OpenWith.MIDDLE_CLICK) && event.button() == GLFW.GLFW_MOUSE_BUTTON_MIDDLE;
|
| 519 | }
|
| 520 |
|
| 521 | public boolean wantsToOpen(KeyEvent event) {
|
| 522 | return canOpen() && openWith.is(OpenWith.KEY) && openKey.isBound() && event.key() == openKey.getValue();
|
| 523 | }
|
| 524 |
|
| 525 | private boolean canOpen() {
|
| 526 | if (!isEnabled() || !openContents.isOn() || mc.player == null) {
|
| 527 | return false;
|
| 528 | }
|
| 529 | return !pauseInCreative.isOn() || !mc.player.hasInfiniteMaterials();
|
| 530 | }
|
| 531 |
|
| 532 | // Opens a window for the item and says whether one opened.
|
| 533 | public boolean openContents(ItemStack stack) {
|
| 534 | Item item = stack.getItem();
|
| 535 | if (item == Items.WRITABLE_BOOK || item == Items.WRITTEN_BOOK) {
|
| 536 | mc.gui.setScreen(new BookViewScreen(BookViewScreen.BookAccess.fromItem(stack)));
|
| 537 | return true;
|
| 538 | }
|
| 539 | if (item instanceof BundleItem && hasBundleItems(stack)) {
|
| 540 | open(stack, stack.get(DataComponents.BUNDLE_CONTENTS).itemCopyStream().toList(), tintOf(stack));
|
| 541 | return true;
|
| 542 | }
|
| 543 | if (hasContents(stack)) {
|
| 544 | open(stack, contentsOf(stack), tintOf(stack));
|
| 545 | return true;
|
| 546 | }
|
| 547 | if (item == Items.ENDER_CHEST && !remembered.isEmpty()) {
|
| 548 | open(stack, new ArrayList<>(remembered), ENDER_TINT);
|
| 549 | return true;
|
| 550 | }
|
| 551 | return false;
|
| 552 | }
|
| 553 |
|
| 554 | // A chest screen closes for good because the server owns it. Your own
|
| 555 | // inventory and another peek window come back when this one shuts.
|
| 556 | private void open(ItemStack stack, List<ItemStack> items, int tint) {
|
| 557 | Screen current = mc.gui.screen();
|
| 558 | Screen parent = current instanceof InventoryScreen || current instanceof PeekScreen ? current : null;
|
| 559 | if (parent == null && current instanceof AbstractContainerScreen<?> container) {
|
| 560 | container.onClose();
|
| 561 | }
|
| 562 | mc.gui.setScreen(new PeekScreen(parent, stack.getHoverName(), items, tint));
|
| 563 | }
|
| 564 | }
|
| 565 |
|