add locale class

This commit is contained in:
Ryder Belserion 2023-03-04 23:16:35 -05:00
parent ca89bd6371
commit aaa07fbd27
No known key found for this signature in database
GPG Key ID: 8FC2E6C54BBF05FE
5 changed files with 144 additions and 8 deletions

View File

@ -42,7 +42,7 @@ public class ConfigSettings implements SettingsHolder {
""
};
conf.setComment("settings", header);
conf.setComment("misc", header);
}
@Comment("Allow damage items to be auctioned off.")

View File

@ -0,0 +1,35 @@
package us.crazycrew.crazyauctions.configurations;
import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.CommentsConfiguration;
/**
* @author RyderBelserion
* @author BadBones69
*
* Date: 3/4/2023
* Time: Unknown
* Last Edited: 3/4/2023 @ 10:23 PM
*
* Description: The locale file.
*/
public class LocaleSettings implements SettingsHolder {
// Empty constructor required by SettingsHolder
public LocaleSettings() {}
@Override
public void registerComments(CommentsConfiguration conf) {
String[] header = {
"Support: https://discord.gg/crazycrew",
"Github: https://github.com/Crazy-Crew",
"",
"Issues: https://github.com/Crazy-Crew/CrazyAuctions/issues",
"Features: https://github.com/Crazy-Crew/CrazyAuctions/discussions",
"",
"We need translations for this locale file. Please don't hesitate to submit any."
};
conf.setComment("misc", header);
}
}

View File

@ -0,0 +1,90 @@
package us.crazycrew.crazyauctions.utils;
import us.crazycrew.crazycore.CrazyLogger;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileUtils {
/**
* Extracts files from inside the .jar into an output
* <p>
* @param input the directory in the .jar
* @param output the output wherever you use this.
* @param replace if we should replace or not.
*/
public static void extract(String input, Path output, boolean replace) {
URL directory = FileUtils.class.getResource(input);
if (directory == null) CrazyLogger.debug("<#E0115F>Could not find <#11e092>" + input + " <#E0115F>in the jar.");
assert directory != null;
if (!directory.getProtocol().equals("jar"))
CrazyLogger.debug("<#E0115F>Failed because the protocol does not equal .jar!");
ZipFile jar;
try {
CrazyLogger.debug("<#E0115F>Starting to extract files from <#11e092>" + input + " <#E0115F>directory in the jar.");
jar = ((JarURLConnection) directory.openConnection()).getJarFile();
} catch (Exception e) {
throw new RuntimeException(e);
}
String filePath = input.substring(1);
Enumeration<? extends ZipEntry> fileEntries = jar.entries();
while (fileEntries.hasMoreElements()) {
ZipEntry entry = fileEntries.nextElement();
String entryName = entry.getName();
if (!entryName.startsWith(filePath)) continue;
Path outFile = output.resolve(entryName);
boolean exists = Files.exists(outFile);
if (!replace && exists) continue;
if (entry.isDirectory()) {
if (exists) {
CrazyLogger.debug("<#E0115F>File already exists.");
return;
}
try {
Files.createDirectories(outFile);
CrazyLogger.debug("<#E0115F>Directories have been created.");
} catch (Exception e) {
e.printStackTrace();
}
continue;
}
try (InputStream inputStream = jar.getInputStream(entry); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile.toFile()))) {
byte[] buffer = new byte[4096];
int readCount;
while ((readCount = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, readCount);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -8,6 +8,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import us.crazycrew.crazyauctions.CrazyAuctions;
import us.crazycrew.crazyauctions.configurations.ConfigSettings;
import us.crazycrew.crazyauctions.configurations.LocaleSettings;
import us.crazycrew.crazyauctions.configurations.PluginSettings;
import us.crazycrew.crazyauctions.configurations.migrations.PluginMigrationService;
import us.crazycrew.crazycore.CrazyLogger;
@ -34,6 +35,7 @@ public class AuctionsStarter implements PluginBootstrap {
private static SettingsManager pluginConfig;
private static SettingsManager config;
private static SettingsManager locale;
@Override
public void bootstrap(@NotNull PluginProviderContext context) {
@ -48,6 +50,11 @@ public class AuctionsStarter implements PluginBootstrap {
.withYamlFile(new File(context.getDataDirectory().toFile(), "config.yml"))
.configurationData(ConfigSettings.class)
.create();
locale = SettingsManagerBuilder
.withYamlFile(new File(context.getDataDirectory().toFile() + "/locale/", pluginConfig.getProperty(PluginSettings.LOCALE_FILE)))
.configurationData(LocaleSettings.class)
.create();
}
@Override
@ -77,4 +84,8 @@ public class AuctionsStarter implements PluginBootstrap {
public static SettingsManager getConfig() {
return config;
}
public static SettingsManager getLocale() {
return locale;
}
}

View File

@ -2,6 +2,9 @@ package us.crazycrew.crazyauctions.utils.misc;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import us.crazycrew.crazyauctions.api.interfaces.Universal;
import us.crazycrew.crazyauctions.configurations.PluginSettings;
import us.crazycrew.crazyauctions.loader.AuctionsStarter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -12,11 +15,12 @@ import java.util.regex.Pattern;
*
* Created: 2/18/2023
* Time: Unknown
* Last Edited: 2/28/2023 @ 3:04 AM
* Last Edited: 3/4/2023 @ 11:15 PM
*
* Description: Color utilities.
*/
public class ColorUtils {
public class ColorUtils implements Universal {
private static final Pattern HEX_PATTERN = Pattern.compile("#[a-fA-F\\d]{6}");
public static String color(String message) {
@ -70,11 +74,7 @@ public class ColorUtils {
return ChatColor.stripColor(msg);
}
public static String getPrefix(String string) {
return "";
}
public static String getPrefix() {
return getPrefix("");
return AuctionsStarter.getPluginConfig().getProperty(PluginSettings.COMMAND_PREFIX);
}
}