This commit is contained in:
Ajneb97 2023-11-08 09:50:45 -03:00
parent c782e8d834
commit fe88b6ae8f
6 changed files with 133 additions and 56 deletions

View file

@ -123,7 +123,7 @@ public class MainCommand implements CommandExecutor, TabCompleter {
return;
}
plugin.getMigrationManager().migrateKits(sender);
plugin.getMigrationManager().migrate(sender);
}
public void verify(Player player,FileConfiguration messagesConfig,MessagesManager msgManager){

View file

@ -129,29 +129,33 @@ public class PlayersConfigManager {
plugin.getPlayerDataManager().setPlayers(players);
}
public void saveConfig(PlayerData playerData){
String playerName = playerData.getName();
CustomConfig playerConfig = getConfigFile(playerData.getUuid()+".yml");
if(playerConfig == null) {
registerConfigFile(playerData.getUuid()+".yml");
playerConfig = getConfigFile(playerData.getUuid()+".yml");
}
FileConfiguration config = playerConfig.getConfig();
config.set("name", playerName);
config.set("kits",null);
for(PlayerDataKit playerDataKit : playerData.getKits()){
String kitName = playerDataKit.getName();
config.set("kits."+kitName+".cooldown",playerDataKit.getCooldown());
config.set("kits."+kitName+".one_time",playerDataKit.isOneTime());
config.set("kits."+kitName+".bought",playerDataKit.isBought());
}
playerConfig.saveConfig();
}
public void saveConfigs(){
ArrayList<PlayerData> players = plugin.getPlayerDataManager().getPlayers();
for(PlayerData playerData : players) {
String playerName = playerData.getName();
CustomConfig playerConfig = getConfigFile(playerData.getUuid()+".yml");
if(playerConfig == null) {
registerConfigFile(playerData.getUuid()+".yml");
playerConfig = getConfigFile(playerData.getUuid()+".yml");
}
FileConfiguration config = playerConfig.getConfig();
config.set("name", playerName);
config.set("kits",null);
for(PlayerDataKit playerDataKit : playerData.getKits()){
String kitName = playerDataKit.getName();
config.set("kits."+kitName+".cooldown",playerDataKit.getCooldown());
config.set("kits."+kitName+".one_time",playerDataKit.isOneTime());
config.set("kits."+kitName+".bought",playerDataKit.isBought());
}
saveConfig(playerData);
}
saveConfigFiles();
}
}

View file

@ -2,11 +2,11 @@ package pk.ajneb97.managers;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitRunnable;
import pk.ajneb97.PlayerKits2;
import pk.ajneb97.configs.KitsConfigManager;
import pk.ajneb97.model.Kit;
import pk.ajneb97.model.KitAction;
import pk.ajneb97.model.KitRequirements;
import pk.ajneb97.configs.PlayersConfigManager;
import pk.ajneb97.model.*;
import pk.ajneb97.model.item.KitItem;
import pk.ajneb97.model.item.KitItemSkullData;
@ -21,6 +21,18 @@ public class MigrationManager {
this.plugin = plugin;
}
public void migrate(CommandSender sender){
new BukkitRunnable(){
@Override
public void run() {
migrateKits(sender);
migratePlayers(sender);
sender.sendMessage(PlayerKits2.prefix+MessagesManager.getColoredMessage(" &aMigration completed."));
}
}.runTaskAsynchronously(plugin);
}
public void migrateKits(CommandSender sender){
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "PlayerKits");
File configFile = new File(bStatsFolder, "kits.yml");
@ -98,8 +110,50 @@ public class MigrationManager {
return;
}
plugin.getConfigsManager().reload();
sender.sendMessage(PlayerKits2.prefix+MessagesManager.getColoredMessage(" &aMigration completed."));
kitsConfigManager.reloadConfigs();
plugin.getVerifyManager().verify();
}
public void migratePlayers(CommandSender sender){
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "PlayerKits");
File configFile = new File(bStatsFolder, "players.yml");
if(!configFile.exists()){
sender.sendMessage(PlayerKits2.prefix+MessagesManager.getColoredMessage(" &cPlayerKits1 players.yml file not found."));
return;
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
PlayersConfigManager playersConfigManager = plugin.getConfigsManager().getPlayersConfigManager();
if(config.contains("Players")) {
for (String uuid : config.getConfigurationSection("Players").getKeys(false)) {
try {
String path = "Players."+uuid;
String name = config.getString(path+".name");
PlayerData playerData = new PlayerData(name,uuid);
for(String kitName : config.getConfigurationSection(path).getKeys(false)){
if(kitName.equals("name")){
continue;
}
PlayerDataKit playerDataKit = new PlayerDataKit(kitName);
playerDataKit.setBought(config.getBoolean(path+"."+kitName+".buyed"));
playerDataKit.setCooldown(config.getLong(path+"."+kitName+".cooldown"));
playerDataKit.setOneTime(config.getBoolean(path+"."+kitName+".one_time"));
playerData.addKit(playerDataKit);
}
playersConfigManager.saveConfig(playerData);
sender.sendMessage(PlayerKits2.prefix+MessagesManager.getColoredMessage(" &aPlayer &7"+name+" &amigrated."));
} catch (Exception e) {
sender.sendMessage(PlayerKits2.prefix + MessagesManager.getColoredMessage(" &cError while trying to migrate player data with uuid &7" + uuid + "&c, check console."));
e.printStackTrace();
}
}
}
playersConfigManager.reloadConfigs();
}
public KitItem getDisplayItem(YamlConfiguration config,String path){
@ -111,7 +165,7 @@ public class MigrationManager {
KitItemSkullData skullData = null;
if(config.contains(path+".display_item_skulldata")) {
String[] fullSkull = config.getString(path+".display_item_skulldata").split(";");
skullData = new KitItemSkullData(null,fullSkull[0],fullSkull[1]);
skullData = new KitItemSkullData(null,fullSkull[1],fullSkull[0]);
}
kitItem.setName(name);

View file

@ -2,11 +2,9 @@ package pk.ajneb97.utils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.bukkit.*;
import org.bukkit.FireworkEffect.Type;
@ -32,8 +30,11 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;
import pk.ajneb97.PlayerKits2;
import pk.ajneb97.model.item.*;
import sun.misc.BASE64Decoder;
public class ItemUtils {
@ -153,22 +154,41 @@ public class ItemUtils {
item.setItemMeta(skullMeta);
return;
}
GameProfile profile = null;
if(id == null) {
profile = new GameProfile(UUID.randomUUID(), owner != null ? owner : "");
}else {
profile = new GameProfile(UUID.fromString(id), owner != null ? owner : "");
}
profile.getProperties().put("textures", new Property("textures", texture));
try {
Field profileField = skullMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(skullMeta, profile);
} catch (IllegalArgumentException|NoSuchFieldException|SecurityException|IllegalAccessException error) {
error.printStackTrace();
}
ServerVersion serverVersion = PlayerKits2.serverVersion;
if(serverVersion.serverVersionGreaterEqualThan(serverVersion,ServerVersion.v1_20_R2)){
UUID uuid = id != null ? UUID.fromString(id) : UUID.randomUUID();
PlayerProfile profile = Bukkit.createPlayerProfile(uuid);
PlayerTextures textures = profile.getTextures();
URL url;
try {
String decoded = new String(Base64.getDecoder().decode(texture));
url = new URL(decoded.substring("{\"textures\":{\"SKIN\":{\"url\":\"".length(), decoded.length() - "\"}}}".length()));
} catch (MalformedURLException error) {
error.printStackTrace();
return;
}
textures.setSkin(url);
profile.setTextures(textures);
skullMeta.setOwnerProfile(profile);
}else{
GameProfile profile = null;
if(id == null) {
profile = new GameProfile(UUID.randomUUID(), owner != null ? owner : "");
}else {
profile = new GameProfile(UUID.fromString(id), owner != null ? owner : "");
}
profile.getProperties().put("textures", new Property("textures", texture));
try {
Field profileField = skullMeta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(skullMeta, profile);
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException | IllegalAccessException error) {
error.printStackTrace();
}
}
item.setItemMeta(skullMeta);
}

View file

@ -5,9 +5,7 @@ import pk.ajneb97.PlayerKits2;
import pk.ajneb97.utils.ServerVersion;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.*;
public class NMSManager {
@ -186,13 +184,14 @@ public class NMSManager {
if((boolean)version.getMethodRef("hasTag").invoke(newItem,null)){
Object compound = version.getMethodRef("getTag").invoke(newItem,null);
Set<String> tags = (Set<String>) version.getMethodRef("getKeys").invoke(compound,null);
Set<String> notTags = new HashSet<>(Arrays.asList(
"ench", "HideFlags", "display", "SkullOwner", "AttributeModifiers", "Enchantments",
"Damage", "CustomModelData", "Potion", "StoredEnchantments", "CustomPotionColor",
"CustomPotionEffects", "Fireworks", "Explosion", "pages", "title", "author", "resolved",
"generation", "Trim", "custom_potion_effects"
));
for(String t : tags) {
if(!t.equals("ench") && !t.equals("HideFlags") && !t.equals("display")
&& !t.equals("SkullOwner") && !t.equals("AttributeModifiers")
&& !t.equals("Enchantments") && !t.equals("Damage") && !t.equals("CustomModelData") && !t.equals("Potion")
&& !t.equals("StoredEnchantments") && !t.equals("CustomPotionColor") && !t.equals("CustomPotionEffects") && !t.equals("Fireworks")
&& !t.equals("Explosion")&& !t.equals("pages") && !t.equals("title") && !t.equals("author") && !t.equals("resolved")
&& !t.equals("generation") && !t.equals("Trim")) {
if(!notTags.contains(t)) {
if(t.equals("BlockEntityTag") && !item.getType().name().contains("SHULKER")){
continue;
}

View file

@ -1,5 +1,5 @@
main: pk.ajneb97.PlayerKits2
version: 1.7.2
version: 1.7.3
name: PlayerKits2
api-version: 1.13
softdepend: [Vault,PlaceholderAPI]