Skip to content

Commit

Permalink
simplify ButtonMap getButtons
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 16, 2024
1 parent 2782bfd commit a47d3da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.ButtonMap;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.mask.Mask;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -90,16 +87,7 @@ public void stop() {
if (displayButton == null) {
return;
}

DisplayButton currentDisplayButton = map.computeIfAbsent(slot, s -> new DisplayButton());
Item item = displayButton.getItem();
if (item != null) {
currentDisplayButton.setItem(item);
}
Consumer<ClickEvent> action = displayButton.getClickAction();
if (action != null) {
currentDisplayButton.setClickAction(action);
}
map.computeIfAbsent(slot, s -> new DisplayButton()).apply(displayButton);
});
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.ButtonMap;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* A simple {@link ButtonMap} with a list of {@link Button}s
Expand Down Expand Up @@ -101,38 +97,17 @@ public void stop() {
Map<Integer, DisplayButton> map = new HashMap<>();
IntFunction<DisplayButton> getDisplayButton = i -> map.computeIfAbsent(i, s -> new DisplayButton());

List<Integer> emptyItemSlots = IntStream.range(0, size).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
List<Integer> emptyActionSlots = IntStream.range(0, size).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

buttonSlotMap.forEach((button, slots) -> {
DisplayButton displayButton = button.display(uuid);
if (displayButton == null) return;

slots.forEach(slot -> {
DisplayButton currentDisplayButton = getDisplayButton.apply(slot);
Item item = displayButton.getItem();
if (item != null) {
currentDisplayButton.setItem(item);
emptyItemSlots.remove(slot);
}
Consumer<ClickEvent> action = displayButton.getClickAction();
if (action != null) {
currentDisplayButton.setClickAction(action);
emptyActionSlots.remove(slot);
}
});
slots.forEach(slot -> getDisplayButton.apply(slot).apply(displayButton));
});

Button defaultButton = getDefaultButton();
DisplayButton defaultDisplayButton = defaultButton.display(uuid);
if (defaultDisplayButton != null) {
Item defaultItem = defaultDisplayButton.getItem();
if (defaultItem != null) {
emptyItemSlots.forEach(slot -> getDisplayButton.apply(slot).setItem(defaultItem));
}
Consumer<ClickEvent> defaultAction = defaultDisplayButton.getClickAction();
if (defaultAction != null) {
emptyActionSlots.forEach(slot -> getDisplayButton.apply(slot).setClickAction(defaultAction));
for (int i = 0; i < size; i++) {
getDisplayButton.apply(i).apply(defaultDisplayButton);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@ public DisplayButton setClickAction(@Nullable Consumer<ClickEvent> clickAction)
this.clickAction = clickAction;
return this;
}

/**
* Apply the display button to this instance
*
* @param displayButton the display button
*/
public void apply(DisplayButton displayButton) {
if (displayButton.item != null) {
this.item = displayButton.item;
}
if (displayButton.clickAction != null) {
this.clickAction = displayButton.clickAction;
}
}
}

0 comments on commit a47d3da

Please sign in to comment.