mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-26 00:38:35 +01:00
energy things...
This commit is contained in:
parent
768b677e41
commit
87b1e12892
6 changed files with 65 additions and 97 deletions
|
@ -18,6 +18,7 @@ import net.minecraft.client.Minecraft;
|
|||
import net.minecraft.client.gui.AbstractGui;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.fml.client.gui.GuiUtils;
|
||||
|
@ -100,6 +101,9 @@ public class EnergyDisplay extends AbstractGui {
|
|||
|
||||
private String getOverlayText() {
|
||||
NumberFormat format = NumberFormat.getInstance();
|
||||
return String.format("%s/%s %s", format.format(this.rfReference.getEnergyStored()), format.format(this.rfReference.getMaxEnergyStored()), I18n.get("misc.actuallyadditions.energy_name"));
|
||||
return new TranslationTextComponent("misc.actuallyadditions.power_long",
|
||||
format.format(this.rfReference.getEnergyStored()),
|
||||
format.format(this.rfReference.getMaxEnergyStored()))
|
||||
.getString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,18 +15,11 @@ import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
|||
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
||||
import de.ellpeck.actuallyadditions.mod.blocks.AABlockItem;
|
||||
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
||||
import de.ellpeck.actuallyadditions.mod.items.base.ItemArmorAA;
|
||||
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
|
||||
import de.ellpeck.actuallyadditions.mod.items.base.ItemHoeAA;
|
||||
import de.ellpeck.actuallyadditions.mod.items.base.ItemSwordAA;
|
||||
import de.ellpeck.actuallyadditions.mod.items.lens.ItemLens;
|
||||
import de.ellpeck.actuallyadditions.mod.material.ArmorMaterials;
|
||||
import de.ellpeck.actuallyadditions.mod.material.ToolMaterials;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.ItemTier;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
|
@ -120,7 +113,7 @@ public final class ActuallyItems {
|
|||
public static final RegistryObject<Item> ITEM_BOOKLET = ITEMS.register("booklet", ItemBooklet::new);
|
||||
public static final RegistryObject<Item> RING_OF_GROWTH = ITEMS.register("ring_of_growth", ItemGrowthRing::new);
|
||||
public static final RegistryObject<Item> RING_OF_MAGNETIZING = ITEMS.register("ring_of_magnetizing", ItemMagnetRing::new);
|
||||
public static final RegistryObject<Item> TELEPORT_STAFF = ITEMS.register("teleport_staff", ItemTeleStaff::new);
|
||||
public static final RegistryObject<Item> TELEPORT_STAFF = ITEMS.register("teleport_staff", ItemTeleportStaff::new);
|
||||
public static final RegistryObject<Item> WINGS_OF_THE_BATS = ITEMS.register("wings_of_the_bats", ItemWingsOfTheBats::new);
|
||||
|
||||
public static final RegistryObject<Item> DRILL_MAIN = ITEMS.register("drill_light_blue", DrillItem::new);
|
||||
|
|
|
@ -23,9 +23,9 @@ import net.minecraft.util.math.vector.Vector3d;
|
|||
import net.minecraft.util.math.vector.Vector3i;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class ItemTeleStaff extends ItemEnergy {
|
||||
public class ItemTeleportStaff extends ItemEnergy {
|
||||
|
||||
public ItemTeleStaff() {
|
||||
public ItemTeleportStaff() {
|
||||
super(250000, 1000);
|
||||
}
|
||||
|
|
@ -11,24 +11,21 @@
|
|||
package de.ellpeck.actuallyadditions.mod.items.base;
|
||||
|
||||
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
|
||||
import de.ellpeck.actuallyadditions.mod.proxy.ClientProxy;
|
||||
import de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
|
||||
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
||||
import de.ellpeck.actuallyadditions.mod.util.Lang;
|
||||
import net.minecraft.client.util.ITooltipFlag;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
@ -37,6 +34,7 @@ import javax.annotation.Nonnull;
|
|||
import javax.annotation.Nullable;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public abstract class ItemEnergy extends ItemBase {
|
||||
|
||||
|
@ -54,27 +52,16 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
this.transfer = transfer;
|
||||
}
|
||||
|
||||
// TODO: [port] make sure this is right
|
||||
@Nullable
|
||||
@Override
|
||||
public CompoundNBT getShareTag(ItemStack stack) {
|
||||
return new CompoundNBT();
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public boolean getShareTag() {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
@Override
|
||||
public void appendHoverText(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
|
||||
super.appendHoverText(stack, worldIn, tooltip, flagIn);
|
||||
stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(storage -> {
|
||||
int energy = 0;
|
||||
if (stack.hasTag() && stack.getTag().contains("Energy")) {
|
||||
energy = stack.getTag().getInt("Energy");
|
||||
}
|
||||
NumberFormat format = NumberFormat.getInstance();
|
||||
tooltip.add(Lang.trans("misc", "power_long", format.format(storage.getEnergyStored()), format.format(storage.getMaxEnergyStored())));
|
||||
});
|
||||
tooltip.add(new TranslationTextComponent("misc.actuallyadditions.power_long", format.format(energy), format.format(this.maxPower)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -101,18 +88,14 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
|
||||
@Override
|
||||
public double getDurabilityForDisplay(ItemStack stack) {
|
||||
return stack.getCapability(CapabilityEnergy.ENERGY, null)
|
||||
.map(cap -> {
|
||||
double maxAmount = cap.getMaxEnergyStored();
|
||||
double energyDif = maxAmount - cap.getEnergyStored();
|
||||
return energyDif / maxAmount;
|
||||
})
|
||||
.orElse(super.getDurabilityForDisplay(stack));
|
||||
if (stack.hasTag() && stack.getTag().contains("Energy")) {
|
||||
return 1 - (stack.getTag().getDouble("Energy") / this.maxPower);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
||||
//float[] color = MathHelper.hsvToRgb(1, 1, 1);
|
||||
//float[] color = AssetUtil.getWheelColor(player.level.getGameTime() % 256);
|
||||
//return MathHelper.color(color[0] / 255F, color[1] / 255F, color[2] / 255F);
|
||||
return super.getRGBDurabilityForDisplay(stack);
|
||||
|
@ -128,13 +111,9 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
|
||||
@Deprecated
|
||||
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) {
|
||||
// if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
||||
// IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null);
|
||||
// if (storage instanceof CustomEnergyStorage) {
|
||||
// ((CustomEnergyStorage) storage).receiveEnergyInternal(maxReceive, simulate);
|
||||
// }
|
||||
// }
|
||||
return 0;
|
||||
return stack.getCapability(CapabilityEnergy.ENERGY)
|
||||
.map(cap -> ((CustomEnergyStorage) cap).receiveEnergyInternal(maxReceive, simulate))
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) {
|
||||
|
@ -147,13 +126,9 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
|
||||
@Deprecated
|
||||
public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) {
|
||||
// if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
||||
// IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null);
|
||||
// if (storage != null) {
|
||||
// return storage.receiveEnergy(maxReceive, simulate);
|
||||
// }
|
||||
// }
|
||||
return 0;
|
||||
return stack.getCapability(CapabilityEnergy.ENERGY)
|
||||
.map(cap -> cap.receiveEnergy(maxReceive, simulate))
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
|
||||
|
@ -179,24 +154,17 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
return new EnergyCapabilityProvider(stack, this);
|
||||
}
|
||||
|
||||
private static class EnergyCapabilityProvider implements ICapabilityProvider {
|
||||
private static class EnergyCapabilityProvider implements ICapabilitySerializable<CompoundNBT> {
|
||||
|
||||
public final CustomEnergyStorage storage;
|
||||
private final LazyOptional<CustomEnergyStorage> energyCapability;
|
||||
|
||||
public EnergyCapabilityProvider(ItemStack stack, ItemEnergy item) {
|
||||
this.storage = new CustomEnergyStorage(item.maxPower, item.transfer, item.transfer) {
|
||||
@Override
|
||||
public int getEnergyStored() {
|
||||
return stack.getOrCreateTag().getInt("Energy");
|
||||
}
|
||||
private final ItemStack stack;
|
||||
|
||||
@Override
|
||||
public void setEnergyStored(int energy) {
|
||||
stack.getOrCreateTag().putInt("Energy", energy);
|
||||
}
|
||||
};
|
||||
public EnergyCapabilityProvider(ItemStack stack, ItemEnergy item) {
|
||||
this.storage = new CustomEnergyStorage(item.maxPower, item.transfer, item.transfer);
|
||||
this.energyCapability = LazyOptional.of(() -> this.storage);
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@ -207,5 +175,19 @@ public abstract class ItemEnergy extends ItemBase {
|
|||
}
|
||||
return LazyOptional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundNBT serializeNBT() {
|
||||
if (this.storage.isDirty())
|
||||
stack.getOrCreateTag().putInt("Energy", this.storage.getEnergyStored());
|
||||
this.storage.clearDirty();
|
||||
return new CompoundNBT();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeNBT(CompoundNBT nbt) {
|
||||
if (stack.getOrCreateTag().contains("Energy"))
|
||||
this.storage.setEnergyStored(stack.getOrCreateTag().getInt("Energy"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,11 +14,24 @@ import net.minecraft.nbt.CompoundNBT;
|
|||
import net.minecraftforge.energy.EnergyStorage;
|
||||
|
||||
public class CustomEnergyStorage extends EnergyStorage {
|
||||
boolean dirty = false;
|
||||
|
||||
public CustomEnergyStorage(int capacity, int maxReceive, int maxExtract) {
|
||||
super(capacity, maxReceive, maxExtract);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||
dirty = true;
|
||||
return super.receiveEnergy(maxReceive, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||
dirty = true;
|
||||
return super.extractEnergy(maxExtract, simulate);
|
||||
}
|
||||
|
||||
public int extractEnergyInternal(int maxExtract, boolean simulate) {
|
||||
int before = this.maxExtract;
|
||||
this.maxExtract = Integer.MAX_VALUE;
|
||||
|
@ -39,37 +52,12 @@ public class CustomEnergyStorage extends EnergyStorage {
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
public void addEnergyRaw(int energy) {
|
||||
this.energy = Math.min(this.energy + energy, this.capacity);
|
||||
public boolean isDirty() {
|
||||
return dirty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||
if (!this.canReceive()) {
|
||||
return 0;
|
||||
}
|
||||
int energy = this.getEnergyStored();
|
||||
|
||||
int energyReceived = Math.min(this.capacity - energy, Math.min(this.maxReceive, maxReceive));
|
||||
if (!simulate) {
|
||||
this.setEnergyStored(energy + energyReceived);
|
||||
}
|
||||
|
||||
return energyReceived;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||
if (!this.canExtract()) {
|
||||
return 0;
|
||||
}
|
||||
int energy = this.getEnergyStored();
|
||||
|
||||
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
||||
if (!simulate) {
|
||||
this.setEnergyStored(energy - energyExtracted);
|
||||
}
|
||||
return energyExtracted;
|
||||
public void clearDirty() {
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
public void readFromNBT(CompoundNBT compound) {
|
||||
|
@ -82,5 +70,6 @@ public class CustomEnergyStorage extends EnergyStorage {
|
|||
|
||||
public void setEnergyStored(int energy) {
|
||||
this.energy = energy;
|
||||
this.dirty = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements
|
|||
this.currentBurnTime--;
|
||||
int produce = currentRecipe.getTotalEnergy() / currentRecipe.getBurnTime();
|
||||
if (produce > 0) {
|
||||
this.storage.addEnergyRaw(produce);
|
||||
this.storage.receiveEnergyInternal(produce, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue