Skip to content

Commit

Permalink
Add bow materials to the encyclopedia
Browse files Browse the repository at this point in the history
Includes a small revamp of material tier pages
  • Loading branch information
KnightMiner committed Dec 24, 2022
1 parent dc02b6b commit f1e7918
Show file tree
Hide file tree
Showing 21 changed files with 731 additions and 496 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.repository.FileRepository;
import slimeknights.mantle.client.book.transformer.BookTransformer;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.client.book.content.ContentMaterial;
import slimeknights.tconstruct.library.client.book.content.ContentMaterialSkull;
import slimeknights.tconstruct.library.client.book.content.ContentModifier;
import slimeknights.tconstruct.library.client.book.content.ContentTool;
import slimeknights.tconstruct.library.client.book.content.RangedMaterialContent;
import slimeknights.tconstruct.library.client.book.sectiontransformer.ModifierSectionTransformer;
import slimeknights.tconstruct.library.client.book.sectiontransformer.ToolSectionTransformer;
import slimeknights.tconstruct.library.client.book.sectiontransformer.materials.SkullMaterialSectionTransformer;
import slimeknights.tconstruct.library.client.book.sectiontransformer.materials.TierRangeMaterialSectionTransformer;
import slimeknights.tconstruct.library.client.book.sectiontransformer.materials.TieredMaterialSectionTransformer;
import slimeknights.tconstruct.shared.item.TinkerBookItem.BookType;
import slimeknights.tconstruct.tools.stats.BowstringMaterialStats;
import slimeknights.tconstruct.tools.stats.ExtraMaterialStats;
import slimeknights.tconstruct.tools.stats.GripMaterialStats;
import slimeknights.tconstruct.tools.stats.HandleMaterialStats;
import slimeknights.tconstruct.tools.stats.HeadMaterialStats;
import slimeknights.tconstruct.tools.stats.LimbMaterialStats;
import slimeknights.tconstruct.tools.stats.SkullStats;

import static slimeknights.tconstruct.library.TinkerBookIDs.ENCYCLOPEDIA_ID;
import static slimeknights.tconstruct.library.TinkerBookIDs.FANTASTIC_FOUNDRY_ID;
Expand All @@ -39,6 +49,10 @@ public static void initBook() {
BookLoader.registerPageType(ContentTool.ID, ContentTool.class);
BookLoader.registerPageType(ContentModifier.ID, ContentModifier.class);

TierRangeMaterialSectionTransformer.registerMaterialType(TConstruct.getResource("melee_harvest"), ContentMaterial::new, HeadMaterialStats.ID, HandleMaterialStats.ID, ExtraMaterialStats.ID);
TierRangeMaterialSectionTransformer.registerMaterialType(TConstruct.getResource("ranged"), RangedMaterialContent::new, LimbMaterialStats.ID, GripMaterialStats.ID, BowstringMaterialStats.ID);
TierRangeMaterialSectionTransformer.registerMaterialType(TConstruct.getResource("skull"), ContentMaterialSkull::new, SkullStats.ID);

// tool transformers
ToolSectionTransformer armorTransformer = new ToolSectionTransformer("armor");
MATERIALS_AND_YOU.addTransformer(ToolSectionTransformer.INSTANCE);
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

import com.google.common.collect.ImmutableList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.ForgeI18n;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.screen.book.BookScreen;
import slimeknights.mantle.client.screen.book.element.BookElement;
import slimeknights.mantle.client.screen.book.element.ItemElement;
import slimeknights.mantle.client.screen.book.element.TextElement;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.client.book.elements.TinkerItemElement;
import slimeknights.tconstruct.library.client.materials.MaterialTooltipCache;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.materials.definition.MaterialVariantId;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
Expand All @@ -29,13 +22,14 @@
import slimeknights.tconstruct.tools.stats.SkullStats;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/** Extension of the material page to display skull stats for the slimeskull */
public class ContentMaterialSkull extends ContentMaterial {
public class ContentMaterialSkull extends AbstractMaterialContent {
/** Translation key for skull recipe */
private static final String SKULL_FROM = TConstruct.makeTranslationKey("book", "material.skull_from");
/** Page ID for using this index directly */
public static final ResourceLocation ID = TConstruct.getResource("skull_material");

/** casting recipe used to create this item */
protected transient IDisplayableCastingRecipe skullRecipe = null;
Expand All @@ -48,6 +42,22 @@ public ContentMaterialSkull(MaterialVariantId material, boolean detailed) {
super(material, detailed);
}

@Override
public ResourceLocation getId() {
return ID;
}

@Nullable
@Override
protected MaterialStatsId getStatType(int index) {
return index == 0 ? SkullStats.ID : null;
}

@Override
protected String getTextKey(MaterialId material) {
return String.format(detailed ? "material.%s.%s.skull_encyclopedia" : "material.%s.%s.skull_flavor", material.getNamespace(), material.getPath());
}

/** Gets the recipe to cast this skull */
@Nullable
private IDisplayableCastingRecipe getSkullRecipe() {
Expand Down Expand Up @@ -113,38 +123,4 @@ protected void addPrimaryDisplayItems(List<ItemElement> displayTools, MaterialVa
}
}
}

@Override
public void build(BookData book, ArrayList<BookElement> list, boolean rightSide) {
MaterialVariantId materialVariant = getMaterialVariant();
this.addTitle(list, getTitle(), true, MaterialTooltipCache.getColor(materialVariant).getValue());

// the cool tools to the left/right
this.addDisplayItems(list, rightSide ? BookScreen.PAGE_WIDTH - 18 : 0, materialVariant);

// align page
int top = getTitleHeight();
int left = rightSide ? 0 : 22;
int y = top + 5;
int x = left + 5;
int w = BookScreen.PAGE_WIDTH - 20;

// skull stats, full width
MaterialId materialId = materialVariant.getId();
int skullTraits = this.addStatsDisplay(x, y, w, list, materialId, SkullStats.ID);
y+= 65;

// inspirational quote, or boring description text
String textKey = String.format(detailed ? "material.%s.%s.skull_encyclopedia" : "material.%s.%s.skull_flavor", materialId.getNamespace(), materialId.getPath());
if (I18n.exists(textKey)) {
// using forge instead of I18n.format as that prevents % from being interpreted as a format key
String translated = ForgeI18n.getPattern(textKey);
if (!detailed) {
translated = '"' + translated + '"';
}
TextData flavourData = new TextData(translated);
flavourData.italic = !detailed;
list.add(new TextElement(x, y + 10 * skullTraits, w, 60, flavourData));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package slimeknights.tconstruct.library.client.book.content;

import net.minecraft.resources.ResourceLocation;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.materials.definition.MaterialVariantId;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
import slimeknights.tconstruct.library.utils.Util;
import slimeknights.tconstruct.tools.stats.BowstringMaterialStats;
import slimeknights.tconstruct.tools.stats.GripMaterialStats;
import slimeknights.tconstruct.tools.stats.LimbMaterialStats;

import javax.annotation.Nullable;

public class RangedMaterialContent extends AbstractMaterialContent {
/** Page ID for using this index directly */
public static final ResourceLocation ID = TConstruct.getResource("ranged_material");

public RangedMaterialContent(MaterialVariantId materialVariant, boolean detailed) {
super(materialVariant, detailed);
}

@Override
public ResourceLocation getId() {
return ID;
}

@Nullable
@Override
protected MaterialStatsId getStatType(int index) {
return switch (index) {
case 0 -> LimbMaterialStats.ID;
case 1 -> GripMaterialStats.ID;
case 2 -> BowstringMaterialStats.ID;
default -> null;
};
}

@Override
protected String getTextKey(MaterialId material) {
if (detailed) {
String primaryKey = String.format("material.%s.%s.ranged", material.getNamespace(), material.getPath());
if (Util.canTranslate(primaryKey)) {
return primaryKey;
}
return String.format("material.%s.%s.encyclopedia", material.getNamespace(), material.getPath());
}
return String.format("material.%s.%s.flavor", material.getNamespace(), material.getPath());
}

@Override
protected boolean supportsStatType(MaterialStatsId statsId) {
return statsId.equals(LimbMaterialStats.ID) || statsId.equals(GripMaterialStats.ID) || statsId.equals(BowstringMaterialStats.ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import slimeknights.mantle.client.book.transformer.SectionTransformer;
import slimeknights.mantle.client.screen.book.element.ItemElement;
import slimeknights.mantle.client.screen.book.element.SizedBookElement;
import slimeknights.tconstruct.library.client.book.content.AbstractMaterialContent;
import slimeknights.tconstruct.library.client.book.content.ContentMaterial;
import slimeknights.tconstruct.library.client.book.content.ContentPageIconList;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
Expand Down Expand Up @@ -41,7 +42,7 @@ public AbstractMaterialSectionTransformer(String sectionName, boolean detailed)
* @param material Material to display
* @return Material page
*/
protected ContentMaterial getPageContent(MaterialVariantId material) {
protected AbstractMaterialContent getPageContent(MaterialVariantId material) {
return new ContentMaterial(material, detailed);
}

Expand Down Expand Up @@ -72,7 +73,7 @@ private static PageData addPageStatic(SectionData data, String name, ResourceLoc
* @param validMaterial Predicate to validate materials
* @param pageCreator Logic to create a page
*/
public static void createPages(BookData book, SectionData sectionData, Predicate<IMaterial> validMaterial, Function<MaterialVariantId, ContentMaterial> pageCreator) {
public static void createPages(BookData book, SectionData sectionData, Predicate<IMaterial> validMaterial, Function<MaterialVariantId,AbstractMaterialContent> pageCreator) {
sectionData.source = BookRepository.DUMMY;
sectionData.parent = book;

Expand All @@ -89,8 +90,8 @@ public static void createPages(BookData book, SectionData sectionData, Predicate

for (IMaterial material : materialList) {
MaterialId materialId = material.getIdentifier();
ContentMaterial contentMaterial = pageCreator.apply(materialId);
PageData page = addPageStatic(sectionData, materialId.toString(), ContentMaterial.ID, contentMaterial);
AbstractMaterialContent contentMaterial = pageCreator.apply(materialId);
PageData page = addPageStatic(sectionData, materialId.toString(), contentMaterial.getId(), contentMaterial);

SizedBookElement icon = new ItemElement(0, 0, 1f, contentMaterial.getDisplayStacks());
while (!overview.addLink(icon, contentMaterial.getTitleComponent(), page)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package slimeknights.tconstruct.library.client.book.sectiontransformer.materials;

import slimeknights.tconstruct.library.client.book.content.ContentMaterial;
import slimeknights.tconstruct.library.client.book.content.AbstractMaterialContent;
import slimeknights.tconstruct.library.client.book.content.ContentMaterialSkull;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
import slimeknights.tconstruct.library.materials.definition.IMaterial;
import slimeknights.tconstruct.library.materials.definition.MaterialVariantId;
import slimeknights.tconstruct.tools.stats.SkullStats;

/** Section transformer for skull material pages */
/** @deprecated use {@link TierRangeMaterialSectionTransformer} */
@Deprecated
public class SkullMaterialSectionTransformer extends AbstractMaterialSectionTransformer {
public SkullMaterialSectionTransformer(String sectionName, boolean detailed) {
super(sectionName, detailed);
Expand All @@ -19,7 +20,7 @@ protected boolean isValidMaterial(IMaterial material) {
}

@Override
protected ContentMaterial getPageContent(MaterialVariantId material) {
protected AbstractMaterialContent getPageContent(MaterialVariantId material) {
return new ContentMaterialSkull(material, detailed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,46 @@
import slimeknights.mantle.client.book.transformer.BookTransformer;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.client.book.content.AbstractMaterialContent;
import slimeknights.tconstruct.library.client.book.content.ContentMaterial;
import slimeknights.tconstruct.library.materials.IMaterialRegistry;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
import slimeknights.tconstruct.library.materials.definition.IMaterial;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.materials.definition.MaterialManager;
import slimeknights.tconstruct.library.materials.definition.MaterialVariantId;
import slimeknights.tconstruct.library.materials.stats.IMaterialStats;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
import slimeknights.tconstruct.tools.stats.ExtraMaterialStats;
import slimeknights.tconstruct.tools.stats.HandleMaterialStats;
import slimeknights.tconstruct.tools.stats.HeadMaterialStats;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Section transformer to show a range of materials tiers in the book
*/
public class TierRangeMaterialSectionTransformer extends BookTransformer {
private static final Set<MaterialStatsId> VISIBLE_STATS = ImmutableSet.of(HeadMaterialStats.ID, HandleMaterialStats.ID, ExtraMaterialStats.ID);
private static final Set<MaterialStatsId> MELEE_HARVEST = ImmutableSet.of(HeadMaterialStats.ID, HandleMaterialStats.ID, ExtraMaterialStats.ID);
private static final ResourceLocation KEY = TConstruct.getResource("material_tier");

private static final Map<ResourceLocation,MaterialType> MATERIAL_TYPES = new HashMap<>();

public static final TierRangeMaterialSectionTransformer INSTANCE = new TierRangeMaterialSectionTransformer();

/** Registers a new group of stat types to show on a page */
public static void registerMaterialType(ResourceLocation id, BiFunction<MaterialVariantId,Boolean,AbstractMaterialContent> constructor, MaterialStatsId... stats) {
if (MATERIAL_TYPES.putIfAbsent(id, new MaterialType(constructor, ImmutableSet.copyOf(stats))) != null) {
throw new IllegalArgumentException("Duplicate material stat group " + id);
}
}

@Override
public void transform(BookData book) {
for (SectionData section : book.sections) {
Expand All @@ -45,7 +60,8 @@ public void transform(BookData book) {
try {
int min = 0;
int max = Integer.MAX_VALUE;
boolean detailed;
Function<MaterialVariantId,AbstractMaterialContent> pageBuilder;
Set<MaterialStatsId> visibleStats;
TagKey<IMaterial> tag = null;

// if primitive, its either an int tier, or a tag
Expand All @@ -56,7 +72,9 @@ public void transform(BookData book) {
} else {
tag = MaterialManager.getTag(JsonHelper.convertToResourceLocation(json, KEY.toString()));
}
detailed = false;
pageBuilder = id -> new ContentMaterial(id, false);
visibleStats = MELEE_HARVEST;
TConstruct.LOG.warn("Using tconstruct:material_tier with a number or tag is deprecated");
} else if (json.isJsonObject()) {
// object means we have a tier/min/max, or potentially a tag
JsonObject jsonObject = json.getAsJsonObject();
Expand All @@ -67,14 +85,20 @@ public void transform(BookData book) {
min = GsonHelper.getAsInt(jsonObject, "min", 0);
max = GsonHelper.getAsInt(jsonObject, "max", Integer.MAX_VALUE);
}
detailed = GsonHelper.getAsBoolean(jsonObject, "detailed", false);
if (jsonObject.has("tag")) {
tag = MaterialManager.getTag(JsonHelper.getResourceLocation(jsonObject, "tag"));
}
ResourceLocation type = jsonObject.has("type") ? JsonHelper.getResourceLocation(jsonObject, "type") : TConstruct.getResource("melee_harvest");
MaterialType typeData = MATERIAL_TYPES.get(type);
if (typeData == null) {
throw new JsonSyntaxException("Invalid material section type " + type);
}
visibleStats = typeData.visibleStats();
pageBuilder = typeData.getMapping(GsonHelper.getAsBoolean(jsonObject, "detailed", false));
} else {
throw new JsonSyntaxException("Invalid tconstruct:material_tier, expected number or JSON object");
}
AbstractMaterialSectionTransformer.createPages(book, section, new ValidMaterial(VISIBLE_STATS, min, max, tag), id -> new ContentMaterial(id, detailed));
AbstractMaterialSectionTransformer.createPages(book, section, new ValidMaterial(visibleStats, min, max, tag), pageBuilder);
} catch (JsonSyntaxException e) {
TConstruct.LOG.error("Failed to parse material tier section data", e);
}
Expand Down Expand Up @@ -110,4 +134,11 @@ public boolean test(IMaterial material) {
return false;
}
}

/** Internal record from the registry */
private record MaterialType(BiFunction<MaterialVariantId,Boolean,AbstractMaterialContent> pageConstructor, Set<MaterialStatsId> visibleStats) {
public Function<MaterialVariantId,AbstractMaterialContent> getMapping(boolean detailed) {
return id -> pageConstructor.apply(id, detailed);
}
}
}

0 comments on commit f1e7918

Please sign in to comment.