Skip to content

Commit

Permalink
fix curios rendering and add ender helmet to curios slot
Browse files Browse the repository at this point in the history
  • Loading branch information
cech12 committed Dec 9, 2021
1 parent 1d578ef commit 1ea2f3e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 97 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Forge Recommended Versioning](https://mcforge.readthedocs.io/en/latest/conventions/versioning/).

## [1.17.1-1.10.0.1] - 2021-12-07
### Fixed
- Curios rendering fixed
- Ender Helmet couldn't be placed in curio slot

## [1.17.1-1.10.0.0] - 2021-12-07
### Changed
- Update mod to Forge 1.17.1-37.0.48
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_id=usefulhats
mod_version=1.10.0.0
mod_version=1.10.0.1
minecraft_version=1.17.1
forge_version=37.0.48

Expand Down
108 changes: 108 additions & 0 deletions src/main/java/cech12/usefulhats/client/CurioRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cech12.usefulhats.client;

import cech12.usefulhats.item.IUsefulHatModelOwner;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.client.ICurioRenderer;

import java.util.Map;

// see https://github.com/TheIllusiveC4/Curios/wiki/1.16.5-to-1.17:-Updates-and-Changes#rendering-system
public class CurioRenderer implements ICurioRenderer {

private static final Map<String, ResourceLocation> ARMOR_TEXTURE_RES_MAP = Maps.newHashMap();

private static CurioRenderer instance;

private HumanoidModel<LivingEntity> model;

private CurioRenderer() {
}

public static CurioRenderer getInstance() {
if (instance == null) {
instance = new CurioRenderer();
}
return instance;
}

@OnlyIn(Dist.CLIENT)
private HumanoidModel<LivingEntity> getModel(ItemStack stack) {
if (stack.getItem() instanceof IUsefulHatModelOwner) {
return UsefulHatLayers.usefulHatModel;
} else {
if (model == null) {
model = new HumanoidModel<>(Minecraft.getInstance().getEntityModels().bakeLayer(ModelLayers.PLAYER_OUTER_ARMOR));
}
return model;
}
}

@OnlyIn(Dist.CLIENT)
private ResourceLocation getTexture(ItemStack stack, String type) {
ArmorItem item = (ArmorItem) stack.getItem();
String locationString;
if (item instanceof IUsefulHatModelOwner) {
locationString = item.getArmorTexture(stack, stack.getEntityRepresentation(), stack.getEquipmentSlot(), type);
} else {
//mostly copied from BipedArmorLayer class
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);
}
locationString = String.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, 1, type == null ? "" : String.format("_%s", type));
}
ResourceLocation location = ARMOR_TEXTURE_RES_MAP.get(locationString);
if (location == null && locationString != null) {
location = new ResourceLocation(locationString);
ARMOR_TEXTURE_RES_MAP.put(locationString, location);
}
return location;
}

@Override
public <T extends LivingEntity, M extends EntityModel<T>> void render(ItemStack stack, SlotContext slotContext, PoseStack matrixStack, RenderLayerParent<T, M> renderLayerParent, MultiBufferSource renderTypeBuffer, int light, float limbSwing, float limbSwingAmount, float partialTicks, float ageInTicks, float netHeadYaw, float headPitch) {
Item item = stack.getItem();
HumanoidModel<LivingEntity> model = this.getModel(stack);
model.setupAnim(slotContext.entity(), limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch);
model.prepareMobModel(slotContext.entity(), limbSwing, limbSwingAmount, partialTicks);
ICurioRenderer.followBodyRotations(slotContext.entity(), model);
//mostly copied from UsefulHatLayer
model.copyPropertiesTo(model);
boolean flag1 = stack.hasFoil();
int i = ((net.minecraft.world.item.DyeableLeatherItem)item).getColor(stack);
float f = (float)(i >> 16 & 255) / 255.0F;
float f1 = (float)(i >> 8 & 255) / 255.0F;
float f2 = (float)(i & 255) / 255.0F;
this.renderLayer(matrixStack, renderTypeBuffer, light, flag1, model, f, f1, f2, this.getTexture(stack, null));
this.renderLayer(matrixStack, renderTypeBuffer, light, flag1, model, 1.0F, 1.0F, 1.0F, this.getTexture(stack, "overlay"));
}

@OnlyIn(Dist.CLIENT)
private void renderLayer(PoseStack p_241738_1_, MultiBufferSource p_241738_2_, int p_241738_3_, boolean p_241738_5_, HumanoidModel<LivingEntity> p_241738_6_, float p_241738_8_, float p_241738_9_, float p_241738_10_, ResourceLocation armorResource) {
//mostly copied from UsefulHatLayer
VertexConsumer ivertexbuilder = ItemRenderer.getFoilBuffer(p_241738_2_, p_241738_6_.renderType(armorResource), false, p_241738_5_);
p_241738_6_.renderToBuffer(p_241738_1_, ivertexbuilder, p_241738_3_, OverlayTexture.NO_OVERLAY, p_241738_8_, p_241738_9_, p_241738_10_, 1.0F);
}

}
8 changes: 8 additions & 0 deletions src/main/java/cech12/usefulhats/init/ModItems.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cech12.usefulhats.init;

import cech12.usefulhats.UsefulHatsUtils;
import cech12.usefulhats.client.CurioRenderer;
import cech12.usefulhats.compat.CuriosMod;
import cech12.usefulhats.item.*;
import cech12.usefulhats.UsefulHatsMod;
Expand Down Expand Up @@ -31,6 +32,7 @@
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import top.theillusivec4.curios.api.client.CuriosRendererRegistry;
import top.theillusivec4.curios.api.event.CurioChangeEvent;

@Mod.EventBusSubscriber(modid= UsefulHatsMod.MOD_ID, bus= Mod.EventBusSubscriber.Bus.MOD)
Expand Down Expand Up @@ -99,6 +101,12 @@ public static void addEventListeners() {
*/
@OnlyIn(Dist.CLIENT)
public static void setupClient() {
//curio rendering
if (CuriosMod.isLoaded()) {
for (Item item : ModItems.items) {
CuriosRendererRegistry.register(item, CurioRenderer::getInstance);
}
}
//register overlay
OverlayRegistry.registerOverlayAbove(ForgeIngameGui.HELMET_ELEMENT, "UsefulHats_Overlay", (gui, mStack, partialTicks, screenWidth, screenHeight) -> {
if (Minecraft.useFancyGraphics())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
package cech12.usefulhats.item;

import cech12.usefulhats.client.UsefulHatModel;
import cech12.usefulhats.config.Config;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.type.capability.ICurio;

import java.util.Map;

/**
* This class is a capability for the Curios API.
*/
public class AbstractHatItemCurioCapability implements ICurio {

private static final Map<String, ResourceLocation> ARMOR_TEXTURE_RES_MAP = Maps.newHashMap();

private final ItemStack stack;
private Object model;

public AbstractHatItemCurioCapability(ItemStack stack) {
this.stack = stack;
Expand All @@ -40,7 +22,7 @@ public AbstractHatItemCurioCapability(ItemStack stack) {
//public void onUnequip(String identifier, int index, LivingEntity livingEntity) {}

@Override
public boolean canEquip(String identifier, LivingEntity livingEntity) {
public boolean canEquip(SlotContext slotContext) {
return Config.CURIOS_ENABLED.getValue();
}

Expand All @@ -50,84 +32,10 @@ public ItemStack getStack() {
}

@Override
public void curioTick(String identifier, int index, LivingEntity livingEntity) {
if (Config.CURIOS_ENABLED.getValue() && livingEntity instanceof Player) {
Player player = (Player) livingEntity;
public void curioTick(SlotContext slotContext) {
if (Config.CURIOS_ENABLED.getValue() && slotContext.entity() instanceof Player player) {
this.stack.getItem().onArmorTick(this.stack, player.level, player);
}
}

/* TODO see https://github.com/TheIllusiveC4/Curios/wiki/1.16.5-to-1.17:-Updates-and-Changes#rendering-system
@OnlyIn(Dist.CLIENT)
private HumanoidModel<LivingEntity> getModel() {
if (model == null) {
if (this.stack.getItem() instanceof IUsefulHatModelOwner) {
model = new UsefulHatModel<>();
} else {
model = new HumanoidModel<>(0.5F);
}
}
return (HumanoidModel<LivingEntity>) model;
}
@OnlyIn(Dist.CLIENT)
private ResourceLocation getTexture(String type) {
ArmorItem item = (ArmorItem) this.stack.getItem();
String locationString;
if (item instanceof IUsefulHatModelOwner) {
locationString = item.getArmorTexture(this.stack, this.stack.getEntityRepresentation(), this.stack.getEquipmentSlot(), type);
} else {
//mostly copied from BipedArmorLayer class
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);
}
locationString = String.format("%s:textures/models/armor/%s_layer_%d%s.png", domain, texture, 1, type == null ? "" : String.format("_%s", type));
}
ResourceLocation location = ARMOR_TEXTURE_RES_MAP.get(locationString);
if (location == null && locationString != null) {
location = new ResourceLocation(locationString);
ARMOR_TEXTURE_RES_MAP.put(locationString, location);
}
return location;
}
@Override
@OnlyIn(Dist.CLIENT)
public boolean canRender(String identifier, int index, LivingEntity livingEntity) {
return true;
}
@Override
@OnlyIn(Dist.CLIENT)
public void render(String identifier, int index, PoseStack matrixStack, MultiBufferSource renderTypeBuffer, int light, LivingEntity entity, float limbSwing, float limbSwingAmount, float partialTicks, float ageInTicks, float netHeadYaw, float headPitch) {
Item item = this.stack.getItem();
HumanoidModel<LivingEntity> model = this.getModel();
model.setupAnim(entity, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch);
model.prepareMobModel(entity, limbSwing, limbSwingAmount, partialTicks);
RenderHelper.followBodyRotations(entity, model);
//mostly copied from UsefulHatLayer
model.copyPropertiesTo(model);
boolean flag1 = this.stack.hasFoil();
int i = ((net.minecraft.world.item.DyeableLeatherItem)item).getColor(this.stack);
float f = (float)(i >> 16 & 255) / 255.0F;
float f1 = (float)(i >> 8 & 255) / 255.0F;
float f2 = (float)(i & 255) / 255.0F;
this.renderLayer(matrixStack, renderTypeBuffer, light, flag1, model, f, f1, f2, this.getTexture(null));
this.renderLayer(matrixStack, renderTypeBuffer, light, flag1, model, 1.0F, 1.0F, 1.0F, this.getTexture("overlay"));
}
@OnlyIn(Dist.CLIENT)
private void renderLayer(PoseStack p_241738_1_, MultiBufferSource p_241738_2_, int p_241738_3_, boolean p_241738_5_, HumanoidModel<LivingEntity> p_241738_6_, float p_241738_8_, float p_241738_9_, float p_241738_10_, ResourceLocation armorResource) {
//mostly copied from UsefulHatLayer
VertexConsumer ivertexbuilder = ItemRenderer.getFoilBuffer(p_241738_2_, p_241738_6_.renderType(armorResource), false, p_241738_5_);
p_241738_6_.renderToBuffer(p_241738_1_, ivertexbuilder, p_241738_3_, OverlayTexture.NO_OVERLAY, p_241738_8_, p_241738_9_, p_241738_10_, 1.0F);
}
*/

}
1 change: 1 addition & 0 deletions src/main/resources/data/curios/tags/items/head.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"usefulhats:aquanaut_helmet",
"usefulhats:bunny_ears",
"usefulhats:chopping_hat",
"usefulhats:ender_helmet",
"usefulhats:halo",
"usefulhats:lucky_hat",
"usefulhats:mining_hat",
Expand Down

0 comments on commit 1ea2f3e

Please sign in to comment.