Using custom code snippets in procedures with blockstates

Started by Dary on

Topic category: Advanced modding

Last seen on 12:17, 15. Mar 2024
Joined Nov 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Using custom code snippets in procedures with blockstates

Hello,

I recently came across this awesome tutorial

https://mcreator.net/forum/54347/tutorial-how-use-cauldrons-procedures

and thought that it could help me with my mod.

​ I'm trying to make my flower pot change material of the pot by right-click with a dye.I made some changes in the pot class,folowing modding tutorials, to create new PropertyInteger for colors.Changes were accepted by a recompilation, the client runs. But when I tried to run a right-click procedure folowing this tutorial, I get plenty of errors.It can't find almost every variable=(The same was with the changed code made by hand,it cannot access PropertyInteger variable from there...

I know, that my silly attempt to do this hard work is not the same as working vanilla cauldron things, but I really want to understand what's wrong.

Thanks in advance for any help or response)

recompilation error:

C:\Users\Dary\MCreatorWorkspaces\travelers_dream_blocks\build\sources\main\java\net\mcreator\travelers_dream_blocks\MCreatorFlowerPotwithflowerOnBlockRightClicked.java:48: error: cannot find symbol
			int color = blockState.getValue(MCreatorFlowerPotwithflower.color);
			                                                           ^
  symbol:   variable color
  location: class MCreatorFlowerPotwithflower
C:\Users\Dary\MCreatorWorkspaces\travelers_dream_blocks\build\sources\main\java\net\mcreator\travelers_dream_blocks\MCreatorFlowerPotwithflowerOnBlockRightClicked.java:48: error: cannot find symbol
			int color = blockState.getValue(MCreatorFlowerPotwithflower.color);
			            ^
  symbol:   variable blockState
  location: class MCreatorFlowerPotwithflowerOnBlockRightClicked
C:\Users\Dary\MCreatorWorkspaces\travelers_dream_blocks\build\sources\main\java\net\mcreator\travelers_dream_blocks\MCreatorFlowerPotwithflowerOnBlockRightClicked.java:51: error: method withProperty in interface IBlockState cannot be applied to given types;
			world.setBlockState(pos, blockState.withProperty(color, Integer.valueOf(4)), 3);
			                                   ^
  required: IProperty<T>,V
  found: int,Integer
  reason: cannot infer type-variable(s) T,V
    (argument mismatch; int cannot be converted to IProperty<T>)
  where T,V are type-variables:
    T extends Comparable<T> declared in method <T,V>withProperty(IProperty<T>,V)
    V extends T declared in method <T,V>withProperty(IProperty<T>,V)
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s

my flower pot procedure is almost the same as in the tutorial :I check for a flower block pot with "if getblock at x,y,z is flowerpot and item in entity main hand is dye4 do" and then lines in code snippets:

int color = blockState.getValue(MCreatorFlowerPotwithflower.color); // my attempt to get PropertyInteger value from pot class

BlockPos pos = new BlockPos(x, y, z);

IBlockState blockState = world.getBlockState(pos);

world.setBlockState(pos,  blockState.withProperty(color, Integer.valueOf(4)), 3);

 

I don't like the first and the last lines,I don't know exactly how to  get access to color property

My flower pot class:

package net.mcreator.travelers_dream_blocks;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraft.world.World;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.Item;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.material.Material;
import net.minecraft.block.SoundType;
import net.minecraft.block.Block;

import java.util.HashMap;

@Elementstravelers_dream_blocks.ModElement.Tag
public class MCreatorFlowerPotwithflower extends Elementstravelers_dream_blocks.ModElement {
	@GameRegistry.ObjectHolder("travelers_dream_blocks:flowerpotwithflower")
	public static final Block block = null;

	public MCreatorFlowerPotwithflower(Elementstravelers_dream_blocks instance) {
		super(instance, 161);
	}

	@Override
	public void initElements() {
		elements.blocks.add(() -> new BlockCustom());
		elements.items.add(() -> new ItemBlock(block).setRegistryName(block.getRegistryName()));
	}

	@SideOnly(Side.CLIENT)
	@Override
	public void registerModels(ModelRegistryEvent event) {
		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(
				"travelers_dream_blocks:flowerpotwithflower", "inventory"));
	}

	public static class BlockCustom extends Block {
		public static final PropertyInteger color = PropertyInteger.create("color", 0, 15);

		public BlockCustom() {
			super(Material.GLASS);
			setRegistryName("flowerpotwithflower");
			setUnlocalizedName("flowerpotwithflower");
			setSoundType(SoundType.STONE);
			setHardness(0F);
			setResistance(0F);
			setLightLevel(0F);
			setLightOpacity(0);
			setCreativeTab(MCreatorTravelersDream.tab);
			this.setDefaultState(this.blockState.getBaseState().withProperty(color, Integer.valueOf(0)));
		}

		@Override
		protected net.minecraft.block.state.BlockStateContainer createBlockState() {
			return new net.minecraft.block.state.BlockStateContainer(this, new IProperty[]{color});
		}

		@Override
		public int getMetaFromState(IBlockState state) {
			return state.getValue(color);
		}

		@Override
		public IBlockState getStateFromMeta(int meta) {
			return this.getDefaultState().withProperty(color, meta);
		}

		@SideOnly(Side.CLIENT)
		@Override
		public BlockRenderLayer getBlockLayer() {
			return BlockRenderLayer.CUTOUT_MIPPED;
		}

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

		@Override
		public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) {
			return false;
		}

		@Override
		public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entity, EnumHand hand, EnumFacing side,
				float hitX, float hitY, float hitZ) {
			super.onBlockActivated(world, pos, state, entity, hand, side, hitX, hitY, hitZ);
			int x = pos.getX();
			int y = pos.getY();
			int z = pos.getZ();
			Block block = this;
			{
				java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
				$_dependencies.put("entity", entity);
				$_dependencies.put("x", x);
				$_dependencies.put("y", y);
				$_dependencies.put("z", z);
				$_dependencies.put("world", world);
						MCreatorFlowerPotwithflowerOnBlockRightClicked.executeProcedure($_dependencies);

			}
			return true;
		}
	}
}

 

Last seen on 12:17, 15. Mar 2024
Joined Nov 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Well, after placing code…
Fri, 11/22/2019 - 15:16

Well, after placing code snippets

 

BlockPos pos = new BlockPos(x, y, z);

IBlockState blockState = world.getBlockState(pos);

int color = blockState.getValue(MCreatorFlowerPotwithflower.color); // my attempt to get PropertyInteger value from pot class

world.setBlockState(pos,  blockState.withProperty(color, Integer.valueOf(4)), 3);

 

in right positions (silly mistake) i'm getting only errors about "color" variable.

I suggest you to ask in the…
Fri, 11/22/2019 - 15:21

I suggest you to ask in the original topic as the author can help you. If you mess with code, you are expected to know a bit about it too.

Last seen on 14:12, 3. Jun 2023
Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I can't test the code, but…
Fri, 11/22/2019 - 16:22

I can't test the code, but the issue is that the procedure cannot find the variable "color".

The reason is that the classes of MCreator blocks have a different structure. In the case of BlockCauldron, you only have the "block" class itself. Your flower pot instead has:

  1. MCreatorFlowerPotwithflower, which handles stuff such as registering and world generation, AND
  2. BlockCustom, another class inside the first, with the actual block info (including the blockstate).

What you're doing right now is asking the procedure to grab the "color" variable from MCreatorFlowerPotwithflower, and NOT from BlockCustom. The variable cannot be found, so it throws a "cannot find symbol" error.

To tell the procedure to look inside BlockCustom, you should replace "MCreatorFlowerPotwithflower.color" with " MCreatorFlowerPotwithflower.BlockCustom.color" (the same goes for the 4th snippet)

Another minor thing, but when you declare static final variables, their names should be capitalized (the blockstate property in BlockCustom should be "COLOR" instead of "color"). You don't have to, but it's a convention that should be followed anyway. In your case it would make it easier to distinguish between the blockstate property "COLOR", and the int variable "color" that you're declaring in the 3rd snippet. If you do rename the property "color" to "COLOR", remember to adjust all the other places where the "color" property appears.