Jump to content

[Solved] WorldSavedData readFromNBT not called


DestinySpork

Recommended Posts

I am making a mod in which I want to have oil, but I don't want to have actual oil liquids generating in the world. What I want to do is use WorldSavedData to store chunk coords of the chunks which contain oil. It works fine, apart from the fact that readFromNBT is never called, and so my data is not present after a world relog or a game restart.

 

All of my code is here:

https://github.com/DestinySpork/AppliedAutomation

 

But here is my WorldSavedData class to make it easier:

 

public class OreSaves extends WorldSavedData{

private int[] oil2;
public List<Integer> oil = new ArrayList<Integer>();
public List<Integer> newOil = new ArrayList<Integer>();

public OreSaves() {
	super(key);
}


public boolean isOilChunk(Chunk chunk){


	if(oil.contains(chunk.xPosition)){
		return true;
	}

	return false;
}

public final static String key = "appliedscience.oregens";

/**
 * this was in the tutorial, not sure what it's needed for
public static OreSaves forworld(World world){
	MapStorage storage = world.getPerWorldStorage();
	OreSaves result = (OreSaves)storage.loadData(OreSaves.class, key);
	if(result == null){
		result = new OreSaves();
		storage.setData(key,  result);
	}
	return new OreSaves();
}
**/

public static OreSaves getSave(World world){
	MapStorage storage = world.getPerWorldStorage();
	OreSaves instance = (OreSaves) storage.loadData(OreSaves.class, key);
	if(instance == null){
		instance = new OreSaves();
		storage.setData(key,  instance);
	}
	return instance;
}

public void generateOil(int chunkX, int chunkZ){
	System.out.println("Oil was generated");
	System.out.println("Added " + chunkX + " , " + chunkZ);


	newOil.addAll(oil);
	newOil.add(chunkX);
	newOil.add(chunkZ);
	oil = newOil;

	this.markDirty();

}



/**
 * <i> Requires </i> Java 8 or higher
 */
@Override
public void readFromNBT(NBTTagCompound nbt) {
	System.out.println("Read from NBT!");
	int[] t = nbt.getIntArray("Oil");
	oil = IntStream.of(t).boxed().collect(Collectors.toList());

}

/**
 * <i> Requires </i> Java 8 or higher
 */
@Override
public void writeToNBT(NBTTagCompound nbt) {
	System.err.println("Writing NBT");
	int[] t = oil.stream().mapToInt(e -> e).toArray();
	nbt.setIntArray("Oil", t);
}
}

 

I call it from a command:

@Override
public void execute(ICommandSender sender, String[] args) throws CommandException {
	boolean isOil = OreSaves.getSave(sender.getEntityWorld()).isOilChunk(sender.getEntityWorld().getChunkFromBlockCoords(sender.getPosition()));
	if(isOil) sender.addChatMessage(new ChatComponentText("The current chunk contains "+EnumChatFormatting.DARK_GRAY+"Oil."));

	else
	sender.addChatMessage(new ChatComponentText("The current chunk contains no oil."));
	return;
}

Link to comment
Share on other sites

  • 5 years later...

For anyone else experiencing the same issues, the github link will no longer work.

Please make sure that you have a constructor that accepts a String and passes it to super like so:
 

@SuppressWarnings("unused")
public YourSavedData(String s) {
    super(s);
}

The code for MapStorage tries to instantiate it, and if it doesn't have that constructor, it will silently throw an exception without crashing.

Edited by Majd123mc
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.