Need help with an encoding/decoding algorithm for procedures

Started by Black_hole on

Topic category: Help with modding (Java Edition)

Last seen on 20:03, 3. Jul 2023
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Need help with an encoding/decoding algorithm for procedures

Hello guys.

"I want to create a Waystone algorithm with an encoding and decoding function using a procedure. Here's what I have done so far for the encoding:

The algorithm should take the X position of the player, then take the lengh of the number, and combine both as a single number. It then do the same for the Y and Z values, before adding "dim" and finally the "dimension". What I want to do is encode the position. Having one variable per coordinate means that we would need three variables per waypoint (and therefore per waystone). I want to encode them in a simple way. All we do is take the length of the coordinate and put it in front of the coordinate itself. If the number is negative, we just add a '0' before the length. So, for example, if we have X = 273.75, the encoded version of X would be '6273.75' because '273.75' has 6 characters. We repeat this process for each variable and put it all together. Therefore, for X = 273.75, Y = -42, and Z = -300.12, the encoding would be '6273.75034207300.12' and that's it. Note that the encoding is in string format, while the coordinates are in float.

 

For decoding, we do the opposite. Let's take this example: "6273.75034207300.12" The first digit "6" represents the length of the X variable, so we read the following six digits, which span over six characters. Therefore, X = 273.75. Next, we move on to the next set of digits. We observe a "0", indicating that the number must be negative. Then we see a "3", which means the first symbol is "-". The remaining two digits form the value of Y, which is "-42". We repeat the process for Z. We encounter a "0", indicating it is negative. Then we find a "7", and since we know the first symbol is "-", there are only six remaining symbols. Hence, Z = -300.12. It's so simple yet so hard to encode within Mcreator...

 

Can somebody helps me with the decoding part? It's getting really messy in my head.

Last seen on 20:18, 12. Mar 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This seems like an overly…
Thu, 06/29/2023 - 20:24

This seems like an overly complicated format. Why don't you seperate each item with spaces?

In this case, storing a list of positions would look like:

273.75 -42 -300.12 minecraft:overworld -19.21 102 19.6 minecraft:the_end

This string would represent:

  1. X=273.75, Y=-42, Z=-300.12 in the Overworld
  2. X=-19.21, Y=102, Z=19.6 in the End

In this case, you can simply loop over the string split with spaces, and take every fourth item. If you wanted it to be even simpler, you could seperate multiple entries with a different seperator:

273.75 -42 -300.12 minecraft:overworld_-19.21 102 19.6 minecraft:the_end

I can provide Java code if you want it.

Last seen on 20:03, 3. Jul 2023
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yes provide please.
Thu, 06/29/2023 - 23:10

Yes provide please.

Last seen on 20:03, 3. Jul 2023
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
But I believe I have…
Thu, 06/29/2023 - 23:18

But I believe I have understood your point. The reason I don't separate the values with spaces is because I want to minimize memory usage. My intention is to add this mod to my server, and I want it to be memory-friendly.

Last seen on 20:18, 12. Mar 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I think you will be fine…
Sat, 07/01/2023 - 19:30

I think you will be fine with the memory usage. The only times I have encountered an out-of-memory error is when I am doing crazy recursive things.

Anyways, here's some code:

class EncodingProcedure {
	public double x;
	public double y;
	public double z;
	public String dimension;
	public EncodingProcedure(double x, double y, double z, String dimension) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.dimension = dimension;
	}
	public String encode() {
		return "" + this.x + " " + this.y + " " + this.z + " " + this.dimension;
	}
	public static EncodingProcedure decode(String i) {
		String[] s = i.split(" ", 0);
		return new EncodingProcedure(Double.parseDouble(s[0]), Double.parseDouble(s[1]), Double.parseDouble(s[2]), s[3]);
	}
}

To use this, create and save a new procedure called "Encoding", then press "Lock/unlock element code", and then double‐click on the procedure again. (Don't erase the "package com.mcreator..." line at the beginning.)

  • To encode: Use the custom code snippet (new net.mcreator.yourmodnamehere.procedures.EncodingProcedure(x, y, z, dimension)).encode() Then you can use this in any text-related block.
  • To decode: net.mcreator.yourmodnamehere.procedures.EncodingProcedure location = net.mcreator.yourmodnamehere.procedures.EncodingProcedure.decode(YOURTEXTVARIABLENAME); Then to get the x, y, z, and dimension properties, use location.x location.y location.z location.dimension
Last seen on 20:03, 3. Jul 2023
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
thanks you 👍
Mon, 07/03/2023 - 18:54

thanks you 👍