Jump to content

[1.8.9] Creating a txt file, and accessing with a command.


Recommended Posts

Posted
  On 8/18/2016 at 3:19 PM, Animefan8888 said:

You need the variable in your main mod class to the substring stuff.

I'm really confused now, can you just do it with my code and paste it? I'm really not getting what I'm supposed to be doing.

Posted
  On 8/18/2016 at 3:27 PM, Animefan8888 said:

No I can not, if you don't understand you need to learn java.

You have been the most unhelpful person, you aren't even trying to give me a simple explanation. What the heck do you mean by I need the variable in the main class to the substring?

Posted

Sorry I left out the word "be" in that sentence it should have been "You need the variable in your main mod class to be the substring stuff." That means

public void preInit(FMLPreInitializationEvent event) {
      path = event.getSourceFile().getAbsolutePath().substring(0,absolutePath.lastIndexOf(File.separator));

Then go about adding the files name to the end in your Command. Sorry for not being helpful, but if you don't understand Java you need to learn before coming here and asking for help.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
  On 8/18/2016 at 3:39 PM, Animefan8888 said:

Sorry I left out the word "be" in that sentence it should have been "You need the variable in your main mod class to be the substring stuff." That means

public void preInit(FMLPreInitializationEvent event) {
      path = event.getSourceFile().getAbsolutePath().substring(0,absolutePath.lastIndexOf(File.separator));

Then go about adding the files name to the end in your Command. Sorry for not being helpful, but if you don't understand Java you need to learn before coming here and asking for help.

 

It crashes now when trying to run it:

 

 

  Reveal hidden contents

 

 

My code:

Main:

package com.simpleafk;

import java.io.File;
import java.io.IOException;

import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(modid="simpleafk", name= "Simple AFK", version="1.0", clientSideOnly = true)

public class AFKMain
{
public static String path;


//Creating instances.
    @Instance("simpleafk")
    public static AFKMain instance;
    

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
    	
    	 path = event.getSourceFile().getAbsolutePath().substring(0, path.lastIndexOf(File.separator));
    	MinecraftForge.EVENT_BUS.register(new ChatListener());
    	
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	ClientCommandHandler.instance.registerCommand(new SampleCommand());
    	ClientCommandHandler.instance.registerCommand(new WhitelistAdd());
    	
     }
}

 

 

WhitelistAdd:

 

package com.simpleafk;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;

public class WhitelistAdd extends CommandBase {
private static final String FILE_PATH = "";


@Override
public String getCommandName() {
	// TODO Auto-generated method stub
	return "wparty";
}

@Override
public String getCommandUsage(ICommandSender sender) {
	// TODO Auto-generated method stub
	return "wparty <add|remove|clear|list> <username>";
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender icommandsender) {

	return true;
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
	// TODO Auto-generated method stub



	if (args.length == 0){
		sender.addChatMessage(new ChatComponentText("You must specify a valid username!"));
	}
	else {


		if (args[0] == "add") {

				// The name of the file to open.
		        String fileName = "whitelist.txt";

		        

		        try {
		        	String user = args[1];
		        	File file = new File(AFKMain.path + "\\whitelist.txt");
		            if (!file.exists()){
		            	file.createNewFile();
		            }
		            String absolutePath = file.getAbsolutePath();
		        	// Assume default encoding.
		            FileWriter fileWriter =
		                new FileWriter(file.getAbsolutePath());

		            // Always wrap FileWriter in BufferedWriter.
		            BufferedWriter bufferedWriter =
		                new BufferedWriter(fileWriter);

		            // Note that write() does not automatically
		            // append a newline character.
		            bufferedWriter.write(user);
		            
		            bufferedWriter.newLine();
		            
		           

		            // Always close files.
		            bufferedWriter.close();
		            Minecraft.getMinecraft().thePlayer.sendChatMessage("Done Writing! File is at:" );
		        }
		        catch(IOException ex) {
		            System.out.println(
		                "Error writing to file '"
		                + fileName + "'");
		            // Or we could just do this:
		            // ex.printStackTrace();

		        }
			}


		    





		else if (args[0] == "clear"){

		}
		else if (args[0] == "remove"){

		}
}
}
}

 

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.