|
| 1 | +package ch.njol.skript.test.utils; |
| 2 | + |
| 3 | +import ch.njol.skript.test.runner.TestMode; |
| 4 | +import com.destroystokyo.paper.profile.PlayerProfile; |
| 5 | +import com.destroystokyo.paper.profile.ProfileProperty; |
| 6 | +import io.papermc.paper.persistence.PersistentDataContainerView; |
| 7 | +import org.bukkit.*; |
| 8 | +import org.bukkit.entity.EntityType; |
| 9 | +import org.bukkit.entity.Player; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jspecify.annotations.Nullable; |
| 12 | + |
| 13 | +import java.time.Duration; |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.Date; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +/** |
| 20 | + * A wrapper for an {@link OfflinePlayer} and a custom {@link PlayerProfile}. |
| 21 | + * Allows having a valid {@link OfflinePlayer} object without the need to do any lookups, especially if Mojang authorization is down. |
| 22 | + */ |
| 23 | +public class TestOfflinePlayer implements OfflinePlayer { |
| 24 | + |
| 25 | + private static final String PLAYER_NAME = "SkriptLang"; |
| 26 | + private static final UUID PLAYER_UUID = UUID.randomUUID(); |
| 27 | + private static final OfflinePlayer PLAYER = Bukkit.getOfflinePlayer(PLAYER_UUID); |
| 28 | + private static final PlayerProfile PLAYER_PROFILE = PLAYER.getPlayerProfile(); |
| 29 | + private static final @Nullable TestOfflinePlayer instance; |
| 30 | + |
| 31 | + static { |
| 32 | + if (TestMode.ENABLED) { |
| 33 | + instance = new TestOfflinePlayer(); |
| 34 | + PLAYER_PROFILE.setProperty(new ProfileProperty( |
| 35 | + "textures", |
| 36 | + "ewogICJ0aW1lc3RhbXAiIDogMTc0NzQyOTg2MTQwOCwKICAicHJvZmlsZUlkIiA6ICI2OWUzNzAyNjJjN2Q0MjU1YWM3NjliMTNhNWZlOGY3NCIsCiAgInByb2ZpbGVOYW1lIiA6ICJTYWh2ZGUiLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTE2MGFiZWVhNDI1YzZmODMyYjc0NmE0NTQ0YzVmYjlhOTgxYjAyZTFiZDg1ZmVhNWM3ZWY4MzFiZGM4NzRmMyIKICAgIH0KICB9Cn0=" |
| 37 | + )); |
| 38 | + } else { |
| 39 | + instance = null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private TestOfflinePlayer() {} |
| 44 | + |
| 45 | + public static @Nullable TestOfflinePlayer getInstance() { |
| 46 | + return instance; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public @Nullable String getName() { |
| 51 | + return PLAYER_NAME; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public UUID getUniqueId() { |
| 56 | + return PLAYER_UUID; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isOnline() { |
| 61 | + return PLAYER.isOnline(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean isConnected() { |
| 66 | + return PLAYER.isConnected(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public PlayerProfile getPlayerProfile() { |
| 71 | + return PLAYER_PROFILE; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean isBanned() { |
| 76 | + return PLAYER.isBanned(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Date expires, @Nullable String source) { |
| 81 | + return PLAYER.ban(reason, expires, source); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Instant expires, @Nullable String source) { |
| 86 | + return PLAYER.ban(reason, expires, source); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Duration duration, @Nullable String source) { |
| 91 | + return PLAYER.ban(reason, duration, source); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isWhitelisted() { |
| 96 | + return PLAYER.isWhitelisted(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void setWhitelisted(boolean value) { |
| 101 | + PLAYER.setWhitelisted(value); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public @Nullable Player getPlayer() { |
| 106 | + return PLAYER.getPlayer(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public long getFirstPlayed() { |
| 111 | + return PLAYER.getFirstPlayed(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public long getLastPlayed() { |
| 116 | + return PLAYER.getLastPlayed(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean hasPlayedBefore() { |
| 121 | + return PLAYER.hasPlayedBefore(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public long getLastLogin() { |
| 126 | + return PLAYER.getLastLogin(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public long getLastSeen() { |
| 131 | + return PLAYER.getLastSeen(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public @Nullable Location getRespawnLocation(boolean loadLocationAndValidate) { |
| 136 | + return PLAYER.getRespawnLocation(loadLocationAndValidate); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void incrementStatistic(Statistic statistic) throws IllegalArgumentException { |
| 141 | + PLAYER.incrementStatistic(statistic); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void decrementStatistic(Statistic statistic) throws IllegalArgumentException { |
| 146 | + PLAYER.decrementStatistic(statistic); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void incrementStatistic(Statistic statistic, int amount) throws IllegalArgumentException { |
| 151 | + PLAYER.incrementStatistic(statistic, amount); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void decrementStatistic(Statistic statistic, int amount) throws IllegalArgumentException { |
| 156 | + PLAYER.decrementStatistic(statistic, amount); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void setStatistic(Statistic statistic, int newValue) throws IllegalArgumentException { |
| 161 | + PLAYER.setStatistic(statistic, newValue); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int getStatistic(Statistic statistic) throws IllegalArgumentException { |
| 166 | + return PLAYER.getStatistic(statistic); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void incrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException { |
| 171 | + PLAYER.incrementStatistic(statistic, material); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void decrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException { |
| 176 | + PLAYER.decrementStatistic(statistic, material); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int getStatistic(Statistic statistic, Material material) throws IllegalArgumentException { |
| 181 | + return PLAYER.getStatistic(statistic, material); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void incrementStatistic(Statistic statistic, Material material, int amount) throws IllegalArgumentException { |
| 186 | + PLAYER.incrementStatistic(statistic, material, amount); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void decrementStatistic(Statistic statistic, Material material, int amount) throws IllegalArgumentException { |
| 191 | + PLAYER.decrementStatistic(statistic, material, amount); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void setStatistic(Statistic statistic, Material material, int newValue) throws IllegalArgumentException { |
| 196 | + PLAYER.setStatistic(statistic, material, newValue); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void incrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException { |
| 201 | + PLAYER.incrementStatistic(statistic, entityType); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void decrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException { |
| 206 | + PLAYER.decrementStatistic(statistic, entityType); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public int getStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException { |
| 211 | + return PLAYER.getStatistic(statistic, entityType); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void incrementStatistic(Statistic statistic, EntityType entityType, int amount) throws IllegalArgumentException { |
| 216 | + PLAYER.incrementStatistic(statistic, entityType, amount); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void decrementStatistic(Statistic statistic, EntityType entityType, int amount) { |
| 221 | + PLAYER.decrementStatistic(statistic, entityType, amount); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public void setStatistic(Statistic statistic, EntityType entityType, int newValue) { |
| 226 | + PLAYER.setStatistic(statistic, entityType, newValue); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public @Nullable Location getLastDeathLocation() { |
| 231 | + return PLAYER.getLastDeathLocation(); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public @Nullable Location getLocation() { |
| 236 | + return PLAYER.getLocation(); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public PersistentDataContainerView getPersistentDataContainer() { |
| 241 | + return PLAYER.getPersistentDataContainer(); |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public @NotNull Map<String, Object> serialize() { |
| 246 | + return PLAYER.serialize(); |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public boolean isOp() { |
| 251 | + return PLAYER.isOp(); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public void setOp(boolean value) { |
| 256 | + PLAYER.setOp(value); |
| 257 | + } |
| 258 | + |
| 259 | +} |
0 commit comments