Skip to content

Commit

Permalink
Don't mutate lore in config item.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Sep 8, 2024
1 parent 7197215 commit e09dfe4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ private Supplier<GuiItem> toItem(Player player, Locker locker, PaginatedGuiRefre
.toGuiItem(event -> {
if (isLockerSelected) {
this.state.setDestinationLocker(null);
this.sendingGUI.updateDestinationItem(player, null, "");
this.sendingGUI.updateDestinationItem(player, "");
refresher.refresh();
return;
}

this.state.setDestinationLocker(uuid);
this.sendingGUI.updateDestinationItem(player, uuid, locker.description());
this.sendingGUI.updateDestinationItem(player, locker.description());
refresher.refresh();
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
import com.eternalcode.parcellockers.parcel.repository.ParcelRepository;
import com.eternalcode.parcellockers.shared.ExceptionHandler;
import com.eternalcode.parcellockers.user.UserRepository;
import static com.eternalcode.parcellockers.util.AdventureUtil.RESET_ITEM;
import de.rapha149.signgui.SignGUI;
import de.rapha149.signgui.SignGUIAction;
import dev.rollczi.liteskullapi.SkullAPI;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import java.util.ArrayList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand All @@ -31,6 +34,7 @@
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;

public class ParcelSendingGUI extends GuiView {

Expand All @@ -48,9 +52,6 @@ public class ParcelSendingGUI extends GuiView {

private final ParcelSendingGUIState state;

private ConfigItem receiverItem;
private ConfigItem destinationItem;

private Gui gui;

public ParcelSendingGUI(Plugin plugin,
Expand Down Expand Up @@ -153,9 +154,6 @@ public void show(Player player) {
descriptionSignGui.open(player);
});

this.receiverItem = guiSettings.parcelReceiverItem;
this.destinationItem = guiSettings.parcelDestinationLockerItem;

GuiItem storageItem = guiSettings.parcelStorageItem.toGuiItem(event -> {
ParcelItemStorageGUI storageGUI = new ParcelItemStorageGUI(
this.plugin,
Expand Down Expand Up @@ -263,7 +261,7 @@ else if (slotsSize <= 18 && this.state.getSize() == ParcelSize.SMALL) {
this.gui.setItem(14, largeButton.toGuiItem(event -> this.setSelected(this.gui, ParcelSize.LARGE)));
this.gui.setItem(21, nameGuiItem);
this.gui.setItem(22, descriptionGuiItem);
this.gui.setItem(23, this.receiverItem.toGuiItem(event -> new ReceiverSelectionGui(
this.gui.setItem(23, guiSettings.parcelReceiverItem.toGuiItem(event -> new ReceiverSelectionGui(
this.plugin,
this.scheduler,
this.config,
Expand All @@ -274,7 +272,7 @@ else if (slotsSize <= 18 && this.state.getSize() == ParcelSize.SMALL) {
this.state
).show(player)));

this.gui.setItem(30, this.destinationItem.toGuiItem(event -> new DestinationSelectionGUI(
this.gui.setItem(30, guiSettings.destinationLockerItem.toGuiItem(event -> new DestinationSelectionGUI(
this.plugin,
this.scheduler,
this.config,
Expand Down Expand Up @@ -319,36 +317,37 @@ private void setSelected(Gui gui, boolean priority) {
}

public void updateReceiverItem(Player player, String receiverName) {
PluginConfiguration settings = this.config;
PluginConfiguration.GuiSettings guiSettings = settings.guiSettings;
this.announcer.sendMessage(player, this.config.messages.parcelReceiverSet);

if (this.receiverItem.lore.size() > 1) {
this.receiverItem.lore.remove(1);
}

if (receiverName != null) {
this.receiverItem.lore.add(guiSettings.parcelReceiverGuiSetLine.replace("{RECEIVER}", receiverName));
this.receiverItem.setGlow(true);
}
if (receiverName == null || receiverName.isEmpty()) {
this.gui.updateItem(23, this.config.guiSettings.parcelReceiverItem.toItemStack());
return;
}

this.gui.updateItem(23, this.receiverItem.toItemStack());
this.announcer.sendMessage(player, settings.messages.parcelReceiverSet);
String line = this.config.guiSettings.parcelReceiverGuiSetLine.replace("{RECEIVER}", receiverName);
this.gui.updateItem(23, createActiveItem(this.config.guiSettings.parcelReceiverItem, line));
}

public void updateDestinationItem(Player player, UUID destinationLockerUuid, String destinationLockerDesc) {
PluginConfiguration settings = this.config;
PluginConfiguration.GuiSettings guiSettings = settings.guiSettings;
public void updateDestinationItem(Player player, String destinationLockerDesc) {
this.announcer.sendMessage(player, this.config.messages.parcelDestinationSet);

if (this.destinationItem.lore.size() > 1) {
this.destinationItem.lore.remove(1);
if (destinationLockerDesc == null || destinationLockerDesc.isEmpty()) {
this.gui.updateItem(30, this.config.guiSettings.destinationLockerItem.toItemStack());
return;
}

if (destinationLockerDesc != null) {
this.destinationItem.lore.add(guiSettings.parcelDestinationLockerSetLine.replace("{DESCRIPTION}", destinationLockerDesc));
this.destinationItem.setGlow(true);
}
String line = this.config.guiSettings.parcelDestinationLockerSetLine.replace("{DESCRIPTION}", destinationLockerDesc);
this.gui.updateItem(30, createActiveItem(this.config.guiSettings.destinationLockerItem, line));
}

private @NotNull ItemStack createActiveItem(ConfigItem item, String appendLore) {
List<String> itemLore = new ArrayList<>(item.lore);
itemLore.add(appendLore);

this.gui.updateItem(30, this.destinationItem.toItemStack());
this.announcer.sendMessage(player, settings.messages.parcelDestinationSet);
return item.toBuilder()
.lore(itemLore.stream().map(element -> RESET_ITEM.append(miniMessage.deserialize(element))).toList())
.glow(true)
.build();
}

}

0 comments on commit e09dfe4

Please sign in to comment.