Jump to content

[1.11] Block with two properties?


Awesome_Spider

Recommended Posts

A while ago I was having trouble with a block always facing north. This is fixed now, however now I'm on to a new block which requires two properties: Facing and Active. Facing is an EnumFacing and Active is a boolean. I am stuck with what to do with Block#getMetaFromState and Block#getStateFromMeta.

 

My block code so far:

public class BlockPurifier extends BlockTileEntity<TileEntityPurifier> {

    public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
    public static final PropertyBool ACTIVE = PropertyBool.create("active");

    public BlockPurifier() {
        super(Material.ROCK, "purifier");

        IBlockState defaultState = this.blockState.getBaseState();
        defaultState.withProperty(FACING, EnumFacing.NORTH);
        defaultState.withProperty(ACTIVE, false);

        this.setDefaultState(defaultState);

        setHardness(3f);
        setResistance(5f);
        setHarvestLevel("pickaxe", 0);
    }

    @Override
    public Class<TileEntityPurifier> getTileEntityClass() {
        return TileEntityPurifier.class;
    }

    @Nullable
    @Override
    public TileEntityPurifier createTileEntity(World world, IBlockState state) {
        return new TileEntityPurifier();
    }

    @Override
    public BlockStateContainer createBlockState() {
        return new BlockStateContainer(this, FACING);
    }

    @Override
    public int getMetaFromState(IBlockState state) {
        return ((EnumFacing)state.getValue(FACING)).getHorizontalIndex(); //How do I add a boolean to this?
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); //How do I add a boolean to this?
    }

    @Override
    public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack) {
        EnumFacing entityFacing = entity.getHorizontalFacing();

        if(!world.isRemote) {
            if(entityFacing == EnumFacing.NORTH) {
                entityFacing = EnumFacing.SOUTH;
            } else if(entityFacing == EnumFacing.EAST) {
                entityFacing = EnumFacing.WEST;
            } else if(entityFacing == EnumFacing.SOUTH) {
                entityFacing = EnumFacing.NORTH;
            } else if(entityFacing == EnumFacing.WEST) {
                entityFacing = EnumFacing.EAST;
            }

            world.setBlockState(pos, state.withProperty(FACING, entityFacing), 2);
        }
    }
}

Link to comment
Share on other sites

You need to add 4 to the meta when it is off or on your preference, and then decode that your self in the getStateFromMeta.

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

If your block has 6 facings, you need to add 8 (or better yet, boolean-or with 8 ) when getting the meta.

If it has 4 (BlockHorizontal) then you can get away with adding 4.

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

What those worthies are saying is that meta has 4 bits, and facing is going to use up 2 or 3 of them. That leaves you at least the 1 bit needed for a boolean value. With that much Forge knowledge, you should be able to figure out a way to pack both values into the 4-bit meta and then unpack them again (that's fundamental programming technique, sometimes taught here, but you should get into the habit of finding external references for standard techniques).

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

On 2/7/2017 at 8:17 PM, Daeruin said:

See definition #2: https://www.merriam-webster.com/dictionary/worthies

"a worthy or prominent person"

I looked at that guess i didn't scroll down far enough; thanks.

Edited by Animefan8888
Typo

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

Does your block have 4 facings (like the furnace) or 6 (like the piston)? If you have 4 facings, you can add/subtract 4 to get the other value, if you need 6 you'd have to add 8. Or better, use bitwise operators (look at BlockOldLeaves for an example in Block#getStateFromMeta and Block#getMetaFromState).

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Don't subtract. Wrong. Bad.

 

int eightBit = (meta & 7);

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

Ok. So would this work? I can't test it because the models/textures aren't finished.

 

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

        if (!state.getValue(ACTIVE)) {
            meta |= 4;
        }

        return meta;
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(ACTIVE, (meta & 4) == 0);
    }

 

Edited by Awesome_Spider
Link to comment
Share on other sites

Man, if only the forums worked and I could view spoilers...

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

You can't? Here:

@Override
    public int getMetaFromState(IBlockState state) {
        int meta = 0;

        meta = meta | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();

        if (!state.getValue(ACTIVE)) {
            meta |= 4;
        }

        return meta;
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(ACTIVE, (meta & 4) == 0);
    }

 

Link to comment
Share on other sites

Won't work when ACTIVE is false, because your meta -> facing will pass in a value greater than 4, which the enum won't know what to do with.  You need to &3 that bit to strip off the 4th bit.

 

(And no, I can't view spoilers, I don't know why. The JS on this forum is broken as all getout for me: I can't start new threads, I can't use post preview, and I can't open spoilers)

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

9 hours ago, Draco18s said:

(And no, I can't view spoilers, I don't know why. The JS on this forum is broken as all getout for me: I can't start new threads, I can't use post preview, and I can't open spoilers)

 

I had this issue on another site once, and it resolved once I removed a Chrome extension related to pop-ups. Do you maybe have a Firefox (?) extension installed blocking it? Or try disabling each extension one-by-one and see if that fixes it.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

1 hour ago, larsgerrits said:

I had this issue on another site once, and it resolved once I removed a Chrome extension related to pop-ups. Do you maybe have a Firefox (?) extension installed blocking it? Or try disabling each extension one-by-one and see if that fixes it.

That gives me a result that a Greasemonkey script that I had to strip out the image in someone's (annoyingly large) signature is at fault.

 

By which I mean, an empty Greasemonkey script breaks everything (as in, only comments, and disabling it makes spoilers work). Which makes no fucking sense.  At all.  And tells me, once again, that this forum's javascript is shitty.  The infinite recursion trying to start a new thread was the first red flag.

 

Huh, turns out the infinite recursion is also because of the greasemonkey script.

 

That's fucking idiotic.

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

I went over to Invision Power Services to complain/file a bug report, only to not know the password to my existing account (I can't make an account because my username AND email are in use by an existing account) but I can't reset my password because there are no user accounts using that email address.  What.

 

Oh, and despite my language and apparent distaste for this forum software, it's not the worst forum software I've had the displeasure of using.  Nope, that'd be the wreck that is the Dungeon Defenders 2 forums, built by Duxter. Everything uses AJAX, which means navigating pages skips over your browser history.  It also means they had to hack together a way to link to specific pages/posts, using redirects (further fucking over the browser history).  Page numbers above 10 display as the number 10.  There's no "report to moderator" feature.  At one point removing the flag that marks someone as a troll (moderator feature, muting that person's posts) also made that person a moderator.  I'm not making that up.

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

Just now, Leomelonseeds said:

What are you using the active boolean for? My custom furnace has a boolean isBurning that is not encoded into the metadata but still works.

"And still works" covers a lot of ways it could be handled. Vanilla's furnace uses two whole blocks for it, whereas a TE could store the value and it could be retrieved using getActualState, so that statement by itself isn't actually helpful.  Or even necessarily related.  There's lots of reasons one might want to have two properties on their block.

 

I've got one with three.

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

Ok. Thanks. Now how would I change my blockstate json file to incorporate this extra property? I have an animated texture for when "active" is true and a normal one when "active" is false.

 

{
  "forge_marker": 1,
  "defaults": {
    "textures": {
      "front": "roboticraft:blocks/purifier_front_inactive",
      "top": "roboticraft:blocks/purifier_side",
      "side": "roboticraft:blocks/steam_engine_side",
      "particle": "roboticraft:blocks/steam_engine_side"
    }
  },
  "variants": {
    "normal": {
      "model": "orientable"
    },
    "inventory": {
      "model": "orientable"
    },
    "facing=north": {
      "model": "orientable"
    },
    "facing=south": {
      "model": "orientable",
      "y": 180
    },
    "facing=west": {
      "model": "orientable",
      "y": 270
    },
    "facing=east": {
      "model": "orientable",
      "y": 90
    }
  }
}

 

Btw, I understand you not liking this new software. I have this issue when visiting the site on IPad, for a while we couldn't edit posts, the BBCode capabilities are lacking, putting any text after spoilers puts the text in the spoiler instead of on the outside, the forum doesn't notify through email anymore, etc. They are working it out I'm sure, it is a new software after all.

Link to comment
Share on other sites

1 minute ago, Awesome_Spider said:

the forum doesn't notify through email anymore

 
 

You can change every notification setting here: http://www.minecraftforge.net/forum/notifications/options/

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Oops. I had copied my other machine's blockstate file and didn't change every texture path over. That's embarrassing. Here is the updated json file:

 "defaults": {
    "textures": {
      "front": "roboticraft:blocks/purifier_front_inactive",
      "top": "roboticraft:blocks/purifier_side",
      "side": "roboticraft:blocks/purifier_side",
      "particle": "roboticraft:blocks/purifier_side"
    }
  },
  "variants": {
    "normal": {
      "model": "orientable"
    },
    "inventory": {
      "model": "orientable"
    },
    "facing=north": {
      "model": "orientable"
    },
    "facing=south": {
      "model": "orientable",
      "y": 180
    },
    "facing=west": {
      "model": "orientable",
      "y": 270
    },
    "facing=east": {
      "model": "orientable",
      "y": 90
    }
  }
}

 

Does anyone know how I add another property to this?

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