Skip to content

Commit

Permalink
Tweaked impl. of the power and global power loaders
Browse files Browse the repository at this point in the history
* Now uses `IExtendedJsonDataLoader#onReject` for handling disabling powers and global power sets
* Now constructs the translation key of powers before registration
* Also fixed an issue with translation keys of powers nested in sub-folders
  • Loading branch information
eggohito committed Jun 30, 2024
1 parent f217048 commit 5772b87
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 90 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ org.gradle.parallel=true
fabric_version=0.97.0+1.20.4
cca_version=5.4.0
pal_version=1.9.0
calio_version=1.14.0-alpha.1+mc.1.20.4
calio_version=1.14.0-alpha.2+mc.1.20.4
clothconfig_version=13.0.121
modmenu_version=9.0.0
aea_version=1.7.1+1.20.4
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/github/apace100/apoli/Apoli.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import io.github.apace100.apoli.registry.ApoliClassData;
import io.github.apace100.apoli.util.*;
import io.github.apace100.apoli.util.modifier.ModifierOperations;
import io.github.apace100.calio.Calio;
import io.github.apace100.calio.util.CalioResourceConditions;
import io.github.ladysnake.pal.AbilitySource;
import io.github.ladysnake.pal.Pal;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.entity.LivingEntity;
Expand Down Expand Up @@ -113,9 +114,7 @@ public void onInitialize() {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new PowerTypes());
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new GlobalPowerSetLoader());

ResourceConditions.register(ApoliResourceConditions.ANY_NAMESPACE_LOADED, jsonObject -> ApoliResourceConditions.namespacesLoaded(jsonObject, PowerTypes.LOADED_NAMESPACES, false));
ResourceConditions.register(ApoliResourceConditions.ALL_NAMESPACES_LOADED, jsonObject -> ApoliResourceConditions.namespacesLoaded(jsonObject, PowerTypes.LOADED_NAMESPACES, true));

CalioResourceConditions.ALIASES.addNamespaceAlias(MODID, Calio.MOD_NAMESPACE);
Criteria.register(GainedPowerCriterion.ID.toString(), GainedPowerCriterion.INSTANCE);

LOGGER.info("Apoli " + VERSION + " has initialized. Ready to power up your game!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
public class GlobalPowerSetLoader extends IdentifiableMultiJsonDataLoader implements IdentifiableResourceReloadListener {

public static final Identifier PHASE = Apoli.identifier("phase/global_powers");

public static final Set<Identifier> DEPENDENCIES = Util.make(new HashSet<>(), set -> set.add(Apoli.identifier("powers")));

public static final Set<Identifier> DISABLED = new HashSet<>();
public static final Map<Identifier, GlobalPowerSet> ALL = new HashMap<>();

private static final Gson GSON = new GsonBuilder()
Expand Down Expand Up @@ -51,9 +52,11 @@ public GlobalPowerSetLoader() {
@Override
protected void apply(MultiJsonDataContainer prepared, ResourceManager manager, Profiler profiler) {

ALL.clear();
prevId = null;

DISABLED.clear();
ALL.clear();

Map<Identifier, List<GlobalPowerSet>> loadedGlobalPowerSets = new HashMap<>();

prepared.forEach((packName, id, jsonElement) -> {
Expand Down Expand Up @@ -97,6 +100,7 @@ protected void apply(MultiJsonDataContainer prepared, ResourceManager manager, P
prevPriority = loadingPriority + 1;
}

DISABLED.remove(id);
globalPowerSets.add(globalPowerSet);

} catch (Exception e) {
Expand Down Expand Up @@ -134,6 +138,15 @@ protected void apply(MultiJsonDataContainer prepared, ResourceManager manager, P

}

@Override
public void onReject(String packName, Identifier resourceId) {

if (!ALL.containsKey(resourceId)) {
DISABLED.add(resourceId);
}

}

@Override
public Identifier getFabricId() {
return Apoli.identifier("global_powers");
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/io/github/apace100/apoli/power/PowerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

public class PowerType<T extends Power> {

Expand Down Expand Up @@ -55,9 +56,13 @@ public PowerType<?> setSubPower(boolean subPower) {
return this;
}

public void setTranslationKeys(String name, String description) {
public PowerType<?> setTranslationKeys(String name, String description) {

this.nameTranslationKey = name;
this.descriptionTranslationKey = description;

return this;

}

public PowerType<?> setDisplayTexts(Text name, Text description) {
Expand Down Expand Up @@ -107,23 +112,27 @@ public T get(Entity entity) {
}

public String getOrCreateNameTranslationKey() {
if(nameTranslationKey == null || nameTranslationKey.isEmpty()) {
nameTranslationKey =
"power." + identifier.getNamespace() + "." + identifier.getPath() + ".name";

if (nameTranslationKey == null || nameTranslationKey.isEmpty()) {
this.nameTranslationKey = Util.createTranslationKey("power", identifier) + ".name";
}

return nameTranslationKey;

}

public MutableText getName() {
return name != null ? name.copy() : Text.translatable(getOrCreateNameTranslationKey());
}

public String getOrCreateDescriptionTranslationKey() {
if(descriptionTranslationKey == null || descriptionTranslationKey.isEmpty()) {
descriptionTranslationKey =
"power." + identifier.getNamespace() + "." + identifier.getPath() + ".description";

if (descriptionTranslationKey == null || descriptionTranslationKey.isEmpty()) {
this.descriptionTranslationKey = Util.createTranslationKey("power", identifier) + ".description";
}

return descriptionTranslationKey;

}

public MutableText getDescription() {
Expand Down Expand Up @@ -157,4 +166,4 @@ public boolean equals(Object obj) {
Identifier id = ((PowerType)obj).getIdentifier();
return identifier.equals(id);
}
}
}
46 changes: 24 additions & 22 deletions src/main/java/io/github/apace100/apoli/power/PowerTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.github.apace100.apoli.power.factory.PowerFactories;
import io.github.apace100.apoli.power.factory.PowerFactory;
import io.github.apace100.apoli.registry.ApoliRegistries;
import io.github.apace100.apoli.util.ApoliResourceConditions;
import io.github.apace100.calio.data.IdentifiableMultiJsonDataLoader;
import io.github.apace100.calio.data.MultiJsonDataContainer;
import io.github.apace100.calio.data.SerializableData;
import io.github.apace100.calio.data.SerializableDataTypes;
import io.github.apace100.calio.util.CalioResourceConditions;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand All @@ -30,6 +30,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Util;
import net.minecraft.util.profiler.Profiler;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -213,8 +214,13 @@ protected void apply(MultiJsonDataContainer prepared, ResourceManager manager, P

}

private boolean isResourceConditionValid(Identifier id, JsonObject jo) {
return ApoliResourceConditions.test(id, jo);
@Override
public void onReject(String packName, Identifier resourceId) {

if (!PowerTypeRegistry.contains(resourceId)) {
PowerTypeRegistry.disable(resourceId);
}

}

private void readPower(String packName, Identifier id, JsonObject jsonObject) {
Expand Down Expand Up @@ -264,7 +270,16 @@ else if (PowerTypeRegistry.isDisabled(id)) {

@Nullable
private PowerType<?> readSubPower(String packName, Identifier id, JsonObject jsonObject) {
return readPower(packName, id, jsonObject, true, PowerType::new);

if (CalioResourceConditions.objectMatchesConditions(id, jsonObject)) {
return readPower(packName, id, jsonObject, true, PowerType::new);
}

else {
this.onReject(packName, id);
return null;
}

}

@Nullable
Expand All @@ -273,16 +288,6 @@ private PowerType<?> readPower(String packName, Identifier id, JsonObject jsonOb
Identifier powerFactoryId = SerializableDataTypes.IDENTIFIER.read(JsonHelper.getElement(jsonObject, "type"));
int loadingPriority = JsonHelper.getInt(jsonObject, "loading_priority", 0);

if (!isResourceConditionValid(id, jsonObject)) {

if (!PowerTypeRegistry.contains(id)) {
PowerTypeRegistry.disable(id);
}

return null;

}

boolean multiple = this.isMultiple(powerFactoryId);
if (multiple) {
powerFactoryId = SIMPLE;
Expand Down Expand Up @@ -329,18 +334,15 @@ private PowerType<?> finishReadingPower(BiFunction<Identifier, PowerType<?>, Pow

private PowerType<?> loadPower(Identifier id, PowerFactory<?>.Instance powerFactoryInstance, BiFunction<Identifier, PowerFactory<?>.Instance, PowerType<?>> powerTypeFactory, JsonObject jsonObject, boolean isSubPower) {

JsonElement nameJson = jsonObject.get("name");
JsonElement descriptionJson = jsonObject.get("description");
String nameTranslationKey = Util.createTranslationKey("power", id) + ".name";
String descriptionTranslationKey = Util.createTranslationKey("power", id) + ".description";

Text name = nameJson == null
? null
: ApoliDataTypes.DEFAULT_TRANSLATABLE_TEXT.read(nameJson);
Text description = descriptionJson == null
? null
: ApoliDataTypes.DEFAULT_TRANSLATABLE_TEXT.read(descriptionJson);
Text name = ApoliDataTypes.DEFAULT_TRANSLATABLE_TEXT.read(jsonObject.asMap().getOrDefault("name", new JsonPrimitive(nameTranslationKey)));
Text description = ApoliDataTypes.DEFAULT_TRANSLATABLE_TEXT.read(jsonObject.asMap().getOrDefault("description", new JsonPrimitive(descriptionTranslationKey)));

boolean hidden = JsonHelper.getBoolean(jsonObject, "hidden", false);
return powerTypeFactory.apply(id, powerFactoryInstance)
.setTranslationKeys(nameTranslationKey, descriptionTranslationKey)
.setDisplayTexts(name, description)
.setHidden(hidden || isSubPower)
.setSubPower(isSubPower);
Expand Down

This file was deleted.

0 comments on commit 5772b87

Please sign in to comment.