Skip to content

Commit

Permalink
simplify button view
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 15, 2024
1 parent eba7c05 commit e8fabb9
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent;
import me.hsgamer.hscore.bukkit.gui.object.BukkitItem;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
Expand All @@ -25,27 +25,23 @@ public class InputButton implements Button {
private BiFunction<@NotNull UUID, @Nullable ItemStack, @Nullable ItemStack> displayItemFunction = (uuid, item) -> item;

@Override
public BukkitItem getItem(@NotNull UUID uuid) {
return new BukkitItem(displayItemFunction.apply(uuid, getInputItem(uuid)));
}

@Override
public void handleAction(@NotNull ClickEvent wrappedEvent) {
if (!(wrappedEvent instanceof BukkitClickEvent)) return;
UUID uuid = wrappedEvent.getViewerID();
InventoryClickEvent event = ((BukkitClickEvent) wrappedEvent).getEvent();
ItemStack cursorItem = Optional.ofNullable(event.getCursor())
.filter(itemStack -> itemStack.getType() != Material.AIR)
.map(ItemStack::clone)
.orElse(null);
ItemStack storeItem = getInputItem(uuid);
event.getWhoClicked().setItemOnCursor(storeItem);
setInputItem(uuid, cursorItem);
}

@Override
public boolean forceSetAction(@NotNull UUID uuid) {
return true;
public @Nullable DisplayButton view(@NotNull UUID uuid) {
return new DisplayButton(
new BukkitItem(displayItemFunction.apply(uuid, getInputItem(uuid))),
this,
event -> {
if (!(event instanceof BukkitClickEvent)) return;
UUID viewerID = event.getViewerID();
InventoryClickEvent bukkitEvent = ((BukkitClickEvent) event).getEvent();
ItemStack cursorItem = Optional.ofNullable(bukkitEvent.getCursor())
.filter(itemStack -> itemStack.getType() != Material.AIR)
.map(ItemStack::clone)
.orElse(null);
ItemStack storeItem = getInputItem(viewerID);
bukkitEvent.getWhoClicked().setItemOnCursor(storeItem);
setInputItem(viewerID, cursorItem);
}
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent;
import me.hsgamer.hscore.bukkit.gui.object.BukkitItem;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
Expand All @@ -24,27 +24,23 @@ public class OutputButton implements Button {
private BiFunction<@NotNull UUID, @Nullable ItemStack, @Nullable ItemStack> displayItemFunction = (uuid, item) -> item;

@Override
public BukkitItem getItem(@NotNull UUID uuid) {
return new BukkitItem(displayItemFunction.apply(uuid, getOutputItem(uuid)));
}

@Override
public void handleAction(@NotNull ClickEvent wrappedEvent) {
if (!(wrappedEvent instanceof BukkitClickEvent)) return;
UUID uuid = wrappedEvent.getViewerID();
InventoryClickEvent event = ((BukkitClickEvent) wrappedEvent).getEvent();
ItemStack item = event.getCursor();
if (item != null && item.getType() != Material.AIR) {
return;
}
ItemStack storeItem = getOutputItem(uuid);
event.getWhoClicked().setItemOnCursor(storeItem);
setOutputItem(uuid, null);
}

@Override
public boolean forceSetAction(@NotNull UUID uuid) {
return true;
public @Nullable DisplayButton view(@NotNull UUID uuid) {
return new DisplayButton(
new BukkitItem(displayItemFunction.apply(uuid, getOutputItem(uuid))),
this,
event -> {
if (!(event instanceof BukkitClickEvent)) return;
UUID viewerID = event.getViewerID();
InventoryClickEvent bukkitEvent = ((BukkitClickEvent) event).getEvent();
ItemStack item = bukkitEvent.getCursor();
if (item != null && item.getType() != Material.AIR) {
return;
}
ItemStack storeItem = getOutputItem(viewerID);
bukkitEvent.getWhoClicked().setItemOnCursor(storeItem);
setOutputItem(viewerID, null);
}
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.ButtonMap;
import me.hsgamer.hscore.minecraft.gui.button.ViewedButton;
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 @@ -74,8 +76,8 @@ public void stop() {
}

@Override
public @NotNull Map<@NotNull Integer, @NotNull ViewedButton> getButtons(@NotNull UUID uuid, int size) {
Map<Integer, ViewedButton> map = new HashMap<>();
public @NotNull Map<@NotNull Integer, @NotNull DisplayButton> getButtons(@NotNull UUID uuid, int size) {
Map<Integer, DisplayButton> map = new HashMap<>();
for (Mask mask : masks) {
Optional<Map<Integer, Button>> buttons = mask.generateButtons(uuid, size);
if (!buttons.isPresent()) continue;
Expand All @@ -84,16 +86,21 @@ public void stop() {
return;
}

Item item = button.getItem(uuid);
if (item == null && !button.forceSetAction(uuid)) {
DisplayButton displayButton = button.view(uuid);
if (displayButton == null) {
return;
}

ViewedButton viewedButton = map.computeIfAbsent(slot, s -> new ViewedButton());
DisplayButton currentDisplayButton = map.computeIfAbsent(slot, s -> new DisplayButton());
Item item = displayButton.getDisplayItem();
if (item != null) {
viewedButton.setDisplayItem(item);
currentDisplayButton.setDisplayItem(item);
}
viewedButton.setButton(button);
Consumer<ClickEvent> action = displayButton.getAction();
if (action != null) {
currentDisplayButton.setAction(action);
}
currentDisplayButton.setButton(button);
});
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import me.hsgamer.hscore.minecraft.gui.GUIProperties;
import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.ui.property.IdentifiedUpdatable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -88,19 +87,9 @@ private int getCurrentIndex(UUID uuid) {
}

@Override
public Item getItem(@NotNull UUID uuid) {
public DisplayButton view(@NotNull UUID uuid) {
update(uuid);
return buttons.get(getCurrentIndex(uuid)).getItem(uuid);
}

@Override
public void handleAction(@NotNull ClickEvent event) {
buttons.get(getCurrentIndex(event.getViewerID())).handleAction(event);
}

@Override
public boolean forceSetAction(@NotNull UUID uuid) {
return buttons.get(getCurrentIndex(uuid)).forceSetAction(uuid);
return buttons.get(getCurrentIndex(uuid)).view(uuid);
}

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

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -33,7 +34,7 @@ public DummyButton(@Nullable Item itemStack) {
}

@Override
public @Nullable Item getItem(@NotNull UUID uuid) {
return itemFunction.apply(uuid);
public @NotNull DisplayButton view(@NotNull UUID uuid) {
return new DisplayButton(itemFunction.apply(uuid), this, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.button.impl;

import me.hsgamer.hscore.minecraft.gui.button.Button;
import me.hsgamer.hscore.minecraft.gui.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import me.hsgamer.hscore.minecraft.gui.button.DisplayButton;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -44,7 +43,7 @@ public ListButton addButton(@NotNull Button... button) {
}

/**
* Should the button keep the current index for the unique id on every {@link #getItem(UUID)} times?
* Should the button keep the current index for the unique id on every {@link #view(UUID)} times?
*
* @return true if it should
*/
Expand All @@ -53,7 +52,7 @@ public boolean isKeepCurrentIndex() {
}

/**
* Should the button keep the current index for the unique id on every {@link #getItem(UUID)} times?
* Should the button keep the current index for the unique id on every {@link #view(UUID)} times?
*
* @param keepCurrentIndex true if it should
*
Expand Down Expand Up @@ -84,45 +83,30 @@ public List<Button> getButtons() {
}

@Override
public Item getItem(@NotNull UUID uuid) {
public void init() {
this.buttons.forEach(Button::init);
}

@Override
public void stop() {
this.buttons.forEach(Button::stop);
}

@Override
public DisplayButton view(@NotNull UUID uuid) {
if (keepCurrentIndex && currentIndexMap.containsKey(uuid)) {
return buttons.get(currentIndexMap.get(uuid)).getItem(uuid);
return buttons.get(currentIndexMap.get(uuid)).view(uuid);
}

for (int i = 0; i < buttons.size(); i++) {
Button button = buttons.get(i);
Item item = button.getItem(uuid);
if (item != null || button.forceSetAction(uuid)) {
DisplayButton item = button.view(uuid);
if (item != null) {
currentIndexMap.put(uuid, i);
return item;
}
}

return null;
}

@Override
public void handleAction(@NotNull ClickEvent event) {
Optional.ofNullable(currentIndexMap.get(event.getViewerID()))
.map(buttons::get)
.ifPresent(button -> button.handleAction(event));
}

@Override
public boolean forceSetAction(@NotNull UUID uuid) {
return Optional.ofNullable(currentIndexMap.get(uuid))
.map(buttons::get)
.map(button -> button.forceSetAction(uuid))
.orElse(false);
}

@Override
public void init() {
this.buttons.forEach(Button::init);
}

@Override
public void stop() {
this.buttons.forEach(Button::stop);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.button.impl;

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 org.jetbrains.annotations.NotNull;

Expand All @@ -9,18 +11,20 @@
/**
* The null button, only with action
*/
public class NullButton extends SimpleButton {
public class NullButton implements Button {
private final Consumer<@NotNull ClickEvent> consumer;

/**
* Create a new button
*
* @param consumer the consumer
*/
public NullButton(@NotNull Consumer<@NotNull ClickEvent> consumer) {
super(uuid -> null, consumer);
this.consumer = consumer;
}

@Override
public boolean forceSetAction(@NotNull UUID uuid) {
return true;
public @NotNull DisplayButton view(@NotNull UUID uuid) {
return new DisplayButton(null, this, consumer);
}
}
Loading

0 comments on commit e8fabb9

Please sign in to comment.