Search in sources :

Example 1 with IGlobalResearchTree

use of com.minecolonies.api.research.IGlobalResearchTree in project minecolonies by ldtteam.

the class GenericRecipeUtils method getResearchDisplayName.

@NotNull
private static ITextComponent getResearchDisplayName(@NotNull final ResourceLocation researchId) {
    final IGlobalResearchTree researchTree = IGlobalResearchTree.getInstance();
    // first, try to see if this is a research id
    final IGlobalResearch research = researchTree.getResearch(researchId);
    if (research != null) {
        return research.getName();
    }
    // next, see if it's an effect id
    final Set<IGlobalResearch> researches = researchTree.getResearchForEffect(researchId);
    if (researches != null && !researches.isEmpty()) {
        // there might be more than one, but this should be sufficient for now
        return researches.iterator().next().getName();
    }
    // otherwise it may be an effect with no research (perhaps disabled via datapack)
    return new StringTextComponent("???");
}
Also used : IGlobalResearch(com.minecolonies.api.research.IGlobalResearch) StringTextComponent(net.minecraft.util.text.StringTextComponent) IGlobalResearchTree(com.minecolonies.api.research.IGlobalResearchTree) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IGlobalResearchTree

use of com.minecolonies.api.research.IGlobalResearchTree 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);
        }
    }
}
Also used : GlobalResearch(com.minecolonies.coremod.research.GlobalResearch) ResourceLocation(net.minecraft.util.ResourceLocation) Collection(java.util.Collection) DedicatedServer(net.minecraft.server.dedicated.DedicatedServer) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ResearchEffectCategory(com.minecolonies.coremod.research.ResearchEffectCategory) IGlobalResearchTree(com.minecolonies.api.research.IGlobalResearchTree) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 3 with IGlobalResearchTree

use of com.minecolonies.api.research.IGlobalResearchTree in project minecolonies by Minecolonies.

the class GenericRecipeUtils method getResearchDisplayName.

@NotNull
private static ITextComponent getResearchDisplayName(@NotNull final ResourceLocation researchId) {
    final IGlobalResearchTree researchTree = IGlobalResearchTree.getInstance();
    // first, try to see if this is a research id
    final IGlobalResearch research = researchTree.getResearch(researchId);
    if (research != null) {
        return research.getName();
    }
    // next, see if it's an effect id
    final Set<IGlobalResearch> researches = researchTree.getResearchForEffect(researchId);
    if (researches != null && !researches.isEmpty()) {
        // there might be more than one, but this should be sufficient for now
        return researches.iterator().next().getName();
    }
    // otherwise it may be an effect with no research (perhaps disabled via datapack)
    return new StringTextComponent("???");
}
Also used : IGlobalResearch(com.minecolonies.api.research.IGlobalResearch) StringTextComponent(net.minecraft.util.text.StringTextComponent) IGlobalResearchTree(com.minecolonies.api.research.IGlobalResearchTree) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with IGlobalResearchTree

use of com.minecolonies.api.research.IGlobalResearchTree in project minecolonies by Minecolonies.

the class ResearchListener method calcResearchTree.

/**
 * Parses out a GlobalResearch map to apply parent/child relationships between researches, and to graft and warn about inconsistent relationships.
 *
 * @param researchMap   A Map of ResearchIDs to GlobalResearches to turn into a GlobalResearchTree.
 * @return              An IGlobalResearchTree containing the validated researches.
 */
private IGlobalResearchTree calcResearchTree(final Map<ResourceLocation, GlobalResearch> researchMap) {
    final IGlobalResearchTree researchTree = MinecoloniesAPIProxy.getInstance().getGlobalResearchTree();
    // The research tree should be reset on world unload, but certain events and disconnects break that.  Do it here, too.
    researchTree.reset();
    // Next, set up child relationships, and handle cases where they're not logically consistent.
    for (final Map.Entry<ResourceLocation, GlobalResearch> entry : researchMap.entrySet()) {
        if (entry.getValue().getParent().getPath().isEmpty() && entry.getValue().getDepth() > 1) {
            // For now, log and re-graft entries with no parent and depth to the root of their branch.
            entry.setValue(new GlobalResearch(entry.getValue().getId(), entry.getValue().getBranch(), 1, entry.getValue().getEffects(), entry.getValue().getIconTextureResourceLocation(), entry.getValue().getIconItemStack(), entry.getValue().isImmutable()));
            Log.getLogger().error(entry.getValue().getBranch() + "/" + entry.getKey() + "could not be attached to tree: inconsistent depth for parentage.");
        } else if (!entry.getValue().getParent().getPath().isEmpty()) {
            if (researchMap.containsKey(entry.getValue().getParent())) {
                if (researchMap.get(entry.getValue().getParent()).getBranch().equals(entry.getValue().getBranch())) {
                    researchMap.get(entry.getValue().getParent()).addChild(entry.getValue());
                } else {
                    Log.getLogger().error(entry.getValue().getBranch() + "/" + entry.getKey() + "could not be attached to " + entry.getValue().getParent() + " on " + researchMap.get(entry.getValue().getParent()).getBranch());
                    // For now, log and re-graft entries with inconsistent parent-child relationships as a separate primary research.
                    entry.setValue(new GlobalResearch(entry.getValue().getId(), entry.getValue().getBranch(), 1, entry.getValue().getEffects(), entry.getValue().getIconTextureResourceLocation(), entry.getValue().getIconItemStack(), entry.getValue().isImmutable()));
                }
            } else {
                Log.getLogger().error(entry.getValue().getBranch() + "/" + entry.getKey() + " could not find parent " + entry.getValue().getParent());
                // For now, log and re-graft entries with inconsistent parent-child relationships as a separate primary research.
                entry.setValue(new GlobalResearch(entry.getValue().getId(), entry.getValue().getBranch(), 1, entry.getValue().getEffects(), entry.getValue().getIconTextureResourceLocation(), entry.getValue().getIconItemStack(), entry.getValue().isImmutable()));
            }
        }
        researchTree.addResearch(entry.getValue().getBranch(), entry.getValue(), true);
    }
    return researchTree;
}
Also used : GlobalResearch(com.minecolonies.coremod.research.GlobalResearch) ResourceLocation(net.minecraft.util.ResourceLocation) HashMap(java.util.HashMap) Map(java.util.Map) IGlobalResearchTree(com.minecolonies.api.research.IGlobalResearchTree)

Example 5 with IGlobalResearchTree

use of com.minecolonies.api.research.IGlobalResearchTree 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);
        }
    }
}
Also used : GlobalResearch(com.minecolonies.coremod.research.GlobalResearch) ResourceLocation(net.minecraft.util.ResourceLocation) Collection(java.util.Collection) DedicatedServer(net.minecraft.server.dedicated.DedicatedServer) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ResearchEffectCategory(com.minecolonies.coremod.research.ResearchEffectCategory) IGlobalResearchTree(com.minecolonies.api.research.IGlobalResearchTree) MinecraftServer(net.minecraft.server.MinecraftServer)

Aggregations

IGlobalResearchTree (com.minecolonies.api.research.IGlobalResearchTree)9 ResourceLocation (net.minecraft.util.ResourceLocation)6 IGlobalResearch (com.minecolonies.api.research.IGlobalResearch)4 GlobalResearch (com.minecolonies.coremod.research.GlobalResearch)4 HashMap (java.util.HashMap)3 StringTextComponent (net.minecraft.util.text.StringTextComponent)3 NotNull (org.jetbrains.annotations.NotNull)3 ResearchEffectCategory (com.minecolonies.coremod.research.ResearchEffectCategory)2 Collection (java.util.Collection)2 Map (java.util.Map)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 MinecraftServer (net.minecraft.server.MinecraftServer)2 DedicatedServer (net.minecraft.server.dedicated.DedicatedServer)2 IColony (com.minecolonies.api.colony.IColony)1 ILocalResearch (com.minecolonies.api.research.ILocalResearch)1 ILocalResearchTree (com.minecolonies.api.research.ILocalResearchTree)1 LuaFunction (dan200.computercraft.api.lua.LuaFunction)1 ArrayList (java.util.ArrayList)1