001package net.tnemc.plugincore.core.id; 002 003/* 004 * This class provides UUID-related functions, and has a concept of 005 * "live" UUIDs and "stored" UUIDs. 006 * 007 * live UUIDs are any UUID that is directly associated with a Player account. 008 * 009 * stored UUIDs are any UUID that is generated for use inside the plugin 010 * only. This could be a fake account related to an NPC account, or related 011 * to a name that has never been registered on Minecraft.net. 012 */ 013 014import java.nio.charset.StandardCharsets; 015import java.util.Optional; 016import java.util.UUID; 017 018import static net.tnemc.plugincore.core.utils.PlayerHelper.playerMatcher; 019 020/** 021 * Represents a provider that is used to resolve and store UUID<->Username Pairs. 022 * 023 * @author creatorfromhell 024 * @since 0.1.2.0 025 */ 026public interface UUIDProvider { 027 028 /** 029 * Returns the associated {@link UUIDAPI uuid api} associated with this provider. 030 * 031 * @see UUIDAPI 032 * @return The associated {@link UUIDAPI uuid api} associated with this provider. 033 */ 034 UUIDAPI api(); 035 036 /** 037 * Used to retrieve a {@link UUIDPair}. 038 * 039 * @param name The username of the pair. 040 * @return An optional containing the pair if found, otherwise an empty 041 * optional. 042 */ 043 Optional<UUIDPair> retrieve(final String name); 044 045 /** 046 * Used to retrieve a name from its associated {@link UUID}. 047 * @param id The {@link UUID} to use in the search. 048 * @return An optional containing the name if found, otherwise an empty optional. 049 */ 050 Optional<String> retrieveName(final UUID id); 051 052 /** 053 * Used to store a Username & UUID pair. This could be to a map, or to 054 * a database for persistent usage or to both. 055 * @param pair The {@link UUIDPair} 056 */ 057 void store(final UUIDPair pair); 058 059 default UUID generateOffline(final String name) { 060 return UUID.nameUUIDFromBytes(("Offline:" + name).getBytes(StandardCharsets.UTF_8)); 061 } 062 063 /** 064 * Used to determine if a string is a valid minecraft username or not. 065 * @param name The name to check. 066 * @return True if the name is a valid minecraft username, otherwise false. 067 */ 068 static boolean validate(final String name) { 069 if(name.length() >= 3 && name.length() <= 16) { 070 return playerMatcher().matcher(name).matches(); 071 } 072 return false; 073 } 074 075 /** 076 * Used to determine if a string is a valid UUID. 077 * @param identifier The string to check. 078 * @return True if the name is a valid UUID, otherwise false. 079 */ 080 static boolean isUUID(final String identifier) { 081 try { 082 UUID.fromString(identifier); 083 return true; 084 } catch(Exception ignore) { 085 return false; 086 } 087 } 088}