Jump to content

[Solved]canplaceblock at "method"


Shanedublin

Recommended Posts

I have an Item that places blocks when you right click and it almost works' except that it is placing the blocks like you could place a torch. Ex: when you right click right under you it will place the block, then the block will push you out of the way.  I am having a hard time finding the method that is for block placement and not item placement.    If anyone could point me in the right direction I'd be grateful. 

Link to comment
Share on other sites

if i understand correctly, you want it to work like a normal block? meaning, instead of it placing the block down and pushing you out, it shouldn't be placed at all unless you move out of the way?

 

anyway, just take a look at ItemBlock.class. you'll probably find what you want there

Link to comment
Share on other sites

I've looked in that class And I see the onItemUse Method , the description says it's for items and not for blocks.  And I see the canPlaceItemBlockOnSide method and that one isn't quite working... It's checking for for a player that is one block lower than the player is. I'll try adding some +1s some where to seed if I can get it to work.  Thanks for your help.

Link to comment
Share on other sites

I figured it out.. when I was checking where to place the block I was increment the (x,y,z) location twice.  I end up using a combination of the onItemUse and the canPlaceItemBlockAt method.  There is probably some easier way to do it, but it works so I wont touch it for a while.

 

When In doubt print all the vars!

Link to comment
Share on other sites

if you actually read and understood the code, you'd know it's for blocks. almost all the vanilla blocks use that class. if you wanna make sure what you made is clean and if you wanna understand better how vanilla mc places its blocks down, here are some things you can do:

 

if you're patient and thorough enough, start at clickMouse(int par1) in Minecraft.class. follow and understand the code until you reach the point where it tries to use an item (or place a block down). it might take a while, but you'd get a pretty good understanding of how blocks are placed after you do that.

 

or you can try making a new block for testing. just a plain basic one and then create your own item for it that extends ItemBlock (doesn't have to be a subclass of ItemBlock if you know what you're doing). then play around with both your item class and your block class to see how they work together. canPlaceItemBlockAt is probably not working for you because you're probably already doing something else that's messing it up. which is why i suggest making a new block for testing (something clean).

 

of course you can just stick with what you have since you say it's working already, but then i doubt you really understand why it's working. it could help loads in the future if you really understand what's happening

Link to comment
Share on other sites

if you actually read and understood the code, you'd know it's for blocks. almost all the vanilla blocks use that class. ...

I know that that class is for blocks, that is why i used it. I have an Item that places blocks when you right click on a surface.  I know why it wasn't working. 

 

 

... if you're patient and thorough enough, start at clickMouse(int par1) in Minecraft.class. follow and understand the code until you reach the point where it tries to use an item (or place a block down). it might take a while, but you'd get a pretty good understanding of how blocks are placed after you do that. ...

 

I didn't know where clicks where initially handled thanks for pointing that out.

 

 

 

 

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

    • 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;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
  • Topics

×
×
  • Create New...

Important Information

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