Skip to content

Commit deeae25

Browse files
committed
- armor strength fix
1 parent e6260c8 commit deeae25

File tree

10 files changed

+107
-13
lines changed

10 files changed

+107
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## v2.0.5 (1.21)
22
- fixed fake-tag ingredient interactions
3-
- fixed crash related to opening statistics on forge (or other unaccounted accesses of armorrendering)
3+
- fixed crash related to opening statistics on forge (or other unaccounted accesses of armorrendering)
4+
- adjusted armor strength for generated materials

common/libs/arsenal-local.jar

4.44 KB
Binary file not shown.

common/src/main/java/smartin/miapi/client/gui/crafting/crafter/replace/CraftViewRework.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.joml.Vector4f;
1919
import smartin.miapi.Miapi;
2020
import smartin.miapi.client.gui.*;
21+
import smartin.miapi.client.gui.crafting.CraftingScreen;
2122
import smartin.miapi.craft.CraftAction;
2223
import smartin.miapi.modules.edit_options.EditOption;
2324
import smartin.miapi.modules.properties.slot.SlotProperty;
@@ -122,6 +123,7 @@ public CraftViewRework(int x, int y, int width, int height, int offset, CraftOpt
122123
ItemStack craftedStack = action.getPreview();
123124
if (!ItemStack.matches(editContext.getItemstack(), craftedStack)) {
124125
editContext.craft(action.toPacket(Networking.createBuffer()));
126+
CraftingScreen.getInstance().setItem(craftedStack);
125127
}
126128
editContext.getScreenHandler().removeSlotListener(listener);
127129
}

common/src/main/java/smartin/miapi/material/MaterialProperty.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,6 @@ public static Material getMaterial(JsonElement element) {
204204
return null;
205205
}
206206

207-
/**
208-
* Gets the used Material of a ModuleInstance
209-
*
210-
* @param instance
211-
* @return
212-
*/
213207
@Nullable
214208
/**
215209
* Gets the used Material of a ModuleInstance

common/src/main/java/smartin/miapi/material/generated/GeneratedMaterial.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class GeneratedMaterial implements Material {
6262
public Map<String, Map<ModuleProperty<?>, Object>> properties = new HashMap<>();
6363
SmithingMode smithingMode = SmithingMode.NONE;
6464
ItemStack smithingTemplate = ItemStack.EMPTY;
65+
Optional<Float> armorHardness = Optional.empty();
6566

6667
public static Codec<GeneratedMaterial> CODEC = RecordCodecBuilder.create((instance) ->
6768
instance.group(
@@ -77,19 +78,23 @@ public class GeneratedMaterial implements Material {
7778
Codec.list(ItemStack.CODEC)
7879
.fieldOf("toolItems")
7980
.forGetter(m -> m.toolItems.stream().map(Item::getDefaultInstance).toList()),
81+
Codec.FLOAT
82+
.optionalFieldOf("armor_hardness")
83+
.forGetter(m -> m.armorHardness),
8084
ResourceLocation.CODEC
8185
.optionalFieldOf("smithing_key")
8286
.forGetter(m -> m.smithingParent),
8387
ItemStack.CODEC
8488
.optionalFieldOf("smithing_template", ItemStack.EMPTY)
8589
.forGetter(m -> m.swordItem.getDefaultInstance())
86-
).apply(instance, (itemstack, additionalIngredient, swordItem, ingredient_toolItems, smithingKey, smithingItem) -> {
90+
).apply(instance, (itemstack, additionalIngredient, swordItem, ingredient_toolItems, armor, smithingKey, smithingItem) -> {
8791
GeneratedMaterial material = new GeneratedMaterial(itemstack, additionalIngredient, ((SwordItem) (swordItem.getItem())).getTier(),
8892
ingredient_toolItems.stream().map(itemStack -> (TieredItem) itemStack.getItem()).toList()
8993
);
9094
if (smithingKey != null && smithingKey.isPresent()) {
9195
material.setSmithingMaterial(smithingKey.get(), Ingredient.of(smithingItem));
9296
}
97+
armor.ifPresent(aFloat -> material.stats.put("armor_hardness", (double) aFloat));
9398
return material;
9499
}));
95100

@@ -191,7 +196,7 @@ public Material getMaterial(ModuleInstance moduleInstance) {
191196
@Override
192197
public double getDouble(String property) {
193198
if (property.equals("hardness")) {
194-
if(GeneratedMaterialManager.verboseLogging()){
199+
if (GeneratedMaterialManager.verboseLogging()) {
195200
Miapi.LOGGER.info("returning hardness " + stats.get("armor_hardness"));
196201
}
197202
return stats.get("armor_hardness");

common/src/main/java/smartin/miapi/material/generated/GeneratedMaterialManager.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,71 @@ public static void onReloadServer(RegistryAccess access) {
114114
toolMaterial.getTier().getRepairIngredient().getItems() != null &&
115115
toolMaterial.getTier().getRepairIngredient().getItems().length > 0)
116116
.filter(toolMaterial -> !toolMaterial.getTier().getRepairIngredient().getItems()[0].is(RegistryInventory.MIAPI_FORBIDDEN_TAG))
117+
.filter(GeneratedMaterialManager::isValidItem)
117118
.filter(toolMaterial -> Arrays.stream(toolMaterial.getTier().getRepairIngredient().getItems())
118119
.allMatch(itemStack -> MaterialProperty.getMaterialFromIngredient(itemStack) == null && !itemStack.getItem().equals(Items.BARRIER)))
120+
119121
.toList());
120122
Map<Tier, List<TieredItem>> tieredItem = new HashMap<>();
121123
toolItems.forEach(item -> {
122124
tieredItem.computeIfAbsent(item.getTier(), (i) -> new ArrayList<>()).add(item);
123125
});
126+
Map<Tier, List<TieredItem>> consolidated = new HashMap<>();
127+
128+
for (Map.Entry<Tier, List<TieredItem>> entry : tieredItem.entrySet()) {
129+
Tier currentTier = entry.getKey();
130+
List<TieredItem> currentItems = entry.getValue();
131+
132+
boolean merged = false;
133+
134+
for (Map.Entry<Tier, List<TieredItem>> consolidatedEntry : consolidated.entrySet()) {
135+
Tier existingTier = consolidatedEntry.getKey();
136+
137+
if (isSameTier(currentTier, existingTier)) {
138+
consolidatedEntry.getValue().addAll(currentItems);
139+
merged = true;
140+
break;
141+
}
142+
}
143+
144+
if (!merged) {
145+
// No matching tier found, so just put a fresh entry
146+
consolidated.put(currentTier, new ArrayList<>(currentItems));
147+
}
148+
}
124149
Map<Tier, List<TieredItem>> insufficientItems = new HashMap<>();
125-
tieredItem.forEach((t, items) -> {
150+
consolidated.forEach((t, items) -> {
126151
boolean hasSword = items.stream().anyMatch(SwordItem.class::isInstance);
127152
boolean hasAxe = items.stream().anyMatch(AxeItem.class::isInstance);
128153
if (!(hasSword && hasAxe)) {
129154
insufficientItems.put(t, items);
130155
}
131156
});
132-
insufficientItems.forEach((t, items) -> tieredItem.remove(t));
157+
insufficientItems.forEach((t, items) -> consolidated.remove(t));
133158

134159
if (MiapiConfig.getServerConfig().generatedMaterials.generateOtherMaterials) {
160+
consolidated.forEach((tier, tieredItems) -> {
161+
ItemStack mainIngredient = tier.getRepairIngredient().getItems()[0];
162+
if (verboseLogging()) {
163+
Miapi.LOGGER.info("attempting material generation for " + mainIngredient.getHoverName().getString());
164+
}
165+
if (isValidItem(mainIngredient.getItem())) {
166+
GeneratedMaterial generatedMaterial = new GeneratedMaterial(
167+
mainIngredient,
168+
tier.getRepairIngredient(),
169+
tier,
170+
tieredItems
171+
);
172+
if (generatedMaterial.isValid()) {
173+
if (verboseLogging()) {
174+
Miapi.LOGGER.info("Generated Material " + generatedMaterial.getID());
175+
}
176+
generatedMaterials.add(generatedMaterial);
177+
}
178+
}
179+
});
180+
/*
135181
toolItems.stream()
136-
.filter(GeneratedMaterialManager::isValidItem)
137182
.map(TieredItem::getTier)
138183
.collect(Collectors.toSet())
139184
.stream()
@@ -163,6 +208,8 @@ public static void onReloadServer(RegistryAccess access) {
163208
Miapi.LOGGER.error("could not generate Material for " + toolMaterial.getRepairIngredient().getItems()[0], e);
164209
}
165210
});
211+
212+
*/
166213
}
167214

168215

@@ -246,12 +293,36 @@ public static boolean isSameTier(Tier first, Tier second) {
246293
if (!isSameIngredient(first.getRepairIngredient(), second.getRepairIngredient())) {
247294
return false;
248295
}
296+
if (percentDif(first.getAttackDamageBonus(), second.getAttackDamageBonus()) > 0.1) {
297+
return false;
298+
}
299+
if (percentDif(first.getSpeed(), second.getSpeed()) > 0.1) {
300+
return false;
301+
}
302+
if (percentDif(first.getUses(), second.getUses()) >0.1) {
303+
return false;
304+
}
305+
if (percentDif(first.getEnchantmentValue(), second.getEnchantmentValue()) > 0.1) {
306+
return false;
307+
}
249308
} catch (RuntimeException runtimeException) {
250309
return false;
251310
}
252311
return true;
253312
}
254313

314+
public static double percentDif(int a, int b) {
315+
return percentDif(a, (double) b);
316+
}
317+
318+
public static double percentDif(float a, float b) {
319+
return percentDif(a, (double) b);
320+
}
321+
322+
public static double percentDif(double a, double b) {
323+
return (Math.abs(a - b) / ((a + b) / 2.0)) * 100.0;
324+
}
325+
255326
public static Tier selectBetterTier(Tier first, Tier second) {
256327
if (first.getAttackDamageBonus() == second.getAttackDamageBonus()) {
257328
return first.getSpeed() > second.getSpeed() ? first : second;

common/src/main/java/smartin/miapi/material/generated/GeneratedMaterialPropertyManager.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.JsonElement;
44
import com.mojang.datafixers.util.Either;
55
import com.mojang.serialization.JsonOps;
6+
import net.minecraft.core.Holder;
67
import net.minecraft.core.HolderLookup;
78
import net.minecraft.core.component.DataComponentType;
89
import net.minecraft.core.component.DataComponents;
@@ -12,7 +13,9 @@
1213
import net.minecraft.tags.TagKey;
1314
import net.minecraft.world.entity.EquipmentSlot;
1415
import net.minecraft.world.entity.EquipmentSlotGroup;
16+
import net.minecraft.world.entity.ai.attributes.Attribute;
1517
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
18+
import net.minecraft.world.entity.ai.attributes.Attributes;
1619
import net.minecraft.world.item.*;
1720
import net.minecraft.world.item.component.ItemAttributeModifiers;
1821
import net.minecraft.world.item.enchantment.ItemEnchantments;
@@ -165,12 +168,14 @@ private static void setupProperties(Item item, ResourceLocation id, String type,
165168
.stream()
166169
.filter(a -> !firstAttributes.contains(a.attribute()) && !secondAttributes.contains(a.attribute()))
167170
.filter(a -> a.modifier().operation() == AttributeModifier.Operation.ADD_VALUE)
171+
.filter(GeneratedMaterialPropertyManager::isRelevantAttribute)
168172
.toList();
169173
if (modifiers.isEmpty()) {
170174
modifiers = item.getDefaultAttributeModifiers().modifiers()
171175
.stream()
172176
.filter(a -> !firstAttributes.contains(a.attribute()) && !secondAttributes.contains(a.attribute()))
173177
.filter(a -> a.modifier().operation() == AttributeModifier.Operation.ADD_VALUE)
178+
.filter(GeneratedMaterialPropertyManager::isRelevantAttribute)
174179
.toList();
175180
}
176181

@@ -191,6 +196,22 @@ private static void setupProperties(Item item, ResourceLocation id, String type,
191196
properties.put(type, propertyMap);
192197
}
193198

199+
private static final Set<Holder<Attribute>> IGNORED_ATTRIBUTES = Set.of(
200+
Attributes.ATTACK_DAMAGE,
201+
Attributes.ATTACK_SPEED,
202+
Attributes.ARMOR,
203+
Attributes.ARMOR_TOUGHNESS,
204+
Attributes.KNOCKBACK_RESISTANCE
205+
);
206+
207+
private static boolean isRelevantAttribute(ItemAttributeModifiers.Entry a) {
208+
boolean att = IGNORED_ATTRIBUTES.contains(a.attribute());
209+
if (!att) {
210+
Miapi.LOGGER.info("valid attribute!" + a.attribute().getRegisteredName());
211+
}
212+
return !att;
213+
}
214+
194215
public static boolean shouldApplyProperty(
195216
MiapiServerConfig.GeneratedMaterialsCategory.GeneratePropertyOption config, String idString) {
196217

fabric/libs/arsenal-local.jar

4.44 KB
Binary file not shown.

neoforge/libs/arsenal-local.jar

4.44 KB
Binary file not shown.

0 commit comments

Comments
 (0)