Created
September 21, 2024 22:37
-
-
Save Densamisten/cf060042820830bce680758757b9983b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exonihility.client.gui; | |
import exonihility.client.config.Config; | |
import exonihility.client.module.Extension; | |
import exonihility.client.module.ModuleManager; | |
import exonihility.client.util.toasts.impl.builder.BasicToastBuilder; | |
import net.fabricmc.api.EnvType; | |
import net.fabricmc.api.Environment; | |
import net.minecraft.client.MinecraftClient; | |
import net.minecraft.client.gui.DrawContext; | |
import net.minecraft.client.gui.Selectable; | |
import net.minecraft.client.gui.screen.Screen; | |
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; | |
import net.minecraft.client.gui.screen.option.GameOptionsScreen; | |
import net.minecraft.client.gui.tooltip.Tooltip; | |
import net.minecraft.client.gui.widget.ButtonWidget; | |
import net.minecraft.client.gui.widget.ClickableWidget; | |
import net.minecraft.client.gui.widget.CyclingButtonWidget; | |
import net.minecraft.client.gui.widget.EntryListWidget; | |
import net.minecraft.client.option.GameOptions; | |
import net.minecraft.text.Text; | |
import net.minecraft.util.Formatting; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
@Environment(EnvType.CLIENT) | |
public class AllyshipOptionsScreen extends GameOptionsScreen { | |
private static final Text TITLE_TEXT = Text.translatable("controls.keybinds.title"); | |
private Screen parent; | |
private ButtonWidget toggleCombatDropdownButton; // Combat dropdown button | |
private ButtonWidget toggleMovementDropdownButton; // Movement dropdown button | |
private ButtonWidget toggleExploitDropdownButton; // Exploit dropdown button | |
private ButtonWidget toggleWorldDropdownButton; // World dropdown button | |
private ButtonWidget toggleRenderDropdownButton; // Render dropdown button | |
private ButtonWidget togglePlayerDropdownButton; // Player dropdown button | |
private MyCustomListWidget entryListWidget; | |
private final List<MyCustomListWidget.Entry> playerDropdownEntries = new ArrayList<>(); // Track Player dropdown entries | |
private boolean playerDropdownVisible = false; // Track visibility of the Player dropdown | |
private boolean combatDropdownVisible = false; // Track visibility of the Combat dropdown | |
private boolean movementDropdownVisible = false; // Track visibility of the Movement dropdown | |
private boolean exploitDropdownVisible = false; // Track visibility of the Exploit dropdown | |
private boolean worldDropdownVisible = false; // Track visibility of the World dropdown | |
private boolean renderDropdownVisible = false; // Track visibility of the Render dropdown | |
private final List<MyCustomListWidget.Entry> combatDropdownEntries = new ArrayList<>(); // Track Combat dropdown entries | |
private final List<MyCustomListWidget.Entry> movementDropdownEntries = new ArrayList<>(); // Track Movement dropdown entries | |
private final List<MyCustomListWidget.Entry> exploitDropdownEntries = new ArrayList<>(); // Track Exploit dropdown entries | |
private final List<MyCustomListWidget.Entry> worldDropdownEntries = new ArrayList<>(); // Track World dropdown entries | |
private final List<MyCustomListWidget.Entry> renderDropdownEntries = new ArrayList<>(); // Track Render dropdown entries | |
private int combatDropdownStartY; // Track where the Combat dropdown starts rendering | |
private int movementDropdownStartY; // Track where the Movement dropdown starts rendering | |
private int exploitDropdownStartY; // Track where the Exploit dropdown starts rendering | |
private int worldDropdownStartY; // Track where the World dropdown starts rendering | |
private int renderDropdownStartY; // Track where the Render dropdown starts rendering | |
private int playerDropdownStartY; // Track where the Player dropdown starts rendering | |
CyclingButtonWidget<Object> cyclingButton; | |
String initiallyActiveFeature = determineInitiallyActiveFeature(); | |
int centerX = width / 2; | |
// Button width adjusted to a smaller size (160) | |
int buttonWidth = 160; | |
public AllyshipOptionsScreen(Screen parent, GameOptions gameOptions) { | |
super(parent, gameOptions, TITLE_TEXT); | |
this.parent = parent; | |
} | |
private String determineInitiallyActiveFeature() { | |
if (Config.getBoolean(Config.CREATIVEFLY)) { | |
return "CreativeFly"; | |
} else if (Config.getBoolean(Config.FLIGHT)) { | |
return "Flight"; | |
} else if (Config.getBoolean(Config.BOATFLY)) { | |
return "BoatFly"; | |
} else { | |
return "Disabled"; | |
} | |
} | |
@Override | |
protected void init() { | |
updateElementPositions(); | |
} | |
@Override | |
public void resize(MinecraftClient client, int width, int height) { | |
super.resize(client, width, height); | |
updateElementPositions(); | |
} | |
private void updateElementPositions() { | |
// Clear existing children to prevent duplication | |
this.clearChildren(); | |
int centerX = width / 2; | |
int buttonWidth = 50; | |
int buttonHeight = 20; | |
int buttonSpacing = 20; // Space between each button | |
// Total number of dropdown buttons | |
int totalDropdownButtons = 6; | |
// Calculate total width of all dropdown buttons including spaces between them | |
int totalDropdownWidth = (totalDropdownButtons * buttonWidth) + ((totalDropdownButtons - 1) * buttonSpacing); | |
// Calculate the starting X position for the first dropdown button to center everything | |
int startXDropdown = centerX - (totalDropdownWidth / 2); | |
// Create the custom widget list | |
entryListWidget = new MyCustomListWidget(this.client, this.width, this.height, 90, this.height - 40, 20); | |
this.addSelectableChild(entryListWidget); | |
// Combat dropdown button | |
this.toggleCombatDropdownButton = ButtonWidget.builder(Text.literal("Combat"), button -> toggleCombatDropdown()) | |
.dimensions(startXDropdown, 50, buttonWidth, buttonHeight) | |
.build(); | |
combatDropdownStartY = toggleCombatDropdownButton.getY() + toggleCombatDropdownButton.getHeight() + 5; | |
this.addDrawableChild(toggleCombatDropdownButton); | |
// Movement dropdown button | |
this.toggleMovementDropdownButton = ButtonWidget.builder(Text.literal("Movement"), button -> toggleMovementDropdown()) | |
.dimensions(startXDropdown + (buttonWidth + buttonSpacing), 50, buttonWidth, buttonHeight) | |
.build(); | |
movementDropdownStartY = toggleMovementDropdownButton.getY() + toggleMovementDropdownButton.getHeight() + 5; | |
this.addDrawableChild(toggleMovementDropdownButton); | |
// Exploit dropdown button | |
this.toggleExploitDropdownButton = ButtonWidget.builder(Text.literal("Exploit"), button -> toggleExploitDropdown()) | |
.dimensions(startXDropdown + 2 * (buttonWidth + buttonSpacing), 50, buttonWidth, buttonHeight) | |
.build(); | |
exploitDropdownStartY = toggleExploitDropdownButton.getY() + toggleExploitDropdownButton.getHeight() + 5; | |
this.addDrawableChild(toggleExploitDropdownButton); | |
// World dropdown button | |
this.toggleWorldDropdownButton = ButtonWidget.builder(Text.literal("World"), button -> toggleWorldDropdown()) | |
.dimensions(startXDropdown + 3 * (buttonWidth + buttonSpacing), 50, buttonWidth, buttonHeight) | |
.build(); | |
worldDropdownStartY = toggleWorldDropdownButton.getY() + toggleWorldDropdownButton.getHeight() + 5; | |
this.addDrawableChild(toggleWorldDropdownButton); | |
// Render dropdown button | |
this.toggleRenderDropdownButton = ButtonWidget.builder(Text.literal("Render"), button -> toggleRenderDropdown()) | |
.dimensions(startXDropdown + 4 * (buttonWidth + buttonSpacing), 50, buttonWidth, buttonHeight) | |
.build(); | |
renderDropdownStartY = toggleRenderDropdownButton.getY() + toggleRenderDropdownButton.getHeight() + 5; | |
this.addDrawableChild(toggleRenderDropdownButton); | |
// Player dropdown button | |
this.togglePlayerDropdownButton = ButtonWidget.builder(Text.literal("Player"), button -> togglePlayerDropdown()) | |
.dimensions(startXDropdown + 5 * (buttonWidth + buttonSpacing), 50, buttonWidth, buttonHeight) | |
.build(); | |
playerDropdownStartY = togglePlayerDropdownButton.getY() + togglePlayerDropdownButton.getHeight() + 5; | |
this.addDrawableChild(togglePlayerDropdownButton); | |
// Position and create the Generate button | |
ButtonWidget openSystemButton = ButtonWidget.builder(Text.literal("System"), button -> { | |
client.setScreen(new AllyshipSystemInformationScreen(parent, gameOptions)); | |
}) | |
.dimensions(centerX - 75, 100, 150, 20) // Adjust the Y-coordinate as needed to align with other UI elements | |
.tooltip(Tooltip.of(Text.literal("Reveals the system screen"))) | |
.build(); | |
addDrawableChild(openSystemButton); | |
} | |
@Override | |
protected void addOptions() { | |
} | |
@Override | |
public void removed() { | |
this.client.options.write(); | |
} | |
@Override | |
public void close() { | |
client.setScreen(parent); | |
} | |
// Toggle dropdown for Player | |
private void togglePlayerDropdown() { | |
// Close other dropdowns when this one is opened | |
closeAllDropdownsExcept("player"); | |
playerDropdownVisible = !playerDropdownVisible; | |
if (playerDropdownVisible) { | |
addPlayerDropdownEntries(); | |
} else { | |
removePlayerDropdownEntries(); | |
} | |
this.entryListWidget.setScrollAmount(0); | |
this.entryListWidget.updateList(); | |
} | |
private void addPlayerDropdownEntries() { | |
playerDropdownEntries.clear(); | |
// Add additional entries here if needed | |
} | |
private void removePlayerDropdownEntries() { | |
entryListWidget.removeEntries(playerDropdownEntries); | |
playerDropdownEntries.clear(); | |
} | |
// Toggle dropdown for Combat | |
private void toggleCombatDropdown() { | |
// Close other dropdowns when this one is opened | |
closeAllDropdownsExcept("combat"); | |
combatDropdownVisible = !combatDropdownVisible; | |
if (combatDropdownVisible) { | |
addCombatDropdownEntries(); | |
} else { | |
removeCombatDropdownEntries(); | |
} | |
this.entryListWidget.setScrollAmount(0); | |
this.entryListWidget.updateList(); | |
} | |
private void addCombatDropdownEntries() { | |
combatDropdownEntries.clear(); | |
// Create the KillAura button entry | |
ButtonEntryWidget killauraEntry = new ButtonEntryWidget("KillAura", 0, combatDropdownStartY, 100, 20, () -> { | |
toggleModule("KillAura"); | |
System.out.println("KillAura toggled!"); | |
}); | |
combatDropdownEntries.add(killauraEntry); | |
entryListWidget.addCustomEntry(killauraEntry); | |
// Create the Reach button entry with asynchronous action | |
ButtonEntryWidget reachEntry = new ButtonEntryWidget("Reach", 0, combatDropdownStartY + 20, 100, 20, () -> { | |
CompletableFuture.runAsync(() -> { | |
toggleModule("Reach"); | |
System.out.println("Reach toggled!"); | |
}); | |
}); | |
combatDropdownEntries.add(reachEntry); | |
entryListWidget.addCustomEntry(reachEntry); | |
} | |
private void removeCombatDropdownEntries() { | |
entryListWidget.removeEntries(combatDropdownEntries); | |
combatDropdownEntries.clear(); | |
} | |
// Toggle dropdown for Movement | |
private void toggleMovementDropdown() { | |
closeAllDropdownsExcept("movement"); | |
movementDropdownVisible = !movementDropdownVisible; | |
if (movementDropdownVisible) { | |
addMovementDropdownEntries(); | |
} else { | |
removeMovementDropdownEntries(); | |
} | |
entryListWidget.setScrollAmount(0); | |
entryListWidget.updateList(); | |
} | |
private void addMovementDropdownEntries() { | |
movementDropdownEntries.clear(); | |
// Add Movement entries with appropriate null checks and actions | |
ButtonEntryWidget movementOption1 = new ButtonEntryWidget("AutoJump", 0, movementDropdownStartY, 100, 20, () -> { | |
toggleModule("AutoJump"); | |
System.out.println("AutoJump toggled!"); | |
}); | |
movementDropdownEntries.add(movementOption1); | |
entryListWidget.addCustomEntry(movementOption1); | |
ButtonEntryWidget movementOption2 = new ButtonEntryWidget("AutoWalk", 0, movementDropdownStartY + 20, 100, 20, () -> { | |
toggleModule("AutoWalk"); | |
System.out.println("AutoWalk toggled!"); | |
}); | |
movementDropdownEntries.add(movementOption2); | |
entryListWidget.addCustomEntry(movementOption2); | |
// Add Carousel entry as a ButtonEntryWidget | |
ButtonEntryWidget carouselOption = new ButtonEntryWidget("Carousel", 0, movementDropdownStartY + 40, 100, 20, () -> { | |
toggleModule("Carousel"); | |
System.out.println("Carousel toggled!"); | |
}); | |
movementDropdownEntries.add(carouselOption); | |
entryListWidget.addCustomEntry(carouselOption); | |
// Add Jesus entry as a ButtonEntryWidget | |
ButtonEntryWidget jesusOption = new ButtonEntryWidget("Jesus", 0, movementDropdownStartY + 60, 100, 20, () -> { | |
toggleModule("Jesus"); | |
System.out.println("Jesus toggled!"); | |
}); | |
movementDropdownEntries.add(jesusOption); | |
entryListWidget.addCustomEntry(jesusOption); | |
// Add JetPack entry as a ButtonEntryWidget | |
ButtonEntryWidget jetPackOption = new ButtonEntryWidget("JetPack", 0, movementDropdownStartY + 80, 100, 20, () -> { | |
toggleModule("JetPack"); | |
System.out.println("JetPack toggled!"); | |
}); | |
movementDropdownEntries.add(jetPackOption); | |
entryListWidget.addCustomEntry(jetPackOption); | |
// Add Lock entry as a ButtonEntryWidget | |
ButtonEntryWidget lockOption = new ButtonEntryWidget("Lock", 0, movementDropdownStartY + 100, 100, 20, () -> { | |
toggleModule("Lock"); | |
System.out.println("Lock toggled!"); | |
}); | |
movementDropdownEntries.add(lockOption); | |
entryListWidget.addCustomEntry(lockOption); | |
// Add NoFall entry as a ButtonEntryWidget | |
ButtonEntryWidget noFallOption = new ButtonEntryWidget("NoFall", 0, movementDropdownStartY + 120, 100, 20, () -> { | |
toggleModule("NoFall"); | |
System.out.println("NoFall toggled!"); | |
}); | |
movementDropdownEntries.add(noFallOption); | |
entryListWidget.addCustomEntry(noFallOption); | |
// Add Speed entry as a ButtonEntryWidget | |
ButtonEntryWidget speedOption = new ButtonEntryWidget("Speed", 0, movementDropdownStartY + 140, 100, 20, () -> { | |
toggleModule("Speed"); | |
System.out.println("Speed toggled!"); | |
}); | |
movementDropdownEntries.add(speedOption); | |
entryListWidget.addCustomEntry(speedOption); | |
// Add Spider entry as a ButtonEntryWidget | |
ButtonEntryWidget spiderOption = new ButtonEntryWidget("Spider", 0, movementDropdownStartY + 160, 100, 20, () -> { | |
toggleModule("Spider"); | |
System.out.println("Spider toggled!"); | |
}); | |
movementDropdownEntries.add(spiderOption); | |
entryListWidget.addCustomEntry(spiderOption); | |
// Add Glide entry as a ButtonEntryWidget in the addMovementDropdownEntries method | |
ButtonEntryWidget glideOption = new ButtonEntryWidget("Glide", 0, movementDropdownStartY + 220, 100, 20, () -> { | |
toggleModule("Glide"); | |
System.out.println("Glide toggled!"); | |
}); | |
movementDropdownEntries.add(glideOption); | |
entryListWidget.addCustomEntry(glideOption); | |
ButtonEntryWidget autoSwimOption = new ButtonEntryWidget("AutoSwim", 0, movementDropdownStartY + 220, 100, 20, () -> { | |
toggleModule("AutoSwim"); | |
System.out.println("AutoSwim Toggled!"); | |
}); | |
movementDropdownEntries.add(autoSwimOption); | |
entryListWidget.addCustomEntry(autoSwimOption); | |
ButtonEntryWidget blinkOption = new ButtonEntryWidget("Blink", 0, movementDropdownStartY + 220, 100, 20, () -> { | |
toggleModule("Blink"); | |
System.out.println("Blink toggled!"); | |
}); | |
movementDropdownEntries.add(blinkOption); | |
entryListWidget.addCustomEntry(blinkOption); | |
// Adjust the position to be within the screen view | |
int buttonPosX = centerX + 100; | |
int buttonPosY = movementDropdownStartY + 200; // Adjust based on where it should be relative to other elements | |
// Initialize the cycling button with adjusted position and size | |
this.cyclingButton = CyclingButtonWidget.builder((value) -> Text.of((String) value)) | |
.values("CreativeFly", "Flight", "BoatFly", "Disabled") | |
.initially(initiallyActiveFeature) | |
.tooltip((value) -> { | |
String[] modes = {"CreativeFly", "Flight", "BoatFly"}; | |
for (String mode : modes) { | |
boolean enable = mode.equals(value); | |
ModuleManager.getInstance.getModuleByName(mode.toLowerCase()).setEnabled(enable); | |
} | |
return Tooltip.of(Text.of("Enables " + (value.equals("Disabled") ? " Nothing" : value))); | |
}) | |
.build(buttonPosX, buttonPosY, buttonWidth, 20, Text.of("Aerial Navigation: "), (button, value) -> { | |
System.out.println("Aerial Navigation changed to: " + value); | |
updateConfig(value.toString()); | |
}); | |
// Add the cycling button to the list of drawable children to make it visible | |
this.addDrawableChild(cyclingButton); | |
} | |
private void updateConfig(String selectedValue) { | |
Config.set(selectedValue, true); | |
String[] features = {"CreativeFly", "Flight", "BoatFly"}; | |
for (String feature : features) { | |
if (!feature.equals(selectedValue)) { | |
Config.set(feature, false); | |
} | |
} | |
} | |
private void removeMovementDropdownEntries() { | |
entryListWidget.removeEntries(movementDropdownEntries); | |
movementDropdownEntries.clear(); | |
} | |
// Toggle dropdown for Exploit | |
private void toggleExploitDropdown() { | |
closeAllDropdownsExcept("exploit"); | |
exploitDropdownVisible = !exploitDropdownVisible; | |
if (exploitDropdownVisible) { | |
addExploitDropdownEntries(); | |
} else { | |
removeExploitDropdownEntries(); | |
} | |
entryListWidget.setScrollAmount(0); | |
entryListWidget.updateList(); | |
} | |
private void addExploitDropdownEntries() { | |
exploitDropdownEntries.clear(); | |
} | |
private void removeExploitDropdownEntries() { | |
entryListWidget.removeEntries(exploitDropdownEntries); | |
exploitDropdownEntries.clear(); | |
} | |
// Toggle dropdown for World | |
private void toggleWorldDropdown() { | |
closeAllDropdownsExcept("world"); | |
worldDropdownVisible = !worldDropdownVisible; | |
if (worldDropdownVisible) { | |
addWorldDropdownEntries(); | |
} else { | |
removeWorldDropdownEntries(); | |
} | |
entryListWidget.setScrollAmount(0); | |
entryListWidget.updateList(); | |
} | |
private void addWorldDropdownEntries() { | |
worldDropdownEntries.clear(); | |
} | |
private void removeWorldDropdownEntries() { | |
entryListWidget.removeEntries(worldDropdownEntries); | |
worldDropdownEntries.clear(); | |
} | |
// Toggle dropdown for Render | |
private void toggleRenderDropdown() { | |
closeAllDropdownsExcept("render"); | |
renderDropdownVisible = !renderDropdownVisible; | |
if (renderDropdownVisible) { | |
addRenderDropdownEntries(); | |
} else { | |
removeRenderDropdownEntries(); | |
} | |
entryListWidget.setScrollAmount(0); | |
entryListWidget.updateList(); | |
} | |
private void addRenderDropdownEntries() { | |
renderDropdownEntries.clear(); | |
ButtonEntryWidget renderOption1 = new ButtonEntryWidget("FullBright", 0, renderDropdownStartY, 100, 20, () -> { | |
toggleModule("FullBright"); | |
System.out.println("FullBright toggled!"); | |
}); | |
renderDropdownEntries.add(renderOption1); | |
entryListWidget.addCustomEntry(renderOption1); | |
} | |
private void removeRenderDropdownEntries() { | |
entryListWidget.removeEntries(renderDropdownEntries); | |
renderDropdownEntries.clear(); | |
} | |
private void closeAllDropdownsExcept(String dropdown) { | |
if (!"combat".equals(dropdown) && combatDropdownVisible) { | |
combatDropdownVisible = false; | |
removeCombatDropdownEntries(); | |
} | |
if (!"movement".equals(dropdown) && movementDropdownVisible) { | |
movementDropdownVisible = false; | |
removeMovementDropdownEntries(); | |
} | |
if (!"exploit".equals(dropdown) && exploitDropdownVisible) { | |
exploitDropdownVisible = false; | |
removeExploitDropdownEntries(); | |
} | |
if (!"world".equals(dropdown) && worldDropdownVisible) { | |
worldDropdownVisible = false; | |
removeWorldDropdownEntries(); | |
} | |
if (!"render".equals(dropdown) && renderDropdownVisible) { | |
renderDropdownVisible = false; | |
removeRenderDropdownEntries(); | |
} | |
if (!"player".equals(dropdown) && playerDropdownVisible) { | |
playerDropdownVisible = false; | |
removePlayerDropdownEntries(); | |
} | |
} | |
// Method to toggle module and save its state | |
private void toggleModule(String moduleName) { | |
// Attempt to get the module from the manager | |
Extension module = ModuleManager.getInstance.getModuleByName(moduleName); | |
// Check if the module is not null before calling toggle | |
if (module != null) { | |
module.toggle(); // Toggle the module state | |
System.out.println(moduleName + " module toggled!"); | |
// Ensure state consistency between module and config | |
Config.set(moduleName, module.isEnabled()); | |
Config.save(); // Save the updated configuration | |
} else { | |
System.err.println("Module not found: " + moduleName); // Log an error if the module is missing | |
} | |
// Update button states based on the new config values | |
updateButtonStates(); | |
} | |
private void updateButtonStates() { | |
updateEntriesState(combatDropdownEntries); | |
updateEntriesState(movementDropdownEntries); | |
updateEntriesState(exploitDropdownEntries); | |
updateEntriesState(worldDropdownEntries); | |
updateEntriesState(renderDropdownEntries); | |
updateEntriesState(playerDropdownEntries); | |
} | |
private void updateEntriesState(List<MyCustomListWidget.Entry> entries) { | |
for (MyCustomListWidget.Entry entry : entries) { | |
if (entry instanceof ButtonEntryWidget buttonEntry) { | |
boolean isEnabled = Config.getBoolean(buttonEntry.getButtonText()); | |
buttonEntry.setActive(isEnabled); | |
buttonEntry.updateButtonAppearance(); // Ensure appearance matches the state | |
} | |
} | |
} | |
@Override | |
public void render(DrawContext context, int mouseX, int mouseY, float delta) { | |
super.render(context, mouseX, mouseY, delta); | |
this.entryListWidget.render(context, mouseX, mouseY, delta); | |
} | |
@Override | |
public boolean mouseClicked(double mouseX, double mouseY, int button) { | |
// Handle clicks on the entry list widget first | |
if (this.entryListWidget.mouseClicked(mouseX, mouseY, button)) { | |
return true; | |
} | |
// Check if the cycling button is visible and active before processing clicks | |
if (this.cyclingButton != null && this.cyclingButton.visible && this.cyclingButton.active) { | |
if (this.client != null && this.client.player != null) { | |
if (button == 2) { // Middle mouse button click | |
// Check if the cycling button's current value is "Flight" | |
if (this.cyclingButton.getValue().equals("Flight")) { | |
// Open the FlightScreen | |
this.client.setScreen(new FlightScreen(parent, gameOptions)); | |
} | |
} | |
} | |
} | |
// Fallback to the superclass mouseClicked method for other interactions | |
return super.mouseClicked(mouseX, mouseY, button); | |
} | |
public static class MyCustomListWidget extends EntryListWidget<MyCustomListWidget.Entry> { | |
public MyCustomListWidget(MinecraftClient client, int width, int height, int top, int bottom, int itemHeight) { | |
super(client, width, height, top, itemHeight); | |
} | |
@Override | |
protected void drawMenuListBackground(DrawContext context) { | |
// Override to prevent background drawing | |
} | |
@Override | |
protected void drawSelectionHighlight(DrawContext context, int y, int entryWidth, int entryHeight, int borderColor, int fillColor) { | |
// Override to prevent selection highlight drawing | |
} | |
public void addCustomEntry(Entry entry) { | |
this.addEntry(entry); | |
} | |
public void removeEntries(List<MyCustomListWidget.Entry> entries) { | |
this.children().removeAll(entries); | |
this.setScrollAmount(0); | |
} | |
public void updateList() { | |
this.setScrollAmount(0); | |
} | |
@Override | |
public int getScrollbarX() { | |
return this.width - 6; | |
} | |
@Override | |
public int getRowWidth() { | |
return this.width - 12; | |
} | |
@Override | |
protected void appendClickableNarrations(NarrationMessageBuilder builder) { | |
} | |
public abstract static class Entry extends EntryListWidget.Entry<MyCustomListWidget.Entry> { | |
} | |
} | |
public static class ButtonEntryWidget extends MyCustomListWidget.Entry { | |
private final ButtonWidget buttonWidget; | |
private final Runnable onClickAction; | |
private boolean isActive; | |
private final String buttonText; | |
public ButtonEntryWidget(String buttonText, int x, int y, int width, int height, Runnable onClickAction) { | |
this.buttonText = buttonText; | |
this.onClickAction = onClickAction; | |
// Initialize the button with the default style | |
this.buttonWidget = ButtonWidget.builder(Text.literal(getFormattedButtonText()), button -> toggleState()) | |
.dimensions(x, y, width, height) | |
.build(); | |
// Set initial active state based on configuration | |
this.isActive = Boolean.TRUE.equals(Config.getBoolean(buttonText)); | |
updateButtonAppearance(); | |
} | |
private void toggleState() { | |
isActive = !isActive; // Toggle the state | |
Config.set(buttonText, isActive); // Save the new state | |
updateButtonAppearance(); // Update the button's visual appearance | |
} | |
private void updateButtonAppearance() { | |
// Update button text with appropriate color formatting to indicate state | |
buttonWidget.setMessage(Text.literal(getFormattedButtonText())); | |
} | |
private String getFormattedButtonText() { | |
// Apply color formatting to show active/inactive states | |
return isActive ? Formatting.GREEN + buttonText : Formatting.RED + buttonText; | |
} | |
@Override | |
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { | |
// Update button position for consistency with the list | |
buttonWidget.setX(x); | |
buttonWidget.setY(y); | |
// Render the button with Minecraft's default look | |
buttonWidget.render(context, mouseX, mouseY, tickDelta); | |
} | |
@Override | |
public boolean mouseClicked(double mouseX, double mouseY, int button) { | |
if (isMouseOver(mouseX, mouseY)) { | |
isActive = !isActive; | |
if (onClickAction != null) { | |
onClickAction.run(); | |
new BasicToastBuilder() | |
.title("Action Triggered!") | |
.description("Button: " + buttonText + " clicked") | |
.build() | |
.show(); | |
} | |
return true; | |
} | |
return false; | |
} | |
public void setActive(boolean active) { | |
this.isActive = active; | |
updateButtonAppearance(); | |
} | |
public String getButtonText() { | |
return buttonText; | |
} | |
} | |
public static class TextEntryWidget extends MyCustomListWidget.Entry { | |
private final String text; | |
private boolean isSelected; | |
private final Runnable onClickAction; | |
private int x, y; | |
public TextEntryWidget(String text, int yPosition, Runnable onClickAction) { | |
this.text = text; | |
this.onClickAction = onClickAction; | |
this.x = 0; | |
this.y = yPosition; | |
} | |
@Override | |
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { | |
this.x = x; | |
this.y = y; | |
int textColor = isSelected ? 0x00FF00 : 0xFFFFFF; | |
context.drawText(MinecraftClient.getInstance().textRenderer, text, x, y, textColor, false); | |
} | |
@Override | |
public boolean mouseClicked(double mouseX, double mouseY, int button) { | |
if (isMouseOver(mouseX - x, mouseY - y, 150)) { | |
isSelected = !isSelected; | |
if (isSelected && onClickAction != null) { | |
onClickAction.run(); | |
new BasicToastBuilder() | |
.title("Action Triggered!") | |
.description("Enabled: " + text) | |
.build() | |
.show(); | |
} | |
return true; | |
} | |
return false; | |
} | |
public boolean isMouseOver(double mouseX, double mouseY, int entryWidth) { | |
return mouseX >= 0 && mouseX <= entryWidth && mouseY >= 0 && mouseY <= 20; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment