-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
minecraft/gui/button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/SimpleButton.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package me.hsgamer.hscore.minecraft.gui.button; | ||
|
||
import me.hsgamer.hscore.minecraft.gui.common.action.Action; | ||
import me.hsgamer.hscore.minecraft.gui.common.button.Button; | ||
import me.hsgamer.hscore.minecraft.gui.common.event.ClickEvent; | ||
import me.hsgamer.hscore.minecraft.gui.common.inventory.InventoryContext; | ||
import me.hsgamer.hscore.minecraft.gui.common.item.ActionItem; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A simple button | ||
*/ | ||
public class SimpleButton implements Button { | ||
private final Function<UUID, Object> itemFunction; | ||
private final Consumer<ClickEvent> consumer; | ||
|
||
/** | ||
* Create a new simple button | ||
* | ||
* @param itemFunction the item function | ||
* @param consumer the consumer | ||
*/ | ||
public SimpleButton(@NotNull Function<@NotNull UUID, @Nullable Object> itemFunction, @NotNull Consumer<@NotNull ClickEvent> consumer) { | ||
this.itemFunction = itemFunction; | ||
this.consumer = consumer; | ||
} | ||
|
||
/** | ||
* Create a new button | ||
* | ||
* @param item the item | ||
* @param consumer the consumer | ||
*/ | ||
public SimpleButton(@Nullable Object item, @NotNull Consumer<@NotNull ClickEvent> consumer) { | ||
this(uuid -> item, consumer); | ||
} | ||
|
||
/** | ||
* Create a new button with a null item | ||
* | ||
* @param consumer the consumer | ||
*/ | ||
public SimpleButton(@NotNull Consumer<@NotNull ClickEvent> consumer) { | ||
this((Object) null, consumer); | ||
} | ||
|
||
/** | ||
* Create a new button | ||
* | ||
* @param itemFunction the item function | ||
*/ | ||
public SimpleButton(@NotNull Function<@NotNull UUID, @Nullable Object> itemFunction) { | ||
this(itemFunction, event -> { | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new button | ||
* | ||
* @param item the item | ||
*/ | ||
public SimpleButton(@Nullable Object item) { | ||
this(uuid -> item); | ||
} | ||
|
||
@Override | ||
public @NotNull ActionItem getItem(InventoryContext context) { | ||
return new ActionItem() | ||
.setItem(itemFunction.apply(context.getViewerID())) | ||
.setAction(new Action() { | ||
@Override | ||
public void handleClick(ClickEvent event) { | ||
consumer.accept(event); | ||
} | ||
}); | ||
} | ||
} |