Jump to content

[1.10.2 | 1.11.2] [SOLVED] Energy Capability


Recommended Posts

Posted

Hi.

 

When changing the default EnergyStorage class, so basically implementing my own using IEnergyStorage, do I need to register a new capability for and do I need a new interface for it? Or can I work with the default capability while using my own implementation of EnergyStorage?

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Posted

Hi.

 

When changing the default EnergyStorage class, so basically implementing my own using IEnergyStorage, do I need to register a new capability for and do I need a new interface for it? Or can I work with the default capability while using my own implementation of EnergyStorage?

 

Thx in advance.

Bektor

You do not need to do anything capability related.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Hi.

 

When changing the default EnergyStorage class, so basically implementing my own using IEnergyStorage, do I need to register a new capability for and do I need a new interface for it? Or can I work with the default capability while using my own implementation of EnergyStorage?

 

Thx in advance.

Bektor

You do not need to do anything capability related.

Well, the Forge energy system stuff uses capabilities, so I want to use them, too. I'm just wondering when I'm creating my own EnergyStorage class which is actually a slightly modified version (so it's using IEnergyStorage, but offers also more methods) of EnergyStorage (but does not extend it, it implements the same interface however), if I have to create in that case a new capability and a new interface or not...........

Developer of Primeval Forest.

Posted

Hi.

 

When changing the default EnergyStorage class, so basically implementing my own using IEnergyStorage, do I need to register a new capability for and do I need a new interface for it? Or can I work with the default capability while using my own implementation of EnergyStorage?

 

Thx in advance.

Bektor

You do not need to do anything capability related.

Well, the Forge energy system stuff uses capabilities, so I want to use them, too. I'm just wondering when I'm creating my own EnergyStorage class which is actually a slightly modified version (so it's using IEnergyStorage, but offers also more methods) of EnergyStorage (but does not extend it, it implements the same interface however), if I have to create in that case a new capability and a new interface or not...........

If you are using the forge capability system and that Capability is already registered you do not need to do any capability registering stuff. You can create new implementations whenever you want and use them as long as it ultimately implements IEnergyStorage.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Hi.

 

When changing the default EnergyStorage class, so basically implementing my own using IEnergyStorage, do I need to register a new capability for and do I need a new interface for it? Or can I work with the default capability while using my own implementation of EnergyStorage?

 

Thx in advance.

Bektor

You do not need to do anything capability related.

Well, the Forge energy system stuff uses capabilities, so I want to use them, too. I'm just wondering when I'm creating my own EnergyStorage class which is actually a slightly modified version (so it's using IEnergyStorage, but offers also more methods) of EnergyStorage (but does not extend it, it implements the same interface however), if I have to create in that case a new capability and a new interface or not...........

If you are using the forge capability system and that Capability is already registered you do not need to do any capability registering stuff. You can create new implementations whenever you want and use them as long as it ultimately implements IEnergyStorage.

Ok, thx. This is working, but somehow it does not save the energy. When I reload the world it just starts by 0 again while it was at 250 when I saved the world.

 

TileEntity

 

public class TileEntitySolarPanel extends TileEntity implements ITickable {

private final BaseMPContainer container;

public TileEntitySolarPanel() {
	this.container = new BaseMPContainer();
}

@Override
public void update() {
	if(this.hasWorld()) {
		if(!this.getWorld().provider.hasNoSky() && this.getWorld().canBlockSeeSky(this.getPos().offset(EnumFacing.UP))
				&& this.getWorld().getSkylightSubtracted() == 0 && this.container.getEnergyStored() != this.container.getMaxEnergyStored())
			this.container.receiveEnergy(2, false);

		final TileEntity tileEntity = this.getWorld().getTileEntity(this.getPos().offset(EnumFacing.DOWN));

		if(tileEntity != null && !tileEntity.isInvalid()) {
			if(tileEntity.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.UP)) {
				IEnergyStorage consumer = tileEntity.getCapability(CapabilityEnergy.ENERGY, EnumFacing.UP);

				if(consumer != null)
					this.container.extractEnergy(consumer.receiveEnergy(this.container.getEnergyStored(), false), false);
			}
		}
	}
	System.out.println(this.container.getEnergyStored());
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
	return super.writeToNBT(compound);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket() {
	return super.getUpdatePacket();
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
	super.onDataPacket(net, pkt);
	this.readFromNBT(pkt.getNbtCompound());
}

@Override
@SuppressWarnings("unchecked")
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if(facing == EnumFacing.DOWN && capability == CapabilityEnergy.ENERGY)
		return (T) this.container;

	return super.getCapability(capability, facing);
}

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
	if(facing == EnumFacing.DOWN && capability == CapabilityEnergy.ENERGY)
		return true;

	return super.hasCapability(capability, facing);
}
}

 

 

Block

 

public class BlockSolarPanel extends Block implements ITileEntityProvider {

public BlockSolarPanel() {
	super(Material.IRON);
	this.isBlockContainer = true;
	this.setHardness(1.5f);
	this.setResistance(10.f);
	this.setLightOpacity(0);

	this.setCreativeTab(ModCreativeTabs.mcpowerTab);
}

@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
	return new TileEntitySolarPanel();
}
}

 

 

BaseMPContainer

 

public class BaseMPContainer implements IEnergyStorage, INBTSerializable<NBTTagCompound> {

private int stored;
private int capacity;
private int input;
private int output;

public BaseMPContainer() {
	this(250, 10, 10);
}

public BaseMPContainer(int capacity, int input, int output) {
	this(0, capacity, input, output);
}

public BaseMPContainer(int power, int capacity, int input, int output) {
	this.stored = power;
	this.capacity = capacity;
	this.input = input;
	this.output = output;
}

public BaseMPContainer(NBTTagCompound dataTag) {
	this.deserializeNBT(dataTag);
}

@Override
public NBTTagCompound serializeNBT() {
	final NBTTagCompound dataTag = new NBTTagCompound();

	dataTag.setInteger("JAEStored", this.stored);
	dataTag.setInteger("JAECapacity", this.capacity);
	dataTag.setInteger("JAEInput", this.input);
	dataTag.setInteger("JAEOutput", this.output);

	return dataTag;
}

@Override
public void deserializeNBT(NBTTagCompound nbt) {
	if(nbt.hasKey("JAEStored"))
		this.stored = nbt.getInteger("JAEStored");
	if(nbt.hasKey("JAECapacity"))
		this.capacity = nbt.getInteger("JAECapacity");
	if(nbt.hasKey("JAEInput"))
		this.input = nbt.getInteger("JAEInput");
	if(nbt.hasKey("JAEOutput"))
		this.output = nbt.getInteger("JAEOutput");

	if(this.stored > this.getMaxEnergyStored())
		this.stored = this.getMaxEnergyStored();
}

@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
	final int acceptedPower = Math.min(this.getMaxEnergyStored() - this.getEnergyStored(), Math.min(this.getMaxInput(), maxReceive));

	if(!simulate)
		this.stored += acceptedPower;

	return this.canReceive() ? acceptedPower : 0;
}

@Override
public int extractEnergy(int maxExtract, boolean simulate) {
	final int removedPower = Math.min(this.getEnergyStored(), Math.min(this.getMaxOutput(), maxExtract));

	if(!simulate)
		this.stored -= removedPower;
	return this.canExtract() ? removedPower : 0;
}

@Override
public int getEnergyStored() {
	return this.stored;
}

@Override
public int getMaxEnergyStored() {
	return this.capacity;
}

public void setMaxEnergyStored(int capacity) {
	this.capacity = capacity;

	if(this.stored > capacity)
		this.stored = capacity;
}

public int getMaxInput() {
	return this.input;
}

public void setMaxInput(int input) {
	this.input = input;
}

public int getMaxOutput() {
	return this.output;
}

public void setMaxOutput(int output) {
	this.output = output;
}

@Override
public boolean canExtract() {
	return this.getMaxOutput() > 0 && this.stored > 0;
}

@Override
public boolean canReceive() {
	return this.getMaxInput() > 0;
}
}

 

 

Any ideas? Also open for ideas to improve the code. ;)

Developer of Primeval Forest.

Posted

You dont read and write it in your TE.

Oh yeah, totally forgot and overlooked that. So I basically have to put the container stuff in there like that?:

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	this.container.deserializeNBT(compound);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
	this.container.serializeNBT();
	return super.writeToNBT(compound);
}

Developer of Primeval Forest.

Posted

yes

Ok, I changed it and it does still not work.

	@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	//this.container.setEnergyStored(compound.getInteger("StoredJAE"));
	this.container.deserializeNBT(compound);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
	//compound.setInteger("StoredJAE", this.container.getEnergyStored());
	compound.setTag("StoredJAE", this.container.serializeNBT());
	return super.writeToNBT(compound);
}

I also tried it with the setInteger thing, does still not work. Oh and deserializeNBT needs an NBTTAgCompound, so getTag does not work.

Developer of Primeval Forest.

Posted

this does not work?

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        container.deserializeNBT(compound.getCompoundTag("StoredJAE"));
    }

 

what do you meen by

Oh and deserializeNBT needs an NBTTAgCompound, so getTag does not work.
Posted

this does not work?

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        container.deserializeNBT(compound.getCompoundTag("StoredJAE"));
    }

 

what do you meen by

Oh and deserializeNBT needs an NBTTAgCompound, so getTag does not work.

Yeah, overread the getCompoundTag. But even when using getCompoundTag, it gives me no errors anymore, but seems not to be able to save and load the data....

 

I'm also wondering from where this comes:

 

 

224

138

226

140

228

142

230

144

232

146

234

148

236

150

238

152

240

242

154

244

156

246

158

248

160

250

162

250

164

250

166

250

168

250

170

250

172

250

250

174

250

176

250

178

250

180

250

182

250

184

250

186

250

188

250

190

250

192

250

194

250

250

196

250

198

250

200

250

202

250

204

250

206

250

208

250

210

250

212

250

214

250

216

250

218

250

220

250

250

222

250

224

250

226

250

228

250

230

250

232

250

234

250

236

250

238

250

240

250

242

250

244

250

246

250

248

250

250

250

 

 

As you can see, the energy reaches 250, goes down again, and then up again etc. ... But I'm not removing energy from the block!

 

@Override
public void update() {
	if(this.hasWorld() && !this.world.isRemote) {
		if(!this.getWorld().provider.hasNoSky() && this.getWorld().canBlockSeeSky(this.getPos().offset(EnumFacing.UP))
				&& this.getWorld().getSkylightSubtracted() == 0 && this.container.getEnergyStored() != this.container.getMaxEnergyStored())
			this.container.receiveEnergy(2, false);

		final TileEntity tileEntity = this.getWorld().getTileEntity(this.getPos().offset(EnumFacing.DOWN));

		if(tileEntity != null && !tileEntity.isInvalid()) {
			if(tileEntity.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.UP)) {
				IEnergyStorage consumer = tileEntity.getCapability(CapabilityEnergy.ENERGY, EnumFacing.UP);

				if(consumer != null)
					this.container.extractEnergy(consumer.receiveEnergy(this.container.getEnergyStored(), false), false);
			}
		}
		System.out.println(this.container.getEnergyStored());
	}
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	//this.container.setEnergyStored(compound.getInteger("StoredJAE"));
	this.container.deserializeNBT(compound.getCompoundTag("StoredJAE"));
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
	//compound.setInteger("StoredJAE", this.container.getEnergyStored());
	compound.setTag("StoredJAE", this.container.serializeNBT());
	return super.writeToNBT(compound);
}

To be sure it's not a problem with isRemote stuff, I just added a little check for this, but it's still counting the energy somehow twice.

Developer of Primeval Forest.

Posted

put breakpoints on it and see what it does

Ok, after setting a breakpoint in those two lines:

			this.container.receiveEnergy(2, false); (TileEntity)
	return this.canReceive() ? acceptedPower : 0; (BaseMPcontainer)

So, basically, I found now out that it seems to count perfectly until the number 10 was reached. At this point it starts for an unknown reason again by 2 and then 12 and then 4 and then 14 and then 6 and then 16 .....

I think it's clear what I mean. Oh and directly before the number 10 was reached the world has been fully loaded.

Developer of Primeval Forest.

Posted

Ok, I changed it and it does still not work.

	@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	//this.container.setEnergyStored(compound.getInteger("StoredJAE"));
	this.container.deserializeNBT(compound);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
	//compound.setInteger("StoredJAE", this.container.getEnergyStored());
	compound.setTag("StoredJAE", this.container.serializeNBT());
	return super.writeToNBT(compound);
}

I also tried it with the setInteger thing, does still not work. Oh and deserializeNBT needs an NBTTAgCompound, so getTag does not work.

 

Of course it doesn't work. In writeToNBT, you store the NBTTagCompound created by

this.container.serializeNBT()

in the root TileEntity tag but in the readFromNBT method, you try to deserialize it from the root tag instead of the sub tag.

 

You need to change the line in readFromNBT to:

 

this.container.deserializeNBT(compound.getCompoundTag("StoredJAE"));

Don't make mods if you don't know Java.

Check out my website: http://shadowfacts.net

Developer of many mods

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.