Skip to content

Commit

Permalink
Implement modifier icons
Browse files Browse the repository at this point in the history
Will be needed for UI stuff later, might make a few more icons later
  • Loading branch information
KnightMiner committed Oct 2, 2022
1 parent f62f000 commit 3ade0bd
Show file tree
Hide file tree
Showing 74 changed files with 253 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package slimeknights.tconstruct.library.client.modifiers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.blaze3d.vertex.PoseStack;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import slimeknights.mantle.data.IEarlySafeManagerReloadListener;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.client.RenderUtils;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierId;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* Class handling the loading of modifier UI icons
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Log4j2
public class ModifierIconManager implements IEarlySafeManagerReloadListener {
/** Icon file to load, has merging behavior but forge prevents multiple mods from loading the same file */
private static final String ICONS = "tinkering/modifier_icons.json";
/** First layer of the default icon, will be tinted */
private static final ResourceLocation DEFAULT_PAGES = TConstruct.getResource("gui/modifiers/default_pages");
/** Second layer of the default icon, will be tinted */
private static final ResourceLocation DEFAULT_COVER = TConstruct.getResource("gui/modifiers/default_cover");
/** Instance of this manager */
public static final ModifierIconManager INSTANCE = new ModifierIconManager();

/** Map of icons for each modifier */
private static Map<ModifierId,ResourceLocation> modifierIcons = Collections.emptyMap();

/**
* Initializes this manager, registering it relevant event busses
*/
public static void init() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(ModifierIconManager::textureStitch);
bus.addListener(ModifierIconManager::onResourceManagerRegister);
}

/** Called on resource manager build to add the manager */
private static void onResourceManagerRegister(RegisterClientReloadListenersEvent manager) {
manager.registerReloadListener(INSTANCE);
}

/** Called on texture stitch to add the new textures */
private static void textureStitch(TextureStitchEvent.Pre event) {
if (event.getAtlas().location().equals(InventoryMenu.BLOCK_ATLAS)) {
modifierIcons.values().forEach(event::addSprite);
event.addSprite(DEFAULT_COVER);
event.addSprite(DEFAULT_PAGES);
}
}

@Override
public void onReloadSafe(ResourceManager manager) {
// start building the model map
Map<ModifierId,ResourceLocation> icons = new HashMap<>();

// get a list of files from all namespaces
List<JsonObject> jsonFiles = JsonHelper.getFileInAllDomainsAndPacks(manager, ICONS, null);
// first object is bottom most pack, so upper resource packs will replace it
for (int i = jsonFiles.size() - 1; i >= 0; i--) {
JsonObject json = jsonFiles.get(i);
// right now just do simply key value pairs
for (Entry<String,JsonElement> entry : json.entrySet()) {
// get a valid name
String key = entry.getKey();
ModifierId name = ModifierId.tryParse(key);
if (name == null) {
log.error("Skipping invalid modifier key " + key + " as it is not a valid resource location");
// ensure it's not already parsed
} else if (!icons.containsKey(name)) {
// get a valid element, remove if null, error if not primitive
JsonElement element = entry.getValue();
if (element.isJsonNull()) {
icons.remove(name);
} else if (element.isJsonPrimitive()) {
// primitive means texture path
ResourceLocation path = ResourceLocation.tryParse(element.getAsString());
if (path != null) {
icons.put(name, path);
} else {
log.error("Skipping invalid modifier " + key + " as the path is invalid");
}
} else {
log.error("Skipping key " + key + " as the value is not a valid path");
}
}
}
}
// replace the map
modifierIcons = icons;
}

/**
* Renders a modifier icon at the given location
* @param matrices Matrix stack instance
* @param modifier Modifier to draw
* @param x X offset
* @param y Y offset
* @param z Render depth offset, typically 100 is good
* @param size Size to render, 16 is default
*/
public static void renderIcon(PoseStack matrices, Modifier modifier, int x, int y, int z, int size) {
RenderUtils.setup(InventoryMenu.BLOCK_ATLAS);
TextureAtlas atlas = Minecraft.getInstance().getModelManager().getAtlas(InventoryMenu.BLOCK_ATLAS);

ResourceLocation icon = modifierIcons.get(modifier.getId());
if (icon != null) {
Screen.blit(matrices, x, y, z, size, size, atlas.getSprite(icon));
} else {
Screen.blit(matrices, x, y, z, size, size, atlas.getSprite(DEFAULT_PAGES));
RenderUtils.setColorRGBA(0xFF000000 | modifier.getColor());
Screen.blit(matrices, x, y, z, size, size, atlas.getSprite(DEFAULT_COVER));
RenderUtils.setColorRGBA(-1);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package slimeknights.tconstruct.plugin.jei.modifiers;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import mezz.jei.api.ingredients.IIngredientRenderer;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.item.TooltipFlag;
import slimeknights.tconstruct.library.client.RenderUtils;
import slimeknights.tconstruct.library.client.modifiers.ModifierIconManager;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;

import javax.annotation.Nullable;
Expand All @@ -26,12 +23,7 @@ public enum ModifierBookmarkIngredientRenderer implements IIngredientRenderer<Mo
@Override
public void render(PoseStack matrixStack, @Nullable ModifierEntry entry) {
if (entry != null) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, ModifierRecipeCategory.BACKGROUND_LOC);
Screen.blit(matrixStack, 0, 0, 224f, 0f, 16, 16, 256, 256);
RenderUtils.setColorRGBA(0xFF000000 | entry.getModifier().getColor());
Screen.blit(matrixStack, 0, 0, 240f, 0f, 16, 16, 256, 256);
RenderUtils.setColorRGBA(-1);
ModifierIconManager.renderIcon(matrixStack, entry.getModifier(), 0, 0, 100, 16);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import slimeknights.tconstruct.library.client.data.spritetransformer.IColorMapping;
import slimeknights.tconstruct.library.client.data.spritetransformer.ISpriteTransformer;
import slimeknights.tconstruct.library.client.data.spritetransformer.RecolorSpriteTransformer;
import slimeknights.tconstruct.library.client.modifiers.ModifierIconManager;
import slimeknights.tconstruct.tables.client.PatternGuiTextureLoader;

import java.util.function.Consumer;
Expand All @@ -24,6 +25,7 @@ public static void onConstruct() {
TinkerBook.initBook();
// needs to register listeners early enough for minecraft to load
PatternGuiTextureLoader.init();
ModifierIconManager.init();

// add the recipe cache invalidator to the client
Consumer<RecipesUpdatedEvent> recipesUpdated = event -> RecipeCacheInvalidator.reload(true);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions src/main/resources/assets/tconstruct/tinkering/modifier_icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"tconstruct:reinforced": "tconstruct:item/materials/reinforcement/iron",
"tconstruct:unbreakable": "tconstruct:gui/modifiers/unbreakable",
"tconstruct:emerald": "minecraft:item/emerald",
"tconstruct:diamond": "minecraft:item/diamond",
"tconstruct:netherite": "minecraft:item/netherite_ingot",
"tconstruct:overforced": "tconstruct:item/materials/reinforcement/slimesteel",
"tconstruct:overslime": "minecraft:item/slime_ball",
"tconstruct:soulbound": "minecraft:item/totem_of_undying",
"tconstruct:worldbound": "minecraft:item/netherite_scrap",
"tconstruct:tank": "tconstruct:gui/modifiers/tank",

"tconstruct:autosmelt": "tconstruct:gui/modifiers/harvest/autosmelt",
"tconstruct:fortune": "tconstruct:gui/modifiers/luck",
"tconstruct:looting": "tconstruct:gui/modifiers/luck",
"tconstruct:luck": "tconstruct:gui/modifiers/luck",
"tconstruct:melting": "tconstruct:gui/modifiers/harvest/melting",
"tconstruct:silky": "tconstruct:item/materials/silky_cloth",

"tconstruct:bucketing": "minecraft:item/bucket",
"tconstruct:firestarter": "minecraft:item/flint_and_steel",
"tconstruct:fireprimer": "minecraft:item/fire_charge",
"tconstruct:glowing": "tconstruct:gui/modifiers/glowing",
"tconstruct:pathing": "minecraft:item/iron_shovel",
"tconstruct:stripping": "minecraft:item/iron_axe",
"tconstruct:tilling": "minecraft:item/iron_hoe",

"tconstruct:exchanging": "tconstruct:gui/modifiers/harvest/exchanging",
"tconstruct:expanded": "tconstruct:gui/modifiers/harvest/expanded",
"tconstruct:hydraulic": "tconstruct:gui/modifiers/harvest/hydraulic",
"tconstruct:haste": "tconstruct:gui/modifiers/harvest/haste",
"tconstruct:haste_armor": "tconstruct:gui/modifiers/harvest/haste",
"tconstruct:blasting": "tconstruct:gui/modifiers/harvest/blasting",
"tconstruct:lightspeed": "tconstruct:gui/modifiers/harvest/lightspeed",
"tconstruct:magnetic": "tconstruct:gui/modifiers/harvest/magnetic",

"tconstruct:antiaquatic": "tconstruct:gui/modifiers/melee/antiaquatic",
"tconstruct:bane_of_sssss": "tconstruct:gui/modifiers/melee/bane_of_sssss",
"tconstruct:cooling": "tconstruct:gui/modifiers/melee/cooling",
"tconstruct:killager": "tconstruct:gui/modifiers/melee/killager",
"tconstruct:piercing": "tconstruct:gui/modifiers/melee/piercing",
"tconstruct:smite": "tconstruct:gui/modifiers/melee/smite",
"tconstruct:sharpness": "tconstruct:gui/modifiers/melee/sharpness",
"tconstruct:swiftstrike": "tconstruct:gui/modifiers/melee/swiftstrike",

"tconstruct:dual_wielding": "tconstruct:gui/modifiers/melee/dual_wielding",
"tconstruct:fiery": "tconstruct:gui/modifiers/melee/fiery",
"tconstruct:experienced": "minecraft:item/experience_bottle",
"tconstruct:knockback": "tconstruct:gui/modifiers/melee/knockback",
"tconstruct:knockback_armor": "tconstruct:gui/modifiers/melee/knockback",
"tconstruct:necrotic": "tconstruct:gui/modifiers/melee/necrotic",
"tconstruct:padded": "tconstruct:gui/modifiers/melee/padded",
"tconstruct:severing": "tconstruct:gui/modifiers/melee/severing",
"tconstruct:sweeping_edge": "tconstruct:gui/modifiers/melee/sweeping",
"tconstruct:spilling": "tconstruct:gui/modifiers/melee/spilling",

"tconstruct:protection": "tconstruct:gui/modifiers/armor/protection",
"tconstruct:melee_protection": "tconstruct:gui/modifiers/armor/melee_protection",
"tconstruct:projectile_protection": "tconstruct:gui/modifiers/armor/projectile_protection",
"tconstruct:blast_protection": "tconstruct:gui/modifiers/armor/blast_protection",
"tconstruct:magic_protection": "tconstruct:gui/modifiers/armor/magic_protection",
"tconstruct:fire_protection": "tconstruct:gui/modifiers/armor/fire_protection",
"tconstruct:dragonborn": "tconstruct:gui/modifiers/armor/dragonborn",
"tconstruct:turtle_shell": "tconstruct:gui/modifiers/armor/turtle_shell",
"tconstruct:shulking": "tconstruct:gui/modifiers/armor/shulking",
"tconstruct:revitalizing": "tconstruct:gui/modifiers/armor/revitalizing",
"tconstruct:knockback_resistance": "tconstruct:gui/modifiers/armor/knockback_resistance",

"tconstruct:ricochet": "tconstruct:gui/modifiers/armor/ricochet",
"tconstruct:springy": "tconstruct:gui/modifiers/armor/springy",
"tconstruct:sticky": "tconstruct:gui/modifiers/armor/sticky",
"tconstruct:thorns": "tconstruct:gui/modifiers/armor/thorns",

"tconstruct:aqua_affinity": "minecraft:item/heart_of_the_sea",
"tconstruct:item_frame": "minecraft:item/item_frame",
"tconstruct:respiration": "tconstruct:gui/modifiers/armor/respiration",
"tconstruct:slurping": "tconstruct:gui/modifiers/armor/slurping",
"tconstruct:zoom": "minecraft:item/spyglass",

"tconstruct:reach": "tconstruct:gui/modifiers/armor/reach",
"tconstruct:strength": "tconstruct:gui/modifiers/armor/strength",
"tconstruct:unarmed": "tconstruct:gui/modifiers/armor/unarmed",

"tconstruct:leaping": "tconstruct:gui/modifiers/armor/leaping",
"tconstruct:pockets": "tconstruct:gui/modifiers/armor/pockets",
"tconstruct:shield_strap": "tconstruct:gui/modifiers/armor/shield_strap",
"tconstruct:speedy": "tconstruct:gui/modifiers/armor/speedy",
"tconstruct:step_up": "tconstruct:gui/modifiers/armor/step_up",
"tconstruct:tool_belt": "tconstruct:gui/modifiers/armor/tool_belt",
"tconstruct:wetting": "tconstruct:gui/modifiers/armor/wetting",

"tconstruct:bouncy": "tconstruct:gui/modifiers/armor/bouncy",
"tconstruct:double_jump": "tconstruct:gui/modifiers/armor/double_jump",
"tconstruct:feather_falling": "tconstruct:gui/modifiers/armor/feather_falling",
"tconstruct:flamewake": "tconstruct:gui/modifiers/armor/flamewake",
"tconstruct:frost_walker": "tconstruct:gui/modifiers/armor/frost_walker",
"tconstruct:lightspeed_armor": "tconstruct:gui/modifiers/armor/lightspeed",
"tconstruct:path_maker": "tconstruct:gui/modifiers/armor/path_maker",
"tconstruct:plowing": "tconstruct:gui/modifiers/armor/plowing",
"tconstruct:snowdrift": "tconstruct:gui/modifiers/armor/snowdrift",
"tconstruct:soulspeed": "tconstruct:gui/modifiers/armor/soulspeed",

"tconstruct:gilded": "minecraft:item/golden_apple",
"tconstruct:writable": "minecraft:item/writable_book",
"tconstruct:harmonious": "minecraft:item/music_disc_13",
"tconstruct:recapitated": "tconstruct:gui/modifiers/recapitated",
"tconstruct:resurrected": "minecraft:item/end_crystal",
"tconstruct:draconic": "tconstruct:gui/modifiers/draconic",

"tconstruct:golden": "minecraft:item/golden_chestplate",
"tconstruct:dyed": "minecraft:item/green_dye"
}

0 comments on commit 3ade0bd

Please sign in to comment.