DND91 Posted May 5, 2013 Posted May 5, 2013 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. Quote
DND91 Posted May 5, 2013 Author Posted May 5, 2013 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 Quote
DND91 Posted May 5, 2013 Author Posted May 5, 2013 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 } } Quote
AlekBardzo Posted February 14, 2015 Posted February 14, 2015 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(); Quote
AlekBardzo Posted February 15, 2015 Posted February 15, 2015 Yes, I know, it does pop-up in search though. Besides I still cannot get it to work... Quote
Recommended Posts
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.