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
  On 1/4/2017 at 1:02 PM, Bektor said:

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
  On 1/4/2017 at 1:17 PM, Animefan8888 said:

  Quote

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
  On 1/4/2017 at 1:24 PM, Bektor said:

  Quote

  Quote

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
  On 1/4/2017 at 2:09 PM, Animefan8888 said:

  Quote

  Quote

  Quote

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

 

  Reveal hidden contents

 

 

Block

 

  Reveal hidden contents

 

 

BaseMPContainer

 

  Reveal hidden contents

 

 

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

Developer of Primeval Forest.

Posted
  On 1/4/2017 at 2:53 PM, Animefan8888 said:

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
  On 1/4/2017 at 3:19 PM, loordgek said:

no

//write
compound.setTag(name, serializable.serializeNBT());
//read
serializable.deserializeNBT(compound.getCompoundTag(name))

What is this serializable? The container variable??

Developer of Primeval Forest.

Posted
  On 1/4/2017 at 3:23 PM, loordgek said:

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

  Quote
Oh and deserializeNBT needs an NBTTAgCompound, so getTag does not work.
Posted
  On 1/4/2017 at 3:46 PM, loordgek said:

this does not work?

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

 

what do you meen by

  Quote
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:

 

  Reveal hidden contents

 

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
  On 1/4/2017 at 4:07 PM, loordgek said:

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
  On 1/4/2017 at 3:37 PM, Bektor said:

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.