package qualitymobs.configs; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.attribute.Attribute; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.persistence.PersistentDataType; import qualitymobs.QualityMobs; import java.io.*; import java.util.HashMap; import static org.bukkit.plugin.java.JavaPlugin.getPlugin; // This should work? Not entirely sure. // Need to make a default mobs.yml file somewhere in the jar for a default file. public class EntityConfigManager { public static HashMap mobHashMap = new HashMap<>(); public static void generateMobHashMap() { mobHashMap.clear(); File folder = new File(getPlugin(QualityMobs.class).getDataFolder(), "Mobs/"); File[] listOfFiles = folder.listFiles(); // Ignore this warning, the folder will ALWAYS have a file // due to the default file generated on startup. for (File file : listOfFiles) { FileConfiguration mobConfig = new YamlConfiguration(); // Load the configuration file. try { mobConfig.load(file); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } // For each ID in the config, generate a QualityEntity for the ID, and add it to the entity HashMap. // When spawning a new mob, the attributes are retrieved from the HashMap using the ID provided by the command. for (String mobID : mobConfig.getKeys(false)) { EntityConfig newEntity = new EntityConfig(); newEntity.setID(mobID); newEntity.setType(mobConfig.get(mobID + ".Type", "ZOMBIE").toString()); newEntity.setDisplay(mobConfig.get(mobID + ".Display", "").toString()); newEntity.setDisguise(mobConfig.get(mobID + ".Disguise", "").toString()); newEntity.setHealth(mobConfig.get(mobID + ".Attributes.Health", "").toString()); newEntity.setDamage(mobConfig.get(mobID + ".Attributes.Damage", "").toString()); newEntity.setArmor(mobConfig.get(mobID + ".Attributes.Armor", "").toString()); newEntity.setArmorToughness(mobConfig.get(mobID + ".Attributes.ArmorToughness", "").toString()); newEntity.setKnockback(mobConfig.get(mobID + ".Attributes.Knockback", "").toString()); newEntity.setAttackSpeed(mobConfig.get(mobID + ".Attributes.AttackSpeed", "").toString()); newEntity.setFlyingSpeed(mobConfig.get(mobID + ".Attributes.FlyingSpeed", "").toString()); newEntity.setFollowRange(mobConfig.get(mobID + ".Attributes.FollowRange", "").toString()); newEntity.setKnockbackResistance(mobConfig.get(mobID + ".Attributes.KnockbackResistance", "").toString()); newEntity.setLuck(mobConfig.get(mobID + ".Attributes.Luck", "").toString()); newEntity.setMovementSpeed(mobConfig.get(mobID + ".Attributes.MovementSpeed", "").toString()); mobHashMap.put(mobID, newEntity); } } Bukkit.getConsoleSender().sendMessage(Component.text("[QualityMobs] Successfully loaded " + mobHashMap.size() + " custom mobs.")); } public static HashMap getMobHashMap() { return mobHashMap; } // This method can be used to reapply config options to existing QualityEntities. public static void applyConfigOptions(EntityConfig config, LivingEntity entity) { // Used for applying new options when the plugin is reloaded. entity.getPersistentDataContainer().set(new NamespacedKey(QualityMobs.getPlugin(QualityMobs.class),"QualityMob"), PersistentDataType.STRING, config.getID()); // Generic try { entity.customName(Component.text(config.getDisplay())); } catch (NullPointerException ignored) {} // Options // TODO: Add options. // Attributes try { entity.getAttribute(Attribute.GENERIC_MAX_HEALTH ).setBaseValue(config.getHealth()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE ).setBaseValue(config.getDamage()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_ARMOR ).setBaseValue(config.getArmor()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_ARMOR_TOUGHNESS ).setBaseValue(config.getArmorToughness()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK ).setBaseValue(config.getKnockback()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_ATTACK_SPEED ).setBaseValue(config.getAttackSpeed()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_FLYING_SPEED ).setBaseValue(config.getFlyingSpeed()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_FOLLOW_RANGE ).setBaseValue(config.getFollowRange()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE).setBaseValue(config.getKnockbackResistance()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_LUCK ).setBaseValue(config.getLuck()); } catch (NullPointerException ignored) {} try { entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED ).setBaseValue(config.getMovementSpeed()); } catch (NullPointerException ignored) {} } }