Jump to content

Blockstates Property for a Block


Siqhter

Recommended Posts

I have read past forum posts addressing this, but I still can't seem to figure it out. I have a block that should create a variant for each EnumDyeColor. (So, 16 altogether.) I have already gotten this same block to work using a "facing" property so that it faces the player when placed, but I can't seem to get the "color" property to work. It generates 16 items without textures in the inventory and without textures when placed. I am pretty sure it's a JSON thing, because of the error which I will post below. Thanks.

Spoiler

Exception loading model for variant test_block#color=6,facing=east for blockstate "test_block"

The console also says "suppressed 59 additional model loading errors."

Here is my JSON file.

Spoiler

{
  "forge_marker": 1,
  "variants": {
    "type": {
      "normal": {
        "model": "my_mod:test_block"
      }
    },

    "facing=north": { "model": "my_mod:test_block", "y": 270 },
    "facing=east": { "model": "my_mod:test_block", "y": 0 },
    "facing=south": { "model": "my_mod:test_block", "y": 90 },
    "facing=west": { "model": "my_mod:test_block", "y": 180 },

    "color": {
      "0": {
        "textures": {
          "top": "blocks/hardened_clay_stained_white"
        }
      },
      "1": {
        "textures": {
          "top": "blocks/hardened_clay_stained_orange"
        }
      },
      "2": {
        "textures": {
          "top": "blocks/hardened_clay_stained_magenta"
        }
      },
      "3": {
        "textures": {
          "top": "blocks/hardened_clay_stained_light_blue"
        }
      },
      "4": {
        "textures": {
          "top": "blocks/hardened_clay_stained_yellow"
        }
      },
      "5": {
        "textures": {
          "top": "blocks/hardened_clay_stained_lime"
        }
      },
      "6": {
        "textures": {
          "top": "blocks/hardened_clay_stained_pink"
        }
      },
      "7": {
        "textures": {
          "top": "blocks/hardened_clay_stained_gray"
        }
      },
      "8": {
        "textures": {
          "top": "blocks/hardened_clay_stained_silver"
        }
      },
      "9": {
        "textures": {
          "top": "blocks/hardened_clay_stained_cyan"
        }
      },
      "10": {
        "textures": {
          "top": "blocks/hardened_clay_stained_purple"
        }
      },
      "11": {
        "textures": {
          "top": "blocks/hardened_clay_stained_blue"
        }
      },
      "12": {
        "textures": {
          "top": "blocks/hardened_clay_stained_brown"
        }
      },
      "13": {
        "textures": {
          "top": "blocks/hardened_clay_stained_green"
        }
      },
      "14": {
        "textures": {
          "top": "blocks/hardened_clay_stained_red"
        }
      },
      "15": {
        "textures": {
          "top": "blocks/hardened_clay_stained_black"
        }
      }
    }
  }
}

And just to clarify, the block model is loading a "test_block" in, just not generating the blockstate variants for the other 16 colors.

Link to comment
Share on other sites

  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

You can't have that many states.  IBlockState is still baked by a 1 nibble (half byte) metadata that can hold only 16 values. 

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

You can have UnlistedProperties, or create a new block for every color. I recommend the 2nd one

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)

Link to comment
Share on other sites

6 hours ago, Cadiboo said:

You can have UnlistedProperties, or create a new block for every color. I recommend the 2nd one

 

Unlisted properties aren't used to determine which model to render for a block, they're used by custom block models to determine how they should render. The correct solution here (while keeping a single block) would be to store the extra data in a TileEntity and use regular properties that have their values set in Block#getActualState. Though Mojang is moving away from having a single block for all colours with The Flattening in 1.13, so it may be best to split the colours into separate blocks now (as Cadiboo suggested).

 

I have an example of a block with a colour and a facing here: Block, TileEntity, blockstates file

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Write to NBT - write your data to a NBTTagCompound for serialisation (saving)

Read from NBT - read your data from a NBTTagCompound for deserialisation (loading)

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)

Link to comment
Share on other sites

On 12/14/2018 at 12:24 AM, Choonster said:

I have an example of a block with a colour and a facing here

Huh, I looked through that, and my JSON file is formatted the same way, but it gives me an Exception loading model for variant error. I made sure to register the tileentity, but maybe I'm missing something else with the actual model?

Link to comment
Share on other sites

Show your new code? Preferably as a GitHub repository 

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)

Link to comment
Share on other sites

Yeah, I've been getting a repository set up. For now, I'll just post the code directly.

Spoiler

public class BlockCounter extends BlockColored {

    public static final IProperty<EnumFacing> FACING = PropertyDirection.create("facing");

    public static final AxisAlignedBB RED_COUNTER_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);

    public BlockCounter(final Material material) {
        super(Material.CLAY);
        setCreativeTab(CreativeTabs.DECORATIONS);
    }

    @Override
    public boolean isOpaqueCube(IBlockState state) {
        return false;
    }

    @Override
    public boolean isFullCube(IBlockState state) {
        return false;
    }

    @Override
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
        return RED_COUNTER_AABB;
    }

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

    @Override
    public boolean hasTileEntity(final IBlockState state) {
        return true;
    }

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

    @Nullable
    public TileEntityCounter getTileEntity(final IBlockAccess world, final BlockPos pos) {
        return (TileEntityCounter) world.getTileEntity(pos);
    }

    public EnumFacing getFacing(final IBlockAccess world, final BlockPos pos) {
        final TileEntityCounter tileEntity = getTileEntity(world, pos);
        return tileEntity != null ? tileEntity.getFacing() : EnumFacing.NORTH;
    }

    public void setFacing(final IBlockAccess world, final BlockPos pos, final EnumFacing facing) {
        final TileEntityCounter tileEntity = getTileEntity(world, pos);
        if (tileEntity != null) {
            tileEntity.setFacing(facing);
        }
    }

    @Override
    public void onBlockPlacedBy(final World worldIn, final BlockPos pos, final IBlockState state, final EntityLivingBase placer, final ItemStack stack) {
        setFacing(worldIn, pos, EnumFacing.getDirectionFromEntityLiving(pos, placer).getOpposite());
    }

    @Override
    public IBlockState getActualState(final IBlockState state, final IBlockAccess worldIn, final BlockPos pos) {
        return state.withProperty(FACING, getFacing(worldIn, pos));
    }

    @Override
    public void getSubBlocks(CreativeTabs item, NonNullList<ItemStack> items) {
        for (int i = 0; i < EnumDyeColor.values().length; i++) {
            items.add(new ItemStack(this, 1, i));
        }
    }

    @Override
    public boolean onBlockActivated(final World worldIn, final BlockPos pos, final IBlockState state, final EntityPlayer playerIn, final EnumHand hand, final EnumFacing side, final float hitX, final float hitY, final float hitZ)
    {
        final ItemStack heldItem = playerIn.getHeldItem(hand);

        if (!heldItem.isEmpty()) {
            final Optional<EnumDyeColor> dyeColour = DyeUtils.colorFromStack(heldItem);
            if (dyeColour.isPresent()) {
                final boolean success = recolorBlock(worldIn, pos, side, dyeColour.get());
                if (success && !playerIn.isCreative()) {
                    heldItem.shrink(1);
                }
                return true;
            }
        }
        return false;
    }
}
Spoiler

public class TileEntityCounter extends TileEntity {

    private EnumFacing facing = EnumFacing.NORTH;

    public EnumFacing getFacing() {
        return facing;
    }

    public void setFacing(final EnumFacing facing) {
        this.facing = facing;
        markDirty();
    }

    @Override
    public void readFromNBT(final NBTTagCompound compound) {
        super.readFromNBT(compound);
        facing = EnumFacing.getFront(compound.getInteger("facing"));
    }

    @Override
    public NBTTagCompound writeToNBT(final NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setInteger("facing", facing.getIndex());
        return compound;
    }

    private void notifyBlockUpdate() {
        final IBlockState state = getWorld().getBlockState(getPos());
        getWorld().notifyBlockUpdate(getPos(), state, state, 3);
    }

    @Override
    public void markDirty() {
        super.markDirty();
        notifyBlockUpdate();
    }

    @Override
    public NBTTagCompound getUpdateTag() {
        return writeToNBT(new NBTTagCompound());
    }

    @Nullable
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        return new SPacketUpdateTileEntity(getPos(), 0, getUpdateTag());
    }

    @Override
    public void onDataPacket(final NetworkManager net, final SPacketUpdateTileEntity pkt) {
        readFromNBT(pkt.getNbtCompound());
        notifyBlockUpdate();
    }

    @Override
    public boolean shouldRefresh(final World world, final BlockPos pos, final IBlockState oldState, final IBlockState newSate) {
        return oldState.getBlock() != newSate.getBlock();
    }
}

These are the two java classes for the Block itself, and then the TileEntity.

Link to comment
Share on other sites

I've been having issues with the repo right now. I duplicated some files accidentally, and some of the old files are still in it. Here is the blockstate and model file. The java classes are the ones posted above. Thanks.

Spoiler

{
  "forge_marker": 1,
  "defaults": {
    "model": "testmod:counter"
  },
  "variants": {
    "facing": {
      "down": {
        "x": 90
      },
      "up": {
        "x": 270
      },
      "north": {
        "y": 270
      },
      "south": {
        "y": 90
      },
      "east": {
        "y": 0
      },
      "west": {
        "y": 180
      }
    },
    "color": {
      "white": {
        "textures": {
          "top": "blocks/hardened_clay_stained_white"
        }
      },
      "orange": {
        "textures": {
          "top": "blocks/hardened_clay_stained_orange"
        }
      },
      "magenta": {
        "textures": {
          "top": "blocks/hardened_clay_stained_magenta"
        }
      },
      "light_blue": {
        "textures": {
          "top": "blocks/hardened_clay_stained_light_blue"
        }
      },
      "yellow": {
        "textures": {
          "top": "blocks/hardened_clay_stained_yellow"
        }
      },
      "lime": {
        "textures": {
          "top": "blocks/hardened_clay_stained_lime"
        }
      },
      "pink": {
        "textures": {
          "top": "blocks/hardened_clay_stained_pink"
        }
      },
      "gray": {
        "textures": {
          "top": "blocks/hardened_clay_stained_gray"
        }
      },
      "silver": {
        "textures": {
          "top": "blocks/hardened_clay_stained_silver"
        }
      },
      "cyan": {
        "textures": {
          "top": "blocks/hardened_clay_stained_cyan"
        }
      },
      "purple": {
        "textures": {
          "top": "blocks/hardened_clay_stained_purple"
        }
      },
      "blue": {
        "textures": {
          "top": "blocks/hardened_clay_stained_blue"
        }
      },
      "brown": {
        "textures": {
          "top": "blocks/hardened_clay_stained_brown"
        }
      },
      "green": {
        "textures": {
          "top": "blocks/hardened_clay_stained_green"
        }
      },
      "red": {
        "textures": {
          "top": "blocks/hardened_clay_stained_red"
        }
      },
      "black": {
        "textures": {
          "top": "blocks/hardened_clay_stained_black"
        }
      }
    }
  }
}

Spoiler

{
    "textures": {
        "particle": "blocks/concrete_white",
        "bottom": "blocks/concrete_white",
        "top": "blocks/hardened_clay_stained_red"
    },
    "display": {
        "gui": {
            "rotation": [ 30, 135, 0 ],
            "translation": [ 0, 0, 0 ],
            "scale": [ 0.625, 0.625, 0.625 ]
        },
        "ground": {
            "rotation": [ 0, 0, 0 ],
            "translation": [ 0, 3, 0 ],
            "scale": [ 0.25, 0.25, 0.25 ]
        },
        "fixed": {
            "rotation": [ 0, 0, 0 ],
            "translation": [ 0, 0, 0 ],
            "scale": [ 0.5, 0.5, 0.5 ]
        },
        "thirdperson_righthand": {
            "rotation": [ 75, 45, 0 ],
            "translation": [ 0, 2.5, 0 ],
            "scale": [ 0.375, 0.375, 0.375 ]
        },
        "thirdperson_lefthand": {
            "rotation": [ 75, 225, 0 ],
            "translation": [ 0, 2.5, 0 ],
            "scale": [ 0.375, 0.375, 0.375 ]
        },
        "firstperson_righthand": {
            "rotation": [ 0, 45, 0 ],
            "translation": [ 0, 0, 0 ],
            "scale": [ 0.40, 0.40, 0.40 ]
        },
        "firstperson_lefthand": {
            "rotation": [ 0, 225, 0 ],
            "translation": [ 0, 0, 0 ],
            "scale": [ 0.40, 0.40, 0.40 ]
        }
    },
    "elements": [
        {
            "name": "Cube",
            "from": [ 2.0, 0.0, 0.0 ],
            "to": [ 16.0, 14.0, 16.0 ],
            "faces": {
                "north": { "texture": "#bottom", "uv": [ 0.0, 0.0, 16.0, 14.0 ] },
                "east": { "texture": "#bottom", "uv": [ 1.0, 0.0, 15.0, 14.0 ] },
                "south": { "texture": "#bottom", "uv": [ 0.0, 0.0, 16.0, 14.0 ] },
                "west": { "texture": "#bottom", "uv": [ 1.0, 0.0, 15.0, 14.0 ] },
                "up": { "texture": "#bottom", "uv": [ 0.0, 0.0, 16.0, 14.0 ] },
                "down": { "texture": "#bottom", "uv": [ 0.0, 0.0, 16.0, 14.0 ] }
            }
        },
        {
            "name": "Cube",
            "from": [ 0.0, 14.0, 0.0 ],
            "to": [ 16.0, 16.0, 16.0 ],
            "faces": {
                "north": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 2.0 ] },
                "east": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 2.0 ] },
                "south": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 2.0 ] },
                "west": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 2.0 ] },
                "up": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 16.0 ] },
                "down": { "texture": "#top", "uv": [ 0.0, 0.0, 16.0, 16.0 ] }
            }
        }
    ]
}

 

Link to comment
Share on other sites

On 12/16/2018 at 2:45 AM, Siqhter said:

@Override public IBlockState getActualState(final IBlockState state, final IBlockAccess worldIn, final BlockPos pos) { return state.withProperty(FACING, getFacing(worldIn, pos)); }

You are not including the color in your state here.

 

On 12/16/2018 at 12:13 AM, Siqhter said:

However, using the getSubBlocks method, the textures for all other 15 blocks are not registering.

What is your issue? Are item textures not there? Or those of blocks? If it's blocks then see above, otherwise you need to show how you are registering models for your itemblocks.

Link to comment
Share on other sites

17 minutes ago, Siqhter said:

Ok, so would I return COLOR and  FACING?

Yes.

18 minutes ago, Siqhter said:

Because right now it gives an error if I pass in COLOR.

What is the mysterious "it"? The compiler? The IDE? The game? If it's the first two then you should be able to fix it, since it's just a compile error and those are easily fixable with some java knowledge. If it's the latter then post the error log.

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.