feat: random skin on join

This commit is contained in:
ineanto 2023-12-22 14:57:24 +01:00
parent 153d666b78
commit 30803363ba
3 changed files with 54 additions and 63 deletions

View file

@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.ineanto.nicko.appearance.random.RandomNameFetcher;
import xyz.ineanto.nicko.command.NickoCommand;
import xyz.ineanto.nicko.config.Configuration;
import xyz.ineanto.nicko.config.ConfigurationManager;
@ -38,6 +39,7 @@ public class NickoBukkit extends JavaPlugin {
private Configuration configuration;
private LocaleFileManager localeFileManager;
private PlayerNameStore nameStore;
private RandomNameFetcher nameFetcher;
private Metrics metrics;
public NickoBukkit() {
@ -63,6 +65,7 @@ public class NickoBukkit extends JavaPlugin {
mojangAPI = new MojangAPI();
dataStore = new PlayerDataStore(mojangAPI, getNickoConfig());
nameStore = new PlayerNameStore();
nameFetcher = new RandomNameFetcher(this);
if (!Bukkit.getOnlineMode()) {
getLogger().warning("Nicko has not been tested using offline mode!");
@ -178,6 +181,10 @@ public class NickoBukkit extends JavaPlugin {
}
}
public RandomNameFetcher getNameFetcher() {
return nameFetcher;
}
public PlayerDataStore getDataStore() {
return dataStore;
}

View file

@ -1,5 +1,6 @@
package xyz.ineanto.nicko.event;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -34,37 +35,44 @@ public class PlayerJoinListener implements Listener {
final I18N i18n = new I18N(player);
final PlayerNameStore nameStore = instance.getNameStore();
final PlayerDataStore dataStore = instance.getDataStore();
nameStore.storeName(player);
Bukkit.getScheduler().runTaskLater(instance, () -> {
final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
optionalProfile.ifPresent(profile -> {
if (profile.hasData()) {
final AppearanceManager appearanceManager = new AppearanceManager(player);
final boolean needsASkinChange = profile.getSkin() != null && !profile.getSkin().equals(player.getName());
final ActionResult actionResult = appearanceManager.updatePlayer(needsASkinChange, false);
if (!actionResult.isError()) {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.OK));
} else {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.ERROR, i18n.translateStringWithoutPrefix(actionResult.getErrorKey())));
}
final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
optionalProfile.ifPresent(profile -> {
// Random Skin on connection feature
if (profile.isRandomSkin()) {
Bukkit.broadcast(Component.text("§c[ELITE DEBUG] §fJoined with Random Skin."));
final String name = instance.getNameFetcher().getRandomUsername();
final String skin = instance.getNameFetcher().getRandomUsername();
profile.setName(name);
profile.setSkin(skin);
dataStore.updateCache(player.getUniqueId(), profile);
}
if (profile.hasData()) {
final AppearanceManager appearanceManager = new AppearanceManager(player);
final boolean needsASkinChange = profile.getSkin() != null && !profile.getSkin().equals(player.getName());
final ActionResult actionResult = appearanceManager.updatePlayer(needsASkinChange, false);
if (!actionResult.isError()) {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.OK));
} else {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.ERROR, i18n.translateStringWithoutPrefix(actionResult.getErrorKey())));
}
}
});
for (Player online : Bukkit.getOnlinePlayers().stream().filter(op -> op.getUniqueId() != player.getUniqueId()).toList()) {
final Optional<NickoProfile> optionalOnlinePlayerProfile = dataStore.getData(online.getUniqueId());
optionalOnlinePlayerProfile.ifPresent(profile -> {
final AppearanceManager appearanceManager = new AppearanceManager(online);
final boolean needsASkinChange = profile.getSkin() != null && !profile.getSkin().equals(online.getName());
final ActionResult actionResult = appearanceManager.updateForOthers(needsASkinChange, false);
if (actionResult.isError()) {
logger.warning("Something wrong happened while updating players to joining player (" + actionResult.getErrorKey() + ")");
}
});
for (Player online : Bukkit.getOnlinePlayers().stream().filter(op -> op.getUniqueId() != player.getUniqueId()).toList()) {
final Optional<NickoProfile> optionalOnlinePlayerProfile = dataStore.getData(online.getUniqueId());
optionalOnlinePlayerProfile.ifPresent(profile -> {
final AppearanceManager appearanceManager = new AppearanceManager(online);
final boolean needsASkinChange = profile.getSkin() != null && !profile.getSkin().equals(online.getName());
final ActionResult actionResult = appearanceManager.updateForOthers(needsASkinChange, false);
if (actionResult.isError()) {
logger.warning("Something wrong happened while updating players to joining player (" + actionResult.getErrorKey() + ")");
}
});
}
}, 20L);
}
@SuppressWarnings("unchecked") final ArrayList<UUID> viewers = (ArrayList<UUID>) PlayerCheckGUIData.VIEWERS.clone();
viewers.forEach(uuid -> {