Jump to content

Recommended Posts

Posted (edited)

Do you mean setting the block to face the player when placed down?

Override Block#getStateForPlacement and return the blockstate of facing in the player's direction.

Also change the variants in the blockstates json to match your rotation.

 

For reference, look at how the vanilla furnace handles it.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

  • 3 weeks later...
Posted
On 3/20/2019 at 11:14 PM, DavidM said:

Do you mean setting the block to face the player when placed down?

Override Block#getStateForPlacement and return the blockstate of facing in the player's direction.

Also change the variants in the blockstates json to match your rotation.

 

For reference, look at how the vanilla furnace handles it.

Hi! Sorry that it has been a while but do you mind if you could give me an example of 1.13 rotation on placement feature? I have tried messing with my code but I cannot get rotation to work in my block.

I checked the furnace code and got the rotation code from it but it does not work as some things come out as deprecated or just not working. Sorry to be annoying if I am, but thanks if I get any replies again.

Posted

How to do rotation:

 

1. Store the direction

Add this to your block class:
 

public static final PropertyDirection FACING = BlockHorizontal.FACING;

This just stores the direction.

 

2. Tell forge that direction is part of the state

You need to tell forge direction is part of the state, so it can load it from the blockstate.

Add this to your block's code:

setDefaultState(getDefaultState().withProperty(FACING, EnumFacing.NORTH)

(Add it to the constructor)

 

3. Set the state when the block is placed.

You have to set the state when the block is placed. Add this to your block class:

@Override
    @Nonnull
    public IBlockState getStateForPlacement(@Nullable World world, @Nullable BlockPos pos, @Nullable EnumFacing facing, float hitX, float hitY, float hitZ, int meta, @Nullable EntityLivingBase placer, EnumHand hand) {
        assert placer != null;
        return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
    }

The code

placer.getHorizontalFacing().getOpposite()

Just gets the horizontal facing direction (where you are looking along the x and z axis) then gets the opposite so the block faces you.

 

4. Add it to your blockstate

This is an example of a blockstate with facing (works in 1.12.2):

{
  "variants": {
    "facing=north": { "model": "modid:model" },
    "facing=west": { "model": "modid:model", "y": 90 },
    "facing=south": { "model": "modid:model", "y": 180 },
    "facing=east": { "model": "modid:model", "y": 270 }
  }
}

(replace modid with your modid, model with the model resource location)

Posted (edited)
17 hours ago, Big_Bad_E said:

How to do rotation:

 

1. Store the direction

Add this to your block class:
 


public static final PropertyDirection FACING = BlockHorizontal.FACING;

This just stores the direction.

 

2. Tell forge that direction is part of the state

You need to tell forge direction is part of the state, so it can load it from the blockstate.

Add this to your block's code:


setDefaultState(getDefaultState().withProperty(FACING, EnumFacing.NORTH)

(Add it to the constructor)

 

3. Set the state when the block is placed.

You have to set the state when the block is placed. Add this to your block class:


@Override
    @Nonnull
    public IBlockState getStateForPlacement(@Nullable World world, @Nullable BlockPos pos, @Nullable EnumFacing facing, float hitX, float hitY, float hitZ, int meta, @Nullable EntityLivingBase placer, EnumHand hand) {
        assert placer != null;
        return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
    }

The code


placer.getHorizontalFacing().getOpposite()

Just gets the horizontal facing direction (where you are looking along the x and z axis) then gets the opposite so the block faces you.

 

4. Add it to your blockstate

This is an example of a blockstate with facing (works in 1.12.2):


{
  "variants": {
    "facing=north": { "model": "modid:model" },
    "facing=west": { "model": "modid:model", "y": 90 },
    "facing=south": { "model": "modid:model", "y": 180 },
    "facing=east": { "model": "modid:model", "y": 270 }
  }
}

(replace modid with your modid, model with the model resource location)

6

Hi, sorry but I am looking for 1.13.2 as it says in the title as I am trying to update my mod from 1.12.2 to 1.13.2. I know from 1.12.2, but 1.13.2 I still need to figure out. Thanks for replying anyway.

Though if this is for 1.13.2 (though you do say 1.12.2 in the blockstate) please can you tell me how my workspace should look?

Edited by Billy Playz
Posted (edited)
6 hours ago, Billy Playz said:

Hi, sorry but I am looking for 1.13.2 as it says in the title as I am trying to update my mod from 1.12.2 to 1.13.2. I know from 1.12.2, but 1.13.2 I still need to figure out. Thanks for replying anyway.

Though if this is for 1.13.2 (though you do say 1.12.2 in the blockstate) please can you tell me how my workspace should look?

Sorry totally managed to misread that!
I currently cannot test for 1.13.2 (gradle hates me :/), but the code should not change to my knowledge. If it does not work, what methods are missing/what is the error?

I have read through the notes and have not seen any changes to IProperty or any non-deprecated methods.

Edited by Big_Bad_E
  • Like 1
Posted

Everything is the same except #2 which has been moved to its own method (fillStateContainer)

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)

Posted
13 hours ago, Big_Bad_E said:

Sorry totally managed to misread that!
I currently cannot test for 1.13.2 (gradle hates me :/), but the code should not change to my knowledge. If it does not work, what methods are missing/what is the error? 

I have read through the notes and have not seen any changes to IProperty or any non-deprecated methods.

Hi! This is what my Eclipse workspace area looks like when I tried your code. TY.

Xh2Vo7o.png

Posted

Don't just blindly copy the code others provide. For that matter never blindly copy other's code. In this case you are trying to use 1.12 code in a 1.13 environment.

You can always see how this is done in vanilla, something like BlockDispenser for example should be exactly what you need.

 

On 4/10/2019 at 12:22 AM, Big_Bad_E said:

assert placer != null;

I don't really see the point of this assertion. If the placer is null you will crash anyway one line later with a NPE. 

Posted
37 minutes ago, V0idWa1k3r said:

Don't just blindly copy the code others provide. For that matter never blindly copy other's code. In this case you are trying to use 1.12 code in a 1.13 environment. 

You can always see how this is done in vanilla, something like BlockDispenser for example should be exactly what you need.

 

I don't really see the point of this assertion. If the placer is null you will crash anyway one line later with a NPE. 

3

I do understand, it is just he asked what errors showed up as he could not figure out why it does not work as he could not see any notes saying it has been changed (and I know it has changed, hence why I am wanting to have some help on how to do it). I am aware that copying code blindly is bad, I have learnt. I am asking for rotating code in a java class as the vanilla code does not work and I have no idea what I am doing with it in 1.13.2 as the code has changed. What do I extend if I need to? What do I implement if I need to? What is the code for the placement? What is the code for checking the player? Where do I put the code? Can the code be in a class file, or does it have to be where I have set the properties? Is my code correct? The only answer I know is that I have no idea how to do it, and I cannot find any help on this. This is why I have turned to the forums - for answers.

Posted
4 hours ago, Billy Playz said:

I am asking for rotating code in a java class as the vanilla code does not work

Why? Vanilla code works perfectly fine for vanilla. In this case it should work perfectly for your use-case aswell as long as you implement it correctly.

 

4 hours ago, Billy Playz said:

I have no idea what I am doing with it in 1.13.2 as the code has changed.

If you have no idea what the code does you can look at vanilla's class and figure it out. Sure it changed but that doesn't stop you from figuring out how it works. You are not looking at magical spaghetti code of 10000+ lines after all.

 

4 hours ago, Billy Playz said:

What do I extend if I need to?

Since you are making a block you need to extend Block.

 

4 hours ago, Billy Playz said:

What do I implement if I need to?

Depends on what you want your block to do. Again, see vanilla for examples.

 

4 hours ago, Billy Playz said:

What is the code for the placement?

Again, you can see it for yourself in vanilla classes like BlockFurnace, BlockDispenser, BlockPiston, etc.

 

4 hours ago, Billy Playz said:

What is the code for checking the player?

What does this question even mean? Why do you need to be "checking" the player?

 

4 hours ago, Billy Playz said:

Where do I put the code?

...in your block class.

 

4 hours ago, Billy Playz said:

Can the code be in a class file, or does it have to be where I have set the properties?

I am sorry but if you are asking questons like these you need to learn basic java. You can't make a minecraft mod without understanding the fundamentals of the programming language you are using.

 

4 hours ago, Billy Playz said:

Is my code correct?

Your IDE tells you that.

 

4 hours ago, Billy Playz said:

This is why I have turned to the forums - for answers.

Well, here are your answers - learn java(at least the basics of it) and look at how vanilla does things. Then you can come back with more specific questions we would be able to answer without telling you to look at vanilla's code.

Posted (edited)
placer.getHorizontalFacing().getOpposite()

That line is what "checks the player" (checks where they are facing).

It seems a remap might of changed it, try using:

PropertyEnum<EnumFacing>

instead of

PropertyDirection

and set it to be:

new PropertyEnum<EnumFacing>("facing", EnumFacing.class, Collections2.filter(Lists.newArrayList(EnumFacing.values()), EnumFacing.Plane.HORIZONTAL);

 

 

Edited by Big_Bad_E
Posted (edited)
6 hours ago, V0idWa1k3r said:

Don't just blindly copy the code others provide. For that matter never blindly copy other's code. In this case you are trying to use 1.12 code in a 1.13 environment.

You can always see how this is done in vanilla, something like BlockDispenser for example should be exactly what you need.

 

I don't really see the point of this assertion. If the placer is null you will crash anyway one line later with a NPE. 

Because Intellij is annoying, and you never know. Makes it easier to debug I guess, also personal preference.

 

Also, yes, never blindly copy paste code, and if you are stuck you can always get code from vanilla blocks (that is where that code is from anyways)

 

I would suggest playing around in Java with other things, Forge is one of the hardest libraries out there. If you want to stick with Minecraft, Spigot is a lot easier. You could also mess around with Discord bots (https://github.com/DV8FromTheWorld/JDA/releases) or make games/software.

Edited by Big_Bad_E

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.