Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.bencodez.votingplugin.neoforge;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.spongepowered.configurate.yaml.YamlConfigurationLoader;

/** Derives missing-file defaults from the shared Bukkit templates, not a second schema. */
final class NeoForgeConfigurationDefaults {
private NeoForgeConfigurationDefaults() { }

static byte[] fromShared(String name, byte[] template) throws IOException {
if (!List.of("Config.yml", "VoteSites.yml", "SpecialRewards.yml").contains(name)) return template;
var node = YamlConfigurationLoader.builder().source(() -> new BufferedReader(
new StringReader(new String(template, StandardCharsets.UTF_8)))).build().load();
switch (name) {
case "Config.yml" -> {
for (String key : List.of("CloseInventoryOnVote", "UseVoteStreaks", "PerSiteCoolDownEvents",
"LimitMonthlyVotes")) node.node(key).set(false);
node.node("OfflineVotesLimit", "Enabled").set(false);
node.node("VoteBroadcast", "Type").set("NONE");
}
case "SpecialRewards.yml" -> {
node.node("VoteParty", "Enabled").set(false);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
node.node("EnableMonthlyAwards").set(false);
for (String key : List.of("AnySiteRewards", "VoteCoolDownEndedReward", "VoteMilestones",
"VoteStreaks", "VoteStreak", "Cumulative", "MileStones", "FirstVote",
"FirstVoteToday", "AllSites", "AlmostAllSites")) node.node(key).set(java.util.Map.of());
}
case "VoteSites.yml" -> {
// The shared examples combine unsupported actions. Keep their site settings,
// but offer only one supported action, without opting into offline execution.
for (var site : node.node("VoteSites").childrenMap().values()) {
site.node("ForceOffline").set(false);
site.node("GiveOffline").set(false);
var commands = site.node("Rewards", "Commands").copy();
site.node("Rewards").set(java.util.Map.of());
site.node("Rewards", "Commands").set(commands);
}
}
default -> throw new IllegalStateException("Unexpected default template");
}
StringWriter rendered = new StringWriter();
YamlConfigurationLoader.builder().sink(() -> new BufferedWriter(rendered)).build().save(node);
return rendered.toString().getBytes(StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static NeoForgeRewardPlan.Action expand(NeoForgeRewardPlan.Action action
.replace("%ServiceSite%", serviceSite).replace("%servicesite%", serviceSite));
}

private String unsupportedGlobalBehavior() {
String unsupportedGlobalBehavior() {
if (node(config, ignoreCase, "CloseInventoryOnVote").getBoolean(true)) {
return "CloseInventoryOnVote is not supported by NeoForge replay yet";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -10,6 +11,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;

import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
Expand Down Expand Up @@ -98,6 +100,13 @@ private static NeoForgeRuntime start(Path directory, Clock clock, Object server)
ConfigurationNode voteSites = YamlConfigurationLoader.builder().path(voteSitesFile).build().load();
ConfigurationNode specialRewards = YamlConfigurationLoader.builder().path(specialRewardsFile).build().load();
ConfigurationNode bungeeSettings = YamlConfigurationLoader.builder().path(bungeeSettingsFile).build().load();
String rewardBlocker = new NeoForgeRewardConfiguration(config, voteSites, specialRewards)
.unsupportedGlobalBehavior();
if (rewardBlocker != null) {
Logger.getLogger(NeoForgeRuntime.class.getName()).warning("NeoForge reward configuration: "
+ rewardBlocker + "; affected votes remain pending. Review the NeoForge setup guide;"
+ " existing configuration files are not changed.");
}
NeoForgeVoteConfiguration voteConfiguration = NeoForgeVoteConfiguration.load(config, voteSites);
NeoForgeProxySocketConfiguration proxyConfiguration;
try {
Expand Down Expand Up @@ -138,8 +147,9 @@ private static Path installDefault(Path directory, String name) throws IOExcepti
if (Files.notExists(target)) {
try (InputStream resource = NeoForgeRuntime.class.getClassLoader().getResourceAsStream(name)) {
if (resource == null) throw new IOException("Missing packaged configuration: " + name);
byte[] initial = NeoForgeConfigurationDefaults.fromShared(name, resource.readAllBytes());
try {
Files.copy(resource, target);
Files.copy(new ByteArrayInputStream(initial), target);
} catch (FileAlreadyExistsException ignored) {
// Another bootstrap installed the same file; load the installed file below.
}
Expand Down
Loading
Loading