Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2][solved] Block getMetaFromState for multiple properties
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
DonKresenko

[1.12.2][solved] Block getMetaFromState for multiple properties

By DonKresenko, February 9, 2018 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018 (edited)

I have two properties in my block, LEVEL and FACING. I need to store the meta in order to save the properties.

 

I have this code

public int getMetaFromState(IBlockState state)
    {
        return ((Integer)state.getValue(LEVEL)).intValue();
    }

which saves LEVEL property. 

 

I need a way to save FACING property too.

Edited February 9, 2018 by DonKresenko
solved
  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018

Or even if I have 3 properties, then I would probably need TileEntity class

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018 (edited)

I found this in BlockEndPortalFrame, but I am not quite familiar with this

public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();

        if (((Boolean)state.getValue(EYE)).booleanValue())
        {
            i |= 4;
        }

        return i;
    }
Edited February 9, 2018 by DonKresenko
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018

You can store up to 4 bits of data in metadata.

If you want to store more than one property, you need to encode the data into one 4 bit number. One example:

 

Two properties: Facing (0-3) and Active (true or false).Facing needs two bits (values 0b00, 0b01, 0b10 and 0b11), Active needs one bit (values 0b0 and 0b1). Now you need to shift one over so that they do not collide (pseudocode follows):

activeBit = active ? 0b1 : 0b0;
activeShifted = activeBit << 2; // activeShifted is now 0b100 or 0b000
facingBits = facing; // 0b00 - 0b11
combined = activeShifted | facingBits // e.g. 0b101 for facing 0b01 and active 0b1

 

The reverse then has to be done when reading from metadata and is left as an exercise to the reader.

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018
7 minutes ago, diesieben07 said:

You can store up to 4 bits of data in metadata.

If you want to store more than one property, you need to encode the data into one 4 bit number. One example:

 

Two properties: Facing (0-3) and Active (true or false).Facing needs two bits (values 0b00, 0b01, 0b10 and 0b11), Active needs one bit (values 0b0 and 0b1). Now you need to shift one over so that they do not collide (pseudocode follows):


activeBit = active ? 0b1 : 0b0;
activeShifted = activeBit << 2; // activeShifted is now 0b100 or 0b000
facingBits = facing; // 0b00 - 0b11
combined = activeShifted | facingBits // e.g. 0b101 for facing 0b01 and active 0b1

 

The reverse then has to be done when reading from metadata and is left as an exercise to the reader.

Thank you for the information.

 

I have LEVEL property (0,6) that takes 3 bits and FACING (n, s, e, w) which takes 2 bits.

So I cannot do this cause I need 5 bits?

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018
2 minutes ago, DonKresenko said:

I have LEVEL property (0,6) that takes 3 bits and FACING (n, s, e, w) which takes 2 bits.

So I cannot do this cause I need 5 bits?

Exactly. You will need a TileEntity or two blocks.

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018

Alright. Thank you

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018 (edited)

I now decreased my property LEVEL to 2 bits (0-3)

 

I tried this code but it seems not to be working

public int getMetaFromState(IBlockState state)
    {
    	int lvl = ((Integer)state.getValue(LEVEL)).intValue();
    	lvl <<= 2;
    	int i = ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
    	// System.out.println(lvl+" | "+i+" = "+(lvl |= i));
    	lvl |= i;
    	
    	return lvl;
    }

What am I doing wrong?

Edited February 9, 2018 by DonKresenko
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018

That looks right. How does your getStateFromMeta look?

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018
3 minutes ago, diesieben07 said:

That looks right. How does your getStateFromMeta look?

That is getStateFromMeta method

 

I get this exception

Spoiler

java.lang.IllegalArgumentException: Cannot set property PropertyInteger{name=level, clazz=class java.lang.Integer, values=[0, 1, 2, 3]} to 4 on block testmod:store_block, it is not an allowed value

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018
Just now, DonKresenko said:

That is getStateFromMeta method

You did not post this...

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018 (edited)

sorry I read getMetaFromState

 

public IBlockState getStateFromMeta(int meta)
    {
    	EnumFacing enumfacing = EnumFacing.getFront(meta);

        if (enumfacing.getAxis() == EnumFacing.Axis.Y)
        {
            enumfacing = EnumFacing.NORTH;
        }
        
        return this.getDefaultState().withProperty(LEVEL, Integer.valueOf(meta)).withProperty(FACING, enumfacing);
    }
Edited February 9, 2018 by DonKresenko
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018

You can't just use the metadata as-is, you need to decode, i.e. do the inverse of what you are doing when encoding.

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018 (edited)

Ok.

public IBlockState getStateFromMeta(int meta)
    {
    	int lvl = meta>>2;
    	int face = meta & 0b11;
        
    	EnumFacing enumfacing = EnumFacing.getFront(face);

        if (enumfacing.getAxis() == EnumFacing.Axis.Y)
        {
            enumfacing = EnumFacing.NORTH;
        }
        
        return this.getDefaultState().withProperty(LEVEL, Integer.valueOf(lvl)).withProperty(FACING, enumfacing);
        
    }

This works for NORTH and SOUTH but not for the EAST and WEST

This is my problem now

 

(note that directional block is working, this is after reloading the world)

 

Edited February 9, 2018 by DonKresenko
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018

When using EnumFacing::getHorizontalIndex for encoding you must use EnumFacing.getHorizontal for decoding. EnumFacing.getFront only works with EnumFacing::getIndex.

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018

Alright I almost got it. The problem now is this

image

 

code:

public IBlockState getStateFromMeta(int meta)
    {
    	int lvl = meta>>2;
    	int face = meta & 0b11;
    	
    	EnumFacing enumfacing = EnumFacing.getHorizontal(face);
    	
    	if (enumfacing.getAxis() == EnumFacing.Axis.Y)
        {
            enumfacing = EnumFacing.NORTH;
        }
    	
        return this.getDefaultState().withProperty(LEVEL, Integer.valueOf(lvl)).withProperty(FACING, enumfacing);
        
    }

 

  • Quote

Share this post


Link to post
Share on other sites

DonKresenko    0

DonKresenko

DonKresenko    0

  • Stone Miner
  • DonKresenko
  • Members
  • 0
  • 63 posts
Posted February 9, 2018

Got it now :)

Thank you for your help

 

I removed this
 

if (enumfacing.getAxis() == EnumFacing.Axis.Y)
        {
            enumfacing = EnumFacing.NORTH;
        }

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45732 posts
Posted February 9, 2018

That doesn't look like a problem with the metadata, but more like you have a typo in your model or blockstate json.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2097

Draco18s

Draco18s    2097

  • Reality Controller
  • Draco18s
  • Members
  • 2097
  • 14037 posts
Posted February 9, 2018
2 hours ago, DonKresenko said:

.withProperty(LEVEL, Integer.valueOf(lvl))

You don't need to box here. withProperty(LEVEL, lvl) works just fine.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Draco18s
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Draco18s · Posted 6 minutes ago

      I don't know why you're having so much trouble. https://github.com/Draco18s/ReasonableRealism/blob/1.14.4/src/main/java/com/draco18s/harderfarming/EventHandlers.java#L42-L57
    • Draco18s
      JSON questions

      By Draco18s · Posted 11 minutes ago

      LootTableLoadEvent. So you don't overwrite other mods' additional seeds. Change the number of "rolls"
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 35 minutes ago

      package net.batonfack.fantasymod.client.renders; import net.batonfack.fantasymod.FantasyMod; import net.batonfack.fantasymod.client.models.ModelUnicornWithAbstracHorse; import net.batonfack.fantasymod.client.models.ModelUnicornWitoutAbstracHorse; import net.batonfack.fantasymod.entities.UnicornEntity; import net.minecraft.client.renderer.entity.AbstractHorseRenderer; import net.minecraft.client.renderer.entity.EntityRenderer; import net.minecraft.client.renderer.entity.EntityRendererManager; import net.minecraft.client.renderer.entity.MobRenderer; import net.minecraft.entity.passive.horse.AbstractHorseEntity; import net.minecraft.util.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.fml.client.registry.IRenderFactory; @OnlyIn(Dist.CLIENT) public class UnicornEntityRender extends AbstractHorseRenderer<UnicornEntity, ModelUnicornWithAbstracHorse<AbstractHorseEntity>> // <<---The Line with the error { //public UnicornEntityRender(EntityRendererManager manager) { // super(manager, new UnicornEntityModel(0), 0f); //} //private static ModelUnicornWitoutAbstracHorse<UnicornEntity> UnicornModel; public static final ResourceLocation unicorn = new ResourceLocation("fantasymod:" + "textures/entity/unicorn_entity.png"); private static float shadowOpaque = 0.0f; public UnicornEntityRender(EntityRendererManager manager, ModelUnicornWithAbstracHorse<AbstractHorseEntity> UnicornModel, float p_i50961_3_) { super(manager, new ModelUnicornWithAbstracHorse<>(0.0f), p_i50961_3_); } @Override protected ResourceLocation getEntityTexture(UnicornEntity entity) { return unicorn; } public static class RenderFactory implements IRenderFactory<UnicornEntity> { @Override public EntityRenderer<? super UnicornEntity> createRenderFor(EntityRendererManager manager) { return new UnicornEntityRender(manager, new ModelUnicornWithAbstracHorse<>(0.0f), shadowOpaque); } } }  
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 36 minutes ago

    • The_Unkown675
      Can't connect to own server; No syncable config

      By The_Unkown675 · Posted 47 minutes ago

      Server debug file - https://paste.gg/p/anonymous/0671148362b5476fbce55b8922262dad Latest log file -https://paste.gg/p/anonymous/bab7c2b3ca0947df9b10bce9c2632c22 Hope this helps! If you need anything else please let me know, I'll be happy to help, thank you for your help!
  • Topics

    • Oliviafrostpaw
      13
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw
      Started December 8

    • plugsmustard
      1
      JSON questions

      By plugsmustard
      Started 2 hours ago

    • DragonITA
      46
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA
      Started Monday at 10:06 AM

    • The_Unkown675
      2
      Can't connect to own server; No syncable config

      By The_Unkown675
      Started 18 hours ago

    • PolarBr0
      1
      Forge 1.12.2 crashes and wont load world!

      By PolarBr0
      Started 1 hour ago

  • Who's Online (See full list)

    • tesla0740
    • fcelon
    • _alycia_riley_
    • Hamzat213
    • Label
    • Oliviafrostpaw
    • AdieCraft
    • Draco18s
    • MoeBoy76
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2][solved] Block getMetaFromState for multiple properties
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community