Skip to content

Commit

Permalink
handle generic event on Button
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 17, 2024
1 parent 5887a46 commit 6ca3fde
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -136,20 +137,25 @@ public DisplayButton display(@NotNull UUID uuid) {
return null;
}

Optional.ofNullable(displayButton.getClickAction())
.map(displayButtonAction -> (Consumer<ClickEvent>) event -> {
if (preventSpamClick && clickCheckList.contains(uuid)) {
return;
}
clickCheckList.add(uuid);
clickFuturePredicate.apply(event).thenAccept(result -> {
clickCheckList.remove(uuid);
if (Boolean.TRUE.equals(result)) {
displayButtonAction.accept(event);
Optional.ofNullable(displayButton.getAction())
.map(displayButtonAction -> (Consumer<ViewerEvent>) event -> {
if (event instanceof ClickEvent) {
ClickEvent clickEvent = (ClickEvent) event;
if (preventSpamClick && clickCheckList.contains(uuid)) {
return;
}
});
clickCheckList.add(uuid);
clickFuturePredicate.apply(clickEvent).thenAccept(result -> {
clickCheckList.remove(uuid);
if (Boolean.TRUE.equals(result)) {
displayButtonAction.accept(event);
}
});
} else {
displayButtonAction.accept(event);
}
})
.ifPresent(displayButton::setClickAction);
.ifPresent(displayButton::setAction);

return displayButton;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected InventoryGUIDisplay(@NotNull UUID uuid, @NotNull H holder) {
public void handleEvent(ViewerEvent event) {
if (event instanceof ClickEvent) {
ClickEvent clickEvent = (ClickEvent) event;
getViewedButton(clickEvent.getSlot()).map(DisplayButton::getClickAction).ifPresent(consumer -> consumer.accept(clickEvent));
getViewedButton(clickEvent.getSlot()).map(DisplayButton::getAction).ifPresent(consumer -> consumer.accept(clickEvent));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.button;

import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,7 +17,7 @@ public class DisplayButton {
public static final DisplayButton EMPTY = new DisplayButton();

private Item item;
private Consumer<ClickEvent> clickAction;
private Consumer<ViewerEvent> action;

/**
* Get the item to display
Expand All @@ -41,24 +42,40 @@ public DisplayButton setItem(@Nullable Item item) {
}

/**
* Get the click action
* Get the action to handle the event
*
* @return the action
*/
@Nullable
public Consumer<ClickEvent> getClickAction() {
return clickAction;
public Consumer<ViewerEvent> getAction() {
return action;
}

/**
* Set the click action
* Set the action to handle the event
*
* @param clickAction the action
* @param action the action
*
* @return the current instance
*/
public DisplayButton setClickAction(@Nullable Consumer<ClickEvent> clickAction) {
this.clickAction = clickAction;
public DisplayButton setAction(@Nullable Consumer<ViewerEvent> action) {
this.action = action;
return this;
}

/**
* Set the action to handle the click event
*
* @param action the action
*
* @return the current instance
*/
public DisplayButton setClickAction(@Nullable Consumer<ClickEvent> action) {
this.action = action == null ? null : event -> {
if (event instanceof ClickEvent) {
action.accept((ClickEvent) event);
}
};
return this;
}

Expand All @@ -71,8 +88,8 @@ public void apply(DisplayButton displayButton) {
if (displayButton.item != null) {
this.item = displayButton.item;
}
if (displayButton.clickAction != null) {
this.clickAction = displayButton.clickAction;
if (displayButton.action != null) {
this.action = displayButton.action;
}
}
}

0 comments on commit 6ca3fde

Please sign in to comment.