Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make upgrade not poof on mod update #227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.level.BlockEvent;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand All @@ -79,6 +80,18 @@ public MiningGadget() {

//TODO Add an override for onCreated and initialize all NBT Tags in it

@Override
public void verifyTagAfterLoad(@NotNull CompoundTag tag) {
if (UpgradeTools.containsUpgrades(tag)) {
UpgradeTools.walkUpgradesOnTag(tag, (CompoundTag upgradeTag, String upgradeName) -> {
if (upgradeName.equalsIgnoreCase("three_by_three")) {
return Upgrade.SIZE_1.getName();
}
return null;
});
}
}

@Override
public int getMaxDamage(ItemStack stack) {
return this.energyCapacity;
Expand Down Expand Up @@ -414,7 +427,7 @@ else if (g > 0 && b == 1)
efficiency = UpgradeTools.getUpgradeFromGadget((stack), Upgrade.EFFICIENCY_1).get().getTier();

float hardness = getHardness(coords, (Player) player, efficiency);
hardness = hardness * MiningProperties.getRange(stack) * 1;
// hardness = hardness * MiningProperties.getRange(stack) * 1;
hardness = (float) Math.floor(hardness);
if (hardness == 0) hardness = 1;
for (BlockPos coord : coords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class UpgradeTools {
Expand Down Expand Up @@ -120,6 +121,23 @@ public static List<Upgrade> getActiveUpgradesFromTag(CompoundTag tagCompound) {
return functionalUpgrades;
}

public static void walkUpgradesOnTag(CompoundTag tagCompound, BiFunction<CompoundTag, String, String> consumer) {
ListTag upgrades = tagCompound.getList(KEY_UPGRADES, Tag.TAG_COMPOUND);

if (upgrades.isEmpty())
return;

for (int i = 0; i < upgrades.size(); i++) {
CompoundTag tag = upgrades.getCompound(i);

var name = tag.getString(KEY_UPGRADE);
var result = consumer.apply(tag, name);
if (result != null && !result.equalsIgnoreCase(name)) {
tag.putString(KEY_UPGRADE, result);
}
}
}

@Nullable
public static Upgrade getUpgradeByName(String name) {
// If the name doesn't exist then move on
Expand All @@ -145,6 +163,9 @@ public static List<Upgrade> getActiveUpgrades(ItemStack tool) {
public static boolean containsUpgrades(ItemStack tool) {
return tool.getOrCreateTag().contains(KEY_UPGRADES);
}
public static boolean containsUpgrades(CompoundTag tag) {
return tag.contains(KEY_UPGRADES);
}

/**
* Get a single upgrade and it's tier
Expand Down