Jump to content

Adding Armor Variants [1.18]


MWR

Recommended Posts

I have this idea where you can add emblems to your chestplate to distinguish yourself. What I am wondering is whether or not I need to register every variant. It can get quite overwhelming to need to register 4 chestplates for every symbol, and they wouldn’t be treated as the vanilla chestplate. Would I be able to avoid this through tags or some other way?

Link to comment
Share on other sites

11 hours ago, MWR said:

It can get quite overwhelming to need to register 4 chestplates for every symbol, and they wouldn’t be treated as the vanilla chestplate. Would I be able to avoid this through tags or some other way?

it depends on in which way you want to modify the Chestplate, in which way do you want to modify the Chestplate?

Note: if you want a different Item for each modification, you need to register all the Items

Link to comment
Share on other sites

yes, you can use tags (nbt tags within the item, not the new "item tags").

you need a custom recipe that takes a chestplate and some items and returns a chestplate with some data in nbt tags. and you need to render your stuff when you render your armor item.

if you are going to use the crafting table (you don't have to, it can be your own block), in assemble method of the recipe call startingChestPlate.copy() to get the itemstack to return. then, result.getOrCreateTag() returns the root tag. it's best to make a CompoundTag for your mod, fill it using putString calls and then append your compound tag under the root using the put method.

Link to comment
Share on other sites

13 hours ago, MFMods said:

yes, you can use tags (nbt tags within the item, not the new "item tags").

you need a custom recipe that takes a chestplate and some items and returns a chestplate with some data in nbt tags. and you need to render your stuff when you render your armor item.

Ok, so I came up with this:

{
  "type": "minecraft:crafting_shapeless",
  "ingredients": [
    {
      "item": "minecraft:diamond_chestplate"
    },
    {
      "item": "minecraft:gunpowder"
    }
  ],
  "result": {
    "item": "minecraft:diamond_chestplate",
    "nbt": "{Emblem:Creeper}"
  }
}
13 hours ago, MFMods said:

if you are going to use the crafting table (you don't have to, it can be your own block), in assemble method of the recipe call startingChestPlate.copy() to get the itemstack to return. then, result.getOrCreateTag() returns the root tag. it's best to make a CompoundTag for your mod, fill it using putString calls and then append your compound tag under the root using the put method.

Where would this assemble method go? Would I need to make an ArmorItem class of some sort? The custom recipe added this tag without needing code, so do I just need to figure out how to render it based on this NBT tag?

Would I need to override the vanilla ArmorItem, or make a new one? If possible, I don't want to change vanilla chestplates, in case it somehow breaks functionality with other mods.

 

Edited by MWR
I realized that I need more than JSONs if I need to remove a tag.
Link to comment
Share on other sites

make a class EmblemRecipe that extends SpecialRecipe. in method "matches" confirm that the player provided a chestplate (maybe you'll allow adding things in steps?) and things you accept. in "assemble" method you make a recipe result - vanilla chestplate with nbt or your own item with nbt, that's up to you.

use SimpleRecipeSerializer as a recipe serializer. register it in RegistryEvent.Register<IRecipeSerializer<?>> event.

make a json file like this:
{
  "type": "your_mod_id:recipe_name"
}
where recipe_name is name you give to the serializer. basically this is the recipe class:

public class YourRecipe extends SpecialRecipe
{
	public static IRecipeSerializer<YourRecipe> StupidSerializer = (IRecipeSerializer<YourRecipe>) (new SimpleRecipeSerializer<YourRecipe>(YourRecipe::new).setRegistryName(YourMod.MODID, "recipe_name"));


	public YourRecipe(ResourceLocation resourceLocation)
	{
		super(resourceLocation);
	}



	@Override
	public boolean matches(CraftingInventory inventory, World world)
	{
		return true if items are acceptable;
	}



	@Override
	public ItemStack assemble(CraftingInventory inventory)
	{
		return nice new itemstack with nbt;
	}



	@Override
	public IRecipeSerializer<?> getSerializer()
	{
		return StupidSerializer;
	}
}

 

Link to comment
Share on other sites

On 5/30/2022 at 8:25 AM, MFMods said:

make a class EmblemRecipe that extends SpecialRecipe. in method "matches" confirm that the player provided a chestplate (maybe you'll allow adding things in steps?) and things you accept. in "assemble" method you make a recipe result - vanilla chestplate with nbt or your own item with nbt, that's up to you.

use SimpleRecipeSerializer as a recipe serializer. register it in RegistryEvent.Register<IRecipeSerializer<?>> event.

make a json file like this:
{
  "type": "your_mod_id:recipe_name"
}
where recipe_name is name you give to the serializer. basically this is the recipe class:

public class YourRecipe extends SpecialRecipe
{
	public static IRecipeSerializer<YourRecipe> StupidSerializer = (IRecipeSerializer<YourRecipe>) (new SimpleRecipeSerializer<YourRecipe>(YourRecipe::new).setRegistryName(YourMod.MODID, "recipe_name"));


	public YourRecipe(ResourceLocation resourceLocation)
	{
		super(resourceLocation);
	}



	@Override
	public boolean matches(CraftingInventory inventory, World world)
	{
		return true if items are acceptable;
	}



	@Override
	public ItemStack assemble(CraftingInventory inventory)
	{
		return nice new itemstack with nbt;
	}



	@Override
	public IRecipeSerializer<?> getSerializer()
	{
		return StupidSerializer;
	}
}

 

Would I have to make a JSON for every combination, or is there a way to have generic ingredients and results?

Link to comment
Share on other sites

I'm not sure if there is a more elegant way to hold a list of potential ingredients, but this works:

public class EmblemRecipe extends CustomRecipe {

	ArrayList<Item> ingredients = new ArrayList<>(Arrays.asList(Items.GUNPOWDER, Items.BONE, Items.REDSTONE));
	String[] emblems = {"Creeper", "Skeleton", "Redstone"};
	
	public EmblemRecipe(ResourceLocation id) {
		super(id);
	}

	@Override
	public boolean matches(CraftingContainer inv, Level level) {
		ItemStack chestplate = ItemStack.EMPTY;
		ItemStack ingredient = ItemStack.EMPTY;
		
		for(int i = 0; i < inv.getContainerSize(); ++i) {
			ItemStack itemstack = inv.getItem(i);
			if (chestplate.isEmpty() && itemstack.getItem() instanceof ArmorItem && ((ArmorItem)itemstack.getItem()).getSlot() == EquipmentSlot.CHEST) {
				chestplate = itemstack;
			}
			
			if (ingredient.isEmpty() && ingredients.contains(itemstack.getItem())) {
				ingredient = itemstack;
			}
		}
		
		return !chestplate.isEmpty() && !ingredient.isEmpty();
	}

	@Override
	public ItemStack assemble(CraftingContainer inv) {
		ItemStack chestplate = null;
		CompoundTag compoundtag = null;
		String emblem = null;
		for(int i = 0; i < inv.getContainerSize(); ++i ) {
			ItemStack itemstack = inv.getItem(i);
			if (itemstack.getItem() instanceof ArmorItem) {
				chestplate = itemstack.copy();
				compoundtag = chestplate.getOrCreateTagElement("emblem");
			}
			if (ingredients.contains(itemstack.getItem())) {
				emblem = emblems[ingredients.indexOf(itemstack.getItem())];
			}
		}
		compoundtag.putString("Emblem", emblem);
		chestplate.setTag(compoundtag);
		return chestplate;
	}

	@Override
	public boolean canCraftInDimensions(int width, int height) {
		return width * height >= 2;
	}

	@Override
	public ItemStack getResultItem() {
		return ItemStack.EMPTY;
	}

	@Override
	public RecipeSerializer<?> getSerializer() {
		return ModRecipeSerializer.EMBLEM_RECIPE.get();
	}

	@Override
	public RecipeType<?> getType() {
		return RecipeType.CRAFTING;
	}

}

Now my struggle is rendering the armor. I can't find where vanilla armor is rendered, and when I do, would I need to extend that class and make my own renderer for armor?

Link to comment
Share on other sites

On 6/3/2022 at 3:40 AM, diesieben07 said:

HumanoidArmorLayer. You can provide your own model and rendering via getArmorModel.

Do I need to make my own model if I just want change the texture?

Link to comment
Share on other sites

  • 2 weeks later...
On 6/3/2022 at 3:40 AM, diesieben07 said:

HumanoidArmorLayer. You can provide your own model and rendering via getArmorModel.

So far, I've overridden getArmorResource:

public class ModHumanoidArmorLayer<T extends LivingEntity, M extends HumanoidModel<T>, A extends HumanoidModel<T>> extends HumanoidArmorLayer<T, M, A> {

  ...
    
   @Override
   public ResourceLocation getArmorResource(net.minecraft.world.entity.Entity entity, ItemStack stack, EquipmentSlot slot, @Nullable String type) {
      ArmorItem item = (ArmorItem)stack.getItem();
      String texture = item.getMaterial().getName();
      String domain = "minecraft";
      int idx = texture.indexOf(':');
      if (idx != -1) {
         domain = texture.substring(0, idx);
         texture = texture.substring(idx + 1);
      }
      
      String emblem = stack.getTagElement("emblem").getString("emblem");
      
      String s1 = String.format(java.util.Locale.ROOT, "%s:textures/models/armor/%s_layer_%d%s_%s.png", domain, texture, (usesInnerModel(slot) ? 2 : 1), type == null ? "" : String.format(java.util.Locale.ROOT, "_%s", type), emblem);

      s1 = net.minecraftforge.client.ForgeHooksClient.getArmorTexture(entity, stack, s1, slot, type);
      ResourceLocation resourcelocation = ARMOR_LOCATION_CACHE.get(s1);

      if (resourcelocation == null) {
         resourcelocation = new ResourceLocation(s1);
         ARMOR_LOCATION_CACHE.put(s1, resourcelocation);
      }

      return resourcelocation;
   }
}

But how could I override the vanilla HumanoidArmorLayer? I made my own custom model for getArmorModel (which is copy/pasted from vanilla), but I am not sure how to control its textures without HumanoidArmorLayer.

Link to comment
Share on other sites

17 hours ago, diesieben07 said:

getArmorTexture will be used as the texture. Don't make your own layer.

Here is my code for ModArmorItem:

public class ModArmorItem extends ArmorItem {

   public ModArmorItem(ArmorMaterial material, EquipmentSlot slot, Properties properties) {
      super(material, slot, properties);
   }
   
   @Override
   public String getArmorTexture(ItemStack stack, Entity entity, EquipmentSlot slot, String type) {
      ArmorItem item = (ArmorItem)stack.getItem();
      String texture = item.getMaterial().getName();
      String domain = "minecraft";
      String emblem = "";
      
      if (stack.getTagElement("Emblem") != null) {
         emblem = stack.getTagElement("Emblem").getString("Emblem");
         domain = "emblematic";
      }
      
      texture = texture.substring(texture.indexOf(':') + 1);

      return String.format(java.util.Locale.ROOT, "%s:textures/models/armor/%s_layer_1%s.png", domain, texture, emblem == "" ? "" : String.format(java.util.Locale.ROOT, "_%s", emblem)); //TODO: change when including leather because of overlays
   }
}

I'm not sure what I did wrong. The texture does not change, but I can verify that the modified chestplate code is being used.

Edited by MWR
Fixed an error in code, but it is still not working.
Link to comment
Share on other sites

11 hours ago, diesieben07 said:

What do you mean by this?

One time I crashed because of a bug in my code. I was just saying that because of that, I know it’s my code being used for the chestplate and not vanilla code.

Also, I learned the reason why it’s not working is because of the Emblem tag. stack.getTagElement(“Emblem”) returns null even if it has that tag. I’m not sure what is the correct way to get the Emblem tag.

Edited by MWR
Realized why it’s not working.
Link to comment
Share on other sites

2 hours ago, Luis_ST said:

you never add the Tag to the ItemStack

Where am I supposed to add it? I handled adding the tag in the custom recipe, and it adds the proper NBT data to the chestplate.

Link to comment
Share on other sites

I got it working! I was using getTagElement incorrectly, I'm not sure what the correct way is to use getTagElement, but it doesn't matter.

@Override
public String getArmorTexture(ItemStack stack, Entity entity, EquipmentSlot slot, String type) {
   ArmorItem item = (ArmorItem)stack.getItem();
   String texture = item.getMaterial().getName();
   String domain = "minecraft";
   String emblem = "";

   if (stack.getTag().contains("Emblem")) {
      emblem = stack.getTag().getString("Emblem");
      domain = "emblematic";
   }
      
   texture = texture.substring(texture.indexOf(':') + 1);

   return String.format(java.util.Locale.ROOT, "%s:textures/models/armor/%s_layer_1%s.png", domain, texture, emblem == "" ? "" : String.format(java.util.Locale.ROOT, "_%s", emblem)); //TODO: change when including leather because of overlays
}

Thank you to all who helped!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.