mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-19 15:09:19 +00:00
cosmetic user provider impl
This commit is contained in:
@@ -7,9 +7,12 @@ import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics;
|
|||||||
import com.hibiscusmc.hmccosmetics.gui.Menu;
|
import com.hibiscusmc.hmccosmetics.gui.Menu;
|
||||||
import com.hibiscusmc.hmccosmetics.gui.Menus;
|
import com.hibiscusmc.hmccosmetics.gui.Menus;
|
||||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||||
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUserProvider;
|
||||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||||
|
import lombok.Getter;
|
||||||
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
||||||
import org.bukkit.Color;
|
import org.bukkit.Color;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -145,6 +148,16 @@ public final class HMCCosmeticsAPI {
|
|||||||
return CosmeticSlot.register(id);
|
return CosmeticSlot.register(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a new cosmetic user provider to use to construct {@link CosmeticUser}s.
|
||||||
|
*
|
||||||
|
* @param provider the provider to register
|
||||||
|
* @throws IllegalArgumentException if another plugin has already registered a provider
|
||||||
|
*/
|
||||||
|
public static void registerCosmeticUserProvider(final CosmeticUserProvider provider) {
|
||||||
|
CosmeticUsers.registerProvider(provider);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the NMS version of the server as recognized by HMCCosmetics.
|
* Retrieves the NMS version of the server as recognized by HMCCosmetics.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.hibiscusmc.hmccosmetics.listener;
|
package com.hibiscusmc.hmccosmetics.listener;
|
||||||
|
|
||||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||||
|
import com.hibiscusmc.hmccosmetics.api.HMCCosmeticsAPI;
|
||||||
import com.hibiscusmc.hmccosmetics.api.events.PlayerLoadEvent;
|
import com.hibiscusmc.hmccosmetics.api.events.PlayerLoadEvent;
|
||||||
import com.hibiscusmc.hmccosmetics.api.events.PlayerPreLoadEvent;
|
import com.hibiscusmc.hmccosmetics.api.events.PlayerPreLoadEvent;
|
||||||
import com.hibiscusmc.hmccosmetics.api.events.PlayerUnloadEvent;
|
import com.hibiscusmc.hmccosmetics.api.events.PlayerUnloadEvent;
|
||||||
@@ -10,9 +11,11 @@ import com.hibiscusmc.hmccosmetics.database.Database;
|
|||||||
import com.hibiscusmc.hmccosmetics.database.UserData;
|
import com.hibiscusmc.hmccosmetics.database.UserData;
|
||||||
import com.hibiscusmc.hmccosmetics.gui.Menus;
|
import com.hibiscusmc.hmccosmetics.gui.Menus;
|
||||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||||
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUserProvider;
|
||||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||||
import com.hibiscusmc.hmccosmetics.user.manager.UserEmoteManager;
|
import com.hibiscusmc.hmccosmetics.user.manager.UserEmoteManager;
|
||||||
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
|
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@@ -23,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class PlayerConnectionListener implements Listener {
|
public class PlayerConnectionListener implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
@@ -51,7 +55,7 @@ public class PlayerConnectionListener implements Listener {
|
|||||||
Database.get(uuid).thenAccept(data -> {
|
Database.get(uuid).thenAccept(data -> {
|
||||||
if (data == null) return;
|
if (data == null) return;
|
||||||
Bukkit.getScheduler().runTask(HMCCosmeticsPlugin.getInstance(), () -> {
|
Bukkit.getScheduler().runTask(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||||
CosmeticUser cosmeticUser = new CosmeticUser(uuid, data);
|
CosmeticUser cosmeticUser = CosmeticUsers.getProvider().createCosmeticUser(uuid, data);
|
||||||
CosmeticUsers.addUser(cosmeticUser);
|
CosmeticUsers.addUser(cosmeticUser);
|
||||||
MessagesUtil.sendDebugMessages("Run User Join for " + uuid);
|
MessagesUtil.sendDebugMessages("Run User Join for " + uuid);
|
||||||
|
|
||||||
@@ -64,6 +68,9 @@ public class PlayerConnectionListener implements Listener {
|
|||||||
cosmeticUser.updateCosmetic();
|
cosmeticUser.updateCosmetic();
|
||||||
}, 4);
|
}, 4);
|
||||||
});
|
});
|
||||||
|
}).exceptionally(ex -> {
|
||||||
|
log.error("Unable to load Cosmetic User {}", uuid, ex);
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.hibiscusmc.hmccosmetics.user;
|
||||||
|
|
||||||
|
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||||
|
import com.hibiscusmc.hmccosmetics.database.UserData;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow custom implementations of a {@link CosmeticUser}.
|
||||||
|
*/
|
||||||
|
public interface CosmeticUserProvider {
|
||||||
|
CosmeticUserProvider DEFAULT = new Default();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct the custom {@link CosmeticUser}.
|
||||||
|
* @param playerId the player uuid
|
||||||
|
* @param userData the user data associated with the player
|
||||||
|
* @return the {@link CosmeticUser}
|
||||||
|
*/
|
||||||
|
CosmeticUser createCosmeticUser(UUID playerId, UserData userData);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the plugin that is providing this {@link CosmeticUserProvider}
|
||||||
|
* @return the plugin
|
||||||
|
*/
|
||||||
|
Plugin getProviderPlugin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation.
|
||||||
|
*/
|
||||||
|
class Default implements CosmeticUserProvider {
|
||||||
|
@Override
|
||||||
|
public CosmeticUser createCosmeticUser(UUID playerId, UserData userData) {
|
||||||
|
return new CosmeticUser(playerId, userData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Plugin getProviderPlugin() {
|
||||||
|
return HMCCosmeticsPlugin.getInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.hibiscusmc.hmccosmetics.user;
|
|||||||
|
|
||||||
import com.google.common.collect.HashBiMap;
|
import com.google.common.collect.HashBiMap;
|
||||||
import com.hibiscusmc.hmccosmetics.util.HMCCServerUtils;
|
import com.hibiscusmc.hmccosmetics.util.HMCCServerUtils;
|
||||||
|
import lombok.Getter;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -11,9 +12,10 @@ import java.util.Set;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class CosmeticUsers {
|
public class CosmeticUsers {
|
||||||
|
|
||||||
private static final HashBiMap<UUID, CosmeticUser> COSMETIC_USERS = HashBiMap.create();
|
private static final HashBiMap<UUID, CosmeticUser> COSMETIC_USERS = HashBiMap.create();
|
||||||
|
|
||||||
|
private static CosmeticUserProvider PROVIDER = CosmeticUserProvider.DEFAULT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a user to the Hashmap of stored CosmeticUsers. This will not override an entry if it already exists. If you need to override, delete then add.
|
* Adds a user to the Hashmap of stored CosmeticUsers. This will not override an entry if it already exists. If you need to override, delete then add.
|
||||||
* @param user The user to add to the HashMap.
|
* @param user The user to add to the HashMap.
|
||||||
@@ -72,6 +74,31 @@ public class CosmeticUsers {
|
|||||||
return COSMETIC_USERS.get(player.getUniqueId());
|
return COSMETIC_USERS.get(player.getUniqueId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a custom {@link CosmeticUserProvider} to provide your own user implementation to
|
||||||
|
* be used and queried.
|
||||||
|
* @param provider the provider to register
|
||||||
|
* @throws IllegalArgumentException if the provider is already registered by another plugin
|
||||||
|
*/
|
||||||
|
public static void registerProvider(final CosmeticUserProvider provider) {
|
||||||
|
if(provider != CosmeticUserProvider.DEFAULT) {
|
||||||
|
throw new IllegalArgumentException("CosmeticUserProvider already registered by %s, this conflicts with %s attempting to register their own.".formatted(
|
||||||
|
PROVIDER.getProviderPlugin().getName(),
|
||||||
|
provider.getProviderPlugin().getName()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
PROVIDER = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the current {@link CosmeticUserProvider} being used.
|
||||||
|
* @return the current {@link CosmeticUserProvider} being used
|
||||||
|
*/
|
||||||
|
public static CosmeticUserProvider getProvider() {
|
||||||
|
return PROVIDER;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the values for CosmeticUsers to allow you to iterate over everyone.
|
* Gets all the values for CosmeticUsers to allow you to iterate over everyone.
|
||||||
* @return CosmeticUsers in a set. This will never be null, but might be empty.
|
* @return CosmeticUsers in a set. This will never be null, but might be empty.
|
||||||
|
|||||||
Reference in New Issue
Block a user