Add server-free plugin behavior tests

This commit is contained in:
2026-09-26 22:53:58 -07:00
parent ba96b8a73a
commit c760f5ce46
12 changed files with 358 additions and 39 deletions

View File

@@ -0,0 +1,50 @@
package com.majinnaibu.bukkitplugins.metropolis;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
final class CurrentHomesStore {
private CurrentHomesStore() {}
static Map<UUID, Integer> load(File file, Function<String, UUID> legacyOwnerResolver)
throws IOException, InvalidConfigurationException {
Map<UUID, Integer> homesByOwner = new HashMap<>();
if (!file.exists()) {
return homesByOwner;
}
YamlConfiguration yaml = new YamlConfiguration();
yaml.load(file);
for (String ownerKey : yaml.getKeys(false)) {
UUID ownerId;
try {
ownerId = UUID.fromString(ownerKey);
} catch (IllegalArgumentException ex) {
ownerId = legacyOwnerResolver.apply(ownerKey);
}
int homeNumber = yaml.getInt(ownerKey, 0);
if (ownerId != null && homeNumber > 0) {
homesByOwner.put(ownerId, homeNumber);
}
}
return homesByOwner;
}
static void save(File file, Map<UUID, Integer> homesByOwner) throws IOException {
File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}
YamlConfiguration yaml = new YamlConfiguration();
homesByOwner.forEach((ownerId, homeNumber) -> yaml.set(ownerId.toString(), homeNumber));
yaml.save(file);
}
}

View File

@@ -21,8 +21,6 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@@ -302,34 +300,13 @@ public class MetropolisPlugin extends JavaPlugin {
private void loadCurrentHomes() {
File homesFile = new File(getDataFolder(), "currentHomes.yml");
if (!homesFile.exists()) {
return;
}
YamlConfiguration homes = new YamlConfiguration();
try {
homes.load(homesFile);
} catch (IOException | InvalidConfigurationException e) {
getLogger().log(java.util.logging.Level.SEVERE, "Unable to load currentHomes.yml", e);
return;
}
_currentHomes.clear();
boolean migratedLegacyNames = false;
for(String ownerKey : homes.getKeys(false)){
UUID ownerId;
try {
ownerId = UUID.fromString(ownerKey);
} catch (IllegalArgumentException ex) {
ownerId = getServer().getOfflinePlayer(ownerKey).getUniqueId();
migratedLegacyNames = true;
}
int homeNumber = homes.getInt(ownerKey, 0);
if (homeNumber > 0) {
_currentHomes.put(ownerId, homeNumber);
}
}
if (migratedLegacyNames) {
_currentHomes.putAll(CurrentHomesStore.load(
homesFile,
ownerName -> getServer().getOfflinePlayer(ownerName).getUniqueId()));
saveCurrentHomes();
} catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) {
getLogger().log(java.util.logging.Level.SEVERE, "Unable to load currentHomes.yml", e);
}
}
@@ -1095,12 +1072,7 @@ public class MetropolisPlugin extends JavaPlugin {
if (player != null) {
return player;
}
for (Player onlinePlayer : getServer().getOnlinePlayers()) {
if (onlinePlayer.getName().equalsIgnoreCase(name)) {
return onlinePlayer;
}
}
return null;
return PlayerLookup.findOnline(name, getServer().getOnlinePlayers());
}
public OfflinePlayer getOfflinePlayer(String name){
@@ -1167,11 +1139,8 @@ public class MetropolisPlugin extends JavaPlugin {
private void saveCurrentHomes() {
File homesFile = new File(getDataFolder(), "currentHomes.yml");
YamlConfiguration homes = new YamlConfiguration();
_currentHomes.forEach((playerId, homeNumber) -> homes.set(playerId.toString(), homeNumber));
try {
getDataFolder().mkdirs();
homes.save(homesFile);
CurrentHomesStore.save(homesFile, _currentHomes);
} catch (IOException e) {
getLogger().log(java.util.logging.Level.SEVERE, "Unable to save currentHomes.yml", e);
}

View File

@@ -0,0 +1,19 @@
package com.majinnaibu.bukkitplugins.metropolis;
import org.bukkit.entity.Player;
final class PlayerLookup {
private PlayerLookup() {}
static Player findOnline(String requestedName, Iterable<? extends Player> onlinePlayers) {
if (requestedName == null) {
return null;
}
for (Player player : onlinePlayers) {
if (requestedName.equalsIgnoreCase(player.getName())) {
return player;
}
}
return null;
}
}