Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.15.1] Looking for a replacement for Blockstate.tick


kwpugh
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 weeks later...

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

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.

Guest
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • When I try to load it. It starts up but none can join the server as the forge jar is corrupted some how. Believe it is a fault in the update.  
    • Sorry I didn’t explain it in much detail at first hopefully this will be much more easier to understand. I am running a 1.16.5 forge server with java 8 there is multiple errors which you can see in the paste bin right at the top. Not sure what is the problem with it I followed a YouTube video it worked for him but not for me I believe it is because it is not registering the main class which is coursing the forge not to work it has no mods at all.
    • Here is the Link to the debug.log.  
    • I haven't been able to find the debug.log, however here is the list of the mods: https://imgur.com/gtrTKxO
    • Thanks for the help everybody! I tried this and although the block did register, the fluid just appears as a flat plane you can only see if you break the block under it. I tried debugging it as much as I could but alas I couldn't solve it Here is the code for my "fixed" liquidblock as suggested by Luis_ST: package com.hotmail.majdroaydi.minitech.blocks; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.sounds.SoundEvent; import net.minecraft.tags.FluidTags; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LiquidBlock; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.FlowingFluid; import net.minecraft.world.level.pathfinder.PathComputationType; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import java.util.Optional; import java.util.function.Supplier; public class ForgeLiquidBlock extends LiquidBlock { public ForgeLiquidBlock(Supplier<? extends FlowingFluid> supplier, Properties properties) { super(supplier, properties); } @Override public VoxelShape getCollisionShape(BlockState p_54760_, BlockGetter p_54761_, BlockPos p_54762_, CollisionContext p_54763_) { return p_54763_.isAbove(STABLE_SHAPE, p_54762_, true) && p_54760_.getValue(LEVEL) == 0 && p_54763_.canStandOnFluid(p_54761_.getFluidState(p_54762_.above()), getFluid()) ? STABLE_SHAPE : Shapes.empty(); } @Override public boolean isPathfindable(BlockState p_54704_, BlockGetter p_54705_, BlockPos p_54706_, PathComputationType p_54707_) { return !getFluid().is(FluidTags.LAVA); } @Override public boolean skipRendering(BlockState p_54716_, BlockState p_54717_, Direction p_54718_) { return p_54717_.getFluidState().getType().isSame(getFluid()); } @Override public void onPlace(BlockState p_54754_, Level p_54755_, BlockPos p_54756_, BlockState p_54757_, boolean p_54758_) { if (this.shouldSpreadLiquid(p_54755_, p_54756_, p_54754_)) { p_54755_.getLiquidTicks().scheduleTick(p_54756_, p_54754_.getFluidState().getType(), getFluid().getTickDelay(p_54755_)); } } @Override public BlockState updateShape(BlockState p_54723_, Direction p_54724_, BlockState p_54725_, LevelAccessor p_54726_, BlockPos p_54727_, BlockPos p_54728_) { if (p_54723_.getFluidState().isSource() || p_54725_.getFluidState().isSource()) { p_54726_.getLiquidTicks().scheduleTick(p_54727_, p_54723_.getFluidState().getType(), getFluid().getTickDelay(p_54726_)); } //return super.updateShape(p_54723_, p_54724_, p_54725_, p_54726_, p_54727_, p_54728_); return p_54723_; // Calling super.updateShape will just call LiquidBlock's updateShape, not what we are looking for! Thankfully, Block.updateShape, simply enough, just returns the first parameter. } @Override public void neighborChanged(BlockState p_54709_, Level p_54710_, BlockPos p_54711_, Block p_54712_, BlockPos p_54713_, boolean p_54714_) { if (this.shouldSpreadLiquid(p_54710_, p_54711_, p_54709_)) { p_54710_.getLiquidTicks().scheduleTick(p_54711_, p_54709_.getFluidState().getType(), getFluid().getTickDelay(p_54710_)); } } private boolean shouldSpreadLiquid(Level p_54697_, BlockPos p_54698_, BlockState p_54699_) { if (getFluid().is(FluidTags.LAVA)) { boolean flag = p_54697_.getBlockState(p_54698_.below()).is(Blocks.SOUL_SOIL); for(Direction direction : POSSIBLE_FLOW_DIRECTIONS) { BlockPos blockpos = p_54698_.relative(direction.getOpposite()); if (p_54697_.getFluidState(blockpos).is(FluidTags.WATER)) { Block block = p_54697_.getFluidState(p_54698_).isSource() ? Blocks.OBSIDIAN : Blocks.COBBLESTONE; p_54697_.setBlockAndUpdate(p_54698_, net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_54697_, p_54698_, p_54698_, block.defaultBlockState())); this.fizz(p_54697_, p_54698_); return false; } if (flag && p_54697_.getBlockState(blockpos).is(Blocks.BLUE_ICE)) { p_54697_.setBlockAndUpdate(p_54698_, net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_54697_, p_54698_, p_54698_, Blocks.BASALT.defaultBlockState())); this.fizz(p_54697_, p_54698_); return false; } } } return true; } private void fizz(LevelAccessor p_54701_, BlockPos p_54702_) { p_54701_.levelEvent(1501, p_54702_, 0); } @Override public ItemStack pickupBlock(LevelAccessor p_153772_, BlockPos p_153773_, BlockState p_153774_) { if (p_153774_.getValue(LEVEL) == 0) { p_153772_.setBlock(p_153773_, Blocks.AIR.defaultBlockState(), 11); return new ItemStack(getFluid().getBucket()); } else { return ItemStack.EMPTY; } } @Override public Optional<SoundEvent> getPickupSound() { return getFluid().getPickupSound(); } } The code for my OilFluid can be found above.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.