So I have a modded flower I made called a Primrose, that is supposed to spawn new flowers into the spaces around it.
It works fine when there is just one Primrose in the world, but upon placing more something is going wrong. For example, assume that over time Primrose 1 placed three flowers around itself, and then Primrose 2 was placed. Once Primrose 2 is placed Primrose 1 stops growing its flowers, yet the spawn rate of flowers around Primrose 2 accelerates. From what I can understand, the BlocksPos variables I use are updated with each new Primrose placed, so that the all the tick rates of the previous Primroses are being applied to the newest one.
I thought that each new Primrose would record their own positions and act as their own object, since they are their own individual blocks, not all be connected.
I have the Primrose class below; does anyone understand what is going on???
PS: The last time I posted on this forum literally no one helped to answer my question, while I finally got it, it was something anyone could have helped with, so please help this time.
The game only has a single instance of any given block's object as they are instantiated as static objects, so all of their fields are essentially static even if not declared as such; the save data simply contains IDs which are used to tell it what sort of block has been stored (1 = stone, 2 = grass, 3 = dirt, etc), with its coordinates being its location within the save file/world.
If you actually need to save data for a particular block you need to either use metadata (block states) or make a tile entity.
What you need to do is move the code in "onBlockAdded" (the offsets from pos) to "updateTick", which provides the coordinates for the block being ticked. Also, you use an if-else instead of individual ifs, meaning that the first case is most likely to be chosen while the last case will only be chosen if every other case fails, resulting in a significant directional bias. You could also use a single line which adds a random offset from -1 to 1 to x and z with (rand.nextInt(3) - 1), which will give a 1 in 9 chance of choosing a particular block per block tick (about once every 68 seconds); for example, the following is the updateTick method of BlockGrass, which also offsets y so it can spread up and down, with 4 attempts at spreading:
You probably also want to include additional checks, such as the light level, both at the block it tries to spread to and the position of the ticked block so it uproots if it is too low (similar to the code above; grass first checks if the light level is too low and the block above it blocks too much light, setting itself to dirt, otherwise it tries to spread). You also don't need to check for anything other than air blocks at the chosen location (you first call world.isAirBlock() then you check for other flowers, which also wouldn't work properly anyway without the call to world.isAirBlock() unless you included every single non-air block); for example (from BlockMushroom):
Note that this calls a "canBlockStay()" method, which checks for whether the location is valid (light level and the block below) and makes the code easier to understand (as opposed to copying the same thing multiple times).
I'd also suggest changing your "pickFlower()" method to "pickFlower(Random rand)", this way you can call it during world generation and get consistent results (as otherwise it will be completely random, as the Random passed to updateTick effectively is; you may also want to instead get the block state at the current location and use it as the state for new flowers so they are of the same type).