Jump to content

[1.12] Drawing Line


Kokkie

Recommended Posts

Now, I saw this post.

I thought, you know, let's add that to my mod. So I basically did the things in there and made some changes. But it doesn't work.

Here's my block class.

public class BlockAutoMiner extends Block {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		setDefaultState(
				blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	@Override
	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	@Override
	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	@Override
	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			te.setLinePos(pos);
		}
	}

	@Override
	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	@Override
	public int getMetaFromState(IBlockState state) {
		return state.getValue(FACING).getIndex();
	}

	@Override
	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
	}

	@Override
	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation(state.getValue(FACING)));
	}

	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = stateIn.getValue(FACING);
				double d0 = pos.getX() + 0.5D;
				double d1 = pos.getY() + 0.5D;
				double d2 = pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
		if (playerIn.getHeldItemMainhand().getItem() == AMItems.LINE_BUTTON && worldIn.isRemote) {
			playerIn.swingArm(EnumHand.MAIN_HAND);
		} else if (playerIn.getHeldItemMainhand().getItem() == AMItems.LINE_BUTTON && !worldIn.isRemote) {
			if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
				TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
				te.setDrawLine(false);
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote) {
			if (hand == EnumHand.MAIN_HAND) {
				if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addFuel(getItemBurnTime(playerIn.getHeldItem(hand)));
					}
				} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						if (te.ACTIVATED) {
							te.setActivated(false);
						} else {
							if (te.FUEL_AMOUNT > 0) {
								te.setActivated(true);
							}
						}
					}
				} else if (playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(Blocks.TORCH)) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addTorch();
					}
				} else if (playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addBlock();
					}
				} else if (playerIn.getHeldItem(hand).getItem() == AMItems.LINE_BUTTON) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.setDrawLine(true);
					}
				}
			}
		} else {
			if (hand == EnumHand.MAIN_HAND) {
				if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH
						|| TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))
						|| playerIn.getHeldItem(hand).getItem() == AMItems.LINE_BUTTON) {
					Minecraft.getMinecraft().player.swingArm(hand);
				}
			}
		}
		return true;

	}

	private int getItemBurnTime(ItemStack stack) {
		if (stack.isEmpty()) {
			return 0;
		} else {
			Item item = stack.getItem();
			if (!item.getRegistryName().getResourceDomain().equals("minecraft")) {
				int burnTime = net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(stack);
				if (burnTime != 0) {
					return burnTime;
				}
			}

			if (item == Item.getItemFromBlock(Blocks.WOODEN_SLAB)) {
				return 160;
			} else if (item == Item.getItemFromBlock(Blocks.WOOL)) {
				return 120;
			} else if (item == Item.getItemFromBlock(Blocks.CARPET)) {
				return 80;
			} else if (item == Item.getItemFromBlock(Blocks.LADDER)) {
				return 320;
			} else if (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON)) {
				return 120;
			} else if (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD) {
				return 320;
			} else if (item == Item.getItemFromBlock(Blocks.COAL_BLOCK)) {
				return 16000;
			} else if (item instanceof ItemTool && "WOOD".equals(((ItemTool) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemSword && "WOOD".equals(((ItemSword) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe) item).getMaterialName())) {
				return 200;
			} else if (item == Items.STICK) {
				return 120;
			} else if (item != Items.BOW && item != Items.FISHING_ROD) {
				if (item == Items.SIGN) {
					return 200;
				} else if (item == Items.COAL) {
					return 1600;
				} else if (item == Items.LAVA_BUCKET) {
					return 20000;
				} else if (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL) {
					if (item == Items.BLAZE_ROD) {
						return 2400;
					} else if (item instanceof ItemDoor && item != Items.IRON_DOOR) {
						return 200;
					} else {
						return item instanceof ItemBoat ? 400 : 0;
					}
				} else {
					return 120;
				}
			} else {
				return 320;
			}
		}
	}

	@Override
	public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
				TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
				for (int i = 0; i < te.TORCHES; i++) {
					worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(),
							new ItemStack(Blocks.TORCH)));
				}
				for (int x = 0; x < te.BLOCKS; x++) {
					worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(),
							new ItemStack(Blocks.COBBLESTONE)));
				}
			}
		}
		super.breakBlock(worldIn, pos, state);
	}

	@Override
	public boolean hasTileEntity() {
		return true;
	}

	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityAutoMiner();
	}

	@Override
	public boolean hasTileEntity(IBlockState state) {
		return true;
	}
}

My tile entity class.

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;
	public boolean UPDATE = false;
	public int TICKS = 0;
	public int TORCHES = 0;
	public int PLACE_TORCH = 0;
	public int BLOCKS = 0;
	public boolean DRAW_LINE = false;
	public BlockPos BEGIN_LINE_POS = new BlockPos(0, 0, 0);
	public int DRAW_LINE_X = 0;
	public int DRAW_LINE_Y = 0;
	public int DRAW_LINE_Z = 0;

	public TileEntityAutoMiner() {
	}

	public void addFuel(int amount) {
		FUEL_AMOUNT += amount;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeFuel() {
		if (FUEL_AMOUNT > 0) {
			FUEL_AMOUNT--;
			IBlockState state = world.getBlockState(pos);
			world.markBlockRangeForRenderUpdate(pos, pos);
			markDirty();
		}
	}

	public void addTorch() {
		TORCHES++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeTorches(int amount) {
		TORCHES -= amount;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void addPlaceTorch() {
		PLACE_TORCH++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removePlaceTorches() {
		PLACE_TORCH = 0;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void addBlock() {
		BLOCKS++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeBlock() {
		BLOCKS--;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void setActivated(boolean active) {
		ACTIVATED = active;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		world.notifyBlockUpdate(pos, state, state, 3);
		world.scheduleBlockUpdate(pos, getBlockType(), 0, 0);
	}

	public void addTick() {
		TICKS++;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void removeTicks() {
		TICKS = 0;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void setDrawLine(boolean draw) {
		DRAW_LINE = draw;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void setLinePos(BlockPos pos) {
		BEGIN_LINE_POS = pos;
		DRAW_LINE_X = pos.getX();
		DRAW_LINE_Y = pos.getY();
		DRAW_LINE_Z = pos.getZ();
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", FUEL_AMOUNT);
		compound.setBoolean("Activated", ACTIVATED);
		compound.setInteger("Ticks", TICKS);
		compound.setInteger("Torches", TORCHES);
		compound.setInteger("PlaceTorch", PLACE_TORCH);
		compound.setInteger("Blocks", BLOCKS);
		compound.setBoolean("DrawLine", DRAW_LINE);
		compound.setInteger("PosX", DRAW_LINE_X);
		compound.setInteger("PosY", DRAW_LINE_Y);
		compound.setInteger("PosZ", DRAW_LINE_Z);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		FUEL_AMOUNT = compound.getInteger("FuelAmount");
		ACTIVATED = compound.getBoolean("Activated");
		TICKS = compound.getInteger("Ticks");
		TORCHES = compound.getInteger("Torches");
		PLACE_TORCH = compound.getInteger("PlaceTorch");
		BLOCKS = compound.getInteger("Blocks");
		DRAW_LINE = compound.getBoolean("DrawLine");
		DRAW_LINE_X = compound.getInteger("PosX");
		DRAW_LINE_Y = compound.getInteger("PosY");
		DRAW_LINE_Z = compound.getInteger("PosZ");
		BEGIN_LINE_POS = new BlockPos(DRAW_LINE_X, DRAW_LINE_Y, DRAW_LINE_Z);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			addTick();
			UPDATE = true;
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				setActivated(false);
				markDirty();
			}
			if (TICKS == 20) {
				if (!world.isRemote) {
					EnumFacing facing = world.getBlockState(pos).getValue(BlockAutoMiner.FACING);
					EnumFacing facingL = facing.rotateYCCW();
					EnumFacing facingR = facing.rotateY();
					Random random = new Random();

					BlockPos positionC = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					BlockPos positionL = positionC.offset(facingL);
					BlockPos positionR = positionC.offset(facingR);
					BlockPos positionCU = positionC.up();
					BlockPos positionLU = positionL.up();
					BlockPos positionRU = positionR.up();
					BlockPos positionCUU = positionCU.up();
					BlockPos positionLUU = positionLU.up();
					BlockPos positionRUU = positionRU.up();

					IBlockState stateC = world.getBlockState(positionC);
					IBlockState stateL = world.getBlockState(positionL);
					IBlockState stateR = world.getBlockState(positionR);
					IBlockState stateCU = world.getBlockState(positionCU);
					IBlockState stateLU = world.getBlockState(positionLU);
					IBlockState stateRU = world.getBlockState(positionRU);
					IBlockState stateCUU = world.getBlockState(positionCUU);
					IBlockState stateLUU = world.getBlockState(positionLUU);
					IBlockState stateRUU = world.getBlockState(positionRUU);

					List<ItemStack> stackC = stateC.getBlock().getDrops(world, positionC, stateC, 0);
					List<ItemStack> stackL = stateL.getBlock().getDrops(world, positionL, stateL, 0);
					List<ItemStack> stackR = stateR.getBlock().getDrops(world, positionR, stateR, 0);
					List<ItemStack> stackCU = stateCU.getBlock().getDrops(world, positionCU, stateCU, 0);
					List<ItemStack> stackLU = stateLU.getBlock().getDrops(world, positionLU, stateLU, 0);
					List<ItemStack> stackRU = stateRU.getBlock().getDrops(world, positionRU, stateRU, 0);
					List<ItemStack> stackCUU = stateCUU.getBlock().getDrops(world, positionCUU, stateCUU, 0);
					List<ItemStack> stackLUU = stateLUU.getBlock().getDrops(world, positionLUU, stateLUU, 0);
					List<ItemStack> stackRUU = stateRUU.getBlock().getDrops(world, positionRUU, stateRUU, 0);

					dropItems(stackC, stateC, positionC);
					dropItems(stackL, stateL, positionL);
					dropItems(stackR, stateR, positionR);
					dropItems(stackCU, stateCU, positionCU);
					dropItems(stackLU, stateLU, positionLU);
					dropItems(stackRU, stateRU, positionRU);
					dropItems(stackCUU, stateCUU, positionCUU);
					dropItems(stackLUU, stateLUU, positionLUU);
					dropItems(stackRUU, stateRUU, positionRUU);

					setBlockToAir(stateC, positionC);
					setBlockToAir(stateL, positionL);
					setBlockToAir(stateR, positionR);
					setBlockToAir(stateCU, positionCU);
					setBlockToAir(stateLU, positionLU);
					setBlockToAir(stateRU, positionRU);
					setBlockToAir(stateCUU, positionCUU);
					setBlockToAir(stateLUU, positionLUU);
					setBlockToAir(stateRUU, positionRUU);

					if ((TORCHES >= 2) && (PLACE_TORCH >= 6)) {
						BlockPos positionLLU = positionLU.offset(facingL);
						BlockPos positionRRU = positionRU.offset(facingR);

						if (world.getBlockState(positionLLU).isSideSolid(world, positionLLU, facingR)
								&& (world.getBlockState(positionLU).getMaterial() == Material.AIR)) {
							if (world.setBlockState(positionLU,
									Blocks.TORCH.getDefaultState().withProperty(BlockTorch.FACING, facingR))) {
								removeTorches(1);
								removePlaceTorches();
							}
						}
						if (world.getBlockState(positionRRU).isSideSolid(world, positionRRU, facingL)
								&& (world.getBlockState(positionRU).getMaterial() == Material.AIR)) {
							if (world.setBlockState(positionRU,
									Blocks.TORCH.getDefaultState().withProperty(BlockTorch.FACING, facingL))) {
								removeTorches(1);
								removePlaceTorches();
							}
						}
					} else {
						addPlaceTorch();
					}

					BlockPos positionD = positionC.down();
					IBlockState stateD = world.getBlockState(positionD);
					if (BLOCKS > 0) {
						if (!stateD.isTopSolid()) {
							world.setBlockState(positionD, Blocks.COBBLESTONE.getDefaultState());
							removeBlock();
						}
					} else {
						if (!stateD.isTopSolid()) {
							setActivated(false);
							removeTicks();
						}
					}
					if (!world.getBlockState(pos.down()).isTopSolid()) {
						setActivated(false);
						removeTicks();
					}
					if (world.getBlockState(positionC).getMaterial().isSolid()) {
						setActivated(false);
						removeTicks();
					}
				}
			}
			if (TICKS == 40) {
				if (!world.isRemote) {
					BlockPos position = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					BlockPos position1 = pos;
					world.setBlockState(position, world.getBlockState(pos));
					TileEntityAutoMiner te = new TileEntityAutoMiner();
					te.ACTIVATED = ACTIVATED;
					te.FUEL_AMOUNT = FUEL_AMOUNT;
					te.TICKS = TICKS;
					te.UPDATE = UPDATE;
					te.TORCHES = TORCHES;
					te.PLACE_TORCH = PLACE_TORCH;
					te.BLOCKS = BLOCKS;
					te.BEGIN_LINE_POS = BEGIN_LINE_POS;
					te.DRAW_LINE = DRAW_LINE;
					te.DRAW_LINE_X = DRAW_LINE_X;
					te.DRAW_LINE_Y = DRAW_LINE_Y;
					te.DRAW_LINE_Z = DRAW_LINE_Z;
					ACTIVATED = false;
					TICKS = 0;
					UPDATE = false;
					TORCHES = 0;
					PLACE_TORCH = 0;
					BLOCKS = 0;
					BEGIN_LINE_POS = new BlockPos(0, 0, 0);
					DRAW_LINE = false;
					DRAW_LINE_X = 0;
					DRAW_LINE_Y = 0;
					DRAW_LINE_Z = 0;
					world.setTileEntity(position, te);
					world.setBlockToAir(position1);
				}
			}
		} else {
			if (UPDATE) {
				IBlockState state = world.getBlockState(pos);
				world.markBlockRangeForRenderUpdate(pos, pos);
			}
			UPDATE = false;
		}
		if (TICKS >= 40) {
			removeTicks();
		}
		if (DRAW_LINE) {
			GL11.glPushMatrix();
			GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
			GL11.glTranslated(-pos.getX(), -pos.getY(), -pos.getZ());
			GL11.glDisable(GL11.GL_LIGHTING);
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			drawLine(new Vec3d(DRAW_LINE_X, DRAW_LINE_Y, DRAW_LINE_Z), new Vec3d(getPos()));
			GL11.glPopAttrib();
			GL11.glPopMatrix();
		}
	}

	private void drawLine(Vec3d blockA, Vec3d blockB) {
		Tessellator tess = Tessellator.getInstance();
		BufferBuilder buff = tess.getBuffer();
		blockB.addVector(0, 1, 0);
		blockB.addVector(0, -1, 0);
		blockA.addVector(0, -0.02f, 0);
		buff.begin(7, buff.getVertexFormat());
		buff.color(0F, 1F, 1F, 1F);

		Vec3d recLong = blockA.subtract(blockB);
		Vec3d perpendicular = Vec3d.fromPitchYaw(1.0F, 1.0F);
		perpendicular = perpendicular.normalize();

		float Width = 1f / 16f;

		Vec3d R1 = blockA.subtract(blockB);
		Vec3d R2 = blockA.subtract(blockB);
		Vec3d R3 = blockA.subtract(blockB);
		Vec3d R4 = blockA.subtract(blockB);

		R1.addVector(blockA.x + perpendicular.x * Width, 0, 0);
		R1.addVector(0, 0, blockA.z + perpendicular.z * Width);
		R2.addVector(blockA.x - perpendicular.x * Width, 0, 0);
		R2.addVector(0, 0, blockA.z - perpendicular.z * Width);
		R1.addVector(0, blockA.y - 0.01, 0);
		R2.addVector(0, blockA.y - 0.01, 0);

		R3.addVector(blockB.x + perpendicular.x * Width, 0, 0);
		R3.addVector(0, 0, blockB.z + perpendicular.z * Width);
		R4.addVector(blockB.x - perpendicular.x * Width, 0, 0);
		R4.addVector(0, 0, blockB.z - perpendicular.z * Width);
		R3.addVector(0, blockB.y + 0.75, 0);
		R4.addVector(0, blockB.y + 0.75, 0);

		int[] a = { (int) (R1.x + 0.5), (int) R1.y, (int) (R1.z + 0.5) };
		int[] b = { (int) (R3.x + 0.5), (int) R3.y, (int) (R3.z + 0.5) };
		int[] c = { (int) (R4.x + 0.5), (int) R4.y, (int) (R4.z + 0.5) };
		int[] d = { (int) (R2.x + 0.5), (int) R2.y, (int) (R2.z + 0.5) };

		buff.addVertexData(a);
		buff.addVertexData(b);
		buff.addVertexData(c);
		buff.addVertexData(d);

		tess.draw();
	}

	private void dropItems(List<ItemStack> stackList, IBlockState state, BlockPos pos) {
		for (int i = 0; i < stackList.size(); i++) {
			ItemStack stack = stackList.get(i);
			if ((stack.getItem() instanceof ItemBlock)
					&& ((Block.getBlockFromItem(stack.getItem()).getBlockHardness(state, world, pos) < 0F)
							|| (Block.getBlockFromItem(stack.getItem()).getBlockHardness(state, world, pos) > 30F))) {
				continue;
			}
			world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack));
		}
	}

	private void setBlockToAir(IBlockState state, BlockPos pos) {
		if (!(state.getBlockHardness(world, pos) > 30F) && !(state.getBlockHardness(world, pos) < 0F)) {
			world.setBlockToAir(pos);
		}
	}
}

Also, how can I cancel the player breaking the block in the onBlockClicked() method?

Edited by Kokkie

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Why are you trying to draw anything in your tile's update method? Not only is that a different thread, rendering is something that must be done each frame, not each tick. A tick is 1/20 of a second. A frame can literaly be any time value dependant on user's settings, monitor and other things. You must render your things in rederers/render events and nowhere else. 

44 minutes ago, Kokkie said:

buff.begin(7, buff.getVertexFormat());

getVertexFormat gets the current drawing format. As you are only starting to draw things this getter can't be used. See DefaultVertexFormats for available formats.

Link to comment
Share on other sites

1 hour ago, V0idWa1k3r said:

Why are you trying to draw anything in your tile's update method? Not only is that a different thread, rendering is something that must be done each frame, not each tick. A tick is 1/20 of a second. A frame can literaly be any time value dependant on user's settings, monitor and other things. You must render your things in rederers/render events and nowhere else. 

Added a TESR.

 

1 hour ago, V0idWa1k3r said:

getVertexFormat gets the current drawing format. As you are only starting to draw things this getter can't be used. See DefaultVertexFormats for available formats.

Changed it to DefaultVertexFormats.POSITION_COLOR.

It doesn't work though.

Block.

public class BlockAutoMiner extends Block {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		setDefaultState(
				blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	@Override
	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	@Override
	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	@Override
	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			te.setLinePos(pos);
		}
	}

	@Override
	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	@Override
	public int getMetaFromState(IBlockState state) {
		return state.getValue(FACING).getIndex();
	}

	@Override
	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
	}

	@Override
	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation(state.getValue(FACING)));
	}

	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = stateIn.getValue(FACING);
				double d0 = pos.getX() + 0.5D;
				double d1 = pos.getY() + 0.5D;
				double d2 = pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote) {
			if (hand == EnumHand.MAIN_HAND) {
				if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addFuel(getItemBurnTime(playerIn.getHeldItem(hand)));
					}
				} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						if (te.ACTIVATED) {
							te.setActivated(false);
						} else {
							if (te.FUEL_AMOUNT > 0) {
								te.setActivated(true);
							}
						}
					}
				} else if (playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(Blocks.TORCH)) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addTorch();
					}
				} else if (playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addBlock();
					}
				} else if (playerIn.getHeldItem(hand).getItem() == AMItems.LINE_BUTTON) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.toggleDrawLine();
					}
				}
			}
		} else {
			if (hand == EnumHand.MAIN_HAND) {
				if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH
						|| TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))
						|| playerIn.getHeldItem(hand).getItem() == AMItems.LINE_BUTTON) {
					Minecraft.getMinecraft().player.swingArm(hand);
				}
			}
		}
		return true;

	}

	private int getItemBurnTime(ItemStack stack) {
		if (stack.isEmpty()) {
			return 0;
		} else {
			Item item = stack.getItem();
			if (!item.getRegistryName().getResourceDomain().equals("minecraft")) {
				int burnTime = net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(stack);
				if (burnTime != 0) {
					return burnTime;
				}
			}

			if (item == Item.getItemFromBlock(Blocks.WOODEN_SLAB)) {
				return 160;
			} else if (item == Item.getItemFromBlock(Blocks.WOOL)) {
				return 120;
			} else if (item == Item.getItemFromBlock(Blocks.CARPET)) {
				return 80;
			} else if (item == Item.getItemFromBlock(Blocks.LADDER)) {
				return 320;
			} else if (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON)) {
				return 120;
			} else if (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD) {
				return 320;
			} else if (item == Item.getItemFromBlock(Blocks.COAL_BLOCK)) {
				return 16000;
			} else if (item instanceof ItemTool && "WOOD".equals(((ItemTool) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemSword && "WOOD".equals(((ItemSword) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe) item).getMaterialName())) {
				return 200;
			} else if (item == Items.STICK) {
				return 120;
			} else if (item != Items.BOW && item != Items.FISHING_ROD) {
				if (item == Items.SIGN) {
					return 200;
				} else if (item == Items.COAL) {
					return 1600;
				} else if (item == Items.LAVA_BUCKET) {
					return 20000;
				} else if (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL) {
					if (item == Items.BLAZE_ROD) {
						return 2400;
					} else if (item instanceof ItemDoor && item != Items.IRON_DOOR) {
						return 200;
					} else {
						return item instanceof ItemBoat ? 400 : 0;
					}
				} else {
					return 120;
				}
			} else {
				return 320;
			}
		}
	}

	@Override
	public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
				TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
				for (int i = 0; i < te.TORCHES; i++) {
					worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(),
							new ItemStack(Blocks.TORCH)));
				}
				for (int x = 0; x < te.BLOCKS; x++) {
					worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(),
							new ItemStack(Blocks.COBBLESTONE)));
				}
			}
		}
		super.breakBlock(worldIn, pos, state);
	}

	@Override
	public boolean hasTileEntity() {
		return true;
	}

	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityAutoMiner();
	}

	@Override
	public boolean hasTileEntity(IBlockState state) {
		return true;
	}
}

TileEntity.

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;
	public boolean UPDATE = false;
	public int TICKS = 0;
	public int TORCHES = 0;
	public int PLACE_TORCH = 0;
	public int BLOCKS = 0;
	public boolean DRAW_LINE = false;
	public BlockPos BEGIN_LINE_POS = new BlockPos(0, 0, 0);
	public int DRAW_LINE_X = 0;
	public int DRAW_LINE_Y = 0;
	public int DRAW_LINE_Z = 0;

	public TileEntityAutoMiner() {
	}

	public void addFuel(int amount) {
		FUEL_AMOUNT += amount;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeFuel() {
		if (FUEL_AMOUNT > 0) {
			FUEL_AMOUNT--;
			IBlockState state = world.getBlockState(pos);
			world.markBlockRangeForRenderUpdate(pos, pos);
			markDirty();
		}
	}

	public void addTorch() {
		TORCHES++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeTorches(int amount) {
		TORCHES -= amount;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void addPlaceTorch() {
		PLACE_TORCH++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removePlaceTorches() {
		PLACE_TORCH = 0;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void addBlock() {
		BLOCKS++;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeBlock() {
		BLOCKS--;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void setActivated(boolean active) {
		ACTIVATED = active;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		world.notifyBlockUpdate(pos, state, state, 3);
		world.scheduleBlockUpdate(pos, getBlockType(), 0, 0);
	}

	public void addTick() {
		TICKS++;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void removeTicks() {
		TICKS = 0;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void toggleDrawLine() {
		DRAW_LINE = !DRAW_LINE;
		System.out.println(DRAW_LINE);
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	public void setLinePos(BlockPos pos) {
		BEGIN_LINE_POS = pos;
		DRAW_LINE_X = pos.getX();
		DRAW_LINE_Y = pos.getY();
		DRAW_LINE_Z = pos.getZ();
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", FUEL_AMOUNT);
		compound.setBoolean("Activated", ACTIVATED);
		compound.setInteger("Ticks", TICKS);
		compound.setInteger("Torches", TORCHES);
		compound.setInteger("PlaceTorch", PLACE_TORCH);
		compound.setInteger("Blocks", BLOCKS);
		compound.setBoolean("DrawLine", DRAW_LINE);
		compound.setInteger("PosX", DRAW_LINE_X);
		compound.setInteger("PosY", DRAW_LINE_Y);
		compound.setInteger("PosZ", DRAW_LINE_Z);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		FUEL_AMOUNT = compound.getInteger("FuelAmount");
		ACTIVATED = compound.getBoolean("Activated");
		TICKS = compound.getInteger("Ticks");
		TORCHES = compound.getInteger("Torches");
		PLACE_TORCH = compound.getInteger("PlaceTorch");
		BLOCKS = compound.getInteger("Blocks");
		DRAW_LINE = compound.getBoolean("DrawLine");
		DRAW_LINE_X = compound.getInteger("PosX");
		DRAW_LINE_Y = compound.getInteger("PosY");
		DRAW_LINE_Z = compound.getInteger("PosZ");
		BEGIN_LINE_POS = new BlockPos(DRAW_LINE_X, DRAW_LINE_Y, DRAW_LINE_Z);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			addTick();
			UPDATE = true;
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				setActivated(false);
				markDirty();
			}
			if (TICKS == 20) {
				if (!world.isRemote) {
					EnumFacing facing = world.getBlockState(pos).getValue(BlockAutoMiner.FACING);
					EnumFacing facingL = facing.rotateYCCW();
					EnumFacing facingR = facing.rotateY();
					Random random = new Random();

					BlockPos positionC = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					BlockPos positionL = positionC.offset(facingL);
					BlockPos positionR = positionC.offset(facingR);
					BlockPos positionCU = positionC.up();
					BlockPos positionLU = positionL.up();
					BlockPos positionRU = positionR.up();
					BlockPos positionCUU = positionCU.up();
					BlockPos positionLUU = positionLU.up();
					BlockPos positionRUU = positionRU.up();

					IBlockState stateC = world.getBlockState(positionC);
					IBlockState stateL = world.getBlockState(positionL);
					IBlockState stateR = world.getBlockState(positionR);
					IBlockState stateCU = world.getBlockState(positionCU);
					IBlockState stateLU = world.getBlockState(positionLU);
					IBlockState stateRU = world.getBlockState(positionRU);
					IBlockState stateCUU = world.getBlockState(positionCUU);
					IBlockState stateLUU = world.getBlockState(positionLUU);
					IBlockState stateRUU = world.getBlockState(positionRUU);

					List<ItemStack> stackC = stateC.getBlock().getDrops(world, positionC, stateC, 0);
					List<ItemStack> stackL = stateL.getBlock().getDrops(world, positionL, stateL, 0);
					List<ItemStack> stackR = stateR.getBlock().getDrops(world, positionR, stateR, 0);
					List<ItemStack> stackCU = stateCU.getBlock().getDrops(world, positionCU, stateCU, 0);
					List<ItemStack> stackLU = stateLU.getBlock().getDrops(world, positionLU, stateLU, 0);
					List<ItemStack> stackRU = stateRU.getBlock().getDrops(world, positionRU, stateRU, 0);
					List<ItemStack> stackCUU = stateCUU.getBlock().getDrops(world, positionCUU, stateCUU, 0);
					List<ItemStack> stackLUU = stateLUU.getBlock().getDrops(world, positionLUU, stateLUU, 0);
					List<ItemStack> stackRUU = stateRUU.getBlock().getDrops(world, positionRUU, stateRUU, 0);

					dropItems(stackC, stateC, positionC);
					dropItems(stackL, stateL, positionL);
					dropItems(stackR, stateR, positionR);
					dropItems(stackCU, stateCU, positionCU);
					dropItems(stackLU, stateLU, positionLU);
					dropItems(stackRU, stateRU, positionRU);
					dropItems(stackCUU, stateCUU, positionCUU);
					dropItems(stackLUU, stateLUU, positionLUU);
					dropItems(stackRUU, stateRUU, positionRUU);

					setBlockToAir(stateC, positionC);
					setBlockToAir(stateL, positionL);
					setBlockToAir(stateR, positionR);
					setBlockToAir(stateCU, positionCU);
					setBlockToAir(stateLU, positionLU);
					setBlockToAir(stateRU, positionRU);
					setBlockToAir(stateCUU, positionCUU);
					setBlockToAir(stateLUU, positionLUU);
					setBlockToAir(stateRUU, positionRUU);

					if ((TORCHES >= 2) && (PLACE_TORCH >= 6)) {
						BlockPos positionLLU = positionLU.offset(facingL);
						BlockPos positionRRU = positionRU.offset(facingR);

						if (world.getBlockState(positionLLU).isSideSolid(world, positionLLU, facingR)
								&& (world.getBlockState(positionLU).getMaterial() == Material.AIR)) {
							if (world.setBlockState(positionLU,
									Blocks.TORCH.getDefaultState().withProperty(BlockTorch.FACING, facingR))) {
								removeTorches(1);
								removePlaceTorches();
							}
						}
						if (world.getBlockState(positionRRU).isSideSolid(world, positionRRU, facingL)
								&& (world.getBlockState(positionRU).getMaterial() == Material.AIR)) {
							if (world.setBlockState(positionRU,
									Blocks.TORCH.getDefaultState().withProperty(BlockTorch.FACING, facingL))) {
								removeTorches(1);
								removePlaceTorches();
							}
						}
					} else {
						addPlaceTorch();
					}

					BlockPos positionD = positionC.down();
					IBlockState stateD = world.getBlockState(positionD);
					if (BLOCKS > 0) {
						if (!stateD.isTopSolid()) {
							world.setBlockState(positionD, Blocks.COBBLESTONE.getDefaultState());
							removeBlock();
						}
					} else {
						if (!stateD.isTopSolid()) {
							setActivated(false);
							removeTicks();
						}
					}
					if (!world.getBlockState(pos.down()).isTopSolid()) {
						setActivated(false);
						removeTicks();
					}
					if (world.getBlockState(positionC).getMaterial().isSolid()) {
						setActivated(false);
						removeTicks();
					}
				}
			}
			if (TICKS == 40) {
				if (!world.isRemote) {
					BlockPos position = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					BlockPos position1 = pos;
					world.setBlockState(position, world.getBlockState(pos));
					TileEntityAutoMiner te = new TileEntityAutoMiner();
					te.ACTIVATED = ACTIVATED;
					te.FUEL_AMOUNT = FUEL_AMOUNT;
					te.TICKS = TICKS;
					te.UPDATE = UPDATE;
					te.TORCHES = TORCHES;
					te.PLACE_TORCH = PLACE_TORCH;
					te.BLOCKS = BLOCKS;
					te.BEGIN_LINE_POS = BEGIN_LINE_POS;
					te.DRAW_LINE = DRAW_LINE;
					te.DRAW_LINE_X = DRAW_LINE_X;
					te.DRAW_LINE_Y = DRAW_LINE_Y;
					te.DRAW_LINE_Z = DRAW_LINE_Z;
					ACTIVATED = false;
					TICKS = 0;
					UPDATE = false;
					TORCHES = 0;
					PLACE_TORCH = 0;
					BLOCKS = 0;
					BEGIN_LINE_POS = new BlockPos(0, 0, 0);
					DRAW_LINE = false;
					DRAW_LINE_X = 0;
					DRAW_LINE_Y = 0;
					DRAW_LINE_Z = 0;
					world.setTileEntity(position, te);
					world.setBlockToAir(position1);
				}
			}
		} else {
			if (UPDATE) {
				IBlockState state = world.getBlockState(pos);
				world.markBlockRangeForRenderUpdate(pos, pos);
			}
			UPDATE = false;
		}
		if (TICKS >= 40) {
			removeTicks();
		}	
	}

	private void dropItems(List<ItemStack> stackList, IBlockState state, BlockPos pos) {
		for (int i = 0; i < stackList.size(); i++) {
			ItemStack stack = stackList.get(i);
			if ((stack.getItem() instanceof ItemBlock)
					&& ((Block.getBlockFromItem(stack.getItem()).getBlockHardness(state, world, pos) < 0F)
							|| (Block.getBlockFromItem(stack.getItem()).getBlockHardness(state, world, pos) > 30F))) {
				continue;
			}
			world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack));
		}
	}

	private void setBlockToAir(IBlockState state, BlockPos pos) {
		if (!(state.getBlockHardness(world, pos) > 30F) && !(state.getBlockHardness(world, pos) < 0F)) {
			world.setBlockToAir(pos);
		}
	}
}

TESR.

public class TESRAutoMiner extends TileEntitySpecialRenderer<TileEntityAutoMiner> {

	@Override
	public void render(TileEntityAutoMiner te, double x, double y, double z, float partialTicks, int destroyStage,
			float alpha) {
		super.render(te, x, y, z, partialTicks, destroyStage, alpha);
		if (te.DRAW_LINE) {
			GL11.glPushMatrix();
			GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
			GL11.glTranslated(-te.getPos().getX(), -te.getPos().getY(), -te.getPos().getZ());
			GL11.glDisable(GL11.GL_LIGHTING);
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			drawLine(new Vec3d(te.DRAW_LINE_X, te.DRAW_LINE_Y, te.DRAW_LINE_Z), new Vec3d(te.getPos()));
			GL11.glPopAttrib();
			GL11.glPopMatrix();
		}
	}

	private void drawLine(Vec3d blockA, Vec3d blockB) {
		Tessellator tess = Tessellator.getInstance();
		BufferBuilder buff = tess.getBuffer();
		blockB.addVector(0, 1, 0);
		blockB.addVector(0, -1, 0);
		blockA.addVector(0, -0.02f, 0);
		buff.begin(7, DefaultVertexFormats.POSITION_COLOR);
		buff.color(0F, 1F, 1F, 1F);

		Vec3d recLong = blockA.subtract(blockB);
		Vec3d perpendicular = Vec3d.fromPitchYaw(1.0F, 1.0F);
		perpendicular = perpendicular.normalize();

		float Width = 1f / 16f;

		Vec3d R1 = blockA.subtract(blockB);
		Vec3d R2 = blockA.subtract(blockB);
		Vec3d R3 = blockA.subtract(blockB);
		Vec3d R4 = blockA.subtract(blockB);

		R1.addVector(blockA.x + perpendicular.x * Width, 0, 0);
		R1.addVector(0, 0, blockA.z + perpendicular.z * Width);
		R2.addVector(blockA.x - perpendicular.x * Width, 0, 0);
		R2.addVector(0, 0, blockA.z - perpendicular.z * Width);
		R1.addVector(0, blockA.y - 0.01, 0);
		R2.addVector(0, blockA.y - 0.01, 0);

		R3.addVector(blockB.x + perpendicular.x * Width, 0, 0);
		R3.addVector(0, 0, blockB.z + perpendicular.z * Width);
		R4.addVector(blockB.x - perpendicular.x * Width, 0, 0);
		R4.addVector(0, 0, blockB.z - perpendicular.z * Width);
		R3.addVector(0, blockB.y + 0.75, 0);
		R4.addVector(0, blockB.y + 0.75, 0);

		int[] a = { (int) (R1.x + 0.5), (int) R1.y, (int) (R1.z + 0.5) };
		int[] b = { (int) (R3.x + 0.5), (int) R3.y, (int) (R3.z + 0.5) };
		int[] c = { (int) (R4.x + 0.5), (int) R4.y, (int) (R4.z + 0.5) };
		int[] d = { (int) (R2.x + 0.5), (int) R2.y, (int) (R2.z + 0.5) };

		buff.addVertexData(a);
		buff.addVertexData(b);
		buff.addVertexData(c);
		buff.addVertexData(d);

		tess.draw();
	}

}

 

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

 

31 minutes ago, Kokkie said:

GL11

Do not use GL11 methods directly. Use GlStateManager. Using GL directly messes up with GlStateManager's flags and can screw some things up.

 

32 minutes ago, Kokkie said:

GL11.glTranslated(-te.getPos().getX(), -te.getPos().getY(), -te.getPos().getZ());

Why are you offsetting the matrix by TE's negative position? That is not a good idea, there is a reason these

33 minutes ago, Kokkie said:

double x, double y, double z

parameters exist. They are used as your render coordinates. Use them.

 

34 minutes ago, Kokkie said:

GL11.glPushAttrib(GL11.GL_ENABLE_BIT);

Why?

 

34 minutes ago, Kokkie said:

buff.begin(7, DefaultVertexFormats.POSITION_COLOR);

7 is GL_QUADS. If you are drawing a line GL_LINES(1) makes much more sense.

 

35 minutes ago, Kokkie said:

buff.addVertexData(a);

This adds raw data to the buffer. Unless you know how to use this you should not be doing that. Use BufferBuilder::pos to specify position, BufferBuilder::color to specify color and BufferBuilder::endVertex to end the current vertex data.

 

Vec3d start = new Vec3d(te.getPos().getX(), te.getPos().getY(), te.getPos().getZ());
Vec3d end = start.addVector(0, 10, 0);
Vec3d posDiff = end.subtract(start);
GlStateManager.pushMatrix();
GlStateManager.glLineWidth(1F);
GlStateManager.disableTexture2D();
BufferBuilder bb = Tessellator.getInstance().getBuffer();
bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
bb.pos(x, y, z).color(1, 1, 1, 1F).endVertex();
bb.pos(posDiff.x, posDiff.y, posDiff.z).color(1, 1, 1, 1F).endVertex();
Tessellator.getInstance().draw();
GlStateManager.enableTexture2D();
GlStateManager.popMatrix();

This is a super-quick(and a bit messy) example that draws the line in 4! calls 2 of which are begin and draw calls. Everything above and below are gl setups/state restoring. I could've even dropped push/pop matrix here as I am not doing anything with the matrix.

 

If you need something more complex than a simple GL line(a line with a texture, more controllable line, rotating line, etc) I suggest you to look at vanilla examples like TileEntityBeaconRenderer or RenderGuardian. The later will need to be adapted to be a TESR but will work just fine.

Link to comment
Share on other sites

1 hour ago, V0idWa1k3r said:

7 is GL_QUADS. If you are drawing a line GL_LINES(1) makes much more sense.

GL_LINES are 1 pixel thick lines (and may not render at all). Quads is better.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

11 minutes ago, Draco18s said:

GL_LINES are 1 pixel thick lines (and may not render at all). Quads is better.

You can specify the width of the line with GlStateManager::glLineWidth. Quads are better for more control over the rendering process, as they are quads and can be positioned/rotated/textured as you wish, but lines work just fine for simple things. For example vanilla uses LINE_STRIP(which is a variation GL_LINES) do draw the bounding box over the block you are looking at and it works perfect. Lines and quads are just used for different purposes ;)

 

29 minutes ago, Kokkie said:

Thanks, but now how do I change the pos it draws the line to (from the te pos)?

You calculate the vector that points from your TE to the point you want the line to go to and use your start vector + that vector as your end coordinates.

 

Edited by V0idWa1k3r
Link to comment
Share on other sites

23 minutes ago, V0idWa1k3r said:

You can specify the width of the line with GlStateManager::glLineWidth. Quads are better for more control over the rendering process, as they are quads and can be positioned/rotated/textured as you wish, but lines work just fine for simple things. For example vanilla uses LINE_STRIP(which is a variation GL_LINES) do draw the bounding box over the block you are looking at and it works perfect. Lines and quads are just used for different purposes ;)

I've had problems with it in the past, mainly by either not rendering anything at all, or by ignoring the line width specified.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Why wouldn't this work?

			Vec3d start = new Vec3d(te.getPos());
			Vec3d end = new Vec3d(te.BEGIN_LINE_POS);
			GlStateManager.disableTexture2D();
			BufferBuilder bb = Tessellator.getInstance().getBuffer();
			bb.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION_COLOR);
			bb.pos(start.x + 0.5, start.y + 0.5, start.z + 0.5).color(0, 1, 1, 1F).endVertex();
			bb.pos(end.x, end.y, end.z).color(0, 1, 1, 1F).endVertex();
			Tessellator.getInstance().draw();
			GlStateManager.enableTexture2D();

Can someone explain?

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

You are still using your TE's positions as the start of the drawing vector. Use the x/y/z parameters from the draw method.

Your end vector must be a drawing start vector + delta vector of your TE's position and the end position of the line. If your line goes 10 blocks up from your TE it will be [x + 0, y + 10, z + 0]. 

Edited by V0idWa1k3r
Link to comment
Share on other sites

So.. This?

			Vec3i start = new Vec3i(x, y, z);
			Vec3d end = new Vec3d(te.BEGIN_LINE_POS.subtract(start));
			GlStateManager.disableTexture2D();
			BufferBuilder bb = Tessellator.getInstance().getBuffer();
			bb.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION_COLOR);
			bb.pos(x + 0.5, y + 0.5, z + 0.5).color(0, 1, 1, 1F).endVertex();
			bb.pos(end.x + 0.5, end.y + 0.5, end.z + 0.5).color(0, 1, 1, 1F).endVertex();
			Tessellator.getInstance().draw();
			GlStateManager.enableTexture2D();

It for some reason draws a line from the block to a random place...

Edited by Kokkie

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Almost. 

13 minutes ago, Kokkie said:

Vec3d end = new Vec3d(te.BEGIN_LINE_POS.subtract(start));

You should not subtract the start vector as the start vector is the start of the line for rendering. Subtract your TE's position instead - that is the logical start of your line. 

Link to comment
Share on other sites

Changed it to this.

			Vec3d start = new Vec3d(x, y, z);
			Vec3d end = new Vec3d(te.BEGIN_LINE_POS.subtract(te.getPos()));
			GlStateManager.disableTexture2D();
			BufferBuilder bb = Tessellator.getInstance().getBuffer();
			bb.begin(GL11.GL_LINE_STRIP, DefaultVertexFormats.POSITION_COLOR);
			bb.pos(x + 0.5, y + 0.5, z + 0.5).color(0, 1, 1, 1F).endVertex();
			bb.pos(end.x + 0.5, end.y + 0.5, end.z + 0.5).color(0, 1, 1, 1F).endVertex();
			Tessellator.getInstance().draw();
			GlStateManager.enableTexture2D();

Doesn't work.

Edited by Kokkie

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Looks correct. What is 

8 minutes ago, Kokkie said:

te.BEGIN_LINE_POS

? Is it pointing to fixed coordinates in the world? Is it being synced to the client?

8 minutes ago, Kokkie said:

new Vec3d(te.BEGIN_LINE_POS.subtract(te.getPos()));

=>

te.BEGIN_LINE_POS.subtract(te.getPos()); Vec3d::subtract already returns a new Vec3d object.

 

EDIT: there was an issue in my example. This line:

bb.pos(posDiff.x, posDiff.y, posDiff.z).color(1, 1, 1, 1F).endVertex();

is supposed to be

bb.pos(x + posDiff.x, y + posDiff.y, z + posDiff.z).color(1, 1, 1, 1F).endVertex();

As you need to specify the end position relative to render coordinates aswell, my bad. Change that line in your code too and it 'should' work provided that everything else is correct.

Edited by V0idWa1k3r
Link to comment
Share on other sites

So, now I want to add a block outline of where it used to be, but it doesn't, also the line doesn't work anymore either.

public class TESRAutoMiner extends TileEntitySpecialRenderer<TileEntityAutoMiner> {
	@Override
	public void render(TileEntityAutoMiner te, double x, double y, double z, float partialTicks, int destroyStage,
			float alpha) {
		if (te.DRAW_LINE) {
			BlockPos pos = new BlockPos(x, y, z);
			AMUtils.drawLineMiddle(te.getPos(), te.BEGIN_LINE_POS, pos);
			AMUtils.drawBox(te.BEGIN_LINE_POS, pos);
		}
	}
}
public class AMUtils {
	public static void drawLineMiddle(BlockPos start, BlockPos end, BlockPos pos) {
		BlockPos start1 = start.add(0.5, 0.5, 0.5);
		BlockPos end1 = end.add(0.5, 0.5, 0.5);
		drawLine(start1, end1, pos);
	}

	private static void drawLine(BlockPos start, BlockPos end, BlockPos pos) {
		Vec3d start1 = new Vec3d(start);
		Vec3d end1 = new Vec3d(end);
		Vec3d posDiff = end1.subtract(start1);
		GlStateManager.pushMatrix();
		GlStateManager.glLineWidth(2F);
		GlStateManager.disableTexture2D();
		GlStateManager.disableLighting();
		BufferBuilder bb = Tessellator.getInstance().getBuffer();
		bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		bb.pos(pos.getX(), pos.getY(), pos.getZ()).color(0, 1, 0, 1F).endVertex();
		bb.pos(pos.getX() + posDiff.x, pos.getY() + posDiff.y, pos.getZ() + posDiff.z).color(0, 1, 0, 1F).endVertex();
		Tessellator.getInstance().draw();
		GlStateManager.enableLighting();
		GlStateManager.enableTexture2D();
		GlStateManager.popMatrix();
	}

	public static void drawBox(BlockPos block, BlockPos pos) {
		drawLine(block, block.add(1, 0, 0), pos);
		drawLine(block, block.add(0, 1, 0), pos);
		drawLine(block, block.add(0, 0, 1), pos);
		
		drawLine(block.add(1, 0, 0), block.add(1, 1, 0), pos);
		drawLine(block.add(1, 0, 0), block.add(1, 0, 1), pos);
		
		drawLine(block.add(0, 1, 0), block.add(1, 1, 0), pos);
		drawLine(block.add(0, 1, 0), block.add(0, 1, 1), pos);
		
		drawLine(block.add(0, 0, 1), block.add(1, 0, 1), pos);
		drawLine(block.add(0, 0, 1), block.add(0, 1, 1), pos);
		
		drawLine(block.add(1, 1, 0), block.add(1, 1, 1), pos);
		drawLine(block.add(1, 0, 1), block.add(1, 1, 1), pos);
		drawLine(block.add(0, 1, 1), block.add(1, 1, 1), pos);
	}
}

 

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

1 minute ago, Kokkie said:

BlockPos pos = new BlockPos(x, y, z);

You can't pass the x/y/z like that. BlockPos rounds anything you pass to it to an integer. For rendering the double precision matters.

If you want to render an outline around a block I recommend not reinventing the wheel and seeing how vanilla does it at RenderGlobal::drawSelectionBoundingBox/RenderGlobal::drawBoundingBox. That method even is public so you can just offset the matrix/BufferBuilder, call it and you are done.

I would also recommend debugging if your methods are called at all. 

Link to comment
Share on other sites

I now have this.

public class TESRAutoMiner extends TileEntitySpecialRenderer<TileEntityAutoMiner> {
	@Override
	public void render(TileEntityAutoMiner te, double x, double y, double z, float partialTicks, int destroyStage,
			float alpha) {
		if (te.DRAW_LINE) {
			AMUtils.drawLineMiddle(te.getPos(), te.BEGIN_LINE_POS, x, y, z);
			AMUtils.drawBox(te.BEGIN_LINE_POS, x, y, z);
		}
	}
	
	@Override
	public boolean isGlobalRenderer(TileEntityAutoMiner te) {
		return true;
	}
}
public class AMUtils {
	public static void drawLineMiddle(BlockPos start, BlockPos end, double x, double y, double z) {
		drawLine(start, end, x + 0.5, y + 0.5, z + 0.5);
	}

	private static void drawLine(BlockPos start, BlockPos end, double x, double y, double z) {
		Vec3d start1 = new Vec3d(start);
		Vec3d end1 = new Vec3d(end);
		Vec3d posDiff = end1.subtract(start1);
		GlStateManager.pushMatrix();
		GlStateManager.glLineWidth(2F);
		GlStateManager.disableTexture2D();
		GlStateManager.disableLighting();
		BufferBuilder bb = Tessellator.getInstance().getBuffer();
		bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		bb.pos(x, y, z).color(1, 0, 0, 1F).endVertex();
		bb.pos(x + posDiff.x, y + posDiff.y, z + posDiff.z).color(1, 0, 0, 1F).endVertex();
		Tessellator.getInstance().draw();
		GlStateManager.enableLighting();
		GlStateManager.enableTexture2D();
		GlStateManager.popMatrix();
	}

	public static void drawBox(BlockPos block, double x, double y, double z) {
		double minX = block.getX();
		double minY = block.getY();
		double minZ = block.getZ();
		double maxX = block.getX() + 1;
		double maxY = block.getY() + 1;
		double maxZ = block.getZ() + 1;
		GlStateManager.enableBlend();
        GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
        GlStateManager.glLineWidth(2.0F);
        GlStateManager.disableTexture2D();
        GlStateManager.depthMask(false);
		RenderGlobal.drawBoundingBox(minX, minY, minZ, maxX, maxY, maxZ, 1, 0, 0, 1);
        GlStateManager.depthMask(true);
        GlStateManager.enableTexture2D();
        GlStateManager.disableBlend();
	}
}

It renders the line, but doesn't render the box.

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Before drawing the box using RenderGlobal's method you must offset your BufferBuilder or GL's matrix to where you actually want to render the box at. RenderGlobal renders a [minX, minY, minZ] to [maxX, maxY, maxZ] box. You are currently offseting by x/y/z of the BlockPos in the world, and you must offset by render x/y/z + delta vector of the BlockPos in the world and your TE's pos, similar to how you draw a line. 

Link to comment
Share on other sites

I've sort of got it working, it only draws 4 lines instead of 12... Only the top Z lines and bottom X lines.

public class TESRAutoMiner extends TileEntitySpecialRenderer<TileEntityAutoMiner> {
	@Override
	public void render(TileEntityAutoMiner te, double x, double y, double z, float partialTicks, int destroyStage,
			float alpha) {
		if (te.DRAW_LINE) {
			AMUtils.drawLineMiddle(te.getPos(), te.BEGIN_LINE_POS, x, y, z);
			AMUtils.drawBox(te.getPos(), te.BEGIN_LINE_POS, x, y, z);
		}
	}
	
	@Override
	public boolean isGlobalRenderer(TileEntityAutoMiner te) {
		return true;
	}
}
public class AMUtils {
	public static void drawLineMiddle(BlockPos start, BlockPos end, double x, double y, double z) {
		drawLine(start, end, x + 0.5, y + 0.5, z + 0.5);
	}

	private static void drawLine(BlockPos start, BlockPos end, double x, double y, double z) {
		Vec3d start1 = new Vec3d(start);
		Vec3d end1 = new Vec3d(end);
		Vec3d posDiff = end1.subtract(start1);
		GlStateManager.pushMatrix();
		GlStateManager.glLineWidth(2F);
		GlStateManager.disableTexture2D();
		GlStateManager.disableLighting();
		BufferBuilder bb = Tessellator.getInstance().getBuffer();
		bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		bb.pos(x, y, z).color(1, 0, 0, 1F).endVertex();
		bb.pos(x + posDiff.x, y + posDiff.y, z + posDiff.z).color(1, 0, 0, 1F).endVertex();
		Tessellator.getInstance().draw();
		GlStateManager.enableLighting();
		GlStateManager.enableTexture2D();
		GlStateManager.popMatrix();
	}

	public static void drawBox(BlockPos start, BlockPos end, double x, double y, double z) {
		Vec3d start1 = new Vec3d(start);
		Vec3d end1 = new Vec3d(end);
		Vec3d posDiff = end1.subtract(start1);
		double minX = x + posDiff.x;
		double minY = y + posDiff.y;
		double minZ = z + posDiff.z;
		double maxX = x + posDiff.x + 1;
		double maxY = y + posDiff.y + 1;
		double maxZ = z + posDiff.z + 1;
		GlStateManager.pushMatrix();
		GlStateManager.glLineWidth(2F);
		GlStateManager.disableTexture2D();
		GlStateManager.disableLighting();
		BufferBuilder bb = Tessellator.getInstance().getBuffer();
		bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
		RenderGlobal.drawBoundingBox(bb, minX, minY, minZ, maxX, maxY, maxZ, 1, 0, 0, 1);
		Tessellator.getInstance().draw();
		GlStateManager.enableLighting();
		GlStateManager.enableTexture2D();
		GlStateManager.popMatrix();
	}
}

Also, how can I make it so it renders even if you can't see the block, like beacons?

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

2 minutes ago, Kokkie said:

bb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);

If you look at RenderGlobal::drawBoundingBox method you will see that it uses a different GL mode of 3, which is GL_LINE_STRIP. The difference is that LINES requires you to specify a beginning an an end for each new line you draw, where LINE_STRIP assumes that the end of the last line is the beginning of the new one. You are not using the correct GL mode here.

 

4 minutes ago, Kokkie said:

Also, how can I make it so it renders even if you can't see the block, like beacons?

You know they say that a good question already contains an answer? This is one very good question. You know that there is a block in game that does X. The next logical step - lookup how it does X in it's code. In your case you need to override TileEntity::getRenderBoundingBox and possibly TileEntity::getMaxRenderDistanceSquared if you want your tile to be drawn more than 64 blocks away.

Link to comment
Share on other sites

This is something you will need to use QUADS for. You need to specify 4 vertices per quad(4 corners) and 6 quads(faces) make up a cube if positioned correctly. The ordering(CW vs CCW) also matters. You might need to experiment with it a bit to get the desired results. Vanilla uses models to abstract from direct rendering so I can't quite provide an example of direct rendering here.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.