Jump to content

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


CoalOres

Recommended Posts

I've had a request for a mod in which a user can execute a command "/wparty add user" and it will add that user to a text file (created in the same directory as the mod, in the mods folder), every time the command is executed the name needs to be written a line down. There also needs to be a "/wparty clear" and "/wparty remove user". Then, when the user executed "/wparty party", it takes those names and executes a command in bulk with a small delay between them "/party user". Note the /party is a command on the server I am using it on, and so all I need to do is send a message through the player (easy).

 

This is the beginnings of my attempt:

 

WhitelistAdd Command:

package com.simpleafk;

import java.io.BufferedOutputStream;
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.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 <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 {

		String user = args[1];
		if (args[0] == "add") {

			if (user.length() == 0){

			}
			else {
				try {
			FileWriter fw = new FileWriter("wpartylist.txt");
			 fw.write(user);


			fw.close();
		}
				catch (IOException x){

				}
		}
		}



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

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

		}
}
}
}

 

This is a CLIENT side mod.

 

 

Any help would be appreciated, thanks in advance!

Link to comment
Share on other sites

Load to a ArrayList from the File and write to that file, look up how to write to a txt file in Java.

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.

Link to comment
Share on other sites

Load to a ArrayList from the File and write to that file, look up how to write to a txt file in Java.

I've tried quite a few things but none of it seems to work. I don't know where to specify the path for the file, I want to create it in the same directory as the mods. Could you give me a quick tutorial?

Link to comment
Share on other sites

Poof a tutorial thanks google.

https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html

Look at the tutorial, until you understand it. Then edit it to your desires.

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.

Link to comment
Share on other sites

That was just a guess, but what you will want to do is make a String set it equal to preevent.getSourceFile().getAbsolutePath()

And use something like this to get rid of the file name on the end then add your own file name to the end.

File file = File("C:\\abcfolder\\textfile.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.
    substring(0,absolutePath.lastIndexOf(File.separator));

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.

Link to comment
Share on other sites

That was just a guess, but what you will want to do is make a String set it equal to preevent.getSourceFile().getAbsolutePath()

And use something like this to get rid of the file name on the end then add your own file name to the end.

File file = File("C:\\abcfolder\\textfile.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.
    substring(0,absolutePath.lastIndexOf(File.separator));

File(String) method is undefined for my WhitelistAdd command. How do I implement this?

Link to comment
Share on other sites

That is not what I meant make a static variable in your main mod class, then initialize it like I told you to.

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.

Link to comment
Share on other sites

That is not what I meant make a static variable in your main mod class, then initialize it like I told you to.

"preevent cannot be resolved"

 

This error without any code is meaningless.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Actually I understand

// Use this method
preInit(FMLPreInitializationEvent event)
//                                               ^ preevent

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.

Link to comment
Share on other sites

That is not what I meant make a static variable in your main mod class, then initialize it like I told you to.

"preevent cannot be resolved"

 

This error without any code is meaningless.

 

package com.simpleafk;

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 absolutePath = preevent.getSourceFile().getAbsolutePath();

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

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {

    	MinecraftForge.EVENT_BUS.register(new ChatListener());
    	
    }

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

Link to comment
Share on other sites

Oh jesus christ.  You can't just do this:

public static String absolutePath = preevent.getSourceFile().getAbsolutePath();

 

Also, "preevent" doesn't even exist in your event methods.

 

public static String absolutePath;

public void preInit(FMLPreInitializationEvent event) {
    absolutePath = event.getSourceFile().getAbsolutePath();
}

 

Go learn Java.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Oh jesus christ.  You can't just do this:

public static String absolutePath = preevent.getSourceFile().getAbsolutePath();

 

Also, "preevent" doesn't even exist in your event methods.

 

public static String absolutePath;

public void preInit(FMLPreInitializationEvent event) {
    absolutePath = event.getSourceFile().getAbsolutePath();
}

 

Go learn Java.

Sorry I must have misread the title of this forum thread, I was fairly sure it said "Modder Support".

Link to comment
Share on other sites

Oh jesus christ.  You can't just do this:

public static String absolutePath = preevent.getSourceFile().getAbsolutePath();

 

Also, "preevent" doesn't even exist in your event methods.

 

public static String absolutePath;

public void preInit(FMLPreInitializationEvent event) {
    absolutePath = event.getSourceFile().getAbsolutePath();
}

 

Go learn Java.

Sorry I must have misread the title of this forum thread, I was fairly sure it said "Modder Support".

Though if you don't know Java we won't teach it, there are plenty of tutorials out there for that. Now the last step is to modify the snippet of code I gave to suit  your own needs if you run into any trouble after that come back.

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.

Link to comment
Share on other sites

Sorry I must have misread the title of this forum thread, I was fairly sure it said "Modder Support".

 

From the description of this section:

This is the support section for those modding with Forge. Help with modding goes in here, however, please keep in mind that this is not a Java school. You are expected to have basic knowledge of Java before posting here.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Oh jesus christ.  You can't just do this:

public static String absolutePath = preevent.getSourceFile().getAbsolutePath();

 

Also, "preevent" doesn't even exist in your event methods.

 

public static String absolutePath;

public void preInit(FMLPreInitializationEvent event) {
    absolutePath = event.getSourceFile().getAbsolutePath();
}

 

Go learn Java.

Sorry I must have misread the title of this forum thread, I was fairly sure it said "Modder Support".

Though if you don't know Java we won't teach it, there are plenty of tutorials out there for that. Now the last step is to modify the snippet of code I gave to suit  your own needs if you run into any trouble after that come back.

 

I can't create the substring for some reason but this is my code:

 

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.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 <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 {

		String user = args[1];
		if (args[0] == "add") 



			if (user.length() == 0){

			}
			else {
				// The name of the file to open.
		        String fileName = "whitelist.txt";
		        String filePath = AFKMain.absolutePath + "\\whitelist.txt";
		        

		        try {
		            // Assume default encoding.
		            FileWriter fileWriter =
		                new FileWriter(filePath);

		            // 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();
		        }
		        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"){

		}
}
}
}

 

The rest is from the tutorial you linked me to. It still does not create a file anywhere.

Link to comment
Share on other sites

Show me the preInit

@EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
    	
    	 absolutePath = event.getSourceFile().getAbsolutePath();
    	MinecraftForge.EVENT_BUS.register(new ChatListener());
    	
    }

Link to comment
Share on other sites

You did not implement this

File file = File("C:\\abcfolder\\textfile.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
// ^Important part

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.

Link to comment
Share on other sites

You did not implement this

File file = File("C:\\abcfolder\\textfile.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
// ^Important part

 

So I've added this to the processCommand:

 

File file = new File(AFKMain.absolutePath + "\whitelist.txt");
	String absolutePath = file.getAbsolutePath();
	String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));

 

What else do I need to do, I wouldn't have thought it would be this hard :(

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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