Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • storing values in an external file
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
hkiller1

storing values in an external file

By hkiller1, November 5, 2012 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

hkiller1    1

hkiller1

hkiller1    1

  • Stone Miner
  • hkiller1
  • Members
  • 1
  • 52 posts
Posted November 5, 2012

I am trying to make a money mod, but I need a way to store and change the variables like the player name and money amount in a external file. What would I use to do this?

  • Quote

Share this post


Link to post
Share on other sites

Ardenexal    1

Ardenexal

Ardenexal    1

  • Tree Puncher
  • Ardenexal
  • Members
  • 1
  • 6 posts
Posted November 6, 2012

The easiest would be to create an xml file or yaml which alot of other mods use. If ur not sure how to write to an xml or yaml file you can just search for java tutorials tyere are quite a few out there. Hooe that helps u out, good luck

  • Quote

Share this post


Link to post
Share on other sites

hkiller1    1

hkiller1

hkiller1    1

  • Stone Miner
  • hkiller1
  • Members
  • 1
  • 52 posts
Posted November 6, 2012

Whats better xml or yaml?

  • Quote

Share this post


Link to post
Share on other sites

keepcalm    87

keepcalm

keepcalm    87

  • Diamond Finder
  • keepcalm
  • Forge Modder
  • 87
  • 281 posts
Posted November 7, 2012

YAML. It's simpler and easier for everyone.

  • Quote

Share this post


Link to post
Share on other sites

hkiller1    1

hkiller1

hkiller1    1

  • Stone Miner
  • hkiller1
  • Members
  • 1
  • 52 posts
Posted November 7, 2012

Do I import the src or the jar into the project for yaml?

  • Quote

Share this post


Link to post
Share on other sites

rich1051414    36

rich1051414

rich1051414    36

  • Creeper Killer
  • rich1051414
  • Forge Modder
  • 36
  • 160 posts
Posted November 7, 2012

Or if you don't need the player to be able to modify these values, just compress and export the map with the mappings directly. You can do this like this:

Saving:

File savedirectory = new File(world.getSaveHandler().getSaveDirectoryName().concat("/MySaveInfo.dat"));
savedirectory.createNewFile();
ObjectOutputStream os = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(savedirectory)));
os.writeObject(MapContainingPlayerMappedVariables);

Loading:

File savedirectory = new File(world.getSaveHandler().getSaveDirectoryName().concat("/MySaveInfo.dat"));
if(savedirectory.exists()){
ObjectInputStream os = new ObjectInputStream(new GZIPInputStream(new FileInputStream(savedirectory)));
MapContainingPlayerMappedVariables.putAll((Map)os.readObject());
}

 

Remember to encompass file saving/loading in a try catch block in case there are write errors.

  • Quote

Share this post


Link to post
Share on other sites

hkiller1    1

hkiller1

hkiller1    1

  • Stone Miner
  • hkiller1
  • Members
  • 1
  • 52 posts
Posted November 8, 2012

Im just going to use yaml.

  • Quote

Share this post


Link to post
Share on other sites

hkiller1    1

hkiller1

hkiller1    1

  • Stone Miner
  • hkiller1
  • Members
  • 1
  • 52 posts
Posted November 8, 2012

I got the save function to work but im having some trouble with the load function, curently I have

package economy.common;

import java.io.*;
import java.util.*;

import org.yaml.snakeyaml.Yaml;

public class Data {

public static String LoadData(String player) throws IOException {
File file = new File("config/EconomyAcounts.yml");

if(!file.exists()) { 
	try {
		file.createNewFile();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

//TODO: Load file

 Yaml yaml = new Yaml();
 List<String> list = (List<String>) yaml.load(document);

 //TODO: find the amount of money the player has

//return amount
}

public static void SaveData(String player, String amount) throws FileNotFoundException {
File file = new File("config/EconomyAcounts.yml");

if(!file.exists()) { 
	try {
		file.createNewFile();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

    Map<String, Object> data = new HashMap<String, Object>();
    data.put(player, amount);
    Yaml yaml = new Yaml();
    String output = yaml.dump(data);
    PrintWriter out = new PrintWriter(file);
    out.println(output);
    out.close();
    
}

}

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Oliviafrostpaw
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw · Posted 13 minutes ago

      After some poking, the function is not firing whatsoever
    • RaphGamingz
      [1.14.4] Dimensions

      By RaphGamingz · Posted 1 hour ago

      Anyone?
    • RaphGamingz
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By RaphGamingz · Posted 1 hour ago

      Are you sure this is correct, shouldn’t it be  new ResourceLocation(“minecraft”,”grass”) And is the function firing?
    • mervinrasquinha
      Introducing Stonecage

      By mervinrasquinha · Posted 3 hours ago

      If you’re interested in a modpack with a little bit of tech, a little bit of magic, and a lot of adventure, I would love for you to try out my modpack Stonecage. Stonecage takes mods you might have seen before (and many you might not have), and adds a twist to them to make them fresh again. The main gimmick of the pack is that it makes stone unmineable, meaning you’ll have to explore caves and dungeons the way they were intended, and find solutions to problems you never knew existed. It aims to be a hardcore pack that’s more fair than most, and leaves you to seek combat and adventure on your own terms. While you may start out weak, you’ll find magical artifacts known as curios in your adventures that can be crafted into powerful baubles that will help you in combat and elsewhere. You’ll also find pieces of ancient machines that with research can be deciphered and put back together, bringing industry back to a world that has long been without it. The pack is heavily centered around researching these lost machines, and the research table will guide you through the modpack while giving you more freedom than a quest book would. With a small, curated list of mods, most computers will likely be able to run it, and the many, many lines of scripting keep these mods integrated into what feels like a cohesive whole. If this sounds like fun to you, I encourage you to check it out. If you do, I hope you have as much fun playing it as I did creating it.
    • Darth_Cobalt
      Forge 1.14.4 crashes.

      By Darth_Cobalt · Posted 4 hours ago

      Hi I just downloaded forge and every time i try to load it, it crashes. I have tried 1.14.4 - 28.1.0 and 1.14.4 - 28.1.106. It crashes with both versions. I couldn't figure out how to upload the crash files, could somebody explain how I can do that?
  • Topics

    • Oliviafrostpaw
      7
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw
      Started December 8

    • RaphGamingz
      1
      [1.14.4] Dimensions

      By RaphGamingz
      Started Yesterday at 07:45 AM

    • mervinrasquinha
      0
      Introducing Stonecage

      By mervinrasquinha
      Started 3 hours ago

    • Darth_Cobalt
      0
      Forge 1.14.4 crashes.

      By Darth_Cobalt
      Started 4 hours ago

    • leonardsores
      0
      Fun mod interactions

      By leonardsores
      Started 4 hours ago

  • Who's Online (See full list)

    • Oliviafrostpaw
    • xerca
    • Redstoneguy129
    • Raelsyl
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • storing values in an external file
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community