Jump to content

[Solved] [1.16.5] How to make a chunk capability


BlockyPenguin

Recommended Posts

Hi, I'm trying to save some additional data to chunks, and have figured out that Capabilities are probably the way to go. After looking around the internet for a bit I think I got the gist and I set to coding. However, the AttachCapabilitiesEvent#addCapability() method seems to require an ICapabilityProvider, which I don't remember seeing anything on. Here's what I've got so far:

 

IRift class:

public interface IRift {
	boolean isRift();
	float getRiftActivityPercent();
	void setRiftActivityPercent(float riftActivityPercent);
	void makeRift();
	void closeRift();
	void riftEvent();
}

 

RiftStorage class:

public class RiftStorage implements Capability.IStorage<IRift> {

	@Override
	public INBT writeNBT(Capability<IRift> capability, IRift instance, Direction side) {
		CompoundNBT nbt = new CompoundNBT();
		
		if(instance.isRift()) {
			nbt.putBoolean("isRift", instance.isRift());
			nbt.putFloat("riftActivityPercent", instance.getRiftActivityPercent());
		}
		
		return nbt;
	}

	@Override
	public void readNBT(Capability<IRift> capability, IRift instance, Direction side, INBT nbt) {
		CompoundNBT cnbt = ((CompoundNBT)nbt);
		
		if(cnbt.getBoolean("isRift")) {
			instance.makeRift();
			instance.setRiftActivityPercent(cnbt.getFloat("riftActivityPercent"));
		}
	}
}

 

RiftCapability class:

public class RiftCapability implements IRift {
	
	private boolean isRift = false;
	private float riftActivityPercent = 0.0f;

	@Override
	public boolean isRift() {
		return isRift;
	}

	@Override
	public float getRiftActivityPercent() {
		return riftActivityPercent;
	}

	@Override
	public void setRiftActivityPercent(float riftActivityPercent) {
		if(riftActivityPercent < 0 || riftActivityPercent > 1) throw new IndexOutOfBoundsException("Rift Activity Percent cannot be less than 0 or greater than 1!");
		else this.riftActivityPercent = riftActivityPercent;
	}

	@Override
	public void makeRift() {
		this.isRift = true;
	}

	@Override
	public void closeRift() {
		this.isRift = false;
	}

	@Override
	public void riftEvent() {
		//TODO: Fire a rift event.
	}
}

 

Advice much appreciated. Thanks :)

Edited by BlockyPenguin

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

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.