Jump to content

help WorldSavedData for a Client side only mod


MiKeY_

Recommended Posts

Hi, So I'm trying to use WordSavedData for a client side only mod to store a list of blocks. I've attempted to follow other mods, the mc source and the forge docs but I can't seem to understand why when I restart the world the data saved to the nbt is lost.

 

Heres my WorldSavedData


public class BlockStorage extends WorldSavedData {

    private static final String DATA_KEY = Reference.MOD_NAME + "_BlockData";

    private HashMap<String, BlockData> blockStorage = new HashMap<>();

    public BlockStorage() {
        super(DATA_KEY);
    }

    public BlockStorage(String name) {
        super(name);
    }

    public static BlockStorage get(World world) {
        MapStorage storage = world.getMapStorage();

        if (storage == null)
            throw new IllegalStateException("World#getMapStorage returned null. The following WorldSave failed to save data: " + DATA_KEY);

        BlockStorage instance = (BlockStorage) storage.getOrLoadData(BlockStorage.class, DATA_KEY);

        if (instance == null) {
            instance = new BlockStorage();
            storage.setData(DATA_KEY, instance);
        }

        return instance;
    }

    @Override
    @ParametersAreNonnullByDefault
    public void readFromNBT(NBTTagCompound nbt) {

        NBTTagList list = nbt.getTagList("blocks", 10);

        for (int i = 0; i < list.tagCount(); ++i)
        {
            NBTTagCompound compound = list.getCompoundTagAt(i);

            blockStorage.put(
                    compound.getString("key"),
                    new BlockData(
                            compound.getString("entryName"),
                            new OutlineColor( compound.getIntArray("color") ),
                            new ItemStack( compound.getCompoundTag("stack") ),
                            compound.getBoolean("drawing")
                    )
            );
        }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound)
    {
        NBTTagList list = new NBTTagList();

        blockStorage.forEach( (k, v) -> {
            NBTTagCompound c = new NBTTagCompound();

            c.setString("key", k);

            c.setString("entryName", v.getEntryName());
            c.setIntArray("color", new int[]{v.getOutline().getRed(), v.getOutline().getGreen(), v.getOutline().getBlue()});
            c.setBoolean("drawing", v.isDrawing());
            c.setTag("stack", v.getItemStack().writeToNBT(new NBTTagCompound()));

            list.appendTag(c);
        });

        compound.setTag("blocks", list);

        return compound;
    }

    public HashMap<String, BlockData> getBlockStorage() {
        return blockStorage;
    }

    public void setBlockStorage(HashMap<String, BlockData> blockStorage) {
        this.blockStorage = blockStorage;
    }
}

 

Heres how I access & save to it

BlockStorage storage = BlockStorage.get(XRay.mc.player.world);

// No blocks exist
if( storage == null )
	return;

storage.getBlockStorage().forEach( (k, v) -> {
  System.out.println(k);
  System.out.println(v.toString());
});

// Adding to it
BlockStorage storage = BlockStorage.get(mc.player.world);
if (storage != null) {
  storage.setBlockStorage( Controller.getBlockStore().getStore() );
  storage.markDirty();
}

 

Any help is appreacheted but if this is something that isn't possible in a single player only mod then any advice on what I should be using instead would be greatly appreachted too ?

Link to comment
Share on other sites

My approach is to create a folder in the user's .minecraft folder (which you can get from Minecraft.getInstance().gameDir) putting a subfolder for my mod, and then subdirectories for each world named after the server address (Minecraft.getInstance().getCurrentServerData()). It works most of the time, but the problem with this approach is that if you're connected to a server, then you use a command or something that moves you to a different server without going back to the connection screen, your mod will still think you're in the server you originally connected to.

  • Like 1
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.