TRMelon Posted May 7, 2018 Posted May 7, 2018 (edited) I have a very basic 'machine' block with variants and facing properties in its blockstate, the variant is saved and loaded when you re-open the world using 'getMetaFromState' override, and 'getStateFromMeta' also overridden. The problem is I don't know how to save the facing property as eventually, after adding more variants, there won't be enough room in the meta to store both properties. I'd like to save the facing property in a tile entity but I'm not sure how to load it when the world is saved and re-opened, is this possible? I have shown what I have so far, and how I register the tile entity (main and CommonProxy classes). Machine block class: package theredmelon.enhancedmechanisms.objects.machines; import net.minecraft.block.Block; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; import theredmelon.enhancedmechanisms.EnhancedMechanisms; import theredmelon.enhancedmechanisms.init.BlockInit; import theredmelon.enhancedmechanisms.init.ItemInit; import theredmelon.enhancedmechanisms.objects.blocks.item.IMetaBlockName; import theredmelon.enhancedmechanisms.objects.blocks.item.ItemBlockBase; import theredmelon.enhancedmechanisms.tileentity.TileEntityMachine; import theredmelon.enhancedmechanisms.utils.IHasModel; import theredmelon.enhancedmechanisms.utils.handlers.EnumHandler.MachineTypes; public class Machine extends Block implements IMetaBlockName, IHasModel, ITileEntityProvider { public static final PropertyEnum TYPE = PropertyEnum.create("type", MachineTypes.class); public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public Machine (String name, Material material, CreativeTabs tab) { super(material); setCreativeTab(tab); setUnlocalizedName(name); setRegistryName(name); setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(TYPE, MachineTypes.THREEFOLD_COMBINER)); BlockInit.BLOCKS.add(this); ItemInit.MACHINE_ITEM = new ItemBlockBase(this).setRegistryName(name); ItemInit.ITEMS.add(ItemInit.MACHINE_ITEM); } @Override public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) { TileEntity tileEntity = world.getTileEntity(pos); if (tileEntity instanceof TileEntityMachine) { TileEntityMachine machine = (TileEntityMachine) tileEntity; machine.setFacing(placer.getHorizontalFacing().getOpposite()); } return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(TYPE, MachineTypes.values()[meta]); } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING, TYPE}); } @Override public int getMetaFromState(IBlockState state) { MachineTypes type = (MachineTypes) state.getValue(TYPE); return type.getID(); } @Override public IBlockState getStateFromMeta(int meta) { return getDefaultState().withProperty(TYPE, MachineTypes.values()[meta]); } @Override public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items) { for (int i = 0; i < MachineTypes.values().length; i++) { items.add(new ItemStack(this, 1, i)); } } @Override public String getSpecialName(ItemStack stack) { return MachineTypes.values()[stack.getItemDamage()].getName(); } @Override public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) { return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos))); } @Override public int damageDropped(IBlockState state) { return getMetaFromState(state); } @Override public void registerModels() { for (int i = 0; i < MachineTypes.values().length; i++) { EnhancedMechanisms.proxy.registerItemVariantsRenderer(Item.getItemFromBlock(this), i, "inventory", MachineTypes.values()[i].getName()); } } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new TileEntityMachine(); } } TileEntityMachine: package theredmelon.enhancedmechanisms.tileentity; import com.ibm.icu.impl.duration.impl.DataRecord.EUnitVariant; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; public class TileEntityMachine extends TileEntity { private EnumFacing facing; public EnumFacing getFacing() { if (facing != null) { return facing; } return EnumFacing.NORTH; } public void setFacing(EnumFacing facing) { this.facing = facing; markDirty(); } @Override public void readFromNBT(NBTTagCompound compound) { facing = EnumFacing.getHorizontal(compound.getInteger("Facing")); super.readFromNBT(compound); } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("Facing", facing.getHorizontalIndex()); return compound; } } EnhancedMechanisms (Main): package theredmelon.enhancedmechanisms; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import theredmelon.enhancedmechanisms.init.BlockInit; import theredmelon.enhancedmechanisms.init.CraftingInit; import theredmelon.enhancedmechanisms.proxy.CommonProxy; import theredmelon.enhancedmechanisms.utils.Reference; @Mod(modid = Reference.MOD_ID, name = Reference.NAME, version = Reference.VERSION) public class EnhancedMechanisms { @Instance public static EnhancedMechanisms instance; @SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.COMMON_PROXY_CLASS) public static CommonProxy proxy; @EventHandler public static void PreInit(FMLPreInitializationEvent event) { } @EventHandler public static void init(FMLInitializationEvent event) { CraftingInit.registerRecepies(); proxy.registerTileEntities(); } @EventHandler public static void PostInit(FMLPostInitializationEvent event) { } } CommonProxy: package theredmelon.enhancedmechanisms.proxy; import net.minecraft.item.Item; import net.minecraftforge.fml.common.registry.GameRegistry; import theredmelon.enhancedmechanisms.tileentity.TileEntityMachine; import theredmelon.enhancedmechanisms.utils.Reference; public class CommonProxy { public void registerItemRenderer(Item item, int meta, String id) { } public void registerItemVariantsRenderer(Item item, int meta, String id, String varientName) { } public void registerTileEntities() { GameRegistry.registerTileEntity(TileEntityMachine.class, Reference.MOD_ID + "TileEntityMachine"); } } Edited May 7, 2018 by TRMelon Quote
TRMelon Posted May 7, 2018 Author Posted May 7, 2018 Using hasTileEntity and createTileEntity now, instead of ITileEntityProvider Added mod id to all unlocalized names Removed common proxy, now the code is just in the main class Don't know what to do about the rest. Quote If you want to set the facing of your block, you must set it on your tile entity. Is this what you mean? public void setFacing(EnumFacing facing) { this.facing = facing; setFacing(facing); markDirty(); } This is what I've added to my machine class: @Override public boolean hasTileEntity() { return true; } @Override public TileEntity createTileEntity(World world, IBlockState state) { return new TileEntityMachine(); } @Override public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) { if (worldIn.getTileEntity(pos) instanceof TileEntityMachine) { TileEntityMachine machine = (TileEntityMachine) worldIn.getTileEntity(pos); return state.withProperty(FACING, machine.getFacing()); } return state.withProperty(FACING, EnumFacing.NORTH); } And I still have this: @Override public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) { TileEntity tileEntity = world.getTileEntity(pos); if (tileEntity instanceof TileEntityMachine) { TileEntityMachine machine = (TileEntityMachine) tileEntity; machine.setFacing(placer.getHorizontalFacing().getOpposite()); } return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()).withProperty(TYPE, MachineTypes.values()[meta]); } Not sure this is what you meant I had to do, but now the block always faces north when placed. Quote
TRMelon Posted May 7, 2018 Author Posted May 7, 2018 Done, but am I supposed to use the IBlockState parameter? Removed those checks (got them from YouTube videos, clearly not great ones) I dont know how I'm meant to set that in getActualState, because it has no placer parameter which is how I get the facing direction. This is what I have now, the blocks place in the right direction but go to south when I re-open the world: @Override public boolean hasTileEntity(IBlockState state) { return true; } @Override public TileEntity createTileEntity(World world, IBlockState state) { return new TileEntityMachine(); } @Override public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) { TileEntityMachine machine = (TileEntityMachine) worldIn.getTileEntity(pos); return state.withProperty(FACING, machine.getFacing()); } @Override public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { TileEntityMachine machine = (TileEntityMachine) worldIn.getTileEntity(pos);; machine.setFacing(placer.getHorizontalFacing().getOpposite()); } Quote
TRMelon Posted May 7, 2018 Author Posted May 7, 2018 That worked! Thank you so much, I've been googling for too many hours. Quote
Recommended Posts
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.