GameModeMenuEntry.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.gui.modules.entries;
  24. import me.shedaniel.rei.api.ConfigObject;
  25. import me.shedaniel.rei.api.REIHelper;
  26. import me.shedaniel.rei.api.widgets.Tooltip;
  27. import me.shedaniel.rei.gui.modules.MenuEntry;
  28. import me.shedaniel.rei.impl.ScreenHelper;
  29. import net.minecraft.client.MinecraftClient;
  30. import net.minecraft.client.gui.Element;
  31. import net.minecraft.client.sound.PositionedSoundInstance;
  32. import net.minecraft.client.util.math.MatrixStack;
  33. import net.minecraft.sound.SoundEvents;
  34. import net.minecraft.text.TranslatableText;
  35. import net.minecraft.world.GameMode;
  36. import java.util.Collections;
  37. import java.util.List;
  38. import java.util.Locale;
  39. public class GameModeMenuEntry extends MenuEntry {
  40. public final String text;
  41. public final GameMode gameMode;
  42. private int x, y, width;
  43. private boolean selected, containsMouse, rendering;
  44. private int textWidth = -69;
  45. public GameModeMenuEntry(GameMode gameMode) {
  46. this.text = gameMode.getTranslatableName().getString();
  47. this.gameMode = gameMode;
  48. }
  49. private int getTextWidth() {
  50. if (textWidth == -69) {
  51. this.textWidth = Math.max(0, font.getStringWidth(text));
  52. }
  53. return this.textWidth;
  54. }
  55. @Override
  56. public int getEntryWidth() {
  57. return getTextWidth() + 4;
  58. }
  59. @Override
  60. public int getEntryHeight() {
  61. return 12;
  62. }
  63. @Override
  64. public List<? extends Element> children() {
  65. return Collections.emptyList();
  66. }
  67. @Override
  68. public void updateInformation(int xPos, int yPos, boolean selected, boolean containsMouse, boolean rendering, int width) {
  69. this.x = xPos;
  70. this.y = yPos;
  71. this.selected = selected;
  72. this.containsMouse = containsMouse;
  73. this.rendering = rendering;
  74. this.width = width;
  75. }
  76. @Override
  77. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  78. if (selected) {
  79. fill(matrices, x, y, x + width, y + 12, -12237499);
  80. }
  81. if (selected && containsMouse) {
  82. REIHelper.getInstance().queueTooltip(Tooltip.create(new TranslatableText("text.rei.gamemode_button.tooltip.entry", text)));
  83. }
  84. font.draw(matrices, text, x + 2, y + 2, selected ? 16777215 : 8947848);
  85. }
  86. @Override
  87. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  88. if (rendering && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + 12) {
  89. MinecraftClient.getInstance().player.sendChatMessage(ConfigObject.getInstance().getGamemodeCommand().replaceAll("\\{gamemode}", gameMode.name().toLowerCase(Locale.ROOT)));
  90. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  91. ScreenHelper.getLastOverlay().removeGameModeMenu();
  92. return true;
  93. }
  94. return super.mouseClicked(mouseX, mouseY, button);
  95. }
  96. }