Batteries

This commit is contained in:
DBotThePony 2021-08-11 22:25:33 +07:00
parent cc05975c2f
commit 7283c9a1ae
Signed by: DBot
GPG Key ID: DCC23B5715498507
6 changed files with 285 additions and 5 deletions

View File

@ -17,10 +17,13 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import ru.dbotthepony.mc.otm.block.BlockAndroidStation;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation;
import ru.dbotthepony.mc.otm.item.ItemBattery;
import ru.dbotthepony.mc.otm.item.ItemPill;
import ru.dbotthepony.mc.otm.menu.AndroidStationMenu;
import ru.dbotthepony.mc.otm.screen.AndroidStationScreen;
import java.math.BigDecimal;
public class Registry {
public static final DamageSource DAMAGE_BECOME_ANDROID = new DamageSource("otmBecomeAndroid");
public static final DamageSource DAMAGE_BECOME_HUMANE = new DamageSource("otmBecomeHumane");
@ -36,6 +39,13 @@ public class Registry {
public static final ResourceLocation PILL_ANDROID = new ResourceLocation(OverdriveThatMatters.MOD_ID, "pill_android");
public static final ResourceLocation PILL_HUMANE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "pill_humane");
public static final ResourceLocation BATTERY_CRUDE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_crude");
public static final ResourceLocation BATTERY_BASIC = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_basic");
public static final ResourceLocation BATTERY_NORMAL = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_normal");
public static final ResourceLocation BATTERY_DENSE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_dense");
public static final ResourceLocation BATTERY_CAPACITOR = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_capacitor");
public static final ResourceLocation BATTERY_CREATIVE = new ResourceLocation(OverdriveThatMatters.MOD_ID, "battery_creative");
}
public static class Blocks {
@ -53,15 +63,34 @@ public class Registry {
public static final ItemPill PILL_ANDROID = new ItemPill(ItemPill.PillType.BECOME_ANDROID);
public static final ItemPill PILL_HUMANE = new ItemPill(ItemPill.PillType.BECOME_HUMANE);
public static final ItemBattery BATTERY_CRUDE = new ItemBattery(new BigDecimal(30_000), new BigDecimal(150), new BigDecimal(150));
public static final ItemBattery BATTERY_BASIC = new ItemBattery(new BigDecimal(60_000), new BigDecimal(300), new BigDecimal(300));
public static final ItemBattery BATTERY_NORMAL = new ItemBattery(new BigDecimal(250_000), new BigDecimal(1000), new BigDecimal(1000));
public static final ItemBattery BATTERY_DENSE = new ItemBattery(new BigDecimal(1_000_000), new BigDecimal(2000), new BigDecimal(2000));
public static final ItemBattery BATTERY_CAPACITOR = new ItemBattery(new BigDecimal(150_000), new BigDecimal(15000), new BigDecimal(15000));
public static final ItemBattery BATTERY_CREATIVE = new ItemBattery();
static {
PILL_ANDROID.setRegistryName(Names.PILL_ANDROID);
PILL_HUMANE.setRegistryName(Names.PILL_HUMANE);
BATTERY_CRUDE.setRegistryName(Names.BATTERY_CRUDE);
BATTERY_BASIC.setRegistryName(Names.BATTERY_BASIC);
BATTERY_NORMAL.setRegistryName(Names.BATTERY_NORMAL);
BATTERY_DENSE.setRegistryName(Names.BATTERY_DENSE);
BATTERY_CAPACITOR.setRegistryName(Names.BATTERY_CAPACITOR);
BATTERY_CREATIVE.setRegistryName(Names.BATTERY_CREATIVE);
}
public static void register(final RegistryEvent.Register<Item> event) {
event.getRegistry().register(ANDROID_STATION);
event.getRegistry().register(PILL_ANDROID);
event.getRegistry().register(PILL_HUMANE);
event.getRegistry().register(BATTERY_CRUDE);
event.getRegistry().register(BATTERY_BASIC);
event.getRegistry().register(BATTERY_NORMAL);
event.getRegistry().register(BATTERY_DENSE);
event.getRegistry().register(BATTERY_CAPACITOR);
event.getRegistry().register(BATTERY_CREATIVE);
// OverdriveThatMatters.LOGGER.info("Registered items");
}

View File

@ -31,7 +31,7 @@ public class BlockEntityAndroidStation extends BlockEntityMatteryPoweredMachine
public BlockEntityAndroidStation(BlockPos p_155229_, BlockState p_155230_) {
super(Registry.BlockEntities.ANDROID_STATION, p_155229_, p_155230_);
energy = new MatteryMachineEnergyStorage(MatteryMachineEnergyStorage.MachineType.WORKER, new BigDecimal(128_000), new BigDecimal(4096), new BigDecimal(256));
energy = new MatteryMachineEnergyStorage(MatteryMachineEnergyStorage.MachineType.WORKER, new BigDecimal(100_000), new BigDecimal(250), new BigDecimal(250));
energy_resolver = LazyOptional.of(() -> energy);
}

View File

@ -0,0 +1,224 @@
package ru.dbotthepony.mc.otm.item;
import net.minecraft.ChatFormatting;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.menu.FormattingHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.List;
public class ItemBattery extends Item {
public class BatteryMatteryCapability implements IMatteryEnergyStorage, ICapabilityProvider {
private final BigDecimal storage;
private final BigDecimal max_input;
private final BigDecimal max_output;
private final boolean is_creative;
private final ItemStack stack;
private BigDecimal energy() {
CompoundTag tag = stack.getOrCreateTag();
if (tag.contains("otm_energy"))
return new BigDecimal(tag.getString("otm_energy"));
return BigDecimal.ZERO;
}
private void energy(BigDecimal value) {
stack.getOrCreateTag().putString("otm_energy", value.toString());
}
BatteryMatteryCapability(ItemStack stack, BigDecimal storage, BigDecimal max_input, BigDecimal max_output) {
this.stack = stack;
is_creative = false;
this.storage = storage;
this.max_input = max_input;
this.max_output = max_output;
}
BatteryMatteryCapability(ItemStack stack) {
this.stack = stack;
is_creative = true;
storage = MatteryCapability.LONG_MAX_VALUE;
max_input = MatteryCapability.LONG_MAX_VALUE;
max_output = MatteryCapability.LONG_MAX_VALUE;
}
@Nonnull
@Override
public BigDecimal extractEnergyOuter(BigDecimal howMuch, boolean simulate) {
return extractEnergyInner(howMuch, simulate);
}
@Nonnull
@Override
public BigDecimal extractEnergyInner(BigDecimal howMuch, boolean simulate) {
if (is_creative)
return howMuch;
BigDecimal new_energy = energy().subtract(howMuch.min(max_output), MatteryCapability.ROUND_RULES).max(BigDecimal.ZERO);
BigDecimal diff = energy().subtract(new_energy);
if (!simulate) {
energy(new_energy);
}
return diff;
}
@Nonnull
@Override
public BigDecimal receiveEnergyOuter(BigDecimal howMuch, boolean simulate) {
return receiveEnergyInner(howMuch, simulate);
}
@Nonnull
@Override
public BigDecimal receiveEnergyInner(BigDecimal howMuch, boolean simulate) {
if (is_creative)
return howMuch;
BigDecimal new_energy = energy().add(howMuch.min(max_input), MatteryCapability.ROUND_RULES).min(storage);
BigDecimal diff = new_energy.subtract(energy());
if (!simulate) {
energy(new_energy);
}
return diff;
}
@Override
public BigDecimal getMissingPower() {
if (is_creative)
return MatteryCapability.LONG_MAX_VALUE;
return IMatteryEnergyStorage.super.getMissingPower();
}
@Nonnull
@Override
public BigDecimal getBatteryLevel() {
if (is_creative)
return MatteryCapability.LONG_MAX_VALUE;
return energy();
}
@Nonnull
@Override
public BigDecimal getMaxBatteryLevel() {
if (is_creative)
return MatteryCapability.LONG_MAX_VALUE;
return storage;
}
private final LazyOptional<IMatteryEnergyStorage> resolver = LazyOptional.of(() -> this);
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if (cap == MatteryCapability.ENERGY)
return resolver.cast();
return LazyOptional.empty();
}
@Override
public boolean canExtract() {
return true;
}
@Override
public boolean canReceive() {
return true;
}
@Override
public boolean canExtractOuter() {
return true;
}
@Override
public boolean canReceiveOuter() {
return true;
}
@Override
public boolean canExtractInner() {
return true;
}
@Override
public boolean canReceiveInner() {
return true;
}
}
private final boolean is_creative;
public final BigDecimal storage;
public final BigDecimal receive;
public final BigDecimal extract;
private final Component THROUGHPUT;
private static final Component INFINITE_STORAGE = new TranslatableComponent("otm.item.power.infinite.storage").withStyle(ChatFormatting.GRAY);
public ItemBattery(BigDecimal storage, BigDecimal receive, BigDecimal extract) {
super(new Properties().stacksTo(1).tab(CreativeModeTab.TAB_MISC));
is_creative = false;
this.storage = storage;
this.receive = receive;
this.extract = extract;
THROUGHPUT = new TranslatableComponent("otm.item.power.normal.throughput", FormattingHelper.formatPower(receive), FormattingHelper.formatPower(extract)).withStyle(ChatFormatting.GRAY);
}
public ItemBattery() {
super(new Properties().stacksTo(1).rarity(Rarity.EPIC).tab(CreativeModeTab.TAB_MISC));
is_creative = true;
this.storage = MatteryCapability.LONG_MAX_VALUE;
this.receive = MatteryCapability.LONG_MAX_VALUE;
this.extract = MatteryCapability.LONG_MAX_VALUE;
THROUGHPUT = new TranslatableComponent("otm.item.power.infinite.throughput").withStyle(ChatFormatting.GRAY);
}
@Override
public void appendHoverText(ItemStack stack, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
super.appendHoverText(stack, p_41422_, p_41423_, p_41424_);
if (is_creative) {
p_41423_.add(INFINITE_STORAGE);
p_41423_.add(THROUGHPUT);
} else {
if (stack.getCapability(MatteryCapability.ENERGY).resolve().get() instanceof BatteryMatteryCapability cap)
p_41423_.add(new TranslatableComponent("otm.item.power.normal.storage", FormattingHelper.formatPower(cap.getBatteryLevel()), FormattingHelper.formatPower(storage)).withStyle(ChatFormatting.GRAY));
else
p_41423_.add(new TranslatableComponent("otm.item.power.normal.storage", "???", FormattingHelper.formatPower(storage)).withStyle(ChatFormatting.GRAY));
p_41423_.add(THROUGHPUT);
}
}
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
if (is_creative)
return new BatteryMatteryCapability(stack);
return new BatteryMatteryCapability(stack, storage, receive, extract);
}
}

View File

@ -65,11 +65,15 @@ public class FormattingHelper {
return new TranslatableComponent("otm.gui.level", formatSI(power, POWER_NAME), formatSI(max_power, POWER_NAME));
}
public static FormattedText formatSI(BigDecimal value) {
public static Component formatPower(BigDecimal power) {
return formatSI(power, POWER_NAME);
}
public static Component formatSI(BigDecimal value) {
return formatSI(value, "");
}
public static FormattedText formatSI(BigDecimal value, Object end_suffix) {
public static Component formatSI(BigDecimal value, Object end_suffix) {
if (value.compareTo(BigDecimal.ZERO) == 0) {
if ("".equals(end_suffix)) {
return new TextComponent("0.00");

View File

@ -6,6 +6,10 @@ import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import java.util.Optional;
public class BatterySlot extends Slot {
public BatterySlot(Container container, int index, int x, int y) {
@ -14,7 +18,13 @@ public class BatterySlot extends Slot {
@Override
public boolean mayPlace(ItemStack stack) {
Optional<IMatteryEnergyStorage> storage = stack.getCapability(MatteryCapability.ENERGY).resolve();
if (storage.isPresent()) {
return storage.get().canExtractOuter();
}
LazyOptional<IEnergyStorage> capability = stack.getCapability(CapabilityEnergy.ENERGY);
return capability.isPresent() && capability.resolve().get().canExtract();
return capability.resolve().isPresent() && capability.resolve().get().canExtract();
}
}

View File

@ -6,6 +6,12 @@
"otm.gui.level": "%s / %s",
"otm.gui.power.name": "MtE",
"otm.item.power.infinite.storage": "Stored energy: Infinity / Infinity",
"otm.item.power.infinite.throughput": "Max I/O Infinite / Infinite",
"otm.item.power.normal.storage": "Stored energy: %s / %s",
"otm.item.power.normal.throughput": "Max I/O %s / %s",
"otm.suffix.merge": "%s %s",
"otm.suffix.kilo": "%s k%s",
@ -29,5 +35,12 @@
"container.otm.android_station": "Android station",
"block.overdrive_that_matters.android_station": "Android station",
"item.overdrive_that_matters.pill_android": "Android pill",
"item.overdrive_that_matters.pill_humane": "Humane pill"
"item.overdrive_that_matters.pill_humane": "Humane pill",
"item.overdrive_that_matters.battery_crude": "Crude Battery",
"item.overdrive_that_matters.battery_basic": "Basic Battery",
"item.overdrive_that_matters.battery_normal": "Ordinary Battery",
"item.overdrive_that_matters.battery_dense": "Dense Battery",
"item.overdrive_that_matters.battery_capacitor": "Capacitor Battery",
"item.overdrive_that_matters.battery_creative": "Creative Battery"
}