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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations