package info.u_team.u_team_core.energy;

import info.u_team.u_team_core.api.sync.DataHolder;
import net.minecraftforge.energy.EnergyStorage;

public class BasicEnergyStorage extends EnergyStorage {
	
	public BasicEnergyStorage(int capacity) {
		this(capacity, capacity, capacity, 0);
	}
	
	public BasicEnergyStorage(int capacity, int maxTransfer) {
		this(capacity, maxTransfer, maxTransfer, 0);
	}
	
	public BasicEnergyStorage(int capacity, int maxReceive, int maxExtract) {
		this(capacity, maxReceive, maxExtract, 0);
	}
	
	public BasicEnergyStorage(int capacity, int maxReceive, int maxExtract, int energy) {
		super(capacity, maxReceive, maxExtract, energy);
	}
	
	@Override
	public int extractEnergy(int maxExtract, boolean simulate) {
		final var value = super.extractEnergy(maxExtract, simulate);
		if (!simulate) {
			onEnergyChanged();
		}
		return value;
	}
	
	@Override
	public int receiveEnergy(int maxReceive, boolean simulate) {
		final var value = super.receiveEnergy(maxReceive, simulate);
		if (!simulate) {
			onEnergyChanged();
		}
		return value;
	}
	
	public int getEnergy() {
		return super.getEnergyStored();
	}
	
	public void setEnergy(int energy) {
		this.energy = energy;
		onEnergyChanged();
	}
	
	public void addEnergy(int energy) {
		this.energy += energy;
		if (this.energy > getMaxEnergyStored()) {
			this.energy = getMaxEnergyStored();
		} else if (this.energy < 0) {
			this.energy = 0;
		}
		onEnergyChanged();
	}
	
	public void removeEnergy(int energy) {
		addEnergy(-energy);
	}
	
	public void onEnergyChanged() {
	}
	
	public DataHolder createSyncHandler() {
		return DataHolder.createIntHolder(this::getEnergy, this::setEnergy);
	}
	
}

@@ -86,16 +86,84 @@
 Holder;%0A
+import net.minecraft.nbt.CompoundTag;%0Aimport net.minecraft.nbt.Tag;%0A
 import n
@@ -1614,24 +1614,378 @@
 ed() %7B%0A%09%7D%0A%09%0A
+%09@Override%0A%09public CompoundTag serializeNBT() %7B%0A%09%09final var compound = new CompoundTag();%0A%09%09compound.putInt(%22energy%22, getEnergyStored());%0A%09%09return compound;%0A%09%7D%0A%09%0A%09@Override%0A%09public void deserializeNBT(Tag tag) %7B%0A%09%09if (tag instanceof CompoundTag compoundTag) %7B%0A%09%09%09setEnergy(compoundTag.getInt(%22energy%22));%0A%09%09%7D else %7B%0A%09%09%09super.deserializeNBT(tag);%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09public Data

Add methods for serializing again, as it breaks some assumptions. Its easier to have an extra compound tag