Skip to content

Instantly share code, notes, and snippets.

@Xalcon
Created June 3, 2017 09:15
Show Gist options
  • Save Xalcon/443f928480a5baa1da35b2b93fff7b67 to your computer and use it in GitHub Desktop.
Save Xalcon/443f928480a5baa1da35b2b93fff7b67 to your computer and use it in GitHub Desktop.
Quick example on auto tileentity registration
public <T extends Block> T register(T block)
{
GameRegistry.register(block);
if(block instanceof ITileEntityProvider)
{
HasTileEntity teInfo = block.getClass().getDeclaredAnnotation(HasTileEntity.class);
if(hasTe != null)
{
GameRegistry.registerTileEntity(teInfo.tileEntityClass(), teInfo.registryName());
}
else
{
throw new IllegalStateException("Unable to register TileEntity: Block "+block.getClass()+" is a tile entity provider but doesnt have a @HasTileEntity annotation");
}
}
return block;
}
package net.xalcon.ecotec.experimental;
import net.minecraft.tileentity.TileEntity;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) // this means the annotation is available at runtime and will not be stripped by the compiler
public @interface HasTileEntity
{
Class<? extends TileEntity> tileEntityClass();
String registryName();
}
package net.xalcon.ecotec.experimental.blocks;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.xalcon.ecotec.Ecotec;
import net.xalcon.ecotec.experimental.HasTileEntity;
import net.xalcon.ecotec.experimental.tileentities.TestTile;
import javax.annotation.Nullable;
@HasTileEntity(tileEntityClass = TestTile.class, registryName = Ecotec.MODID + ".test_tile")
public class TestBlock extends Block implements ITileEntityProvider
{
public TestBlock(Material blockMaterialIn, MapColor blockMapColorIn)
{
super(blockMaterialIn, blockMapColorIn);
}
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta)
{
return new TestTile();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment