Jump to content

Recommended Posts

Posted

The title pretty much says it all.

 

I have made a class that extends WorldSavedData but i don't know how register it.

Googled a bit about how to save and load custom data on a world and found out that i should use that class.

 

How do i register or "activate" the class i have made?

 

MyWorldSavedData class

 

package mods.dnd91.minecraft.hivecraft;

 

import java.util.Iterator;

 

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.village.Village;

import net.minecraft.world.World;

import net.minecraft.world.WorldSavedData;

import net.minecraft.world.storage.MapStorage;

 

public class HiveCraftWorldData extends WorldSavedData {

 

final static String key = "HiveCraft.World.Data";

 

public static HiveCraftWorldData forWorld(World world) {

        // Retrieves the MyWorldData instance for the given world, creating it if necessary

MapStorage storage = world.perWorldStorage;

HiveCraftWorldData result = (HiveCraftWorldData)storage.loadData(HiveCraftWorldData.class, key);

if (result == null) {

result = new HiveCraftWorldData();

storage.setData(key, result);

}

return result;

}

 

public int tickCounter = 0;

 

public HiveCraftWorldData() {

super(key);

}

 

public void tick()

    {

        ++this.tickCounter;

        //Iterator iterator = this.villageList.iterator();

 

        /*while (iterator.hasNext())

        {

            Village village = (Village)iterator.next();

            village.tick(this.tickCounter);

        }

 

        this.removeAnnihilatedVillages();

        this.dropOldestVillagerPosition();

        this.addNewDoorsToVillageOrCreateVillage();*/

 

        if (this.tickCounter % 400 == 0)

        {

            this.markDirty();

        }

    }

 

@Override

public void readFromNBT(NBTTagCompound nbttagcompound) {

// TODO Auto-generated method stub

 

}

 

@Override

public void writeToNBT(NBTTagCompound nbttagcompound) {

// TODO Auto-generated method stub

 

}

 

}

 

 

 

Regards DND

 

EDIT:

With the TickHandlar im able to load the data with WORLDLOAD... but the class that extends WorldSavedData don't run the write and read functions.... for NTB. :/

Posted

Thanks for the help!

 

For ppl that are having problems with WorldSaveData as i did: Here is the code i used:

 

To the @Init i just added ... to the end:

TickRegistry.registerTickHandler(new TickHandler(), Side.SERVER);

 

TickHandler.java

 

package mods.dnd91.minecraft.hivecraft;

 

import java.util.EnumSet;

 

import net.minecraft.world.World;

 

import cpw.mods.fml.common.ITickHandler;

import cpw.mods.fml.common.TickType;

 

public class TickHandler implements ITickHandler {

 

 

public static HiveCraftWorldData worldData = null;

 

@Override

public void tickStart(EnumSet<TickType> type, Object... tickData) {

if(type.equals(EnumSet.of(TickType.WORLDLOAD))){

World world = null;

for(int i = 0; i < tickData.length; i++)

if(tickData instanceof World)

world = (World)tickData;

if(world == null){

return;

}

 

worldData = HiveCraftWorldData.forWorld(world);

worldData.markDirty();

}

}

 

@Override

public void tickEnd(EnumSet<TickType> type, Object... tickData) {

 

}

 

@Override

public EnumSet<TickType> ticks() {

// TODO Auto-generated method stub

return EnumSet.of(TickType.WORLDLOAD);

}

 

@Override

public String getLabel() {

// TODO Auto-generated method stub

return "HiveCraft_Ticker";

}

 

}

 

 

 

MyWorldSavedData.java

 

package mods.dnd91.minecraft.hivecraft;

 

import java.util.Iterator;

 

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.village.Village;

import net.minecraft.world.World;

import net.minecraft.world.WorldSavedData;

import net.minecraft.world.storage.MapStorage;

 

public class HiveCraftWorldData extends WorldSavedData {

 

final static String key = "HiveCraftWorldData";

 

public static HiveCraftWorldData forWorld(World world) {

        // Retrieves the MyWorldData instance for the given world, creating it if necessary

MapStorage storage = world.perWorldStorage;

HiveCraftWorldData result = (HiveCraftWorldData)storage.loadData(HiveCraftWorldData.class, key);

if (result == null) {

result = new HiveCraftWorldData();

storage.setData(key, result);

}

return result;

}

 

public HiveCraftWorldData() {

super(key);

}

 

public HiveCraftWorldData(String key){

super(key);

}

 

@Override

public void readFromNBT(NBTTagCompound nbttagcompound) {

System.out.println("READ!");

// TODO Auto-generated method stub

 

}

 

@Override

public void writeToNBT(NBTTagCompound nbttagcompound) {

System.out.println("WRITE!");

// TODO Auto-generated method stub

 

}

 

}

 

 

 

 

  • 1 year later...
Posted

Should anybody look for it (to make a tutorial or something), in 1.8 there is no more :

MapStorage storage = world.perWorldStorage;

there is instead :

MapStorage storage = world.getPerWorldStorage();

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.