Skip to content

Commit

Permalink
finish up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Nov 22, 2024
1 parent 3ba384f commit 8a3b54d
Show file tree
Hide file tree
Showing 39 changed files with 1,493 additions and 283 deletions.
25 changes: 19 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import org.apache.commons.io.output.ByteArrayOutputStream
import org.apache.tools.ant.filters.ReplaceTokens
import java.nio.charset.Charset

plugins {
id("java")
Expand All @@ -7,9 +9,10 @@ plugins {
}

val langVersion: Int = 17
val encoding = "UTF-8"

group = "com.dre.brewery"
version = "3.3.5-SNAPSHOT"
version = "3.3.6-SNAPSHOT"

repositories {
mavenCentral()
Expand Down Expand Up @@ -75,17 +78,17 @@ dependencies {


tasks {

build {
dependsOn(shadowJar)
finalizedBy("kotlinReducedJar")
}

jar {
archiveClassifier.set("original")
//enabled = false // Shadow produces our jar files
enabled = false // Shadow produces our jar files
}
withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
options.encoding = encoding
}
test {
useJUnitPlatform()
Expand All @@ -94,10 +97,10 @@ tasks {
processResources {
outputs.upToDateWhen { false }
filter<ReplaceTokens>(mapOf(
"tokens" to mapOf("version" to project.version.toString()),
"tokens" to mapOf("version" to "${project.version};${getGitBranch()}"),
"beginToken" to "\${",
"endToken" to "}"
))
)).filteringCharset = encoding
}

shadowJar {
Expand Down Expand Up @@ -131,6 +134,16 @@ tasks {
}
}

fun getGitBranch(): String = ByteArrayOutputStream().use { stream ->
var branch = "none"
project.exec {
commandLine = listOf("git", "rev-parse", "--abbrev-ref", "HEAD")
standardOutput = stream
}
if (stream.size() > 0) branch = stream.toString(Charset.defaultCharset().name()).trim()
return branch
}

java {
toolchain.languageVersion = JavaLanguageVersion.of(langVersion)
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/dre/brewery/BreweryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@
@Getter
public class BreweryPlugin extends JavaPlugin {

// TODO: Should we do file backups? - Jsinco
// TODO: File backups
// TODO: Change the addon API FileManager to use Okaeri
// TODO: Add localized header so recipes, cauldron, and custom-items file can be updated w/o removing comments

private static final int RESOURCE_ID = 114777;

Expand Down Expand Up @@ -132,7 +131,7 @@ public void onEnable() {

// Load config and lang
Config config = ConfigManager.getConfig(Config.class);
Lang lang = ConfigManager.getConfig(Lang.class);
ConfigManager.newInstance(Lang.class, false);

if (config.isFirstCreation()) {
config.onFirstCreation();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dre/brewery/api/addons/AddonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void loadAddon(File file) {
managerField.set(addon, this);


if (addon.getAddonInfo() == null) {
if (addon.getAddonInfo() == null) { // This CAN be null for us. It's only annotated NotNull for addons.
plugin.errorLog("Addon " + addonClass.getSimpleName() + " is missing the AddonInfo annotation. It will not be loaded.");
continue;
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/dre/brewery/configuration/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ConfigManager {
BreweryXConfigurer.class, BreweryXConfigurer::new,
YamlSnakeYamlConfigurer.class, YamlSnakeYamlConfigurer::new
);
protected static final List<Supplier<BidirectionalTransformer<?, ?>>> TRANSFORMERS = List.of(
private static final List<Supplier<BidirectionalTransformer<?, ?>>> TRANSFORMERS = List.of(
MaterialTransformer::new
);

Expand Down Expand Up @@ -75,12 +75,11 @@ public static <T extends AbstractOkaeriConfigFile> T getConfig(Class<T> configCl
* @param configClass The class of the config to replace
* @param <T> The type of the config
*/
public static <T extends AbstractOkaeriConfigFile> void newInstance(T configClass) {
LOADED_CONFIGS.put(configClass.getClass(), createConfig(configClass.getClass(),
getFilePath(configClass.getClass()),
configClass.getConfigurer(),
new StandardSerdes(),
getOkaeriConfigFileOptions(configClass.getClass()).update()));
public static <T extends AbstractOkaeriConfigFile> void newInstance(Class<T> configClass, boolean overwrite) {
if (!overwrite && LOADED_CONFIGS.containsKey(configClass)) {
return;
}
LOADED_CONFIGS.put(configClass, createConfig(configClass));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public String update(String line, ConfigLineInfo lineInfo, List<ConfigLineInfo>
for (int i = 0; i < (path.size() - 1); i++) {
ConfigLineInfo pathElement = path.get(i);
Optional<FieldDeclaration> field = currentDeclaration.getField(pathElement.getName());
if (!field.isPresent()) {
if (field.isEmpty()) {
return line;
}
GenericsDeclaration fieldType = field.get().getType();
Expand Down Expand Up @@ -187,8 +187,6 @@ public String update(String line, ConfigLineInfo lineInfo, List<ConfigLineInfo>
}

comment.append(ConfigPostprocessor.createComment(BreweryXConfigurer.this.commentPrefix, finalComment));
// append blank line to final comment

return ConfigPostprocessor.addIndent(comment.toString(), lineInfo.getIndent()) + line;
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
Expand All @@ -20,7 +19,7 @@ public ConfigTranslations(Translation activeTranslation) {
public ConfigTranslations(Translation activeTranslation, Yaml yamlInstance) {
try (InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("configlangs/" + activeTranslation.getFilename())) {
.getResourceAsStream("config-langs/" + activeTranslation.getFilename())) {

translations = yamlInstance.load(inputStream);
} catch (IOException e) {
Expand All @@ -36,7 +35,9 @@ public ConfigTranslations(Translation activeTranslation, Yaml yamlInstance) {
@Nullable
public String getTranslation(String key) {
try {
if (!key.contains(".")) {
if (translations == null) {
return null;
} else if (!key.contains(".")) {
return (String) translations.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@AllArgsConstructor
public enum Translation {

// Languages added should have a config and a lang translation (resources/configlangs/, resources/languages/)
// Languages added should have a config and a lang translation (resources/config-langs/, resources/languages/)

EN("en.yml"),
DE("de.yml"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import com.dre.brewery.configuration.annotation.OkaeriConfigFileOptions;
import com.dre.brewery.configuration.sector.CauldronSector;
import com.dre.brewery.configuration.sector.capsule.ConfigCauldronIngredient;
import eu.okaeri.configs.annotation.Header;

import java.util.HashMap;
import java.util.Map;


@OkaeriConfigFileOptions(value = "cauldron.yml", update = false)
@Header("!!! IMPORTANT: BreweryX configuration files do NOT support external comments! If you add any comments, they will be overwritten!!!")
@OkaeriConfigFileOptions("cauldron.yml")
public class CauldronFile extends AbstractOkaeriConfigFile {

@LocalizedComment("cauldronFile.header")
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/dre/brewery/configuration/files/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@


@OkaeriConfigFileOptions("config.yml")
@DefaultCommentSpace(1)
@Header({"Our proper config guide can be found at: https://brewery.lumamc.net/en/guide/edit_config/",
@Header({"!!! IMPORTANT: BreweryX configuration files do NOT support external comments! If you add any comments, they will be overwritten!!!",
"Our proper config guide can be found at: https://brewery.lumamc.net/en/guide/edit_config/",
"Still have questions? Join our Discord: https://discord.gg/ZTGCzeKg45"})
@Footer({"", "Yep, that's it! The end of config.yml! I had so much fun! And you?..."})
@DefaultCommentSpace(1)
@Getter @Setter
public class Config extends AbstractOkaeriConfigFile {

Expand Down Expand Up @@ -153,7 +154,7 @@ public void onFirstCreation() {
@LocalizedComment("config.useOffhandForCauldron")
private boolean useOffhandForCauldron = false;

@LocalizedComment("config.loadDataAsync") // Unused, see configlangs/en.yml#config.loadDataAsync comment
@LocalizedComment("config.loadDataAsync") // Unused, see config-langs/en.yml#config.loadDataAsync comment
private boolean loadDataAsync = true;

@LocalizedComment("config.hangoverDays")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import com.dre.brewery.configuration.sector.CustomItemsSector;
import com.dre.brewery.configuration.sector.capsule.ConfigCustomItem;
import com.dre.brewery.recipe.RecipeItem;
import eu.okaeri.configs.annotation.Header;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@OkaeriConfigFileOptions(value = "custom-items.yml", update = false)
@Header("!!! IMPORTANT: BreweryX configuration files do NOT support external comments! If you add any comments, they will be overwritten!!!")
@OkaeriConfigFileOptions("custom-items.yml")
@Setter
public class CustomItemsFile extends AbstractOkaeriConfigFile {

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/dre/brewery/configuration/files/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

// Our bind file for this class should vary based on what language the user has set in the config.
@OkaeriConfigFileOptions(useLangFileName = true)
@Header("Translations for BreweryX")
@Header({"!!! IMPORTANT: BreweryX configuration files do NOT support external comments! If you add any comments, they will be overwritten!!!",
"Translations for BreweryX"})
@DefaultCommentSpace(1)
@SuppressWarnings("unused")
public class Lang extends AbstractOkaeriConfigFile {
Expand All @@ -36,7 +37,10 @@ public void reload() {
}

public void mapStrings() {
mappedEntries = new HashMap<>();
BreweryPlugin plugin = BreweryPlugin.getInstance();
plugin.log("Using language&7: &6" + this.getBindFile().getFileName());

this.mappedEntries = new HashMap<>();
for (Field field : this.getClass().getDeclaredFields()) {
if (field.getType() != String.class) {
continue;
Expand All @@ -45,12 +49,12 @@ public void mapStrings() {
try {
CustomKey customKey = field.getAnnotation(CustomKey.class);
if (customKey != null) {
mappedEntries.put(customKey.value(), (String) field.get(this));
this.mappedEntries.put(customKey.value(), (String) field.get(this));
} else {
mappedEntries.put(field.getName(), (String) field.get(this));
this.mappedEntries.put(field.getName(), (String) field.get(this));
}
} catch (IllegalAccessException e) {
BreweryPlugin.getInstance().errorLog("Lang failed to get a field value! &6(" + field.getName() + ")", e);
plugin.errorLog("Lang failed to get a field value! &6(" + field.getName() + ")", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import com.dre.brewery.configuration.annotation.OkaeriConfigFileOptions;
import com.dre.brewery.configuration.sector.RecipesSector;
import com.dre.brewery.configuration.sector.capsule.ConfigRecipe;
import eu.okaeri.configs.annotation.Header;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Header("!!! IMPORTANT: BreweryX configuration files do NOT support external comments! If you add any comments, they will be overwritten!!!")
@Footer({
"More recipe ideas:",
"Dandelion Liquor",
Expand All @@ -32,7 +34,7 @@
"",
"There are a lot of items in Minecraft like Vines, Milk and items added by plugins that would make great ingredients."
})
@OkaeriConfigFileOptions(value = "recipes.yml", update = false)
@OkaeriConfigFileOptions("recipes.yml")
@Setter
public class RecipesFile extends AbstractOkaeriConfigFile {

Expand Down
45 changes: 22 additions & 23 deletions src/main/java/com/dre/brewery/storage/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ public void tryAutoSave() {
}
}

public void exit(boolean save, boolean async) {
this.exit(save, async, null);
}

public void exit(boolean save, boolean async, Runnable callback) {
if (save) {
saveAll(async, () -> {
this.closeConnection();
plugin.log("Closed connection from&7:&a " + this.getClass().getSimpleName());
if (callback != null) {
callback.run();
}
});
} else {
this.closeConnection(); // let databases close their connections
plugin.log("Closed connection from&7:&a " + this.getClass().getSimpleName());
if (callback != null) {
callback.run();
}
}
}

public void saveAll(boolean async) {
saveAll(async, null);
}
Expand Down Expand Up @@ -111,29 +133,6 @@ protected void closeConnection() {
// Implemented in subclasses that use database connections
}


public void exit(boolean save, boolean async) {
this.exit(save, async, null);
}

public void exit(boolean save, boolean async, Runnable callback) {
if (save) {
saveAll(async, () -> {
this.closeConnection();
plugin.log("Closed connection from&7:&a " + this.getClass().getSimpleName());
if (callback != null) {
callback.run();
}
});
} else {
this.closeConnection(); // let databases close their connections
plugin.log("Closed connection from&7:&a " + this.getClass().getSimpleName());
if (callback != null) {
callback.run();
}
}
}

public static DataManager createDataManager(ConfiguredDataManager record) throws StorageInitException {
DataManager dataManager = switch (record.getType()) {
case FLATFILE -> new FlatFileStorage(record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.UUID;


// TODO: Should we use Okaeri here?
public class FlatFileStorage extends DataManager {

private final File rawFile;
Expand Down
File renamed without changes.
Loading

0 comments on commit 8a3b54d

Please sign in to comment.