Jump to content

How do I store a string in the block's nbt data?


Anonomys

Recommended Posts

Hello, I'm trying to store data in the block's nbt data, but i have no idea how to do this, apparently im supposed to use capabilities but there's so little documentation, and I have no idea how to do this, can someone please provide me some example code?

This is my signature. I don't really too much know about java but i try to help and i try to get help from others too. Thank you!

Link to comment
Share on other sites

You only need a capability if it is not your BlockEntity.

https://forge.gemwire.uk/wiki/Capabilities (includes a simple example at the bottom but for a serializable Entity capability rather than a BlockEntity - same principle applies).

 

If it is your BlockEntity

https://forge.gemwire.uk/wiki/Block_Entities#Storing_Data_within_your_BlockEntity

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Hey I did all that, tbh i've been struggling the last hour trying to make the thing, I did everything but now I'm trying to add the capability to the block and then set it when it is placed down, this is the code:

	@NotNull
	@Override
	public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
		return LazyOptional.of(ATMCapability::new).cast();
	}

	@Override
	public void onPlace(BlockState state, Level world, BlockPos pos, BlockState blockState, boolean p_60570_) {
	    world.getCapability(ATMCapability.INSTANCE).ifPresent((cap) -> {
	        cap.setPrivateKey("test");
			cap.setAtmId("test");
		});

		System.out.println("ATM created!");
		System.out.println("Private key: " + world.getCapability(ATMCapability.INSTANCE).orElseThrow(NullPointerException::new).getPrivateKey());
		System.out.println("ATM ID: " + world.getCapability(ATMCapability.INSTANCE).orElseThrow(NullPointerException::new).getAtmId());
	}

I have no idea if this is even correct, but i dont know what else to do, it keeps giving me the nullpointerexception which means that its not being saved

This is my signature. I don't really too much know about java but i try to help and i try to get help from others too. Thank you!

Link to comment
Share on other sites

Read the part about attaching capabilities on the wiki.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Yes, but I don't understand the AttachCapabilitiesEvent<Entity>, how does that attach it to the atm block, I tried subscribing and all of that, changing code i dont know what to do, can you please explain it?

This is my signature. I don't really too much know about java but i try to help and i try to get help from others too. Thank you!

Link to comment
Share on other sites

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Hey, yeah i did that and made a blockentity and now im getting this error 

class ...atm.capability.ATMCapability cannot be cast to class ...atm.capability.ATMCapabilityInterface

 

Edited by Anonomys

This is my signature. I don't really too much know about java but i try to help and i try to get help from others too. Thank you!

Link to comment
Share on other sites

If you don't know what that error means you are in the wrong forum.

You want a "learning java" forum.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Huh?

A capability implementation must implement its own interface.

You don't even show the full stacktrace of the error which will tell you where the cast occurs (maybe implicitly inserted by the compiler because of generics?).

 

If you want further help, you need to show everything you are doing.

Posting snippets of code out of context and truncated error messages just means your question unanswerable and will probably just mean your question gets ignored.

We are not going to waste our time chasing you for relevant information. It is you that wants help.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

I found where the error was, in the blockentity class the getCapability was returning the ATMCapability.INSTANCE instead of the ATMCapabilityImplementation. Sorry for taking your time

This is my signature. I don't really too much know about java but i try to help and i try to get help from others too. Thank you!

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.



×
×
  • Create New...

Important Information

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