Skip to content

Commit

Permalink
Add support for inventory title placeholders (#229)
Browse files Browse the repository at this point in the history
* Add support for inventory title placeholders

* Fix NoItemGUI usage

* Moved where island is stored

Co-authored-by: Peaches_MLG <[email protected]>
  • Loading branch information
dlsf and PeachesMLG authored Jul 2, 2021
1 parent 9ae94c9 commit 8025ea6
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
@NoArgsConstructor
@AllArgsConstructor
public class NoItemGUI {

/**
* The size of the GUI
* The size of the GUI.
*/
public int size;

/**
* The title of the GUI
* The title of the GUI.
*/
public String title;

/**
* The background of the GUI
* The background of the GUI.
*/
public Background background;

}
7 changes: 2 additions & 5 deletions src/main/java/com/iridium/iridiumskyblock/gui/BankGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
*/
public class BankGUI extends GUI {

private final Island island;

/**
* The default constructor.
*
* @param island The Island this GUI belongs to
*/
public BankGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().bankGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().bankGUI, island);
}

@Override
Expand All @@ -38,7 +35,7 @@ public void addContent(Inventory inventory) {
InventoryUtils.fillInventory(inventory, getNoItemGUI().background);

for (BankItem bankItem : IridiumSkyblock.getInstance().getBankItemList()) {
IslandBank islandBank = IridiumSkyblock.getInstance().getIslandManager().getIslandBank(island, bankItem);
IslandBank islandBank = IridiumSkyblock.getInstance().getIslandManager().getIslandBank(getIsland(), bankItem);
inventory.setItem(bankItem.getItem().slot, ItemStackUtils.makeItem(bankItem.getItem(), Collections.singletonList(new Placeholder("amount", bankItem.toString(islandBank.getNumber())))));
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/iridium/iridiumskyblock/gui/BiomeGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ public class BiomeGUI extends GUI {

private final List<XBiome> biomes;
private final int page;
private final Island island;
private final World.Environment environment;
private final CooldownProvider<CommandSender> cooldownProvider;

public BiomeGUI(int page, Island island, World.Environment environment, CooldownProvider<CommandSender> cooldownProvider) {
super(IridiumSkyblock.getInstance().getInventories().biomeGUI);
super(IridiumSkyblock.getInstance().getInventories().biomeGUI, island);
this.biomes = XBiome.VALUES.stream().filter(biome -> biome.getEnvironment() == environment).collect(Collectors.toList());
this.environment = environment;
this.page = page;
this.island = island;
this.cooldownProvider = cooldownProvider;
}

Expand Down Expand Up @@ -64,12 +62,12 @@ public void onInventoryClick(InventoryClickEvent event) {

Player player = (Player) event.getWhoClicked();
if (event.getSlot() == size - 7 && page > 1) {
player.openInventory(new BiomeGUI(page - 1, this.island, environment, cooldownProvider).getInventory());
player.openInventory(new BiomeGUI(page - 1, getIsland(), environment, cooldownProvider).getInventory());
return;
}

if (event.getSlot() == size - 3 && (size - 9) * page < biomes.size()) {
player.openInventory(new BiomeGUI(page + 1, this.island, environment, cooldownProvider).getInventory());
player.openInventory(new BiomeGUI(page + 1, getIsland(), environment, cooldownProvider).getInventory());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BlockValueGUI extends GUI {
* @param type The type of valuable block shown in this GUI
*/
public BlockValueGUI(BlockValueType type) {
super(IridiumSkyblock.getInstance().getInventories().blockValue);
super(IridiumSkyblock.getInstance().getInventories().blockValue, null);
this.guiType = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
*/
public class BoostersGUI extends GUI {

private final Island island;

/**
* The default constructor.
*
* @param island The Island this GUI belongs to
*/
public BoostersGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().boostersGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().boostersGUI, island);
}

@Override
Expand All @@ -42,7 +39,7 @@ public void addContent(Inventory inventory) {

for (Map.Entry<String, Booster> entry : IridiumSkyblock.getInstance().getBoosterList().entrySet()) {
Item item = entry.getValue().item;
IslandBooster islandBooster = IridiumSkyblock.getInstance().getIslandManager().getIslandBooster(island, entry.getKey());
IslandBooster islandBooster = IridiumSkyblock.getInstance().getIslandManager().getIslandBooster(getIsland(), entry.getKey());
long minutes = LocalDateTime.now().until(islandBooster.getTime(), ChronoUnit.MINUTES);
long seconds = LocalDateTime.now().until(islandBooster.getTime(), ChronoUnit.SECONDS) - minutes * 60;
inventory.setItem(item.slot, ItemStackUtils.makeItem(item, Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ConfirmationGUI extends GUI {
* @param cooldownProvider The provider for cooldowns that should be started on success
*/
public ConfirmationGUI(@NotNull Runnable runnable, @NotNull CooldownProvider<CommandSender> cooldownProvider) {
super(IridiumSkyblock.getInstance().getInventories().confirmationGUI);
super(IridiumSkyblock.getInstance().getInventories().confirmationGUI, null);
this.runnable = runnable;
this.cooldownProvider = cooldownProvider;
}
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/com/iridium/iridiumskyblock/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@

import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.PlaceholderBuilder;
import com.iridium.iridiumskyblock.configs.inventories.NoItemGUI;
import lombok.AllArgsConstructor;
import com.iridium.iridiumskyblock.database.Island;
import lombok.NoArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents a clickable GUI.
* Base for all other classes in this package.
*/
@AllArgsConstructor
@NoArgsConstructor
public abstract class GUI implements InventoryHolder {

private NoItemGUI noItemGUI;
private @Nullable Island island;

/**
* The default constructor.
*
* @param noItemGUI The NoItemGUI of this GUI
* @param island The island of this GUI. Can be null
*/
public GUI(@NotNull NoItemGUI noItemGUI, @Nullable Island island) {
this.noItemGUI = noItemGUI;
this.island = island;
}

@NotNull
@Override
public Inventory getInventory() {
Inventory inventory = Bukkit.createInventory(this, noItemGUI.size, StringUtils.color(noItemGUI.title));
String title = island == null ? noItemGUI.title : StringUtils.processMultiplePlaceholders(noItemGUI.title, new PlaceholderBuilder().applyIslandPlaceholders(island).build());
Inventory inventory = Bukkit.createInventory(this, noItemGUI.size, StringUtils.color(title));

Bukkit.getScheduler().runTaskAsynchronously(IridiumSkyblock.getInstance(), () -> addContent(inventory));

Expand All @@ -47,4 +61,8 @@ public Inventory getInventory() {
public NoItemGUI getNoItemGUI() {
return noItemGUI;
}

public Island getIsland() {
return island;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class InventoryConfigGUI extends GUI {
private final InventoryConfig inventoryConfig;

public InventoryConfigGUI(InventoryConfig inventoryConfig) {
super(inventoryConfig);
super(inventoryConfig, null);
this.inventoryConfig = inventoryConfig;
}

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/iridium/iridiumskyblock/gui/InvitesGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
public class InvitesGUI extends GUI {

private final Island island;
private final HashMap<Integer, String> invites;

/**
Expand All @@ -30,21 +29,20 @@ public class InvitesGUI extends GUI {
* @param island The Island this GUI belongs to
*/
public InvitesGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().islandInvitesGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().islandInvitesGUI, island);
invites = new HashMap<>();
}

@Override
public void addContent(Inventory inventory) {
List<IslandInvite> islandInvites = IridiumSkyblock.getInstance().getDatabaseManager().getIslandInviteTableManager().getEntries(island);
List<IslandInvite> islandInvites = IridiumSkyblock.getInstance().getDatabaseManager().getIslandInviteTableManager().getEntries(getIsland());
inventory.clear();
InventoryUtils.fillInventory(inventory, getNoItemGUI().background);


int i = 0;
for (IslandInvite islandInvite : islandInvites) {
List<Placeholder> placeholderList = new PlaceholderBuilder().applyPlayerPlaceholders(islandInvite.getUser()).applyIslandPlaceholders(island).build();
List<Placeholder> placeholderList = new PlaceholderBuilder().applyPlayerPlaceholders(islandInvite.getUser()).applyIslandPlaceholders(getIsland()).build();
placeholderList.add(new Placeholder("inviter", islandInvite.getInviter().getName()));
placeholderList.add(new Placeholder("time", islandInvite.getTime().format(DateTimeFormatter.ofPattern(IridiumSkyblock.getInstance().getConfiguration().dateTimeFormat))));
inventory.setItem(i, ItemStackUtils.makeItem(IridiumSkyblock.getInstance().getInventories().islandInvitesGUI.item, placeholderList));
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/iridium/iridiumskyblock/gui/IslandRanksGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@
*/
public class IslandRanksGUI extends GUI {

private final Island island;

/**
* The default constructor.
*
* @param island The Island this GUI belongs to
*/
public IslandRanksGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().islandRanksGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().islandRanksGUI, island);
}

@Override
public void addContent(Inventory inventory) {
inventory.clear();
IslandRanksInventoryConfig islandRanks = IridiumSkyblock.getInstance().getInventories().islandRanksGUI;
InventoryUtils.fillInventory(inventory, islandRanks.background);
List<User> members = IridiumSkyblock.getInstance().getIslandManager().getIslandMembers(island);
List<User> members = IridiumSkyblock.getInstance().getIslandManager().getIslandMembers(getIsland());
inventory.setItem(islandRanks.owner.slot, ItemStackUtils.makeItem(islandRanks.owner,
Collections.singletonList(new Placeholder("members", island.getOwner().getName()))));
Collections.singletonList(new Placeholder("members", getIsland().getOwner().getName()))));
inventory.setItem(islandRanks.coOwner.slot, ItemStackUtils.makeItem(islandRanks.coOwner,
Collections.singletonList(new Placeholder("members", members.stream().filter(member -> member.getIslandRank().equals(IslandRank.CO_OWNER)).map(User::getName).collect(Collectors.joining(", "))))));
inventory.setItem(islandRanks.moderator.slot, ItemStackUtils.makeItem(islandRanks.moderator,
Expand All @@ -61,15 +58,15 @@ public void addContent(Inventory inventory) {
public void onInventoryClick(InventoryClickEvent event) {
IslandRanksInventoryConfig islandRanks = IridiumSkyblock.getInstance().getInventories().islandRanksGUI;
if (event.getSlot() == islandRanks.owner.slot)
event.getWhoClicked().openInventory(new PermissionsGUI(island, IslandRank.OWNER).getInventory());
event.getWhoClicked().openInventory(new PermissionsGUI(getIsland(), IslandRank.OWNER).getInventory());
else if (event.getSlot() == islandRanks.coOwner.slot)
event.getWhoClicked().openInventory(new PermissionsGUI(island, IslandRank.CO_OWNER).getInventory());
event.getWhoClicked().openInventory(new PermissionsGUI(getIsland(), IslandRank.CO_OWNER).getInventory());
else if (event.getSlot() == islandRanks.moderator.slot)
event.getWhoClicked().openInventory(new PermissionsGUI(island, IslandRank.MODERATOR).getInventory());
event.getWhoClicked().openInventory(new PermissionsGUI(getIsland(), IslandRank.MODERATOR).getInventory());
else if (event.getSlot() == islandRanks.member.slot)
event.getWhoClicked().openInventory(new PermissionsGUI(island, IslandRank.MEMBER).getInventory());
event.getWhoClicked().openInventory(new PermissionsGUI(getIsland(), IslandRank.MEMBER).getInventory());
else if (event.getSlot() == islandRanks.visitor.slot)
event.getWhoClicked().openInventory(new PermissionsGUI(island, IslandRank.VISITOR).getInventory());
event.getWhoClicked().openInventory(new PermissionsGUI(getIsland(), IslandRank.VISITOR).getInventory());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class IslandTopGUI extends GUI {
* The default constructor.
*/
public IslandTopGUI() {
super(IridiumSkyblock.getInstance().getInventories().islandTopGUI);
super(IridiumSkyblock.getInstance().getInventories().islandTopGUI, null);
}

@Override
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/iridium/iridiumskyblock/gui/LogsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
*/
public class LogsGUI extends GUI {

private final Island island;

private int membersPage = 1;
private int invitesPage = 1;
private int trustsPage = 1;
Expand All @@ -45,8 +43,7 @@ public class LogsGUI extends GUI {
* @param island The Island this GUI belongs to
*/
public LogsGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().logsGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().logsGUI, island);
}

@Override
Expand All @@ -66,11 +63,11 @@ public void addContent(Inventory inventory) {
}

public void setItemStack(Inventory inventory, Item item, int page, LogAction... logActions) {
ItemStack itemStack = ItemStackUtils.makeItem(item, new PlaceholderBuilder().applyIslandPlaceholders(island).build());
ItemStack itemStack = ItemStackUtils.makeItem(item, new PlaceholderBuilder().applyIslandPlaceholders(getIsland()).build());
ItemMeta itemMeta = itemStack.getItemMeta();
List<String> lore = new ArrayList<>();

List<IslandLog> islandLogs = IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().getEntries(island).stream()
List<IslandLog> islandLogs = IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().getEntries(getIsland()).stream()
.filter(islandLog -> Arrays.stream(logActions).anyMatch(logAction -> logAction.equals(islandLog.getLogAction())))
.sorted(Comparator.comparing(IslandLog::getTime).reversed())
.collect(Collectors.toList());
Expand Down Expand Up @@ -192,7 +189,7 @@ public void onInventoryClick(InventoryClickEvent event) {
}

private boolean canChangePage(int page, int change, LogAction... logActions) {
List<IslandLog> islandLogs = IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().getEntries(island).stream()
List<IslandLog> islandLogs = IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().getEntries(getIsland()).stream()
.filter(islandLog -> Arrays.stream(logActions).anyMatch(logAction -> logAction.equals(islandLog.getLogAction())))
.sorted(Comparator.comparing(IslandLog::getTime).reversed())
.collect(Collectors.toList());
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/iridium/iridiumskyblock/gui/MembersGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
public class MembersGUI extends GUI {

private final Island island;
private final HashMap<Integer, User> members;

/**
Expand All @@ -27,8 +26,7 @@ public class MembersGUI extends GUI {
* @param island The Island this GUI belongs to
*/
public MembersGUI(@NotNull Island island) {
super(IridiumSkyblock.getInstance().getInventories().membersGUI);
this.island = island;
super(IridiumSkyblock.getInstance().getInventories().membersGUI, island);
this.members = new HashMap<>();
}

Expand All @@ -38,8 +36,8 @@ public void addContent(Inventory inventory) {
InventoryUtils.fillInventory(inventory, IridiumSkyblock.getInstance().getInventories().membersGUI.background);

int i = 0;
for (User member : island.getMembers()) {
inventory.setItem(i, ItemStackUtils.makeItem(IridiumSkyblock.getInstance().getInventories().membersGUI.item, new PlaceholderBuilder().applyPlayerPlaceholders(member).applyIslandPlaceholders(island).build()));
for (User member : getIsland().getMembers()) {
inventory.setItem(i, ItemStackUtils.makeItem(IridiumSkyblock.getInstance().getInventories().membersGUI.item, new PlaceholderBuilder().applyPlayerPlaceholders(member).applyIslandPlaceholders(getIsland()).build()));
members.put(i, member);
i++;
}
Expand Down
Loading

0 comments on commit 8025ea6

Please sign in to comment.