Skip to content

Commit

Permalink
Merge pull request #198 from Anoyomouse/creative_battery
Browse files Browse the repository at this point in the history
[Enhancement] Creative battery and Updated to ForceCapabilities
  • Loading branch information
MichaelHillcox authored Oct 20, 2023
2 parents d91cd3b + b21108c commit ae2fe60
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"item.mininggadgets.upgrade_battery_1": "Upgrade: Battery, Tier 1",
"item.mininggadgets.upgrade_battery_2": "Upgrade: Battery, Tier 2",
"item.mininggadgets.upgrade_battery_3": "Upgrade: Battery, Tier 3",
"item.mininggadgets.upgrade_battery_creative": "Upgrade: Battery, Creative",
"item.mininggadgets.upgrade_efficiency_1": "Upgrade: Efficiency, Tier 1",
"item.mininggadgets.upgrade_efficiency_2": "Upgrade: Efficiency, Tier 2",
"item.mininggadgets.upgrade_efficiency_3": "Upgrade: Efficiency, Tier 3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "mininggadgets:item/upgrade_battery_creative"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ public static boolean insertButton(ModificationTableContainer container, ItemSta
// Did we just insert a battery upgrade?
if(card.getBaseName().equals(Upgrade.BATTERY_1.getBaseName())) {
UpgradeBatteryLevels.getBatteryByLevel(card.getTier()).ifPresent(power -> {
laser.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> ((EnergisedItem) e).updatedMaxEnergy(power.getPower()));
laser.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> {
((EnergisedItem) e).updatedMaxEnergy(power.getPower());
if (card.getTier() == Upgrade.BATTERY_CREATIVE.getTier()) {
e.receiveEnergy(e.getMaxEnergyStored() - e.getEnergyStored(), false);
}
});
});
MiningProperties.setBatteryTier(laser, card.getTier());
}

return true;
Expand Down Expand Up @@ -98,8 +104,10 @@ public static void extractButton(ModificationTableContainer container, ServerPla
MiningProperties.setBeamMaxRange(laser, UpgradeTools.getMaxBeamRange(0));
}

if (upgrade.getBaseName().equals(Upgrade.BATTERY_1.getBaseName()))
if (upgrade.getBaseName().equals(Upgrade.BATTERY_1.getBaseName())) {
MiningProperties.setBatteryTier(laser, 0);
laser.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> ((EnergisedItem) e).updatedMaxEnergy(UpgradeBatteryLevels.BATTERY.getPower()));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected void addTranslations() {
addItem(ModItems.BATTERY_1, "Upgrade: Battery, Tier 1");
addItem(ModItems.BATTERY_2, "Upgrade: Battery, Tier 2");
addItem(ModItems.BATTERY_3, "Upgrade: Battery, Tier 3");
addItem(ModItems.BATTERY_CREATIVE, "Upgrade: Creative Battery");

// Upgrade tooltips :D
add("tooltop.mininggadgets.empty", "Used to craft other upgrades");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static int getMaxCapacity(ItemStack stack, int capacity) {
public void updatedMaxEnergy(int max) {
stack.getOrCreateTag().putInt("max_energy", max);
this.capacity = max;
this.energy = Math.min(max, this.energy);

// Ensure the current stored energy is up to date with the new max.
this.receiveEnergy(1, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public int getMaxDamage(ItemStack stack) {

@Override
public boolean isBarVisible(ItemStack stack) {
if (MiningProperties.getBatteryTier(stack) == Upgrade.BATTERY_CREATIVE.getTier())
return false;

IEnergyStorage energy = stack.getCapability(ForgeCapabilities.ENERGY, null).orElse(null);
return (energy.getEnergyStored() < energy.getMaxEnergyStored());
}
Expand All @@ -111,13 +114,19 @@ public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundT

@Override
public int getBarWidth(ItemStack stack) {
if (MiningProperties.getBatteryTier(stack) == Upgrade.BATTERY_CREATIVE.getTier())
return 13;

return stack.getCapability(ForgeCapabilities.ENERGY, null)
.map(e -> Math.min(13 * e.getEnergyStored() / e.getMaxEnergyStored(), 13))
.orElse(0);
}

@Override
public int getBarColor(ItemStack stack) {
if (MiningProperties.getBatteryTier(stack) == Upgrade.BATTERY_CREATIVE.getTier())
return Mth.color(0, 1, 0);

return stack.getCapability(ForgeCapabilities.ENERGY)
.map(e -> Mth.hsvToRgb(Math.max(0.0F, (float) e.getEnergyStored() / (float) e.getMaxEnergyStored()) / 3.0F, 1.0F, 1.0F))
.orElse(super.getBarColor(stack));
Expand Down Expand Up @@ -189,6 +198,9 @@ public static void changeRange(ItemStack tool) {
}

public static boolean canMine(ItemStack tool) {
if (MiningProperties.getBatteryTier(tool) == Upgrade.BATTERY_CREATIVE.getTier())
return true;

IEnergyStorage energy = tool.getCapability(ForgeCapabilities.ENERGY, null).orElse(null);
int cost = getEnergyCost(tool);

Expand Down Expand Up @@ -503,6 +515,9 @@ else if (g > 0 && b == 1)
}

public static int getEnergyCost(ItemStack stack) {
if (MiningProperties.getBatteryTier(stack) == Upgrade.BATTERY_CREATIVE.getTier())
return 0;

int cost = Config.MININGGADGET_BASECOST.get();
List<Upgrade> upgrades = UpgradeTools.getActiveUpgrades(stack);
if (upgrades.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ModItems {
public static final RegistryObject<Item> BATTERY_1 = UPGRADE_ITEMS.register("upgrade_battery_1", () -> new UpgradeCard(Upgrade.BATTERY_1));
public static final RegistryObject<Item> BATTERY_2 = UPGRADE_ITEMS.register("upgrade_battery_2", () -> new UpgradeCard(Upgrade.BATTERY_2));
public static final RegistryObject<Item> BATTERY_3 = UPGRADE_ITEMS.register("upgrade_battery_3", () -> new UpgradeCard(Upgrade.BATTERY_3));
public static final RegistryObject<Item> BATTERY_CREATIVE = UPGRADE_ITEMS.register("upgrade_battery_creative", () -> new UpgradeCard(Upgrade.BATTERY_CREATIVE));
public static final RegistryObject<Item> EFFICIENCY_1 = UPGRADE_ITEMS.register("upgrade_efficiency_1", () -> new UpgradeCard(Upgrade.EFFICIENCY_1));
public static final RegistryObject<Item> EFFICIENCY_2 = UPGRADE_ITEMS.register("upgrade_efficiency_2", () -> new UpgradeCard(Upgrade.EFFICIENCY_2));
public static final RegistryObject<Item> EFFICIENCY_3 = UPGRADE_ITEMS.register("upgrade_efficiency_3", () -> new UpgradeCard(Upgrade.EFFICIENCY_3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private MiningProperties() {}
private static final String PRECISION_MODE = "precisionMode";
private static final String VOLUME = "volume";
private static final String FREEZE_PARTICLE_DELAY = "freeze_particle_delay";
private static final String KEY_BATTERY_TIER = "battery_tier";

public static final String KEY_FILTERS = "filters";
public static final String COLOR_RED = "colorRed";
Expand Down Expand Up @@ -220,6 +221,16 @@ public static int getFreezeDelay(ItemStack gadget) {
return !compound.contains(FREEZE_PARTICLE_DELAY) ? setFreezeDelay(gadget, 0) : compound.getInt(FREEZE_PARTICLE_DELAY);
}

public static int setBatteryTier(ItemStack gadget, int tier) {
gadget.getOrCreateTag().putInt(KEY_BATTERY_TIER, tier);
return tier;
}

public static int getBatteryTier(ItemStack gadget) {
CompoundTag compound = gadget.getOrCreateTag();
return !compound.contains(KEY_BATTERY_TIER) ? setBatteryTier(gadget, 0) : compound.getInt(KEY_BATTERY_TIER);
}

/**
* So this is a bit fun, because we only need the items in our list we're ditching half the data
* that the `Items` actually contains.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum Upgrade {
BATTERY_1("battery_1", ModItems.BATTERY_1, 1, () -> 0),
BATTERY_2("battery_2", ModItems.BATTERY_2, 2, () -> 0),
BATTERY_3("battery_3", ModItems.BATTERY_3, 3, () -> 0),
BATTERY_CREATIVE("battery_CREATIVE", ModItems.BATTERY_CREATIVE, 4, () -> 0),

RANGE_1("range_1", ModItems.RANGE_1, 1, () -> 0),
RANGE_2("range_2", ModItems.RANGE_2, 2, () -> 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum UpgradeBatteryLevels {
BATTERY(0, Config.MININGGADGET_MAXPOWER.get()),
BATTERY_1(1, Config.UPGRADECOST_BATTERY1.get()),
BATTERY_2(2, Config.UPGRADECOST_BATTERY2.get()),
BATTERY_3(3, Config.UPGRADECOST_BATTERY3.get());
BATTERY_3(3, Config.UPGRADECOST_BATTERY3.get()),
BATTERY_CREATIVE(4, Integer.MAX_VALUE);

private int level;
private int power;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ae2fe60

Please sign in to comment.