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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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