Jump to content

Recommended Posts

Posted

So as the title states I have a flower that I wish to make glow at night and not during the day. I have made several attempts at this all ending in not working correctly. The code I have now will make it glow at night if placed at night, but continues to glow during the day. If placed during the day, it will not glow at night....

 

Here is the code I have now.

public class BlockTwilightRose extends BlockFlower
{

	public BlockTwilightRose(int i, int j) 
        {
                super(i, j);
                this.setCreativeTab(Trees.tabTreeDeco);
                this.setTickRandomly(true);
        }

        public int getTextureSize()
        {
        	return 128;
        }
        
        public String getTextureFile()
        {
         return "/microjunk/trees/common/aart/HD_flowers.png";
        }
        
        /*public int tickRate()
        {
            return 10;
        }*/
        public int onBlockPlaced(World par1World, int par2, int par3, int par4, int par5, float par6, float par7, float par8, int par9)
        {
        	if (!par1World.isRemote)
            {
                long var6 = par1World.getWorldTime();
        		if(par1World.getBlockLightValue_do(par2, par3, par4,true)<10.0F || (par1World.canBlockSeeTheSky(par2, par3, par4) && var6 > 12000))
        		{
        			//var1.setBlockWithNotify(var2, var3, var4, blockID);
        			this.setLightValue(1F);
        		}
        		else
        		{
        			//var1.setBlockWithNotify(var2, var3, var4, blockID);
        			this.setLightValue(0F);
        		}
            }
            return par9;
        }        
        @Override
        public void updateTick(World var1, int var2, int var3, int var4, Random var5)
        {
        	this.onBlockPlaced(var1, var2, var3, var4,0,0F,0F,0F,0);
        }
        
        //public void randomDisplayTick(World var1, int var2, int var3, int var4, Random var5)
        //{
        //    this.updateTick(var1, var2, var3, var4, var5);
        //}
        
        /**
         * Returns the quantity of items to drop on block destruction.
         */
        public int quantityDropped(Random par1Random)
        {
            return 1;
        }

        /**
         * Returns the ID of the items to drop on destruction.
         */
        public int idDropped(int par1, Random par2Random, int par3)
        {
            return Trees.twilightRose.blockID;
        }
        
        
}

 

Any help at all is appreciated, thanks

Posted

Unfortunately setLightValue is a class-wide...thing.

 

If you open up BlockRedstoneLamp you'll see that it actually uses two block IDs to be lit or unlit:

 

Block.java

public static final Block redstoneLampIdle = (new BlockRedstoneLight(123, false))
public static final Block redstoneLampActive = (new BlockRedstoneLight(124, true))

 

BlockRedstoneLight.java

public BlockRedstoneLight(int par1, boolean par2)
    {
        super(par1, 211, Material.redstoneLight);
        this.powered = par2;

        if (par2)
        {
            this.setLightValue(1.0F);
            ++this.blockIndexInTexture;
        }
    }

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

Unfortunately setLightValue is a class-wide...thing.

 

If you open up BlockRedstoneLamp you'll see that it actually uses two block IDs to be lit or unlit:

 

Block.java

public static final Block redstoneLampIdle = (new BlockRedstoneLight(123, false))
public static final Block redstoneLampActive = (new BlockRedstoneLight(124, true))

 

BlockRedstoneLight.java

public BlockRedstoneLight(int par1, boolean par2)
    {
        super(par1, 211, Material.redstoneLight);
        this.powered = par2;

        if (par2)
        {
            this.setLightValue(1.0F);
            ++this.blockIndexInTexture;
        }
    }

 

Actually there's a forge-hook for this purpose! It's called << int getLightValue(IBlockAccess world, int x, int y, int z) >>

You can read the javadoc what it does and what you have to return.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Yes, I have seen the getLightValue, I wasnt sure how to set it up, because when I tried it just kept showing errors, and suggesting to make this change or that change. So I abandoned hope using that. I even read the javadoc for it and its kind of vague to me. I was hoping to use a update tick but everything I tried doing that has not worked correctly or failed completely. Thats why I asked here. Could you possibly show me an example using getLightValue? Or know a github repo source whos mod uses it that I can look at to see what I was missing or doing wrong? Forge docs just dont have enough examples of how to set things up....

Posted

Actually there's a forge-hook for this purpose! It's called << int getLightValue(IBlockAccess world, int x, int y, int z) >>

You can read the javadoc what it does and what you have to return.

 

Oh fancy.

 

Still if that method isn't working for you, the redstone lamp way works :P

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

Try harnessing ticks in some way. You'll likely have to set up some code to keep it synced, but a static variable (since they'll all be turning on at the same time) could be checked repeatedly for the correct value, and trigger the change in either light value or block.

 

It would be pretty cool if you could figure out how to dynamically change the light value, as that would allow you to make it pulse softly in the dark, or slowly fade "on" at twilight, instead of turning on abruptly like a light switch.

 

I like this idea, and I'm half tempted to code it and post it here.

Posted

I like this idea, and I'm half tempted to code it and post it here.

 

I am too, actually. :P

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

It would be pretty cool if you could figure out how to dynamically change the light value, as that would allow you to make it pulse softly in the dark, or slowly fade "on" at twilight, instead of turning on abruptly like a light switch.

 

I like this idea, and I'm half tempted to code it and post it here.

 

I wish someone would help, I have been trying for almost 3 weeks and this is best I got

Posted

  You will either have to:

1) rely on random ticks to update the block to a lit one, not precise

 

2) use a tile entity to keep track of time,  heavier on processing

 

3) keep track of all loaded block coords and update via a scheduled tick handler.

 

Or you could try using a scheduled tick handler to manipulate a boolean that sets the light value on Day/night rotations, however sleep will ruin the tick handler so you'll need to make a check for that...

I think its my java of the variables.

Posted

I tried to keep this within one block ID, but  there's no way (that I know of) to do it that simply. There doesn't seem to be any way to force the lighting engine to recognize an updated lightValue, which is a pity. Minecraft's lighting engine generally seems rather buggy and featureless, tbh.

 

Anyways, I'll play with this more tomorrow and see what I can do, but no matter what the result is going to consume more ID's (and system resources) than I'd like.

Posted

That's a shame.  Oh well.

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

that would be cool !  a really rare radioactive flower :) dunno what it would do but it would be cool non the less :) i might try to do that later but dont expect results cos im not great at this :)

Use examples, i have aspergers.

Examples make sense to me.

Posted

So no night flower then huh. Well at least I got more interest here than I did in the MinecraftForums... It seems there people just dont have enough concern to actually contribute to the development of mods as they do here....

 

I would really like to see this though, it would be a great addition to my More Than Just Trees mod.

Posted

yeha if someone smart enough could code the (resource heavy i know) tile entity version and paste it on here that would be cool :)

Use examples, i have aspergers.

Examples make sense to me.

Posted

IBlockAccess is an interface.  It supplied a handful of methods that might be relevant for that function call.  It's like World-lite.

 

In any case, I found that IBlockAccess was insufficient for the desired effect.  Here's what I ended up with:

 

public class GlowFlower extends Block {
//If you want a flower as per flower, extend BlockFlower.  I wanted mine to be placable under water, so I couldn't.
protected static World theWorld;

public GlowFlower(int par1, int par2, Material par3Material) {
	super(par1, par2, par3Material);
	setBlockName("Glow Moss");
	setStepSound(soundGrassFootstep);
        	setTickRandomly(true);
        	float var4 = 0.2F;
        	setBlockBounds(0.5F - var4, 0.0F, 0.5F - var4, 0.5F + var4, var4 * 3.0F, 0.5F + var4);
        	setCreativeTab(CreativeTabs.tabDecorations);
}

public GlowFlower(int par1, Material par2) {
	super(par1, par2);
	setBlockName("Glow Moss");
	setStepSound(soundGrassFootstep);
        	float var4 = 0.2F;
        	setBlockBounds(0.5F - var4, 0.0F, 0.5F - var4, 0.5F + var4, var4 * 3.0F, 0.5F + var4);
        	setCreativeTab(CreativeTabs.tabDecorations);
}

public void onBlockAdded(World world, int x, int y, int z) {
	if(theWorld == null)
		theWorld = world;
	world.scheduleBlockUpdate(x, y, z, blockID, 600);
}

public void updateTick(World world, int x, int y, int z, Random par5Random) {
	if(theWorld == null)
		theWorld = world;  //make sure theWorld is not null
	getLightValue(null, x, y, z); //force recheck of light level, probably not needed
	world.scheduleBlockUpdate(x, y, z, blockID, 600);  //schedule another update
}

@Override
public int getLightValue(IBlockAccess world, int x, int y, int z) {
	if(theWorld != null) {
		int a = theWorld.calculateSkylightSubtracted(0); //force calculation of current lighting levels
		if(a > 
			return 8;
		else
			return 0;
	}
	else
		return 8; //light level when the world is loaded (loading from save does not call onAdded, onPlaced, etc.)
}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
        return null;
    }

public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }

public int getRenderType()
    {
        return 1;
    }

 

It DOES light up at night and turn off during the day, but it's updates are once a minute (I didn't feel like burdening the system with frequent updates).  And I had to make a class-level reference to the World in order to get the current sky light level.

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

Did you run it?

 

Note that this line:

 

int a = theWorld.calculateSkylightSubtracted(0); //force calculation of current lighting levels

 

Gets how much below 15 the current light level is (note the SUBTRACTED part).  Oddly I didn't find the "get current light level" function.  Either I missed seeing it, or it's part of a different class.

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

IBlockAccess is an interface.  It supplied a handful of methods that might be relevant for that function call.  It's like World-lite.

 

In any case, I found that IBlockAccess was insufficient for the desired effect.  Here's what I ended up with:

 

public class GlowFlower extends Block {
//If you want a flower as per flower, extend BlockFlower.  I wanted mine to be placable under water, so I couldn't.
protected static World theWorld;

public GlowFlower(int par1, int par2, Material par3Material) {
	super(par1, par2, par3Material);
	setBlockName("Glow Moss");
	setStepSound(soundGrassFootstep);
        	setTickRandomly(true);
        	float var4 = 0.2F;
        	setBlockBounds(0.5F - var4, 0.0F, 0.5F - var4, 0.5F + var4, var4 * 3.0F, 0.5F + var4);
        	setCreativeTab(CreativeTabs.tabDecorations);
}

public GlowFlower(int par1, Material par2) {
	super(par1, par2);
	setBlockName("Glow Moss");
	setStepSound(soundGrassFootstep);
        	float var4 = 0.2F;
        	setBlockBounds(0.5F - var4, 0.0F, 0.5F - var4, 0.5F + var4, var4 * 3.0F, 0.5F + var4);
        	setCreativeTab(CreativeTabs.tabDecorations);
}

public void onBlockAdded(World world, int x, int y, int z) {
	if(theWorld == null)
		theWorld = world;
	world.scheduleBlockUpdate(x, y, z, blockID, 600);
}

public void updateTick(World world, int x, int y, int z, Random par5Random) {
	if(theWorld == null)
		theWorld = world;  //make sure theWorld is not null
	getLightValue(null, x, y, z); //force recheck of light level, probably not needed
	world.scheduleBlockUpdate(x, y, z, blockID, 600);  //schedule another update
}

@Override
public int getLightValue(IBlockAccess world, int x, int y, int z) {
	if(theWorld != null) {
		int a = theWorld.calculateSkylightSubtracted(0); //force calculation of current lighting levels
		if(a > 
			return 8;
		else
			return 0;
	}
	else
		return 8; //light level when the world is loaded (loading from save does not call onAdded, onPlaced, etc.)
}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
        return null;
    }

public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }

public int getRenderType()
    {
        return 1;
    }

 

It DOES light up at night and turn off during the day, but it's updates are once a minute (I didn't feel like burdening the system with frequent updates).  And I had to make a class-level reference to the World in order to get the current sky light level.

 

Thanks, this works pretty good, was playing with it some to make the flower a bit brighter, and also to answer the other question by vogner I would really like it to light up at night, not in the dark necessarily. I dont plan to have it spawn in the caves or anything like that, and also I plant to have it as rare as I can get it....

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.