Posted March 27, 20169 yr 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; }
March 28, 20169 yr Author Well I rewrote my whole world saved data class using Diesieben's example and it works now. For anyone else having issues: https://github.com/Questology/Questology/blob/master/src/main/java/demonmodders/questology/QuestologyWorldData.java
December 22, 20213 yr 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 December 22, 20213 yr 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.