Jump to content

Recommended Posts

Posted

I may just be going about this all wrong, but I am trying to get a reference to the name of the world so that I can write a text file that loads with each world. I can get the coordinates.txt file to appear in the directory above the 'saves' folder, but I need it to be different for each world. Each new map I make loads the same coordinates file created from the last world. Here is the java file im using the code in.

 

package com.exline.dbzmod;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import com.exline.dbzmod.block.ModWorldGen;
import com.exline.dbzmod.block.ModWorldGenAgain;

import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerList;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraft.world.WorldSavedDataCallableSave;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.ModClassLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.server.FMLServerHandler;

//find nearest dragonball
//this just updates the radar coordinates when called
//called in DragonBallEventHandler when corresponding block is destroyed

public class FindNearest 
{
	public static int xto;
	public static int zto;
	
	public static void init() throws Exception
	{
		xto = ModWorldGen.x0;
		zto = ModWorldGen.z0;
		String dir;

		MinecraftServer mc = FMLCommonHandler.instance().getMinecraftServerInstance();
		if (mc != null)
		{
			dir = mc.getFolderName();
		}
		else
		{
			dir = null;
		}
		
		if (dir != null)
		{
			File varTmpDir = new File("/saves/"+dir+"/coordinates.txt");
			boolean exists = varTmpDir.exists();
		
			if(exists != true) 
			{
				saveCoordinates();
			}
			else
			{
				loadCoordinates();
			}
		}
	}
	
	public static void findOne() throws Exception
	{
		xto = ModWorldGen.x1;
		zto = ModWorldGen.z1;
		
		saveCoordinates();
	}
	
	public static void findTwo() throws Exception
	{
		xto = ModWorldGen.x2;
		zto = ModWorldGen.z2;
		
		saveCoordinates();
	}
	
	public static void findThree() throws Exception
	{
		xto = ModWorldGen.x3;
		zto = ModWorldGen.z3;
		
		saveCoordinates();
	}
	
	public static void findFour() throws Exception
	{
		xto = ModWorldGen.x4;
		zto = ModWorldGen.z4;
		
		saveCoordinates();
	}
	
	public static void findFive() throws Exception
	{
		xto = ModWorldGen.x5;
		zto = ModWorldGen.z5;
		
		saveCoordinates();
	}
	
	public static void findSix() throws Exception
	{
		xto = ModWorldGen.x6;
		zto = ModWorldGen.z6;
		
		saveCoordinates();
	}
	
	public static void findSeven() throws Exception
	{
		//this method resets the dragon balls positions and re-spawns them in the world
		
		ModWorldGen.x0 += 120;
		ModWorldGen.z0 += 120;
		
		ModWorldGen.x1 -= 120;
		ModWorldGen.z1 -= 120;
		
		ModWorldGen.x2 += 120;
		ModWorldGen.z2 -= 120;
		
		ModWorldGen.x3 -= 120;
		ModWorldGen.z3 += 120;
		
		ModWorldGen.x4 += 120;
		ModWorldGen.z4 -= 120;
		
		ModWorldGen.x5 -= 120;
		ModWorldGen.z5 += 120;
		
		ModWorldGen.x6 -= 300;
		ModWorldGen.z6 += 300;
		
		xto = ModWorldGen.x0;
		zto = ModWorldGen.z0;
		
		saveCoordinates();
		
		GameRegistry.registerWorldGenerator(new ModWorldGenAgain(), 1);
	}
	
	public static void saveCoordinates() throws Exception
	{
		String dir;
		
		MinecraftServer mc = FMLCommonHandler.instance().getMinecraftServerInstance();
		if (mc != null)
		{
			dir = mc.getFolderName();
		}
		else
		{
			dir = null;
		}
		
		if (dir != null)
		{
		
			FileWriter saveFile = new FileWriter("/saves/"+dir+"/coordinates.txt");
			//write data to file now!!!
			saveFile.write("\n");
			saveFile.write(xto + "\n");
			saveFile.write(zto + "\n");
			saveFile.write("\n");
			//close filewriter
			saveFile.close();
		}
	}
	public static void loadCoordinates() throws Exception
	{
		String dir;
		
		MinecraftServer mc = FMLCommonHandler.instance().getMinecraftServerInstance();
		if (mc != null)
		{
			dir = mc.getFolderName();
		}
		else
		{
			dir = null;
		}
		
		if (dir != null)
		{		
			BufferedReader saveFile = new BufferedReader(new FileReader("/saves/"+dir+"/coordinates.txt"));
			// Throw away the blank line at the top.
			saveFile.readLine(); 
			// Get the integer value from the String.
			xto = Integer.parseInt(saveFile.readLine()); 
			zto = Integer.parseInt(saveFile.readLine());
		    
			saveFile.close();
		}
	}
		
}

 

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.