Jump to content

[1.8] Multiple Block Properties


KnaxelBaby

Recommended Posts

Hello forge community :) while i was developing my first forge mod for minecraft 1.8 i ran into an issue with my knowledge on minecrafts property and meta data system.  I have a jar with a custom model that changes  depending on the amount of the substance put into the jar. this property name is AMOUNT but i also want another property for the type of the substance being put in (there are two) this property i would like to be called SPECIES (its a plant). I was looking how minecraft does this for the anvil (is has a directional property and a damage property) and they used this code here to get the meta and state

 

    
  public IBlockState getStateFromMeta(int p_getStateFromMeta_1_)
  {
    return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(p_getStateFromMeta_1_ & 0x3)).withProperty(DAMAGE, Integer.valueOf((p_getStateFromMeta_1_ & 0xF) >> 2));
  }
  public int getMetaFromState(IBlockState p_getMetaFromState_1_)
  {
    int i = 0;
    
    i |= ((EnumFacing)p_getMetaFromState_1_.getValue(FACING)).getHorizontalIndex();
    i |= ((Integer)p_getMetaFromState_1_.getValue(DAMAGE)).intValue() << 2;
    
    return i;
  }
  
  protected BlockState createBlockState()
  {
    return new BlockState(this, new IProperty[] { FACING, DAMAGE });
  }

 

Theyre using bitwis operators and i was wondering how i could implement the same idea to my mod :) thank you

Link to comment
Share on other sites

You have 4 bits of metadata (numbers 0-15). If you can squeeze your stuff into that... do so. I am not sure what exactly your question is.

Well i understand that, but i need two meta datas essentially, the anvil has directional data and damage. how can i do something similar.

  public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
  public static final PropertyInteger DAMAGE = PropertyInteger.create("damage", 0, 2);

Link to comment
Share on other sites

That should be impossible. Please post your complete block class.

here it is

package knaxelbaby.modcore.blocks;

import java.util.ArrayList;

import knaxelbaby.modcore.Reference;
import knaxelbaby.modcore.blocks.access.RWBlockJarInteractable;
import knaxelbaby.modcore.models.Mason_Jar_0;
import knaxelbaby.modcore.models.Mason_Jar_1;
import knaxelbaby.modcore.models.Mason_Jar_2;
import knaxelbaby.modcore.models.Mason_Jar_3;
import knaxelbaby.modcore.models.Mason_Jar_4;
import knaxelbaby.modcore.models.Mason_Jar_5;
import knaxelbaby.modcore.models.Mason_Jar_6;
import knaxelbaby.modcore.models.Mason_Jar_7;
import knaxelbaby.modcore.models.Mason_Jar_8;
import knaxelbaby.modcore.models.Mason_Jar_Empty;
import knaxelbaby.modcore.models.Mason_Jar_Model;
import knaxelbaby.modcore.tileentities.TileEntityJar;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class RWBlockJar extends BlockContainer implements
	RWBlockJarInteractable {

public static final PropertyInteger AMOUNT =PropertyInteger.create("amount", 0, 9);
public static final PropertyInteger SPECIES =PropertyInteger.create("amount", 0, 9);
private ArrayList<Mason_Jar_Model> models = new ArrayList<Mason_Jar_Model>();

private Mason_Jar_0 jar0 = new Mason_Jar_0();
private Mason_Jar_1 jar1 = new Mason_Jar_1();
private Mason_Jar_2 jar2 = new Mason_Jar_2();
private Mason_Jar_3 jar3 = new Mason_Jar_3();
private Mason_Jar_4 jar4 = new Mason_Jar_4();
private Mason_Jar_5 jar5 = new Mason_Jar_5();
private Mason_Jar_6 jar6 = new Mason_Jar_6();
private Mason_Jar_7 jar7 = new Mason_Jar_7();
private Mason_Jar_8 jar8 = new Mason_Jar_8();
private Mason_Jar_Empty jare = new Mason_Jar_Empty();

private ResourceLocation texture = new ResourceLocation(Reference.MODID,
		"textures/custom/Mason_Jar.png");

public RWBlockJar(Material mat) {
	super(mat);
    setDefaultState(this.getDefaultState().withProperty(AMOUNT, Integer.valueOf(0)).withProperty(SPECIES, Integer.valueOf(0)));
	models.add(jare);
	models.add(jar0);
	models.add(jar1);
	models.add(jar2);
	models.add(jar3);
	models.add(jar4);
	models.add(jar5);
	models.add(jar6);
	models.add(jar7);
	models.add(jar8);

}

public TileEntity createNewTileEntity(World world, int arg) {
	return new TileEntityJar();
}

public int getRenderType() {
	return -1;
}

public boolean isOpaqueCube() {
	return false;
}

public boolean renderAsNormalBlock() {
	return false;
}

public IBlockState getStateFromMeta(int meta) {
    return getDefaultState().withProperty(AMOUNT, Integer.valueOf( meta & 0x3)).withProperty(SPECIES, Integer.valueOf(( meta & 0xF) >> 2));
}
public int getMetaFromState(IBlockState state) {
    int i = 0;
    
    i |= ((Integer)state.getValue(AMOUNT)).intValue();
    i |= ((Integer)state.getValue(SPECIES)).intValue() << 2;
    
    System.out.println(i);
    
    return i;
  }
protected BlockState createBlockState() {
	return new BlockState(this, new IProperty[] { AMOUNT, SPECIES});
}

public ResourceLocation getTextureLocation() {
	return texture;
}

public void setCurrentModel(World world, BlockPos pos, int i) {
	IBlockState state = ((IBlockState) world.getBlockState(pos));
	int meta = getMetaFromState(state);
	world.setBlockState(pos, state.withProperty(AMOUNT, Integer.valueOf(i)));
}


public boolean isFull(World world, BlockPos pos) {
	return (((Integer) world.getBlockState(pos).getValue(AMOUNT)).intValue() == 9);
}

public Mason_Jar_Model getCurrentModel(World world, BlockPos pos) {
	return models.get(((Integer) world.getBlockState(pos).getValue(AMOUNT)).intValue());
}

public int getCurrentModelIndex(World world, BlockPos pos) {
	return ((Integer)world.getBlockState(pos).getValue(AMOUNT)).intValue();
}

public boolean isEmpty(World world, BlockPos pos) {
	return (((Integer) world.getBlockState(pos).getValue(AMOUNT)).intValue() == 0);
}

@Override
public int getSpecies(World world, BlockPos pos) {
	return ((Integer)world.getBlockState(pos).getValue(SPECIES)).intValue();
}

@Override
public void setSpecies(World world, BlockPos pos, int i) {
	world.setBlockState(pos, world.getBlockState(pos).withProperty(SPECIES, i));

}
}

heres the interactable that lets me access certain methods

package knaxelbaby.modcore.blocks.access;

import knaxelbaby.modcore.models.Mason_Jar_Model;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public abstract interface RWBlockJarInteractable {
public abstract void setCurrentModel(World world, BlockPos pos, int i);
public abstract boolean isFull(World world, BlockPos pos);
public abstract Mason_Jar_Model getCurrentModel(World world, BlockPos pos);
public abstract int getCurrentModelIndex(World world, BlockPos pos);
public abstract ResourceLocation getTextureLocation();
public abstract boolean isEmpty(World worldObj, BlockPos pos);
public abstract int getSpecies(World world, BlockPos pos);
public abstract void setSpecies(World world,BlockPos pos, int i);
}

and the renderer class

package knaxelbaby.modcore.renderers;

import java.util.ArrayList;

import knaxelbaby.modcore.Reference;
import knaxelbaby.modcore.blocks.access.RWBlockJarInteractable;
import knaxelbaby.modcore.blocks.access.RWPieceUsable;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

public class RendererJar extends TileEntitySpecialRenderer {

public RendererJar() {

}

public void renderTileEntityAt(TileEntity entity, double x, double y,
		double z, float angle, int i) {
	GL11.glPushMatrix();
	GL11.glTranslatef((float) (x + .5), (float) (y + 1.5), (float) (z + .5));
	GL11.glRotatef(180, 0, 0, 1F);
	RWBlockJarInteractable usable = ((RWBlockJarInteractable) entity.getWorld()
			.getBlockState(entity.getPos()).getBlock());
	ResourceLocation res = usable.getTextureLocation();
	this.bindTexture(res);

	GL11.glPushMatrix();
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	usable.getCurrentModel(entity.getWorld(), entity.getPos()).render(
			0.0625F);
	GL11.glPopMatrix();
	GL11.glPopMatrix();
}

}

Link to comment
Share on other sites

Well, both your properties have 10 values... You can only store one of them in metadata, since metadata has only 4 bits. But you would need

ceil(log[sub]2[/sub](10x10=100)) = 7

bits to store both of your properties. You need to store your data in a TileEntity or make more than one Block.

how would i store TileEntity data

Link to comment
Share on other sites

For each block with TileEntity on specific world position, there is a TileEntity matches the block.

So you can store data as normal field, and read and save the data using readFromNBT & writeToNBT

 

There are plenty of tutorials describing how to make TileEntity, so you would easily be able to make one.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.