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

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

  • Author

Will i need to call the ticks function with a tickhandler or can i make forge do it for me? Thanks for the fast answer :)

  • Author

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

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

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.