| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.ChatFormatting;
|
| 10 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 11 | import net.minecraft.client.input.MouseButtonEvent;
|
| 12 | import net.minecraft.client.model.geom.ModelLayers;
|
| 13 | import net.minecraft.client.model.object.book.BookModel;
|
| 14 | import net.minecraft.client.renderer.RenderPipelines;
|
| 15 | import net.minecraft.core.Holder.Reference;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.chat.FormattedText;
|
| 20 | import net.minecraft.network.chat.MutableComponent;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.util.ARGB;
|
| 23 | import net.minecraft.util.Mth;
|
| 24 | import net.minecraft.util.RandomSource;
|
| 25 | import net.minecraft.world.entity.player.Inventory;
|
| 26 | import net.minecraft.world.inventory.EnchantmentMenu;
|
| 27 | import net.minecraft.world.item.ItemStack;
|
| 28 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class EnchantmentScreen extends AbstractContainerScreen<EnchantmentMenu> {
|
| 32 | private static final Identifier[] ENABLED_LEVEL_SPRITES = new Identifier[]{
|
| 33 | Identifier.withDefaultNamespace("container/enchanting_table/level_1"),
|
| 34 | Identifier.withDefaultNamespace("container/enchanting_table/level_2"),
|
| 35 | Identifier.withDefaultNamespace("container/enchanting_table/level_3")
|
| 36 | };
|
| 37 | private static final Identifier[] DISABLED_LEVEL_SPRITES = new Identifier[]{
|
| 38 | Identifier.withDefaultNamespace("container/enchanting_table/level_1_disabled"),
|
| 39 | Identifier.withDefaultNamespace("container/enchanting_table/level_2_disabled"),
|
| 40 | Identifier.withDefaultNamespace("container/enchanting_table/level_3_disabled")
|
| 41 | };
|
| 42 | private static final Identifier ENCHANTMENT_SLOT_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/enchanting_table/enchantment_slot_disabled");
|
| 43 | private static final Identifier ENCHANTMENT_SLOT_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace(
|
| 44 | "container/enchanting_table/enchantment_slot_highlighted"
|
| 45 | );
|
| 46 | private static final Identifier ENCHANTMENT_SLOT_SPRITE = Identifier.withDefaultNamespace("container/enchanting_table/enchantment_slot");
|
| 47 | private static final Identifier ENCHANTING_TABLE_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/enchanting_table.png");
|
| 48 | private static final Identifier ENCHANTING_BOOK_LOCATION = Identifier.withDefaultNamespace("textures/entity/enchantment/enchanting_table_book.png");
|
| 49 | private final RandomSource random = RandomSource.create();
|
| 50 | private BookModel bookModel;
|
| 51 | public float flip;
|
| 52 | public float oFlip;
|
| 53 | public float flipT;
|
| 54 | public float flipA;
|
| 55 | public float open;
|
| 56 | public float oOpen;
|
| 57 | private ItemStack last = ItemStack.EMPTY;
|
| 58 |
|
| 59 | public EnchantmentScreen(final EnchantmentMenu menu, final Inventory inventory, final Component title) {
|
| 60 | super(menu, inventory, title);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected void init() {
|
| 65 | super.init();
|
| 66 | this.bookModel = new BookModel(this.minecraft.getEntityModels().bakeLayer(ModelLayers.BOOK));
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void containerTick() {
|
| 71 | super.containerTick();
|
| 72 | this.minecraft.player.experienceDisplayStartTick = this.minecraft.player.tickCount;
|
| 73 | this.tickBook();
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 78 | int xo = (this.width - this.imageWidth) / 2;
|
| 79 | int yo = (this.height - this.imageHeight) / 2;
|
| 80 |
|
| 81 | for (int i = 0; i < 3; i++) {
|
| 82 | double xx = event.x() - (xo + 60);
|
| 83 | double yy = event.y() - (yo + 14 + 19 * i);
|
| 84 | if (xx >= 0.0 && yy >= 0.0 && xx < 108.0 && yy < 19.0 && this.menu.clickMenuButton(this.minecraft.player, i)) {
|
| 85 | this.minecraft.gameMode.handleInventoryButtonClick(this.menu.containerId, i);
|
| 86 | return true;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | return super.mouseClicked(event, doubleClick);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 95 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 96 | int xo = (this.width - this.imageWidth) / 2;
|
| 97 | int yo = (this.height - this.imageHeight) / 2;
|
| 98 | graphics.blit(RenderPipelines.GUI_TEXTURED, ENCHANTING_TABLE_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 99 | this.extractBook(graphics, xo, yo);
|
| 100 | EnchantmentNames.getInstance().initSeed(this.menu.getEnchantmentSeed());
|
| 101 | int goldCount = this.menu.getGoldCount();
|
| 102 |
|
| 103 | for (int i = 0; i < 3; i++) {
|
| 104 | int leftPos = xo + 60;
|
| 105 | int leftPosText = leftPos + 20;
|
| 106 | int cost = this.menu.costs[i];
|
| 107 | if (cost == 0) {
|
| 108 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ENCHANTMENT_SLOT_DISABLED_SPRITE, leftPos, yo + 14 + 19 * i, 108, 19);
|
| 109 | } else {
|
| 110 | String costText = cost + "";
|
| 111 | int textWidth = 86 - this.font.width(costText);
|
| 112 | FormattedText message = EnchantmentNames.getInstance().getRandomName(this.font, textWidth);
|
| 113 | int col = -9937334;
|
| 114 | if ((goldCount < i + 1 || this.minecraft.player.experienceLevel < cost) && !this.minecraft.player.hasInfiniteMaterials()) {
|
| 115 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ENCHANTMENT_SLOT_DISABLED_SPRITE, leftPos, yo + 14 + 19 * i, 108, 19);
|
| 116 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DISABLED_LEVEL_SPRITES[i], leftPos + 1, yo + 15 + 19 * i, 16, 16);
|
| 117 | graphics.textWithWordWrap(this.font, message, leftPosText, yo + 16 + 19 * i, textWidth, ARGB.opaque((col & 16711422) >> 1), false);
|
| 118 | col = -12550384;
|
| 119 | } else {
|
| 120 | int xx = mouseX - (xo + 60);
|
| 121 | int yy = mouseY - (yo + 14 + 19 * i);
|
| 122 | if (xx >= 0 && yy >= 0 && xx < 108 && yy < 19) {
|
| 123 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ENCHANTMENT_SLOT_HIGHLIGHTED_SPRITE, leftPos, yo + 14 + 19 * i, 108, 19);
|
| 124 | graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 125 | col = -128;
|
| 126 | } else {
|
| 127 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ENCHANTMENT_SLOT_SPRITE, leftPos, yo + 14 + 19 * i, 108, 19);
|
| 128 | }
|
| 129 |
|
| 130 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ENABLED_LEVEL_SPRITES[i], leftPos + 1, yo + 15 + 19 * i, 16, 16);
|
| 131 | graphics.textWithWordWrap(this.font, message, leftPosText, yo + 16 + 19 * i, textWidth, col, false);
|
| 132 | col = -8323296;
|
| 133 | }
|
| 134 |
|
| 135 | graphics.text(this.font, costText, leftPosText + 86 - this.font.width(costText), yo + 16 + 19 * i + 7, col);
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | private void extractBook(final GuiGraphicsExtractor graphics, final int left, final int top) {
|
| 141 | float a = this.minecraft.getDeltaTracker().getGameTimeDeltaPartialTick(false);
|
| 142 | float open = Mth.lerp(a, this.oOpen, this.open);
|
| 143 | float flip = Mth.lerp(a, this.oFlip, this.flip);
|
| 144 | int x0 = left + 14;
|
| 145 | int y0 = top + 14;
|
| 146 | int x1 = x0 + 38;
|
| 147 | int y1 = y0 + 31;
|
| 148 | graphics.book(this.bookModel, ENCHANTING_BOOK_LOCATION, 40.0F, open, flip, x0, y0, x1, y1);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float ignored) {
|
| 153 | float a = this.minecraft.getDeltaTracker().getGameTimeDeltaPartialTick(false);
|
| 154 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 155 | boolean infiniteMaterials = this.minecraft.player.hasInfiniteMaterials();
|
| 156 | int gold = this.menu.getGoldCount();
|
| 157 |
|
| 158 | for (int i = 0; i < 3; i++) {
|
| 159 | int minLevel = this.menu.costs[i];
|
| 160 | Optional<Reference<Enchantment>> enchant = this.minecraft.level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).get(this.menu.enchantClue[i]);
|
| 161 | if (!enchant.isEmpty()) {
|
| 162 | int enchantLevel = this.menu.levelClue[i];
|
| 163 | int cost = i + 1;
|
| 164 | if (this.isHovering(60, 14 + 19 * i, 108, 17, mouseX, mouseY) && minLevel > 0 && enchantLevel >= 0) {
|
| 165 | List<Component> texts = Lists.newArrayList();
|
| 166 | texts.add(Component.translatable("container.enchant.clue", Enchantment.getFullname(enchant.get(), enchantLevel)).withStyle(ChatFormatting.WHITE));
|
| 167 | if (!infiniteMaterials) {
|
| 168 | texts.add(CommonComponents.EMPTY);
|
| 169 | if (this.minecraft.player.experienceLevel < minLevel) {
|
| 170 | texts.add(Component.translatable("container.enchant.level.requirement", this.menu.costs[i]).withStyle(ChatFormatting.RED));
|
| 171 | } else {
|
| 172 | MutableComponent lapisCost;
|
| 173 | if (cost == 1) {
|
| 174 | lapisCost = Component.translatable("container.enchant.lapis.one");
|
| 175 | } else {
|
| 176 | lapisCost = Component.translatable("container.enchant.lapis.many", cost);
|
| 177 | }
|
| 178 |
|
| 179 | texts.add(lapisCost.withStyle(gold >= cost ? ChatFormatting.GRAY : ChatFormatting.RED));
|
| 180 | MutableComponent levelCost;
|
| 181 | if (cost == 1) {
|
| 182 | levelCost = Component.translatable("container.enchant.level.one");
|
| 183 | } else {
|
| 184 | levelCost = Component.translatable("container.enchant.level.many", cost);
|
| 185 | }
|
| 186 |
|
| 187 | texts.add(levelCost.withStyle(ChatFormatting.GRAY));
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | graphics.setComponentTooltipForNextFrame(this.font, texts, mouseX, mouseY);
|
| 192 | break;
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | public void tickBook() {
|
| 199 | ItemStack current = this.menu.getSlot(0).getItem();
|
| 200 | if (!ItemStack.matches(current, this.last)) {
|
| 201 | this.last = current;
|
| 202 |
|
| 203 | do {
|
| 204 | this.flipT = this.flipT + (this.random.nextInt(4) - this.random.nextInt(4));
|
| 205 | } while (this.flip <= this.flipT + 1.0F && this.flip >= this.flipT - 1.0F);
|
| 206 | }
|
| 207 |
|
| 208 | this.oFlip = this.flip;
|
| 209 | this.oOpen = this.open;
|
| 210 | boolean shouldBeOpen = false;
|
| 211 |
|
| 212 | for (int i = 0; i < 3; i++) {
|
| 213 | if (this.menu.costs[i] != 0) {
|
| 214 | shouldBeOpen = true;
|
| 215 | break;
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | if (shouldBeOpen) {
|
| 220 | this.open += 0.2F;
|
| 221 | } else {
|
| 222 | this.open -= 0.2F;
|
| 223 | }
|
| 224 |
|
| 225 | this.open = Mth.clamp(this.open, 0.0F, 1.0F);
|
| 226 | float diff = (this.flipT - this.flip) * 0.4F;
|
| 227 | float max = 0.2F;
|
| 228 | diff = Mth.clamp(diff, -0.2F, 0.2F);
|
| 229 | this.flipA = this.flipA + (diff - this.flipA) * 0.9F;
|
| 230 | this.flip = this.flip + this.flipA;
|
| 231 | }
|
| 232 | }
|
| 233 |
|