Jump to content

1.7.10 Save Data per World


Gaprosys

Recommended Posts

Hi,

thanks for the reply.

My current code looks like so:

 

IndustriousMageWorldSaveData:

[embed=425,349]

package de.gaprosys.industriousmage.handler_network;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.io.IOException;

import java.util.Map;

import java.util.Set;

 

import de.gaprosys.industriousmage.IndustriousMage;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.world.World;

import net.minecraft.world.WorldSavedData;

import net.minecraft.world.storage.MapStorage;

 

public class IndustriousMageWorldSaveData extends WorldSavedData {

 

final static String key = "IndustriousMageSaveData";

 

public IndustriousMageWorldSaveData(String key) {

super(key);

}

 

public IndustriousMageWorldSaveData() {

super(key);

}

 

public static IndustriousMageWorldSaveData get(World world) {

MapStorage storage = world.mapStorage;

IndustriousMageWorldSaveData result;

try {

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

} catch(NullPointerException ex) {

result = new IndustriousMageWorldSaveData();

storage.setData(key, result);

}

return result;

}

 

public static void saveData(World world) {

try {

world.mapStorage.loadData(IndustriousMageWorldSaveData.class, key).setDirty(true);

} catch(Exception ex) {}

}

 

@Override

public void readFromNBT(NBTTagCompound nbt) {

 

long maxIMP;

long currentIMP;

int id;

String s;

NBTTagList tagList = nbt.getTagList("Player", 10);

 

for(int i = 0; i < tagList.tagCount(); i++) {

NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);

s = tag.getString("playerName");

id = tag.getInteger("playerID");

currentIMP = tag.getLong("playerCurrentIMP");

maxIMP = tag.getLong("playerMaxIMP");

IndustriousMage.impTransfer.addPlayerToNetworks(s);

IndustriousMage.impTransfer.setPlayerIMPNetworkId(s, id);

IndustriousMage.impTransfer.addMaxIMPtoPlayer(s, maxIMP);

IndustriousMage.impTransfer.addCurrentIMPtoPlayer(s, currentIMP);

 

System.out.println();

System.out.println(s);

System.out.println(id);

System.out.println(currentIMP);

System.out.println(maxIMP);

 

}

}

 

@Override

public void writeToNBT(NBTTagCompound nbt) {

 

NBTTagList tagList = new NBTTagList();

ArrayList<String> network = new ArrayList<String>();

network = IndustriousMage.impTransfer.getPlayerIds();

String s;

 

for(int i = 0; i <= network.size(); i++) {

s = (String)network.get(i);

NBTTagCompound tag = new NBTTagCompound();

 

tag.setString("playerName", s);

tag.setInteger("playerID", i);

tag.setLong("playerCurrentIMP", IndustriousMage.impTransfer.getPlayerCurrentIMPNetwork(s));

tag.setLong("playerMaxIMP", IndustriousMage.impTransfer.getPlayerMaxIMPNetwork(s));

 

System.out.println();

System.out.println(s);

System.out.println(i);

System.out.println(IndustriousMage.impTransfer.getPlayerCurrentIMPNetwork(s));

System.out.println(IndustriousMage.impTransfer.getPlayerMaxIMPNetwork(s));

 

tagList.appendTag(tag);

}

nbt.setTag("Player", tagList);

 

}

 

}

[/embed]

 

the System.out.println is for me, To look, what they read. I use just a try to initialize the WorldSavedData Class from me, because, they throw often an NullPointerException by loading a world.

 

The used Events to load and save the datas:

[embed=425,349]

@SubscribeEvent

public void LoadPlayerList(WorldEvent.Load event) {

if(!event.world.isRemote) {

 

IndustriousMageWorldSaveData worldsave;

 

worldsave = IndustriousMageWorldSaveData.get(event.world);

}

}

 

@SubscribeEvent

public void UnloadPlayerList(WorldEvent.Unload event) {

if(!event.world.isRemote) {

IndustriousMageWorldSaveData.saveData(event.world);

}

}

 

@SubscribeEvent

public void SavePlayerList(WorldEvent.Save event) {

if(!event.world.isRemote) {

IndustriousMageWorldSaveData.saveData(event.world);

}

}

 

[/embed]

 

with the LoadPlayer- function I try to load the player list. what they actually not do. With the UnloadPlayerList and SavePlayerList I saved the lists.

 

- Gaprosys

Link to comment
Share on other sites

I habe tried it, and it doesn't worked. I need this to save the content of an internal net. By the player can I does this not saved, because this should work on servers to. If i try to use your solve, they doesn't add new player or save the content for a world unload and world load. My current way work to save the data for a world re log in in the same session, but if I start the Minecraft client new, they set all values to 0.

Link to comment
Share on other sites

I want to make a magic mod for automation. But i does now think, i should now use packets to handle my networks. The idee behind my mod is the use of an kind of network like blood magic. Now i should overwrite my handle class for the networks and sahe then the datas of the network by the player.

But anyway, thanks for your help and sorry for my bad englisch.

 

- Gaprosys

Link to comment
Share on other sites

I have tried to save datas per world. This datas are in an array and in maps. Now I didn't now, how to use the the WorldSavedData class. If I want to access the data, must I then not call the get- method? And if I want to access the data by the world load, must I then not use the WorldEvent? My data where I wanted to save was in an other class.

 

I have said, I save the datas per player to have the data stored somewhere times. I try it then again with the World Saved Data.

Link to comment
Share on other sites

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.