Jump to content

[1.10.2][SOLVED] Saving player data


lukas2005

Recommended Posts

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Use the Capability system.

 

For examples, you can look at the capabilities provided by Forge (look for usages of

CapabilityManager#register

), the capability test mod or my own mod's capabilities (API, implementation).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

is there a way to use NBTTag?

 

NBT should only be used to save data, it shouldn't be used to store it at runtime.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I believe you don't fully understand how data should be stored on players. Well - I might be wrong, but worth asking :)

 

Saving data to player data files is different from assigning data to player when he is online.

 

It would help us help you if you would describe your end goal and what you want to store.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

i have a variable and i need to save it and i have no idea how to use capabilities

Read the docs

http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

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.

Link to comment
Share on other sites

Do any of the existing capabilities (

IItemHandler

,

IFluidHandler

,

IAnimationStateMachine

) match the data you want to store?

 

If they do, use them. If they don't, create your own.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

few questions:

 

1.Where i shloud register my capabliity

2.i have an error when i am trying to register a capability

eclipse marks this with red line:

CapabilityManager.INSTANCE.register(PlayerDataCapabilityClass.PlayerDataCapability.class, PlayerDataCapabilityClass.PlayerDataCapabilityStorage.class, PlayerDataCapabilityClass.PlayerDataCapabilityFactory.class);

PlayerDataCapabilityClass.java:

public class PlayerDataCapabilityClass {

public interface PlayerDataCapability {



}

public static class PlayerDataCapabilityStorage implements Capability.IStorage<PlayerDataCapability> {

	  @Override
	  public NBTBase writeNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side) {
		return null;
	    // return an NBT tag
	  }

		@Override
		public void readNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side,
				NBTBase nbt) {
			// TODO Auto-generated method stub

		}

}

public static class PlayerDataCapabilityFactory implements Callable<PlayerDataCapability> {

	  @Override
	  public PlayerDataCapability call() throws Exception {
		  
	    return null;
	    
	  }
	  
}


}

 

Link to comment
Share on other sites

few questions:

 

1.Where i shloud register my capabliity

2.i have an error when i am trying to register a capability

eclipse marks this with red line:

CapabilityManager.INSTANCE.register(PlayerDataCapabilityClass.PlayerDataCapability.class, PlayerDataCapabilityClass.PlayerDataCapabilityStorage.class, PlayerDataCapabilityClass.PlayerDataCapabilityFactory.class);

PlayerDataCapabilityClass.java:

public class PlayerDataCapabilityClass {

public interface PlayerDataCapability {



}

public static class PlayerDataCapabilityStorage implements Capability.IStorage<PlayerDataCapability> {

	  @Override
	  public NBTBase writeNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side) {
		return null;
	    // return an NBT tag
	  }

		@Override
		public void readNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side,
				NBTBase nbt) {
			// TODO Auto-generated method stub

		}

}

public static class PlayerDataCapabilityFactory implements Callable<PlayerDataCapability> {

	  @Override
	  public PlayerDataCapability call() throws Exception {
		  
	    return null;
	    
	  }
	  
}


}

Listen to eclipse it is probably an invalid arguement.

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.

Link to comment
Share on other sites

It says

"The method register(Class<T>, Capability.IStorage<T>,Class<? extends T>) is not applicable for arguments(Class<PlayerDataCapabilityClass.PlayerDataCapability>,Class<PlayerDataCapabilityClass.PlayerDataCapabilityStorage>,Class<PlayerDataCapabilityClass.PlayerDataCapabilityFactory>)"

 

Link to comment
Share on other sites

1.Where i shloud register my capabliity

Register it in preInit from your

@Mod

class or a class called from it.

 

 

2.i have an error when i am trying to register a capability

There are two overloads of

CapabilityManager#registerCapability

that take a different third argument:

  • One takes the
    Class

    of the capability interface's default implementation

  • One takes an instance of
    Callable

    that returns an instance of the default implementation

 

You're trying to pass the

Class

of the

Callable

implementation, which is incorrect.

 

You haven't actually added any methods to your capability interface or implemented any of the methods in your

IStorage

and

Callable

implementations.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

So i need to pass a PlayerDataCapabilityClass.PlayerDataCapabilityFactory.this as a third argument?

 

No, you need to create an instance of

PlayerDataCapabilityClass.PlayerDataCapabilityFactory

and pass that as the third argument.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

ok i have created and registered capability but how do i use it?

 

Call

ICapabilityProvider#getCapability

to get the instance of your handler attached to the provider.

 

Every

Entity

(including

EntityPlayer

) is an

ICapabilityProvider

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

ok i have created and registered capability but how do i use it?

 

Call

ICapabilityProvider#getCapability

to get the instance of your handler attached to the provider.

 

Every

Entity

(including

EntityPlayer

) is an

ICapabilityProvider

.

where i need to call this and how do i read/write the data?

Link to comment
Share on other sites

where i need to call this and how do i read/write the data?

 

Call it whenever you want to access your handler instance (i.e. the instance of

PlayerDataCapability

). This handler instance stores the data, you need to create methods in your interface to interact with the data (read/write it).

 

The

ICapabilitySerializable

is responsible for reading the handler from and writing the handler to NBT.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

i have this code:

package pl.minepack.gym.capabilities;

import java.util.concurrent.Callable;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;

public class PlayerDataCapabilityClass {

public interface PlayerDataCapability {

	int Strength = 0;

	public int addStrength(int count);
	public int subStrength(int count);

	public void setStrength(int count);
	public int getStrength();

}

public static class PlayerDataCapabilityStorage implements Capability.IStorage<PlayerDataCapability> {

	  @Override
	  public NBTBase writeNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side) {
		  NBTTagCompound nbt = new NBTTagCompound();
		  
		  nbt.setInteger("Strength", instance.getStrength());
		  
		  return nbt;
	  }

		@Override
		public void readNBT(Capability<PlayerDataCapability> capability, PlayerDataCapability instance, EnumFacing side,
				NBTBase nbt) {

			instance.setStrength(((NBTTagCompound)nbt).getInteger("Strength"));

		}

}

public static class PlayerDataCapabilityFactory implements PlayerDataCapability {

	private int Strength = 0;

	@Override
	public int addStrength(int count) {

		return this.Strength += count;
	}

	@Override
	public int subStrength(int count) {

		return this.Strength -= count;
	}

	@Override
	public void setStrength(int count) {

		this.Strength = count;

	}

	@Override
	public int getStrength() {
		// TODO Auto-generated method stub
		return this.Strength;
	}  
}


}

and what now?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements




×
×
  • Create New...

Important Information

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