Skip to content

Commit

Permalink
Improve subtype representation of materials with limited stat types
Browse files Browse the repository at this point in the history
Uses new material registry function first with stat type, fetches the first valid materal for a stat. In most cases that will be wood, but some will use string
This will ultimatley replace fixed materials as tools like bows no longer need to specify bow strings to be valid. This also means binding exclusive materials will show in JEI with a wood exterior
  • Loading branch information
KnightMiner committed Dec 27, 2022
1 parent 5b6f003 commit 8ce4149
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,6 @@ private static boolean hasStatType(MaterialId materialId, MaterialStatsId statsI
return MaterialRegistry.getInstance().getMaterialStats(materialId, statsId).isPresent();
}

/** Gets the first material from the registry for the given stat type */
private static IMaterial getFirstMaterialWithType(MaterialStatsId statsId) {
for (IMaterial material : MaterialRegistry.getMaterials()) {
if (hasStatType(material.getIdentifier(), statsId)) {
return material;
}
}
return IMaterial.UNKNOWN;
}

/** Adds items to the display tools list for all relevant recipes */
protected void addPrimaryDisplayItems(List<ItemElement> displayTools, MaterialVariantId materialId) {
// part builder
Expand Down Expand Up @@ -364,7 +354,7 @@ protected void addDisplayItems(ArrayList<BookElement> list, int x, MaterialVaria
materials.add(materialVariant);
usedMaterial = true;
} else {
materials.add(getFirstMaterialWithType(part.getStatType()));
materials.add(MaterialRegistry.firstWithStatType(part.getStatType()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,21 @@ public JsonObject serialize() {

/** Constant material */
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
private static class First extends RandomMaterial implements Predicate<IMaterial> {
private static class First extends RandomMaterial {
private static final ResourceLocation ID = TConstruct.getResource("first");

/** Stat type for random materials */
private final MaterialStatsId statType;

private MaterialId material;

/** Creates an instance from JSON */
public static First fromJson(JsonObject json) {
MaterialStatsId statType = new MaterialStatsId(JsonHelper.getResourceLocation(json, "stat_type"));
return new First(statType);
}

@Override
public boolean test(IMaterial material) {
return MaterialRegistry.getInstance().getMaterialStats(material.getIdentifier(), statType).isPresent();
}

@Override
public MaterialVariantId getMaterial(Random random) {
if (material == null) {
material = MaterialRegistry.getInstance().getVisibleMaterials().stream()
.filter(this)
.findFirst()
.orElse(IMaterial.UNKNOWN)
.getIdentifier();
}
return material;
return MaterialRegistry.firstWithStatType(statType).getIdentifier();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public final class MaterialRegistry {
Expand All @@ -43,6 +45,9 @@ public final class MaterialRegistry {

static MaterialRegistry INSTANCE;

/** Map of each stat type to its first material */
private static final Map<MaterialStatsId,IMaterial> FIRST_MATERIALS = new HashMap<>();

private final MaterialManager materialManager;
private final MaterialStatsManager materialStatsManager;
private final MaterialTraitsManager materialTraitsManager;
Expand Down Expand Up @@ -183,6 +188,23 @@ public static Function<FriendlyByteBuf,? extends IMaterialStats> getStatDecoder(
return INSTANCE.materialStatsManager.getStatDecoder(id);
}

/** Loads the first material of a stat type */
private static final Function<MaterialStatsId,IMaterial> FIRST_LOADER = statsId -> {
IMaterialRegistry instance = getInstance();
for (IMaterial material : instance.getVisibleMaterials()) {
if (instance.getMaterialStats(material.getIdentifier(), statsId).isPresent()) {
return material;
}
}
return IMaterial.UNKNOWN;
};

/** Gets the first material with the given stat type */
public static IMaterial firstWithStatType(MaterialStatsId id) {
return FIRST_MATERIALS.computeIfAbsent(id, FIRST_LOADER);
}



/* Loading */

Expand All @@ -193,6 +215,7 @@ private static void checkAllLoaded() {
statsLoaded = false;
traitsLoaded = false;
fullyLoaded = true;
FIRST_MATERIALS.clear();
MinecraftForge.EVENT_BUS.post(new MaterialsLoadedEvent());
} else {
fullyLoaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static MaterialNBT randomMaterials(ToolDefinitionData data, int maxTier,

/**
* Adds all sub items to a tool
* TODO 1.19: remove fixed materials
* @param item item being created
* @param itemList List to fill with items
* @param fixedMaterials Materials that should be forced
Expand All @@ -147,10 +148,8 @@ public static void addDefaultSubItems(IModifiable item, List<ItemStack> itemList
MaterialId materialId = MaterialId.tryParse(showOnlyId);
if (materialId != null) {
IMaterial material = MaterialRegistry.getMaterial(materialId);
if (material != IMaterial.UNKNOWN) {
if (addSubItem(item, itemList, material, fixedMaterials)) {
added = true;
}
if (material != IMaterial.UNKNOWN && addSubItem(item, itemList, material, fixedMaterials)) {
added = true;
}
}
}
Expand All @@ -170,17 +169,31 @@ public static void addDefaultSubItems(IModifiable item, List<ItemStack> itemList
private static boolean addSubItem(IModifiable item, List<ItemStack> items, IMaterial material, MaterialVariantId[] fixedMaterials) {
List<PartRequirement> required = item.getToolDefinition().getData().getParts();
MaterialNBT.Builder materials = MaterialNBT.builder();
boolean useMaterial = false;
for (int i = 0; i < required.size(); i++) {
if (fixedMaterials.length > i && fixedMaterials[i] != null && required.get(i).canUseMaterial(fixedMaterials[i])) {
PartRequirement requirement = required.get(i);
// if fixed, used fixed
if (fixedMaterials.length > i && fixedMaterials[i] != null && requirement.canUseMaterial(fixedMaterials[i])) {
materials.add(fixedMaterials[i]);
// mark we used it even if fixed
if (fixedMaterials[i].getId().equals(material.getIdentifier())) {
useMaterial = true;
}
}
else if (required.get(i).canUseMaterial(material.getIdentifier())) {
// if not fixed, try to use requested material
else if (requirement.canUseMaterial(material.getIdentifier())) {
materials.add(material);
useMaterial = true;
} else {
return false;
// fallback to first that works
materials.add(MaterialRegistry.firstWithStatType(requirement.getStatType()));
}
}
items.add(buildItemFromMaterials(item, materials.build()));
return true;
// only report success if we actually used the material somewhere
if (useMaterial) {
items.add(buildItemFromMaterials(item, materials.build()));
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package slimeknights.tconstruct.tools.item;

import net.minecraft.core.NonNullList;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
Expand All @@ -10,7 +9,6 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ProjectileWeaponItem;
Expand All @@ -28,15 +26,13 @@
import slimeknights.tconstruct.library.tools.capability.TinkerDataKeys;
import slimeknights.tconstruct.library.tools.definition.ToolDefinition;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolBuildHandler;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.item.ModifiableLauncherItem;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;
import slimeknights.tconstruct.library.tools.nbt.NamespacedNBT;
import slimeknights.tconstruct.library.tools.nbt.ToolStack;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.tools.TinkerModifiers;
import slimeknights.tconstruct.tools.data.material.MaterialIds;

import java.util.function.Predicate;

Expand Down Expand Up @@ -210,11 +206,4 @@ else if (ModifierUtil.getModifierLevel(bow, TinkerModifiers.scope.getId()) > 0)
}
}
}

@Override
public void fillItemCategory(CreativeModeTab group, NonNullList<ItemStack> items) {
if (this.allowdedIn(group)) {
ToolBuildHandler.addDefaultSubItems(this, items, null, null, null, MaterialIds.string);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mojang.math.Vector3f;
import net.minecraft.ChatFormatting;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
Expand All @@ -25,7 +24,6 @@
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
Expand All @@ -41,7 +39,6 @@
import slimeknights.tconstruct.library.tools.capability.TinkerDataCapability;
import slimeknights.tconstruct.library.tools.definition.ToolDefinition;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolBuildHandler;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.item.ModifiableLauncherItem;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
Expand All @@ -52,7 +49,6 @@
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.library.utils.TooltipKey;
import slimeknights.tconstruct.tools.TinkerModifiers;
import slimeknights.tconstruct.tools.data.material.MaterialIds;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -289,13 +285,6 @@ public void onUseTick(Level level, LivingEntity living, ItemStack bow, int charg
}
}

@Override
public void fillItemCategory(CreativeModeTab group, NonNullList<ItemStack> items) {
if (this.allowdedIn(group)) {
ToolBuildHandler.addDefaultSubItems(this, items, null, null, MaterialIds.string);
}
}

@Override
public List<Component> getStatInformation(IToolStackView tool, @Nullable Player player, List<Component> tooltips, TooltipKey key, TooltipFlag tooltipFlag) {
tooltips = super.getStatInformation(tool, player, tooltips, key, tooltipFlag);
Expand Down

0 comments on commit 8ce4149

Please sign in to comment.