Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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~

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.

 

 

  • Author

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;
	            }
	            }
                }

 

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/

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.

 

 

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.