mirror of
https://github.com/kiranhart/Auction-House.git
synced 2025-03-12 13:19:20 +01:00
🔨 made head selector async
This commit is contained in:
parent
d2fb062ae3
commit
a30a724cf7
@ -18,6 +18,7 @@
|
||||
|
||||
package ca.tweetzy.auctionhouse.guis;
|
||||
|
||||
import ca.tweetzy.auctionhouse.AuctionHouse;
|
||||
import ca.tweetzy.auctionhouse.settings.Settings;
|
||||
import ca.tweetzy.core.gui.BaseGUI;
|
||||
import ca.tweetzy.core.gui.Gui;
|
||||
@ -27,6 +28,7 @@ import ca.tweetzy.flight.hooks.PlaceholderAPIHook;
|
||||
import ca.tweetzy.flight.utils.QuickItem;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@ -40,6 +42,9 @@ public abstract class AuctionPagedGUI<T> extends BaseGUI {
|
||||
protected final Gui parent;
|
||||
protected List<T> items;
|
||||
|
||||
@Setter
|
||||
protected boolean async = false;
|
||||
|
||||
public AuctionPagedGUI(Gui parent, @NonNull final Player player, @NonNull String title, int rows, @NonNull List<T> items) {
|
||||
super(parent, PlaceholderAPIHook.tryReplace(player, title), rows);
|
||||
this.parent = parent;
|
||||
@ -68,21 +73,47 @@ public abstract class AuctionPagedGUI<T> extends BaseGUI {
|
||||
|
||||
private void populateItems() {
|
||||
if (this.items != null) {
|
||||
this.fillSlots().forEach(slot -> setItem(slot, getDefaultItem()));
|
||||
prePopulate();
|
||||
if (!this.async) {
|
||||
renderItems();
|
||||
} else {
|
||||
AuctionHouse.newChain().asyncFirst(() -> {
|
||||
this.fillSlots().forEach(slot -> setItem(slot, getDefaultItem()));
|
||||
prePopulate();
|
||||
|
||||
final List<T> itemsToFill = this.items.stream().skip((page - 1) * (long) this.fillSlots().size()).limit(this.fillSlots().size()).collect(Collectors.toList());
|
||||
pages = (int) Math.max(1, Math.ceil(this.items.size() / (double) this.fillSlots().size()));
|
||||
return this.items.stream().skip((page - 1) * (long) this.fillSlots().size()).limit(this.fillSlots().size()).collect(Collectors.toList());
|
||||
}).asyncLast((data) -> {
|
||||
pages = (int) Math.max(1, Math.ceil(this.items.size() / (double) this.fillSlots().size()));
|
||||
|
||||
setPrevPage(getPreviousButtonSlot(), getPreviousButton());
|
||||
setNextPage(getNextButtonSlot(), getNextButton());
|
||||
setOnPage(e -> draw());
|
||||
setPrevPage(getPreviousButtonSlot(), getPreviousButton());
|
||||
setNextPage(getNextButtonSlot(), getNextButton());
|
||||
setOnPage(e -> draw());
|
||||
|
||||
for (int i = 0; i < this.rows * 9; i++) {
|
||||
if (this.fillSlots().contains(i) && this.fillSlots().indexOf(i) < itemsToFill.size()) {
|
||||
final T object = itemsToFill.get(this.fillSlots().indexOf(i));
|
||||
setButton(i, this.makeDisplayItem(object), click -> this.onClick(object, click));
|
||||
}
|
||||
for (int i = 0; i < this.rows * 9; i++) {
|
||||
if (this.fillSlots().contains(i) && this.fillSlots().indexOf(i) < data.size()) {
|
||||
final T object = data.get(this.fillSlots().indexOf(i));
|
||||
setButton(i, this.makeDisplayItem(object), click -> this.onClick(object, click));
|
||||
}
|
||||
}
|
||||
}).execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void renderItems() {
|
||||
this.fillSlots().forEach(slot -> setItem(slot, getDefaultItem()));
|
||||
prePopulate();
|
||||
|
||||
final List<T> itemsToFill = this.items.stream().skip((page - 1) * (long) this.fillSlots().size()).limit(this.fillSlots().size()).collect(Collectors.toList());
|
||||
pages = (int) Math.max(1, Math.ceil(this.items.size() / (double) this.fillSlots().size()));
|
||||
|
||||
setPrevPage(getPreviousButtonSlot(), getPreviousButton());
|
||||
setNextPage(getNextButtonSlot(), getNextButton());
|
||||
setOnPage(e -> draw());
|
||||
|
||||
for (int i = 0; i < this.rows * 9; i++) {
|
||||
if (this.fillSlots().contains(i) && this.fillSlots().indexOf(i) < itemsToFill.size()) {
|
||||
final T object = itemsToFill.get(this.fillSlots().indexOf(i));
|
||||
setButton(i, this.makeDisplayItem(object), click -> this.onClick(object, click));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,14 +3,20 @@ package ca.tweetzy.auctionhouse.guis.selector;
|
||||
import ca.tweetzy.auctionhouse.guis.AuctionPagedGUI;
|
||||
import ca.tweetzy.auctionhouse.settings.Settings;
|
||||
import ca.tweetzy.core.gui.events.GuiClickEvent;
|
||||
import ca.tweetzy.flight.comp.enums.CompMaterial;
|
||||
import ca.tweetzy.flight.utils.QuickItem;
|
||||
import ca.tweetzy.flight.utils.profiles.builder.XSkull;
|
||||
import ca.tweetzy.flight.utils.profiles.objects.ProfileInputType;
|
||||
import ca.tweetzy.flight.utils.profiles.objects.Profileable;
|
||||
import lombok.NonNull;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class GUIPlayerSelector extends AuctionPagedGUI<OfflinePlayer> {
|
||||
@ -18,24 +24,54 @@ public final class GUIPlayerSelector extends AuctionPagedGUI<OfflinePlayer> {
|
||||
private final Consumer<OfflinePlayer> selectedPlayer;
|
||||
|
||||
public GUIPlayerSelector(@NonNull final Player player, @NonNull final Consumer<OfflinePlayer> selectedPlayer) {
|
||||
super(null, player, Settings.GUI_PLAYER_SELECTOR_TITLE.getString(), 6, Arrays.asList(Bukkit.getOfflinePlayers()));
|
||||
super(null, player, Settings.GUI_PLAYER_SELECTOR_TITLE.getString(), 6, new ArrayList<>());
|
||||
setAsync(true);
|
||||
this.selectedPlayer = selectedPlayer;
|
||||
draw();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prePopulate() {
|
||||
this.items.addAll(getOnlineOfflinePlayers());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemStack makeDisplayItem(OfflinePlayer player) {
|
||||
if (player == null || !player.hasPlayedBefore()) {
|
||||
return QuickItem.of("http://textures.minecraft.net/texture/ee7700096b5a2a87386d6205b4ddcc14fd33cf269362fa6893499431ce77bf9").name("&eUnknown Player").make();
|
||||
}
|
||||
|
||||
final String name = player.hasPlayedBefore() && player.getName() != null ? player.getName() : "Unknown Name o.O";
|
||||
|
||||
return QuickItem
|
||||
.of(player)
|
||||
QuickItem item = QuickItem
|
||||
.of(CompMaterial.PLAYER_HEAD)
|
||||
.name(Settings.GUI_PLAYER_SELECTOR_ITEMS_PLAYER_NAME.getString().replace("%player_name%", name))
|
||||
.lore(this.player, Settings.GUI_PLAYER_SELECTOR_ITEMS_PLAYER_LORE.getStringList())
|
||||
.make();
|
||||
.lore(this.player, Settings.GUI_PLAYER_SELECTOR_ITEMS_PLAYER_LORE.getStringList());
|
||||
|
||||
|
||||
return XSkull
|
||||
.of(item.make())
|
||||
.profile(Profileable.of(player))
|
||||
.fallback(Profileable.of(
|
||||
ProfileInputType.TEXTURE_URL,
|
||||
"http://textures.minecraft.net/texture/ee7700096b5a2a87386d6205b4ddcc14fd33cf269362fa6893499431ce77bf9"
|
||||
))
|
||||
.lenient()
|
||||
.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClick(OfflinePlayer player, GuiClickEvent click) {
|
||||
this.selectedPlayer.accept(player);
|
||||
}
|
||||
|
||||
private List<OfflinePlayer> getOnlineOfflinePlayers() {
|
||||
final List<OfflinePlayer> players = new ArrayList<>(Arrays.asList(Bukkit.getOfflinePlayers()));
|
||||
Bukkit.getOnlinePlayers().forEach(player -> {
|
||||
if (players.stream().anyMatch(target -> target.getUniqueId().equals(player.getUniqueId()))) return;
|
||||
players.add(player);
|
||||
});
|
||||
|
||||
return players;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user