Skip to content

Commit 3bfa66c

Browse files
committed
PR Tweaks
1 parent ef2a8d1 commit 3bfa66c

File tree

7 files changed

+51
-35
lines changed

7 files changed

+51
-35
lines changed

src/main/java/aztech/modern_industrialization/MI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import net.minecraft.server.packs.repository.Pack;
6666
import net.minecraft.server.packs.repository.PackCompatibility;
6767
import net.minecraft.server.packs.repository.PackSource;
68+
import net.minecraft.util.Mth;
6869
import net.minecraft.world.InteractionResult;
6970
import net.minecraft.world.entity.EntityType;
7071
import net.minecraft.world.entity.LivingEntity;
@@ -186,7 +187,7 @@ public MI(IEventBus modBus, Dist dist) {
186187
});
187188
NeoForge.EVENT_BUS.addListener(LivingIncomingDamageEvent.class, event -> {
188189
if (event.getSource().getDirectEntity() instanceof LivingEntity damager
189-
&& damager.getAttributeValue(MIRegistries.INFINITE_DAMAGE) > 0) {
190+
&& damager.getAttributeValue(MIRegistries.INFINITE_DAMAGE) > Mth.EPSILON) {
190191
event.setAmount((float) Integer.MAX_VALUE);
191192
}
192193
});

src/main/java/aztech/modern_industrialization/attributes/DisplayNamedAttribute.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ protected DisplayNamedAttribute(String descriptionId, double defaultValue) {
3535
super(descriptionId, defaultValue);
3636
}
3737

38-
public abstract String getDisplayDescriptionId();
38+
/**
39+
* Translation key to use in tooltips, thanks to our override of {@link #toComponent(AttributeModifier, TooltipFlag)}.
40+
* {@link #getDescriptionId()} is still used in /attribute commands and other contexts.
41+
*/
42+
public abstract String getTooltipDescriptionId();
3943

4044
/*
41-
* The below two methods are copied from IAttributeExtension and use DisplayAttribute#getDisplayDescriptionId so that Attribute#getDescriptionId
45+
* The below methods is copied from IAttributeExtension and uses DisplayAttribute#getDisplayDescriptionId so that Attribute#getDescriptionId
4246
* can return the MIText.AttributeQuantumArmor description id. This is desirable because when using the /attribute command it will display the
4347
* attribute name as "Quantum Armor" instead of just "Armor" like it does when using "attribute.name.generic.armor" as the description id (or
4448
* "Infinite Damage" instead of just "Damage" for the InfiniteDamageAttribute).
@@ -50,23 +54,10 @@ public MutableComponent toComponent(AttributeModifier modif, TooltipFlag flag) {
5054
String key = value > 0 ? "neoforge.modifier.plus" : "neoforge.modifier.take";
5155
ChatFormatting color = this.getStyle(value > 0);
5256

53-
Component attrDesc = Component.translatable(this.getDisplayDescriptionId());
57+
Component attrDesc = Component.translatable(this.getTooltipDescriptionId());
5458
Component valueComp = this.toValueComponent(modif.operation(), value, flag);
5559
MutableComponent comp = Component.translatable(key, valueComp, attrDesc).withStyle(color);
5660

5761
return comp.append(this.getDebugInfo(modif, flag));
5862
}
59-
60-
@Override
61-
public MutableComponent toBaseComponent(double value, double entityBase, boolean merged, TooltipFlag flag) {
62-
MutableComponent comp = Component.translatable("attribute.modifier.equals.0", FORMAT.format(value),
63-
Component.translatable(this.getDisplayDescriptionId()));
64-
if (flag.isAdvanced() && !merged) {
65-
Component debugInfo = Component.literal(" ")
66-
.append(Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), FORMAT.format(value - entityBase))
67-
.withStyle(ChatFormatting.GRAY));
68-
comp.append(debugInfo);
69-
}
70-
return comp;
71-
}
7263
}

src/main/java/aztech/modern_industrialization/attributes/InfiniteDamageAttribute.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,51 @@
2424
package aztech.modern_industrialization.attributes;
2525

2626
import aztech.modern_industrialization.MIText;
27+
import aztech.modern_industrialization.items.tools.QuantumSword;
28+
import net.minecraft.ChatFormatting;
2729
import net.minecraft.network.chat.Component;
2830
import net.minecraft.network.chat.MutableComponent;
31+
import net.minecraft.resources.ResourceLocation;
32+
import net.minecraft.util.Mth;
2933
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
3034
import net.minecraft.world.item.TooltipFlag;
35+
import org.jetbrains.annotations.Nullable;
3136

3237
public class InfiniteDamageAttribute extends DisplayNamedAttribute {
38+
private static final String INFINITY = "\u221e";
39+
3340
public InfiniteDamageAttribute() {
3441
super(MIText.AttributeInfiniteDamage.getTranslationKey(), 0);
3542
}
3643

44+
// Allows a modifier with the base id to render green with the effective value.
45+
// Other modifiers still render blue with the additional value.
46+
@Override
47+
public @Nullable ResourceLocation getBaseId() {
48+
return QuantumSword.BASE_INFINITE_DAMAGE;
49+
}
50+
3751
@Override
3852
public MutableComponent toValueComponent(AttributeModifier.Operation operation, double value, TooltipFlag flag) {
39-
return Component.literal("\u221e");
53+
return Component.literal(INFINITY);
54+
}
55+
56+
@Override
57+
public MutableComponent toBaseComponent(double value, double entityBase, boolean merged, TooltipFlag flag) {
58+
String stringValue = value > Mth.EPSILON ? INFINITY : FORMAT.format(0);
59+
MutableComponent comp = Component.translatable("attribute.modifier.equals.0", stringValue,
60+
Component.translatable(this.getTooltipDescriptionId()));
61+
if (flag.isAdvanced() && !merged) {
62+
Component debugInfo = Component.literal(" ")
63+
.append(Component.translatable("neoforge.attribute.debug.base", FORMAT.format(entityBase), FORMAT.format(value - entityBase))
64+
.withStyle(ChatFormatting.GRAY));
65+
comp.append(debugInfo);
66+
}
67+
return comp;
4068
}
4169

4270
@Override
43-
public String getDisplayDescriptionId() {
71+
public String getTooltipDescriptionId() {
4472
return "attribute.name.generic.attack_damage";
4573
}
4674
}

src/main/java/aztech/modern_industrialization/attributes/QuantumArmorAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public MutableComponent toValueComponent(AttributeModifier.Operation operation,
4040
}
4141

4242
@Override
43-
public String getDisplayDescriptionId() {
43+
public String getTooltipDescriptionId() {
4444
return "attribute.name.generic.armor";
4545
}
4646
}

src/main/java/aztech/modern_industrialization/items/armor/MIArmorEffects.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ private MIArmorEffects() {
3838
}
3939

4040
public static boolean quantumArmorPreventsDamage(LivingEntity entity) {
41-
var attribute = entity.getAttribute(MIRegistries.QUANTUM_ARMOR);
42-
int parts = attribute != null ? (int) attribute.getValue() : 0;
43-
return parts >= 4 || ThreadLocalRandom.current().nextDouble() < parts / 4d;
41+
double parts = entity.getAttributeValue(MIRegistries.QUANTUM_ARMOR);
42+
return ThreadLocalRandom.current().nextDouble() < parts / 4d;
4443
}
4544

4645
public static boolean canTankFlyIntoWall(ItemStack helmet) {

src/main/java/aztech/modern_industrialization/items/armor/QuantumArmorItem.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333

3434
public class QuantumArmorItem extends ArmorItem {
3535
public QuantumArmorItem(ArmorItem.Type type, Properties settings) {
36-
super(MIArmorMaterials.QUANTUM, type, settings.stacksTo(1));
36+
super(MIArmorMaterials.QUANTUM, type, settings.stacksTo(1).attributes(buildModifiers(type)));
3737
}
3838

39-
@Override
40-
public ItemAttributeModifiers getDefaultAttributeModifiers() {
39+
private static ItemAttributeModifiers buildModifiers(ArmorItem.Type type) {
4140
var builder = ItemAttributeModifiers.builder()
4241
.add(
4342
MIRegistries.QUANTUM_ARMOR,

src/main/java/aztech/modern_industrialization/items/tools/QuantumSword.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import aztech.modern_industrialization.MI;
2727
import aztech.modern_industrialization.MIRegistries;
2828
import net.minecraft.core.BlockPos;
29+
import net.minecraft.resources.ResourceLocation;
2930
import net.minecraft.world.entity.EquipmentSlotGroup;
3031
import net.minecraft.world.entity.LivingEntity;
3132
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
@@ -37,26 +38,23 @@
3738
import net.minecraft.world.level.block.state.BlockState;
3839

3940
public class QuantumSword extends Item {
40-
public QuantumSword(Properties settings) {
41-
super(settings);
42-
}
41+
public static final ResourceLocation BASE_INFINITE_DAMAGE = MI.id("base_infinite_damage");
4342

44-
@Override
45-
public ItemAttributeModifiers getDefaultAttributeModifiers() {
46-
return ItemAttributeModifiers.builder()
43+
public QuantumSword(Properties settings) {
44+
super(settings.attributes(ItemAttributeModifiers.builder()
4745
.add(
4846
MIRegistries.INFINITE_DAMAGE,
49-
new AttributeModifier(MI.id("infinite_damage"), 1, AttributeModifier.Operation.ADD_VALUE),
47+
new AttributeModifier(BASE_INFINITE_DAMAGE, 1, AttributeModifier.Operation.ADD_VALUE),
5048
EquipmentSlotGroup.MAINHAND)
51-
.build();
49+
.build()));
5250
}
5351

5452
@Override
5553
public boolean hurtEnemy(ItemStack pStack, LivingEntity pTarget, LivingEntity pAttacker) {
5654
// TODO: if lama was hit, kill the wander trader (and the opposite) and give an
5755
// advancement
5856
// TODO: same for phantoms
59-
return false;
57+
return true;
6058
}
6159

6260
@Override

0 commit comments

Comments
 (0)