Created
July 31, 2017 08:51
-
-
Save boq/90e65106132fbff695ed566874620f9a 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
| import java.util.Optional; | |
| import net.minecraft.block.Block; | |
| import net.minecraft.block.BlockContainer; | |
| import net.minecraft.block.material.Material; | |
| import net.minecraft.block.properties.PropertyInteger; | |
| import net.minecraft.block.state.BlockStateContainer; | |
| import net.minecraft.block.state.IBlockState; | |
| import net.minecraft.client.Minecraft; | |
| import net.minecraft.client.renderer.BufferBuilder; | |
| import net.minecraft.client.renderer.texture.TextureAtlasSprite; | |
| import net.minecraft.entity.EntityLivingBase; | |
| import net.minecraft.item.Item; | |
| import net.minecraft.item.ItemBlock; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.tileentity.TileEntity; | |
| import net.minecraft.util.EnumFacing; | |
| import net.minecraft.util.EnumHand; | |
| import net.minecraft.util.ResourceLocation; | |
| import net.minecraft.util.math.BlockPos; | |
| import net.minecraft.world.World; | |
| import net.minecraftforge.client.model.animation.FastTESR; | |
| import net.minecraftforge.event.RegistryEvent; | |
| import net.minecraftforge.fluids.Fluid; | |
| import net.minecraftforge.fluids.FluidRegistry; | |
| import net.minecraftforge.fml.client.registry.ClientRegistry; | |
| import net.minecraftforge.fml.common.Mod; | |
| import net.minecraftforge.fml.common.Mod.EventBusSubscriber; | |
| import net.minecraftforge.fml.common.Mod.EventHandler; | |
| import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; | |
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
| import net.minecraftforge.fml.common.registry.GameRegistry; | |
| @Mod(modid = "tesr-test") | |
| public class TestMod { | |
| private static TextureAtlasSprite getFluidTexture(Fluid fluid) { | |
| final ResourceLocation textureLocation = fluid.getStill(); | |
| return getTextureAtlasLocation(textureLocation); | |
| } | |
| private static TextureAtlasSprite getTextureAtlasLocation(ResourceLocation textureLocation) { | |
| return Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(textureLocation.toString()); | |
| } | |
| private static class TestTesr extends FastTESR<TesrTE> { | |
| private static void addVertexWithUV(BufferBuilder wr, double x, double y, double z, double u, double v, int skyLight, int blockLight) { | |
| wr.pos(x, y, z).color(0xFF, 0xFF, 0xFF, 0xFF).tex(u, v).lightmap(skyLight, blockLight).endVertex(); | |
| } | |
| @Override | |
| public void renderTileEntityFast(TesrTE te, double x, double y, double z, float partialTicks, int destroyStage, float partial, BufferBuilder wr) { | |
| final int skyLight = 0x00f0; | |
| final int blockLight = 0x00f0; | |
| final TextureAtlasSprite texture = getFluidTexture(te.fluid); | |
| final double uMin = texture.getMinU(); | |
| final double uMax = texture.getMaxU(); | |
| final double vMin = texture.getMinV(); | |
| final double vMax = texture.getMaxV(); | |
| wr.setTranslation(x, y, z); | |
| addVertexWithUV(wr, 1, 0, 0, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 0, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 0, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 0, uMin, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 0, 1, uMin, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 1, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 1, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 1, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 0, 0, uMin, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 0, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 1, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 0, 1, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 1, uMin, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 1, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 0, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 0, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 0, 0, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 0, 1, uMin, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 1, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 0, 0, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 0, uMax, vMin, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 0, uMax, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 0, 1, 1, uMin, vMax, skyLight, blockLight); | |
| addVertexWithUV(wr, 1, 1, 1, uMin, vMin, skyLight, blockLight); | |
| wr.setTranslation(0, 0, 0); | |
| } | |
| } | |
| public static class TesrTE extends TileEntity { | |
| private final Fluid fluid; | |
| public TesrTE(Fluid fluid) { | |
| this.fluid = fluid; | |
| } | |
| @Override | |
| public boolean hasFastRenderer() { | |
| return true; | |
| } | |
| @Override | |
| public boolean shouldRenderInPass(int pass) { | |
| return pass == 1; | |
| } | |
| } | |
| public static final PropertyInteger FLUID = PropertyInteger.create("fluid", 0, 15); | |
| private static final Block testBlock = new BlockContainer(Material.CORAL) { | |
| @Override | |
| public TileEntity createNewTileEntity(World worldIn, int meta) { | |
| Optional<Fluid> maybeFluid = FluidRegistry.getRegisteredFluids().values().stream().skip(meta).findFirst(); | |
| return maybeFluid.map(TesrTE::new).orElse(null); | |
| } | |
| @Override | |
| public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) { | |
| final ItemStack held = placer.getHeldItem(hand); | |
| return getDefaultState().withProperty(FLUID, Math.min(held.getMetadata(), 15)); | |
| } | |
| @Override | |
| protected BlockStateContainer createBlockState() { | |
| return new BlockStateContainer(this, FLUID); | |
| } | |
| @Override | |
| public int getMetaFromState(IBlockState state) { | |
| return state.getValue(FLUID); | |
| } | |
| @Override | |
| public boolean isOpaqueCube(IBlockState state) { | |
| return false; | |
| } | |
| }; | |
| @EventBusSubscriber | |
| public static class BlockHolder { | |
| @SubscribeEvent | |
| public static void onBlockRegister(RegistryEvent.Register<Block> evt) { | |
| evt.getRegistry().register(testBlock.setRegistryName("tesr-test")); | |
| } | |
| @SubscribeEvent | |
| public static void onItemRegister(RegistryEvent.Register<Item> evt) { | |
| evt.getRegistry().register(new ItemBlock(testBlock).setRegistryName("tesr-test")); | |
| } | |
| } | |
| @EventHandler | |
| public void preInit(FMLPreInitializationEvent evt) { | |
| GameRegistry.registerTileEntity(TesrTE.class, "tesr-te"); | |
| ClientRegistry.bindTileEntitySpecialRenderer(TesrTE.class, new TestTesr()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment