Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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;
}

  • 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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.