Setting the location of an entity to move it not working

Started by willk55 on

Topic category: Help with MCreator software

Joined Aug 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Setting the location of an entity to move it not working

I want to move an entity 1/3 of a block in the positive x direction. For some reason using Set location of entity to (x position + 1/3) (y position) (z position) as seen in the image below isn't working. Why not?

https://drive.google.com/file/d/1Nu-3FDQVvfJVvYAk5s1LtlfYxQ10GqYQ/view?usp=sharing

Joined Aug 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I did some experimenting and…
Mon, 10/12/2020 - 17:44

I did some experimenting and found my problem: 1/3 = 0. This is because Java (the language Minecraft runs in) is a strongly typed language. This basically means that when you do things with values they don't change to another type. For example, 1 and 3 are integers, so they don't have a decimal expansion and anything you do with them also can't have a decimal expansion. 1/3 actually does equal 0.333, but the result can't have a decimal, so java rounds the result down to 0. So how do you fix this? Well, you need to give one of the numbers a decimal expansion so that the result can also have a decimal expansion. MCreator's blocky editor automatically removes trailing 0s from numbers, so you'll need to modify the Java code directly. The code I put will compile to:

_ent.setPositionAndUpdate(((entity.getPosX()) + (1 / 3)), (entity.getPosY()), (entity.getPosZ()));

but if I change it to:

_ent.setPositionAndUpdate(((entity.getPosX()) + (1 / 3.0)), (entity.getPosY()), (entity.getPosZ()));

it will work as expected. I think this is a bit strange and unintuitive for people who are new to strongly-typed languages so maybe a later update of MCreator could fix this.