Skip to content

Commit 9cb8f4d

Browse files
committed
Prevent the creation of excessive amounts of model instances in armor models
1 parent 813c008 commit 9cb8f4d

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

src/main/resources/forge-1.20.1/templates/armor.java.ftl

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ public abstract class ${name}Item extends ArmorItem {
9090
<#if data.helmetModelName != "Default" && data.getHelmetModel()??>
9191
@Override public void initializeClient(Consumer<IClientItemExtensions> consumer) {
9292
consumer.accept(new IClientItemExtensions() {
93+
private HumanoidModel armorModel = null;
9394
@Override @OnlyIn(Dist.CLIENT) public HumanoidModel getHumanoidArmorModel(LivingEntity living, ItemStack stack, EquipmentSlot slot, HumanoidModel defaultModel) {
94-
HumanoidModel armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
95-
"head", new ${data.helmetModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.helmetModelName}.LAYER_LOCATION)).${data.helmetModelPart},
96-
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
97-
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
98-
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
99-
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
100-
"right_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
101-
"left_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap())
102-
)));
95+
if (armorModel == null) {
96+
armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
97+
"head", new ${data.helmetModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.helmetModelName}.LAYER_LOCATION)).${data.helmetModelPart},
98+
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
99+
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
100+
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
101+
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
102+
"right_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
103+
"left_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap())
104+
)));
105+
}
103106
armorModel.crouching = living.isShiftKeyDown();
104107
armorModel.riding = defaultModel.riding;
105108
armorModel.young = living.isBaby();
@@ -137,16 +140,20 @@ public abstract class ${name}Item extends ArmorItem {
137140
<#if data.bodyModelName != "Default" && data.getBodyModel()??>
138141
@Override public void initializeClient(Consumer<IClientItemExtensions> consumer) {
139142
consumer.accept(new IClientItemExtensions() {
143+
private HumanoidModel armorModel = null;
140144
@Override @OnlyIn(Dist.CLIENT) public HumanoidModel getHumanoidArmorModel(LivingEntity living, ItemStack stack, EquipmentSlot slot, HumanoidModel defaultModel) {
141-
HumanoidModel armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
142-
"body", new ${data.bodyModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bodyModelName}.LAYER_LOCATION)).${data.bodyModelPart},
143-
"left_arm", new ${data.bodyModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bodyModelName}.LAYER_LOCATION)).${data.armsModelPartL},
144-
"right_arm", new ${data.bodyModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bodyModelName}.LAYER_LOCATION)).${data.armsModelPartR},
145-
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
146-
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
147-
"right_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
148-
"left_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap())
149-
)));
145+
if (armorModel == null) {
146+
${data.bodyModelName} model = new ${data.bodyModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bodyModelName}.LAYER_LOCATION));
147+
armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
148+
"body", model.${data.bodyModelPart},
149+
"left_arm", model.${data.armsModelPartL},
150+
"right_arm", model.${data.armsModelPartR},
151+
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
152+
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
153+
"right_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
154+
"left_leg", new ModelPart(Collections.emptyList(), Collections.emptyMap())
155+
)));
156+
}
150157
armorModel.crouching = living.isShiftKeyDown();
151158
armorModel.riding = defaultModel.riding;
152159
armorModel.young = living.isBaby();
@@ -184,16 +191,20 @@ public abstract class ${name}Item extends ArmorItem {
184191
<#if data.leggingsModelName != "Default" && data.getLeggingsModel()??>
185192
@Override public void initializeClient(Consumer<IClientItemExtensions> consumer) {
186193
consumer.accept(new IClientItemExtensions() {
194+
private HumanoidModel armorModel = null;
187195
@Override @OnlyIn(Dist.CLIENT) public HumanoidModel getHumanoidArmorModel(LivingEntity living, ItemStack stack, EquipmentSlot slot, HumanoidModel defaultModel) {
188-
HumanoidModel armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
189-
"left_leg", new ${data.leggingsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.leggingsModelName}.LAYER_LOCATION)).${data.leggingsModelPartL},
190-
"right_leg", new ${data.leggingsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.leggingsModelName}.LAYER_LOCATION)).${data.leggingsModelPartR},
191-
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
192-
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
193-
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
194-
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
195-
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap())
196-
)));
196+
if (armorModel == null) {
197+
${data.leggingsModelName} model = new ${data.leggingsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.leggingsModelName}.LAYER_LOCATION));
198+
armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
199+
"left_leg", model.${data.leggingsModelPartL},
200+
"right_leg", model.${data.leggingsModelPartR},
201+
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
202+
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
203+
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
204+
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
205+
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap())
206+
)));
207+
}
197208
armorModel.crouching = living.isShiftKeyDown();
198209
armorModel.riding = defaultModel.riding;
199210
armorModel.young = living.isBaby();
@@ -231,16 +242,20 @@ public abstract class ${name}Item extends ArmorItem {
231242
<#if data.bootsModelName != "Default" && data.getBootsModel()??>
232243
@Override public void initializeClient(Consumer<IClientItemExtensions> consumer) {
233244
consumer.accept(new IClientItemExtensions() {
245+
private HumanoidModel armorModel = null;
234246
@Override @OnlyIn(Dist.CLIENT) public HumanoidModel getHumanoidArmorModel(LivingEntity living, ItemStack stack, EquipmentSlot slot, HumanoidModel defaultModel) {
235-
HumanoidModel armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
236-
"left_leg", new ${data.bootsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bootsModelName}.LAYER_LOCATION)).${data.bootsModelPartL},
237-
"right_leg", new ${data.bootsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bootsModelName}.LAYER_LOCATION)).${data.bootsModelPartR},
238-
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
239-
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
240-
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
241-
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
242-
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap())
243-
)));
247+
if (armorModel == null) {
248+
${data.bootsModelName} model = new ${data.bootsModelName}(Minecraft.getInstance().getEntityModels().bakeLayer(${data.bootsModelName}.LAYER_LOCATION));
249+
armorModel = new HumanoidModel(new ModelPart(Collections.emptyList(), Map.of(
250+
"left_leg", model.${data.bootsModelPartL},
251+
"right_leg", model.${data.bootsModelPartR},
252+
"head", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
253+
"hat", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
254+
"body", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
255+
"right_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap()),
256+
"left_arm", new ModelPart(Collections.emptyList(), Collections.emptyMap())
257+
)));
258+
}
244259
armorModel.crouching = living.isShiftKeyDown();
245260
armorModel.riding = defaultModel.riding;
246261
armorModel.young = living.isBaby();

0 commit comments

Comments
 (0)