-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional pack configuration options.
This addresses the feature requests of #33
- Loading branch information
Showing
6 changed files
with
191 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
common/src/main/java/net/darkhax/openloader/packs/PackOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package net.darkhax.openloader.packs; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
import com.mojang.serialization.JsonOps; | ||
import net.darkhax.openloader.Constants; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.ComponentSerialization; | ||
import net.minecraft.server.packs.repository.Pack; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class PackOptions { | ||
|
||
@Expose | ||
@SerializedName("enabled") | ||
public boolean enabled = true; | ||
|
||
@Expose | ||
@SerializedName("required") | ||
public boolean required = true; | ||
|
||
@Expose | ||
@SerializedName("position") | ||
public String position = "top"; | ||
|
||
@Expose | ||
@SerializedName("pack_name") | ||
public JsonElement packName = null; | ||
|
||
@Expose | ||
@SerializedName("description") | ||
public JsonElement description = null; | ||
|
||
@Expose | ||
@SerializedName("description_includes_source") | ||
public boolean addSourceToDescription = true; | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return "PackOptions[enabled=" + enabled + ",required=" + required + ",position=" + position + "]"; | ||
} | ||
|
||
public Pack.Position getPosition() { | ||
|
||
if (position != null) { | ||
|
||
if (position.equalsIgnoreCase("top")) { | ||
return Pack.Position.TOP; | ||
} | ||
|
||
else if (position.equalsIgnoreCase("bottom")) { | ||
return Pack.Position.BOTTOM; | ||
} | ||
|
||
else { | ||
Constants.LOG.error("Position type '{}' is not valid. You must use 'top' or 'bottom'.", this.position); | ||
} | ||
} | ||
|
||
return Pack.Position.TOP; | ||
} | ||
|
||
public Component getDisplayName(String defaultPackName) { | ||
|
||
if (packName != null) { | ||
|
||
if (packName.isJsonPrimitive() && packName.getAsJsonPrimitive().isString()) { | ||
|
||
return Component.literal(packName.getAsString()); | ||
} | ||
|
||
else if (packName.isJsonObject() || packName.isJsonArray()) { | ||
|
||
try { | ||
|
||
return ComponentSerialization.CODEC.decode(JsonOps.INSTANCE, this.packName).getOrThrow(false, error -> Constants.LOG.error("Invalid pack name for pack '{}': {}", defaultPackName, error)).getFirst(); | ||
} | ||
|
||
catch (RuntimeException e) { | ||
|
||
Constants.LOG.error(e); | ||
} | ||
} | ||
} | ||
|
||
return Component.literal(defaultPackName); | ||
} | ||
|
||
public Component getDescription(String pack, Component defaultDescription) { | ||
|
||
if (description != null) { | ||
|
||
if (description.isJsonPrimitive() && description.getAsJsonPrimitive().isString()) { | ||
|
||
return Component.literal(description.getAsString()); | ||
} | ||
|
||
else if (description.isJsonObject() || description.isJsonArray()) { | ||
|
||
try { | ||
|
||
return ComponentSerialization.CODEC.decode(JsonOps.INSTANCE, this.description).getOrThrow(false, error -> Constants.LOG.error("Invalid pack description for pack '{}': {}", pack, error)).getFirst(); | ||
} | ||
|
||
catch (RuntimeException e) { | ||
|
||
Constants.LOG.error(e); | ||
} | ||
} | ||
} | ||
|
||
return defaultDescription; | ||
} | ||
|
||
public static PackOptions readOptions(File packCandidate) { | ||
|
||
final File optionsFile = new File(packCandidate.getParent(), packCandidate.getName() + ".packmeta"); | ||
|
||
if (optionsFile.exists()) { | ||
|
||
if (optionsFile.isFile()) { | ||
|
||
try (FileReader reader = new FileReader(optionsFile)) { | ||
|
||
return Constants.GSON.fromJson(reader, PackOptions.class); | ||
} | ||
|
||
catch (IOException e) { | ||
|
||
Constants.LOG.error("Failed to read pack options. The file is not formatted correctly! {}", optionsFile.getAbsolutePath()); | ||
Constants.LOG.catching(e); | ||
} | ||
} | ||
|
||
else { | ||
|
||
Constants.LOG.error("Pack options must be a file! {}", optionsFile.getAbsolutePath()); | ||
} | ||
} | ||
|
||
// defaults | ||
Constants.LOG.debug("Using default pack options for {}", optionsFile.getAbsolutePath()); | ||
return new PackOptions(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters