use of com.minecolonies.coremod.research.ResearchEffectCategory in project minecolonies by ldtteam.
the class ResearchListener method apply.
@Override
protected void apply(@NotNull final Map<ResourceLocation, JsonElement> object, @NotNull final IResourceManager resourceManagerIn, @NotNull final IProfiler profilerIn) {
Log.getLogger().info("Beginning load of research for University.");
// First, index and map out all research effects. We need to be able to map them before creating Researches themselves.
// Because data packs, can't assume effects are in one specific location.
// For now, we'll populate relative levels when doing so, but we probably want to do that dynamically.
final Map<ResourceLocation, ResearchEffectCategory> effectCategories = parseResearchEffects(object);
// We /shouldn't/ get any removes before the Research they're trying to remove exists,
// but it can happen if multiple data packs affect each other.
// Instead, get lists of research locations for researches and branches to not load and quit their parsing early.
final Tuple<Collection<ResourceLocation>, Collection<ResourceLocation>> removeResearchesAndBranches = parseRemoveResearches(object);
// Next, populate a new map of IGlobalResearches, identified by researchID.
// This allows us to figure out root/branch relationships more sanely.
// We need the effectCategories and levels to do this.
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
final Map<ResourceLocation, GlobalResearch> researchMap = parseResearches(object, effectCategories, removeResearchesAndBranches.getA(), removeResearchesAndBranches.getB(), !(server instanceof DedicatedServer));
// After we've loaded all researches, we can then try to assign child relationships.
// This is also the phase where we'd try to support back-calculating university levels for researches without them/with incorrect ones.
final IGlobalResearchTree researchTree = calcResearchTree(researchMap);
// Finally, check for branch-specific settings -- these are optional and only apply to the IGlobalResearchTree.
parseResearchBranches(object, researchTree);
Log.getLogger().info("Loaded " + researchMap.values().size() + " recipes for " + researchTree.getBranches().size() + " research branches");
// We only need to send to players during a data pack reload event during live play.
if (server != null) {
for (ServerPlayerEntity player : server.getPlayerList().getPlayers()) {
researchTree.sendGlobalResearchTreePackets(player);
}
}
}
use of com.minecolonies.coremod.research.ResearchEffectCategory in project minecolonies by ldtteam.
the class ResearchListener method parseResearchEffects.
/**
* Parses out a json map for elements containing ResearchEffects, categorizes those effects, and calculates relative values for each effect level.
*
* @param object A Map containing the resource location of each json file, and the element within that json file.
* @return A Map containing the ResearchEffectIds and ResearchEffectCategories each ID corresponds to.
*/
private Map<ResourceLocation, ResearchEffectCategory> parseResearchEffects(final Map<ResourceLocation, JsonElement> object) {
final Map<ResourceLocation, ResearchEffectCategory> effectCategories = new HashMap<>();
for (final Map.Entry<ResourceLocation, JsonElement> entry : object.entrySet()) {
final JsonObject effectJson = entry.getValue().getAsJsonObject();
if (effectJson.has(RESEARCH_EFFECT_PROP)) {
final ResearchEffectCategory category;
if ((effectJson.has(RESEARCH_NAME_PROP) && effectJson.get(RESEARCH_NAME_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_NAME_PROP).getAsJsonPrimitive().isString()) && effectJson.has(RESEARCH_SUBTITLE_PROP) && effectJson.get(RESEARCH_SUBTITLE_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_SUBTITLE_PROP).getAsJsonPrimitive().isString()) {
category = new ResearchEffectCategory(entry.getKey().toString(), effectJson.get(RESEARCH_NAME_PROP).getAsString(), effectJson.get(RESEARCH_SUBTITLE_PROP).getAsString());
} else if ((effectJson.has(RESEARCH_NAME_PROP) && effectJson.get(RESEARCH_NAME_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_NAME_PROP).getAsJsonPrimitive().isString())) {
category = new ResearchEffectCategory(entry.getKey().toString(), effectJson.get(RESEARCH_NAME_PROP).getAsString());
} else if (effectJson.has(RESEARCH_SUBTITLE_PROP) && effectJson.get(RESEARCH_SUBTITLE_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_SUBTITLE_PROP).getAsJsonPrimitive().isString()) {
category = new ResearchEffectCategory(entry.getKey().toString(), null, effectJson.get(RESEARCH_SUBTITLE_PROP).getAsString());
} else {
category = new ResearchEffectCategory(entry.getKey().toString());
}
if (effectJson.has(RESEARCH_EFFECT_LEVELS_PROP) && effectJson.get(RESEARCH_EFFECT_LEVELS_PROP).isJsonArray()) {
for (JsonElement levelElement : effectJson.get(RESEARCH_EFFECT_LEVELS_PROP).getAsJsonArray()) {
if (levelElement.isJsonPrimitive() && levelElement.getAsJsonPrimitive().isNumber()) {
category.add(levelElement.getAsNumber().floatValue());
}
}
} else // If no levels are defined, assume will go from zero to max level as a single action, and store on/off.
{
category.add(5f);
}
effectCategories.put(category.getId(), category);
} else // Files which declare effect: or effectType:, but lack ID or have the wrong types are malformed.
if (effectJson.has(RESEARCH_EFFECT_PROP)) {
Log.getLogger().error(entry.getKey() + " is a research effect, but does not contain all required fields. Research Effects must have effect: and id:string fields.");
}
}
return effectCategories;
}
use of com.minecolonies.coremod.research.ResearchEffectCategory in project minecolonies by Minecolonies.
the class ResearchListener method apply.
@Override
protected void apply(@NotNull final Map<ResourceLocation, JsonElement> object, @NotNull final IResourceManager resourceManagerIn, @NotNull final IProfiler profilerIn) {
Log.getLogger().info("Beginning load of research for University.");
// First, index and map out all research effects. We need to be able to map them before creating Researches themselves.
// Because data packs, can't assume effects are in one specific location.
// For now, we'll populate relative levels when doing so, but we probably want to do that dynamically.
final Map<ResourceLocation, ResearchEffectCategory> effectCategories = parseResearchEffects(object);
// We /shouldn't/ get any removes before the Research they're trying to remove exists,
// but it can happen if multiple data packs affect each other.
// Instead, get lists of research locations for researches and branches to not load and quit their parsing early.
final Tuple<Collection<ResourceLocation>, Collection<ResourceLocation>> removeResearchesAndBranches = parseRemoveResearches(object);
// Next, populate a new map of IGlobalResearches, identified by researchID.
// This allows us to figure out root/branch relationships more sanely.
// We need the effectCategories and levels to do this.
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
final Map<ResourceLocation, GlobalResearch> researchMap = parseResearches(object, effectCategories, removeResearchesAndBranches.getA(), removeResearchesAndBranches.getB(), !(server instanceof DedicatedServer));
// After we've loaded all researches, we can then try to assign child relationships.
// This is also the phase where we'd try to support back-calculating university levels for researches without them/with incorrect ones.
final IGlobalResearchTree researchTree = calcResearchTree(researchMap);
// Finally, check for branch-specific settings -- these are optional and only apply to the IGlobalResearchTree.
parseResearchBranches(object, researchTree);
Log.getLogger().info("Loaded " + researchMap.values().size() + " recipes for " + researchTree.getBranches().size() + " research branches");
// We only need to send to players during a data pack reload event during live play.
if (server != null) {
for (ServerPlayerEntity player : server.getPlayerList().getPlayers()) {
researchTree.sendGlobalResearchTreePackets(player);
}
}
}
use of com.minecolonies.coremod.research.ResearchEffectCategory in project minecolonies by Minecolonies.
the class ResearchListener method parseResearchEffects.
/**
* Parses out a json map for elements containing ResearchEffects, categorizes those effects, and calculates relative values for each effect level.
*
* @param object A Map containing the resource location of each json file, and the element within that json file.
* @return A Map containing the ResearchEffectIds and ResearchEffectCategories each ID corresponds to.
*/
private Map<ResourceLocation, ResearchEffectCategory> parseResearchEffects(final Map<ResourceLocation, JsonElement> object) {
final Map<ResourceLocation, ResearchEffectCategory> effectCategories = new HashMap<>();
for (final Map.Entry<ResourceLocation, JsonElement> entry : object.entrySet()) {
final JsonObject effectJson = entry.getValue().getAsJsonObject();
if (effectJson.has(RESEARCH_EFFECT_PROP)) {
final ResearchEffectCategory category;
if ((effectJson.has(RESEARCH_NAME_PROP) && effectJson.get(RESEARCH_NAME_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_NAME_PROP).getAsJsonPrimitive().isString()) && effectJson.has(RESEARCH_SUBTITLE_PROP) && effectJson.get(RESEARCH_SUBTITLE_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_SUBTITLE_PROP).getAsJsonPrimitive().isString()) {
category = new ResearchEffectCategory(entry.getKey().toString(), effectJson.get(RESEARCH_NAME_PROP).getAsString(), effectJson.get(RESEARCH_SUBTITLE_PROP).getAsString());
} else if ((effectJson.has(RESEARCH_NAME_PROP) && effectJson.get(RESEARCH_NAME_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_NAME_PROP).getAsJsonPrimitive().isString())) {
category = new ResearchEffectCategory(entry.getKey().toString(), effectJson.get(RESEARCH_NAME_PROP).getAsString());
} else if (effectJson.has(RESEARCH_SUBTITLE_PROP) && effectJson.get(RESEARCH_SUBTITLE_PROP).isJsonPrimitive() && effectJson.get(RESEARCH_SUBTITLE_PROP).getAsJsonPrimitive().isString()) {
category = new ResearchEffectCategory(entry.getKey().toString(), null, effectJson.get(RESEARCH_SUBTITLE_PROP).getAsString());
} else {
category = new ResearchEffectCategory(entry.getKey().toString());
}
if (effectJson.has(RESEARCH_EFFECT_LEVELS_PROP) && effectJson.get(RESEARCH_EFFECT_LEVELS_PROP).isJsonArray()) {
for (JsonElement levelElement : effectJson.get(RESEARCH_EFFECT_LEVELS_PROP).getAsJsonArray()) {
if (levelElement.isJsonPrimitive() && levelElement.getAsJsonPrimitive().isNumber()) {
category.add(levelElement.getAsNumber().floatValue());
}
}
} else // If no levels are defined, assume will go from zero to max level as a single action, and store on/off.
{
category.add(5f);
}
effectCategories.put(category.getId(), category);
} else // Files which declare effect: or effectType:, but lack ID or have the wrong types are malformed.
if (effectJson.has(RESEARCH_EFFECT_PROP)) {
Log.getLogger().error(entry.getKey() + " is a research effect, but does not contain all required fields. Research Effects must have effect: and id:string fields.");
}
}
return effectCategories;
}
Aggregations