Skip to content
Closed
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ that don't want to install mods on the client.
This mod was heavily inspired by [Aurora's Decorations](https://modrinth.com/mod/aurorasdecorations) mod,
which you should try out/use instead if you aren't looking for server side mod!

## Configuration
Features can be configured in `config/polydecorations.json` (created on first start, with everything enabled).
Every feature can be set to one of these values:
- `true` - enabled (default).
- `false` - its recipes are removed, but its blocks and items still exist, so nothing gets removed from existing worlds.
Features without recipes (`wall_lanterns`, `fence_leads`) stop working instead.
- `"hard"` - removed completely. Its blocks, items and entities aren't registered, it doesn't use
any Polymer block states and its assets aren't added to the resource pack.
Blocks and items of this feature are removed from existing worlds!

```json
{
"features": {
"bench": false,
"statues": "hard"
}
}
```

Features: `canvas`, `mailbox`, `sign_post`, `rope`, `hammer`, `trowel`, `wall_lanterns` (placing lanterns on walls),
`fence_leads` (tying a lead to a fence without a leashed mob), `shelf`, `bench`, `table`, `tool_rack`, `stump`, `sleeping_bag`,
`brazier`, `copper_campfire`, `globe`, `display_case`, `flower_pots`, `ghost_lights`, `trashcan`, `basket`, `cardboard_box`,
`wind_chime`, `statues` and `tied_containers` (tying containers with string to hide their contents).

## Extra mods you might want to use!

### Polymer AutoHost
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/eu/pb4/polydecorations/DecorationsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package eu.pb4.polydecorations;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Files;
import java.util.EnumMap;
import java.util.Map;

// Loaded from the mixin plugin, so it can't touch any Minecraft classes
public final class DecorationsConfig {
private static final Logger LOGGER = LoggerFactory.getLogger("PolyDecorations");
private static final Map<DecorationsFeature, Mode> MODES = load();

public static Mode mode(DecorationsFeature feature) {
return MODES.getOrDefault(feature, Mode.ENABLED);
}

private static Map<DecorationsFeature, Mode> load() {
var modes = new EnumMap<DecorationsFeature, Mode>(DecorationsFeature.class);
if (System.getProperty("fabric-api.datagen") != null) {
return modes;
}

var path = FabricLoader.getInstance().getConfigDir().resolve("polydecorations.json");
var features = new JsonObject();
var write = true;
try {
if (Files.exists(path)) {
var json = JsonParser.parseString(Files.readString(path)).getAsJsonObject();
if (json.has("features")) {
features = json.getAsJsonObject("features");
}
write = false;
}

for (var key : features.keySet()) {
if (DecorationsFeature.byKey(key) == null) {
LOGGER.warn("Unknown feature '{}' in config/polydecorations.json", key);
}
}

for (var feature : DecorationsFeature.values()) {
if (!features.has(feature.key())) {
features.addProperty(feature.key(), true);
write = true;
continue;
}

var mode = Mode.of(features.get(feature.key()));
if (mode == null) {
LOGGER.warn("Invalid value for '{}' in config/polydecorations.json, expected true, false or \"hard\"", feature.key());
} else if (mode != Mode.ENABLED) {
modes.put(feature, mode);
}
}
} catch (Throwable e) {
LOGGER.error("Failed to read config/polydecorations.json, all features will be enabled!", e);
return new EnumMap<>(DecorationsFeature.class);
}

if (write) {
try {
var json = new JsonObject();
json.add("features", features);
Files.createDirectories(path.getParent());
Files.writeString(path, new GsonBuilder().setPrettyPrinting().create().toJson(json));
} catch (Throwable e) {
LOGGER.warn("Failed to write config/polydecorations.json", e);
}
}

if (!modes.isEmpty()) {
LOGGER.info("Disabled features: {}", modes);
}
return modes;
}

public enum Mode {
ENABLED,
// Content stays registered, but can't be crafted or obtained in survival
DISABLED,
// Content isn't registered at all
HARD;

@Nullable
private static Mode of(JsonElement element) {
if (!(element instanceof JsonPrimitive primitive)) {
return null;
}
if (primitive.isBoolean()) {
return primitive.getAsBoolean() ? ENABLED : DISABLED;
}
return switch (primitive.getAsString()) {
case "true" -> ENABLED;
case "false" -> DISABLED;
case "hard" -> HARD;
default -> null;
};
}
}
}
116 changes: 116 additions & 0 deletions src/main/java/eu/pb4/polydecorations/DecorationsFeature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package eu.pb4.polydecorations;

import org.jetbrains.annotations.Nullable;

import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;

// Patterns match names of assets (without type folder and extension, like "oak_bench_left" or "statue/oak/head"),
// recipes, loot tables and translation keys. Names not matching any feature are shared.
// Loaded from the mixin plugin, so it can't touch any Minecraft classes
public enum DecorationsFeature {
CANVAS("canvas", "canvas_*", "font/canvas", "sgui/pixel"),
MAILBOX("*_mailbox", "base_mailbox", "mailbox_*", "sgui/mailbox_select", "sgui/shelf_2", "palettes/wood/*"),
SIGN_POST("*_sign_post", "base_sign_post", "sign_post", "sign_post_*", "palettes/wood/*"),
ROPE("rope", "rope_*", "rope/*"),
HAMMER("hammer"),
TROWEL("trowel"),
WALL_LANTERNS("*wall_lantern", "lantern_support", "lantern_support/*"),
FENCE_LEADS("first_leash_fence_knot"),
SHELF("*_shelf", "*_shelf_top", "*_shelf_double", "base_shelf*", "sgui/shelf", "sgui/shelf_2"),
BENCH("*_bench", "*_bench_*", "base_bench*", "seat"),
TABLE("*_table", "*_table_*", "base_table*"),
TOOL_RACK("*_tool_rack", "base_tool_rack"),
STUMP("*_stump", "*_stump_tall", "*_stump_top", "base_stump*", "seat"),
SLEEPING_BAG("*sleeping_bag*", "palettes/bed_color/*"),
BRAZIER("*brazier*", "empty2", "copper_campfire_fire", "copper_campfire_log_lit", "palette/copper_bars/*"),
COPPER_CAMPFIRE("copper_campfire*"),
GLOBE("globe*", "tiny_potato"),
DISPLAY_CASE("display_case*"),
FLOWER_POTS("large_flower_pot", "long_flower_pot", "long_flower_pot/*", "empty"),
GHOST_LIGHTS("*ghost_light"),
TRASHCAN("trashcan*", "trashcan/*", "sgui/trashcan"),
BASKET("basket*"),
CARDBOARD_BOX("cardboard_box*"),
WIND_CHIME("wind_chime*", "wind_chime/*"),
STATUES("statue", "*_statue", "statue/*"),
TIED_CONTAINERS("tied", "tie_container");

private final String key;
private final Pattern assets;

DecorationsFeature(String... assetPatterns) {
this.key = this.name().toLowerCase(Locale.ROOT);
var regex = new StringBuilder();
for (var pattern : assetPatterns) {
if (!regex.isEmpty()) {
regex.append('|');
}
regex.append(Pattern.quote(pattern).replace("*", "\\E.*\\Q"));
}
this.assets = Pattern.compile(regex.toString());
}

public String key() {
return this.key;
}

public DecorationsConfig.Mode mode() {
return DecorationsConfig.mode(this);
}

// Can be crafted and obtained in survival
public boolean isEnabled() {
return this.mode() == DecorationsConfig.Mode.ENABLED;
}

// Has its blocks, items and entities registered
public boolean isRegistered() {
return this.mode() != DecorationsConfig.Mode.HARD;
}

public static boolean anyRegistered(DecorationsFeature... features) {
for (var feature : features) {
if (feature.isRegistered()) {
return true;
}
}
return false;
}

public static boolean anyHardDisabled() {
for (var feature : values()) {
if (!feature.isRegistered()) {
return true;
}
}
return false;
}

public static Set<DecorationsFeature> owners(String name) {
var set = EnumSet.noneOf(DecorationsFeature.class);
for (var feature : values()) {
if (feature.assets.matcher(name).matches()) {
set.add(feature);
}
}
return set;
}

public static boolean isUsed(String name) {
var owners = owners(name);
return owners.isEmpty() || owners.stream().anyMatch(DecorationsFeature::isRegistered);
}

@Nullable
public static DecorationsFeature byKey(String key) {
for (var feature : values()) {
if (feature.key.equals(key)) {
return feature;
}
}
return null;
}
}
58 changes: 58 additions & 0 deletions src/main/java/eu/pb4/polydecorations/DecorationsMixinPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package eu.pb4.polydecorations;

import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static eu.pb4.polydecorations.DecorationsFeature.*;

public class DecorationsMixinPlugin implements IMixinConfigPlugin {
private static final String PACKAGE = "eu.pb4.polydecorations.mixin.";
// Mixins only needed by some features, skipped if all of them are hard disabled
private static final Map<String, DecorationsFeature[]> FEATURE_MIXINS = Map.ofEntries(
Map.entry("BlockItemMixin", new DecorationsFeature[]{ WALL_LANTERNS }),
Map.entry("LanternBlockMixin", new DecorationsFeature[]{ WALL_LANTERNS, ROPE }),
Map.entry("CeilingHangingSignBlockMixin", new DecorationsFeature[]{ ROPE }),
Map.entry("LeadItemMixin", new DecorationsFeature[]{ FENCE_LEADS }),
Map.entry("FaceAttachedHorizontalDirectionalBlockMixin", new DecorationsFeature[]{ STUMP }),
Map.entry("ShulkerBoxBlockEntityMixin", new DecorationsFeature[]{ BASKET, TIED_CONTAINERS }),
Map.entry("ItemContainerContentsMixin", new DecorationsFeature[]{ TIED_CONTAINERS }),
Map.entry("LivingEntityMixin", new DecorationsFeature[]{ SLEEPING_BAG }),
Map.entry("ServerPlayerMixin", new DecorationsFeature[]{ SLEEPING_BAG }),
Map.entry("SignBlockEntityMixin", new DecorationsFeature[]{ SIGN_POST }),
Map.entry("flowerpot.PlantBlockMixin", new DecorationsFeature[]{ FLOWER_POTS }),
Map.entry("flowerpot.SaplingBlockMixin", new DecorationsFeature[]{ FLOWER_POTS })
);

@Override
public void onLoad(String mixinPackage) {}

@Override
public String getRefMapperConfig() {
return null;
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
var features = FEATURE_MIXINS.get(mixinClassName.startsWith(PACKAGE) ? mixinClassName.substring(PACKAGE.length()) : mixinClassName);
return features == null || DecorationsFeature.anyRegistered(features);
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}

@Override
public List<String> getMixins() {
return null;
}

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {}
}
2 changes: 2 additions & 0 deletions src/main/java/eu/pb4/polydecorations/ModInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void onInitialize() {
LOGGER.warn("=====================================================");
}

FeatureResourceCondition.register();
DecorationsBlocks.register();
DecorationsBlockEntities.register();
DecorationsModels.register();
Expand All @@ -70,6 +71,7 @@ public void onInitialize() {
GuiTextures.register();
PolydexCompat.register();
PolymerResourcePackUtils.addModAssets(ID);
FeatureAssetFilter.register();
PolymerResourcePackUtils.markAsRequired();
ResourcePackExtras.forDefault().addBridgedModelsFolder(id("block"), id("sgui"));
}
Expand Down
Loading