diff options
| author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-08-10 10:15:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-10 17:15:01 +0900 |
| commit | 091ce4c72c123f43f317c097818ace15f3a085fa (patch) | |
| tree | 9a151e29494ef2683b2bff59fec19b56d4a05fb0 /src/main/java/cc/polyfrost/oneconfig/internal/config/profiles | |
| parent | 799c389fdb993e363d71d268e0df9ae9f0a0c8a1 (diff) | |
| download | OneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.tar.gz OneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.tar.bz2 OneConfig-091ce4c72c123f43f317c097818ace15f3a085fa.zip | |
Profile command (#88)
* e
* omg finish profile shit
* api and fix 1.12.2 preprocess
* fix class names
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/internal/config/profiles')
| -rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java new file mode 100644 index 0000000..baa5d48 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/profiles/Profiles.java @@ -0,0 +1,129 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021, 2022 Polyfrost. + * <https://polyfrost.cc> <https://github.com/Polyfrost/> + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see <https://www.gnu.org/licenses/>. You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * <https://polyfrost.cc/legal/oneconfig/additional-terms> + */ + +package cc.polyfrost.oneconfig.internal.config.profiles; + +import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; +import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; +import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Profiles { + private static final Logger LOGGER = LogManager.getLogger("OneConfig Profiles"); + public static final File nonProfileSpecificDir = new File("OneConfig/config"); + public static final File profileDir = new File("OneConfig/profiles"); + private static ArrayList<String> profiles; + + public static String getCurrentProfile() { + if (!profileDir.exists() && !profileDir.mkdir()) { + LOGGER.fatal("Could not create profiles folder"); + return null; + } + if (profiles == null) { + String[] profilesArray = profileDir.list((file, s) -> file.isDirectory()); + if (profilesArray != null) profiles = new ArrayList<>(Arrays.asList(profilesArray)); + } + if (!getProfileDir(OneConfigConfig.currentProfile).exists()) { + createProfile(OneConfigConfig.currentProfile); + } + return OneConfigConfig.currentProfile; + } + + public static void createProfile(String name) { + File folder = new File(profileDir, name); + if (!folder.exists() && !folder.mkdir()) { + LOGGER.fatal("Could not create profile folder"); + return; + } + profiles.add(name); + } + + public static File getProfileDir() { + return getProfileDir(getCurrentProfile()); + } + + public static File getProfileDir(String profile) { + return new File(profileDir, profile); + } + + public static File getProfileFile(String file) { + return new File(getProfileDir(), file); + } + + public static File getNonProfileSpecificDir(String file) { + return new File(nonProfileSpecificDir, file); + } + + public static List<String> getProfiles() { + return new ArrayList<>(profiles); + } + + public static boolean doesProfileExist(String profile) { + return profiles.contains(profile); + } + + public static void loadProfile(String profile) { + ConfigCore.saveAll(); + OneConfigConfig.currentProfile = profile; + OneConfigConfig.getInstance().save(); + ConfigCore.reInitAll(); + } + + public static void renameProfile(String name, String newName) { + try { + File newFile = new File(profileDir, newName); + FileUtils.moveDirectory(getProfileDir(name), newFile); + if (OneConfigConfig.currentProfile.equals(name)) OneConfigConfig.currentProfile = newName; + profiles.remove(name); + profiles.add(newName); + } catch (IOException e) { + LOGGER.error("Failed to rename profile"); + } + } + + public static void deleteProfile(String name) { + if (name.equals(getCurrentProfile())) { + if (profiles.size() == 1) { + LOGGER.error("Cannot delete only profile!"); + return; + } + loadProfile(profiles.stream().filter(entry -> !entry.equals(name)).findFirst().get()); + } + try { + FileUtils.deleteDirectory(getProfileDir(name)); + profiles.remove(name); + } catch (IOException e) { + LOGGER.error("Failed to delete profile"); + } + } +} |
