Skip to content

Commit

Permalink
go back to ConfigHead suppliers and fix some legacy migration stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Jan 4, 2025
1 parent 107d514 commit 8d7e61c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ jobs:
HANGAR_TOKEN: ${{ secrets.HANGAR_TOKEN }}
REPO_USERNAME: ${{ secrets.REPO_USERNAME }}
REPO_SECRET: ${{ secrets.REPO_SECRET }}
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
with:
arguments: publishRelease --stacktrace
22 changes: 15 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ tasks {
// Much rather use a task in Gradle than a GitHub action for this,
// but, may want to look into finding a small plugin for this since BreweryX has
// a variety of addons that would also need this code copied into them.
val webhook = DiscordWebhook(System.getenv("DISCORD_WEBHOOK") ?: return@doLast)
webhook.message = "A new version of BreweryX has been released!"
val webhook = DiscordWebhook(System.getenv("DISCORD_WEBHOOK") ?: run {
println("No Discord Webhook found, skipping Discord announcement.")
return@doLast
})
webhook.message = "@everyone"
webhook.embedTitle = "BreweryX - v${project.version}"
webhook.embedDescription = readChangeLog()
webhook.send()
Expand Down Expand Up @@ -223,7 +226,10 @@ publishing {


modrinth {
token.set(System.getenv("MODRINTH_TOKEN"))
token.set(System.getenv("MODRINTH_TOKEN") ?: run {
println("No Modrinth API key found, skipping Modrinth publication.")
return@modrinth
})
projectId.set(project.name) // This can be the project ID or the slug. Either will work!
versionNumber.set(project.version.toString())
versionType.set("release") // This is the default -- can also be `beta` or `alpha`
Expand All @@ -240,12 +246,14 @@ hangarPublish {
version.set(project.version.toString())
channel.set("Release")
id.set(project.name)
apiKey.set(System.getenv("HANGAR_TOKEN"))
apiKey.set(System.getenv("HANGAR_TOKEN") ?: run {
println("No Hangar API key found, skipping Hangar publication.")
return@register
})
platforms {
register(Platforms.PAPER) {
// TODO: Swap jar with modrinth release link
//url.set("https://modrinth.com/plugin/breweryx/versions")
jar.set(tasks.shadowJar.flatMap { it.archiveFile })
url.set("https://modrinth.com/plugin/breweryx/versions")
//jar.set(tasks.shadowJar.flatMap { it.archiveFile })
platformVersions.set(listOf("1.13.x", "1.14.x", "1.15.x", "1.16.x", "1.17.x", "1.18.x", "1.19.x", "1.20.x", "1.21.x"))
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/dre/brewery/BreweryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,21 @@ public final class BreweryPlugin extends JavaPlugin {
// can start loading.
instance = this;
this.migrateBreweryDataFolder();
MCVersion = MinecraftVersion.getIt();
scheduler = UniversalScheduler.getScheduler(this);
TranslationManager.newInstance(this.getDataFolder());
}

@Override
public void onLoad() {
MCVersion = MinecraftVersion.getIt();
scheduler = UniversalScheduler.getScheduler(this);
}

@Override
public void onEnable() {
if (getMCVersion().isOrLater(MinecraftVersion.V1_14)) {
// Campfires are weird, Initialize once now so it doesn't lag later when we check for campfires under Cauldrons
// Campfires are weird. Initialize once now, so it doesn't lag later when we check for campfires under Cauldrons
getServer().createBlockData(Material.CAMPFIRE);
}
}


@Override
public void onEnable() {
// Register Item Loaders
CustomItem.registerItemLoader(this);
SimpleItem.registerItemLoader(this);
Expand Down
66 changes: 42 additions & 24 deletions src/main/java/com/dre/brewery/configuration/ConfigHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,72 @@
import eu.okaeri.configs.serdes.OkaeriSerdesPack;
import eu.okaeri.configs.serdes.standard.StandardSerdes;
import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer;
import org.bukkit.plugin.java.JavaPlugin;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

/**
* A class which manages the creation and retrieval of config files. This class
* can be used as a singleton {@link ConfigManager} or as a standalone class {@link com.dre.brewery.api.addons.AddonConfigManager}.
* can be used as a singleton: {@link ConfigManager}, or as a standalone class: {@link com.dre.brewery.api.addons.AddonConfigManager}.
*/
public class ConfigHead {

public final Map<Class<? extends Configurer>, Configurer> CONFIGURERS = new HashMap<>(Map.of(
BreweryXConfigurer.class, new BreweryXConfigurer(),
YamlSnakeYamlConfigurer.class, new YamlSnakeYamlConfigurer()
@Getter
private final Map<Class<? extends Configurer>, Supplier<Configurer>> configurerSupplierMap = new HashMap<>(Map.of(
BreweryXConfigurer.class, BreweryXConfigurer::new,
YamlSnakeYamlConfigurer.class, YamlSnakeYamlConfigurer::new
));
@Getter
private final Set<OkaeriSerdesPack> preparedSerdesPacks = new HashSet<>();
@Getter
private final Set<BidirectionalTransformer<?, ?>> preparedBiDirectionalTransformers = new HashSet<>();

// Should be private and use getters
// These can stay public, the fields above should be private though.
public final Map<Class<? extends AbstractOkaeriConfigFile>, AbstractOkaeriConfigFile> LOADED_CONFIGS = new HashMap<>();
public Path DATA_FOLDER;

public ConfigHead() {
// This sometimes loads before onLoad(), so we just get our instance straight from Bukkit instead.
this.DATA_FOLDER = BreweryPlugin.getInstance().getDataFolder().toPath();
}

public ConfigHead(Path dataFolder) {
this.DATA_FOLDER = dataFolder;
}

/**
* Get a configurer from the CONFIGURERS map with all of the packs and transformers added.
* @param configurerClass The class of the configurer to get
* @return The configurer instance
* @param <T> The type of the configurer
*/
@NotNull
public <T extends Configurer> T getConfigurer(Class<T> configurerClass) {
if (!configurerSupplierMap.containsKey(configurerClass)) {
Logging.errorLog("Tried to get a Configurer that doesn't exist to this ConfigHead: " + configurerClass.getCanonicalName());
return (T) configurerSupplierMap.get(BreweryXConfigurer.class).get();
}

T configurer = (T) configurerSupplierMap.get(configurerClass).get();
for (OkaeriSerdesPack pack : preparedSerdesPacks) {
configurer.register(pack);
}
for (BidirectionalTransformer<?, ?> transformer : preparedBiDirectionalTransformers) {
configurer.register(registry -> registry.register(transformer));
}
return configurer;
}

/**
* Get a config instance from the LOADED_CONFIGS map, or create a new instance if it doesn't exist
Expand Down Expand Up @@ -161,12 +192,7 @@ public <T extends AbstractOkaeriConfigFile> T createConfig(Class<T> configClass,
*/
public <T extends AbstractOkaeriConfigFile> T createConfig(Class<T> configClass, Path file) {
OkaeriConfigFileOptions options = getOkaeriConfigFileOptions(configClass);

Configurer configurer = CONFIGURERS.get(options.configurer());
if (configurer == null) {
Logging.errorLog("Configurer cannot be null. Make sure you've registered the configurer before trying to use it!");
configurer = CONFIGURERS.get(BreweryXConfigurer.class);
}
Configurer configurer = this.getConfigurer(options.configurer());

return createConfig(configClass, file, configurer, new StandardSerdes(), options.update(), options.removeOrphans());
}
Expand Down Expand Up @@ -202,11 +228,7 @@ public <T extends AbstractOkaeriConfigFile> T createBlankConfigInstance(Class<T>
* @param packs The array of OkaeriSerdesPack instances to be added to the configurers.
*/
public void addSerdesPacks(OkaeriSerdesPack... packs) {
CONFIGURERS.values().forEach(configurer -> {
for (OkaeriSerdesPack pack : packs) {
configurer.register(pack);
}
});
preparedSerdesPacks.addAll(Arrays.asList(packs));
}

/**
Expand All @@ -216,11 +238,7 @@ public void addSerdesPacks(OkaeriSerdesPack... packs) {
* @param transformers The array of BidirectionalTransformer instances to be added to the configurers.
*/
public void addBidirectionalTransformers(BidirectionalTransformer<?, ?>... transformers) {
CONFIGURERS.values().forEach(configurer -> {
for (BidirectionalTransformer<?, ?> transformer : transformers) {
configurer.register(registry -> registry.register(transformer));
}
});
preparedBiDirectionalTransformers.addAll(Arrays.asList(transformers));
}

/**
Expand All @@ -230,7 +248,7 @@ public void addBidirectionalTransformers(BidirectionalTransformer<?, ?>... trans
* @param configurer The Configurer instance to be added.
*/
public void addConfigurer(Configurer configurer) {
CONFIGURERS.put(configurer.getClass(), configurer);
configurerSupplierMap.put(configurer.getClass(), () -> configurer);
}

// Util
Expand Down

0 comments on commit 8d7e61c

Please sign in to comment.