| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 6 | import net.minecraft.client.renderer.RenderPipelines;
|
| 7 | import net.minecraft.client.renderer.state.MapRenderState;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.tags.ItemTags;
|
| 12 | import net.minecraft.world.entity.player.Inventory;
|
| 13 | import net.minecraft.world.inventory.CartographyTableMenu;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.Items;
|
| 16 | import net.minecraft.world.item.MapItem;
|
| 17 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 18 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class CartographyTableScreen extends AbstractContainerScreen<CartographyTableMenu> {
|
| 23 | private static final Identifier ERROR_SPRITE = Identifier.withDefaultNamespace("container/cartography_table/error");
|
| 24 | private static final Identifier SCALED_MAP_SPRITE = Identifier.withDefaultNamespace("container/cartography_table/scaled_map");
|
| 25 | private static final Identifier DUPLICATED_MAP_SPRITE = Identifier.withDefaultNamespace("container/cartography_table/duplicated_map");
|
| 26 | private static final Identifier MAP_SPRITE = Identifier.withDefaultNamespace("container/cartography_table/map");
|
| 27 | private static final Identifier LOCKED_SPRITE = Identifier.withDefaultNamespace("container/cartography_table/locked");
|
| 28 | private static final Identifier BG_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/cartography_table.png");
|
| 29 | private final MapRenderState mapRenderState = new MapRenderState();
|
| 30 |
|
| 31 | public CartographyTableScreen(final CartographyTableMenu menu, final Inventory inventory, final Component title) {
|
| 32 | super(menu, inventory, title);
|
| 33 | this.titleLabelY -= 2;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 38 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 39 | int xo = this.leftPos;
|
| 40 | int yo = this.topPos;
|
| 41 | graphics.blit(RenderPipelines.GUI_TEXTURED, BG_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 42 | ItemStack additionalItem = this.menu.getSlot(1).getItem();
|
| 43 | boolean isDuplication = additionalItem.is(Items.MAP);
|
| 44 | boolean isScaling = additionalItem.is(Items.PAPER);
|
| 45 | boolean isLocking = additionalItem.is(Items.GLASS_PANE);
|
| 46 | ItemStack map = this.menu.getSlot(0).getItem();
|
| 47 | MapId mapId = map.get(DataComponents.MAP_ID);
|
| 48 | boolean locked = false;
|
| 49 | MapItemSavedData mapData;
|
| 50 | if (mapId != null) {
|
| 51 | mapData = MapItem.getSavedData(mapId, this.minecraft.level);
|
| 52 | if (mapData != null) {
|
| 53 | if (mapData.locked) {
|
| 54 | locked = true;
|
| 55 | if (isScaling || isLocking) {
|
| 56 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ERROR_SPRITE, xo + 35, yo + 31, 28, 21);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | if (isScaling && (!map.is(ItemTags.EXTENDABLE_MAPS) || mapData.scale >= 4)) {
|
| 61 | locked = true;
|
| 62 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ERROR_SPRITE, xo + 35, yo + 31, 28, 21);
|
| 63 | }
|
| 64 | }
|
| 65 | } else {
|
| 66 | mapData = null;
|
| 67 | }
|
| 68 |
|
| 69 | this.extractResultingMap(graphics, mapId, mapData, isDuplication, isScaling, isLocking, locked);
|
| 70 | }
|
| 71 |
|
| 72 | private void extractResultingMap(
|
| 73 | final GuiGraphicsExtractor graphics,
|
| 74 | final @Nullable MapId id,
|
| 75 | final @Nullable MapItemSavedData data,
|
| 76 | final boolean isDuplication,
|
| 77 | final boolean isScaling,
|
| 78 | final boolean isLocking,
|
| 79 | final boolean locked
|
| 80 | ) {
|
| 81 | int xo = this.leftPos;
|
| 82 | int yo = this.topPos;
|
| 83 | if (isScaling && !locked) {
|
| 84 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SCALED_MAP_SPRITE, xo + 67, yo + 13, 66, 66);
|
| 85 | this.extractMap(graphics, id, data, xo + 85, yo + 31, 0.226F);
|
| 86 | } else if (isDuplication) {
|
| 87 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DUPLICATED_MAP_SPRITE, xo + 67 + 16, yo + 13, 50, 66);
|
| 88 | this.extractMap(graphics, id, data, xo + 86, yo + 16, 0.34F);
|
| 89 | graphics.nextStratum();
|
| 90 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DUPLICATED_MAP_SPRITE, xo + 67, yo + 13 + 16, 50, 66);
|
| 91 | this.extractMap(graphics, id, data, xo + 70, yo + 32, 0.34F);
|
| 92 | } else if (isLocking) {
|
| 93 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, MAP_SPRITE, xo + 67, yo + 13, 66, 66);
|
| 94 | this.extractMap(graphics, id, data, xo + 71, yo + 17, 0.45F);
|
| 95 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, LOCKED_SPRITE, xo + 118, yo + 60, 10, 14);
|
| 96 | } else {
|
| 97 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, MAP_SPRITE, xo + 67, yo + 13, 66, 66);
|
| 98 | this.extractMap(graphics, id, data, xo + 71, yo + 17, 0.45F);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private void extractMap(
|
| 103 | final GuiGraphicsExtractor graphics, final @Nullable MapId id, final @Nullable MapItemSavedData data, final int x, final int y, final float scale
|
| 104 | ) {
|
| 105 | if (id != null && data != null) {
|
| 106 | graphics.pose().pushMatrix();
|
| 107 | graphics.pose().translate(x, y);
|
| 108 | graphics.pose().scale(scale, scale);
|
| 109 | this.minecraft.getMapRenderer().extractRenderState(id, data, this.mapRenderState);
|
| 110 | graphics.map(this.mapRenderState);
|
| 111 | graphics.pose().popMatrix();
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|