Jump to content

[1.15.1] Looking for a replacement for Blockstate.tick


Recommended Posts

Posted

Here is my 1.14.4 source code for reference:

package com.kwpugh.gobber2.items.rings;

import java.util.List;

import net.minecraft.block.BambooBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.CactusBlock;
import net.minecraft.block.ChorusFlowerBlock;
import net.minecraft.block.CocoaBlock;
import net.minecraft.block.CoralBlock;
import net.minecraft.block.CropsBlock;
import net.minecraft.block.IGrowable;
import net.minecraft.block.NetherWartBlock;
import net.minecraft.block.SaplingBlock;
import net.minecraft.block.SugarCaneBlock;
import net.minecraft.block.VineBlock;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemCustomRingFarmer extends Item
{

	public ItemCustomRingFarmer(Properties properties)
	{
		super(properties);
	}

	public void inventoryTick(ItemStack stack, World world, Entity entity, int par4, boolean par5)
    {
        
    	if(!(entity instanceof PlayerEntity) || world.isRemote)
        {
            return;
        }

    	PlayerEntity player = (PlayerEntity)entity;
        ItemStack equippedMain = player.getHeldItemMainhand();
        ItemStack equippedOff = player.getHeldItemOffhand();
        
        if(stack == equippedMain || stack == equippedOff)
        {
            int range = 7;
            for(int x = -range; x < range+1; x++)
            {
                for(int z = -range; z < range+1; z++)
                {
                    for(int y = -range; y < range+1; y++)
                    {
                        int theX = MathHelper.floor(player.posX+x);
                        int theY = MathHelper.floor(player.posY+y);
                        int theZ = MathHelper.floor(player.posZ+z);
                        
                        BlockPos tagetPos = new BlockPos(theX, theY, theZ);
                        
                        BlockState blockstate = world.getBlockState(tagetPos);
                                              
                        //For basic growing blocks that use tick()
                        if ((blockstate.getBlock() instanceof CropsBlock) ||
                        		(blockstate.getBlock() instanceof SaplingBlock)) 
                        {
                        	if (!world.isRemote)
                    		{
                        		if (player.ticksExisted % 12 == 0)
                        		{
                        			blockstate.tick(world, tagetPos, world.rand);
                       		 	}                                                               
                    		}
                        }
                        
                        //For slower growing blocks that use tick()
                        if ((blockstate.getBlock() instanceof VineBlock) ||                     		               
                        		(blockstate.getBlock() instanceof SugarCaneBlock) ||
                        		(blockstate.getBlock() instanceof NetherWartBlock) ||
                        		(blockstate.getBlock() instanceof CactusBlock))
                        {
                        	if (!world.isRemote)
                    		{
                        		if (player.ticksExisted % 5 == 0)
                        		{
                        			blockstate.tick(world, tagetPos, world.rand);
                       		 	}                                                               
                    		}
                        }
                        	
                        //For faster growing blocks that use tick()
                        if ((blockstate.getBlock() instanceof BambooBlock) ||                         		
                        		(blockstate.getBlock() instanceof CoralBlock) ||		
                        		(blockstate.getBlock() instanceof CocoaBlock) ||  
                        		(blockstate.getBlock() instanceof ChorusFlowerBlock) )
                        {
                        	if (!world.isRemote)
                    		{
                        		if (player.ticksExisted % 60 == 0)
                        		{
                        			blockstate.tick(world, tagetPos, world.rand);
                        			// 
                       		 	}                                                               
                    		}
                        }
                    }
                }
            }
       }
    }
	
    @Override
	public void addInformation(ItemStack stack, World world, List<ITextComponent> list, ITooltipFlag flag)
	{
		super.addInformation(stack, world, list, flag);				
		list.add(new StringTextComponent(TextFormatting.BLUE + "Works on most crops, plants, and trees"));
		list.add(new StringTextComponent(TextFormatting.GREEN + "Range: 14 blocks"));
	}  
}

 

I traced through the forge reference sources and found that it is not called but anything but the StemBlock.class.   In 1.15.1, StemBlock appears to use its own method for growing.

 

This may not be the best approach, but it is what I figured out in 1.14.4.   If there is a better approach, I would love to hear about it.

 

Regards.

Posted

I've been keeping an eye on this thread to see if anyone else responded since I just started learning forge and have been playing around with some similar ideas on my own. I'm still focusing on 1.14 so I don't know the exact specifics of 1.15, but I think I can offer a little bit of help. In this case, it looks like the goal of your item is to speed up the growth of nearby crops. Instead of requesting a tick update on each of those crops it finds, you can skip the middle man and look for some function that will boost the age property of the crops directly.

 

In 1.14, I accomplished that by calling the function grow() which is built into descendants of growable things in vanilla (seeds, carrots, etc.). It's possible that function changed in 1.15 (I don't know), so I would look into the BonemealItem class to see what vanilla does to grow crops and that should point you in the right direction. The basic idea in 1.14 at least is that function updates the age property of the crop, so to grow crops you just need to update the block state with an incremented age. Different crop types (as you've identified already) have different age values (stuff like wheat go from like 0 -> 7 for example), so you'll need to keep that in mind. You'll probably need to add on a little randomness of your own to make it not just instantly grow all the crops around you (the bonemeal effect is pretty strong), but that shouldn't be too difficult to accomplish.

 

As for more general code quality stuff, I can spot a few places where you might take advantage of some refactoring in your port. You have some pretty deeply nested conditionals that can make things difficult to understand at a glance, and a lot of them have some pretty repetitive checks. For one, you check !isRemote() inside of every condition instead of once outside them all. This can be pulled out front and you'll save some lines and clear things up. For my own coding style at least, I see several opportunities to break up some repetitive code into its own functions to help improve the readability and ease of debugging. That's all subjective though, so take that with a grain of salt.

 

As a side note: I've only been on these forums for a short time and didn't realize it at first but when you're adding code examples to a post there's actually a drop down menu in the bottom right where you can change the language from HTML to Java --> it'll automatically put some syntax highlighting to make it much nicer to read.

 

Hope this helps.

Posted

Thank you for the feedback.  I'm still new to coding Java and still getting the hang of things.   

 

An earlier version of the class relied on the grow() to affect CropsBlock and SaplingsBlock instances.   I will need to study some of the other block types (Vines, Stems, etc) to see how those can work.  

 

The general nature of the block state.tick() was convenient, but does not seem to have a replacement.

 

Here is were things landed.

package com.kwpugh.ring_of_growth;

import java.util.List;

import net.minecraft.block.BambooBlock;
import net.minecraft.block.BambooSaplingBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.CropsBlock;
import net.minecraft.block.IGrowable;
import net.minecraft.block.SaplingBlock;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;


/**
 * 
 * Only effective on Crops, Saplings, and Bamboo until I find an alternative to blockstate.tick() 
 * 
 */

public class ItemRingGrowth extends Item
{

	public ItemRingGrowth(Properties properties)
	{
		super(properties);
	}
 
	public void inventoryTick(ItemStack stack, World world, Entity entity, int par4, boolean par5)
    {      
    	if(!(entity instanceof PlayerEntity) || world.isRemote)
        {
            return;
        }

    	PlayerEntity player = (PlayerEntity)entity;
        ItemStack equippedMain = player.getHeldItemMainhand();
        ItemStack equippedOff = player.getHeldItemOffhand();
        
        if(stack == equippedMain || stack == equippedOff)
        {
            int range = 7;
            for(int x = -range; x < range+1; x++)
            {
                for(int z = -range; z < range+1; z++)
                {
                    for(int y = -range; y < range+1; y++)
                    {
                        int theX = MathHelper.floor(player.func_226277_ct_()+x);
                        int theY = MathHelper.floor(player.func_226278_cu_()+y);
                        int theZ = MathHelper.floor(player.func_226281_cx_()+z);
                        
                        BlockPos targetPos = new BlockPos(theX, theY, theZ);                       
                        BlockState blockstate = world.getBlockState(targetPos);

                        if ((blockstate.getBlock() instanceof CropsBlock) ||
                        		(blockstate.getBlock() instanceof SaplingBlock) ||
                        		(blockstate.getBlock() instanceof BambooBlock) ||
                        		(blockstate.getBlock() instanceof BambooSaplingBlock)) 
                        {
                            IGrowable igrowable = (IGrowable)blockstate.getBlock();
                            if ((igrowable.canGrow(world, targetPos, blockstate, world.isRemote)) && (player.ticksExisted % 120 == 0))
                            {
                               if (world instanceof ServerWorld)
                               {
                                  if (igrowable.canUseBonemeal(world, world.rand, targetPos, blockstate))
                                  {
                                     igrowable.func_225535_a_((ServerWorld)world, world.rand, targetPos, blockstate);
                                  }
                               }
                            }
                         }
                    }
                }
            }
       }
    }
	
    @Override
	public void addInformation(ItemStack stack, World world, List<ITextComponent> list, ITooltipFlag flag)
	{
		super.addInformation(stack, world, list, flag);				
		list.add(new StringTextComponent(TextFormatting.BLUE + "Works on many crops, plants, and trees"));
		list.add(new StringTextComponent(TextFormatting.GREEN + "Range: 14 blocks"));
	}  
}

 

That you for the feedback.

  • 4 weeks later...
Posted

BlockState#tick still exists in 1.15...

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Thanks everyone, I appreciate the help.

 

I found the ticking function, which does not have a regular name in the mappings I have installed.

 

blockstate.func_227033_a_((ServerWorld) world, targetPos, world.rand);

 

 

Ojb: the grow() is limited to a few type of growables, so that was only a partial solution.  I decided to go with a functions that ticks blockstates as a generalized solution.

 

Thanks for the guidance.

 

Regards

Posted
13 hours ago, kwpugh said:

which does not have a regular name in the mappings I have installed.

Update your mappings?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.