Search in sources :

Example 1 with IGlobalResearch

use of com.minecolonies.api.research.IGlobalResearch 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 2 with IGlobalResearch

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

the class ClientEventHandler method handleCrafterRecipeTooltips.

/**
 * Display crafter recipe-related information on the client.
 * @param colony   The colony to check against, if one is present.
 * @param toolTip  The tooltip to add the text onto.
 * @param item     The item that will have the tooltip text added.
 */
private static void handleCrafterRecipeTooltips(@Nullable final IColony colony, final List<ITextComponent> toolTip, final Item item) {
    final List<CustomRecipe> recipes = CustomRecipeManager.getInstance().getRecipeByOutput(item);
    if (recipes.isEmpty()) {
        return;
    }
    for (CustomRecipe rec : recipes) {
        if (!rec.getShowTooltip() || rec.getCrafter().length() < 2) {
            continue;
        }
        final BuildingEntry craftingBuilding = crafterToBuilding.get().get(rec.getCrafter());
        if (craftingBuilding == null)
            continue;
        final ITextComponent craftingBuildingName = getFullBuildingName(craftingBuilding);
        if (rec.getMinBuildingLevel() > 0) {
            final String schematicName = craftingBuilding.getRegistryName().getPath();
            // the above is not guaranteed to match (and indeed doesn't for a few buildings), but
            // does match for all currently interesting crafters, at least.  there doesn't otherwise
            // appear to be an easy way to get the schematic name from a BuildingEntry ... or
            // unless we can change how colony.hasBuilding uses its parameter...
            final IFormattableTextComponent reqLevelText = new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ITEM_BUILDLEVEL_TOOLTIP_GUI, craftingBuildingName, rec.getMinBuildingLevel());
            if (colony != null && colony.hasBuilding(schematicName, rec.getMinBuildingLevel(), true)) {
                reqLevelText.setStyle(Style.EMPTY.withColor(TextFormatting.AQUA));
            } else {
                reqLevelText.setStyle(Style.EMPTY.withColor(TextFormatting.RED));
            }
            toolTip.add(reqLevelText);
        } else {
            final IFormattableTextComponent reqBuildingTxt = new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ITEM_AVAILABLE_TOOLTIP_GUI, craftingBuildingName).setStyle(Style.EMPTY.withItalic(true).withColor(TextFormatting.GRAY));
            toolTip.add(reqBuildingTxt);
        }
        if (rec.getRequiredResearchId() != null) {
            final Set<IGlobalResearch> researches;
            if (IMinecoloniesAPI.getInstance().getGlobalResearchTree().hasResearch(rec.getRequiredResearchId())) {
                researches = new HashSet<>();
                researches.add(IMinecoloniesAPI.getInstance().getGlobalResearchTree().getResearch(rec.getRequiredResearchId()));
            } else {
                researches = IMinecoloniesAPI.getInstance().getGlobalResearchTree().getResearchForEffect(rec.getRequiredResearchId());
            }
            if (researches != null) {
                final TextFormatting researchFormat;
                if (colony != null && (colony.getResearchManager().getResearchTree().hasCompletedResearch(rec.getRequiredResearchId()) || colony.getResearchManager().getResearchEffects().getEffectStrength(rec.getRequiredResearchId()) > 0)) {
                    researchFormat = TextFormatting.AQUA;
                } else {
                    researchFormat = TextFormatting.RED;
                }
                for (IGlobalResearch research : researches) {
                    toolTip.add(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ITEM_REQUIRES_RESEARCH_TOOLTIP_GUI, research.getName()).setStyle(Style.EMPTY.withColor(researchFormat)));
                }
            }
        }
    }
}
Also used : CustomRecipe(com.minecolonies.coremod.colony.crafting.CustomRecipe) BuildingEntry(com.minecolonies.api.colony.buildings.registry.BuildingEntry) IGlobalResearch(com.minecolonies.api.research.IGlobalResearch)

Example 3 with IGlobalResearch

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

the class GlobalResearchFactory method deserialize.

@NotNull
@Override
public IGlobalResearch deserialize(@NotNull final IFactoryController controller, @NotNull final CompoundNBT nbt) {
    final ResourceLocation parent = new ResourceLocation(nbt.getString(TAG_PARENT));
    final ResourceLocation id = new ResourceLocation(nbt.getString(TAG_ID));
    final ResourceLocation branch = new ResourceLocation(nbt.getString(TAG_BRANCH));
    final TranslationTextComponent desc = new TranslationTextComponent(nbt.getString(TAG_NAME));
    final int depth = nbt.getInt(TAG_RESEARCH_LVL);
    final int sortOrder = nbt.getInt(TAG_RESEARCH_SORT);
    final boolean onlyChild = nbt.getBoolean(TAG_ONLY_CHILD);
    final ResourceLocation iconTexture = new ResourceLocation(nbt.getString(TAG_ICON_TEXTURE));
    final String[] iconStackParts = nbt.getString(TAG_ICON_ITEM_STACK).split(":");
    final ItemStack iconStack = new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation(iconStackParts[0], iconStackParts[1])));
    iconStack.setCount(Integer.parseInt(iconStackParts[2]));
    final TranslationTextComponent subtitle = new TranslationTextComponent(nbt.getString(TAG_SUBTITLE_NAME));
    final boolean instant = nbt.getBoolean(TAG_INSTANT);
    final boolean autostart = nbt.getBoolean(TAG_AUTOSTART);
    final boolean immutable = nbt.getBoolean(TAG_IMMUTABLE);
    final boolean hidden = nbt.getBoolean(TAG_HIDDEN);
    final IGlobalResearch research = getNewInstance(id, branch, parent, desc, depth, sortOrder, iconTexture, iconStack, subtitle, onlyChild, hidden, autostart, instant, immutable);
    NBTUtils.streamCompound(nbt.getList(TAG_COSTS, Constants.NBT.TAG_COMPOUND)).forEach(compound -> {
        String[] costParts = compound.getString(TAG_COST_ITEM).split(":");
        if (costParts.length == 3) {
            final ItemStack is = new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation(costParts[0], costParts[1])));
            is.setCount(Integer.parseInt(costParts[2]));
            if (compound.hasUUID(TAG_COST_NBT)) {
                is.setTag(compound.getCompound(TAG_COST_NBT));
            }
            research.addCost(new ItemStorage(is, false, !is.hasTag()));
        }
    });
    NBTUtils.streamCompound(nbt.getList(TAG_REQS, Constants.NBT.TAG_COMPOUND)).forEach(compound -> research.addRequirement(Objects.requireNonNull(IResearchRequirementRegistry.getInstance().getValue(ResourceLocation.tryParse(compound.getString(TAG_REQ_TYPE)))).readFromNBT(compound.getCompound(TAG_REQ_ITEM))));
    NBTUtils.streamCompound(nbt.getList(TAG_EFFECTS, Constants.NBT.TAG_COMPOUND)).forEach(compound -> research.addEffect(Objects.requireNonNull(IResearchEffectRegistry.getInstance().getValue(ResourceLocation.tryParse(compound.getString(TAG_EFFECT_TYPE)))).readFromNBT(compound.getCompound(TAG_EFFECT_ITEM))));
    NBTUtils.streamCompound(nbt.getList(TAG_CHILDS, Constants.NBT.TAG_COMPOUND)).forEach(compound -> research.addChild(new ResourceLocation(compound.getString(TAG_RESEARCH_CHILD))));
    return research;
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) IGlobalResearch(com.minecolonies.api.research.IGlobalResearch) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with IGlobalResearch

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

the class GlobalResearchFactory method serialize.

@NotNull
@Override
public CompoundNBT serialize(@NotNull final IFactoryController controller, @NotNull final IGlobalResearch research) {
    final CompoundNBT compound = new CompoundNBT();
    compound.putString(TAG_PARENT, research.getParent().toString());
    compound.putString(TAG_ID, research.getId().toString());
    compound.putString(TAG_BRANCH, research.getBranch().toString());
    compound.putString(TAG_NAME, research.getName().getKey());
    compound.putInt(TAG_RESEARCH_LVL, research.getDepth());
    compound.putInt(TAG_RESEARCH_SORT, research.getSortOrder());
    compound.putBoolean(TAG_ONLY_CHILD, research.hasOnlyChild());
    compound.putString(TAG_ICON_TEXTURE, research.getIconTextureResourceLocation().toString());
    compound.putString(TAG_ICON_ITEM_STACK, research.getIconItemStack().getItem().getRegistryName() + ":" + research.getIconItemStack().getCount());
    compound.putString(TAG_SUBTITLE_NAME, research.getSubtitle().getKey());
    compound.putBoolean(TAG_INSTANT, research.isInstant());
    compound.putBoolean(TAG_AUTOSTART, research.isAutostart());
    compound.putBoolean(TAG_IMMUTABLE, research.isImmutable());
    compound.putBoolean(TAG_HIDDEN, research.isHidden());
    @NotNull final ListNBT costTagList = research.getCostList().stream().map(is -> {
        final CompoundNBT costCompound = new CompoundNBT();
        costCompound.putString(TAG_COST_ITEM, Objects.requireNonNull(is.getItem().getRegistryName()).toString() + ":" + is.getItemStack().getCount());
        if (is.getItemStack().getTag() != null) {
            costCompound.put(TAG_COST_NBT, is.getItemStack().getTag());
        }
        return costCompound;
    }).collect(NBTUtils.toListNBT());
    compound.put(TAG_COSTS, costTagList);
    @NotNull final ListNBT reqTagList = research.getResearchRequirement().stream().map(req -> {
        final CompoundNBT reqCompound = new CompoundNBT();
        reqCompound.putString(TAG_REQ_TYPE, req.getRegistryEntry().getRegistryName().toString());
        reqCompound.put(TAG_REQ_ITEM, req.writeToNBT());
        return reqCompound;
    }).collect(NBTUtils.toListNBT());
    compound.put(TAG_REQS, reqTagList);
    @NotNull final ListNBT effectTagList = research.getEffects().stream().map(eff -> {
        final CompoundNBT effectCompound = new CompoundNBT();
        effectCompound.putString(TAG_EFFECT_TYPE, eff.getRegistryEntry().getRegistryName().toString());
        effectCompound.put(TAG_EFFECT_ITEM, eff.writeToNBT());
        return effectCompound;
    }).collect(NBTUtils.toListNBT());
    compound.put(TAG_EFFECTS, effectTagList);
    @NotNull final ListNBT childTagList = research.getChildren().stream().map(child -> {
        final CompoundNBT childCompound = new CompoundNBT();
        childCompound.putString(TAG_RESEARCH_CHILD, child.toString());
        return childCompound;
    }).collect(NBTUtils.toListNBT());
    compound.put(TAG_CHILDS, childTagList);
    return compound;
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) IResearchRequirementRegistry(com.minecolonies.api.research.registry.IResearchRequirementRegistry) IGlobalResearchFactory(com.minecolonies.api.research.factories.IGlobalResearchFactory) FactoryVoidInput(com.minecolonies.api.colony.requestsystem.factory.FactoryVoidInput) Constants(net.minecraftforge.common.util.Constants) IResearchEffect(com.minecolonies.api.research.effects.IResearchEffect) IResearchEffectRegistry(com.minecolonies.api.research.effects.registry.IResearchEffectRegistry) CompoundNBT(net.minecraft.nbt.CompoundNBT) TypeToken(com.google.common.reflect.TypeToken) IFactoryController(com.minecolonies.api.colony.requestsystem.factory.IFactoryController) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) Objects(java.util.Objects) ItemStack(net.minecraft.item.ItemStack) IGlobalResearch(com.minecolonies.api.research.IGlobalResearch) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) NBTUtils(com.minecolonies.api.util.NBTUtils) ResourceLocation(net.minecraft.util.ResourceLocation) ResearchConstants(com.minecolonies.api.research.util.ResearchConstants) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) IResearchRequirement(com.minecolonies.api.research.IResearchRequirement) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with IGlobalResearch

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

the class TryResearchMessage method onExecute.

@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final BuildingUniversity building) {
    final PlayerEntity player = ctxIn.getSender();
    if (player == null) {
        return;
    }
    final IGlobalResearch research = IGlobalResearchTree.getInstance().getResearch(branch, researchId);
    if (reset) {
        if (colony.getResearchManager().getResearchTree().getResearch(branch, researchId) != null) {
            colony.getResearchManager().getResearchTree().attemptResetResearch(player, colony, colony.getResearchManager().getResearchTree().getResearch(branch, researchId));
        }
    } else {
        if ((research.canResearch(building.getBuildingLevel() == building.getMaxBuildingLevel() ? Integer.MAX_VALUE : building.getBuildingLevel(), colony.getResearchManager().getResearchTree())) || player.isCreative()) {
            colony.getResearchManager().getResearchTree().attemptBeginResearch(player, colony, research);
        }
    }
}
Also used : IGlobalResearch(com.minecolonies.api.research.IGlobalResearch) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Aggregations

IGlobalResearch (com.minecolonies.api.research.IGlobalResearch)18 ResourceLocation (net.minecraft.util.ResourceLocation)12 NotNull (org.jetbrains.annotations.NotNull)8 ItemStack (net.minecraft.item.ItemStack)6 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)6 ItemStorage (com.minecolonies.api.crafting.ItemStorage)4 IGlobalResearchBranch (com.minecolonies.api.research.IGlobalResearchBranch)4 TypeToken (com.google.common.reflect.TypeToken)2 BuildingEntry (com.minecolonies.api.colony.buildings.registry.BuildingEntry)2 FactoryVoidInput (com.minecolonies.api.colony.requestsystem.factory.FactoryVoidInput)2 IFactoryController (com.minecolonies.api.colony.requestsystem.factory.IFactoryController)2 IGlobalResearchTree (com.minecolonies.api.research.IGlobalResearchTree)2 IResearchRequirement (com.minecolonies.api.research.IResearchRequirement)2 IResearchEffect (com.minecolonies.api.research.effects.IResearchEffect)2 IResearchEffectRegistry (com.minecolonies.api.research.effects.registry.IResearchEffectRegistry)2 IGlobalResearchFactory (com.minecolonies.api.research.factories.IGlobalResearchFactory)2 IResearchRequirementRegistry (com.minecolonies.api.research.registry.IResearchRequirementRegistry)2 ResearchConstants (com.minecolonies.api.research.util.ResearchConstants)2 NBTUtils (com.minecolonies.api.util.NBTUtils)2 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)2