Jump to content

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


CoalOres

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

 

 

---- Minecraft Crash Report ----

// Who set us up the TNT?

 

Time: 18/08/16 16:46

Description: Initializing game

 

java.lang.NullPointerException: Initializing game

at com.simpleafk.AFKMain.preInit(AFKMain.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:560)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:211)

at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:189)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118)

at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:556)

at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:243)

at net.minecraft.client.Minecraft.startGame(Minecraft.java:451)

at net.minecraft.client.Minecraft.run(Minecraft.java:360)

at net.minecraft.client.main.Main.main(Main.java:116)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

at GradleStart.main(GradleStart.java:26)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at com.simpleafk.AFKMain.preInit(AFKMain.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:560)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:211)

at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:189)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)

at com.google.common.eventbus.EventBus.post(EventBus.java:275)

at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118)

at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:556)

at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:243)

at net.minecraft.client.Minecraft.startGame(Minecraft.java:451)

 

-- Initialization --

Details:

Stacktrace:

at net.minecraft.client.Minecraft.run(Minecraft.java:360)

at net.minecraft.client.main.Main.main(Main.java:116)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

at GradleStart.main(GradleStart.java:26)

 

-- System Details --

Details:

Minecraft Version: 1.8.9

Operating System: Windows 7 (amd64) version 6.1

Java Version: 1.8.0_101, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 843869632 bytes (804 MB) / 1056309248 bytes (1007 MB) up to 1056309248 bytes (1007 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

FML: MCP 9.19 Powered by Forge 11.15.1.1722 4 mods loaded, 4 mods active

States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

UCH mcp{9.19} [Minecraft Coder Pack] (minecraft.jar)

UCH FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.8.9-11.15.1.1722.jar)

UCH Forge{11.15.1.1722} [Minecraft Forge] (forgeSrc-1.8.9-11.15.1.1722.jar)

UCE simpleafk{1.0} [simple AFK] (bin)

Loaded coremods (and transformers):

GL info: ' Vendor: 'ATI Technologies Inc.' Version: '4.5.13399 Compatibility Profile Context 15.200.1062.1004' Renderer: 'AMD Radeon HD 5450'

Launched Version: 1.8.9

LWJGL: 2.9.4

OpenGL: AMD Radeon HD 5450 GL version 4.5.13399 Compatibility Profile Context 15.200.1062.1004, ATI Technologies Inc.

GL Caps: Using GL 1.3 multitexturing.

Using GL 1.3 texture combiners.

Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.

Shaders are available because OpenGL 2.1 is supported.

VBOs are available because OpenGL 1.5 is supported.

 

Using VBOs: Yes

Is Modded: Definitely; Client brand changed to 'fml,forge'

Type: Client (map_client.txt)

Resource Packs:

Current Language: English (US)

Profiler Position: N/A (disabled)

CPU: 2x Intel® Core2 Duo CPU E4600 @ 2.40GHz

 

 

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"){

		}
}
}
}

 

Link to comment
Share on other sites

What do you think  is null on line 30, maybe the variable you haven't initialized yet, that you are trying to use to initialize itself.

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

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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