|
| 1 | +package com.skyblockexp.ezrtp.gui; |
| 2 | + |
| 3 | +import com.skyblockexp.ezrtp.config.EzRtpConfiguration; |
| 4 | +import com.skyblockexp.ezrtp.message.MessageProvider; |
| 5 | +import com.skyblockexp.ezrtp.network.NetworkService; |
| 6 | +import com.skyblockexp.ezrtp.platform.PlatformGuiBridge; |
| 7 | +import com.skyblockexp.ezrtp.platform.PlatformGuiBridgeRegistry; |
| 8 | +import com.skyblockexp.ezrtp.storage.RtpUsageStorage; |
| 9 | +import net.kyori.adventure.text.Component; |
| 10 | +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; |
| 11 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 12 | +import org.bukkit.entity.Player; |
| 13 | +import org.bukkit.inventory.Inventory; |
| 14 | +import org.bukkit.inventory.InventoryHolder; |
| 15 | +import org.bukkit.inventory.ItemStack; |
| 16 | +import org.bukkit.inventory.meta.ItemMeta; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +class RandomTeleportGuiManagerReloadConfigTest { |
| 31 | + |
| 32 | + @AfterEach |
| 33 | + void resetGuiBridgeRegistry() { |
| 34 | + PlatformGuiBridgeRegistry.unregister(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void openSelectionUsesLatestGuiSettingsAfterConfigurationReplacement() { |
| 39 | + RecordingGuiBridge bridge = new RecordingGuiBridge(); |
| 40 | + PlatformGuiBridgeRegistry.register(bridge); |
| 41 | + |
| 42 | + AtomicReference<EzRtpConfiguration> configurationRef = |
| 43 | + new AtomicReference<>(createConfiguration("First GUI", 1)); |
| 44 | + |
| 45 | + RtpUsageStorage usageStorage = mock(RtpUsageStorage.class); |
| 46 | + MessageProvider messageProvider = mock(MessageProvider.class); |
| 47 | + NetworkService networkService = mock(NetworkService.class); |
| 48 | + Player player = mock(Player.class); |
| 49 | + Inventory inventory = mock(Inventory.class); |
| 50 | + when(player.openInventory(inventory)).thenReturn(null); |
| 51 | + |
| 52 | + RandomTeleportGuiManager manager = new RandomTeleportGuiManager( |
| 53 | + null, |
| 54 | + () -> null, |
| 55 | + configurationRef::get, |
| 56 | + () -> networkService, |
| 57 | + () -> messageProvider, |
| 58 | + usageStorage); |
| 59 | + |
| 60 | + boolean firstOpened = manager.openSelection(player); |
| 61 | + |
| 62 | + configurationRef.set(createConfiguration("Second GUI", 2)); |
| 63 | + |
| 64 | + boolean secondOpened = manager.openSelection(player); |
| 65 | + |
| 66 | + assertTrue(firstOpened); |
| 67 | + assertTrue(secondOpened); |
| 68 | + assertEquals(List.of(9, 18), bridge.recordedSizes()); |
| 69 | + assertEquals(List.of("First GUI", "Second GUI"), bridge.recordedTitles()); |
| 70 | + } |
| 71 | + |
| 72 | + private static EzRtpConfiguration createConfiguration(String title, int rows) { |
| 73 | + YamlConfiguration base = new YamlConfiguration(); |
| 74 | + base.set("world", "world"); |
| 75 | + |
| 76 | + YamlConfiguration gui = new YamlConfiguration(); |
| 77 | + gui.set("enabled", true); |
| 78 | + gui.set("title", title); |
| 79 | + gui.set("rows", rows); |
| 80 | + gui.set("worlds.overworld.slot", 0); |
| 81 | + gui.set("worlds.overworld.settings.world", "world"); |
| 82 | + |
| 83 | + return EzRtpConfiguration.fromConfigurations( |
| 84 | + base, |
| 85 | + null, |
| 86 | + gui, |
| 87 | + null, |
| 88 | + null, |
| 89 | + Logger.getLogger(RandomTeleportGuiManagerReloadConfigTest.class.getSimpleName())); |
| 90 | + } |
| 91 | + |
| 92 | + private static final class RecordingGuiBridge implements PlatformGuiBridge { |
| 93 | + private final List<String> recordedTitles = new ArrayList<>(); |
| 94 | + private final List<Integer> recordedSizes = new ArrayList<>(); |
| 95 | + |
| 96 | + @Override |
| 97 | + public Inventory createInventory(InventoryHolder holder, int size, Component title) { |
| 98 | + recordedSizes.add(size); |
| 99 | + recordedTitles.add(PlainTextComponentSerializer.plainText().serialize(title)); |
| 100 | + return mock(Inventory.class); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setDisplayName(ItemMeta meta, Component displayName) { |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void setLore(ItemMeta meta, List<Component> lore) { |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void applyItemMeta(ItemStack icon, ItemMeta meta) { |
| 113 | + } |
| 114 | + |
| 115 | + private List<String> recordedTitles() { |
| 116 | + return recordedTitles; |
| 117 | + } |
| 118 | + |
| 119 | + private List<Integer> recordedSizes() { |
| 120 | + return recordedSizes; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments