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

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 ?

  • Author

Yay! I thought as much but I was being hopeful. Any suggestions on how I would go about saving a list of blocks to the client so they don't have to keep making the list?

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.

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.