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

How to make a ArrayList of block position on the server side and save it so it can be loaded the next time the server reopens?

  • Author

I'm starting to understand how WorldSavedData works, but when reload the game it doesn't load any data. Help?

 

package com.cbultimate.stranded;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.WorldSavedData;
import java.util.ArrayList;

//Created by CBU on 17/1/2017.

public class ModWorldSavedData extends WorldSavedData {
    public static ArrayList<Vector3> ToolCupboards = new ArrayList<Vector3>();

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

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        NBTTagList cupboardList = nbt.getTagList("ToolCupboards", 10);
        ToolCupboards.clear();
        for (int i=0; i< cupboardList.tagCount(); i++){
            NBTTagCompound newCompound = cupboardList.getCompoundTagAt(i);
            int X = newCompound.getInteger("x");
            int Y = newCompound.getInteger("y");
            int Z = newCompound.getInteger("z");
            ToolCupboards.add(new Vector3(X, Y, Z));
        }
        System.out.println("Loaded " + ToolCupboards.size() + " cupboards.");
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        NBTTagList cupboardList = new NBTTagList();
        for (int i=0;i<ToolCupboards.size(); i++){
            NBTTagCompound newCompound = new NBTTagCompound();
            Vector3 position = ToolCupboards.get(i);
            newCompound.setInteger("x", position.x);
            newCompound.setInteger("y", position.y);
            newCompound.setInteger("z", position.z);
            cupboardList.appendTag(newCompound);
        }
        compound.setTag("ToolCupboards", cupboardList);
        return compound;
    }
}

 


    private final static String cupboard_dataIdentifier = "ToolCupboards";

    @SubscribeEvent
    public void blockPlacedEvent(BlockEvent.PlaceEvent event){
        System.out.println("(" + event.getPlacedBlock().getBlock().getUnlocalizedName() + ") was placed " + event.getPos().toString());
        World world = event.getWorld();
        ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

        if (modWorldSavedData == null) {
            modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
        }

        if (event.getPlacedBlock().getBlock().getUnlocalizedName().equals("tile.wooden_toolcupboard")){
            ModWorldSavedData.ToolCupboards.add(new Vector3(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ()));
        }

        world.getPerWorldStorage().setData(cupboard_dataIdentifier, modWorldSavedData);
        System.out.println("Tool Cupboards: " + ModWorldSavedData.ToolCupboards.size());
    }

  • Author

Sorry, but I got it working. I just had to markDirty(). It works perfectly now. I'm just making this for my learning so com.cbultimate is just my name and .com. Domain name's not taken. Thanks for your help.

 

package com.cbultimate.stranded;

import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;

//Created by CBU on 14/1/2017.

public class ModEventHandler {

    @SubscribeEvent
    public void playerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event){
        System.out.println(event.player.getDisplayName() + " has logged in the server.");
    }

    private final static String cupboard_dataIdentifier = "ToolCupboards";

    @SubscribeEvent
    public void blockPlacedEvent(BlockEvent.PlaceEvent event){
        System.out.println("(" + event.getPlacedBlock().getBlock().getUnlocalizedName() + ") was placed " + event.getPos().toString());
        World world = event.getWorld();
        ModWorldSavedData modWorldSavedData = (ModWorldSavedData) world.getPerWorldStorage().getOrLoadData(ModWorldSavedData.class, cupboard_dataIdentifier);

        if (modWorldSavedData == null) {
            modWorldSavedData = new ModWorldSavedData(cupboard_dataIdentifier);
        }

        if (event.getPlacedBlock().getBlock().getUnlocalizedName().equals("tile.wooden_toolcupboard")){
            ModWorldSavedData.ToolCupboards.add(new Vector3(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ()));
            world.getPerWorldStorage().setData(cupboard_dataIdentifier, modWorldSavedData);
            modWorldSavedData.markDirty();
            System.out.println("Tool Cupboards: " + ModWorldSavedData.ToolCupboards.size());
        }
    }
}

 

package com.cbultimate.stranded;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.WorldSavedData;
import java.util.ArrayList;

//Created by CBU on 17/1/2017.

public class ModWorldSavedData extends WorldSavedData {
    public static ArrayList<Vector3> ToolCupboards = new ArrayList<Vector3>();

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

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        NBTTagList cupboardList = nbt.getTagList("ToolCupboards", 10);
        ToolCupboards.clear();
        for (int i=0; i< cupboardList.tagCount(); i++){
            NBTTagCompound newCompound = cupboardList.getCompoundTagAt(i);
            int X = newCompound.getInteger("x");
            int Y = newCompound.getInteger("y");
            int Z = newCompound.getInteger("z");
            ToolCupboards.add(new Vector3(X, Y, Z));
        }
        System.out.println("Loaded " + ToolCupboards.size() + " cupboards.");
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        NBTTagList cupboardList = new NBTTagList();
        for (int i=0;i<ToolCupboards.size(); i++){
            NBTTagCompound newCompound = new NBTTagCompound();
            Vector3 position = ToolCupboards.get(i);
            newCompound.setInteger("x", position.x);
            newCompound.setInteger("y", position.y);
            newCompound.setInteger("z", position.z);
            cupboardList.appendTag(newCompound);
        }
        compound.setTag("ToolCupboards", cupboardList);
        return compound;
    }
}

 

  • Author

I'm sorry if this may sound stupid but If I remove static, my ModEventHandler class can't access it. From there on I'm not sure what to do. I've renamed my mod to mod.cbultimate. Thanks for the tip, my unlocalized names includes ModID now. Thanks for that too.

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.