Skip to content

Commit

Permalink
fix home icons on gui
Browse files Browse the repository at this point in the history
  • Loading branch information
UsainSrht committed Dec 28, 2023
1 parent 1fde640 commit 26f3549
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/main/java/me/usainsrht/uhomes/UHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void onDisable() {
}

public void reload() {
loadConfig();


}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/usainsrht/uhomes/command/SetHomeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public boolean execute(CommandSender sender, String command, String[] args) {
String finalName = name;
CompletableFuture<List<Home>> homesFuture = homeManager.getHomes(uuid);
homesFuture.thenAccept(homes -> {
int homeLimit = homeManager.getHomeLimit(uuid);
if (homes.size() >= homeLimit) {
MessageUtil.send(sender, MainConfig.getMessage("home_limit"), Formatter.number("home_limit", homeLimit));
SoundUtil.play(sender, MainConfig.getSound("home_limit"));
return;
}

if (homes.stream().anyMatch(home -> home.getName() != null && home.getName().equalsIgnoreCase(finalName))) {
MessageUtil.send(sender, MainConfig.getMessage("home_name_already_in_use"), Placeholder.unparsed("home_name", finalName));
SoundUtil.play(sender, MainConfig.getSound("home_name_already_in_use"));
Expand Down
72 changes: 38 additions & 34 deletions src/main/java/me/usainsrht/uhomes/gui/HomesGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
import static net.kyori.adventure.text.format.TextDecoration.State.FALSE;
import static net.kyori.adventure.text.format.TextDecoration.State.NOT_SET;

public class HomesGUI {
Expand Down Expand Up @@ -65,46 +67,48 @@ public static void open(Player player, List<Home> homes, int size, int maxHomes)

int i = 0;
for (Home home : homes) {
Location location = home.getLocation();
ConfigurationSection defaultIcon = MainConfig.getDefaultHomeItem();
TagResolver[] placeholders = new TagResolver[8];
placeholders[0] = Formatter.number("x", location.getX());
placeholders[1] = Formatter.number("y", location.getY());
placeholders[2] = Formatter.number("z", location.getZ());
placeholders[3] = Placeholder.parsed("world", MainConfig.getWorldName(location.getWorld().getName()));
placeholders[4] = Formatter.date("created", LocalDateTime.from(Instant.ofEpochMilli(home.getCreated())));
// date formatter with fallback because of LastTeleport can be -1 (haven't teleported yet)
placeholders[5] = MMUtil.date("last_teleport", LocalDateTime.from(Instant.ofEpochMilli(home.getLastTeleport())));
placeholders[6] = Placeholder.parsed("index", String.valueOf(i));
placeholders[7] = Placeholder.parsed("name",
home.getName() == null ? MainConfig.getUnnamedHomeName().replace("<index>", String.valueOf(i)) : home.getName());

ItemStack icon;
if (home.getIcon() != null) {
icon = home.getIcon();
ItemMeta iconMeta = icon.getItemMeta();

Component displayName = MiniMessage.miniMessage().deserialize(defaultIcon.getString("name"), placeholders).decorationIfAbsent(ITALIC, NOT_SET);
iconMeta.displayName(displayName);

List<Component> lore = new ArrayList<>();
for (String line : defaultIcon.getStringList("lore")) {
lore.add(MiniMessage.miniMessage().deserialize(line, placeholders).decorationIfAbsent(ITALIC, NOT_SET));
}
iconMeta.lore(lore);
} else {
icon = ItemUtil.getItemFromYaml(defaultIcon, placeholders);
}

inventory.setItem(getSlot(i), icon);

inventory.setItem(getSlot(i), getButton(home, i));
i++;
}
player.openInventory(inventory);
}

public static int getSlot(int index) {
return index < 8 ? 9+index : (index < 15 ? 11+index : (index < 22 ? 13+index : 15+index));
return index < 8 ? 10+index : (index < 15 ? 12+index : (index < 22 ? 14+index : 16+index));
}

public static ItemStack getButton(Home home, int index) {
Location location = home.getLocation();
ConfigurationSection defaultIcon = MainConfig.getDefaultHomeItem();
TagResolver[] placeholders = new TagResolver[8];
placeholders[0] = Formatter.number("x", location.getX());
placeholders[1] = Formatter.number("y", location.getY());
placeholders[2] = Formatter.number("z", location.getZ());
placeholders[3] = Placeholder.parsed("world", MainConfig.getWorldName(location.getWorld().getName()));
placeholders[4] = Formatter.date("created", LocalDateTime.ofInstant(Instant.ofEpochMilli(home.getCreated()), ZoneOffset.UTC));
// date formatter with fallback because of LastTeleport can be -1 (haven't teleported yet)
placeholders[5] = MMUtil.date("last_teleport", LocalDateTime.ofInstant(Instant.ofEpochMilli(home.getLastTeleport()), ZoneOffset.UTC));
placeholders[6] = Placeholder.parsed("index", String.valueOf(index));
placeholders[7] = Placeholder.parsed("name",
home.getName() == null ? MainConfig.getUnnamedHomeName().replace("<index>", String.valueOf(index)) : home.getName());

ItemStack icon;
if (home.getIcon() != null) {
icon = home.getIcon();
ItemMeta iconMeta = icon.getItemMeta();

Component displayName = MiniMessage.miniMessage().deserialize(defaultIcon.getString("name"), placeholders).decorationIfAbsent(ITALIC, FALSE);
iconMeta.displayName(displayName);

List<Component> lore = new ArrayList<>();
for (String line : defaultIcon.getStringList("lore")) {
lore.add(MiniMessage.miniMessage().deserialize(line, placeholders).decorationIfAbsent(ITALIC, FALSE));
}
iconMeta.lore(lore);
} else {
icon = ItemUtil.getItemFromYaml(defaultIcon, placeholders);
}
return icon;
}

}
5 changes: 3 additions & 2 deletions src/main/java/me/usainsrht/uhomes/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;

import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
import static net.kyori.adventure.text.format.TextDecoration.State.FALSE;
import static net.kyori.adventure.text.format.TextDecoration.State.NOT_SET;

public class ItemUtil {
Expand All @@ -30,15 +31,15 @@ public static ItemStack getItemFromYaml(ConfigurationSection config, TagResolver

if (config.isSet("name")) {
String name = config.getString("name");
Component parsedName = MiniMessage.miniMessage().deserialize(name, placeholders).decorationIfAbsent(ITALIC, NOT_SET);
Component parsedName = MiniMessage.miniMessage().deserialize(name, placeholders).decorationIfAbsent(ITALIC, FALSE);
itemMeta.displayName(parsedName);
}

if (config.isSet("lore")) {
List<String> lore = config.getStringList("lore");
List<Component> parsedLore = new ArrayList<>();
for (String line : lore) {
parsedLore.add(MiniMessage.miniMessage().deserialize(line, placeholders).decorationIfAbsent(ITALIC, NOT_SET));
parsedLore.add(MiniMessage.miniMessage().deserialize(line, placeholders).decorationIfAbsent(ITALIC, FALSE));
}
itemMeta.lore(parsedLore);
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/me/usainsrht/uhomes/util/MMUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class MMUtil {

public static Component PARSE_ERROR = Component.text("! PARSE ERROR !");

public static @NotNull TagResolver date(@TagPattern final @NotNull String key, final @NotNull TemporalAccessor time) {
public static @NotNull TagResolver date(@TagPattern final @NotNull String key, final @NotNull LocalDateTime time) {
return TagResolver.resolver(key, (argumentQueue, context) -> {
String format = argumentQueue.popOr("Format expected.").value();
String fallback = argumentQueue.hasNext() ? argumentQueue.pop().value() : null;
return Tag.inserting(
context.deserialize(
(Instant.from(time).toEpochMilli() != -1 || fallback == null)
? DateTimeFormatter.ofPattern(format).format(time)
: fallback
)
);
return Tag.inserting(context.deserialize(
(time.toEpochSecond(ZoneOffset.UTC) != -1 || fallback == null)
? DateTimeFormatter.ofPattern(format).format(time)
: fallback));
});
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/me/usainsrht/uhomes/util/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.usainsrht.uhomes.util;

import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Collection;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ gui:
- " <gold><created:'yyyy-MM-dd HH:mm:ss'> "
- ""
- " <white>Last Teleport: "
- " <gold><last_teleport:'yyyy-MM-dd HH:mm:ss'> "
- " <gold><last_teleport:'yyyy-MM-dd HH:mm:ss':'<red>Not teleported yet'> "
- ""
- " <yellow>Left Click to teleport "
- " <yellow>Right Click to rename "
Expand Down

0 comments on commit 26f3549

Please sign in to comment.