Jump to content

(SOLVED) [1.14.4] Block construction and State changing


MineModder2000

Recommended Posts

I want to know how to make blocks / objects that are crafted in the world, rather than in the grid. I am talking about the way nether portals and iron golems are crafted in the world by placing blocks. Also doing things like converting blocks with right clicks such as stripping logs with axes.

Edited by MineModder2000
Link to comment
Share on other sites

1 minute ago, MineModder2000 said:

Also doing things like converting blocks with right clicks such as stripping logs with axes.

Check out AxeItem or HoeItem or ShovelItem. They all do it.

 

2 minutes ago, MineModder2000 said:

I am talking about the way nether portals and iron golems are crafted in the world by placing blocks.

I think the iron golems are handled in the Pumpkin blocks class. You'll need to override onBlockPlacedBy in your Blocks class. And if they aren't your blocks you'll need to use an event. I think BlockEvent.Placed? I'm not sure about the name.

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

On 9/6/2019 at 10:16 PM, Animefan8888 said:

Check out AxeItem or HoeItem or ShovelItem. They all do it.

 

I think the iron golems are handled in the Pumpkin blocks class. You'll need to override onBlockPlacedBy in your Blocks class. And if they aren't your blocks you'll need to use an event. I think BlockEvent.Placed? I'm not sure about the name.

I see. I've been testing out block conversion on right click with one of my own custom items by copying code from AxeItem. I can get wood / plank / log / stripped blocks to convert from one to another, but it only works for those types, nothing else. I noticed also that minecraft.block package doesn't have any log / wood / plank / stripped blocks in it, so where are they defined? This is what I tried :

 

package mymod.thrown;

import java.util.Map;

import com.google.common.collect.ImmutableMap.Builder;

import mymod.My_Mod;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.RotatedPillarBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class Chert extends Item { // Copies EggItem
	
	protected static final Map<Block, Block> BLOCK_STRIPPING_MAP = new Builder<Block, Block>().put(Blocks.CACTUS, Blocks.JUNGLE_LOG).build();
	
    public Chert(Item.Properties builder) {
	   
        super(builder);
    }

    /**
     * Called to trigger the item's "innate" right click behavior. To handle when this item is used on a Block, see
     * {@link #onItemUse}.
     */
       
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
    	  
    	ItemStack itemstack = playerIn.getHeldItem(handIn);
	    
	    if (playerIn.ticksExisted - My_Mod.tick_last >= 16) {

	    	My_Mod.tick_last = playerIn.ticksExisted;
	    	
	        if (!playerIn.abilities.isCreativeMode) {
	    	   
	            itemstack.shrink(1);
	        }
	
	        worldIn.playSound((PlayerEntity) null, playerIn.posX, playerIn.posY, playerIn.posZ, SoundEvents.ENTITY_EGG_THROW, SoundCategory.PLAYERS, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F));
	    	  
	        if (!worldIn.isRemote) {
	        	
			    Chert_Entity chert_entity = new Chert_Entity(worldIn, playerIn);
			    chert_entity.func_213884_b(itemstack);
			    chert_entity.shoot(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.15F, 3.0F);
			    worldIn.addEntity(chert_entity);
	        }
	       
	    playerIn.addStat(Stats.ITEM_USED.get(this));
	    
	    return new ActionResult<>(ActionResultType.SUCCESS, itemstack);
      
	    }
	    
	    else return new ActionResult<>(ActionResultType.PASS, itemstack);
   }
    
   public ActionResultType onItemUse(ItemUseContext context) {
	   
	   World world = context.getWorld();
	   BlockPos blockpos = context.getPos();
	   BlockState blockstate = world.getBlockState(blockpos);
	   Block block = BLOCK_STRIPPING_MAP.get(blockstate.getBlock());
	   		
	        if (block != null && !world.isRemote) {
	        	
	            world.setBlockState(blockpos, block.getDefaultState().with(RotatedPillarBlock.AXIS, blockstate.get(RotatedPillarBlock.AXIS)), 11);
	        }
	   
	   return ActionResultType.SUCCESS;
   }
}

 

Edited by MineModder2000
Link to comment
Share on other sites

8 hours ago, diesieben07 said:

You can't do this. This will be shared between all players and break things.

 

This doesn't work, because the original block state (cactus) does not have an axis.

 

Also, use @Override when overriding methods.

 

You'll have to explain the first one more. The whole point of the if statement and tick variables in the onItemRightClick method is to prevent the item from being thrown too rapidly. How else should I do this?

 

Removing with the with() method did the trick, now any blocks can be converted back and forth, thanks. 

Link to comment
Share on other sites

15 minutes ago, MineModder2000 said:

How else should I do this?

Use a Capability or just the ItemStacks NBT tag.

 

16 minutes ago, MineModder2000 said:

You'll have to explain the first one more.

You can't just store a value in the @Mod class and use it for a timer that is player specific like right clicking. There is only one value on the server.

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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



×
×
  • Create New...

Important Information

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