Created
January 16, 2016 00:41
-
-
Save StillManic/21e0c892cccc5618dec8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class OBJCustomDataBlock extends Block | |
{ | |
public static enum PylonVariant implements IStringSerializable | |
{ | |
MANA, | |
NATURA, | |
GAIA; | |
@Override | |
public String getName() | |
{ | |
return name().toLowerCase(Locale.ROOT); | |
} | |
} | |
public static enum PylonPiece implements IStringSerializable | |
{ | |
CRYSTAL("Crystal"), | |
RINGS_AND_PANELS("Crystal_Ring", "Ring_Panel01", "Ring_Panel02", "Ring_Panel03", "Ring_Panel04"), | |
GEMS("Ring_Gem01", "Ring_Gem02", "Ring_Gem03", "Ring_Gem04"); | |
public String[] groups; | |
private PylonPiece(String... groups) | |
{ | |
this.groups = groups; | |
} | |
@Override | |
public String getName() | |
{ | |
return name().toLowerCase(Locale.ROOT); | |
} | |
} | |
public static final PropertyEnum<PylonVariant> PYLON_VARIANT = PropertyEnum.create("variant", PylonVariant.class); | |
public static final OBJCustomDataBlock instance = new OBJCustomDataBlock(); | |
public static final String name = "OBJCustomDataBlock"; | |
private OBJCustomDataBlock() | |
{ | |
super(Material.iron); | |
setCreativeTab(CreativeTabs.tabBlock); | |
setUnlocalizedName(MODID + ":" + name); | |
setLightLevel(0.5f); | |
float pixelx2 = 1.0f / 16.0f * 2.0f; | |
setBlockBounds(pixelx2, 0.0f, pixelx2, 1.0f - pixelx2, 1.0f / 16.0f * 21.0f, 1.0f - pixelx2); | |
setDefaultState(this.blockState.getBaseState().withProperty(PYLON_VARIANT, PylonVariant.MANA)); | |
} | |
@Override | |
public boolean isOpaqueCube() { return false; } | |
@Override | |
public boolean isFullCube() { return false; } | |
@Override | |
public int getRenderType() { return 2; } | |
@Override | |
public boolean hasTileEntity(IBlockState state) { return true; } | |
@Override | |
public TileEntity createTileEntity(World world, IBlockState state) | |
{ | |
return new OBJCustomDataTileEntity(); | |
} | |
@Override | |
public BlockState createBlockState() | |
{ | |
return new BlockState(this, PYLON_VARIANT); | |
} | |
@Override | |
public int getMetaFromState(IBlockState state) | |
{ | |
return state.getValue(PYLON_VARIANT).ordinal(); | |
} | |
@Override | |
public IBlockState getStateFromMeta(int meta) | |
{ | |
return this.getDefaultState().withProperty(PYLON_VARIANT, PylonVariant.values()[meta > PylonVariant.values().length ? 0 : meta]); | |
} | |
@Override | |
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitz) | |
{ | |
world.setBlockState(pos, state.cycleProperty(PYLON_VARIANT), 3); | |
return true; | |
} | |
@Override | |
public int damageDropped(IBlockState state) | |
{ | |
return getMetaFromState(state); | |
} | |
@Override | |
public void getSubBlocks(Item item, CreativeTabs tab, List<ItemStack> list) | |
{ | |
for (int i = 0; i < PylonVariant.values().length; i++) | |
{ | |
list.add(new ItemStack(item, 1, i)); | |
} | |
} | |
} | |
public static class OBJCustomDataTileEntity extends TileEntity | |
{ | |
} | |
public static class OBJCustomDataRender extends TileEntitySpecialRenderer<OBJCustomDataTileEntity> | |
{ | |
private static Map<String, IFlexibleBakedModel> loadedModels = Maps.newHashMap(); | |
private static IModel manaModel = null; | |
private static IModel naturaModel = null; | |
private static IModel gaiaModel = null; | |
public static void init() | |
{ | |
IResourceManager manager = Minecraft.getMinecraft().getResourceManager(); | |
if (manager instanceof IReloadableResourceManager) | |
{ | |
((IReloadableResourceManager) manager).registerReloadListener(new IResourceManagerReloadListener() | |
{ | |
@Override | |
public void onResourceManagerReload(IResourceManager ignored) | |
{ | |
loadedModels.clear(); | |
loadModels(); | |
} | |
}); | |
} | |
} | |
public static void loadModels() | |
{ | |
if (loadedModels.isEmpty()) | |
{ | |
OBJModel model = null; | |
try | |
{ | |
Gson gson = new Gson(); | |
ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder(); | |
model = (OBJModel) ModelLoaderRegistry.getModel(new ResourceLocation(MODID.toLowerCase() + ":block/pylon.obj")); | |
mapBuilder.put(OBJCustomData.Keys.AMBIENT_OCCLUSION.key, gson.toJson(false)); | |
mapBuilder.put(OBJCustomData.Keys.FLIP_UVS.key, gson.toJson("v")); | |
model = (OBJModel) model.process(mapBuilder.build()); | |
OBJCustomData.GroupConfigHandler handler = model.getCustomData().getConfigHandler(); | |
OBJCustomData.GroupConfigBuilder builder = handler.getConfigBuilder(); | |
for (PylonPiece piece : PylonPiece.values()) | |
{ | |
handler.addConfig(builder.startNew(piece.getName()).hideAll(piece.groups).build()); | |
} | |
manaModel = (OBJModel) model.retexture(ImmutableMap.of("pylon", "#pylon")); | |
naturaModel = (OBJModel) model.retexture(ImmutableMap.of("pylon", "#pylon1")); | |
gaiaModel = (OBJModel) model.retexture(ImmutableMap.of("pylon", "#pylon2")); | |
} | |
catch (IOException e) | |
{ | |
FMLLog.severe("Error loading pylon models."); | |
} | |
} | |
} | |
public static void bakeModels() | |
{ | |
for (PylonVariant variant : PylonVariant.values()) | |
{ | |
for (PylonPiece piece : PylonPiece.values()) | |
{ | |
OBJModel.OBJState state = new OBJModel.OBJState().showConfig(piece.getName()); | |
String modelName = variant.getName() + ":" + piece.getName(); | |
IModel modelToBake = variant == PylonVariant.MANA ? manaModel : (variant == PylonVariant.NATURA ? naturaModel : gaiaModel); | |
loadedModels.put(modelName, modelToBake.bake(state, Attributes.DEFAULT_BAKED_FORMAT, BAKED_TEXTURE_GETTER)); | |
} | |
} | |
} | |
@Override | |
public void renderTileEntityAt(OBJCustomDataTileEntity pylon, double x, double y, double z, float partialTicks, int destroyStage) | |
{ | |
if (pylon != null && pylon.getWorld() != null && !pylon.getWorld().isBlockLoaded(pylon.getPos(), false)) return; | |
if (loadedModels.isEmpty()) bakeModels(); | |
GlStateManager.pushMatrix(); | |
// GlStateManager.disableRescaleNormal(); | |
// GlStateManager.enableBlend(); | |
// GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f); | |
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.locationBlocksTexture); | |
PylonVariant variant = pylon.getWorld() != null ? pylon.getWorld().getBlockState(pylon.getPos()).getValue(OBJCustomDataBlock.PYLON_VARIANT) : PylonVariant.MANA; | |
double worldTime = pylon.getWorld() == null ? 0 : pylon.getWorld().getWorldTime() + partialTicks; | |
if (pylon != null) worldTime += new Random(pylon.getPos().hashCode()).nextInt(360); | |
GlStateManager.translate(x + 0.2 + (variant == PylonVariant.NATURA ? -0.1 : 0), y + 0.05, z + 0.8 + (variant == PylonVariant.NATURA ? 0.1 : 0)); | |
float scale = variant == PylonVariant.NATURA ? 0.8f : 0.6f; | |
GlStateManager.scale(scale, 0.6f, scale); | |
if (variant != PylonVariant.NATURA) | |
{ | |
GlStateManager.pushMatrix(); | |
GlStateManager.translate(0.5f, 0.0f, -0.5f); | |
GlStateManager.rotate((float) worldTime * 1.5f, 0.0f, 1.0f, 0.0f); | |
GlStateManager.translate(-0.5f, 0.0f, 0.5f); | |
this.renderModel(variant, PylonPiece.RINGS_AND_PANELS); | |
GlStateManager.translate(0.0d, Math.sin(worldTime / 20.0d) / 20 - 0.25, 0.0d); | |
this.renderModel(variant, PylonPiece.GEMS); | |
GlStateManager.popMatrix(); | |
} | |
GlStateManager.pushMatrix(); | |
GlStateManager.translate(0.0d, Math.sin(worldTime / 20.0d) / 17.5d, 0.0d); | |
GlStateManager.translate(0.5f, 0.0f, -0.5f); | |
GlStateManager.rotate((float) -worldTime, 0.0f, 1.0f, 0.0f); | |
GlStateManager.translate(-0.5f, 0.0f, 0.5f); | |
// GlStateManager.disableCull(); | |
this.renderModel(variant, PylonPiece.CRYSTAL); | |
GlStateManager.color(1.0f, 1.0f, 1.0f, 0.5f); | |
// GlStateManager.disableAlpha(); | |
GlStateManager.scale(1.1f, 1.1f, 1.1f); | |
GlStateManager.translate(-0.05f, -0.1f, 0.05f); | |
this.renderModel(variant, PylonPiece.CRYSTAL); | |
// GlStateManager.enableAlpha(); | |
// GlStateManager.enableCull(); | |
GlStateManager.popMatrix(); | |
// GlStateManager.disableBlend(); | |
// GlStateManager.enableRescaleNormal(); | |
GlStateManager.popMatrix(); | |
} | |
private void renderModel(PylonVariant variant, PylonPiece piece) | |
{ | |
String modelName = variant.getName() + ":" + piece.getName(); | |
IFlexibleBakedModel model = loadedModels.get(modelName); | |
if (model != null) | |
{ | |
Tessellator tessellator = Tessellator.getInstance(); | |
WorldRenderer worldRenderer = tessellator.getWorldRenderer(); | |
worldRenderer.begin(GL11.GL_QUADS, Attributes.DEFAULT_BAKED_FORMAT); | |
for (BakedQuad quad : model.getGeneralQuads()) | |
{ | |
LightUtil.renderQuadColor(worldRenderer, quad, 0xFFFFFFFF); | |
} | |
tessellator.draw(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"forge_marker": 1, | |
"defaults": { | |
"model": "forgedebugmodelloaderregistry:pylon.obj", | |
"transform": "forge:default-block", | |
"custom": { | |
"flipUVs": "v", | |
"ambient": false, | |
"groupConfigs": { | |
"crystal": { "show": ["Crystal"] }, | |
"rings_and_panes": { "show": ["Crystal_Ring", "Ring_Panel01", "Ring_Panel02", "Ring_Panel03", "Ring_Panel04"] }, | |
"gems": { "show": ["Ring_Gem01", "Ring_Gem02", "Ring_Gem03", "Ring_Gem04"] } | |
} | |
} | |
}, | |
"variants": { | |
"inventory": [{}], | |
"variant": { | |
"mana": {"textures": {"pylon": "forgedebugmodelloaderregistry:pylon"}}, | |
"natura": {"textures": {"pylon": "forgedebugmodelloaderregistry:pylon1"}}, | |
"gaia": {"textures": {"pylon": "forgedebugmodelloaderregistry:pylon2"}} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment