Jump to content

Logging Player's Velocity And Time


thisisatest

Recommended Posts

I've made a player "springboard" that launches them into the air. I want to log the player's velocity every 5 ticks and log it all neatly in a text file after which you could plug the data in Logger Pro and graph it. I am having trouble doing this and as a novice programmer have this mess below:

 

public void onLanded(World worldIn, Entity entityIn)
	    {
	        if (entityIn.isSneaking())
	        {
	            super.onLanded(worldIn, entityIn);
	        }
	        else if (entityIn.motionY < 0.0D)
	        {
	        	entityIn.motionY = 2;
	        	entityIn.motionX = 0;
	        		        	
	        	try{
					File file = new File("data_log.txt"); 
				
					if(!file.exists()) {
						file.createNewFile();
					}
				FileWriter fw = new FileWriter(file); 
				PrintWriter pw = new PrintWriter(fw);

				pw.println(entityIn.motionY);
				//World world = Minecraft.getMinecraft().world;
				
				//double start = world.getTotalWorldTime();
				//while(entityIn.motionY != 0) {
				//if(world != null && (world.getWorldTime() - start) % 5 == 0) {
					pw.println(entityIn.motionY);
				//}
				//}
				pw.close();
				
	        	}
				
				catch(IOException e) {e.printStackTrace();}

	            if (!(entityIn instanceof EntityLivingBase))
	            {
	            	entityIn.motionY *= 0.0D;
                }}}

 

~The commented sections cause a crash~

Link to comment
Share on other sites

Block#onLanded will be called on the server side.

Calling Minecraft.getMinecraft().world on the server side will cause a crash.

Instead, you should use the instance of World given in the parameter of Block#onLanded.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Thank for you for your response; I followed your advice. On execution, however, the while loop produced, for a few seconds before crashing, a 1.57GB file filled with lines stating the player's initial assigned velocity (2.0). To fix this, I think I would like to have this velocity measurement run every 5 ticks or so. I've read about Forge's TickEvent - I frankly don't know how to use it though.

 

Here is my code:

 

	    public void onLanded(World worldIn, Entity entityIn)
	    {
	        if (entityIn.isSneaking())
	        {
	            super.onLanded(worldIn, entityIn);
	        }
	        else if (entityIn.motionY < 0.0D)
	        {
	        	entityIn.motionY = 2;
	        	entityIn.motionX = 0;
	        		        	
	        	try{
					File file = new File("data_log.txt"); 
				
					if(!file.exists()) {
						file.createNewFile();
					}
				FileWriter fw = new FileWriter(file); 
				PrintWriter pw = new PrintWriter(fw);

				double start = worldIn.getTotalWorldTime();
				
				while(entityIn.collidedVertically != true) {
				if(worldIn != null && (worldIn.getWorldTime() - start) % 5 == 0) {
					pw.println(entityIn.motionY);
				}
				}
				pw.close();
				
	        	}
				
				catch(IOException e) {e.printStackTrace();}

	            if (!(entityIn instanceof EntityLivingBase))
	            {
	            	entityIn.motionY *= 0.0D;
	            }
	            }
                }

 

Link to comment
Share on other sites

This is not how anything works. While blocks execution untill it resolves(as literally any other statement). 

So not only are you not writing your line each tick(you are writing it as many times a second as your PC can handle actually, whoch explains the size of the log) your loop wouldn't ever exit, so you basically have a while(true) here.

All of this is basic java.

1 hour ago, thisisatest said:

I've read about Forge's TickEvent - I frankly don't know how to use it though.

You MUST use the tick event for this purpose.

https://mcforge.readthedocs.io/en/latest/events/intro/

Link to comment
Share on other sites

On 5/27/2019 at 9:19 AM, thisisatest said:

after which you could plug the data in Logger Pro and graph it

Just out of curiosity, are you doing this for physics class (IGCSE Physics or something)?

 

You could mark the player that used the springboard, and in your subscriber for TickEvent.PlayerTickEvent (can't remember the spelling), check if the player if marked by the springboard and log his/her position accordingly.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

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.