| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectSet;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Locale;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.ClientRecipeBook;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 15 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 16 | import net.minecraft.client.gui.components.Button;
|
| 17 | import net.minecraft.client.gui.components.CycleButton;
|
| 18 | import net.minecraft.client.gui.components.EditBox;
|
| 19 | import net.minecraft.client.gui.components.Renderable;
|
| 20 | import net.minecraft.client.gui.components.Tooltip;
|
| 21 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 22 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 23 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 24 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 25 | import net.minecraft.client.gui.navigation.ScreenAxis;
|
| 26 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 27 | import net.minecraft.client.gui.screens.Screen;
|
| 28 | import net.minecraft.client.input.CharacterEvent;
|
| 29 | import net.minecraft.client.input.KeyEvent;
|
| 30 | import net.minecraft.client.input.MouseButtonEvent;
|
| 31 | import net.minecraft.client.input.PreeditEvent;
|
| 32 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 33 | import net.minecraft.client.renderer.RenderPipelines;
|
| 34 | import net.minecraft.client.resources.language.LanguageInfo;
|
| 35 | import net.minecraft.client.resources.language.LanguageManager;
|
| 36 | import net.minecraft.network.chat.CommonComponents;
|
| 37 | import net.minecraft.network.chat.Component;
|
| 38 | import net.minecraft.network.protocol.game.ServerboundRecipeBookChangeSettingsPacket;
|
| 39 | import net.minecraft.resources.Identifier;
|
| 40 | import net.minecraft.util.Mth;
|
| 41 | import net.minecraft.util.context.ContextMap;
|
| 42 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 43 | import net.minecraft.world.inventory.AbstractFurnaceMenu;
|
| 44 | import net.minecraft.world.inventory.RecipeBookMenu;
|
| 45 | import net.minecraft.world.inventory.RecipeBookType;
|
| 46 | import net.minecraft.world.inventory.Slot;
|
| 47 | import net.minecraft.world.item.Item;
|
| 48 | import net.minecraft.world.item.ItemStack;
|
| 49 | import net.minecraft.world.item.Items;
|
| 50 | import net.minecraft.world.item.crafting.ExtendedRecipeBookCategory;
|
| 51 | import net.minecraft.world.item.crafting.RecipeBookCategory;
|
| 52 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 53 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 54 | import net.minecraft.world.item.crafting.display.SlotDisplayContext;
|
| 55 | import org.jspecify.annotations.Nullable;
|
| 56 |
|
| 57 | @Environment(EnvType.CLIENT)
|
| 58 | public abstract class RecipeBookComponent<T extends RecipeBookMenu> implements GuiEventListener, Renderable, NarratableEntry {
|
| 59 | public static final WidgetSprites RECIPE_BUTTON_SPRITES = new WidgetSprites(
|
| 60 | Identifier.withDefaultNamespace("recipe_book/button"), Identifier.withDefaultNamespace("recipe_book/button_highlighted")
|
| 61 | );
|
| 62 | protected static final Identifier RECIPE_BOOK_LOCATION = Identifier.withDefaultNamespace("textures/gui/recipe_book.png");
|
| 63 | private static final int BACKGROUND_TEXTURE_WIDTH = 256;
|
| 64 | private static final int BACKGROUND_TEXTURE_HEIGHT = 256;
|
| 65 | private static final Component SEARCH_HINT = Component.translatable("gui.recipebook.search_hint").withStyle(EditBox.SEARCH_HINT_STYLE);
|
| 66 | public static final int IMAGE_WIDTH = 147;
|
| 67 | public static final int IMAGE_HEIGHT = 166;
|
| 68 | private static final int OFFSET_X_POSITION = 86;
|
| 69 | private static final int BORDER_WIDTH = 8;
|
| 70 | private static final Component ALL_RECIPES_TOOLTIP = Component.translatable("gui.recipebook.toggleRecipes.all");
|
| 71 | private static final int TICKS_TO_SWAP_SLOT = 30;
|
| 72 | private int xOffset;
|
| 73 | private int width;
|
| 74 | private int height;
|
| 75 | private float time;
|
| 76 | private @Nullable RecipeDisplayId lastPlacedRecipe;
|
| 77 | private final GhostSlots ghostSlots;
|
| 78 | private final List<RecipeBookTabButton> tabButtons = Lists.newArrayList();
|
| 79 | private @Nullable RecipeBookTabButton selectedTab;
|
| 80 | protected CycleButton<Boolean> filterButton;
|
| 81 | protected final T menu;
|
| 82 | protected Minecraft minecraft;
|
| 83 | private @Nullable EditBox searchBox;
|
| 84 | private String lastSearch = "";
|
| 85 | private final List<RecipeBookComponent.TabInfo> tabInfos;
|
| 86 | private ClientRecipeBook book;
|
| 87 | private final RecipeBookPage recipeBookPage;
|
| 88 | private @Nullable RecipeDisplayId lastRecipe;
|
| 89 | private @Nullable RecipeCollection lastRecipeCollection;
|
| 90 | private final StackedItemContents stackedContents = new StackedItemContents();
|
| 91 | private int timesInventoryChanged;
|
| 92 | private boolean ignoreTextInput;
|
| 93 | private boolean visible;
|
| 94 | private boolean widthTooNarrow;
|
| 95 | private @Nullable ScreenRectangle magnifierIconPlacement;
|
| 96 |
|
| 97 | public RecipeBookComponent(final T menu, final List<RecipeBookComponent.TabInfo> tabInfos) {
|
| 98 | this.menu = menu;
|
| 99 | this.tabInfos = tabInfos;
|
| 100 | SlotSelectTime slotSelectTime = () -> Mth.floor(this.time / 30.0F);
|
| 101 | this.ghostSlots = new GhostSlots(slotSelectTime);
|
| 102 | this.recipeBookPage = new RecipeBookPage(this, slotSelectTime, menu instanceof AbstractFurnaceMenu);
|
| 103 | }
|
| 104 |
|
| 105 | public void init(final int width, final int height, final Minecraft minecraft, final boolean widthTooNarrow) {
|
| 106 | this.minecraft = minecraft;
|
| 107 | this.width = width;
|
| 108 | this.height = height;
|
| 109 | this.widthTooNarrow = widthTooNarrow;
|
| 110 | this.book = minecraft.player.getRecipeBook();
|
| 111 | this.timesInventoryChanged = minecraft.player.getInventory().getTimesChanged();
|
| 112 | this.visible = this.isVisibleAccordingToBookData();
|
| 113 | if (this.visible) {
|
| 114 | this.initVisuals();
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private void initVisuals() {
|
| 119 | boolean isFiltering = this.isFiltering();
|
| 120 | this.xOffset = this.widthTooNarrow ? 0 : 86;
|
| 121 | int xo = this.getXOrigin();
|
| 122 | int yo = this.getYOrigin();
|
| 123 | this.stackedContents.clear();
|
| 124 | this.minecraft.player.getInventory().fillStackedContents(this.stackedContents);
|
| 125 | this.menu.fillCraftSlotsStackedContents(this.stackedContents);
|
| 126 | String oldEdit = this.searchBox != null ? this.searchBox.getValue() : "";
|
| 127 | this.searchBox = new EditBox(this.minecraft.font, xo + 25, yo + 13, 81, 9 + 5, Component.translatable("itemGroup.search"));
|
| 128 | this.searchBox.setMaxLength(50);
|
| 129 | this.searchBox.setVisible(true);
|
| 130 | this.searchBox.setTextColor(-1);
|
| 131 | this.searchBox.setValue(oldEdit);
|
| 132 | this.searchBox.setHint(SEARCH_HINT);
|
| 133 | this.magnifierIconPlacement = ScreenRectangle.of(
|
| 134 | ScreenAxis.HORIZONTAL, xo + 8, this.searchBox.getY(), this.searchBox.getX() - this.getXOrigin(), this.searchBox.getHeight()
|
| 135 | );
|
| 136 | this.recipeBookPage.init(this.minecraft, xo, yo);
|
| 137 | this.filterButton = CycleButton.booleanBuilder(this.getRecipeFilterName(), ALL_RECIPES_TOOLTIP, isFiltering)
|
| 138 | .withTooltip(filtering -> filtering ? Tooltip.create(this.getRecipeFilterName()) : Tooltip.create(ALL_RECIPES_TOOLTIP))
|
| 139 | .withSprite((cycleButton, filtering) -> this.getFilterButtonTextures().get(filtering, cycleButton.isHoveredOrFocused()))
|
| 140 | .displayState(CycleButton.DisplayState.HIDE)
|
| 141 | .create(xo + 110, yo + 12, 26, 16, CommonComponents.EMPTY, (button, value) -> {
|
| 142 | this.toggleFiltering();
|
| 143 | this.sendUpdateSettings();
|
| 144 | this.updateCollections(false, value);
|
| 145 | });
|
| 146 | this.tabButtons.clear();
|
| 147 |
|
| 148 | for (RecipeBookComponent.TabInfo tabInfo : this.tabInfos) {
|
| 149 | this.tabButtons.add(new RecipeBookTabButton(0, 0, tabInfo, this::onTabButtonPress));
|
| 150 | }
|
| 151 |
|
| 152 | if (this.selectedTab != null) {
|
| 153 | this.selectedTab = this.tabButtons.stream().filter(o -> o.getCategory().equals(this.selectedTab.getCategory())).findFirst().orElse(null);
|
| 154 | }
|
| 155 |
|
| 156 | if (this.selectedTab == null) {
|
| 157 | this.selectedTab = this.tabButtons.get(0);
|
| 158 | }
|
| 159 |
|
| 160 | this.selectedTab.select();
|
| 161 | this.selectMatchingRecipes();
|
| 162 | this.updateTabs(isFiltering);
|
| 163 | this.updateCollections(false, isFiltering);
|
| 164 | }
|
| 165 |
|
| 166 | private int getYOrigin() {
|
| 167 | return (this.height - 166) / 2;
|
| 168 | }
|
| 169 |
|
| 170 | private int getXOrigin() {
|
| 171 | return (this.width - 147) / 2 - this.xOffset;
|
| 172 | }
|
| 173 |
|
| 174 | protected abstract WidgetSprites getFilterButtonTextures();
|
| 175 |
|
| 176 | public int updateScreenPosition(final int width, final int imageWidth) {
|
| 177 | int leftPos;
|
| 178 | if (this.isVisible() && !this.widthTooNarrow) {
|
| 179 | leftPos = 177 + (width - imageWidth - 200) / 2;
|
| 180 | } else {
|
| 181 | leftPos = (width - imageWidth) / 2;
|
| 182 | }
|
| 183 |
|
| 184 | return leftPos;
|
| 185 | }
|
| 186 |
|
| 187 | public void toggleVisibility() {
|
| 188 | this.setVisible(!this.isVisible());
|
| 189 | }
|
| 190 |
|
| 191 | public boolean isVisible() {
|
| 192 | return this.visible;
|
| 193 | }
|
| 194 |
|
| 195 | private boolean isVisibleAccordingToBookData() {
|
| 196 | return this.book.isOpen(this.menu.getRecipeBookType());
|
| 197 | }
|
| 198 |
|
| 199 | protected void setVisible(final boolean visible) {
|
| 200 | if (visible) {
|
| 201 | this.initVisuals();
|
| 202 | }
|
| 203 |
|
| 204 | this.visible = visible;
|
| 205 | this.book.setOpen(this.menu.getRecipeBookType(), visible);
|
| 206 | if (!visible) {
|
| 207 | this.recipeBookPage.setInvisible();
|
| 208 | }
|
| 209 |
|
| 210 | this.sendUpdateSettings();
|
| 211 | }
|
| 212 |
|
| 213 | protected abstract boolean isCraftingSlot(Slot slot);
|
| 214 |
|
| 215 | public void slotClicked(final @Nullable Slot slot) {
|
| 216 | if (slot != null && this.isCraftingSlot(slot)) {
|
| 217 | this.lastPlacedRecipe = null;
|
| 218 | this.ghostSlots.clear();
|
| 219 | if (this.isVisible()) {
|
| 220 | this.updateStackedContents();
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | private void selectMatchingRecipes() {
|
| 226 | for (RecipeBookComponent.TabInfo tabInfo : this.tabInfos) {
|
| 227 | for (RecipeCollection recipeCollection : this.book.getCollection(tabInfo.category())) {
|
| 228 | this.selectMatchingRecipes(recipeCollection, this.stackedContents);
|
| 229 | }
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | protected abstract void selectMatchingRecipes(RecipeCollection collection, StackedItemContents stackedContents);
|
| 234 |
|
| 235 | private void updateCollections(final boolean resetPage, final boolean isFiltering) {
|
| 236 | List<RecipeCollection> tabCollection = this.book.getCollection(this.selectedTab.getCategory());
|
| 237 | List<RecipeCollection> collection = Lists.newArrayList(tabCollection);
|
| 238 | collection.removeIf(c -> !c.hasAnySelected());
|
| 239 | String searchTarget = this.searchBox.getValue();
|
| 240 | if (!searchTarget.isEmpty()) {
|
| 241 | ClientPacketListener connection = this.minecraft.getConnection();
|
| 242 | if (connection != null) {
|
| 243 | ObjectSet<RecipeCollection> set = new ObjectLinkedOpenHashSet<>(connection.searchTrees().recipes().search(searchTarget.toLowerCase(Locale.ROOT)));
|
| 244 | collection.removeIf(e -> !set.contains(e));
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | if (isFiltering) {
|
| 249 | collection.removeIf(c -> !c.hasCraftable());
|
| 250 | }
|
| 251 |
|
| 252 | this.recipeBookPage.updateCollections(collection, resetPage, isFiltering);
|
| 253 | }
|
| 254 |
|
| 255 | private void updateTabs(final boolean isFiltering) {
|
| 256 | int xPosTab = (this.width - 147) / 2 - this.xOffset - 30;
|
| 257 | int yPosTab = (this.height - 166) / 2 + 3;
|
| 258 | int yOffset = 27;
|
| 259 | int index = 0;
|
| 260 |
|
| 261 | for (RecipeBookTabButton tabButton : this.tabButtons) {
|
| 262 | ExtendedRecipeBookCategory category = tabButton.getCategory();
|
| 263 | if (category instanceof SearchRecipeBookCategory) {
|
| 264 | tabButton.visible = true;
|
| 265 | tabButton.setPosition(xPosTab, yPosTab + 27 * index++);
|
| 266 | } else if (tabButton.updateVisibility(this.book)) {
|
| 267 | tabButton.setPosition(xPosTab, yPosTab + 27 * index++);
|
| 268 | tabButton.startAnimation(this.book, isFiltering);
|
| 269 | }
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | public void tick() {
|
| 274 | boolean shouldBeVisible = this.isVisibleAccordingToBookData();
|
| 275 | if (this.isVisible() != shouldBeVisible) {
|
| 276 | this.setVisible(shouldBeVisible);
|
| 277 | }
|
| 278 |
|
| 279 | if (this.isVisible()) {
|
| 280 | if (this.timesInventoryChanged != this.minecraft.player.getInventory().getTimesChanged()) {
|
| 281 | this.updateStackedContents();
|
| 282 | this.timesInventoryChanged = this.minecraft.player.getInventory().getTimesChanged();
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | private void updateStackedContents() {
|
| 288 | this.stackedContents.clear();
|
| 289 | this.minecraft.player.getInventory().fillStackedContents(this.stackedContents);
|
| 290 | this.menu.fillCraftSlotsStackedContents(this.stackedContents);
|
| 291 | this.selectMatchingRecipes();
|
| 292 | this.updateCollections(false, this.isFiltering());
|
| 293 | }
|
| 294 |
|
| 295 | private boolean isFiltering() {
|
| 296 | return this.book.isFiltering(this.menu.getRecipeBookType());
|
| 297 | }
|
| 298 |
|
| 299 | @Override
|
| 300 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 301 | if (this.isVisible()) {
|
| 302 | if (!this.minecraft.hasControlDown()) {
|
| 303 | this.time += a;
|
| 304 | }
|
| 305 |
|
| 306 | int xo = this.getXOrigin();
|
| 307 | int yo = this.getYOrigin();
|
| 308 | graphics.blit(RenderPipelines.GUI_TEXTURED, RECIPE_BOOK_LOCATION, xo, yo, 1.0F, 1.0F, 147, 166, 256, 256);
|
| 309 | this.searchBox.extractRenderState(graphics, mouseX, mouseY, a);
|
| 310 |
|
| 311 | for (RecipeBookTabButton tabButton : this.tabButtons) {
|
| 312 | tabButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 313 | }
|
| 314 |
|
| 315 | this.filterButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 316 | this.recipeBookPage.extractRenderState(graphics, xo, yo, mouseX, mouseY, a);
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | public void extractTooltip(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final @Nullable Slot hoveredSlot) {
|
| 321 | if (this.isVisible()) {
|
| 322 | this.recipeBookPage.extractTooltip(graphics, mouseX, mouseY);
|
| 323 | this.ghostSlots.extractTooltip(graphics, this.minecraft, mouseX, mouseY, hoveredSlot);
|
| 324 | }
|
| 325 | }
|
| 326 |
|
| 327 | protected abstract Component getRecipeFilterName();
|
| 328 |
|
| 329 | public void extractGhostRecipe(final GuiGraphicsExtractor graphics, final boolean isResultSlotBig) {
|
| 330 | this.ghostSlots.extractRenderState(graphics, this.minecraft, isResultSlotBig);
|
| 331 | }
|
| 332 |
|
| 333 | @Override
|
| 334 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 335 | if (this.isVisible() && !this.minecraft.player.isSpectator()) {
|
| 336 | if (this.recipeBookPage.mouseClicked(event, this.getXOrigin(), this.getYOrigin(), 147, 166, doubleClick)) {
|
| 337 | RecipeDisplayId recipe = this.recipeBookPage.getLastClickedRecipe();
|
| 338 | RecipeCollection recipeCollection = this.recipeBookPage.getLastClickedRecipeCollection();
|
| 339 | if (recipe != null && recipeCollection != null) {
|
| 340 | if (!this.tryPlaceRecipe(recipeCollection, recipe, event.hasShiftDown())) {
|
| 341 | return false;
|
| 342 | }
|
| 343 |
|
| 344 | this.lastRecipeCollection = recipeCollection;
|
| 345 | this.lastRecipe = recipe;
|
| 346 | if (!this.isOffsetNextToMainGUI()) {
|
| 347 | this.setVisible(false);
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | return true;
|
| 352 | } else {
|
| 353 | if (this.searchBox != null) {
|
| 354 | boolean clickedMagnifierIcon = this.magnifierIconPlacement != null
|
| 355 | && this.magnifierIconPlacement.containsPoint(Mth.floor(event.x()), Mth.floor(event.y()));
|
| 356 | if (clickedMagnifierIcon || this.searchBox.mouseClicked(event, doubleClick)) {
|
| 357 | this.searchBox.setFocused(true);
|
| 358 | return true;
|
| 359 | }
|
| 360 |
|
| 361 | this.searchBox.setFocused(false);
|
| 362 | }
|
| 363 |
|
| 364 | if (this.filterButton.mouseClicked(event, doubleClick)) {
|
| 365 | return true;
|
| 366 | }
|
| 367 |
|
| 368 | for (RecipeBookTabButton tabButton : this.tabButtons) {
|
| 369 | if (tabButton.mouseClicked(event, doubleClick)) {
|
| 370 | return true;
|
| 371 | }
|
| 372 | }
|
| 373 |
|
| 374 | return false;
|
| 375 | }
|
| 376 | } else {
|
| 377 | return false;
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | @Override
|
| 382 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 383 | return this.searchBox != null && this.searchBox.isFocused() ? this.searchBox.mouseDragged(event, dx, dy) : false;
|
| 384 | }
|
| 385 |
|
| 386 | private boolean tryPlaceRecipe(final RecipeCollection recipeCollection, final RecipeDisplayId recipe, final boolean useMaxItems) {
|
| 387 | if (!recipeCollection.isCraftable(recipe) && recipe.equals(this.lastPlacedRecipe)) {
|
| 388 | return false;
|
| 389 | }
|
| 390 |
|
| 391 | this.lastPlacedRecipe = recipe;
|
| 392 | this.ghostSlots.clear();
|
| 393 | this.minecraft.gameMode.handlePlaceRecipe(this.minecraft.player.containerMenu.containerId, recipe, useMaxItems);
|
| 394 | return true;
|
| 395 | }
|
| 396 |
|
| 397 | private void onTabButtonPress(final Button button) {
|
| 398 | if (this.selectedTab != button && button instanceof RecipeBookTabButton recipeBookTabButton) {
|
| 399 | this.replaceSelected(recipeBookTabButton);
|
| 400 | this.updateCollections(true, this.isFiltering());
|
| 401 | }
|
| 402 | }
|
| 403 |
|
| 404 | private void replaceSelected(final RecipeBookTabButton tabButton) {
|
| 405 | if (this.selectedTab != null) {
|
| 406 | this.selectedTab.unselect();
|
| 407 | }
|
| 408 |
|
| 409 | tabButton.select();
|
| 410 | this.selectedTab = tabButton;
|
| 411 | }
|
| 412 |
|
| 413 | private void toggleFiltering() {
|
| 414 | RecipeBookType type = this.menu.getRecipeBookType();
|
| 415 | boolean newSetting = !this.book.isFiltering(type);
|
| 416 | this.book.setFiltering(type, newSetting);
|
| 417 | }
|
| 418 |
|
| 419 | public boolean hasClickedOutside(final double mx, final double my, final int leftPos, final int topPos, final int imageWidth, final int imageHeight) {
|
| 420 | if (!this.isVisible()) {
|
| 421 | return true;
|
| 422 | }
|
| 423 |
|
| 424 | boolean clickedOutside = mx < leftPos || my < topPos || mx >= leftPos + imageWidth || my >= topPos + imageHeight;
|
| 425 | boolean clickedOnRecipeBook = leftPos - 147 < mx && mx < leftPos && topPos < my && my < topPos + imageHeight;
|
| 426 | return clickedOutside && !clickedOnRecipeBook && !this.selectedTab.isHoveredOrFocused();
|
| 427 | }
|
| 428 |
|
| 429 | @Override
|
| 430 | public boolean keyPressed(final KeyEvent event) {
|
| 431 | this.ignoreTextInput = false;
|
| 432 | if (!this.isVisible() || this.minecraft.player.isSpectator()) {
|
| 433 | return false;
|
| 434 | } else if (event.isEscape() && !this.isOffsetNextToMainGUI()) {
|
| 435 | this.setVisible(false);
|
| 436 | return true;
|
| 437 | } else if (this.searchBox.keyPressed(event)) {
|
| 438 | this.checkSearchStringUpdate();
|
| 439 | return true;
|
| 440 | } else if (this.searchBox.capturesInput() && !event.isEscape()) {
|
| 441 | return true;
|
| 442 | } else if (this.minecraft.options.keyChat.matches(event) && !this.searchBox.isFocused()) {
|
| 443 | this.ignoreTextInput = true;
|
| 444 | this.searchBox.setFocused(true);
|
| 445 | return true;
|
| 446 | } else if (event.isSelection() && this.lastRecipeCollection != null && this.lastRecipe != null) {
|
| 447 | AbstractWidget.playButtonClickSound(Minecraft.getInstance().getSoundManager());
|
| 448 | return this.tryPlaceRecipe(this.lastRecipeCollection, this.lastRecipe, event.hasShiftDown());
|
| 449 | } else {
|
| 450 | return false;
|
| 451 | }
|
| 452 | }
|
| 453 |
|
| 454 | @Override
|
| 455 | public boolean keyReleased(final KeyEvent event) {
|
| 456 | this.ignoreTextInput = false;
|
| 457 | return GuiEventListener.super.keyReleased(event);
|
| 458 | }
|
| 459 |
|
| 460 | @Override
|
| 461 | public boolean charTyped(final CharacterEvent event) {
|
| 462 | if (this.ignoreTextInput) {
|
| 463 | return false;
|
| 464 | } else if (!this.isVisible() || this.minecraft.player.isSpectator()) {
|
| 465 | return false;
|
| 466 | } else if (this.searchBox.charTyped(event)) {
|
| 467 | this.checkSearchStringUpdate();
|
| 468 | return true;
|
| 469 | } else {
|
| 470 | return GuiEventListener.super.charTyped(event);
|
| 471 | }
|
| 472 | }
|
| 473 |
|
| 474 | @Override
|
| 475 | public boolean preeditUpdated(final @Nullable PreeditEvent event) {
|
| 476 | if (this.ignoreTextInput) {
|
| 477 | return false;
|
| 478 | } else if (!this.isVisible() || this.minecraft.player.isSpectator()) {
|
| 479 | return false;
|
| 480 | } else {
|
| 481 | return this.searchBox.preeditUpdated(event) ? true : GuiEventListener.super.preeditUpdated(event);
|
| 482 | }
|
| 483 | }
|
| 484 |
|
| 485 | @Override
|
| 486 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 487 | return false;
|
| 488 | }
|
| 489 |
|
| 490 | @Override
|
| 491 | public void setFocused(final boolean focused) {
|
| 492 | }
|
| 493 |
|
| 494 | @Override
|
| 495 | public boolean isFocused() {
|
| 496 | return false;
|
| 497 | }
|
| 498 |
|
| 499 | @Override
|
| 500 | public boolean capturesInput() {
|
| 501 | return this.searchBox != null && this.searchBox.capturesInput();
|
| 502 | }
|
| 503 |
|
| 504 | private void checkSearchStringUpdate() {
|
| 505 | String searchText = this.searchBox.getValue().toLowerCase(Locale.ROOT);
|
| 506 | this.pirateSpeechForThePeople(searchText);
|
| 507 | if (!searchText.equals(this.lastSearch)) {
|
| 508 | this.updateCollections(false, this.isFiltering());
|
| 509 | this.lastSearch = searchText;
|
| 510 | }
|
| 511 | }
|
| 512 |
|
| 513 | private void pirateSpeechForThePeople(final String searchTarget) {
|
| 514 | if ("excitedze".equals(searchTarget)) {
|
| 515 | LanguageManager languageManager = this.minecraft.getLanguageManager();
|
| 516 | String arrrrCode = "en_pt";
|
| 517 | LanguageInfo language = languageManager.getLanguage("en_pt");
|
| 518 | if (language == null || languageManager.getSelected().equals("en_pt")) {
|
| 519 | return;
|
| 520 | }
|
| 521 |
|
| 522 | languageManager.setSelected("en_pt");
|
| 523 | this.minecraft.options.languageCode = "en_pt";
|
| 524 | this.minecraft.reloadResourcePacks();
|
| 525 | this.minecraft.options.save();
|
| 526 | }
|
| 527 | }
|
| 528 |
|
| 529 | private boolean isOffsetNextToMainGUI() {
|
| 530 | return this.xOffset == 86;
|
| 531 | }
|
| 532 |
|
| 533 | public void recipesUpdated() {
|
| 534 | this.selectMatchingRecipes();
|
| 535 | this.updateTabs(this.isFiltering());
|
| 536 | if (this.isVisible()) {
|
| 537 | this.updateCollections(false, this.isFiltering());
|
| 538 | }
|
| 539 | }
|
| 540 |
|
| 541 | public void recipeShown(final RecipeDisplayId recipe) {
|
| 542 | this.minecraft.player.removeRecipeHighlight(recipe);
|
| 543 | }
|
| 544 |
|
| 545 | public void fillGhostRecipe(final RecipeDisplay recipe) {
|
| 546 | this.ghostSlots.clear();
|
| 547 | ContextMap context = SlotDisplayContext.fromLevel(Objects.requireNonNull(this.minecraft.level));
|
| 548 | this.fillGhostRecipe(this.ghostSlots, recipe, context);
|
| 549 | }
|
| 550 |
|
| 551 | protected abstract void fillGhostRecipe(GhostSlots ghostSlots, RecipeDisplay recipe, ContextMap context);
|
| 552 |
|
| 553 | protected void sendUpdateSettings() {
|
| 554 | if (this.minecraft.getConnection() != null) {
|
| 555 | RecipeBookType type = this.menu.getRecipeBookType();
|
| 556 | boolean open = this.book.getBookSettings().isOpen(type);
|
| 557 | boolean filtering = this.book.getBookSettings().isFiltering(type);
|
| 558 | this.minecraft.getConnection().send(new ServerboundRecipeBookChangeSettingsPacket(type, open, filtering));
|
| 559 | }
|
| 560 | }
|
| 561 |
|
| 562 | @Override
|
| 563 | public NarratableEntry.NarrationPriority narrationPriority() {
|
| 564 | return this.visible ? NarratableEntry.NarrationPriority.HOVERED : NarratableEntry.NarrationPriority.NONE;
|
| 565 | }
|
| 566 |
|
| 567 | @Override
|
| 568 | public void updateNarration(final NarrationElementOutput output) {
|
| 569 | List<NarratableEntry> narratableEntries = Lists.newArrayList();
|
| 570 | this.recipeBookPage.listButtons(e -> {
|
| 571 | if (e.isActive()) {
|
| 572 | narratableEntries.add(e);
|
| 573 | }
|
| 574 | });
|
| 575 | narratableEntries.add(this.searchBox);
|
| 576 | narratableEntries.add(this.filterButton);
|
| 577 | narratableEntries.addAll(this.tabButtons);
|
| 578 | Screen.NarratableSearchResult narratable = Screen.findNarratableWidget(narratableEntries, null);
|
| 579 | if (narratable != null) {
|
| 580 | narratable.entry().updateNarration(output.nest());
|
| 581 | }
|
| 582 | }
|
| 583 |
|
| 584 | @Environment(EnvType.CLIENT)
|
| 585 | public record TabInfo(ItemStack primaryIcon, Optional<ItemStack> secondaryIcon, ExtendedRecipeBookCategory category) {
|
| 586 | public TabInfo(final SearchRecipeBookCategory category) {
|
| 587 | this(new ItemStack(Items.COMPASS), Optional.empty(), category);
|
| 588 | }
|
| 589 |
|
| 590 | public TabInfo(final Item icon, final RecipeBookCategory category) {
|
| 591 | this(new ItemStack(icon), Optional.empty(), category);
|
| 592 | }
|
| 593 |
|
| 594 | public TabInfo(final Item primaryIcon, final Item secondaryIcon, final RecipeBookCategory category) {
|
| 595 | this(new ItemStack(primaryIcon), Optional.of(new ItemStack(secondaryIcon)), category);
|
| 596 | }
|
| 597 | }
|
| 598 | }
|
| 599 |
|