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

[1.18] Backport 871428a (Implement a creative battery upgrade) #239

Draft
wants to merge 2 commits into
base: mc/1.18
Choose a base branch
from
Draft
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 @@ -4,9 +4,9 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
Expand All @@ -25,6 +25,6 @@ public CapabilityEnergyProvider(ItemStack stack, int energyCapacity) {
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return cap == CapabilityEnergy.ENERGY ? capability.cast() : LazyOptional.empty();
return cap == ForgeCapabilities.ENERGY ? capability.cast() : LazyOptional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.common.capabilities.ForgeCapabilities;

import java.util.List;

Expand Down Expand Up @@ -51,8 +51,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(CapabilityEnergy.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 @@ -91,8 +97,10 @@ public static void extractButton(ModificationTableContainer container, ServerPla
MiningProperties.setBeamMaxRange(laser, UpgradeTools.getMaxBeamRange(0));
}

if (upgrade.getBaseName().equals(Upgrade.BATTERY_1.getBaseName()))
laser.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> ((EnergisedItem) e).updatedMaxEnergy(UpgradeBatteryLevels.BATTERY.getPower()));
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 @@ -45,6 +45,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 @@ -54,8 +54,8 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.world.BlockEvent;

Expand Down Expand Up @@ -88,7 +88,10 @@ public int getMaxDamage(ItemStack stack) {

@Override
public boolean isBarVisible(ItemStack stack) {
IEnergyStorage energy = stack.getCapability(CapabilityEnergy.ENERGY, null).orElse(null);
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 @@ -100,14 +103,20 @@ public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundT

@Override
public int getBarWidth(ItemStack stack) {
return stack.getCapability(CapabilityEnergy.ENERGY, null)
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) {
return stack.getCapability(CapabilityEnergy.ENERGY)
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 @@ -141,7 +150,7 @@ public void appendHoverText(ItemStack stack, @Nullable Level world, List<Compone
}
}

stack.getCapability(CapabilityEnergy.ENERGY, null)
stack.getCapability(ForgeCapabilities.ENERGY, null)
.ifPresent(energy -> {
TranslatableComponent energyText = !sneakPressed
? new TranslatableComponent("mininggadgets.gadget.energy", MagicHelpers.tidyValue(energy.getEnergyStored()), MagicHelpers.tidyValue(energy.getMaxEnergyStored()))
Expand Down Expand Up @@ -169,7 +178,10 @@ public static void changeRange(ItemStack tool) {
}

public static boolean canMine(ItemStack tool) {
IEnergyStorage energy = tool.getCapability(CapabilityEnergy.ENERGY, null).orElse(null);
if (MiningProperties.getBatteryTier(tool) == Upgrade.BATTERY_CREATIVE.getTier())
return true;

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

if (MiningProperties.getRange(tool) == 3)
Expand Down Expand Up @@ -443,7 +455,7 @@ else if (g > 0 && b == 1)
else*/
durability = durability - 1;
if (durability <= 0) {
stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> e.receiveEnergy(getEnergyCost(stack) * -1, false));
stack.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> e.receiveEnergy(getEnergyCost(stack) * -1, false));
if (MiningProperties.getPrecisionMode(stack)) {
MiningProperties.setCanMine(stack, false);
player.stopUsingItem();
Expand Down Expand Up @@ -472,16 +484,19 @@ else if (g > 0 && b == 1)
pos = lookingAt.getBlockPos().relative(side).relative(right);

if (world.getMaxLocalRawBrightness(pos) <= 7 && world.getBlockState(pos).getMaterial() == Material.AIR) {
int energy = stack.getCapability(CapabilityEnergy.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0);
int energy = stack.getCapability(ForgeCapabilities.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0);
if (energy > Config.UPGRADECOST_LIGHT.get()) {
world.setBlockAndUpdate(pos, ModBlocks.MINERS_LIGHT.get().defaultBlockState());
stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> e.receiveEnergy((Config.UPGRADECOST_LIGHT.get() * -1), false));
stack.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> e.receiveEnergy((Config.UPGRADECOST_LIGHT.get() * -1), false));
}
}
}
}

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 @@ -46,6 +46,7 @@ public class ModItems {
public static final RegistryObject<Item> BATTERY_1 = UPGRADE_ITEMS.register("upgrade_battery_1", () -> Upgrade.BATTERY_1.getCard());
public static final RegistryObject<Item> BATTERY_2 = UPGRADE_ITEMS.register("upgrade_battery_2", () -> Upgrade.BATTERY_2.getCard());
public static final RegistryObject<Item> BATTERY_3 = UPGRADE_ITEMS.register("upgrade_battery_3", () -> Upgrade.BATTERY_3.getCard());
public static final RegistryObject<Item> BATTERY_CREATIVE = UPGRADE_ITEMS.register("upgrade_battery_creative", () -> Upgrade.BATTERY_CREATIVE.getCard());
public static final RegistryObject<Item> EFFICIENCY_1 = UPGRADE_ITEMS.register("upgrade_efficiency_1", () -> Upgrade.EFFICIENCY_1.getCard());
public static final RegistryObject<Item> EFFICIENCY_2 = UPGRADE_ITEMS.register("upgrade_efficiency_2", () -> Upgrade.EFFICIENCY_2.getCard());
public static final RegistryObject<Item> EFFICIENCY_3 = UPGRADE_ITEMS.register("upgrade_efficiency_3", () -> Upgrade.EFFICIENCY_3.getCard());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 @@ -169,6 +170,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 @@ -31,7 +31,7 @@ public enum Upgrade {
THREE_BY_THREE("three_by_three", () -> 0, false),
LIGHT_PLACER("light_placer", () -> 0), // applied at operation based on config. this isn't ideal
// PAVER("paver", () -> 10),

// Tiered
FORTUNE_1("fortune_1", 1, () -> Config.UPGRADECOST_FORTUNE1.get(), true),
FORTUNE_2("fortune_2", 2, () -> Config.UPGRADECOST_FORTUNE2.get(), true),
Expand All @@ -40,6 +40,7 @@ public enum Upgrade {
BATTERY_1("battery_1", 1, () -> 0),
BATTERY_2("battery_2", 2, () -> 0),
BATTERY_3("battery_3", 3, () -> 0),
BATTERY_CREATIVE("battery_CREATIVE", 4, () -> 0),

RANGE_1("range_1", 1, () -> 0),
RANGE_2("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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import net.minecraft.world.level.material.Fluids;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;
Expand Down Expand Up @@ -195,7 +195,7 @@ public void setDurability(int dur, ItemStack stack) {

private void freeze(ItemStack stack) {
int freezeCost = Config.UPGRADECOST_FREEZE.get() * -1;
int energy = stack.getCapability(CapabilityEnergy.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0);
int energy = stack.getCapability(ForgeCapabilities.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0);

if (energy == 0) {
return;
Expand All @@ -220,7 +220,7 @@ private int replaceBlockWithAlternative(Level world, BlockPos pos, BlockState st
return 0;
}

stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> e.receiveEnergy(costOfOperation, false));
stack.getCapability(ForgeCapabilities.ENERGY).ifPresent(e -> e.receiveEnergy(costOfOperation, false));

// If the block is just water logged, remove the fluid
BlockState blockState = world.getBlockState(pos);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.