Skip to content

Instantly share code, notes, and snippets.

@tterrag1098
Last active August 29, 2015 14:24
Show Gist options
  • Save tterrag1098/4da3de31ad637a46a2ac to your computer and use it in GitHub Desktop.
Save tterrag1098/4da3de31ad637a46a2ac to your computer and use it in GitHub Desktop.
The power of @DeleGate!
package tterrag.customthings.common.block;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import tterrag.customthings.common.config.json.BlockType;
import tterrag.customthings.common.config.json.BlockType.BlockData;
import tterrag.customthings.common.config.json.BlockType.MaterialSound;
public class BlockCustom
extends Block
implements IBlockCustom
{
public final BlockType[] types = new BlockType[16];
private final BlockProxy<BlockCustom> proxy;
private final BlockType.BlockData data;
public ArrayList<ItemStack> getDrops(World arg0, int arg1, int arg2, int arg3, int arg4, int arg5)
{
return this.proxy.getDrops(arg0, arg1, arg2, arg3, arg4, arg5);
}
public boolean isToolEffective(String arg0, int arg1)
{
return this.proxy.isToolEffective(arg0, arg1);
}
public boolean isOpaqueCube()
{
return this.proxy.isOpaqueCube();
}
public IIcon getIcon(IBlockAccess arg0, int arg1, int arg2, int arg3, int arg4)
{
return this.proxy.getIcon(arg0, arg1, arg2, arg3, arg4);
}
public IIcon getIcon(int arg0, int arg1)
{
return this.proxy.getIcon(arg0, arg1);
}
public String getHarvestTool(int arg0)
{
return this.proxy.getHarvestTool(arg0);
}
public boolean canHarvestBlock(EntityPlayer arg0, int arg1)
{
return this.proxy.canHarvestBlock(arg0, arg1);
}
public int damageDropped(int arg0)
{
return this.proxy.damageDropped(arg0);
}
public float getBlockHardness(World arg0, int arg1, int arg2, int arg3)
{
return this.proxy.getBlockHardness(arg0, arg1, arg2, arg3);
}
public void registerBlockIcons(IIconRegister arg0)
{
this.proxy.registerBlockIcons(arg0);
}
public int getExpDrop(IBlockAccess arg0, int arg1, int arg2)
{
return this.proxy.getExpDrop(arg0, arg1, arg2);
}
public float getExplosionResistance(Entity arg0, World arg1, int arg2, int arg3, int arg4, double arg5, double arg6, double arg7)
{
return this.proxy.getExplosionResistance(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public int getHarvestLevel(int arg0)
{
return this.proxy.getHarvestLevel(arg0);
}
public BlockCustom(BlockType.BlockData data)
{
super(data.getType().material);
setStepSound(data.getType().sound);
this.data = data;
this.proxy = new BlockProxy(this);
setHardness(0.3F);
setResistance(0.5F);
setCreativeTab(CreativeTabs.tabBlock);
}
public void setType(BlockType type, int meta)
{
this.types[(meta % this.types.length)] = type;
}
public BlockType getType(int meta)
{
return this.types[(meta % this.types.length)];
}
public BlockType getType(ItemStack stack)
{
return this.types[(stack.getItemDamage() % this.types.length)];
}
public BlockType[] getTypes()
{
return this.types;
}
public BlockType.BlockData getData()
{
return this.data;
}
}
package tterrag.customthings.common.block;
import lombok.experimental.Delegate;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import tterrag.customthings.common.config.json.BlockType;
import tterrag.customthings.common.config.json.BlockType.BlockData;
public class BlockCustom extends Block implements IBlockCustom
{
public final BlockType[] types = new BlockType[16];
@Delegate
private final BlockProxy<BlockCustom> proxy;
private final BlockData data;
public BlockCustom(BlockData data)
{
super(data.getType().material);
setStepSound(data.getType().sound);
this.data = data;
this.proxy = new BlockProxy<BlockCustom>(this);
setHardness(0.3f);
setResistance(0.5f);
setCreativeTab(CreativeTabs.tabBlock);
}
@Override
public void setType(BlockType type, int meta)
{
types[meta % types.length] = type;
}
@Override
public BlockType getType(int meta)
{
return types[meta % types.length];
}
@Override
public BlockType getType(ItemStack stack)
{
return types[stack.getItemDamage() % types.length];
}
@Override
public BlockType[] getTypes()
{
return types;
}
@Override
public BlockData getData()
{
return data;
}
}
package tterrag.customthings.common.block;
import java.util.ArrayList;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import tterrag.customthings.CustomThings;
import tterrag.customthings.common.config.json.BlockType;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@RequiredArgsConstructor
public class BlockProxy<T extends Block & IBlockCustom>
{
private final T block;
private final Random rand = new Random();
@SideOnly(Side.CLIENT)
private IIcon[][] icons;
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister register)
{
icons = new IIcon[16][6];
for (int i = 0; i < block.getTypes().length; i++)
{
BlockType type = block.getTypes()[i];
if (type != null)
{
if (type.textureMap == null)
{
icons[i][0] = register.registerIcon(CustomThings.MODID.toLowerCase() + ":" + type.name);
}
else
{
for (int j = 0; j < type.textureMap.length; j++)
{
String tex = type.textureMap[j];
icons[i][j] = register.registerIcon(CustomThings.MODID.toLowerCase() + ":" + tex);
}
}
}
}
}
public IIcon getIcon(int side, int meta)
{
return block.getTypes()[meta].textureMap == null ? icons[meta][0] : icons[meta][side];
}
public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side)
{
return getIcon(side, world.getBlockMetadata(x, y, z));
}
public boolean isOpaqueCube()
{
return block.getData().isOpaque();
}
public boolean isToolEffective(String tool, int metadata)
{
BlockType type = block.getTypes()[metadata];
return type.toolType.isEmpty() ? block.isToolEffective(tool, metadata) : tool.equals(type.toolType);
}
public String getHarvestTool(int metadata)
{
BlockType type = block.getTypes()[metadata];
return type.toolType.isEmpty() ? block.getHarvestTool(metadata) : type.toolType;
}
public boolean canHarvestBlock(EntityPlayer player, int meta)
{
BlockType type = block.getType(meta);
ItemStack held = player.getHeldItem();
int harvestLevel = getHarvestLevel(meta);
if (type.toolType.isEmpty() || held == null)
{
return block.canHarvestBlock(player, meta);
}
return held.getItem().getHarvestLevel(held, getHarvestTool(meta)) >= harvestLevel &&
held.getItem().getToolClasses(held).contains(type.toolType);
}
public int getHarvestLevel(int metadata)
{
return block.getType(metadata).harvestLevel;
}
public float getBlockHardness(World world, int x, int y, int z)
{
BlockType type = getType(world, x, y, z);
return type == null ? block.getBlockHardness(world, x, y, z) : type.hardness;
}
public float getExplosionResistance(Entity par1Entity, World world, int x, int y, int z, double explosionX, double explosionY, double explosionZ)
{
BlockType type = getType(world, x, y, z);
return type == null ? block.getExplosionResistance(par1Entity) : type.resistance;
}
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
{
BlockType type = block.getType(metadata);
return type == null || type.drops.length == 0 ? block.getDrops(world, x, y, z, metadata, fortune) : type.getStackDrops();
}
public int damageDropped(int metadata)
{
return metadata;
}
public int getExpDrop(IBlockAccess world, int metadata, int fortune)
{
BlockType type = block.getTypes()[metadata];
return type == null ? 0 : rand.nextInt(type.maxXp - type.minXp + 1) + type.minXp;
}
private BlockType getType(World world, int x, int y, int z)
{
int meta = world.getBlockMetadata(x, y, z);
return block.getType(meta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment