Jump to content

creating item that change the state of a block


matt1999rd

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
    }
}

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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

    • I want to download Forge 1.20.6, but when I press download, it redirects me to Adfocus and does not provide any download. I have never encountered such a problem when I downloaded Forge 1.18.2 at least 12 months ago.
    • did you find any solutions? i have the same problem and also that mahito doesnt transform
    • Here's a simple example to solve this particular issue: public class ModArmorMaterials { public static final DeferredRegister<ArmorMaterial> ARMORS = DeferredRegister.create(Registries.ARMOR_MATERIAL, MyModName.MOD_ID); public static final RegistryObject<ArmorMaterial> COPPER = registerArmor("copper", Util.make(new EnumMap<>(ArmorItem.Type.class), enumMap -> { enumMap.put(ArmorItem.Type.BOOTS, 1); enumMap.put(ArmorItem.Type.LEGGINGS, 4); enumMap.put(ArmorItem.Type.CHESTPLATE, 5); enumMap.put(ArmorItem.Type.HELMET, 2); enumMap.put(ArmorItem.Type.BODY, 4); }), 12, SoundEvents.ARMOR_EQUIP_IRON, 0.0F, 0.0F, () -> Ingredient.of(Items.COPPER_INGOT)); private static RegistryObject<ArmorMaterial> registerArmor(String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient) { List<ArmorMaterial.Layer> pLayerList = List.of(new ArmorMaterial.Layer(new ResourceLocation(pGroup))); return registerArmor(pGroup, pEnumMap, pEnchantmentValue, pEquipSound, pToughness, pKnockbackResistance, pRepairIngredient, pLayerList); } private static RegistryObject<ArmorMaterial> registerArmor( String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient, List<ArmorMaterial.Layer> pLayerList) { return ARMORS.register(pGroup, () -> new ArmorMaterial(pEnumMap, pEnchantmentValue, pEquipSound, pRepairIngredient, pLayerList, pToughness, pKnockbackResistance)); } public static void register(IEventBus eventBus) { ARMORS.register(eventBus); } } Essentially, the TierSortingRegistry has been removed and so you now need to set up your own DeferredRegister. Please see the minecraftforge GitHub for more information; I found this solution through their issues (https://github.com/MinecraftForge/MinecraftForge/issues/9961)
    • How did I even leave that in there? I’ll try it in a bit, that very well could be it.
  • Topics

×
×
  • Create New...

Important Information

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