Further simplify code for blocks with pages of variants. Remove test blocks

This commit is contained in:
Cheeserolls 2015-04-06 19:29:24 +01:00
parent 918dbdd5ff
commit d6d75d7e40
10 changed files with 182 additions and 367 deletions

View file

@ -8,6 +8,12 @@
package biomesoplenty.api.block; package biomesoplenty.api.block;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Predicate;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.util.IStringSerializable; import net.minecraft.util.IStringSerializable;
public class BOPTreeEnums public class BOPTreeEnums
@ -28,4 +34,51 @@ public class BOPTreeEnums
} }
} }
public static enum TreesFilterType {
ALL, SAPLINGS;
public Predicate<AllTrees> getPredicate(final int pageNum, final int numPerPage)
{
final TreesFilterType filterType = this;
return new Predicate<AllTrees>()
{
@Override
public boolean apply(AllTrees tree)
{
if (filterType == SAPLINGS && (tree == AllTrees.YELLOW_BIG_FLOWER || tree == AllTrees.RED_BIG_FLOWER) )
{
return false;
}
return (tree.ordinal() >= (numPerPage * pageNum)) && (tree.ordinal() < (numPerPage * (pageNum+1)));
}
};
}
}
private static Map<Integer, PropertyEnum[]> propCache = new HashMap<Integer, PropertyEnum[]>();
public static PropertyEnum getVariantProperty(int pageNum, int numPerPage)
{
return getVariantProperty(pageNum, numPerPage, TreesFilterType.ALL);
}
public static PropertyEnum getVariantProperty(int pageNum, int numPerPage, TreesFilterType filterType)
{
// check length and make sure things are in bounds
int len = AllTrees.values().length;
int numPages = (int) Math.ceil( (double)len / numPerPage);
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
// look up the array of properties for pages of size numPerPage and the given filter - if it doesn't exist yet, create it
Integer index = new Integer(numPerPage * TreesFilterType.values().length + filterType.ordinal() );
if (!propCache.containsKey(index)) {propCache.put(index, new PropertyEnum[numPages]);}
// look up the property for page pageNum - if it doesn't exist yet, create it
PropertyEnum[] propArr = propCache.get(index);
if (propArr[pageNum] == null)
{
propArr[pageNum] = PropertyEnum.create("variant", AllTrees.class, filterType.getPredicate(pageNum, numPerPage));
}
return propArr[pageNum];
}
} }

View file

@ -8,6 +8,12 @@
package biomesoplenty.api.block; package biomesoplenty.api.block;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Predicate;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.util.IStringSerializable; import net.minecraft.util.IStringSerializable;
public class BOPWoodEnums public class BOPWoodEnums
@ -27,4 +33,55 @@ public class BOPWoodEnums
} }
} }
public static enum WoodsFilterType {
ALL, WITH_PLANKS;
public Predicate<AllWoods> getPredicate(final int pageNum, final int numPerPage)
{
final WoodsFilterType filterType = this;
return new Predicate<AllWoods>()
{
@Override
public boolean apply(AllWoods wood)
{
if (filterType == WITH_PLANKS && (wood == AllWoods.GIANT_FLOWER || wood == AllWoods.DEAD) )
{
return false;
}
return (wood.ordinal() >= (numPerPage * pageNum)) && (wood.ordinal() < (numPerPage * (pageNum+1)));
}
};
}
}
private static Map<Integer, PropertyEnum[]> propCache = new HashMap<Integer, PropertyEnum[]>();
public static PropertyEnum getVariantProperty(int pageNum, int numPerPage)
{
return getVariantProperty(pageNum, numPerPage, WoodsFilterType.ALL);
}
public static PropertyEnum getVariantProperty(int pageNum, int numPerPage, WoodsFilterType filterType)
{
// check length and make sure things are in bounds
int len = AllWoods.values().length;
int numPages = (int) Math.ceil( (double)len / numPerPage);
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
// look up the array of properties for pages of size numPerPage and the given filter - if it doesn't exist yet, create it
Integer index = new Integer(numPerPage * WoodsFilterType.values().length + filterType.ordinal() );
if (!propCache.containsKey(index)) {propCache.put(index, new PropertyEnum[numPages]);}
// look up the property for page pageNum - if it doesn't exist yet, create it
PropertyEnum[] propArr = propCache.get(index);
if (propArr[pageNum] == null)
{
propArr[pageNum] = PropertyEnum.create("variant", AllWoods.class, filterType.getPredicate(pageNum, numPerPage));
}
return propArr[pageNum];
}
} }

View file

@ -2,9 +2,9 @@ package biomesoplenty.common.block;
import java.util.List; import java.util.List;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import biomesoplenty.api.block.BOPWoodEnums;
import biomesoplenty.api.block.BOPWoodEnums.AllWoods; import biomesoplenty.api.block.BOPWoodEnums.AllWoods;
import biomesoplenty.api.block.IBOPBlock; import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.common.util.block.BlockStateUtils; import biomesoplenty.common.util.block.BlockStateUtils;
@ -26,64 +26,35 @@ public abstract class BlockBOPHalfWoodSlab extends BlockSlab implements IBOPBloc
{ {
// store the variant properties for each page in this array // setup paged variant property
private static PropertyEnum[] variantProperties;
// number of variants per page - HALF requires 1 meta bit, so we have 3 left for the VARIANT, which means we can have eight woods per instance // STAGE require one bit, so we have 3 bits left for the VARIANT which means we can have eight per instance
public static final int variantsPerPage = 8; public static final int VARIANTS_PER_PAGE = 8;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllWoods.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllWoods.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllWoods> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllWoods>()
{
@Override
public boolean apply(AllWoods wood)
{
switch (wood)
{
case GIANT_FLOWER: case DEAD:
return false;
default:
return (wood.ordinal() >= (variantsPerPage * pageNum)) && (wood.ordinal() < (variantsPerPage * (pageNum+1)));
}
}
};
}
// child classes must implement to define their page number // child classes must implement to define their page number
abstract public int getPageNum(); abstract public int getPageNum();
// fetch the variant property for a given page
public static PropertyEnum getVariantProperty(int pageNum)
{
return BOPWoodEnums.getVariantProperty(pageNum, VARIANTS_PER_PAGE, BOPWoodEnums.WoodsFilterType.WITH_PLANKS);
}
// fetch the current instance's variant property // fetch the current instance's variant property
public PropertyEnum getMyVariantProperty() public PropertyEnum getMyVariantProperty()
{ {
return getVariantProperty(this.getPageNum()); return getVariantProperty(getPageNum());
} }
// get the meta bits from the variant
public int metaFromVariant(AllWoods wood) public int metaFromVariant(AllWoods wood)
{ {
return wood.ordinal() % variantsPerPage; return wood.ordinal() % VARIANTS_PER_PAGE;
} }
// get the variant from meta bits (safely)
public AllWoods variantFromMeta(int meta) public AllWoods variantFromMeta(int meta)
{ {
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllWoods.values().length)); // clamp to int i = Math.max(0, Math.min(meta + (this.getPageNum() * VARIANTS_PER_PAGE), AllWoods.values().length));
return AllWoods.values()[i]; return AllWoods.values()[i];
} }
// add properties (note we inherit HALF property from parent BlockSlab) // add properties (note we inherit HALF property from parent BlockSlab)
@Override @Override
protected BlockState createBlockState() {return new BlockState(this, new IProperty[] {HALF, getMyVariantProperty()});} protected BlockState createBlockState() {return new BlockState(this, new IProperty[] {HALF, getMyVariantProperty()});}

View file

@ -11,9 +11,8 @@ package biomesoplenty.common.block;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import com.google.common.base.Predicate;
import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.BOPTreeEnums;
import biomesoplenty.api.block.IBOPBlock; import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.BOPTreeEnums.AllTrees; import biomesoplenty.api.block.BOPTreeEnums.AllTrees;
import biomesoplenty.common.item.ItemBOPBlock; import biomesoplenty.common.item.ItemBOPBlock;
@ -41,61 +40,34 @@ import net.minecraftforge.fml.relauncher.SideOnly;
public abstract class BlockBOPLeaves extends BlockLeaves implements IBOPBlock public abstract class BlockBOPLeaves extends BlockLeaves implements IBOPBlock
{ {
// setup paged variant property
// store the variant properties for each page in this array
private static PropertyEnum[] variantProperties;
// CHECK_DECAY and DECAYABLE require one bit each, so we have 2 bits left for the VARIANT which means we can have four per instance // CHECK_DECAY and DECAYABLE require one bit each, so we have 2 bits left for the VARIANT which means we can have four per instance
public static final int variantsPerPage = 4; public static final int VARIANTS_PER_PAGE = 4;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllTrees.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllTrees.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllTrees> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllTrees>()
{
@Override
public boolean apply(AllTrees tree)
{
return (tree.ordinal() >= (variantsPerPage * pageNum)) && (tree.ordinal() < (variantsPerPage * (pageNum+1)));
}
};
}
// child classes must implement to define their page number // child classes must implement to define their page number
abstract public int getPageNum(); abstract public int getPageNum();
// fetch the variant property for a given page
public static PropertyEnum getVariantProperty(int pageNum)
{
return BOPTreeEnums.getVariantProperty(pageNum, VARIANTS_PER_PAGE);
}
// fetch the current instance's variant property // fetch the current instance's variant property
public PropertyEnum getMyVariantProperty() public PropertyEnum getMyVariantProperty()
{ {
return getVariantProperty(this.getPageNum()); return getVariantProperty(getPageNum());
} }
// get the meta bits from the variant
public int metaFromVariant(AllTrees tree) public int metaFromVariant(AllTrees tree)
{ {
return tree.ordinal() % variantsPerPage; return tree.ordinal() % VARIANTS_PER_PAGE;
} }
// get the variant from meta bits (safely)
public AllTrees variantFromMeta(int meta) public AllTrees variantFromMeta(int meta)
{ {
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllTrees.values().length)); // clamp to int i = Math.max(0, Math.min(meta + (this.getPageNum() * VARIANTS_PER_PAGE), AllTrees.values().length));
return AllTrees.values()[i]; return AllTrees.values()[i];
} }
// add properties - note CHECK_DECAY and DECAYABLE are both inherited from BlockLeaves // add properties - note CHECK_DECAY and DECAYABLE are both inherited from BlockLeaves
@Override @Override
protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { CHECK_DECAY, DECAYABLE, getMyVariantProperty() });} protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { CHECK_DECAY, DECAYABLE, getMyVariantProperty() });}

View file

@ -8,9 +8,8 @@
package biomesoplenty.common.block; package biomesoplenty.common.block;
import com.google.common.base.Predicate;
import biomesoplenty.api.block.BOPWoodEnums.AllWoods; import biomesoplenty.api.block.BOPWoodEnums.AllWoods;
import biomesoplenty.api.block.BOPWoodEnums;
import biomesoplenty.api.block.IBOPBlock; import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.common.item.ItemBOPBlock; import biomesoplenty.common.item.ItemBOPBlock;
import net.minecraft.block.BlockLog; import net.minecraft.block.BlockLog;
@ -23,58 +22,34 @@ import net.minecraft.item.ItemBlock;
public abstract class BlockBOPLog extends BlockLog implements IBOPBlock public abstract class BlockBOPLog extends BlockLog implements IBOPBlock
{ {
// setup paged variant property
// store the variant properties for each page in this array // STAGE require one bit, so we have 3 bits left for the VARIANT which means we can have eight per instance
private static PropertyEnum[] variantProperties; public static final int VARIANTS_PER_PAGE = 4;
// number of variants per page
public static final int variantsPerPage = 4;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllWoods.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllWoods.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllWoods> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllWoods>()
{
@Override
public boolean apply(AllWoods wood)
{
return (wood.ordinal() >= (variantsPerPage * pageNum)) && (wood.ordinal() < (variantsPerPage * (pageNum+1)));
}
};
}
// child classes must implement to define their page number // child classes must implement to define their page number
abstract public int getPageNum(); abstract public int getPageNum();
// fetch the variant property for a given page
public static PropertyEnum getVariantProperty(int pageNum)
{
return BOPWoodEnums.getVariantProperty(pageNum, VARIANTS_PER_PAGE);
}
// fetch the current instance's variant property // fetch the current instance's variant property
public PropertyEnum getMyVariantProperty() public PropertyEnum getMyVariantProperty()
{ {
return getVariantProperty(this.getPageNum()); return getVariantProperty(getPageNum());
} }
// get the meta bits from the variant
public int metaFromVariant(AllWoods wood) public int metaFromVariant(AllWoods wood)
{ {
return wood.ordinal() % variantsPerPage; return wood.ordinal() % VARIANTS_PER_PAGE;
} }
// get the variant from meta bits (safely)
public AllWoods variantFromMeta(int meta) public AllWoods variantFromMeta(int meta)
{ {
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllWoods.values().length)); // clamp to int i = Math.max(0, Math.min(meta + (this.getPageNum() * VARIANTS_PER_PAGE), AllWoods.values().length));
return AllWoods.values()[i]; return AllWoods.values()[i];
} }
@Override @Override
protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { LOG_AXIS, getMyVariantProperty() });} protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { LOG_AXIS, getMyVariantProperty() });}

View file

@ -8,8 +8,6 @@
package biomesoplenty.common.block; package biomesoplenty.common.block;
import com.google.common.base.Predicate;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.IProperty;
@ -17,6 +15,7 @@ import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState; import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemBlock;
import biomesoplenty.api.block.BOPWoodEnums;
import biomesoplenty.api.block.IBOPBlock; import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.BOPWoodEnums.AllWoods; import biomesoplenty.api.block.BOPWoodEnums.AllWoods;
import biomesoplenty.common.item.ItemBOPBlock; import biomesoplenty.common.item.ItemBOPBlock;
@ -24,65 +23,34 @@ import biomesoplenty.common.item.ItemBOPBlock;
public abstract class BlockBOPPlanks extends Block implements IBOPBlock public abstract class BlockBOPPlanks extends Block implements IBOPBlock
{ {
// setup paged variant property
// store the variant properties for each page in this array // STAGE require one bit, so we have 3 bits left for the VARIANT which means we can have eight per instance
private static PropertyEnum[] variantProperties; public static final int VARIANTS_PER_PAGE = 16;
// number of variants per page - all 4 meta bits are available so we can have 16
public static final int variantsPerPage = 16;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllWoods.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllWoods.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllWoods> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllWoods>()
{
@Override
public boolean apply(AllWoods wood)
{
switch (wood)
{
case GIANT_FLOWER: case DEAD:
return false;
default:
return (wood.ordinal() >= (variantsPerPage * pageNum)) && (wood.ordinal() < (variantsPerPage * (pageNum+1)));
}
}
};
}
// child classes must implement to define their page number // child classes must implement to define their page number
abstract public int getPageNum(); abstract public int getPageNum();
// fetch the variant property for a given page
public static PropertyEnum getVariantProperty(int pageNum)
{
return BOPWoodEnums.getVariantProperty(pageNum, VARIANTS_PER_PAGE, BOPWoodEnums.WoodsFilterType.WITH_PLANKS);
}
// fetch the current instance's variant property // fetch the current instance's variant property
public PropertyEnum getMyVariantProperty() public PropertyEnum getMyVariantProperty()
{ {
return getVariantProperty(this.getPageNum()); return getVariantProperty(getPageNum());
} }
// get the meta bits from the variant
public int metaFromVariant(AllWoods wood) public int metaFromVariant(AllWoods wood)
{ {
return wood.ordinal() % variantsPerPage; return wood.ordinal() % VARIANTS_PER_PAGE;
} }
// get the variant from meta bits (safely)
public AllWoods variantFromMeta(int meta) public AllWoods variantFromMeta(int meta)
{ {
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllWoods.values().length)); // clamp to int i = Math.max(0, Math.min(meta + (this.getPageNum() * VARIANTS_PER_PAGE), AllWoods.values().length));
return AllWoods.values()[i]; return AllWoods.values()[i];
} }
// add properties (note we inherit LOG_AXIS property from parent BlockLog) // add properties (note we inherit LOG_AXIS property from parent BlockLog)
@Override @Override
protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { getMyVariantProperty() });} protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { getMyVariantProperty() });}

View file

@ -10,9 +10,8 @@ package biomesoplenty.common.block;
import java.util.Random; import java.util.Random;
import com.google.common.base.Predicate;
import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.BOPTreeEnums;
import biomesoplenty.api.block.BOPTreeEnums.AllTrees; import biomesoplenty.api.block.BOPTreeEnums.AllTrees;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.IGrowable; import net.minecraft.block.IGrowable;
@ -31,65 +30,35 @@ import net.minecraft.world.gen.feature.WorldGenerator;
public abstract class BlockBOPSapling extends BlockDecoration implements IGrowable { public abstract class BlockBOPSapling extends BlockDecoration implements IGrowable {
// store the variant properties for each page in this array // setup paged variant property
private static PropertyEnum[] variantProperties;
// STAGE require one bit, so we have 3 bits left for the VARIANT which means we can have eight per instance // STAGE require one bit, so we have 3 bits left for the VARIANT which means we can have eight per instance
public static final int variantsPerPage = 8; public static final int VARIANTS_PER_PAGE = 8;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllTrees.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllTrees.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllTrees> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllTrees>()
{
@Override
public boolean apply(AllTrees tree)
{
switch (tree)
{
case RED_BIG_FLOWER: case YELLOW_BIG_FLOWER:
return false;
default:
return (tree.ordinal() >= (variantsPerPage * pageNum)) && (tree.ordinal() < (variantsPerPage * (pageNum+1)));
}
}
};
}
// child classes must implement to define their page number // child classes must implement to define their page number
abstract public int getPageNum(); abstract public int getPageNum();
// fetch the variant property for a given page
public static PropertyEnum getVariantProperty(int pageNum)
{
return BOPTreeEnums.getVariantProperty(pageNum, VARIANTS_PER_PAGE, BOPTreeEnums.TreesFilterType.SAPLINGS);
}
// fetch the current instance's variant property // fetch the current instance's variant property
public PropertyEnum getMyVariantProperty() public PropertyEnum getMyVariantProperty()
{ {
return getVariantProperty(this.getPageNum()); return getVariantProperty(getPageNum());
} }
// get the meta bits from the variant
public int metaFromVariant(AllTrees tree) public int metaFromVariant(AllTrees tree)
{ {
return tree.ordinal() % variantsPerPage; return tree.ordinal() % VARIANTS_PER_PAGE;
} }
// get the variant from meta bits (safely)
public AllTrees variantFromMeta(int meta) public AllTrees variantFromMeta(int meta)
{ {
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllTrees.values().length)); // clamp to int i = Math.max(0, Math.min(meta + (this.getPageNum() * VARIANTS_PER_PAGE), AllTrees.values().length));
return AllTrees.values()[i]; return AllTrees.values()[i];
} }
// add properties // add properties
public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1); public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
@Override @Override

View file

@ -1,116 +0,0 @@
/*******************************************************************************
* Copyright 2014, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.block;
import com.google.common.base.Predicate;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.IStringSerializable;
public abstract class BlockCheese extends Block
{
public enum AllCheeses implements IStringSerializable
{
CHEDDAR, EDAM, MOZARELLA, STILTON, PARMESAN, BRIE, LANCS, WENSLEYDALE, GRUYERE, COMTE;
@Override
public String getName()
{
return this.name().toLowerCase();
}
@Override
public String toString()
{
return this.getName();
}
}
// store the variant properties for each page in this array
private static PropertyEnum[] variantProperties;
// number of variants per page
public static final int variantsPerPage = 4;
// fetch a particular page's variant property
// the first time this is called, it also sets up the array of variant properties
protected static PropertyEnum getVariantProperty(int pageNum)
{
int len = AllCheeses.values().length;
int numPages = (int) Math.ceil( (double)len / variantsPerPage);
if (variantProperties == null)
{
variantProperties = new PropertyEnum[numPages];
}
pageNum = Math.max(0, Math.min(pageNum, numPages - 1));
if (variantProperties[pageNum] == null)
{
variantProperties[pageNum] = PropertyEnum.create("variant", AllCheeses.class, getVariantEnumFilter(pageNum));
}
return variantProperties[pageNum];
}
// child classes must implement to define their page number
abstract public int getPageNum();
// fetch the current instance's variant property
public PropertyEnum getMyVariantProperty()
{
return getVariantProperty(this.getPageNum());
}
// define the filter function used to reduce the set of enum values to the subset for the given page
protected static Predicate<AllCheeses> getVariantEnumFilter(final int pageNum)
{
return new Predicate<AllCheeses>()
{
@Override
public boolean apply(AllCheeses cheese)
{
return (cheese.ordinal() >= (variantsPerPage * pageNum)) && (cheese.ordinal() < (variantsPerPage * (pageNum+1)));
}
};
}
public int metaFromVariant(AllCheeses type)
{
return type.ordinal() % variantsPerPage;
}
public AllCheeses variantFromMeta(int meta)
{
int i = Math.max(0, Math.min(meta + (this.getPageNum() * variantsPerPage), AllCheeses.values().length)); // clamp to
return AllCheeses.values()[i];
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] { getMyVariantProperty() });
}
public BlockCheese()
{
super(Material.cake);
}
// map from state to meta and vice verca
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(getMyVariantProperty(), variantFromMeta(meta & 3)); // in addition to other properties
}
@Override
public int getMetaFromState(IBlockState state)
{
return metaFromVariant((AllCheeses) state.getValue(getMyVariantProperty())); // plus bits from other properties
}
}

View file

@ -1,17 +0,0 @@
package biomesoplenty.common.block;
import net.minecraft.block.properties.PropertyEnum;
public class BlockCheese0 extends BlockCheese
{
public static final int PAGENUM = 0;
// create a static reference to this block's variant property
// this is for convenience, and for consistency with other Block classes
public static final PropertyEnum VARIANT = BlockCheese.getVariantProperty(PAGENUM);
@Override
public int getPageNum() {return PAGENUM;}
}

View file

@ -1,17 +0,0 @@
package biomesoplenty.common.block;
import net.minecraft.block.properties.PropertyEnum;
public class BlockCheese1 extends BlockCheese
{
public static final int PAGENUM = 1;
// create a static reference to this block's variant property
// this is for convenience, and for consistency with other Block classes
public static final PropertyEnum VARIANT = BlockCheese.getVariantProperty(PAGENUM);
@Override
public int getPageNum() {return PAGENUM;}
}