Wand:
package tschipp.creativePlus.items;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class Wand extends Item implements IItemColor{
public Vec3d pos1;
public Vec3d pos2;
public Vec3d difference;
public Vec3d posToPlaceBlock;
public IBlockState mat;
public Wand() {
this.setMaxStackSize(1);
}
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tab, List<ItemStack> subItems)
{
NBTTagCompound subTag = new NBTTagCompound();
subTag.setString("material", "minecraft:stone");
subTag.setInteger("damage", 0);
subTag.setDouble("pos1x", 0);
subTag.setDouble("pos1y", 0);
subTag.setDouble("pos1z", 0);
subTag.setDouble("pos2x", 0);
subTag.setDouble("pos2y", 0);
subTag.setDouble("pos2z", 0);
subTag.setBoolean("hasSetPos1", false);
subTag.setBoolean("hasSetPos2", false);
ItemStack stack = new ItemStack(item, 1, 0);
stack.setTagCompound(subTag);
subItems.add(stack);
}
@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack)
{
if(stack.getTagCompound() != null) {
Block block = Block.getBlockFromName(stack.getTagCompound().getString("material"));
IBlockState state = block.getStateFromMeta(stack.getTagCompound().getInteger("damage"));
ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
return "Line Wand (" + blockStack.getDisplayName() +")";
} else {
return "" + I18n.translateToLocal(this.getUnlocalizedNameInefficiently(stack) + ".name");
}
}
@Override
@SideOnly(Side.CLIENT)
public int getColorFromItemstack(ItemStack stack, int tintIndex) {
{
Block block = Block.getBlockFromName(stack.getTagCompound().getString("material"));
IBlockState state = block.getStateFromMeta(stack.getTagCompound().getInteger("damage"));
if(block.getMapColor(state).colorValue == 000) {
System.out.println(block.getMapColor(state));
return 201196;
} else {
return block.getMapColor(state).colorValue;
}
}
}
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
World world = player.worldObj;
if(pos.getX() < 0) {
pos2 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ());
}
if(pos.getX() > 0) {
pos2 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ());
}
if(pos.getZ() < 0) {
pos2 = new Vec3d(pos.getX(), pos.getY(), pos.getZ()-0.5);
}
if(pos.getX() > 0) {
pos2 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() < 0 && pos.getZ() < 0) {
pos2 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()-0.5);
}
if(pos.getX() > 0 && pos.getZ() > 0) {
pos2 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() < 0 && pos.getZ() > 0) {
pos2 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() > 0 && pos.getZ() < 0) {
pos2 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ()-0.5);
}
NBTTagCompound subTag = stack.getTagCompound();
subTag.setDouble("pos2x", pos2.xCoord);
subTag.setDouble("pos2y", pos2.yCoord);
subTag.setDouble("pos2z", pos2.zCoord);
subTag.setBoolean("hasSetPos2", true);
stack.setTagCompound(subTag);
if(!world.isRemote) {
player.addChatMessage(new TextComponentString(TextFormatting.LIGHT_PURPLE + "Set Pos2 at X: " + (int)pos2.xCoord + ", Y: " + (int)pos2.yCoord + ", Z: " + (int)pos2.zCoord));
}
world.scheduleBlockUpdate(pos, world.getBlockState(pos).getBlock(), 0, 1);
return EnumActionResult.PASS;
}
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer playerIn, EnumHand hand)
{
if(stack.getTagCompound().getBoolean("hasSetPos1") && stack.getTagCompound().getBoolean("hasSetPos2") && playerIn.isSneaking()) {
NBTTagCompound nbt = stack.getTagCompound();
Vec3d setPos1 = new Vec3d(nbt.getDouble("pos1x"), nbt.getDouble("pos1y"), nbt.getDouble("pos1z"));
Vec3d setPos2 = new Vec3d(nbt.getDouble("pos2x"), nbt.getDouble("pos2y"), nbt.getDouble("pos2z"));
difference = setPos1.subtractReverse(setPos2).normalize();
posToPlaceBlock = setPos1;
if(setPos1.xCoord > 0 && setPos2.xCoord < 0 || setPos1.xCoord < 0 && setPos2.xCoord > 0 || setPos1.zCoord > 0 && setPos2.zCoord < 0 || setPos1.zCoord < 0 && setPos2.zCoord > 0 || setPos1.yCoord > 256 || setPos2.yCoord > 256 || setPos1.yCoord < 0 || setPos2.yCoord < 0) {
if(!world.isRemote) {
playerIn.addChatComponentMessage(new TextComponentString(TextFormatting.RED +"Don't try to create lines from positive X to negative X, Z, or Y"));
}
} else {
if(compare(setPos1, setPos2)) {
pos1 = null;
if(!world.isRemote) {
world.setBlockState(new BlockPos((int)setPos1.xCoord, (int)setPos1.yCoord, (int)setPos1.zCoord), Block.getBlockFromName(stack.getTagCompound().getString("material")).getStateFromMeta(stack.getTagCompound().getInteger("damage")), 2);
BlockPos pos2 = new BlockPos((int)setPos1.xCoord, (int)setPos1.yCoord, (int)setPos1.zCoord);
world.scheduleBlockUpdate(pos2, world.getBlockState(pos2).getBlock(), 0, 1);
}
pos2 = null;
} else {
while (!compare(setPos1, setPos2))
{
posToPlaceBlock = posToPlaceBlock.add(difference);
if(!world.isRemote) {
world.setBlockState(new BlockPos((int)posToPlaceBlock.xCoord, (int)posToPlaceBlock.yCoord, (int)posToPlaceBlock.zCoord), Block.getBlockFromName(stack.getTagCompound().getString("material")).getStateFromMeta(stack.getTagCompound().getInteger("damage")), 2);
}
}
}
}
}
return new ActionResult(EnumActionResult.SUCCESS, stack);
}
public boolean compare(Vec3d setPos1, Vec3d setPos2) {
if(Math.abs(setPos1.xCoord) <= Math.abs(setPos2.xCoord) && Math.abs(setPos1.zCoord) <= Math.abs(setPos2.zCoord)) {
return Math.abs(posToPlaceBlock.xCoord) >= Math.abs(setPos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) >= Math.abs(setPos2.zCoord);
}
else if(Math.abs(setPos1.xCoord) >= Math.abs(setPos2.xCoord) && Math.abs(setPos1.zCoord) >= Math.abs(setPos2.zCoord)) {
return Math.abs(posToPlaceBlock.xCoord) <= Math.abs(setPos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) <= Math.abs(setPos2.zCoord);
}
else if(Math.abs(setPos1.xCoord) <= Math.abs(setPos2.xCoord) && Math.abs(setPos1.zCoord) >= Math.abs(setPos2.zCoord)) {
return Math.abs(posToPlaceBlock.xCoord) >= Math.abs(setPos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) <= Math.abs(setPos2.zCoord);
}
else if(Math.abs(setPos1.xCoord) >= Math.abs(setPos2.xCoord) && Math.abs(setPos1.zCoord) <= Math.abs(setPos2.zCoord)) {
return Math.abs(posToPlaceBlock.xCoord) <= Math.abs(setPos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) >= Math.abs(setPos2.zCoord);
}
else {
return true;
}
}
/* public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player)
{
World world = player.worldObj;
if(player.isSneaking()) {
mat = world.getBlockState(pos);
NBTTagCompound subTag = itemstack.getTagCompound();
subTag.setString("material", mat.getBlock().getRegistryName().getResourceDomain() + ":" + mat.getBlock().getRegistryName().getResourcePath());
subTag.setInteger("damage", mat.getBlock().getMetaFromState(mat));
Block block = (Block)Block.REGISTRY.getObject(new ResourceLocation(itemstack.getTagCompound().getString("material")));
itemstack.setTagCompound(subTag);
if(!world.isRemote) {
player.addChatComponentMessage(new TextComponentString(TextFormatting.LIGHT_PURPLE +"Material set to: " + net.minecraft.util.text.translation.I18n.translateToLocal(net.minecraft.util.text.translation.I18n.translateToLocal(block.getLocalizedName()))));
}
world.scheduleBlockUpdate(pos, world.getBlockState(pos).getBlock(), 0, 1);
}
else {
if(pos.getX() < 0) {
pos1 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ());
}
if(pos.getX() > 0) {
pos1 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ());
}
if(pos.getZ() < 0) {
pos1 = new Vec3d(pos.getX(), pos.getY(), pos.getZ()-0.5);
}
if(pos.getX() > 0) {
pos1 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() < 0 && pos.getZ() < 0) {
pos1 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()-0.5);
}
if(pos.getX() > 0 && pos.getZ() > 0) {
pos1 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() < 0 && pos.getZ() > 0) {
pos1 = new Vec3d(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5);
}
if(pos.getX() > 0 && pos.getZ() < 0) {
pos1 = new Vec3d(pos.getX()+0.5, pos.getY(), pos.getZ()-0.5);
}
NBTTagCompound subTag = itemstack.getTagCompound();
subTag.setDouble("pos1x", pos1.xCoord);
subTag.setDouble("pos1y", pos1.yCoord);
subTag.setDouble("pos1z", pos1.zCoord);
subTag.setBoolean("hasSetPos1", true);
itemstack.setTagCompound(subTag);
if(!world.isRemote) {
player.addChatComponentMessage(new TextComponentString(TextFormatting.LIGHT_PURPLE +"Set Pos1 at X: " + (int)pos1.xCoord + ", Y: " + (int)pos1.yCoord + ", Z: " + (int)pos1.zCoord));
}
world.scheduleBlockUpdate(pos, world.getBlockState(pos).getBlock(), 0, 1);
}
return true;
}
*/
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
{
Block block = Block.getBlockFromName(stack.getTagCompound().getString("material"));
IBlockState state = block.getStateFromMeta(stack.getTagCompound().getInteger("damage"));
ItemStack blockStack = new ItemStack(block, 1, block.getMetaFromState(state));
tooltip.add("Draws a straight line from two points");
tooltip.add("Material: " + blockStack.getDisplayName());
tooltip.add("Pos1 = X: " + stack.getTagCompound().getDouble("pos1x") + ", Y: " + stack.getTagCompound().getDouble("pos1y") + ", Z: " + stack.getTagCompound().getDouble("pos1z"));
tooltip.add("Pos2 = X: " + stack.getTagCompound().getDouble("pos2x") + ", Y: " + stack.getTagCompound().getDouble("pos2y") + ", Z: " + stack.getTagCompound().getDouble("pos2z"));
}
}
BlockGravelGrass:
package tschipp.buildingblocks.blocks;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import tschipp.buildingblocks.BBMod;
import tschipp.buildingblocks.items.BBItems;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockGravelGrass extends Block implements IBlockColor {
public BlockGravelGrass(Material materialIn)
{
super(materialIn, MapColor.STONE);
this.blockHardness = 0.8F;
this.blockResistance = 3.5F;
this.setSoundType(SoundType.PLANT);
this.setUnlocalizedName("gravelGrass");
this.setCreativeTab(BBMod.buildingBlocks);
this.setHarvestLevel("shovel", 0);
}
@Override
@SideOnly(Side.CLIENT)
public int colorMultiplier(IBlockState state, @Nullable IBlockAccess worldIn, @Nullable BlockPos pos, int tintIndex)
{
return 0;
}
@SideOnly(Side.CLIENT)
public BlockRenderLayer getBlockLayer()
{
return BlockRenderLayer.CUTOUT;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return true;
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
List<ItemStack> drops = new ArrayList<ItemStack>();
drops.add(new ItemStack(Blocks.DIRT, 1));
drops.add(new ItemStack(BBItems.pebbles, 1));
return drops;
}
}