mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-22 15:18:34 +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.gui.AbstractGui;
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
import net.minecraft.util.text.StringTextComponent;
|
import net.minecraft.util.text.StringTextComponent;
|
||||||
|
import net.minecraft.util.text.TranslationTextComponent;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.fml.client.gui.GuiUtils;
|
import net.minecraftforge.fml.client.gui.GuiUtils;
|
||||||
|
@ -100,6 +101,9 @@ public class EnergyDisplay extends AbstractGui {
|
||||||
|
|
||||||
private String getOverlayText() {
|
private String getOverlayText() {
|
||||||
NumberFormat format = NumberFormat.getInstance();
|
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.ActuallyAdditions;
|
||||||
import de.ellpeck.actuallyadditions.mod.blocks.AABlockItem;
|
import de.ellpeck.actuallyadditions.mod.blocks.AABlockItem;
|
||||||
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
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.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.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.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.ItemTier;
|
import net.minecraft.item.ItemTier;
|
||||||
import net.minecraft.item.Items;
|
|
||||||
import net.minecraftforge.eventbus.api.IEventBus;
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
import net.minecraftforge.fml.RegistryObject;
|
import net.minecraftforge.fml.RegistryObject;
|
||||||
import net.minecraftforge.registries.DeferredRegister;
|
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> 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_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> 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> 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);
|
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.util.math.vector.Vector3i;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class ItemTeleStaff extends ItemEnergy {
|
public class ItemTeleportStaff extends ItemEnergy {
|
||||||
|
|
||||||
public ItemTeleStaff() {
|
public ItemTeleportStaff() {
|
||||||
super(250000, 1000);
|
super(250000, 1000);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,24 +11,21 @@
|
||||||
package de.ellpeck.actuallyadditions.mod.items.base;
|
package de.ellpeck.actuallyadditions.mod.items.base;
|
||||||
|
|
||||||
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
|
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.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.client.util.ITooltipFlag;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
|
||||||
import net.minecraft.item.ItemGroup;
|
import net.minecraft.item.ItemGroup;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.CompoundNBT;
|
import net.minecraft.nbt.CompoundNBT;
|
||||||
import net.minecraft.util.Direction;
|
import net.minecraft.util.Direction;
|
||||||
import net.minecraft.util.NonNullList;
|
import net.minecraft.util.NonNullList;
|
||||||
import net.minecraft.util.math.MathHelper;
|
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
|
import net.minecraft.util.text.TranslationTextComponent;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.common.capabilities.Capability;
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||||
|
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
|
||||||
import net.minecraftforge.common.util.LazyOptional;
|
import net.minecraftforge.common.util.LazyOptional;
|
||||||
import net.minecraftforge.energy.CapabilityEnergy;
|
import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
import net.minecraftforge.energy.IEnergyStorage;
|
import net.minecraftforge.energy.IEnergyStorage;
|
||||||
|
@ -37,6 +34,7 @@ import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public abstract class ItemEnergy extends ItemBase {
|
public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
|
@ -54,27 +52,16 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
this.transfer = transfer;
|
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)
|
@OnlyIn(Dist.CLIENT)
|
||||||
@Override
|
@Override
|
||||||
public void appendHoverText(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
|
public void appendHoverText(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
|
||||||
super.appendHoverText(stack, worldIn, tooltip, flagIn);
|
super.appendHoverText(stack, worldIn, tooltip, flagIn);
|
||||||
stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(storage -> {
|
int energy = 0;
|
||||||
NumberFormat format = NumberFormat.getInstance();
|
if (stack.hasTag() && stack.getTag().contains("Energy")) {
|
||||||
tooltip.add(Lang.trans("misc", "power_long", format.format(storage.getEnergyStored()), format.format(storage.getMaxEnergyStored())));
|
energy = stack.getTag().getInt("Energy");
|
||||||
});
|
}
|
||||||
|
NumberFormat format = NumberFormat.getInstance();
|
||||||
|
tooltip.add(new TranslationTextComponent("misc.actuallyadditions.power_long", format.format(energy), format.format(this.maxPower)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,18 +88,14 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getDurabilityForDisplay(ItemStack stack) {
|
public double getDurabilityForDisplay(ItemStack stack) {
|
||||||
return stack.getCapability(CapabilityEnergy.ENERGY, null)
|
if (stack.hasTag() && stack.getTag().contains("Energy")) {
|
||||||
.map(cap -> {
|
return 1 - (stack.getTag().getDouble("Energy") / this.maxPower);
|
||||||
double maxAmount = cap.getMaxEnergyStored();
|
}
|
||||||
double energyDif = maxAmount - cap.getEnergyStored();
|
return 1;
|
||||||
return energyDif / maxAmount;
|
|
||||||
})
|
|
||||||
.orElse(super.getDurabilityForDisplay(stack));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
||||||
//float[] color = MathHelper.hsvToRgb(1, 1, 1);
|
|
||||||
//float[] color = AssetUtil.getWheelColor(player.level.getGameTime() % 256);
|
//float[] color = AssetUtil.getWheelColor(player.level.getGameTime() % 256);
|
||||||
//return MathHelper.color(color[0] / 255F, color[1] / 255F, color[2] / 255F);
|
//return MathHelper.color(color[0] / 255F, color[1] / 255F, color[2] / 255F);
|
||||||
return super.getRGBDurabilityForDisplay(stack);
|
return super.getRGBDurabilityForDisplay(stack);
|
||||||
|
@ -128,13 +111,9 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) {
|
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) {
|
||||||
// if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
return stack.getCapability(CapabilityEnergy.ENERGY)
|
||||||
// IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null);
|
.map(cap -> ((CustomEnergyStorage) cap).receiveEnergyInternal(maxReceive, simulate))
|
||||||
// if (storage instanceof CustomEnergyStorage) {
|
.orElse(0);
|
||||||
// ((CustomEnergyStorage) storage).receiveEnergyInternal(maxReceive, simulate);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) {
|
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) {
|
||||||
|
@ -147,13 +126,9 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) {
|
public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) {
|
||||||
// if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
return stack.getCapability(CapabilityEnergy.ENERGY)
|
||||||
// IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null);
|
.map(cap -> cap.receiveEnergy(maxReceive, simulate))
|
||||||
// if (storage != null) {
|
.orElse(0);
|
||||||
// return storage.receiveEnergy(maxReceive, simulate);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
|
public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
|
||||||
|
@ -179,24 +154,17 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
return new EnergyCapabilityProvider(stack, this);
|
return new EnergyCapabilityProvider(stack, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class EnergyCapabilityProvider implements ICapabilityProvider {
|
private static class EnergyCapabilityProvider implements ICapabilitySerializable<CompoundNBT> {
|
||||||
|
|
||||||
public final CustomEnergyStorage storage;
|
public final CustomEnergyStorage storage;
|
||||||
private final LazyOptional<CustomEnergyStorage> energyCapability;
|
private final LazyOptional<CustomEnergyStorage> energyCapability;
|
||||||
|
|
||||||
public EnergyCapabilityProvider(ItemStack stack, ItemEnergy item) {
|
private final ItemStack stack;
|
||||||
this.storage = new CustomEnergyStorage(item.maxPower, item.transfer, item.transfer) {
|
|
||||||
@Override
|
|
||||||
public int getEnergyStored() {
|
|
||||||
return stack.getOrCreateTag().getInt("Energy");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public EnergyCapabilityProvider(ItemStack stack, ItemEnergy item) {
|
||||||
public void setEnergyStored(int energy) {
|
this.storage = new CustomEnergyStorage(item.maxPower, item.transfer, item.transfer);
|
||||||
stack.getOrCreateTag().putInt("Energy", energy);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.energyCapability = LazyOptional.of(() -> this.storage);
|
this.energyCapability = LazyOptional.of(() -> this.storage);
|
||||||
|
this.stack = stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -207,5 +175,19 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
}
|
}
|
||||||
return LazyOptional.empty();
|
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;
|
import net.minecraftforge.energy.EnergyStorage;
|
||||||
|
|
||||||
public class CustomEnergyStorage extends EnergyStorage {
|
public class CustomEnergyStorage extends EnergyStorage {
|
||||||
|
boolean dirty = false;
|
||||||
|
|
||||||
public CustomEnergyStorage(int capacity, int maxReceive, int maxExtract) {
|
public CustomEnergyStorage(int capacity, int maxReceive, int maxExtract) {
|
||||||
super(capacity, maxReceive, 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) {
|
public int extractEnergyInternal(int maxExtract, boolean simulate) {
|
||||||
int before = this.maxExtract;
|
int before = this.maxExtract;
|
||||||
this.maxExtract = Integer.MAX_VALUE;
|
this.maxExtract = Integer.MAX_VALUE;
|
||||||
|
@ -39,37 +52,12 @@ public class CustomEnergyStorage extends EnergyStorage {
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEnergyRaw(int energy) {
|
public boolean isDirty() {
|
||||||
this.energy = Math.min(this.energy + energy, this.capacity);
|
return dirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void clearDirty() {
|
||||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
dirty = false;
|
||||||
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 readFromNBT(CompoundNBT compound) {
|
public void readFromNBT(CompoundNBT compound) {
|
||||||
|
@ -82,5 +70,6 @@ public class CustomEnergyStorage extends EnergyStorage {
|
||||||
|
|
||||||
public void setEnergyStored(int energy) {
|
public void setEnergyStored(int energy) {
|
||||||
this.energy = energy;
|
this.energy = energy;
|
||||||
|
this.dirty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements
|
||||||
this.currentBurnTime--;
|
this.currentBurnTime--;
|
||||||
int produce = currentRecipe.getTotalEnergy() / currentRecipe.getBurnTime();
|
int produce = currentRecipe.getTotalEnergy() / currentRecipe.getBurnTime();
|
||||||
if (produce > 0) {
|
if (produce > 0) {
|
||||||
this.storage.addEnergyRaw(produce);
|
this.storage.receiveEnergyInternal(produce, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue