Skip to content

Commit dc4cb35

Browse files
committed
Fix adamantium armor enchantment value
1 parent aa0fbf0 commit dc4cb35

6 files changed

Lines changed: 96 additions & 111 deletions

File tree

src/main/java/huix/infinity/common/world/block/IFWAnvilBlock.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import huix.infinity.common.world.block.entity.AnvilBlockEntity;
44
import huix.infinity.common.world.inventory.IFWAnvilMenu;
5+
import huix.infinity.common.world.item.tier.IFWTier;
56
import huix.infinity.common.world.item.tier.IFWTiers;
67
import huix.infinity.util.DurabilityHelper;
78
import net.minecraft.core.BlockPos;
@@ -29,9 +30,9 @@
2930
public class IFWAnvilBlock extends AnvilBlock implements EntityBlock {
3031
private final int maxDurability;
3132
private final int repairLevel;
32-
private final IFWTiers tier;
33+
private final IFWTier tier;
3334

34-
public IFWAnvilBlock(Properties properties, IFWTiers tier) {
35+
public IFWAnvilBlock(Properties properties, IFWTier tier) {
3536
super(properties);
3637
this.tier = tier;
3738
this.repairLevel = tier.repairLevel();
@@ -100,26 +101,26 @@ public BlockState getBrokeState(int currentDamage) {
100101

101102
private BlockState getChippedState() {
102103
return switch (this.tier) {
103-
case COPPER -> IFWBlocks.chipped_copper_anvil.get().defaultBlockState();
104-
case SILVER -> IFWBlocks.chipped_silver_anvil.get().defaultBlockState();
105-
case GOLD -> IFWBlocks.chipped_gold_anvil.get().defaultBlockState();
106-
case IRON -> IFWBlocks.chipped_iron_anvil.get().defaultBlockState();
107-
case ANCIENT_METAL -> IFWBlocks.chipped_ancient_metal_anvil.get().defaultBlockState();
108-
case MITHRIL -> IFWBlocks.chipped_mithril_anvil.get().defaultBlockState();
109-
case ADAMANTIUM -> IFWBlocks.chipped_adamantium_anvil.get().defaultBlockState();
104+
case IFWTiers.COPPER -> IFWBlocks.chipped_copper_anvil.get().defaultBlockState();
105+
case IFWTiers.SILVER -> IFWBlocks.chipped_silver_anvil.get().defaultBlockState();
106+
case IFWTiers.GOLD -> IFWBlocks.chipped_gold_anvil.get().defaultBlockState();
107+
case IFWTiers.IRON -> IFWBlocks.chipped_iron_anvil.get().defaultBlockState();
108+
case IFWTiers.ANCIENT_METAL -> IFWBlocks.chipped_ancient_metal_anvil.get().defaultBlockState();
109+
case IFWTiers.MITHRIL -> IFWBlocks.chipped_mithril_anvil.get().defaultBlockState();
110+
case IFWTiers.ADAMANTIUM -> IFWBlocks.chipped_adamantium_anvil.get().defaultBlockState();
110111
default -> Blocks.AIR.defaultBlockState();
111112
};
112113
}
113114

114115
private BlockState getDamagedState() {
115116
return switch (this.tier) {
116-
case COPPER -> IFWBlocks.damaged_copper_anvil.get().defaultBlockState();
117-
case SILVER -> IFWBlocks.damaged_silver_anvil.get().defaultBlockState();
118-
case GOLD -> IFWBlocks.damaged_gold_anvil.get().defaultBlockState();
119-
case IRON -> IFWBlocks.damaged_iron_anvil.get().defaultBlockState();
120-
case ANCIENT_METAL -> IFWBlocks.damaged_ancient_metal_anvil.get().defaultBlockState();
121-
case MITHRIL -> IFWBlocks.damaged_mithril_anvil.get().defaultBlockState();
122-
case ADAMANTIUM -> IFWBlocks.damaged_adamantium_anvil.get().defaultBlockState();
117+
case IFWTiers.COPPER -> IFWBlocks.damaged_copper_anvil.get().defaultBlockState();
118+
case IFWTiers.SILVER -> IFWBlocks.damaged_silver_anvil.get().defaultBlockState();
119+
case IFWTiers.GOLD -> IFWBlocks.damaged_gold_anvil.get().defaultBlockState();
120+
case IFWTiers.IRON -> IFWBlocks.damaged_iron_anvil.get().defaultBlockState();
121+
case IFWTiers.ANCIENT_METAL -> IFWBlocks.damaged_ancient_metal_anvil.get().defaultBlockState();
122+
case IFWTiers.MITHRIL -> IFWBlocks.damaged_mithril_anvil.get().defaultBlockState();
123+
case IFWTiers.ADAMANTIUM -> IFWBlocks.damaged_adamantium_anvil.get().defaultBlockState();
123124
default -> Blocks.AIR.defaultBlockState();
124125
};
125126
}
@@ -140,7 +141,7 @@ public int maxDurability() {
140141
return this.maxDurability;
141142
}
142143

143-
public IFWTiers tier() {
144+
public IFWTier tier() {
144145
return this.tier;
145146
}
146147

src/main/java/huix/infinity/common/world/inventory/IFWAnvilMenu.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ public IFWAnvilMenu(int containerId, Inventory playerInventory, ContainerLevelAc
6666
this.addDataSlots(this.anvilData);
6767
this.addDataSlot(this.repairLevel);
6868
access.execute((level, pos) -> {
69-
Block block = level.getBlockState(pos).getBlock();
70-
if (block instanceof IFWAnvilBlock anvilBlock) {
69+
if (level.getBlockState(pos).getBlock() instanceof IFWAnvilBlock anvilBlock) {
7170
this.maxDurability = anvilBlock.maxDurability();
7271
this.repairLevel.set(anvilBlock.repairLevel());
7372
}
7473

75-
BlockEntity tileEntity = level.getBlockEntity(pos);
76-
if (tileEntity instanceof AnvilBlockEntity anvilBlockEntity) {
74+
if (level.getBlockEntity(pos) instanceof AnvilBlockEntity anvilBlockEntity) {
7775
this.currentDurability = this.maxDurability - anvilBlockEntity.damage();
7876
}
7977

@@ -118,17 +116,16 @@ protected void onTake(Player player, @NotNull ItemStack stack) {
118116
this.access.execute((level, pos) -> {
119117
BlockState blockstate = level.getBlockState(pos);
120118
if (!player.getAbilities().instabuild && blockstate.is(BlockTags.ANVIL)) {
121-
BlockEntity oldEntity = level.getBlockEntity(pos);
122-
BlockState resultBlock = ((IFWAnvilBlock) blockstate.getBlock()).ifw_damage(this.durabilityCost, oldEntity);
119+
BlockEntity entity = level.getBlockEntity(pos);
120+
BlockState resultBlock = ((IFWAnvilBlock) blockstate.getBlock()).ifw_damage(this.durabilityCost, entity);
123121
if (resultBlock == null) {
124122
level.removeBlock(pos, false);
125123
level.levelEvent(1029, pos, 0);
126124
} else {
127125
level.setBlock(pos, resultBlock, 2);
128-
BlockEntity entity = level.getBlockEntity(pos);
129-
if (entity instanceof AnvilBlockEntity newEntity) {
130-
this.currentDurability = this.maxDurability - ((AnvilBlockEntity)oldEntity).damage();
131-
newEntity.damage(this.maxDurability - this.currentDurability);
126+
if (entity instanceof AnvilBlockEntity anvilEntity) {
127+
this.currentDurability = this.maxDurability - anvilEntity.damage();
128+
anvilEntity.damage(this.maxDurability - this.currentDurability);
132129
this.sendAllDataToRemote();
133130
}
134131

@@ -171,7 +168,7 @@ public void createResult() {
171168
return;
172169
}
173170

174-
this.repairItemCountCost = Math.min((currentDamage - currentDamage % repairCost) / repairCost, ingredient.getCount());
171+
this.repairItemCountCost = Math.min(currentDamage / repairCost, ingredient.getCount());
175172
input.setDamageValue(currentDamage - this.repairItemCountCost * repairCost);
176173
this.resultSlots.setItem(1, input);
177174
this.durabilityCost = this.repairItemCountCost * repairCost;

src/main/java/huix/infinity/common/world/item/tier/IFWArmorMaterials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class IFWArmorMaterials {
8989
map.put(ArmorItem.Type.BOOTS, 1.7F);map.put(ArmorItem.Type.LEGGINGS, 2.9F);
9090
map.put(ArmorItem.Type.CHESTPLATE, 3.3F);map.put(ArmorItem.Type.HELMET, 2.1F);
9191
map.put(ArmorItem.Type.BODY, 3.3F);}),
92-
70, SoundEvents.ARMOR_EQUIP_IRON, 3.0F, 0.1F, 1000, 0, 5,
92+
40, SoundEvents.ARMOR_EQUIP_IRON, 3.0F, 0.1F, 1000, 0, 5,
9393
() -> Ingredient.of(IFWItems.adamantium_nugget), "adamantium"));
9494
public static final Holder<ArmorMaterial> copper_chainmail = ARMOR_MATERIALS.register("copper_chainmail", () ->
9595
register(Util.make(new EnumMap<>(ArmorItem.Type.class), map -> {

src/main/java/huix/infinity/util/DurabilityHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package huix.infinity.util;
22

33
import huix.infinity.common.world.block.IFWAnvilBlock;
4+
import huix.infinity.common.world.item.tier.IFWTier;
45
import huix.infinity.common.world.item.tier.IFWTiers;
56

67
public class DurabilityHelper {
@@ -14,7 +15,7 @@ private static float getBaseDurability() {
1415
return 4.0F;
1516
}
1617

17-
public static int getDurability(IFWTiers tier) {
18+
public static int getDurability(IFWTier tier) {
1819
return (int)((float)(getBaseDurabilityPerIngot() * 31) * tier.getDurability());
1920
}
2021

src/main/java/huix/infinity/util/IFWEnchantmentHelper.java

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import net.minecraft.world.item.enchantment.EnchantmentHelper;
1212
import net.minecraft.world.item.enchantment.EnchantmentInstance;
1313

14-
import java.util.Iterator;
15-
import java.util.List;
16-
import java.util.Optional;
14+
import java.util.*;
1715
import java.util.stream.Stream;
1816

1917
public class IFWEnchantmentHelper {
@@ -27,94 +25,71 @@ public static int getExperienceLevel(final int enchantmentCost) {
2725
}
2826

2927
public static int calculateRequiredExperienceLevel(final RandomSource random, final int slot, int bookshelfCount, final ItemStack stack, final int enchantingMultiplier) {
30-
Item item = stack.getItem();
31-
int i = item.getEnchantmentValue();
32-
if (i <= 0) {
33-
return 0;
34-
} else {
35-
if (bookshelfCount > 24) {
36-
bookshelfCount = 24;
37-
}
28+
final Item item = stack.getItem();
29+
if (item.getEnchantmentValue() <= 0) return 0;
3830

39-
int enchantment_table_power = (1 + bookshelfCount) * 2 * enchantingMultiplier;
40-
int enchantment_levels = getEnchantmentLevelsAlteredByItem(enchantment_table_power, item);
41-
float fraction = (1.0F + (float)slot) / 3.0F;
42-
if (slot < 2) {
43-
fraction += (random.nextFloat() - 0.5F) * 0.2F;
44-
}
31+
bookshelfCount = Math.min(bookshelfCount, 24);
32+
final int enchantmentTablePower = (bookshelfCount + 1) * 2 * enchantingMultiplier;
33+
final int enchantmentLevels = enchantmentLevelsByItem(enchantmentTablePower, item);
4534

46-
return Math.max(Math.round((float)enchantment_levels * fraction), 1);
47-
}
48-
}
35+
float fraction = (slot + 1) / 3.0F;
36+
if (slot < 2) fraction += (random.nextFloat() - 0.5F) * 0.2F;
4937

50-
public static int getEnchantmentLevelsAlteredByItem(final int enchantment_table_power, final Item item) {
51-
int enchantability = item.getEnchantmentValue();
52-
if (enchantability < 1) {
53-
return 0;
54-
} else if (enchantment_table_power <= enchantability) {
55-
return enchantment_table_power;
56-
} else {
57-
float enchantmentLevelsFloat = (float) enchantability;
58-
59-
for(int i = enchantability + 1; i <= enchantment_table_power; ++i) {
60-
if (i <= enchantability * 2) {
61-
enchantmentLevelsFloat += 0.5F;
62-
} else {
63-
if (i > enchantability * 3) {
64-
break;
65-
}
66-
67-
enchantmentLevelsFloat += 0.25F;
68-
}
69-
}
38+
return Math.max(Math.round(enchantmentLevels * fraction), 1);
39+
}
7040

71-
return Math.round(enchantmentLevelsFloat);
41+
private static int enchantmentLevelsByItem(final int enchantment_table_power, final Item item) {
42+
int value = item.getEnchantmentValue();
43+
System.out.println(value);
44+
if (value < 1) return 0;
45+
else if (enchantment_table_power <= value) return enchantment_table_power;
46+
else {
47+
int startA = value + 1;
48+
int endA = Math.min(2 * value, enchantment_table_power);
49+
int countA = endA >= startA ? endA - startA + 1 : 0;
50+
51+
int startB = 2 * value + 1;
52+
int endB = Math.min(3 * value, enchantment_table_power);
53+
int countB = endB >= startB ? endB - startB + 1 : 0;
54+
55+
return Math.round(value + 0.5f * countA + 0.25f * countB);
7256
}
7357
}
7458

7559
public static List<EnchantmentInstance> selectEnchantment(final RandomSource random, final ItemStack stack, int cost, final Stream<Holder<Enchantment>> possibleEnchantments) {
76-
List<EnchantmentInstance> enchantmentsToAdd = Lists.newArrayList();
77-
int i = stack.getEnchantmentValue();
78-
if (i > 0) {
79-
float levelFactor = 1.0F + (random.nextFloat() - 0.5F) * 0.5F;
80-
cost = getExperienceLevel(cost);
81-
List<Holder<Enchantment>> streamList = possibleEnchantments.toList();
82-
List<EnchantmentInstance> possibleInstances = getAvailableEnchantmentResults(cost, stack, streamList.stream());
83-
cost = Mth.clamp(Math.round((float)cost * levelFactor), 1, Integer.MAX_VALUE);
84-
85-
while(cost > 0 && enchantmentsToAdd.size() <= 2 && !possibleInstances.isEmpty()) {
86-
possibleInstances = getAvailableEnchantmentResults(cost, stack, streamList.stream());
87-
88-
Iterator<EnchantmentInstance> iterator = enchantmentsToAdd.iterator();
89-
EnchantmentInstance e;
90-
while(iterator.hasNext()) {
91-
e = iterator.next();
92-
EnchantmentHelper.filterCompatibleEnchantments(possibleInstances, e);
93-
}
60+
if (stack.getEnchantmentValue() <= 0) return Collections.emptyList();
61+
List<EnchantmentInstance> selectedEnchantments = new ArrayList<>();
9462

95-
if (possibleInstances.isEmpty()) {
96-
break;
97-
}
63+
float levelFactor = 1.0F + (random.nextFloat() - 0.5F) * 0.5F;
64+
cost = getExperienceLevel(cost);
65+
cost = Mth.clamp(Math.round((float) cost * levelFactor), 1, Integer.MAX_VALUE);
9866

99-
Optional<EnchantmentInstance> instance = WeightedRandom.getRandomItem(random, possibleInstances);
100-
if (instance.isPresent()) {
101-
e = instance.get();
102-
if (enchantmentsToAdd.size() < 2 && possibleInstances.size() > 1 && e.enchantment.value().ifw_hasLevels() && random.nextInt(2) == 0) {
103-
e.level = random.nextInt(e.level) + 1;
104-
}
67+
final List<Holder<Enchantment>> candidateEnchantments = possibleEnchantments.toList();
10568

69+
while (cost > 0 && selectedEnchantments.size() < 3) {
70+
List<EnchantmentInstance> availableEnchantments = getAvailableEnchantmentResults(cost, stack, candidateEnchantments.stream());
10671

107-
enchantmentsToAdd.add(e);
108-
cost -= e.enchantment.value().ifw_hasLevels() ? e.enchantment.value().getMinCost(e.level) : e.enchantment.value().getMinCost(1) + 5;
109-
}
72+
for (EnchantmentInstance selected : selectedEnchantments) EnchantmentHelper.filterCompatibleEnchantments(availableEnchantments, selected);
11073

111-
if (cost < 5) {
112-
break;
113-
}
74+
if (availableEnchantments.isEmpty()) break;
75+
76+
Optional<EnchantmentInstance> optionalEnchantment = WeightedRandom.getRandomItem(random, availableEnchantments);
77+
if (optionalEnchantment.isPresent()) {
78+
EnchantmentInstance chosen = optionalEnchantment.get();
79+
Holder<Enchantment> enchantmentHolder = chosen.enchantment;
80+
81+
if (selectedEnchantments.size() < 2 && availableEnchantments.size() > 1 && enchantmentHolder.value().ifw_hasLevels() && random.nextInt(2) == 0)
82+
chosen.level = random.nextInt(chosen.level) + 1;
83+
84+
selectedEnchantments.add(chosen);
85+
86+
int costReduction = enchantmentHolder.value().ifw_hasLevels() ? enchantmentHolder.value().getMinCost(chosen.level) : enchantmentHolder.value().getMinCost(1) + 5;
87+
cost -= costReduction;
11488
}
115-
}
11689

117-
return enchantmentsToAdd;
90+
if (cost < 5) break;
91+
}
92+
return selectedEnchantments;
11893
}
11994

12095
public static List<EnchantmentInstance> getAvailableEnchantmentResults(int level, ItemStack stack, Stream<Holder<Enchantment>> possibleEnchantments) {
@@ -135,10 +110,14 @@ public static List<EnchantmentInstance> getAvailableEnchantmentResults(int level
135110
}
136111

137112
public static float getProtectionFactor(ItemStack itemStack) {
138-
if (itemStack.isDamageableItem() && itemStack.getDamageValue() >= itemStack.getMaxDamage() - 1) {
139-
return 0.0F;
140-
} else {
141-
return Math.min(2.0F - (float) itemStack.getDamageValue() / itemStack.getMaxDamage() * 2.0F, 1.0F);
142-
}
113+
if (!itemStack.isDamageableItem()) return 1.0F;
114+
115+
final int maxDamage = itemStack.getMaxDamage();
116+
final int damage = itemStack.getDamageValue();
117+
118+
if (damage >= maxDamage - 1) return 0.0F;
119+
120+
float factor = 2.0F * (1.0F - ((float) damage / maxDamage));
121+
return Math.min(factor, 1.0F);
143122
}
144123
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"neoforge:conditions": [
3+
{
4+
"type": "neoforge:false"
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)