2019-01-16 13:01:24 +01:00
|
|
|
package com.songoda.epicenchants.managers;
|
|
|
|
|
2019-12-28 22:00:25 +01:00
|
|
|
import com.songoda.core.compatibility.ServerVersion;
|
2019-01-16 13:01:24 +01:00
|
|
|
import com.songoda.epicenchants.EpicEnchants;
|
2019-04-04 14:40:11 +02:00
|
|
|
import com.songoda.epicenchants.utils.objects.FileLocation;
|
2020-01-15 23:58:15 +01:00
|
|
|
import com.songoda.epicenchants.utils.settings.Settings;
|
2019-01-17 00:16:30 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
2019-01-16 13:01:24 +01:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
2020-01-15 23:58:15 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2019-08-04 23:49:57 +02:00
|
|
|
import java.nio.file.Files;
|
2019-01-24 20:41:01 +01:00
|
|
|
import java.util.*;
|
2019-01-16 13:01:24 +01:00
|
|
|
|
2019-04-04 14:40:11 +02:00
|
|
|
import static com.songoda.epicenchants.utils.objects.FileLocation.of;
|
2019-01-16 13:01:24 +01:00
|
|
|
import static java.io.File.separator;
|
2019-01-17 00:16:30 +01:00
|
|
|
import static java.util.Arrays.asList;
|
2019-01-16 13:01:24 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
public class FileManager extends Manager<String, FileConfiguration> {
|
|
|
|
|
2019-03-19 14:13:27 +01:00
|
|
|
private final String directory;
|
2019-04-04 14:40:11 +02:00
|
|
|
private final LinkedHashSet<FileLocation> files = new LinkedHashSet<>(asList(
|
2019-08-07 23:13:07 +02:00
|
|
|
of("menus/main-info-menu.yml", true, true),
|
2019-03-25 16:33:11 +01:00
|
|
|
of("menus/enchanter-menu.yml", true, true),
|
|
|
|
of("menus/tinkerer-menu.yml", true, true),
|
2019-04-03 15:58:13 +02:00
|
|
|
of("menus/alchemist-menu.yml", true, true),
|
2019-10-12 23:55:10 +02:00
|
|
|
of("menus/groups/simple-menu.yml", false, true),
|
|
|
|
of("menus/groups/unique-menu.yml", false, true),
|
|
|
|
of("menus/groups/elite-menu.yml", false, true),
|
|
|
|
of("menus/groups/ultimate-menu.yml", false, true),
|
|
|
|
of("menus/groups/legendary-menu.yml", false, true),
|
2019-08-05 04:24:48 +02:00
|
|
|
|
|
|
|
of("enchants/elite/AntiGravity.yml", false),
|
|
|
|
of("enchants/elite/Frozen.yml", false),
|
|
|
|
of("enchants/elite/Poison.yml", false),
|
|
|
|
of("enchants/elite/RocketEscape.yml", false),
|
|
|
|
of("enchants/elite/Shockwave.yml", false),
|
|
|
|
of("enchants/elite/Wither.yml", false),
|
|
|
|
|
|
|
|
of("enchants/legendary/DeathBringer.yml", false),
|
|
|
|
of("enchants/legendary/DeathGod.yml", false),
|
|
|
|
of("enchants/legendary/Enlightened.yml", false),
|
|
|
|
of("enchants/legendary/Gears.yml", false),
|
|
|
|
of("enchants/legendary/LifeSteal.yml", false),
|
|
|
|
of("enchants/legendary/Overload.yml", false),
|
2019-08-07 02:06:33 +02:00
|
|
|
of("enchants/legendary/Resist.yml", false),
|
2019-08-05 04:24:48 +02:00
|
|
|
of("enchants/legendary/SkillSwipe.yml", false),
|
|
|
|
|
|
|
|
of("enchants/simple/Aquatic.yml", false),
|
|
|
|
of("enchants/simple/Confusion.yml", false),
|
|
|
|
of("enchants/simple/Experience.yml", false),
|
|
|
|
of("enchants/simple/Glowing.yml", false),
|
|
|
|
of("enchants/simple/Haste.yml", false),
|
|
|
|
of("enchants/simple/Insomnia.yml", false),
|
|
|
|
of("enchants/simple/Lightning.yml", false),
|
|
|
|
of("enchants/simple/Obliterate.yml", false),
|
|
|
|
of("enchants/simple/Oxygenate.yml", false),
|
|
|
|
|
|
|
|
of("enchants/ultimate/Blind.yml", false),
|
|
|
|
of("enchants/ultimate/Dodge.yml", false),
|
2019-08-07 02:06:33 +02:00
|
|
|
of("enchants/ultimate/Fly.yml", false),
|
|
|
|
of("enchants/ultimate/FoodSteal.yml", false),
|
2019-08-05 04:24:48 +02:00
|
|
|
of("enchants/ultimate/IceAspect.yml", false),
|
|
|
|
of("enchants/ultimate/StormFall.yml", false),
|
|
|
|
|
|
|
|
of("enchants/unique/Berserk.yml", false),
|
2019-08-07 02:06:33 +02:00
|
|
|
of("enchants/unique/Decapitation.yml", false),
|
2019-08-05 04:24:48 +02:00
|
|
|
of("enchants/unique/Explosive.yml", false),
|
|
|
|
of("enchants/unique/FeatherWeight.yml", false),
|
2019-08-07 02:06:33 +02:00
|
|
|
of("enchants/unique/Inquisitive.yml", false),
|
2019-08-05 04:24:48 +02:00
|
|
|
of("enchants/unique/ObsidianDestroyer.yml", false),
|
|
|
|
of("enchants/unique/PlagueCarrier.yml", false),
|
|
|
|
of("enchants/unique/Ragdoll.yml", false),
|
|
|
|
of("enchants/unique/SelfDestruct.yml", false),
|
|
|
|
|
|
|
|
of("groups.yml", true),
|
2019-03-25 16:33:11 +01:00
|
|
|
of("items/special-items.yml", true, true),
|
|
|
|
of("items/dusts.yml", true, true)
|
2019-02-19 14:23:20 +01:00
|
|
|
));
|
2019-01-16 13:01:24 +01:00
|
|
|
|
|
|
|
public FileManager(EpicEnchants instance) {
|
2019-02-19 14:23:20 +01:00
|
|
|
super(instance);
|
2019-12-28 22:00:25 +01:00
|
|
|
directory = ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? "master" : "legacy";
|
|
|
|
Bukkit.getConsoleSender().sendMessage("Using the " + directory + " configurations because version is " + ServerVersion.getServerVersion().name());
|
2019-01-16 13:01:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
public void loadFiles() {
|
2019-03-25 16:33:11 +01:00
|
|
|
files.forEach(fileLocation -> {
|
|
|
|
File file = new File(instance.getDataFolder() + separator + fileLocation.getPath());
|
2019-01-16 13:01:24 +01:00
|
|
|
|
2020-01-15 23:58:15 +01:00
|
|
|
if (!file.exists() && (fileLocation.isRequired() || Settings.FIRST_LOAD.getBoolean())) {
|
2019-03-25 16:33:11 +01:00
|
|
|
Bukkit.getConsoleSender().sendMessage("Creating file: " + fileLocation.getPath());
|
2020-01-18 02:37:43 +01:00
|
|
|
file.getParentFile().mkdirs();
|
2019-03-21 15:35:12 +01:00
|
|
|
|
2019-01-27 16:22:05 +01:00
|
|
|
try {
|
2019-08-07 23:13:07 +02:00
|
|
|
// System.out.println(fileLocation.getResourcePath(directory) + " : " + file.toPath());
|
2019-08-05 04:24:48 +02:00
|
|
|
copy(instance.getResource(fileLocation.getResourcePath(directory)), Files.newOutputStream(file.toPath()));
|
2019-01-27 16:22:05 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-01-17 00:16:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 16:33:11 +01:00
|
|
|
if (fileLocation.isRequired()) {
|
2019-01-27 16:22:05 +01:00
|
|
|
FileConfiguration configuration = new YamlConfiguration();
|
|
|
|
try {
|
|
|
|
configuration.load(file);
|
|
|
|
} catch (IOException | InvalidConfigurationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-03-25 16:33:11 +01:00
|
|
|
add(fileLocation.getPath().replace(".yml", ""), configuration);
|
2019-01-27 16:22:05 +01:00
|
|
|
}
|
|
|
|
});
|
2019-03-21 15:35:12 +01:00
|
|
|
|
2019-08-05 04:24:48 +02:00
|
|
|
instance.getConfig().set("System.First Load", false);
|
2019-08-07 02:06:33 +02:00
|
|
|
instance.saveConfig();
|
2019-01-16 13:01:24 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 00:16:30 +01:00
|
|
|
public FileConfiguration getConfiguration(String key) {
|
2019-02-19 14:23:20 +01:00
|
|
|
return getValueUnsafe(key);
|
2019-01-17 00:16:30 +01:00
|
|
|
}
|
2019-01-24 20:41:01 +01:00
|
|
|
|
2019-03-05 16:59:00 +01:00
|
|
|
public List<File> getYmlFiles(String directory) {
|
2019-01-27 16:22:05 +01:00
|
|
|
File dir = new File(instance.getDataFolder() + separator + directory + separator);
|
2019-03-05 16:59:00 +01:00
|
|
|
File[] allFiles = dir.listFiles();
|
|
|
|
List<File> output = new ArrayList<>();
|
2019-01-24 20:41:01 +01:00
|
|
|
|
2019-03-05 16:59:00 +01:00
|
|
|
if (allFiles == null) {
|
|
|
|
return output;
|
|
|
|
}
|
2019-01-24 20:41:01 +01:00
|
|
|
|
2019-03-05 16:59:00 +01:00
|
|
|
Optional.ofNullable(dir.listFiles((dir1, filename) -> filename.endsWith(".yml"))).ifPresent(list -> {
|
|
|
|
output.addAll(Arrays.asList(list));
|
|
|
|
});
|
|
|
|
|
|
|
|
Arrays.stream(allFiles)
|
|
|
|
.filter(File::isDirectory)
|
|
|
|
.filter(s -> !s.getName().equalsIgnoreCase("old"))
|
|
|
|
.forEach(f -> output.addAll(getYmlFiles(directory + separator + f.getName())));
|
2019-03-21 15:35:12 +01:00
|
|
|
|
2019-03-05 16:59:00 +01:00
|
|
|
return output;
|
2019-01-24 20:41:01 +01:00
|
|
|
}
|
2019-08-05 04:24:48 +02:00
|
|
|
|
|
|
|
private static void copy(InputStream input, OutputStream output) {
|
|
|
|
int n;
|
|
|
|
byte[] buffer = new byte[1024 * 4];
|
|
|
|
|
|
|
|
try {
|
|
|
|
while ((n = input.read(buffer)) != -1) {
|
|
|
|
output.write(buffer, 0, n);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 13:01:24 +01:00
|
|
|
}
|