Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello everyone,

For a 1.14 mod,

I try to create an item that change the state of a block. To do that I'm using setBlockState method of World object. The code for doing this use a variable that confirmed that the rest of the code is working well.

As I search into setBlockState method I find these lines :

/**
 * Sets a block state into this world.Flags are as follows:
 * 1 will cause a block update.
 * 2 will send the change to clients.
 * 4 will prevent the block from being re-rendered.
 * 8 will force any re-renders to run on the main thread instead
 * 16 will prevent neighbor reactions (e.g. fences connecting, observers pulsing).
 * 32 will prevent neighbor reactions from spawning drops.
 * 64 will signify the block is being moved.
 * Flags can be OR-ed
 */

 

 

I think this may change the behaviour but I try for the flag that match the best what I wanted and I trye for 7 and 11 but none of them changes the behaviour.

 

Here is the method where I use the method setBlockState :

private void updatePoweredState(World world, BlockState state,BlockPos pos,BlockState oldState) {
    isPowered = !isPowered;
    if (isPowered){
        System.out.println("etat du block : turn");
        world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.TURN),flags);

    }else {
        System.out.println("etat du block : straight");
        world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.STRAIGHT),flags);
    }
}

 

flags has the value 7 or 11.

 

what the code is doing is changing the state switchposition and then almost instantly changing the state to the previous one.

Edited by matt1999rd

  • Author

Thanks for advice I've corrected my code and here is the new method updatePoweredState :

public void updatePoweredState(World world, BlockState state, BlockPos pos, int flags) {
    if (world.isRemote){
        boolean isPowered = state.get(SWITCH_POSITION).getName().equals("turn");
        System.out.println(isPowered);
        if (isPowered){
            world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.TURN));
        }else {
            world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.STRAIGHT));
        }
        System.out.println(state.get(SWITCH_POSITION));
    }
}

I'm using a proper EnumProperty for my block based on enum called Corners which contains these two attributes. But it's unfortunately not working because every time I use my item to change the blockstate from Corners.STRAIGHT to Corners.TURN something is changing to Corners.STRAIGHT again one tick after and I don't know where I can block this action.

 

Another strange action is when some block are nearby this actually change the RailShape properties.

Edited by matt1999rd

49 minutes ago, matt1999rd said:

I'm using a proper EnumProperty

Then why are you converting it to a string?

 

boolean isPowered = state.get(SWITCH_POSITION) == Corners.TURN

 

And I don't think that this is what you want to check anyway.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author
11 minutes ago, diesieben07 said:

You are now checking for client-side.

 

What on earth is this?

I will give the full definition of this :

protected static EnumProperty<Corners> SWITCH_POSITION ;

static  {
    SWITCH_POSITION = EnumProperty.create("switch_position",Corners.class,(corner)-> {
        return (corner == Corners.STRAIGHT || corner == Corners.TURN);
    });
}

here is the definition of my EnumProperty

It use the class enum called Corners as used in the BlockStateProperty

And here is the Corners class :

public enum Corners implements IStringSerializable {
    TURN(0,"turn"),
    STRAIGHT(1,"straight"),
    TURN_LEFT(2,"turn_left"),
    TURN_RIGHT(3,"turn_right");


    private final int meta ;
    private final String name;



    private Corners(int meta, String name) {
        this.meta=meta;
        this.name=name;

    }

    @Override
    public String getName() {
        return this.name;
    }
}

 

 

  • Author

I use string to compare rather than using boolean because it seems not to work with booleanProperty POWERED (that the property I used before)

  • Author

I've understand here is the code :

public void updatePoweredState(World world, BlockState state, BlockPos pos, int flags) {
    if (!world.isRemote){
        boolean isPowered = (state.get(SWITCH_POSITION) == Corners.TURN);
        if (!isPowered){
            world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.TURN),flags);
        }else {
            world.setBlockState(pos,state.with(SWITCH_POSITION,Corners.STRAIGHT),flags);
        }
        System.out.println(state.get(SWITCH_POSITION));
    }
}

 

I don't need string but this enumproperty will be usefull in my future block I filtered the position from now

Edited by matt1999rd

  • Author

so have you got an idea of what is not working ? I'm pretty sure it's the function setBlockState but I do not understood what flags changes..

3 hours ago, matt1999rd said:

the state is not changing and it is not working

Where do you call updatePoweredState?

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.

  • Author

now the problem is solved but in fact one problem remain is that the main block I use for creating the block is herited from AbstractRailBlock so when I put the block within the game the switch change the shape when I call this function

  • Author

Rail are changing depending on the other rail but I don't know if I can modify the function that change the position regarding neighbor rail..

  • Author

my block is actally in this tags as I've overriden the function isIn. I think that may be the point.

I've not tried though

5 hours ago, matt1999rd said:

my block is actally in this tags as I've overriden the function isIn. I think that may be the point.

I've not tried though

That's not how that works.

https://github.com/Draco18s/ReasonableRealism/tree/1.14.4/src/main/resources/data/minecraft/tags/blocks

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.