Boolean Option

Introduction

The BooleanOption class inherits from the Option<Boolean> class and allows users to switch between two states, typically represented as "on" and "off".

Details

Constructor

The Boolean Option class has two constructors that takes the following parameters:

  • One of the constructors uses the BooleanPool class to store and manage the Boolean values. This is when you don't want the hassle to create a new Boolean for every BooleanOption you create.

  • Supplier Requirement: The Supplier<Boolean> parameter is necessary to obtain the current state of the Boolean value.

  • Consumer Provision: The Consumer<Boolean> parameter updates the Boolean value when the option is interacted with.

  • Name Parameter: The name parameter labels the option within the HUD, enhancing user experience and clarity.

BooleanPool

The BooleanPool class is a thread-safe cache that stores and retrieves Boolean values. It provides a fast and efficient way to access and update the Boolean values. This is what the BooleanPool class looks like

public class BooleanPool {
    private static final Map<String, Boolean> pool = new ConcurrentHashMap<>();

    public static void put(String key, boolean value) {
        pool.put(key, value);
    }
    public static void remove(String key) {
        pool.remove(key);
    }

    public static boolean get(String key) {
        return pool.getOrDefault(key, false);
    }
}

Interactivity

  • Click Response: Clicking the option toggles its state and triggers the associated setter action.

  • Render Behavior: The option is visually represented with a label, changing color to indicate its state (green for "on", red for "off").

  • Mouse Interaction: The class includes methods to handle mouse clicks, ensuring the option responds to user input.

Example Usage

// Create a BooleanOption for sound settings
BooleanOption soundOption = new BooleanOption(
    "Sound Enabled",
    () -> this.isSoundEnabled(), // Getter: Fetches the current sound setting
    value -> this.setSoundEnabled(value) // Setter: Updates the sound setting
);    

Last updated

Was this helpful?