Jump to content

[1.7.2][unsolved] save mana in world [mana is glitching]


knokko

Recommended Posts

hello guys,

I have a problem with mana.

I want to save the mana in the world so players will have to share it.

But I just don't know how that works.

I am going to make blocks that produce mana so this will work finer.

Does anybody know how I can save mana in the world and not in players?

Link to comment
Share on other sites

My understanding of this is that you want to have a 'global' mana variable that is shared between all players, and not exclusive to any one player.

 

I'm not sure, but you could probably save this to the world's NBTTagCompound using

NBTTagCompound worldData = worldObj.getWorldInfo().getTagCompound();

or something like that.

 

then just use

worldData.setInt(keyName, value);

and

worldData.getInt(keyName);

 

I have however never used it, and I'm not sure if this is the right NBTTagCompound to be saving to. It would be something along those lines though, I assume.

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

I have tried the tutorial above but it doesn't save my mana at all.

I havent written everything in the example because I want to only use 1 class for the mana, and I havent all the classes of diesieben.

Here is the code, I think I only need the finishing touch now.

 

package enderpower.magic;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;

public class Magic extends WorldSavedData{

public static int mana;
private static final String IDENTIFIER = "mana";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	mana = nbt.getInteger("mana");

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", mana);

}
public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}

}

Link to comment
Share on other sites

I have made a setter and a getter, but eclipse said every time that mana must be static.

And I dont understand how to use markDirty();

But this is the first time I am using worldsaveddata so dont be surprised I am doing something wrong.

 

 

Here is the updates code:

package enderpower.magic;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;

public class Magic extends WorldSavedData{

private static int mana;
private static final String IDENTIFIER = "enderpower";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	setMana(nbt.getInteger("mana"));

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", getMana());

}
public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}
public static int getMana() {
	return Magic.mana;
}
public static void setMana(int mana) {
	Magic.mana = mana;


}

}

 

Here is the way I am using my setter:

It said I had to make everything static.

 

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_){
	Magic.setMana(Magic.getMana() + 1);


	return true;

}

Link to comment
Share on other sites

The reason it's telling you to make it static is because you're trying to call the field from the class.

 

I suggest you go read up on what static actually means, and the difference between instance fields and class fields.

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

A call to .markDirty() makes Minecraft save the WorldSavedData.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

I allways get errors if I write this.markDirty();

 

So where can I place it and what have I to do with the integer to let it all work?

 

 

EDIT:

 

If I place .markDirty() in setMana I get the error "Cannot make a static reference to the non-static method markDirty() from the type WorldSavedData"

 

if I change the modifier of setMana to non static I get the error in an other class that says "Cannot make a static reference to the non-static method setMana(int) from the type Magic" and says I have to change the modifier of setMana to static.

 

the class code:

 

public class Magic extends WorldSavedData{

public static int mana;
private static final String IDENTIFIER = "enderpower";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	setMana(nbt.getInteger("mana"));

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", getMana());

}
public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}
public static int getMana() {
	return Magic.mana;
}
public void setMana(int mana) {
	Magic.mana = mana;
	this.markDirty();



}

} 

 

here is the code in the class that tries to edit the mana:

 

 public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_){
	Magic.setMana(Magic.getMana() + 1);



	return true;

}

with the error "Cannot make a static reference to the non-static method setMana(int) from the type Magic" and the quick fix "change modifier of 'setMana() ' to 'static'."

 

What can I do to let is save the mana and work correctly?

Link to comment
Share on other sites

Mana is not static anymore, all errors are gone, markDirty() works succesfull now.

But how can I edit the mana from other classes now?

 

If I try it, eclipse says that I have to make mana static.

So what have I to do now?

 

here is the new class code:

 

package enderpower.magic;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;

public class Magic extends WorldSavedData{

private int mana;
private static final String IDENTIFIER = "enderpower";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	setMana(nbt.getInteger("mana"));

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", getMana());

}
public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}
public int getMana() {
	return this.mana;
}
public void setMana(int mana) {
	this.mana = mana;
	this.markDirty();



}

}

Link to comment
Share on other sites

I haven't copied everything from you blindly, I have copied the things I thought I would need.

I can't find wat that fracture exactly is, I don't know I need that or not.

Why do you think I haven't all the methods that you have.

 

I will go away thursday to another country where probably no free wifi is.

That is why I want to know the answer, I am trying to access it from other classes without making the int static.

But I can't figure out how.

Link to comment
Share on other sites

Last time I'm going to say it:

 

The Mana.get method gives you the instance of your class.

You probably mean the Magic.get method because the class is called Magic...

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Mana is the old class that was bugged, Magic is the right class.

I finally understand it, sorry that I was so silly not to read everything good and now I realize what you meant with the instance.

Minecraft saves the mana perfectly now, thank you very much.

Link to comment
Share on other sites

After the mana was saving correctly I have added more kinds of mana.

But now it doesnt save anymore, does anybody understand why?

I dont know what I have done wrong.

 

here is the new code:

ublic class Magic extends WorldSavedData{

private int mana;
private int geemana;
private int fiemana;
private int doumana;
private int endermana;
private static final String IDENTIFIER = "enderpower";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	setMana(nbt.getInteger("mana"));
	setMana(nbt.getInteger("geemana"));
	setMana(nbt.getInteger("fiemana"));
	setMana(nbt.getInteger("doumana"));
	setMana(nbt.getInteger("endermana"));

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", getMana());
	nbt.setInteger("geemana", getGeeMana());
	nbt.setInteger("fiemana", getFieMana());
	nbt.setInteger("doumana", getDouMana());
	nbt.setInteger("endermana", getEnderMana());

}

public int getEnderMana() {
	return this.endermana;
}
public int getDouMana() {
	return this.doumana;
}
public int requestNewId() {
	markDirty();
	return mana++;
}

public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}
public int getGeeMana(){
	return this.geemana;
}

public int getMana() {
	return this.mana;
}
public int getFieMana(){
	return this.fiemana;
}

public void setMana(int mana) {
	this.mana = mana;
	this.markDirty();
}
public void setGeeMana(int geemana){
	this.geemana = geemana;
	this.markDirty();
}
public void setFieMana(int fiemana){
	this.fiemana = fiemana;
	this.markDirty();
}
public void setDouMana(int doumana){
	this.doumana = doumana;
	this.markDirty();
}
public void setEnderMana(int endermana){
	this.endermana = endermana;
	this.markDirty();
}

}

Link to comment
Share on other sites

  • 3 weeks later...

My mana is now saving better but it is glitching too.

It seems that every kind of mana has 2 values.

The first value is resetted if I restart the game, but if I make the mana higher they will get higher too.

The second value is not resetted after I restarts the game and works perfectly.

 

The most wands uses the second value of mana, but 1 wand uses the first value of mana what causes problems.

And look at my manameter:

public class ManaMeterItem extends Item{
public ManaMeterItem(){
	super();
	setCreativeTab(Enderpower.enderpowermagicitemstab);
}
public ItemStack onItemRightClick(ItemStack itemstack, World world,EntityPlayer player){

		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.LIGHT_PURPLE + "your mana = " + Magic.get(world).getThauMana()));
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.YELLOW + "your fiemana = " + Magic.get(world).getFieMana()));
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.GREEN + "your geemana = " + Magic.get(world).getGeeMana()));
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.DARK_BLUE + "your doumana = " + Magic.get(world).getDouMana()));
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.DARK_PURPLE + "your endermana = " + Magic.get(world).getEnderMana()));
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.BLUE + "your siemana = " + Magic.get(world).getSieMana()));	
	return itemstack;

}


}

If I right click I get 12 messages instead of 6 and here they are:

 

 

your mana = 0

your fiemana = 0

your geemana = 0

your doumana = 0

your endermana = 0

your siemana = 0

your mana = 10000

your fiemana = 86760

your geemana = 10000

your doumana = 59550

your endermana = 68400

your siemana = 67210

 

 

 

I hope you understand the problem

 

Here is my Magic class:

public class Magic extends WorldSavedData{

private int mana = 0;
private int geemana = 0;
private int fiemana = 0;
private int doumana = 0;
private int endermana = 0;
private int siemana = 0;
private static final String IDENTIFIER = "enderpower";

public Magic(String identifier) {
	super(identifier);

}
public Magic(){
	super(IDENTIFIER);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	mana = nbt.getInteger("mana");
	geemana = nbt.getInteger("geemana");
	fiemana = nbt.getInteger("fiemana");
	doumana = nbt.getInteger("doumana");
	endermana = nbt.getInteger("endermana");
	siemana = nbt.getInteger("siemana");

}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	nbt.setInteger("mana", mana);
	nbt.setInteger("geemana", geemana);
	nbt.setInteger("fiemana", fiemana);
	nbt.setInteger("doumana", doumana);
	nbt.setInteger("endermana", endermana);
	nbt.setInteger("siemana", siemana);

}

public int getEnderMana() {
	return this.endermana;
}
public int getDouMana() {
	return this.doumana;

}
public int getSieMana(){
	return this.siemana;
}
public int mana() {
	markDirty();
	return mana++;
}

public static Magic get(World world) {
	Magic data = (Magic)world.loadItemData(Magic.class, IDENTIFIER);
	if (data == null) {
		data = new Magic();
		world.setItemData(IDENTIFIER, data);
	}
	return data;
}
public int getGeeMana(){
	return this.geemana;
}

public int getThauMana() {
	return this.mana;
}
public int getFieMana(){
	return this.fiemana;
}
public boolean useMana(int mana, int geemana, int fiemana, int doumana, int endermana, int siemana){
	if(this.mana >= mana && this.geemana >= geemana && this.fiemana >= fiemana && this.endermana >= endermana && this.siemana >= siemana && this.doumana >= doumana){
		this.mana -= mana;
		this.geemana -= geemana;
		this.fiemana -= fiemana;
		this.doumana -= doumana;
		this.endermana -= endermana;
		this.siemana -= siemana;
		this.markDirty();
		return true;
	}
	else{
		return false;
	}

}
public void setMana(int mana, int geemana, int fiemana, int doumana, int endermana, int siemana){
	this.mana = mana;
	this.geemana = geemana;
	this.fiemana = fiemana;
	this.doumana = doumana;
	this.endermana = endermana;
	this.siemana = siemana;
	this.markDirty();

}


public void editMana(int mana, int geemana, int fiemana, int doumana, int endermana, int siemana){
	this.mana += mana;
	this.geemana += geemana;
	this.fiemana += fiemana;
	this.doumana += doumana;
	this.endermana += endermana;
	this.siemana += siemana;
	this.markDirty();
}

}

 

Does anybody understand what goes wrong?

I don't understand this at all.

Link to comment
Share on other sites

How can I save it in the server than?

Do you  mean @SideOnly?

 

And where can I say it must be saved in the server?

No, you need it in client, but the operations on it must only happen on server, then the server should synchronize the data with the clients so they would know what is the data on the server.

 

the reason your code gave 12 messages is because onItemRightClick is called on both the server and the client, you have to check whether you're on the server or the client to get 6 messages from server only.

 

You mainly know whether you're on server or not by world.IsRemote (if you have a World instance) which is false when on server.

 

You need to know more about clients and servers and what happens in them, you also need to know about client-server communications mainly using packets. There are pretty much a lot of articles and tutorials out there.

coolAlias have a pretty good IExtendedEntityProperties tutorial which I found quite similar to WorldSavedData in concept, check it out.

Link to comment
Share on other sites

I have allready read the topic about IExtendedEntityProperties and I have added the "!world.isRemote" check on my manameters.

The manameters work good now.

 

But there is 1 wand that doesn't work now.

 

Here is the NOT working flywand:

public class FlyWand extends Item {
public FlyWand(){
	super();
}

@Override
public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player){
	if(!world.isRemote){
	if(Magic.get(world).useMana(5, 0, 0, 0, 0, 0)){
	player.jumpMovementFactor = 0.5F;
	player.fallDistance = 0.0F;
	player.motionY = player.rotationPitch * -0.025;
	}
	}

	return item;
} 



}

(This wand worked fine before I added the mana check.)

 

Here is a wand that works perfectly and uses the same check:

 public class BlastWand extends Item{
public BlastWand(){
	super();
	setCreativeTab(CreativeTabs.tabCombat);
}
@Override
public ItemStack onItemRightClick(ItemStack itemstack, World world,EntityPlayer player) {
	if(!world.isRemote){
		if (Magic.get(world).useMana(40, 0, 100, 0, 0, 0))
		{

			world.spawnEntityInWorld(new BlastBall(world, player));

		}
	}
	return itemstack;   
        }
}

 

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.