public class MCreatorTest {
public static BlockPos blockpos;
public static class BlockCustom extends Block implements ITileEntityProvider {
public static final PropertyDirection FACING = BlockHorizontal.FACING;
public static final PropertyBool OPENED = PropertyBool.create("open");
public static final PropertyEnum<BlockCustom.EnumType> VARIANT = PropertyEnum.<BlockCustom.EnumType>create("variant", BlockCustom.EnumType.class);
public BlockCustom(String name, float hardness, float resistanse , SoundType soundtype) {
super(Material.WOOD);
setRegistryName(name);
setUnlocalizedName(name);
setSoundType(soundtype);
setHarvestLevel("axe", 1);
setHardness(hardness);
setResistance(resistanse);
setCreativeTab(CreativeTabs.FOOD);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(OPENED, Boolean.FALSE).withProperty(VARIANT, BlockCustom.EnumType.COBBLE_ANDESITE));
}
public boolean isOpaqueCube(IBlockState state)
{
return false;
}
public boolean isFullCube(IBlockState state)
{
return false;
}
public int damageDropped(IBlockState state)
{
return ((BlockCustom.EnumType)state.getValue(VARIANT)).getMetadata();
}
//метод позволяющий нам указать, откуда брать субтипы.
public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items)
{
for (BlockCustom.EnumType blockstone$enumtype : BlockCustom.EnumType.values())
{
items.add(new ItemStack(this, 1, blockstone$enumtype.getMetadata()));
}
}
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(VARIANT, BlockCustom.EnumType.byMetadata(meta));
}
public int getMetaFromState(IBlockState state)
{
return ((BlockCustom.EnumType)state.getValue(VARIANT)).getMetadata();
}
//обязательно создаем контейнер стейтов
protected BlockStateContainer createBlockState()
{
return new BlockStateContainer(this, new IProperty[] {FACING, OPENED, VARIANT});
}
@Override
public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
//перечисление наших подтипов
public static enum EnumType implements IStringSerializable {
COBBLE_GRANITE(0, MapColor.DIRT, "granite", true),
COBBLE_DIORITE(1, MapColor.QUARTZ, "diorite", true),
COBBLE_ANDESITE(2, MapColor.STONE, "andesite", true);
private static final BlockCustom.EnumType[] META_LOOKUP = new BlockCustom.EnumType[values().length];
private final int meta;
private final String name;
private final String unlocalizedName;
private final MapColor mapColor;
private final boolean isNatural;
private EnumType(int meta, MapColor mapColor, String name, boolean isNatural)
{
this(meta, mapColor, name, name, isNatural);
}
private EnumType(int meta, MapColor mapColor, String name, String Uname, boolean isNatural)
{
this.meta = meta;
this.name = name;
this.unlocalizedName = Uname;
this.mapColor = mapColor;
this.isNatural = isNatural;
}
public int getMetadata()
{
return this.meta;
}
public MapColor getMapColor()
{
return this.mapColor;
}
public String toString()
{
return this.name;
}
public static BlockCustom.EnumType byMetadata(int meta)
{
if (meta < 0 || meta >= META_LOOKUP.length)
{
meta = 0;
}
return META_LOOKUP[meta];
}
public String getName()
{
return this.name;
}
public String getUnlocalizedName()
{
return this.unlocalizedName;
}
public boolean isNatural()
{
return this.isNatural;
}
static
{
for (BlockCustom.EnumType blockstone$enumtype : values())
{
META_LOOKUP[blockstone$enumtype.getMetadata()] = blockstone$enumtype;
}
}
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileEntityCustom();
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
TileEntity tileentity = world.getTileEntity(pos);
InventoryHelper.dropInventoryItems(world, pos, (TileEntityCustom) tileentity);
world.removeTileEntity(pos);
super.breakBlock(world, pos, state);
}
@Override
public boolean hasComparatorInputOverride(IBlockState state) {
return true;
}
@Override
public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityCustom) {
return Container.calcRedstoneFromInventory((TileEntityCustom) tileentity);
} else {
return 0;
}
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entity, EnumHand hand, EnumFacing side,
float hitX, float hitY, float hitZ) {
super.onBlockActivated(world, pos, state, entity, hand, side, hitX, hitY, hitZ);
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
Block block = this;
{
java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
$_dependencies.put("entity", entity);
$_dependencies.put("x", x);
$_dependencies.put("y", y);
$_dependencies.put("z", z);
$_dependencies.put("world", world);
blockpos = pos;
if( !entity.isSneaking() )
{
TileEntity tileEntity = world.getTileEntity(pos);
if( state.getValue(OPENED) )
{
NetworkHandler.network.sendToServer(new PacketClose(pos.getX(), pos.getY(), pos.getZ()));
}
else
{
((EntityPlayer) entity).openGui(instance, MCreatorGui.GUIID, world, x, y, z);
NetworkHandler.network.sendToServer(new PacketStates(pos.getX(), pos.getY(), pos.getZ()));
}
world.setBlockState(pos, state.withProperty(OPENED, !state.getValue(OPENED)));
if(tileEntity != null)
{
tileEntity.validate();
world.setTileEntity(pos, tileEntity);
}
}
return true;
}
}
public static class TileEntityCustom extends TileEntityLockableLoot {
private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(9, ItemStack.EMPTY);
private int type = 0;
@Override
public int getSizeInventory() {
return 9;
}
@Override
public boolean isEmpty() {
for (ItemStack itemstack : this.stacks)
if (!itemstack.isEmpty())
return false;
return true;
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
@Override
public ItemStack getStackInSlot(int slot) {
return stacks.get(slot);
}
@Override
public String getName() {
return this.hasCustomName() ? this.customName : "container.test";
}
public void setType(int type)
{
this.type = type;
}
public int getType()
{
return type;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.type = compound.getInteger("type");
this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
if (!this.checkLootAndRead(compound))
ItemStackHelper.loadAllItems(compound, this.stacks);
if (compound.hasKey("CustomName", 8))
this.customName = compound.getString("CustomName");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger("type", type);
if (!this.checkLootAndWrite(compound))
ItemStackHelper.saveAllItems(compound, this.stacks);
if (this.hasCustomName())
compound.setString("CustomName", this.customName);
return compound;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public String getGuiID() {
return "roleplay:test";
}
@Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
this.fillWithLoot(playerIn);
return new ContainerDispenser(playerInventory, this);
}
@Override
protected NonNullList<ItemStack> getItems() {
return this.stacks;
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
readFromNBT(pkt.getNbtCompound());
}
@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), getUpdateTag());
}
@Override
public NBTTagCompound getUpdateTag()
{
return writeToNBT(new NBTTagCompound());
}
public void sync()
{
TileEntityUtil.syncToClient(this);
}
}
}
}