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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
    • Maybe you need to create file in assets/<modid>/items/<itemname>.json with content like this:   { "model": { "type": "minecraft:model", "model": "modname:item/itemname" } }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.