Jump to content

[SOLVED] [1.11.2] Random leaf colors


Daeruin

Recommended Posts

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Try deliting feur builder  It caused this issue in my modpack
    • I am not using hardcoded recipes, I'm using Vanilla's already existing code for leather armor dying. (via extending and implementing DyeableArmorItem / DyeableLeatherItem respectively) I have actually figured out that it's something to do with registering item colors to the ItemColors instance, but I'm trying to figure out where exactly in my mod's code I would be placing a call to the required event handler. Unfortunately the tutorial is criminally undescriptive. The most I've found is that it has to be done during client initialization. I'm currently trying to do the necessary setup via hijacking the item registry since trying to modify the item classes directly (via using SubscribeEvent in the item's constructor didn't work. Class so far: // mrrp mrow - mcmod item painter v1.0 - catzrule ch package catzadvitems.init; import net.minecraft.client.color.item.ItemColors; import net.minecraft.world.item.Item; import net.minecraftforge.registries.ObjectHolder; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.client.event.ColorHandlerEvent; import catzadvitems.item.DyeableWoolArmorItem; @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class Painter { @ObjectHolder("cai:dyeable_wool_chestplate") public static final Item W_CHEST = null; @ObjectHolder("cai:dyeable_wool_leggings") public static final Item W_LEGS = null; @ObjectHolder("cai:dyeable_wool_boots") public static final Item W_SOCKS = null; public Painter() { // left blank, idk if forge throws a fit if constructors are missing, not taking the chance of it happening. } @SubscribeEvent public static void init(FMLClientSetupEvent event) { new Painter(); } @Mod.EventBusSubscriber private static class ForgeBusEvents { @SubscribeEvent public static void registerItemColors(ColorHandlerEvent.Item event) { ItemColors col = event.getItemColors(); col.register(DyeableUnderArmorItem::getItemDyedColor, W_CHEST, W_LEGS, W_SOCKS); //placeholder for other dye-able items here later.. } } } (for those wondering, i couldn't think of a creative wool helmet name)
    • nvm found out it was because i had create h and not f
    • Maybe there's something happening in the 'leather armor + dye' recipe itself that would be updating the held item texture?
    • @SubscribeEvent public static void onRenderPlayer(RenderPlayerEvent.Pre e) { e.setCanceled(true); model.renderToBuffer(e.getPoseStack(), pBuffer, e.getPackedLight(), 0f, 0f, 0f, 0f, 0f); //ToaPlayerRenderer.render(); } Since getting the render method from a separate class is proving to be bit of a brick wall for me (but seems to be the solution in older versions of minecraft/forge) I've decided to try and pursue using the renderToBuffer method directly from the model itself. I've tried this route before but can't figure out what variables to feed it for the vertexConsumer and still can't seem to figure it out; if this is even a path to pursue.  The vanilla model files do not include any form of render methods, and seem to be fully constructed from their layer definitions? Their renderer files seem to take their layers which are used by the render method in the vanilla MobRenderer class. But for modded entities we @Override this function and don't have to feed the method variables because of that? I assume that the render method in the extended renderer takes the layer definitions from the renderer classes which take those from the model files. Or maybe instead of trying to use a render method I should be calling the super from the renderer like   new ToaPlayerRenderer(context, false); Except I'm not sure what I would provide for context? There's a context method in the vanilla EntityRendererProvider class which doesn't look especially helpful. I've been trying something like <e.getEntity(), model<e.getEntity()>> since that generally seems to be what is provided to the renderers for context, but I don't know if it's THE context I'm looking for? Especially since the method being called doesn't want to take this or variations of this.   In short; I feel like I'm super super close but I have to be missing something obvious? Maybe this insane inane ramble post will provide some insight into this puzzle?
  • Topics

×
×
  • Create New...

Important Information

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