Jump to content

Recommended Posts

Posted (edited)

I'm attempting to add seasons to my mod. I thought I was being clever by registering a new block color handler for vanilla leaves that randomizes the leaf color, so every leaf block has a different color. Each season has its own palette of colors to choose randomly from. The fall leaves look fantastic! I update the leaf block colors every time the season changes by forcing Minecraft to reload the renderers, via RenderGlobal#loadRenderers, in a ClientTickEvent.

 

The issue I didn't foresee is that leaves tend to change color randomly all throughout the day, even without me reloading the renderers. I guess there's something in the client that's trying to look up the block color at certain points. I'm not sure what it is. Was my idea just a bad one? Or is there some way to salvage it? Maybe there's some way for me to ensure the same "random" color is always chosen within the same season for a given block? Or store which color is chosen somehow?

 

Code (I chopped out some of the seasons enum for brevity):

 

Spoiler

        blockColors.registerBlockColorHandler(new IBlockColor()
        {
            @Override
            public int colorMultiplier(IBlockState state, @Nullable IBlockAccess blockAccess, @Nullable BlockPos pos, int tintIndex)
            {
                if (blockAccess == null || pos == null)
                {
                    return ColorizerFoliage.getFoliageColorBasic();
                }

                BlockPlanks.EnumType leafType = state.getValue(BlockOldLeaf.VARIANT);
                PrimalTime.Month month = PrimalTime.getMonth(MINECRAFT.world.getTotalWorldTime());

                return month.getRandomLeafColor(leafType);
            }
        }, Blocks.LEAVES);

 


    public enum Month
    {
        EARLY_SPRING("Early Spring", new int[]{13693083, 8235823, 9093906, 11461411, 16046127}, new int[]{16643845, 13360128, 15857547, 11516964, 11703159}, new int[]{5478471, 1402894, 4680230, 5536279}, new int[]{10788864, 15522940, 8795965, 9341257, 16119796}, new int[]{10599766, 10270732, 12439591, 15790414, 14671277}),
        MID_SPRING("Mid-Spring", new int[]{8624642, 13031424, 16769285, 16772867, 16046127}, new int[]{16643845, 13360128, 15857547, 11516964, 6795023}, new int[]{1402894, 2573336, 2638875, 4680230, 5536279}, new int[]{14418458, 11547439, 15121083, 8795965, 10788864}, new int[]{10599766, 10270732, 7460875, 6603561, 15790414});

        private final String monthName;
        private int[] acaciaLeafColors;
        private int[] birchLeafColors;
        private int[] darkOakLeafColors;
        private int[] jungleLeafColors;
        private int[] oakLeafColors;
        private Random random = new Random();

        Month(String monthName, int[] acaciaLeafColors, int[] birchLeafColors, int[] darkOakLeafColors, int[] jungleLeafColors, int[] oakLeafColors)
        {
            this.monthName = monthName;
            this.acaciaLeafColors = acaciaLeafColors;
            this.birchLeafColors = birchLeafColors;
            this.darkOakLeafColors = darkOakLeafColors;
            this.jungleLeafColors = jungleLeafColors;
            this.oakLeafColors = oakLeafColors;
        }

        @Override
        public String toString()
        {
            return this.monthName;
        }

        public String getNameByIndex(int index)
        {
            return Month.values()[index].toString();
        }

        public int getRandomLeafColor(BlockPlanks.EnumType leafType)
        {
            if (leafType == BlockPlanks.EnumType.ACACIA)
                return this.acaciaLeafColors[random.nextInt(acaciaLeafColors.length)];
            else if (leafType == BlockPlanks.EnumType.BIRCH)
                return this.birchLeafColors[random.nextInt(birchLeafColors.length)];
            else if (leafType == BlockPlanks.EnumType.DARK_OAK)
                return this.darkOakLeafColors[random.nextInt(darkOakLeafColors.length)];
            else if (leafType == BlockPlanks.EnumType.JUNGLE)
                return this.jungleLeafColors[random.nextInt(jungleLeafColors.length)];
            else if (leafType == BlockPlanks.EnumType.OAK)
                return this.oakLeafColors[random.nextInt(oakLeafColors.length)];
            else if (leafType == BlockPlanks.EnumType.SPRUCE)
            {
                return ColorizerFoliage.getFoliageColorPine();
            }
            else
                return ColorizerFoliage.getFoliageColorBasic();
        }

    }

 

 

Edited by Daeruin
Marking solved.
Posted

I do it like this in one of my mods:

Random r = new Random(pos.toLong());

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

OK, one wrinkle with this, I guess because of how the math works out. If I only provide four leaf colors to choose randomly from, and use the BlockPos as the random seed, every block along the same Z axis ends up the same color. Easily solved by providing one extra color, but something to be aware of if someone is reading this later.

Posted
1 hour ago, diesieben07 said:

Check out MathHelper.getPositionRandom. It does some bit-mangling to avoid issues like this.

Oh cool, I'll start using this. I don't think my usage had an observable pattern like that, but I didn't know this existed.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I should not be trying to code when it's this late. I need to choose randomly among four or five options. getPositionRandom returns a long. I don't understand what it's doing to generate the random number, or if it could potentially generate a negative number. But I guess I need to do some maths like this?

 

Math.abs(MathHelper.getPositionRandom(pos)) % 5;

 

Posted
9 hours ago, Daeruin said:

I should not be trying to code when it's this late. I need to choose randomly among four or five options. getPositionRandom returns a long. I don't understand what it's doing to generate the random number, or if it could potentially generate a negative number. But I guess I need to do some maths like this?

 


Math.abs(MathHelper.getPositionRandom(pos)) % 5;

 

Yep, that's sufficient. There may be a (very small) bias, but it's not worth worrying about.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Now that I'm doing it right, there is still a noticeable pattern: the colors form a 4x4 grid along the Y axis. However, it isn't that big of a deal since I decided I want to add more colors anyway.

Posted

Yeah I had that problem in 1.7 (where I did my own randomization) before finding a satisfactory result.  And I was working with 11 different sprites!

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
On 1/31/2018 at 7:53 AM, diesieben07 said:

Check out MathHelper.getPositionRandom. It does some bit-mangling to avoid issues like this.

Turns out that MathHelper.getPositionRandom is SideOnly(CLIENT), which makes it (directly) unusable for me.

Ah, but that's just a wrapper function to getCoordinateRandom, which isn't sided. OTOH, I pass my Random instance into a method to get a random position within a circle, followed by several other random calls for a few other things,* so I do actually need a full random instance.

 

*I get the unit circle, multiple by a radius, offset this from the original position, then make up to 8 more [random inside-unit-circle] * [smaller distance] polls, in order to create a randomly placed tight cluster of flowers (plus sometimes I need a boolean random for if the plant can be two-blocks-tall).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.