Started by
Nuparu00
on
Topic category: User side tutorials
Hi. Because there is not his feature in MCreater itself and many of you need it for your mod , I decided to make a video tutorial how to make custom block rotatable , using PropertyDirection.
LINK
Edited by Klemen on Wed, 01/04/2017 - 20:31
@#63 Please @Naparu00 or someone who knows how to add up and down directions. Thanks.
Does this still work in 1.7.6 for 1.12.2????
hey every time i try to use the code it gives me errors all the time im not that good at coding but this is my code
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraft.world.World;
import net.minecraft.world.IBlockAccess;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.Item;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.Minecraft;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.material.Material;
import net.minecraft.block.SoundType;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import java.util.Random;
public class mcreator_test {
public mcreator_test() {
}
public static BlockTest block;
public static Object instance;
public int addFuel(ItemStack fuel) {
return 0;
}
public void serverLoad(FMLServerStartingEvent event) {
}
public void preInit(FMLPreInitializationEvent event) {
block.setRegistryName("test");
GameRegistry.register(block);
GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));
}
public void registerRenderers() {
}
public void load(FMLInitializationEvent event) {
if (event.getSide() == Side.CLIENT) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
.register(Item.getItemFromBlock(block), 0, new ModelResourceLocation("testenvironmentmod:test", "inventory"));
}
}
static {
block = (BlockTest) (new BlockTest().setHardness(2.25F).setResistance(11.5F).setLightLevel(0.0F).setUnlocalizedName("Test")
.setLightOpacity(0).setCreativeTab(CreativeTabs.BUILDING_BLOCKS));
block.setHarvestLevel("pickaxe", 4);
}
public void generateSurface(World world, Random random, int chunkX, int chunkZ) {
}
public void generateNether(World world, Random random, int chunkX, int chunkZ) {
}
static class BlockTest extends Block {
int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0;
boolean red = false;
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
protected BlockTest() {
super(Material.GROUND);
setSoundType(SoundType.WOOD);
this.setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
int i = pos.getX();
int j = pos.getY();
int k = pos.getZ();
world.scheduleUpdate(new BlockPos(i, j, k), this, this.tickRate(world));
}
@Override
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
return red ? 15 : 0;
}
@Override
public int getStrongPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
return red ? 15 : 0;
}
@Override
public void randomDisplayTick(IBlockState state, World world, BlockPos pos, Random random) {
EntityPlayer entity = Minecraft.getMinecraft().player;
int i = pos.getX();
int j = pos.getY();
int k = pos.getZ();
World par1World = world;
int par2 = i;
int par3 = j;
int par4 = k;
Random par5Random = random;
if (true)
for (int la = 0; la < 4; ++la) {
double d0 = (double) ((float) par2 + 0.5F) + (double) (par5Random.nextFloat() - 0.5F) * 0.5D * 20;
double d1 = ((double) ((float) par3 + 0.7F) + (double) (par5Random.nextFloat() - 0.5F) * 0.5D) + 0.5D;
double d2 = (double) ((float) par4 + 0.5F) + (double) (par5Random.nextFloat() - 0.5F) * 0.5D * 20;
double d3 = 0.2199999988079071D;
double d4 = 0.27000001072883606D;
par1World.spawnParticle(EnumParticleTypes.SMOKE_LARGE, d0 - d4, d1 + d3, d2, 0.0D, 0.0D, 0.0D);
}
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@SideOnly(Side.CLIENT)
@Override
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.SOLID;
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
}
@Override
public int tickRate(World world) {
return 10;
}
@Override
public int quantityDropped(Random par1Random) {
return 1;
}
@Override
public IBlockState getStateForEntityRender(IBlockState state) {
return getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
}
@Override
public IBlockState getStateFromMeta(int meta) {
EnumFacing facing = EnumFacing.getFront(meta);
if(facing.getAxis()==EnumFacing.Axis.Y) {
facing=EnumFacing.NORTH;
}
return getDefaultState().withProperty(FACING, facing);
}
@Override
public int getMetaFromState(IBlockState state) {
return ((EnumFacing) state.getValue(FACING)).getIndex();
}
@Override
protected BlockState createBlockState() {
return new BlockState(this, new IProperty[]{FACING});
}
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos,
EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
EntityLivingBase placer) {
return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
}
}
_____________________________________________________________________________________________________
and it gives me this error code
D:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_test.java:187: error: cannot find symbol
protected BlockState createBlockState() {
^
symbol: class BlockState
location: class BlockTest
D:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_test.java:164: error: method does not override or implement a method from a supertype
@Override
^
D:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_test.java:188: error: cannot find symbol
return new BlockState(this, new IProperty[]{FACING});
^
symbol: class BlockState
location: class BlockTest
D:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_test.java:192: error: method does not override or implement a method from a supertype
@Override
^
Note: D:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_test.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errors
1 warning
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
Hi,
I am using 1.12.2 and it didn't work even with the code for +1.9.
Could you please send the code of one and the same simple block with and without rotation so that I can see exactly the difference and I can apply it to other blocks as well? I would be very grateful for that!!
Yeah, please, can you make update to new version?
I put the code you metioned and in the game there is no texture.
Did you edit the blockstate .JSON file? And if yes, are there any error/warning messages in the log?
Yes, I edited the blockstate.json. But today I rewrited the code giving me the purple block by your example code from one of the video commentaries (https://pastebin.com/Exp7HuUn). And the client doesn't even start. Now I got two errors:
C:\Pylo\MCreator179\forge\build\sources\main\java\mod\mcreator\mcreator_sipka.java:55: error: register(K) has private access in GameRegistry
GameRegistry.register(block);
^
where K is a type-variable:
K extends IForgeRegistryEntry declared in method register(K)
C:\Pylo\MCreator179\forge\build\sources\main\java\mod\mcreator\mcreator_sipka.java:56: error: register(K) has private access in GameRegistry
GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));
^
where K is a type-variable:
K extends IForgeRegistryEntry declared in method register(K)
Meantime I solved the rotation by creating 4 blocks (each is facing different direction) and by making 4 procedures which changes one block for another by clicking. But I think the coding would be more elegant.
What version of Minecraft are you modding for? If you are using 1.12.x+ , you can not register the block in the same way as in the version that the code sample was for. I guess I have to update the video someday...
I use 1.12.2. First time I used the code for 1.9 (right bellow the vid) and put the changes for 1.10, 1.11 and 1.12 from commentaries. Then the game worked but textures were missing. Later I thought the sample code was for 1.12 so I tried to use this. And that happened. Never mind, I use the procedure system now. Updating the code system would be great for 1.13 and newer versions. And thank you for your attention.
Can I see your code?
package mod.mcreator;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraft.world.World;
import net.minecraft.world.IBlockAccess;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.NonNullList;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.tileentity.TileEntityLockableLoot;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.Item;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.ContainerDispenser;
import net.minecraft.inventory.Container;
import net.minecraft.init.Items;
import net.minecraft.init.Blocks;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.Minecraft;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.material.Material;
import net.minecraft.block.SoundType;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.entity.EntityLivingBase;
import java.util.Random;
public class mcreator_locker {
public mcreator_locker() {
}
public static BlockLocker block;
public static Item item;
public static Object instance;
public int addFuel(ItemStack fuel) {
return 0;
}
public void serverLoad(FMLServerStartingEvent event) {
}
public void preInit(FMLPreInitializationEvent event) {
block.setRegistryName("locker");
ForgeRegistries.BLOCKS.register(block);
item = new ItemBlock(block).setRegistryName(block.getRegistryName());
ForgeRegistries.ITEMS.register(item);
}
public void registerRenderers() {
}
public void load(FMLInitializationEvent event) {
GameRegistry.registerTileEntity(mcreator_locker.TileEntityCustom.class, "TileEntitylocker");
if (event.getSide() == Side.CLIENT) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
.register(item, 0, new ModelResourceLocation("testenvironmentmod:locker", "inventory"));
}
GameRegistry.addShapedRecipe(new ResourceLocation("testenvironmentmod:locker"), new ResourceLocation("custom"), new ItemStack(item, 1),
new Object[]{"012", "345", "678", Character.valueOf('0'), Blocks.IRON_BLOCK, Character.valueOf('1'), new ItemStack(Items.DYE, 1, 1),
Character.valueOf('2'), Blocks.IRON_BLOCK, Character.valueOf('3'), Blocks.IRON_BLOCK, Character.valueOf('4'), Blocks.CHEST,
Character.valueOf('5'), Blocks.IRON_BLOCK, Character.valueOf('6'), Blocks.IRON_BLOCK, Character.valueOf('7'),
new ItemStack(Items.DYE, 1, 1), Character.valueOf('8'), Blocks.IRON_BLOCK,});
}
static {
block = (BlockLocker) (new BlockLocker().setHardness(2.0F).setResistance(10.0F).setLightLevel(0.0F).setUnlocalizedName("locker")
.setLightOpacity(0).setCreativeTab(CreativeTabs.DECORATIONS));
block.setHarvestLevel("pickaxe", 4);
}
public void generateSurface(World world, Random random, int chunkX, int chunkZ) {
}
public void generateNether(World world, Random random, int chunkX, int chunkZ) {
}
static class BlockLocker extends Block implements ITileEntityProvider {
int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0;
boolean red = false;
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
this.setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
@Override
public IBlockState getStateFromMeta(int meta) {
EnumFacing facing = EnumFacing.getFront(meta);
if(facing.getAxis()==EnumFacing.Axis.Y) {
facing=EnumFacing.NORTH;
}
return getDefaultState().withProperty(FACING, facing);
}
@Override
public int getMetaFromState(IBlockState state) {
return ((EnumFacing) state.getValue(FACING)).getIndex();
}
getStateForPlacement
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, new IProperty[]{FACING});
}
@Override
public IBlockState getStateForPlacement(World worldIn, BlockPos pos,
EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
EntityLivingBase placer) {
return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
protected BlockLocker() {
super(Material.IRON);
setSoundType(SoundType.METAL);
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileEntityCustom();
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
TileEntity tileentity = world.getTileEntity(pos);
InventoryHelper.dropInventoryItems(world, pos, (TileEntityCustom) tileentity);
world.removeTileEntity(pos);
super.breakBlock(world, pos, state);
}
@Override
public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int eventID, int eventParam) {
super.eventReceived(state, worldIn, pos, eventID, eventParam);
TileEntity tileentity = worldIn.getTileEntity(pos);
return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.MODEL;
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
int i = pos.getX();
int j = pos.getY();
int k = pos.getZ();
world.scheduleUpdate(new BlockPos(i, j, k), this, this.tickRate(world));
}
@Override
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
return red ? 15 : 0;
}
@Override
public int getStrongPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
return red ? 15 : 0;
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entity, EnumHand hand, EnumFacing side,
float hitX, float hitY, float hitZ) {
int i = pos.getX();
int j = pos.getY();
int k = pos.getZ();
if (true) {
if (entity instanceof EntityPlayer)
((EntityPlayer) entity).openGui(instance, mcreator_lockerInventory.GUIID, world, i, j, k);
}
return true;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@SideOnly(Side.CLIENT)
@Override
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.TRANSLUCENT;
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return new AxisAlignedBB(0.30000000000000004D, 0.0D, 0.30000000000000004D, 0.7000000000000001D, 2.0D, 0.7000000000000001D);
}
@Override
public int tickRate(World world) {
return 10;
}
@Override
public int quantityDropped(Random par1Random) {
return 1;
}
}
public static class TileEntityCustom extends TileEntityLockableLoot {
private NonNullList<ItemStack> stacks = NonNullList.<ItemStack> withSize(9, ItemStack.EMPTY);
public int getSizeInventory() {
return 9;
}
public boolean isEmpty() {
for (ItemStack itemstack : this.stacks) {
if (!itemstack.isEmpty()) {
return false;
}
}
return true;
}
public String getName() {
return this.hasCustomName() ? this.customName : "container.locker";
}
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.stacks = NonNullList.<ItemStack> withSize(this.getSizeInventory(), ItemStack.EMPTY);
if (!this.checkLootAndRead(compound)) {
ItemStackHelper.loadAllItems(compound, this.stacks);
}
if (compound.hasKey("CustomName", 8)) {
this.customName = compound.getString("CustomName");
}
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
if (!this.checkLootAndWrite(compound)) {
ItemStackHelper.saveAllItems(compound, this.stacks);
}
if (this.hasCustomName()) {
compound.setString("CustomName", this.customName);
}
return compound;
}
public int getInventoryStackLimit() {
return 64;
}
public String getGuiID() {
return "minecraft:locker";
}
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
this.fillWithLoot(playerIn);
return new ContainerDispenser(playerInventory, this);
}
protected NonNullList<ItemStack> getItems() {
return this.stacks;
}
}
}
Is supposed to be in the constructor of your blocks class, it can not be on its own in the class.