Search in sources :

Example 16 with MaterialInstance

use of net.silentchaos512.gear.gear.material.MaterialInstance in project Silent-Gear by SilentChaos512.

the class CustomMaterialItem method getMaterial.

@Nullable
public static MaterialInstance getMaterial(ItemStack stack) {
    String str = stack.getOrCreateTag().getString(NBT_MATERIAL);
    ResourceLocation id = SilentGear.getIdWithDefaultNamespace(str);
    if (id != null) {
        IMaterial material = MaterialManager.get(id);
        if (material != null) {
            return MaterialInstance.of(material);
        }
    }
    return null;
}
Also used : IMaterial(net.silentchaos512.gear.api.material.IMaterial) ResourceLocation(net.minecraft.resources.ResourceLocation) Nullable(javax.annotation.Nullable)

Example 17 with MaterialInstance

use of net.silentchaos512.gear.gear.material.MaterialInstance in project Silent-Gear by SilentChaos512.

the class RepairKitItem method getStoredMaterials.

private static Map<MaterialInstance, Float> getStoredMaterials(ItemStack stack) {
    CompoundTag nbt = stack.getOrCreateTagElement(NBT_STORAGE);
    List<MaterialInstance> list = nbt.getAllKeys().stream().map(MaterialInstance::readShorthand).filter(Objects::nonNull).sorted(Comparator.<MaterialInstance, Integer>comparing(mat1 -> mat1.getTier(PartType.MAIN)).thenComparing(mat1 -> mat1.getDisplayName(PartType.MAIN, ItemStack.EMPTY).plainCopy().getString())).collect(Collectors.toList());
    Map<MaterialInstance, Float> ret = new LinkedHashMap<>();
    list.forEach(mat -> {
        float value = nbt.getFloat(getShorthandKey(mat));
        ret.put(mat, value);
    });
    return ret;
}
Also used : RepairContext(net.silentchaos512.gear.gear.part.RepairContext) GearData(net.silentchaos512.gear.util.GearData) TextUtil(net.silentchaos512.gear.util.TextUtil) java.util(java.util) ItemStats(net.silentchaos512.gear.api.stats.ItemStats) Component(net.minecraft.network.chat.Component) Item(net.minecraft.world.item.Item) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) Tuple(net.minecraft.util.Tuple) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Pair(com.mojang.datafixers.util.Pair) PartType(net.silentchaos512.gear.api.part.PartType) CompoundTag(net.minecraft.nbt.CompoundTag) ChatFormatting(net.minecraft.ChatFormatting) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) TooltipFlag(net.minecraft.world.item.TooltipFlag) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) Mth(net.minecraft.util.Mth) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 18 with MaterialInstance

use of net.silentchaos512.gear.gear.material.MaterialInstance in project Silent-Gear by SilentChaos512.

the class RepairKitItem method getMaterialsToRepair.

private Pair<Map<MaterialInstance, Float>, Integer> getMaterialsToRepair(ItemStack gear, ItemStack repairKit, RepairContext.Type repairType) {
    // Materials should be sorted by tier (ascending)
    Map<MaterialInstance, Float> stored = getStoredMaterials(repairKit);
    Map<MaterialInstance, Float> used = new HashMap<>();
    float gearRepairEfficiency = GearData.getStat(gear, ItemStats.REPAIR_EFFICIENCY);
    float kitEfficiency = this.getRepairEfficiency(repairType);
    int damageLeft = gear.getDamageValue();
    if (gearRepairEfficiency > 0f && kitEfficiency > 0f) {
        for (Map.Entry<MaterialInstance, Float> entry : stored.entrySet()) {
            MaterialInstance mat = entry.getKey();
            float amount = entry.getValue();
            int repairValue = mat.getRepairValue(gear);
            if (repairValue > 0) {
                float totalRepairValue = repairValue * amount;
                int maxRepair = Math.round(totalRepairValue * gearRepairEfficiency * kitEfficiency);
                int toRepair = Math.min(maxRepair, damageLeft);
                damageLeft -= toRepair;
                float repairValueUsed = toRepair / gearRepairEfficiency / kitEfficiency;
                float amountUsed = repairValueUsed / repairValue;
                used.put(mat, amountUsed);
                if (damageLeft <= 0) {
                    break;
                }
            }
        }
    }
    return Pair.of(used, gear.getDamageValue() - damageLeft);
}
Also used : MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance)

Example 19 with MaterialInstance

use of net.silentchaos512.gear.gear.material.MaterialInstance in project Silent-Gear by SilentChaos512.

the class IGearRecipe method getParts.

default Collection<PartData> getParts(Container inv) {
    List<MaterialInstance> materials = new ArrayList<>();
    IMaterial first = null;
    List<PartData> parts = new ArrayList<>();
    for (int i = 0; i < inv.getContainerSize(); ++i) {
        ItemStack stack = inv.getItem(i);
        if (!stack.isEmpty()) {
            MaterialInstance mat = MaterialInstance.from(stack);
            if (mat != null) {
                // If classic mixing is disabled, all materials must be the same
                if (first == null) {
                    first = mat.get();
                } else if (!Config.Common.allowLegacyMaterialMixing.get() && first != mat.get()) {
                    return Collections.emptyList();
                }
                materials.add(mat);
            } else {
                PartData part = PartData.from(stack);
                if (part != null) {
                    parts.add(part);
                }
            }
        }
    }
    if (!materials.isEmpty()) {
        // Construct a tool head
        createToolHead(this.getOutputItem().getGearType(), materials).ifPresent(part -> parts.add(0, part));
    }
    return parts;
}
Also used : IMaterial(net.silentchaos512.gear.api.material.IMaterial) PartData(net.silentchaos512.gear.gear.part.PartData) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) ItemStack(net.minecraft.world.item.ItemStack)

Example 20 with MaterialInstance

use of net.silentchaos512.gear.gear.material.MaterialInstance in project Silent-Gear by SilentChaos512.

the class ShapelessCompoundPartRecipe method matches.

@Override
public boolean matches(CraftingContainer inv, Level worldIn) {
    if (!this.getBaseRecipe().matches(inv, worldIn))
        return false;
    IMaterial first = null;
    for (int i = 0; i < inv.getContainerSize(); ++i) {
        ItemStack stack = inv.getItem(i);
        MaterialInstance mat = MaterialInstance.from(stack);
        if (mat != null) {
            if (!mat.get().isCraftingAllowed(mat, item.getPartType(), this.getGearType(), inv)) {
                return false;
            }
            // If classic mixing is disabled, all materials must be the same
            if (first == null) {
                first = mat.get();
            } else if (!Config.Common.allowLegacyMaterialMixing.get() && first != mat.get()) {
                return false;
            }
        }
    }
    return true;
}
Also used : IMaterial(net.silentchaos512.gear.api.material.IMaterial) ItemStack(net.minecraft.world.item.ItemStack) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) LazyMaterialInstance(net.silentchaos512.gear.gear.material.LazyMaterialInstance)

Aggregations

MaterialInstance (net.silentchaos512.gear.gear.material.MaterialInstance)25 ItemStack (net.minecraft.world.item.ItemStack)12 IMaterial (net.silentchaos512.gear.api.material.IMaterial)10 IMaterialInstance (net.silentchaos512.gear.api.material.IMaterialInstance)10 PartType (net.silentchaos512.gear.api.part.PartType)9 MaterialLayer (net.silentchaos512.gear.api.material.MaterialLayer)8 PartData (net.silentchaos512.gear.gear.part.PartData)8 Component (net.minecraft.network.chat.Component)7 IMaterialDisplay (net.silentchaos512.gear.api.material.IMaterialDisplay)7 TextComponent (net.minecraft.network.chat.TextComponent)6 ResourceLocation (net.minecraft.resources.ResourceLocation)6 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)6 Collectors (java.util.stream.Collectors)5 Nullable (javax.annotation.Nullable)5 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)5 GearType (net.silentchaos512.gear.api.item.GearType)5 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)5 LazyMaterialInstance (net.silentchaos512.gear.gear.material.LazyMaterialInstance)5 java.util (java.util)4 ArrayList (java.util.ArrayList)4