Jump to content

Recommended Posts

Posted

This is my first time making a mod in Minecraft so I'm a little bit of a noob when it comes to all of this. Just as a proof of concept I wanted to make a mod that would see a mathematical expression in the chat and then make my user type the answer back. I imported this library mX parser in order to evaluate the mathematical expression. It was all going well in my dev version and working just as I wanted, so I built the mod and put it into my mods folder. I was absolutely ecstatic to see that my game hadn't crashed but unfortunately when I got into the game and tested to see if it would work it didn't. I narrowed it to down to being an issue with the library just not evaluating the function but I'm not sure why. 

 

This is the code:

import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.mariuszgromada.math.mxparser.Expression;
import org.mariuszgromada.*;


@Mod(modid = TestMod.MODID, version = TestMod.VERSION)
public class TestMod {

	public static final String MODID = "TestMod";
    public static final String VERSION = "1.0";
    @EventHandler
    public void init(FMLInitializationEvent event) {
        FMLCommonHandler.instance().bus().register(this);
        MinecraftForge.EVENT_BUS.register(this);
    }
    
    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent event) throws Exception {
    	String message = event.message.getUnformattedText();
    	
    	if (message.contains("Solve: ")) {
    		String[] splitArr = message.split(": ",2);
    	    System.out.println(splitArr[1]);
    	    Expression e = new Expression(splitArr[1]);
    	    double result = e.calculate();
    	    double waitTime = Math.random()+1;
    	    Thread.sleep((long)waitTime*1000);
    	    if (result - (int)result == 0) {
    	    	
    	    	Minecraft.getMinecraft().thePlayer.sendChatMessage(String.valueOf((int)result));
    	    } else {
    	    	Minecraft.getMinecraft().thePlayer.sendChatMessage(String.valueOf(result));
    	    }
    	}
    }
}

 

Posted

1.8 is no longer supported on this forum due to its age.

Please update to a modern version of Minecraft to receive support.

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

This is my first time making a mod in Minecraft so I'm a little bit of a noob when it comes to all of this. Just as a proof of concept I wanted to make a mod that would see a mathematical expression in the chat and then make my user type the answer back. I imported this library mX parser in order to evaluate the mathematical expression. It was all going well in my dev version and working just as I wanted, so I built the mod and put it into my mods folder. I was absolutely ecstatic to see that my game hadn't crashed but unfortunately when I got into the game and tested to see if it would work it didn't. I narrowed it to down to being an issue with the library just not evaluating the function but I'm not sure why. 

 

This is the code:

import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.mariuszgromada.math.mxparser.Expression;
import org.mariuszgromada.*;


@Mod(modid = TestMod.MODID, version = TestMod.VERSION)
public class TestMod {

	public static final String MODID = "TestMod";
    public static final String VERSION = "1.0";
    @EventHandler
    public void init(FMLInitializationEvent event) {
        FMLCommonHandler.instance().bus().register(this);
        MinecraftForge.EVENT_BUS.register(this);
    }
    
    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent event) throws Exception {
    	String message = event.message.getUnformattedText();
    	
    	if (message.contains("Solve: ")) {
    		String[] splitArr = message.split(": ",2);
    	    System.out.println(splitArr[1]);
    	    Expression e = new Expression(splitArr[1]);
    	    double result = e.calculate();
    	    double waitTime = Math.random()+1;
    	    Thread.sleep((long)waitTime*1000);
    	    if (result - (int)result == 0) {
    	    	
    	    	Minecraft.getMinecraft().thePlayer.sendChatMessage(String.valueOf((int)result));
    	    } else {
    	    	Minecraft.getMinecraft().thePlayer.sendChatMessage(String.valueOf(result));
    	    }
    	}
    }
}

 

Posted

As I told you before, 1.8 is not supported on this forum due to its age.

Making a new thread that is exactly the same as the old one does not change this fact.

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

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.