Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use direct types instead of getters #135

Merged
merged 4 commits into from
Aug 19, 2024

Conversation

svaningelgem
Copy link
Contributor

@svaningelgem svaningelgem commented Aug 19, 2024

Proposal for new configuration format.

I like this for several reasons:

  1. it's not all static anymore (ok, I need the static instance because of the way the library is built up)
  2. the types are directly set, no need for getters anymore. So compile-time enforcement of the types.
  3. the config file can have comments (yey!)
  4. the upgrade of the configuration file takes into account the current entries and seamlessly is able to upgrade without the TConfigLoader class.

I had some issues with some floats that should be doubles (like for example 0.3, that's not representable as a float in java due to the way the bits are set in memory). In these circumstances, in the yaml-annotation-loader, it's enforced to be a double. So I made it a double, but converted it back to a float where needed as I don't think it mattered too much in the end.

Personally I do like double better too because the calculations are more accurate. (but that's just subjective)

the config file looks now like this:

  # Toggle this for no more villages, ruined portals, ...
  structures: false
  # Small lanterns, barrels, chests, ...
  decorations: true
# What language file should be used?
lang: eng.yml

Code equivalent:

    @YamlComment("Toggle this for no more villages, ruined portals, ...")
    @YamlKey("feature_toggle.structures")
    public boolean FEATURE_STRUCTURES_ENABLED = true;
    @YamlComment("Small lanterns, barrels, chests, ...")
    @YamlKey("feature_toggle.decorations")
    public boolean FEATURE_DECORATIONS_ENABLED = true;

    // Extras
    @YamlComment("What language file should be used?")
    @YamlKey("lang")
    public String LANGUAGE_FILE = "eng.yml";

@Hex27
Copy link
Owner

Hex27 commented Aug 19, 2024

Several places were demoted to float to save memory space, but I can't quite remember where. It should be in caches for things where the increased double precision doesn't matter.

As an additional question, does this break existing configs?

@svaningelgem
Copy link
Contributor Author

svaningelgem commented Aug 19, 2024

Several places were demoted to float to save memory space, but I can't quite remember where. It should be in caches for things where the increased double precision doesn't matter.

Only the config-object holds the doubles. So that's just the one instance. All other circumstances haven't changed.
These are the ones that I changed to double:

    public float BIOME_MOUNTAINOUS_FREQUENCY = 0.3f;
    public float BIOME_CAVE_CRYSTALLINECLUSTER_MAXPERTUB = 0.37f;
    public float BIOME_CAVE_DRIPSTONECLUSTER_MAXPERTUB = 0.35f;
    public float BIOME_CAVE_LUSHCLUSTER_MAXPERTUB = 0.35f;

But when used, I change these (new doubles) back to float:

public enum CaveClusterRegistry {
    LUSH(9527213,
            TConfig.c.BIOME_CAVE_LUSHCLUSTER_SEPARATION,
            (float)TConfig.c.BIOME_CAVE_LUSHCLUSTER_MAXPERTUB
    ), DRIPSTONE(5902907,
            TConfig.c.BIOME_CAVE_DRIPSTONECLUSTER_SEPARATION,
            (float)TConfig.c.BIOME_CAVE_DRIPSTONECLUSTER_MAXPERTUB
    ), CRYSTALLINE(4427781,
            TConfig.c.BIOME_CAVE_CRYSTALLINECLUSTER_SEPARATION,
            (float)TConfig.c.BIOME_CAVE_CRYSTALLINECLUSTER_MAXPERTUB
    ), FLUID(79183628, 40, 0.2f),
    ;

and

    public @NotNull FastNoise getMountainousNoise() {
        return NoiseCacheHandler.getNoise(this, NoiseCacheEntry.TW_MOUNTAINOUS, tw -> {
            FastNoise n = new FastNoise((int) tw.getSeed() * 73);
            n.SetNoiseType(NoiseType.Simplex);
            n.SetFrequency((float)TConfig.c.BIOME_MOUNTAINOUS_FREQUENCY);
            return n;
        });
    }

These changes are all in commit 3d173027.

As an additional question, does this break existing configs?

Nope, it won't break existing configs. It will automatically use the exact same structure as existing configs (it's all yaml after all).
So completely transparent to the end-user.

@Hex27 Hex27 merged commit 955d3c9 into Hex27:master Aug 19, 2024
1 check passed
@svaningelgem svaningelgem deleted the use-direct-types-instead-of-getters branch August 19, 2024 08:20
@Hex27
Copy link
Owner

Hex27 commented Aug 19, 2024

Ah those floats are because the FastNoise library was implemented with floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants