Jump to content

Recommended Posts

Posted

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

Posted

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

 

Posted

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

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