Jump to content

[1.11.2] How do I program a block with redstone properties


The_G0dPiXeL

Recommended Posts

What I am trying to do is create a block that does AND gate logic, which has 2 sides for input and 1 side for output.

I have no code except the block registration because I have literally no clue where to start, the API for redstone blocks is terrible and I tried to look at the code for the Observer block and that gave me a little bit of information but I have no idea how to modify that code to my liking as I have no idea what any of those functions do. I've looked everywhere for documentation but alas I could not find a single tutorial, so I had to turn to the forums. I'm wondering if anybody has a modding library or some code that I could use make this.

 

I'm sorry if this seems like I'm asking people to do my homework for me but I did hours upon hours of research and trial and error, yet I can't make it work, so I need help from the community.

Edited by The_G0dPiXeL
Link to comment
Share on other sites

Mmm, mind you I have never done any redstone before, but from ten minutes of looking at Minecraft code, you just (and others please correct me if I am wrong) need to create a propertyEnum, set all of the property states and meta, have canProvidePower set to return true, then (look at how Minecraft discriminates between what "strong" and "weak" is, I choose weak, cause that might be more of what you want, if you want to have behavior like a comparator, set you getStrongPower to your weak power) have getWeakPower return an int value (between and inclusive between 0 and 15) based on the property state. For you case, specifically, you should probably also want to override onNeighborChanged so to detect when power is being provided. Beyond that, everything is self-explanatory, just look at how Minecraft does it, tends to be the best way.  

Link to comment
Share on other sites

1 minute ago, The_G0dPiXeL said:

Alright, so I did some more testing and stuff and I got the output working but I still don't know how to get inputs from both sides

 

https://pastebin.com/WXSYfmpc

from this point, it should be pretty easy all you need to do is chek if on a left and right sides there is power and if yes then give output

 

PS I don't think you should use a variable for this because it is going to be shared by all the ANDGate blocks in Mc 

Link to comment
Share on other sites

51 minutes ago, lukas2005 said:

from this point, it should be pretty easy all you need to do is chek if on a left and right sides there is power and if yes then give output

 

PS I don't think you should use a variable for this because it is going to be shared by all the ANDGate blocks in Mc 

Well, I don't see a function that gives an EnumFacing object that also watches neighbors, so can you give me some ideas.

 

Also how come I can't use a boolean, that's kinda stupid, I'll make it a PropertyBool if that is the case.

Link to comment
Share on other sites

You can NEVER use instance fields to store information about individual blocks (or items), because they are singletons. There is only one instance of that block in the entire game, so if you change that isOn field, it will effect every block of that type. That's what blockstates are for.

 

If your block is supposed to face a specific direction and behave accordingly (like a comparator, repeater, observer, or etc), then you will also need a facing property in your blockstate.

Link to comment
Share on other sites

2 minutes ago, Jay Avery said:

You can NEVER use instance fields to store information about individual blocks (or items), because they are singletons. There is only one instance of that block in the entire game, so if you change that isOn field, it will effect every block of that type. That's what blockstates are for.

 

If your block is supposed to face a specific direction and behave accordingly (like a comparator, repeater, observer, or etc), then you will also need a facing property in your blockstate.

 

I tried using a PropertyBool but it's not working

 

https://pastebin.com/u0tK9BVk

Link to comment
Share on other sites

8 minutes ago, Jay Avery said:

Oh sorry, I only read the beginning of the page which was createBlockState.

 

After I added in those other functions Forge finally initializes, I got a ModelLoader exception but I fixed that.

 

This is the current state of my code, it doesn't give any errors except the usual yggdrasil error, but it doesn't have the functionality I want.

Link to comment
Share on other sites

Do you remember when I said this?

56 minutes ago, Jay Avery said:

If your block is supposed to face a specific direction and behave accordingly (like a comparator, repeater, observer, or etc), then you will also need a facing property in your blockstate.

 

Link to comment
Share on other sites

1 hour ago, Jay Avery said:

Do you remember when I said this?

 

I tend to skim over things from time to time, sorry.

 

I made the block extend BlockDirectional and added a println statement in each function, and I found that my code doesn't even execute onNeighborChange, do you know why this is?

Edited by The_G0dPiXeL
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.